This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
CMakeLists.txt
245 lines (208 loc) · 10.9 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
cmake_minimum_required(VERSION 3.5)
message(STATUS "Running cmake version ${CMAKE_VERSION}")
if (NOT CMAKE_TOOLCHAIN_FILE)
set(CMAKE_TOOLCHAIN_FILE "../cmake/win64.toolchain" CACHE STRING "default toolchain" FORCE)
endif()
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "default to release build type" FORCE)
endif()
project(win-gpg-agent NONE)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "Generator: ${CMAKE_GENERATOR}")
# Release installation directory
if(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux" OR NOT CMAKE_CROSSCOMPILING)
message(FATAL_ERROR "Currently unsuppored host environment \"${CMAKE_HOST_SYSTEM_NAME}\". CMake willl exit now.")
else()
set(HOST_GOOS linux)
set(HOST_GOARCH amd64)
endif()
if(MSYSTEM_NAME STREQUAL "win32")
set(CROSS_GOOS windows)
set(CROSS_GOARCH 386)
message(STATUS "Targeting 32 bits Windows executables")
elseif(MSYSTEM_NAME STREQUAL "win64")
set(CROSS_GOOS windows)
set(CROSS_GOARCH amd64)
message(STATUS "Targeting 64 bits Windows executables")
else()
message(FATAL_ERROR "Currently unsuppored environment \"${MSYSTEM_NAME}\". CMake willl exit now.")
endif()
set(GO_MIN_REQURED_VERSION 1.17)
find_package(Go ${GO_MIN_REQURED_VERSION} REQUIRED)
find_package(Git REQUIRED)
# Make sure we setup go properly
if (DEFINED ENV{GOPATH})
set(GO_PATH "$ENV{GOPATH}")
else()
set(GO_PATH "/tmp/gopkg-${CMAKE_PROJECT_NAME}")
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${GO_PATH})
endif()
message(STATUS "Using go modules with GOPATH=${GO_PATH}")
if(GO_PATH MATCHES "^${PROJECT_SOURCE_DIR}.*")
message(FATAL_ERROR "We are working in \"go modules outside of GOPATH mode\" - GOPATH should be outside of project source directory!")
endif()
########################################################################################################
# Setting up versioning
########################################################################################################
# Project version number
set(PRJ_VERSION_Major "1")
set(PRJ_VERSION_Minor "6")
set(PRJ_VERSION_Patch "3")
if (EXISTS "${PROJECT_SOURCE_DIR}/.git" AND IS_DIRECTORY "${PROJECT_SOURCE_DIR}/.git")
execute_process(COMMAND ${CMAKE_SOURCE_DIR}/cmake/githash.sh ${GIT_EXECUTABLE}
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
RESULT_VARIABLE res
OUTPUT_VARIABLE GIT_HASH
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
# This will cause reconfiguration on every commit
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/.git/index")
else()
set(GIT_HASH "no-git")
endif()
configure_file("${PROJECT_SOURCE_DIR}/cmake/version.go.in" "${PROJECT_SOURCE_DIR}/misc/version.go")
configure_file("${PROJECT_SOURCE_DIR}/cmake/agent.rc.in" "${PROJECT_SOURCE_DIR}/cmd/agent/resources.rc")
configure_file("${PROJECT_SOURCE_DIR}/cmake/agent.xml.in" "${PROJECT_SOURCE_DIR}/cmd/agent/manifest.xml")
configure_file("${PROJECT_SOURCE_DIR}/cmake/pinentry.rc.in" "${PROJECT_SOURCE_DIR}/cmd/pinentry/resources.rc")
configure_file("${PROJECT_SOURCE_DIR}/cmake/pinentry.xml.in" "${PROJECT_SOURCE_DIR}/cmd/pinentry/manifest.xml")
configure_file("${PROJECT_SOURCE_DIR}/cmake/sorelay.rc.in" "${PROJECT_SOURCE_DIR}/cmd/sorelay/resources.rc")
configure_file("${PROJECT_SOURCE_DIR}/cmake/sorelay.xml.in" "${PROJECT_SOURCE_DIR}/cmd/sorelay/manifest.xml")
configure_file("${PROJECT_SOURCE_DIR}/cmake/win-gpg-agent.json.in" "${PROJECT_SOURCE_DIR}/win-gpg-agent.json")
# distribute history
execute_process(COMMAND ${CMAKE_COMMAND} -E env ${GIT_EXECUTABLE} changelog --all --tag "v${PRJ_VERSION_Major}.${PRJ_VERSION_Minor}.${PRJ_VERSION_Patch}" --stdout OUTPUT_FILE ${PROJECT_BINARY_DIR}/changelog.txt ERROR_QUIET)
message(STATUS "Building version \"${PRJ_VERSION_Major}.${PRJ_VERSION_Minor}.${PRJ_VERSION_Patch}\" git \"${GIT_HASH}\"")
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
# Create git pre-commit hook to check formatting before commit
if(EXISTS "${PROJECT_SOURCE_DIR}/.git" AND IS_DIRECTORY "${PROJECT_SOURCE_DIR}/.git")
execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/cmake/pre-commit ${PROJECT_SOURCE_DIR}/.git/hooks/. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
endif()
endif()
########################################################################################################
# Some contexts dependent settings
########################################################################################################
list(APPEND GO_ENV
GOPATH=${GO_PATH}
GOOS=${CROSS_GOOS}
GOARCH=${CROSS_GOARCH}
)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(GO_ARGS "-mod=mod")
else()
set(GO_ARGS "-mod=vendor")
endif()
########################################################################################################
# main target - always out of date, "go build" will figure it out
########################################################################################################
add_custom_target(release
DEPENDS ${PROJECT_SOURCE_DIR}/win-gpg-agent.zip
${PROJECT_SOURCE_DIR}/win-gpg-agent.json
COMMAND ${CMAKE_SOURCE_DIR}/cmake/sign.sh
COMMENT "Building release..."
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
add_custom_command(OUTPUT ${PROJECT_SOURCE_DIR}/win-gpg-agent.zip
DEPENDS ${PROJECT_BINARY_DIR}/changelog.txt
${PROJECT_BINARY_DIR}/agent-gui${CMAKE_EXECUTABLE_SUFFIX}
${PROJECT_BINARY_DIR}/pinentry${CMAKE_EXECUTABLE_SUFFIX}
${PROJECT_BINARY_DIR}/sorelay${CMAKE_EXECUTABLE_SUFFIX}
COMMAND ${CMAKE_COMMAND} -E tar "cfv" ${PROJECT_SOURCE_DIR}/win-gpg-agent.zip --format=zip
changelog.txt agent-gui${CMAKE_EXECUTABLE_SUFFIX} pinentry${CMAKE_EXECUTABLE_SUFFIX} sorelay${CMAKE_EXECUTABLE_SUFFIX}
COMMENT "Archiving release..."
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}")
add_custom_target(bin_agent ALL
DEPENDS ${PROJECT_SOURCE_DIR}/cmd/agent/resources.syso
BYPRODUCTS ${PROJECT_BINARY_DIR}/agent-gui${CMAKE_EXECUTABLE_SUFFIX}
COMMAND ${GO_ENV} ${GO_EXECUTABLE} build -trimpath -o ${PROJECT_BINARY_DIR}/agent-gui${CMAKE_EXECUTABLE_SUFFIX}
-ldflags='-H=windowsgui'
${GO_ARGS}
./cmd/agent
COMMENT "Building agent-gui..."
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
add_custom_command(OUTPUT ${PROJECT_SOURCE_DIR}/cmd/agent/resources.syso
DEPENDS ${PROJECT_SOURCE_DIR}/cmd/agent/resources.rc
${PROJECT_SOURCE_DIR}/cmd/agent/manifest.xml
${PROJECT_SOURCE_DIR}/cmd/agent/icon.ico
COMMAND ${CMAKE_RC_COMPILER} -O coff
-o ${PROJECT_SOURCE_DIR}/cmd/agent/resources.syso
-i ${PROJECT_SOURCE_DIR}/cmd/agent/resources.rc
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
COMMENT "Building agent-gui resources...")
# shortcut
add_custom_target(bin_pinentry ALL
DEPENDS ${PROJECT_BINARY_DIR}/pinentry${CMAKE_EXECUTABLE_SUFFIX}
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/pinentry${CMAKE_EXECUTABLE_SUFFIX}
DEPENDS ${PROJECT_SOURCE_DIR}/cmd/pinentry/resources.syso
COMMAND ${GO_ENV} ${GO_EXECUTABLE} build -trimpath -o ${PROJECT_BINARY_DIR}/pinentry${CMAKE_EXECUTABLE_SUFFIX}
${GO_ARGS}
./cmd/pinentry
COMMENT "Building pinentry..."
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
add_custom_command(OUTPUT ${PROJECT_SOURCE_DIR}/cmd/pinentry/resources.syso
DEPENDS ${PROJECT_SOURCE_DIR}/cmd/pinentry/resources.rc
${PROJECT_SOURCE_DIR}/cmd/pinentry/manifest.xml
${PROJECT_SOURCE_DIR}/cmd/pinentry/icon.ico
COMMAND ${CMAKE_RC_COMPILER} -O coff
-o ${PROJECT_SOURCE_DIR}/cmd/pinentry/resources.syso
-i ${PROJECT_SOURCE_DIR}/cmd/pinentry/resources.rc
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
COMMENT "Building pinentry resources...")
# shortcut
add_custom_target(bin_sorelay ALL
DEPENDS ${PROJECT_BINARY_DIR}/sorelay${CMAKE_EXECUTABLE_SUFFIX}
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/sorelay${CMAKE_EXECUTABLE_SUFFIX}
DEPENDS ${PROJECT_SOURCE_DIR}/cmd/sorelay/resources.syso
COMMAND ${GO_ENV} ${GO_EXECUTABLE} build -trimpath -o ${PROJECT_BINARY_DIR}/sorelay${CMAKE_EXECUTABLE_SUFFIX}
${GO_ARGS}
./cmd/sorelay
COMMENT "Building sorelay..."
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
add_custom_command(OUTPUT ${PROJECT_SOURCE_DIR}/cmd/sorelay/resources.syso
DEPENDS ${PROJECT_SOURCE_DIR}/cmd/sorelay/resources.rc
${PROJECT_SOURCE_DIR}/cmd/sorelay/manifest.xml
${PROJECT_SOURCE_DIR}/cmd/sorelay/icon.ico
COMMAND ${CMAKE_RC_COMPILER} -O coff
-o ${PROJECT_SOURCE_DIR}/cmd/sorelay/resources.syso
-i ${PROJECT_SOURCE_DIR}/cmd/sorelay/resources.rc
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
COMMENT "Building sorelay resources...")
########################################################################################################
# Development
########################################################################################################
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
########################################################################################################
# Go linter
########################################################################################################
add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/staticcheck
COMMAND GOPATH=${GO_PATH} GOOS= GOARCH= ${GO_EXECUTABLE} build ${GO_MOD_ARGS} -o ${PROJECT_BINARY_DIR}/staticcheck
honnef.co/go/tools/cmd/staticcheck
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
COMMENT "Building staticcheck executable...")
add_custom_target(lint
DEPENDS ${PROJECT_BINARY_DIR}/staticcheck
COMMAND ${PROJECT_BINARY_DIR}/staticcheck -f stylish -tests=false ./...
COMMENT "Linting project..."
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
########################################################################################################
# Go dependency management
########################################################################################################
add_custom_target(deps_tidy
SOURCES ${PROJECT_SOURCE_DIR}/go.mod
COMMAND ${GO_ENV} ${GO_EXECUTABLE} mod tidy -compat=${GO_MIN_REQURED_VERSION}
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
COMMENT "Cleaning dependencies (go mod tidy)"
VERBATIM)
add_custom_target(deps_download
SOURCES ${PROJECT_SOURCE_DIR}/go.mod
COMMAND ${GO_ENV} ${GO_EXECUTABLE} mod download all
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
COMMENT "Downloading dependencies (go mod download)"
VERBATIM)
add_custom_target(deps_vendor
SOURCES ${PROJECT_SOURCE_DIR}/go.mod
COMMAND ${GO_ENV} ${GO_EXECUTABLE} mod vendor
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
COMMENT "Creating vendor directory (go mod vendor)"
VERBATIM)
endif()