-
Notifications
You must be signed in to change notification settings - Fork 4
/
CMakeLists.txt
executable file
·375 lines (330 loc) · 13.4 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
cmake_minimum_required(VERSION 3.13)
project(
security-utilities
VERSION 2.0.0 # always 3 components for correct versioning
LANGUAGES C)
message(STATUS "SecurityUtilities version ${security-utilities_VERSION}")
# set(CMAKE_VERBOSE_MAKEFILE on)
if(DEFINED ENV{NDEBUG})
message(STATUS "Setting build type to 'Release' because NDEBUG was set.")
set(CMAKE_BUILD_TYPE
Release # automatically leads to CFLAGS += -DNDEBUG -O3
CACHE STRING "Choose the type of build." FORCE)
elseif(NOT CMAKE_BUILD_TYPE)
message(STATUS "Setting build type to 'Debug' as none was specified.")
set(CMAKE_BUILD_TYPE
Debug # automatically leads to CFLAGS += -g # -O0 not added by default
CACHE STRING "Choose the type of build." FORCE)
endif()
message(STATUS "Build mode: ${CMAKE_BUILD_TYPE}")
set(OPENSSL_COMPONENTS REQUIRED COMPONENTS Crypto SSL) # TODO SSL should not be needed if SECUTILS_NO_TLS
# improved from https://cmake.org/cmake/help/v3.6/module/FindOpenSSL.html
if(NOT DEFINED OPENSSL_ROOT_DIR AND NOT "$ENV{OPENSSL_DIR}" STREQUAL "")
get_filename_component(OPENSSL_ROOT_DIR "$ENV{OPENSSL_DIR}" ABSOLUTE)
set(OPENSSL_INCLUDE_DIR "${OPENSSL_ROOT_DIR}/include")
endif()
if(NOT DEFINED OPENSSL_FOUND) # not already done by superordinate module
if(DEFINED OPENSSL_ROOT_DIR)
find_package(OpenSSL HINTS "${OPENSSL_ROOT_DIR}" NO_DEFAULT_PATH ${OPENSSL_COMPONENTS})
else()
find_package(OpenSSL ${OPENSSL_COMPONENTS})
endif()
STRING(REGEX REPLACE "/?/libcrypto\..*" "" OPENSSL_LIB "${OPENSSL_CRYPTO_LIBRARY}")
endif()
message(STATUS "using OpenSSL version ${OPENSSL_VERSION}")
message(STATUS "using OpenSSL inc dir ${OPENSSL_INCLUDE_DIR}")
STRING(REGEX REPLACE ";.*" "" OPENSSL_INCLUDE_DIR "${OPENSSL_INCLUDE_DIR}")
if(NOT EXISTS "${OPENSSL_INCLUDE_DIR}/openssl")
message(FATAL_ERROR "OpenSSL include directory does not exist: ${OPENSSL_INCLUDE_DIR}/openssl")
endif()
if(NOT DEFINED OPENSSL_LIB_SET AND NOT "$ENV{OPENSSL_LIB}" STREQUAL "")
set(OPENSSL_LIB_SET 1)
get_filename_component(OPENSSL_LIB "$ENV{OPENSSL_LIB}" ABSOLUTE)
if(NOT EXISTS "${OPENSSL_LIB}")
message(FATAL_ERROR "directory OPENSSL_LIB does not exist: ${OPENSSL_LIB}")
endif()
if(TARGET OpenSSL::Crypto)
set(OPENSSL_CRYPTO_LIBRARY "${OPENSSL_LIB}/libcrypto${CMAKE_SHARED_LIBRARY_SUFFIX}")
set_target_properties(OpenSSL::Crypto PROPERTIES IMPORTED_LOCATION ${OPENSSL_CRYPTO_LIBRARY})
endif()
if(TARGET OpenSSL::SSL)
set(OPENSSL_SSL_LIBRARY "${OPENSSL_LIB}/libssl${CMAKE_SHARED_LIBRARY_SUFFIX}")
set_target_properties(OpenSSL::SSL PROPERTIES IMPORTED_LOCATION ${OPENSSL_SSL_LIBRARY})
endif()
# set(OPENSSL_LIBRARIES ${OPENSSL_CRYPTO_LIBRARY} ${OPENSSL_SSL_LIBRARY})
endif()
message(STATUS "using OpenSSL lib dir ${OPENSSL_LIB}")
message(STATUS "using OpenSSL library ${OPENSSL_CRYPTO_LIBRARY}, ${OPENSSL_SSL_LIBRARY}")
if(NOT EXISTS "${OPENSSL_CRYPTO_LIBRARY}")
message(FATAL_ERROR "OpenSSL crypto library file does not exist: ${OPENSSL_CRYPTO_LIBRARY}")
endif()
# Workaround for local OpenSSL builds by default expecting that their
# dynamic libs have been installed in ./${LIB}.
# See for binaries dynamically linked to OpenSSL the output of 'ldd <binary>'
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
set(USERS "^(\w:)?\\Users\\")
set(LIB "bin")
else()
set(USERS "^/(home|Users)/")
set(LIB "lib")
endif()
string(REGEX MATCH ${USERS} MATCHED "${OPENSSL_LIB}")
if(NOT "${MATCHED}" STREQUAL "" AND NOT EXISTS "${OPENSSL_LIB}/${LIB}")
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "." "${OPENSSL_LIB}/${LIB}")
# since CMake 3.13, this works also for Windows
endif()
option(SECURITY_UTILITIES_USE_UTA "Use UTA API" OFF)
option(SECURITY_UTILITIES_USE_ICV "Use configuration ICV" OFF)
option(SECURITY_UTILITIES_NO_TLS "Do not use TLS" OFF)
if(DEFINED ENV{SECUTILS_USE_UTA})
set(SECURITY_UTILITIES_USE_UTA ON)
endif()
if(DEFINED ENV{SECUTILS_USE_ICV})
set(SECURITY_UTILITIES_USE_ICV ON)
endif()
if(DEFINED ENV{SECUTILS_NO_TLS})
set(SECURITY_UTILITIES_NO_TLS ON)
endif()
add_subdirectory(src)
# =============================
# Installing
# =============================
#if(UNIX AND NOT APPLE)
# set(CMAKE_INSTALL_PREFIX "/usr")
#else()
# set(CMAKE_INSTALL_PREFIX "tmp")
#endif()
#set(CMAKE_INSTALL_DOCDIR "${CMAKE_INSTALL_PREFIX}/share/doc")
include(GNUInstallDirs) # any CMAKE_INSTALL_PREFIX must be set before
# https://stackoverflow.com/questions/73248130/how-to-avoid-the-removal-of-the-rpath-during-cmake-install-step
if (APPLE)
set(rbase "@loader_path")
else ()
set(rbase "$ORIGIN")
endif ()
file(RELATIVE_PATH lib_dir
"${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}"
"${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
# Honor user overrides, whether at the command line or FetchContent
set(CMAKE_INSTALL_RPATH "${rbase};${rbase}/${lib_dir}" CACHE STRING "Install RPATH")
find_package(Doxygen QUIET)
if(NOT TARGET security-utilities_doxygen AND Doxygen_FOUND)
set(DOXYGEN_OUTPUT_DIRECTORY doc)
doxygen_add_docs(
security-utilities_doxygen
${CMAKE_CURRENT_LIST_DIR}/src
)
endif()
if(NOT TARGET doc)
add_custom_target(doc)
endif()
if(Doxygen_FOUND)
add_dependencies(doc security-utilities_doxygen)
endif()
if(NOT TARGET uninstall)
add_custom_target(uninstall COMMAND xargs -I{} rm -vf $ENV{DESTDIR}{} <install_manifest.txt)
endif()
if(NOT TARGET build)
add_custom_target(build COMMAND make all)
endif()
# =============================
# Cleaning
# =============================
# https://stackoverflow.com/questions/9680420/looking-for-a-cmake-clean-command-to-clear-up-cmake-output/78133906#78133906
set_property(
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
APPEND
PROPERTY ADDITIONAL_CLEAN_FILES
# only works for predefined names as globbing would be done on cmake run
src/libsecutils/include/secutils/secutils_static_config.h
doc CMakeDoxyfile.in Doxyfile.security-utilities_doxygen
install_manifest.txt _CPack_Packages
)
if(NOT TARGET clean_all)
add_custom_target(clean_all
COMMAND ${CMAKE_BUILD_TOOL} clean
COMMAND find . -name "*.o" -o -regex "./libsecutils-.*" | xargs rm
# after the following, cannot call this target again:
COMMAND find . ( -name "*.cmake" -o -name Makefile )
-not -path ./src/libsecutils/security-utilities_libraryConfig.cmake
-not -path ./src/util/security-utilities_icvutilConfig.cmake
-not -path ./coverage/Makefile
| xargs rm
COMMAND find . -name CMakeFiles | xargs rm -r
COMMAND rm CMakeCache.txt
VERBATIM
)
endif()
# =============================
# Packaging
# =============================
# generate Debian etc. packages using CPack, relying on installation defined above
if(DEFINED CPACK_PACKAGE_NAME)
return()
endif()
SET(CPACK_PACKAGE_NAME libsecutils)
SET(CPACK_PACKAGE_HOMEPAGE_URL "https://github.com/siemens/libsecutils")
SET(CPACK_PACKAGE_ICON "${CMAKE_CURRENT_SOURCE_DIR}/libsecutils.svg")
SET(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.txt")
SET(CPACK_PACKAGE_VENDOR "Siemens")
set(CPACK_PACKAGE_CONTACT "David von Oheimb <[email protected]>")
set(CPACK_STRIP_FILES ON)
set(CPACK_COMPONENTS_ALL
security-utilities_library_Runtime
security-utilities_library_Development
)
if (SECURITY_UTILITIES_USE_ICV)
list(APPEND CPACK_COMPONENTS_ALL security-utilities_icvutil_Runtime)
endif()
set(CPACK_COMPONENT_SECURITY-UTILITIES_LIBRARY_RUNTIME_DESCRIPTION "OpenSSL enhancement wrapper library
OpenSSL wrapper library simplifying use of commonly needed functionality
With extended support for certficate status checking using CRLs and/or OCSP")
set(CPACK_COMPONENT_SECURITY-UTILITIES_LIBRARY_DEVELOPMENT_DESCRIPTION "libsecutils C headers and documentation
Development support for libsecutils")
set(CPACK_COMPONENT_SECURITY-UTILITIES_ICVUTIL_RUNTIME_DESCRIPTION "libsecutils helper binaries
Stand-alone helper CLI applications using libsecutils")
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
set(CPACK_GENERATOR "DEB")
set(EXT "deb")
# see also https://gitlab.kitware.com/cmake/cmake/-/issues/24929 and
# https://stackoverflow.com/questions/46490448/how-i-can-add-a-changelog-in-deb-with-cmake
add_custom_command(
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/debian/changelog"
COMMAND gzip -cn9 "${CMAKE_CURRENT_SOURCE_DIR}/debian/changelog"
> "${CMAKE_CURRENT_BINARY_DIR}/changelog.gz"
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/changelog.gz"
)
if(NOT TARGET changelog)
add_custom_target(changelog ALL DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/changelog.gz")
endif()
# help CPackDeb please dpkg-shlibdeps
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.20.0")
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS_PRIVATE_DIRS "../lib/${CMAKE_INSTALL_FULL_LIBDIR}")
else()
set(CMAKE_INSTALL_RPATH # must be done before add_executable()
"../lib/${CMAKE_INSTALL_FULL_LIBDIR}")
endif()
set(CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS ON)
set(CPACK_DEBIAN_ENABLE_COMPONENT_DEPENDS ON)
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
get_target_property(BINARY_DIR_LIBRARY security-utilities_library BINARY_DIR)
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS_PRIVATE_DIRS "${CMAKE_CURRENT_BINARY_DIR}" "${BINARY_DIR_LIBRARY}")
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_DEBIAN_SECURITY-UTILITIES_LIBRARY_RUNTIME_PACKAGE_NAME "${CPACK_PACKAGE_NAME}")
set(CPACK_DEBIAN_SECURITY-UTILITIES_LIBRARY_RUNTIME_FILE_NAME DEB-DEFAULT)
set(CPACK_DEBIAN_SECURITY-UTILITIES_LIBRARY_RUNTIME_PACKAGE_SECTION "libs")
# see also https://gitlab.kitware.com/cmake/cmake/-/issues/21834
# CPack Deb does not create postinst when installing to /usr/lib/x86_64-linux-gnu
set(CPACK_DEBIAN_SECURITY-UTILITIES_LIBRARY_RUNTIME_PACKAGE_CONTROL_EXTRA "${CMAKE_CURRENT_SOURCE_DIR}/debian/extra/triggers")
set(CPACK_DEBIAN_SECURITY-UTILITIES_LIBRARY_RUNTIME_PACKAGE_CONTROL_STRICT_PERMISSION TRUE)
set(CPACK_DEBIAN_SECURITY-UTILITIES_LIBRARY_DEVELOPMENT_PACKAGE_NAME "${CPACK_PACKAGE_NAME}-dev")
set(CPACK_DEBIAN_SECURITY-UTILITIES_LIBRARY_DEVELOPMENT_FILE_NAME DEB-DEFAULT)
set(CPACK_DEBIAN_SECURITY-UTILITIES_LIBRARY_DEVELOPMENT_PACKAGE_ARCHITECTURE "all")
set(CPACK_DEBIAN_SECURITY-UTILITIES_LIBRARY_DEVELOPMENT_PACKAGE_SECTION "devel")
set(CPACK_DEBIAN_SECURITY-UTILITIES_LIBRARY_DEVELOPMENT_PACKAGE_DEPENDS "libsecutils (>= ${security-utilities_VERSION})")
set(CPACK_DEBIAN_SECURITY-UTILITIES_LIBRARY_DEVELOPMENT_PACKAGE_SUGGESTS "libssl-dev, libuta-dev")
set(CPACK_DEBIAN_SECURITY-UTILITIES_ICVUTIL_RUNTIME_PACKAGE_NAME "${CPACK_PACKAGE_NAME}-bin")
set(CPACK_DEBIAN_SECURITY-UTILITIES_ICVUTIL_RUNTIME_FILE_NAME DEB-DEFAULT)
set(CPACK_DEBIAN_SECURITY-UTILITIES_ICVUTIL_RUNTIME_PACKAGE_SECTION "utils")
else(Linux)
#if(APPLE)
# set(CPACK_GENERATOR "Bundle")
# set(EXT "bundle")
# set(CPACK_BUNDLE_NAME "${CPACK_PACKAGE_NAME}")
#else()
set(CPACK_GENERATOR "ZIP")
set(EXT "zip")
#endif(APPLE)
endif(CMAKE_SYSTEM_NAME MATCHES "Linux")
set(CPACK_SOURCE_GENERATOR "TGZ")
#set(CPACK_SET_DESTDIR TRUE) # prevents package creation error when using cpack
set(CPACK_VERBATIM_VARIABLES YES)
set(CPACK_SOURCE_IGNORE_FILES
${CPACK_IGNORE_FILES}
~$
\\.diff$
\./\.git/
\\.git$
\\.deb$
\\.gz$
\\.zip$
\\.o$
\\.so
\\.dylib$
\./attic/
\./tmp/
CMakeFiles/
_CPack_Packages/
\\.cmake$
/Makefile$
./CMakeCache.txt$
./CMakeDoxyfile.in$
./Doxyfile.doc$
/compile_commands.json$
/install_manifest.*\.txt$
/changelog.gz$
\\.substvars$
\\.log$
\./debian/\\.debhelper/
\./debian/tmp/
\./debian/files$
\./debian/md5sums$
\./debian/shlibs$
\./debian/substvars$
\./debian/triggers$
\./debian/debhelper-build-stamp$
\./debian/libsecutils/
\./debian/libsecutils-dev/
\./debian/libsecutils-bin/
\./doc/
/icvutil$
/src/libsecutils/include/secutils/secutils_static_config.h$
)
include(CPack)
# see also https://gitlab.kitware.com/cmake/cmake/-/issues/21832
# CPack Deb lacks a way to configure copyright files by component
foreach(component IN LISTS CPACK_COMPONENTS_ALL)
if(${component} STREQUAL "security-utilities_library_Runtime")
set(suffix "")
elseif(${component} STREQUAL "security-utilities_library_Development")
set(suffix "-dev")
elseif(${component} STREQUAL "security-utilities_icvutil_Runtime")
set(suffix "-bin")
else()
set(suffix "")
endif()
set(COMPONENT_DOC_DIRECTORY "${PROJECT_NAME}${suffix}")
if(suffix STREQUAL "-dev")
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/doc/html
OPTIONAL # TODO add build for doc/
DESTINATION "${CMAKE_INSTALL_DOCDIR}/${COMPONENT_DOC_DIRECTORY}"
COMPONENT security-utilities_library_Development
)
endif()
install(FILES
"${CMAKE_CURRENT_SOURCE_DIR}/libsecutils.svg"
DESTINATION "${CMAKE_INSTALL_DOCDIR}/${COMPONENT_DOC_DIRECTORY}"
COMPONENT ${component}
)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
install(FILES
"${CMAKE_CURRENT_SOURCE_DIR}/debian/copyright"
"${CMAKE_CURRENT_BINARY_DIR}/changelog.gz"
DESTINATION "${CMAKE_INSTALL_DOCDIR}/${COMPONENT_DOC_DIRECTORY}"
COMPONENT ${component}
)
endif()
endforeach()
if(NOT TARGET ${EXT})
set (pack_full_name
"${CMAKE_CURRENT_BINARY_DIR}/${CPACK_PACKAGE_NAME}-dev_${CPACK_PACKAGE_VERSION}_all.${EXT}")
add_custom_command(
OUTPUT "${pack_full_name}"
DEPENDS build
COMMENT "Building packages possibly including doxygen run"
COMMAND cpack --config CPackConfig.cmake # -D CPACK_PACKAGING_INSTALL_PREFIX=
COMMAND cpack --config CPackSourceConfig.cmake
)
add_custom_target(${EXT} DEPENDS ${pack_full_name})
endif()