-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
290 lines (254 loc) · 10 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# SPDX-FileCopyrightText: Copyright 2023 Cathy J. Fitzpatrick <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
cmake_minimum_required(VERSION 3.30.5)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -flto -Wall -Werror")
set(CMAKE_OBJCXX_FLAGS "${CMAKE_OBJCXX_FLAGS} -fobjc-arc")
set(TEAM_ID "KVRBCYNMT7"
CACHE STRING "Team ID used for code signing.")
set(IDENTITY "Developer ID Application: Cathy Fitzpatrick (${TEAM_ID})"
CACHE STRING "Developer ID used for code signing.")
set(CODESIGN "${CMAKE_CURRENT_SOURCE_DIR}/src/meta/codesign.sh")
set(MAKE_BUNDLE "${CMAKE_CURRENT_SOURCE_DIR}/src/meta/make-bundle.sh")
set(MAIN_APP_BUNDLE "${CMAKE_BINARY_DIR}/keychain-interpose.app")
if(NOT CMAKE_CXX_COMPILER)
# Homebrew's version of Clang is required because we use C++ standard
# modules. Apple's Clang does not currently support the standard version
# of modules.
execute_process(
COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/src/meta/print-compiler.sh"
OUTPUT_VARIABLE CMAKE_CXX_COMPILER)
endif()
# For consistency, we need to use the `ar` binary that comes with the version
# of Clang that we're using.
get_filename_component(CLANG_CXX_DIR ${CMAKE_CXX_COMPILER} DIRECTORY)
set(CMAKE_AR "${CLANG_CXX_DIR}/llvm-ar")
project(keychain-interpose LANGUAGES CXX OBJCXX)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.86 REQUIRED COMPONENTS program_options regex)
# The Boost::regex target automatically links to icudata, icui18n, and icuuc,
# but we don't need any of those.
set_property(TARGET Boost::regex PROPERTY INTERFACE_LINK_LIBRARIES "")
###############################################################################
# libCF++
file(GLOB cfpp_src CONFIGURE_DEPENDS "dependencies/libCF++/CF++/source/*.cpp")
add_library(cfpp STATIC ${cfpp_src})
target_include_directories(cfpp PUBLIC "dependencies/libCF++/CF++/include")
###############################################################################
# keychain-interpose-common
add_library(keychain-interpose-common OBJECT)
target_sources(keychain-interpose-common PUBLIC FILE_SET CXX_MODULES FILES
"src/modules/cathyjf.ki.common.cppm")
block()
find_library(CORE_FOUNDATION_F CoreFoundation REQUIRED)
find_library(SECURITY_F Security REQUIRED)
target_link_libraries(keychain-interpose-common
PUBLIC cfpp
PUBLIC ${CORE_FOUNDATION_F}
PUBLIC ${SECURITY_F})
endblock()
###############################################################################
# encapsulate-app
add_executable(encapsulate-app
"src/encapsulate-app.cpp")
target_link_libraries(encapsulate-app
PRIVATE keychain-interpose-common
PRIVATE Boost::regex)
###############################################################################
# provisionprofile_target
add_custom_command(
OUTPUT
"keychain-interpose.provisionprofile"
COMMAND /usr/bin/base64 ARGS -d
-i "src/meta/profiles/keychain-interpose.provisionprofile.base64"
-o "${CMAKE_BINARY_DIR}/keychain-interpose.provisionprofile"
DEPENDS
"src/meta/profiles/keychain-interpose.provisionprofile.base64"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
VERBATIM
)
add_custom_target("provisionprofile_target" DEPENDS
"keychain-interpose.provisionprofile")
###############################################################################
# gpg-agent.app
set(AGENT_APP_BUNDLE_PARENT "${MAIN_APP_BUNDLE}/Contents/MacOS")
set(AGENT_APP_BUNDLE "${AGENT_APP_BUNDLE_PARENT}/gpg-agent.app")
set(AGENT_FRAMEWORKS "${AGENT_APP_BUNDLE}/Contents/Frameworks")
find_package(PkgConfig REQUIRED)
pkg_check_modules(GPG_ERROR REQUIRED gpg-error)
block(PROPAGATE AGENT_LIBGPG_ERROR)
get_filename_component(libgpg_error_realpath
"${GPG_ERROR_LIBRARY_DIRS}/lib${GPG_ERROR_LIBRARIES}.dylib" REALPATH)
get_filename_component(libgpg_error_filename ${libgpg_error_realpath} NAME)
set(AGENT_LIBGPG_ERROR "${AGENT_FRAMEWORKS}/${libgpg_error_filename}")
endblock()
add_custom_command(
OUTPUT
${AGENT_APP_BUNDLE}
${AGENT_LIBGPG_ERROR}
"${AGENT_APP_BUNDLE}/Contents/embedded.provisionprofile"
COMMAND /bin/bash ARGS -c
[[
"$1" "$(brew --prefix gnupg)"/bin/gpg-agent "$2" "$(brew --prefix)" boost
]]
"shell"
$<TARGET_FILE:encapsulate-app>
${AGENT_APP_BUNDLE}
COMMAND /bin/bash ARGS -c
[[
find "$1" -name '*.dylib' -print0 | xargs -0 -I{} "$2" "{}" "$3"
]]
"shell"
${AGENT_FRAMEWORKS}
${CODESIGN}
${IDENTITY}
COMMAND ${MAKE_BUNDLE} ARGS
"gpg-agent"
${AGENT_APP_BUNDLE_PARENT}
${CMAKE_BINARY_DIR}
${IDENTITY}
DEPENDS
${MAKE_BUNDLE}
"gpg-agent-entitlements"
"keychain-interpose.provisionprofile"
"provisionprofile_target"
"src/meta/Info.plist.m4"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
VERBATIM
)
add_custom_target("gpg-agent_target" DEPENDS
${AGENT_APP_BUNDLE} ${AGENT_LIBGPG_ERROR})
set_property(TARGET gpg-agent_target APPEND
PROPERTY ADDITIONAL_CLEAN_FILES ${AGENT_APP_BUNDLE})
###############################################################################
# generate_entitlements
function(generate_entitlements name)
add_custom_command(OUTPUT "${name}.plist"
COMMAND /bin/bash ARGS -c
[[ m4 -D "$1" "${@:4}" < "$2" > "$3" ]]
"shell"
"MY_TEAM_ID=${TEAM_ID}"
"${CMAKE_CURRENT_SOURCE_DIR}/src/meta/entitlements.plist.m4"
"${CMAKE_BINARY_DIR}/${name}.plist"
${ARGN}
MAIN_DEPENDENCY "src/meta/entitlements.plist.m4"
VERBATIM)
add_custom_target(${name} DEPENDS "${name}.plist")
endfunction()
generate_entitlements(migrate-keys-entitlements)
generate_entitlements(gpg-agent-entitlements -D "ALLOW_DYLD=1")
###############################################################################
# target_codesign
function(target_codesign name)
add_custom_command(TARGET ${name} POST_BUILD
COMMAND ${CODESIGN} ARGS
$<TARGET_FILE:${name}> ${IDENTITY} ${ARGN}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
VERBATIM
)
endfunction()
###############################################################################
# migrate-keys
add_executable(migrate-keys
"src/migrate-keys.cpp"
"src/migrate-keys-helper.mm")
block()
find_library(LOCAL_AUTHENTICATION_F LocalAuthentication REQUIRED)
find_library(FOUNDATION_F Foundation REQUIRED)
find_library(CORE_SERVICES_F CoreServices REQUIRED)
find_library(APPKIT_F AppKit REQUIRED)
target_link_libraries(migrate-keys
PRIVATE keychain-interpose-common
PRIVATE Boost::program_options
PRIVATE ${LOCAL_AUTHENTICATION_F}
PRIVATE ${FOUNDATION_F}
PRIVATE ${CORE_SERVICES_F}
PRIVATE ${APPKIT_F})
endblock()
###############################################################################
# pinentry-wrapper
add_executable(pinentry-wrapper
"src/pinentry-wrapper.cpp")
target_codesign(pinentry-wrapper)
###############################################################################
# dependency-sources.zip
add_custom_command(
OUTPUT
"dependency-sources.zip"
"dependency-sources.zip.txt"
COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/src/meta/download-source.sh" ARGS
${CMAKE_BINARY_DIR}
VERBATIM
USES_TERMINAL
)
add_custom_target("dependency-sources"
DEPENDS "dependency-sources.zip" "dependency-sources.zip.txt")
###############################################################################
# keychain-interpose.app
add_library(keychain-interpose SHARED "src/keychain-interpose.cpp")
set_target_properties(keychain-interpose PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${MAIN_APP_BUNDLE}/Contents/Frameworks"
# Prevent cmake from adding a "lib" prefix to the output filename.
PREFIX "")
set_target_properties(keychain-interpose PROPERTIES PREFIX "")
target_sources(keychain-interpose PRIVATE FILE_SET CXX_MODULES FILES
"src/modules/cathyjf.ki.log.cppm")
add_dependencies(keychain-interpose "gpg-agent_target")
target_include_directories(keychain-interpose PRIVATE ${GPG_ERROR_INCLUDE_DIRS})
target_link_libraries(keychain-interpose
PRIVATE keychain-interpose-common
PRIVATE ${AGENT_LIBGPG_ERROR})
target_codesign(keychain-interpose)
set(APP_RESOURCES "${MAIN_APP_BUNDLE}/Contents/Resources")
add_custom_command(
OUTPUT
"${MAIN_APP_BUNDLE}/Contents/Info.plist"
"${MAIN_APP_BUNDLE}/Contents/_CodeSignature/CodeResources"
"${MAIN_APP_BUNDLE}/Contents/embedded.provisionprofile"
COMMAND /usr/bin/install ARGS
-m u=rx
$<TARGET_FILE:migrate-keys>
$<TARGET_FILE:pinentry-wrapper>
"${MAIN_APP_BUNDLE}/Contents/MacOS"
COMMAND /bin/mkdir ARGS
-p ${APP_RESOURCES}
COMMAND /usr/bin/install ARGS
-m u=r
"README.md"
${APP_RESOURCES}
COMMAND /usr/bin/install ARGS
-m u=rx
"src/resources/gpg-keychain-agent.sh"
"src/resources/help-message.sh"
${APP_RESOURCES}
COMMAND /bin/ln ARGS
-f -s "../MacOS/gpg-agent.app/Contents/Resources/pkg-info"
"${MAIN_APP_BUNDLE}/Contents/Resources/pkg-info"
COMMAND ${MAKE_BUNDLE} ARGS
"keychain-interpose"
${CMAKE_BINARY_DIR}
${CMAKE_BINARY_DIR}
${IDENTITY}
DEPENDS
${MAKE_BUNDLE}
"keychain-interpose.provisionprofile"
"provisionprofile_target"
"keychain-interpose"
"migrate-keys"
"migrate-keys-entitlements"
"pinentry-wrapper"
"gpg-agent_target"
"README.md"
"src/meta/Info.plist.m4"
"src/resources/gpg-keychain-agent.sh"
"src/resources/help-message.sh"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
VERBATIM
)
add_custom_target("keychain-interpose_app_bundle" ALL
DEPENDS "${MAIN_APP_BUNDLE}/Contents/Info.plist")
set_property(TARGET "keychain-interpose_app_bundle" APPEND
PROPERTY ADDITIONAL_CLEAN_FILES ${MAIN_APP_BUNDLE})