Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
executable file
·847 lines (771 loc) · 31.8 KB
/
Copy pathCMakeLists.txt
File metadata and controls
executable file
·847 lines (771 loc) · 31.8 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
cmake_minimum_required(VERSION 3.18)
# set(CMAKE_VERBOSE_MAKEFILE ON)
# set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # needed for sonarCloud scanner when using CMake
project(libgencmp)
set(LIBGENCMP_NAME gencmp)
if(NOT DEFINED GENCMPCLIENT_VERSION)
set(GENCMPCLIENT_VERSION_MAJOR 2)
set(GENCMPCLIENT_VERSION_MINOR 2)
set(GENCMPCLIENT_VERSION ${GENCMPCLIENT_VERSION_MAJOR}.${GENCMPCLIENT_VERSION_MINOR})
endif()
message(STATUS "generic CMP client version " ${GENCMPCLIENT_VERSION})
set(PROJECT_VERSION ${GENCMPCLIENT_VERSION})
set(CMAKE_PROJECT_DESCRIPTION "Generic CMP client library with high-level API based on OpenSSL")
set(CMAKE_PROJECT_HOMEPAGE_URL "https://github.com/siemens/genCMPClient")
set(pc_req_public "openssl, libsecutils")
set(pc_req_private "")
# Option to build shared or static library (default: SHARED)
option(GENCMP_STATIC_LIB "Build static library instead of shared" OFF)
if(GENCMP_STATIC_LIB)
set(GENCMP_LIB_TYPE STATIC)
message(STATUS "Building STATIC library")
else()
set(GENCMP_LIB_TYPE SHARED)
message(STATUS "Building SHARED library")
endif()
# default destination on Linux and macOS: "/usr/local"
#if(UNIX AND NOT APPLE)
# set(CMAKE_INSTALL_PREFIX "/usr")
#else()
# set(CMAKE_INSTALL_PREFIX "tmp")
#endif()
if(DEFINED ENV{ROOTFS})
set(CMAKE_INSTALL_PREFIX $ENV{ROOTFS} CACHE PATH "comment" FORCE)
endif()
# Ensure the .pc file lands in usr/lib/<triplet>/pkgconfig/ not usr/lib/pkgconfig/
# (pkg-config-unavailable-for-cross-compilation).
# Prefer the env var set by dpkg-buildpackage; fall back to calling dpkg-architecture
# directly so CPack builds and plain cmake invocations are also covered.
if(UNIX AND NOT APPLE)
if(DEFINED ENV{DEB_HOST_MULTIARCH})
set(_deb_multiarch "$ENV{DEB_HOST_MULTIARCH}")
else()
execute_process(
COMMAND dpkg-architecture -qDEB_HOST_MULTIARCH
OUTPUT_VARIABLE _deb_multiarch
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET)
endif()
if(_deb_multiarch)
set(CMAKE_INSTALL_LIBDIR "lib/${_deb_multiarch}" CACHE STRING
"Object code libraries (multiarch)" FORCE)
endif()
endif()
include(GNUInstallDirs) # needed for install(...), CMAKE_INSTALL_FULL_LIBDIR, etc.; any CMAKE_INSTALL_PREFIX must be set before
# On Windows with MSVC, automatically imply GENCMP_NO_SECUTILS
# because libSecUtils and CLI are not supported on native Windows builds
if(MSVC)
if(NOT DEFINED ENV{GENCMP_NO_SECUTILS})
message(WARNING "Implying GENCMP_NO_SECUTILS because with native Windows builds, libSecUtils and CLI are not supported so far")
endif()
set(ENV{GENCMP_NO_SECUTILS} 1)
endif()
if(DEFINED ENV{GENCMP_NO_SECUTILS})
message(STATUS "will build without libSecUtils and CLI")
add_compile_definitions(GENCMP_NO_SECUTILS)
set(GENCMP_NO_SECUTILS 1)
set(GENCMP_NO_CLI 1)
endif()
if(DEFINED ENV{SECUTILS_NO_TLS})
set(GENCMP_NO_TLS 1)
endif()
if(DEFINED ENV{GENCMP_NO_TLS})
set(GENCMP_NO_TLS 1)
endif()
if(${GENCMP_NO_TLS})
message(STATUS "will build without TLS support")
add_compile_definitions(GENCMP_NO_TLS)
add_compile_definitions(SECUTILS_NO_TLS)
endif()
# must do add_compile_options() before add_library() and add_executable() see
# https://stackoverflow.com/questions/40516794/cmake-not-applying-compile-option-using-add-compile-options
if(DEFINED ENV{NDEBUG} OR NOT CMAKE_BUILD_TYPE MATCHES Debug)
if(DEFINED ENV{NDEBUG})
message(STATUS "Setting build type to 'Release' because NDEBUG was set.")
elseif(NOT CMAKE_BUILD_TYPE)
message(STATUS "Setting build type to 'Release' by default.")
endif()
set(CMAKE_BUILD_TYPE Release # automatically leads to CFLAGS += -DNDEBUG -O3
CACHE STRING "Choose the type of build." FORCE)
else()
set(CMAKE_BUILD_TYPE Debug # automatically leads to CFLAGS += -g
CACHE STRING "Choose the type of build." FORCE)
if(NOT MSVC)
add_compile_options(-O1) # workaround in case not added by default when libSecUtils is not used
endif()
set(SANITIZER_FLAGS -fsanitize=address,undefined -fno-sanitize-recover=all)
add_compile_options(${SANITIZER_FLAGS})
link_libraries(${SANITIZER_FLAGS})
if(UNIX AND NOT APPLE)
set(COVERAGE_FLAGS --coverage -fprofile-arcs)
add_compile_options(${COVERAGE_FLAGS})
link_libraries(${COVERAGE_FLAGS})
endif()
endif()
message(STATUS "build mode: ${CMAKE_BUILD_TYPE}")
# improved from https://cmake.org/cmake/help/v3.6/module/FindOpenSSL.html
if(NOT "$ENV{OPENSSL_DIR}" STREQUAL "")
get_filename_component(OPENSSL_ROOT_DIR "$ENV{OPENSSL_DIR}" ABSOLUTE)
if(NOT TARGET OpenSSL::Crypto) # not already handled by superordinate module
# invalidate defaults from CMakeCache.txt
set(OPENSSL_VERSION "")
set(OPENSSL_INCLUDE_DIR "")
set(OPENSSL_LIB_SET FALSE)
set(OPENSSL_CRYPTO_LIBRARY "")
set(OPENSSL_SSL_LIBRARY "")
endif()
endif()
if(NOT "$ENV{OPENSSL_DIR}" STREQUAL "" OR
NOT TARGET OpenSSL::Crypto) # not already done by superordinate module
if(${SECUTILS_NO_TLS})
set(OPENSSL_COMPONENTS COMPONENTS Crypto)
else()
set(OPENSSL_COMPONENTS COMPONENTS Crypto SSL)
endif()
if(DEFINED OPENSSL_ROOT_DIR)
if(NOT APPLE) # on macOS, work around find_package() not respecting HINTS and NO_DEFAULT_PATH
find_package(OpenSSL HINTS "${OPENSSL_ROOT_DIR}" NO_DEFAULT_PATH ${OPENSSL_COMPONENTS})
endif()
else()
find_package(OpenSSL ${OPENSSL_COMPONENTS})
endif()
STRING(REGEX REPLACE ";.*" "" OPENSSL_INCLUDE_DIR "${OPENSSL_INCLUDE_DIR}")
if(NOT OPENSSL_INCLUDE_DIR)
set(OPENSSL_INCLUDE_DIR "${OPENSSL_ROOT_DIR}/include")
endif()
if(NOT OPENSSL_VERSION)
file(READ "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h" OPENSSLV_CONTENT)
string(REGEX MATCH "OpenSSL ([0-9]+\\.[0-9]+\\.[0-9]+[^ ]*)" VERSION_MATCH "${OPENSSLV_CONTENT}")
if(VERSION_MATCH)
string(REGEX REPLACE "OpenSSL " "" OPENSSL_VERSION "${VERSION_MATCH}")
else()
set(OPENSSL_VERSION "(unknown)")
endif()
endif()
endif()
message(STATUS "using OpenSSL version ${OPENSSL_VERSION}")
message(STATUS "using OpenSSL inc 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 TARGET OpenSSL::Crypto) # not already done by superordinate module
set(OPENSSL_LIB "$ENV{OPENSSL_LIB}")
if(NOT OPENSSL_LIB)
if (NOT "$ENV{OPENSSL_DIR}" STREQUAL "")
set(OPENSSL_LIB $ENV{OPENSSL_DIR})
elseif(OPENSSL_CRYPTO_LIBRARY)
get_filename_component(OPENSSL_LIB "${OPENSSL_CRYPTO_LIBRARY}" DIRECTORY)
endif()
endif()
get_filename_component(OPENSSL_LIB "${OPENSSL_LIB}" ABSOLUTE)
message(STATUS "using OpenSSL lib dir ${OPENSSL_LIB}")
if(NOT EXISTS "${OPENSSL_LIB}")
message(FATAL_ERROR "directory OPENSSL_LIB does not exist: ${OPENSSL_LIB}")
endif()
if(NOT TARGET OpenSSL::Crypto)
add_library(OpenSSL::Crypto SHARED IMPORTED)
set(OPENSSL_CRYPTO_LIBRARY "${OPENSSL_LIB}/libcrypto${CMAKE_SHARED_LIBRARY_SUFFIX}")
set_target_properties(OpenSSL::Crypto PROPERTIES IMPORTED_LOCATION ${OPENSSL_CRYPTO_LIBRARY})
set_target_properties(OpenSSL::Crypto PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}")
endif()
if(NOT TARGET OpenSSL::SSL)
add_library(OpenSSL::SSL SHARED IMPORTED)
set(OPENSSL_SSL_LIBRARY "${OPENSSL_LIB}/libssl${CMAKE_SHARED_LIBRARY_SUFFIX}")
set_target_properties(OpenSSL::SSL PROPERTIES IMPORTED_LOCATION ${OPENSSL_SSL_LIBRARY})
set_target_properties(OpenSSL::SSL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}")
endif()
if(NOT DEFINED OPENSSL_FOUND)
set(OPENSSL_LIBRARIES ${OPENSSL_CRYPTO_LIBRARY} ${OPENSSL_SSL_LIBRARY})
endif()
endif()
message(STATUS "using OpenSSL library ${OPENSSL_CRYPTO_LIBRARY}, ${OPENSSL_SSL_LIBRARY}")
# Handle cases where OPENSSL_CRYPTO_LIBRARY contains both debug and optimized versions
if(OPENSSL_CRYPTO_LIBRARY MATCHES "optimized|debug")
# Extract the actual library paths from the optimized/debug list
string(REGEX REPLACE ".*optimized;([^;]+).*" "\\1" OPENSSL_CRYPTO_LIB_RELEASE "${OPENSSL_CRYPTO_LIBRARY}")
string(REGEX REPLACE ".*debug;([^;]+).*" "\\1" OPENSSL_CRYPTO_LIB_DEBUG "${OPENSSL_CRYPTO_LIBRARY}")
# Check if at least one version exists
set(CRYPTO_LIB_EXISTS FALSE)
if(DEFINED OPENSSL_CRYPTO_LIB_RELEASE AND EXISTS "${OPENSSL_CRYPTO_LIB_RELEASE}")
set(CRYPTO_LIB_EXISTS TRUE)
endif()
if(DEFINED OPENSSL_CRYPTO_LIB_DEBUG AND EXISTS "${OPENSSL_CRYPTO_LIB_DEBUG}")
set(CRYPTO_LIB_EXISTS TRUE)
endif()
if(NOT CRYPTO_LIB_EXISTS)
message(FATAL_ERROR "No OpenSSL crypto library file (neither optimized or debug variant) found for: ${OPENSSL_CRYPTO_LIBRARY}")
endif()
else()
# Single library path case
if(NOT EXISTS "${OPENSSL_CRYPTO_LIBRARY}")
message(FATAL_ERROR "OpenSSL crypto library file does not exist: ${OPENSSL_CRYPTO_LIBRARY}")
endif()
endif()
# workaround for using local OpenSSL builds by default expecting that
# its dynamic libs have been installed in ./${LIB} when using the libs
# see for binaries dynamically linked to OpenSSL the output of ${LDD} <binary>
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
set(USERS "^([a-zA-Z]:)?[\\\\/]+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()
string(REGEX MATCH "^([0-9]+)" OPENSSL_VERSION_MAJOR "${OPENSSL_VERSION}")
if(NOT OPENSSL_VERSION_MAJOR)
MESSAGE(WARNING "Cannot detect OpenSSL version, assuming at least 3.0")
elseif("${OPENSSL_VERSION_MAJOR}" LESS "3")
set(OPENSSL_VERSION_BELOW_3 1)
endif()
if(OPENSSL_VERSION_BELOW_3)
set(GENCMP_NO_CLI 1)
MESSAGE(WARNING "Cannot build cmpClient CLI app with OpenSSL < 3.0")
endif()
if(DEFINED ENV{USE_LIBCMP} OR OPENSSL_VERSION_BELOW_3)
set(USE_LIBCMP 1)
message(STATUS "using libcmp, which is deprecated")
add_compile_definitions(USE_LIBCMP)
endif()
set(SRC_DIR ${PROJECT_SOURCE_DIR}/src)
set(INC_DIR ${PROJECT_SOURCE_DIR}/include)
if(DEFINED USE_LIBCMP)
set(CMPOSSL_INC_DIR ${PROJECT_SOURCE_DIR}/cmpossl/include)
endif()
include_directories(
${CMAKE_CURRENT_BINARY_DIR}/include
${INC_DIR}
)
if(DEFINED USE_LIBCMP)
include_directories(SYSTEM ${CMPOSSL_INC_DIR}/cmp)
include_directories(SYSTEM ${CMAKE_SYSROOT}/usr/include/cmp)
endif()
# must not add the system OpenSSL include dir before ${CMPOSSL_INC_DIR}/cmp etc.
include_directories(SYSTEM ${OPENSSL_INCLUDE_DIR})
configure_file(${INC_DIR}/genericCMPClient_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/include/genericCMPClient_config.h)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/include/genericCMPClient_config.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT dev
)
configure_file(libgencmp.pc.in ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc @ONLY)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
COMPONENT dev
)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
# help CPackDeb please dpkg-shlibdeps
# maybe use "${CMAKE_INSTALL_FULL_LIBDIR}", as in libsecutils?
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.20.0")
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS_PRIVATE_DIRS
${CMAKE_CURRENT_BINARY_DIR} "${CMAKE_CURRENT_BINARY_DIR}/cmpossl"
"${CMAKE_CURRENT_BINARY_DIR}/libsecutils/src/libsecutils")
else()
set(CMAKE_INSTALL_RPATH # must be done before add_executable()
"../${CMAKE_INSTALL_LIBDIR}")
endif()
endif()
add_compile_definitions(DEBUG_UNUSED)
add_compile_definitions(PEDANTIC)
if(MSVC)
# MSVC compiler flags (Windows-specific)
add_compile_options(/W4) # High warning level
# add_compile_options(/WX-)
# Disable specific warnings if needed
# add_compile_options(/wd4100) # unreferenced formal parameter
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
else()
# GCC/Clang compiler flags
add_compile_options(-pedantic) # -Werror is enabled only for development and CI, using Makefile_v1 without NDEBUG
add_compile_options(
-Wall -Woverflow -Wextra -Wswitch -Wmissing-prototypes -Wstrict-prototypes
-Wsign-compare -Wformat -Wformat-security -Wtype-limits -Wundef
-Wpointer-arith -Wunused-parameter # -Wc99-extensions -Wlanguage-extension-token
-Wconversion -Wsign-conversion -Wshadow
# Using here "no-" variants to cancel options potentially set in parent project(s):
-Wno-declaration-after-statement -Wno-expansion-to-defined
)
# add_compile_options(-Wno-c99-extensions -Wno-language-extension-token -Wno-declaration-after-statement -Wno-expansion-to-defined)
endif()
# TODO maybe clean up code and re-enable property
# set_property(TARGET ${LIBGENCMP_NAME} PROPERTY C_STANDARD 90)
# set_property(TARGET cmpClient PROPERTY C_STANDARD 90)
# would have no effect:
# target_compile_features(${LIBGENCMP_NAME} PRIVATE c_std_90)
# target_compile_features(cmpClient PRIVATE c_std_90)
add_library(${LIBGENCMP_NAME} ${GENCMP_LIB_TYPE}
"${PROJECT_SOURCE_DIR}/.github/workflows/build.yml"
${SRC_DIR}/genericCMPClient.c
${SRC_DIR}/credential_loading.c
)
if(NOT DEFINED GENCMP_NO_CLI)
add_executable(cmpClient
${SRC_DIR}/cmpClient.c
)
target_link_libraries(cmpClient
${LIBGENCMP_NAME}
security-utilities::library
$<$<BOOL:${USE_LIBCMP}>:cmp>
# important: libcmp before libcrypto such that its contents are preferred
OpenSSL::Crypto
$<$<NOT:$<BOOL:${GENCMP_NO_TLS}>>:OpenSSL::SSL>
)
if(DEFINED ENV{SECUTILS_USE_UTA})
target_link_libraries(cmpClient uta)
target_link_directories(cmpClient PRIVATE "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") # usually expands to /usr/local/lib
# set(CMAKE_INSTALL_RPATH "/usr/local/lib")
endif()
endif(NOT DEFINED GENCMP_NO_CLI)
target_link_libraries(${LIBGENCMP_NAME}
$<$<NOT:$<BOOL:${GENCMP_NO_SECUTILS}>>:security-utilities::library>
$<$<BOOL:${USE_LIBCMP}>:cmp>
# important: libcmp before libcrypto such that its contents are preferred
OpenSSL::Crypto
$<$<NOT:$<BOOL:${GENCMP_NO_TLS}>>:OpenSSL::SSL>
)
set(INC_PUBLIC_HDRS
${INC_DIR}/genericCMPClient.h
${CMAKE_CURRENT_BINARY_DIR}/include/genericCMPClient_config.h
${INC_DIR}/credential_loading.h
)
set_target_properties(${LIBGENCMP_NAME} PROPERTIES
VERSION ${GENCMPCLIENT_VERSION} SOVERSION ${GENCMPCLIENT_VERSION_MAJOR}
PUBLIC_HEADER "${INC_PUBLIC_HDRS}"
)
# Automatically export all symbols from Windows DLL (only applicable on Windows)
if(CMAKE_SYSTEM_NAME MATCHES "Windows" AND NOT GENCMP_STATIC_LIB)
set_target_properties(${LIBGENCMP_NAME} PROPERTIES
WINDOWS_EXPORT_ALL_SYMBOLS ON
)
endif()
find_package(Git)
if(GIT_FOUND)
# add --progress if the tool supports it
execute_process(COMMAND ${CMAKE_COMMAND} -E env LC_ALL=C ${GIT_EXECUTABLE} --help submodule
OUTPUT_VARIABLE _TMP_HELP
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if("${_TMP_HELP}" MATCHES "--progress")
set(GIT_PROGRESS "--progress") # gives lengthy output in CI runs
endif()
if("${_TMP_HELP}" MATCHES "--depth")
set(GIT_DEPTH --depth 1) # used to speed up getting submodules
endif()
if(NOT DEFINED GENCMP_NO_SECUTILS)
set(submodules libsecutils)
endif()
if(DEFINED USE_LIBCMP)
set(submodules ${submodules} cmpossl)
endif()
add_custom_target(update
COMMAND echo "updating repo and submodules"
COMMAND git submodule update ${GIT_PROGRESS} ${submodules}
COMMAND git fetch
COMMAND git rebase
)
else()
MESSAGE(WARNING "Cannot find git, so cannot add 'update' targets for submodules")
endif()
if(NOT TARGET build)
add_custom_target(build COMMAND make all)
endif()
# installation and uninstall
install(FILES doc/Generic_CMP_client_API.pdf
DESTINATION ${CMAKE_INSTALL_DOCDIR}-dev
COMPONENT dev
)
if(NOT DEFINED GENCMP_NO_CLI)
find_program(POD2MAN_EXECUTABLE pod2man)
if(NOT POD2MAN_EXECUTABLE)
find_package(Perl QUIET)
if(PERL_FOUND)
execute_process(COMMAND "${PERL_EXECUTABLE}" -MPod::Man -e "1"
RESULT_VARIABLE _pod_man_rc OUTPUT_QUIET ERROR_QUIET)
if(NOT _pod_man_rc EQUAL 0)
unset(PERL_FOUND)
endif()
endif()
if(NOT PERL_FOUND)
message(FATAL_ERROR "Need pod2man or perl with Pod::Man to generate man pages")
endif()
endif()
find_program(GZIP_EXECUTABLE gzip)
if(NOT GZIP_EXECUTABLE)
message(FATAL_ERROR "Need gzip to compress man pages")
endif()
set(_podfile "${CMAKE_CURRENT_SOURCE_DIR}/doc/cmpClient.pod")
if(NOT EXISTS "${_podfile}")
message(FATAL_ERROR "Could not find pod file ${_podfile} to generate man page")
endif()
set(_manout "${CMAKE_CURRENT_BINARY_DIR}/cmpClient.1")
if(POD2MAN_EXECUTABLE)
add_custom_command(OUTPUT "${_manout}"
DEPENDS "${_podfile}"
COMMAND ${POD2MAN_EXECUTABLE}
--section 1 --center ${CMAKE_PROJECT_NAME} --release --stderr --name cmpClient
${_podfile} ${_manout})
else()
add_custom_command(OUTPUT "${_manout}"
DEPENDS "${_podfile}"
COMMAND ${PERL_EXECUTABLE} -MPod::Man -e
"Pod::Man->new(section=>'1',center=>'${CMAKE_PROJECT_NAME}',name=>'cmpClient')->parse_from_file('${_podfile}','${_manout}')")
endif()
add_custom_command(OUTPUT "${_manout}.gz"
COMMAND ${GZIP_EXECUTABLE} -cn9 ${_manout} > ${_manout}.gz
DEPENDS "${_manout}")
if(NOT TARGET doc)
add_custom_target(doc)
endif()
add_custom_target(man-cmpClient ALL DEPENDS "${_manout}.gz")
add_dependencies(doc man-cmpClient)
install(FILES "${_manout}.gz"
DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
COMPONENT bin)
endif()
install(TARGETS ${LIBGENCMP_NAME}
LIBRARY
DESTINATION "${CMAKE_INSTALL_LIBDIR}"
COMPONENT lib
NAMELINK_COMPONENT dev
PUBLIC_HEADER
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
COMPONENT dev
)
if(NOT DEFINED GENCMP_NO_CLI)
install(TARGETS cmpClient
RUNTIME
DESTINATION "${CMAKE_INSTALL_BINDIR}"
COMPONENT bin
)
endif()
if(NOT TARGET uninstall)
add_custom_target(uninstall
COMMAND xargs -r -I% rm -vf \${DESTDIR}% <install_manifest.txt
COMMAND rm -vfr "\${DESTDIR}${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/{secutils,cmp}"
COMMAND find . -path "./\${DESTDIR}${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/secutils"
| xargs -r rmdir "\${DESTDIR}${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/secutils/*"
COMMAND find . -path "./\${DESTDIR}${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/cmp" -empty | xargs -r rmdir
COMMAND find . -path "./\${DESTDIR}${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}" -empty | xargs -r rmdir
COMMAND find . -path "./\${DESTDIR}${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}" -empty | xargs -r rmdir
COMMAND rm -vfr "\${DESTDIR}${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/cmake/security-utilities" # seems not needed: ${PROJECT_NAME} cmpossl
COMMAND find . -path "./\${DESTDIR}${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/security-utilities" -empty | xargs -r rmdir
COMMAND find . -path "./\${DESTDIR}${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/cmake" -empty | xargs -r rmdir
COMMAND find . -path "./\${DESTDIR}${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" -empty | xargs -r rmdir
COMMAND find . -path "./\${DESTDIR}${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}" -empty | xargs -r rmdir
COMMAND find . -path "./\${DESTDIR}${CMAKE_INSTALL_PREFIX}/lib" -empty | xargs -r rmdir
COMMAND find . -path "./\${DESTDIR}${CMAKE_INSTALL_PREFIX}/share/man/man1" -empty | xargs -r rmdir
COMMAND find . -path "./\${DESTDIR}${CMAKE_INSTALL_PREFIX}/share/man" -empty | xargs -r rmdir
COMMAND rm -vfr "\${DESTDIR}${CMAKE_INSTALL_PREFIX}/share/doc/libgencmp{,-*}/*"
COMMAND rmdir "\${DESTDIR}${CMAKE_INSTALL_PREFIX}/share/doc/libgencmp*"
COMMAND rm -vfr "\${DESTDIR}${CMAKE_INSTALL_PREFIX}/share/doc/cmpclient"
COMMAND find . -path "./\${DESTDIR}${CMAKE_INSTALL_PREFIX}/share/doc" -empty | xargs -r rmdir
COMMAND find . -path "./\${DESTDIR}${CMAKE_INSTALL_PREFIX}/share" -empty | xargs -r rmdir
)
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
${CMAKE_CURRENT_BINARY_DIR}/cmpClient
include/genericCMPClient_config.h
compile_commands.json CMakeDoxyfile.in
install_manifest.txt install_manifest_dev.txt install_manifest_lib.txt
_CPack_Packages changelog.gz debian/.debhelper/
)
# https://stackoverflow.com/a/78133906/2145180
if(NOT TARGET clean_all)
add_custom_target(clean_all
COMMAND ${CMAKE_BUILD_TOOL} clean
COMMAND find . -name "*.o" -o -name "*.d" -o -regex "./libgencmp[0-9-].*" | xargs -r rm
COMMAND rm -rf debian/{tmp,libgencmp,libgencmp-dev,libgencmp-bin}
COMMAND rm -rf ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc
# not needed and can go wrong for cmpossl:
# COMMAND find . -path ./libsecutils/Makefile
# | xargs -r -I% ${CMAKE_BUILD_TOOL} -C libsecutils clean
# COMMAND find . -path ./cmpossl/Makefile
# | xargs -r -I% ${CMAKE_BUILD_TOOL} -C cmpossl clean
COMMAND find . -type d -name "build" | xargs -r rm -r
# cowardly not doing rm -r ${CMAKE_BINARY_DIR}
COMMAND rm -f CMakeCache.txt
# after the following, cannot call this target again:
COMMAND find . ( -name "*.cmake" -o -name Makefile )
# -not -path ./libsecutils/* -not -path ./cmpossl/*
-not -path ./libsecutils/src/libsecutils/security-utilities_libraryConfig.cmake
-not -path ./libsecutils/src/util/security-utilities_icvutilConfig.cmake
-not -path ./libsecutils/coverage/Makefile
| xargs -r rm
COMMAND find . -name CMakeFiles
# -not -path ./libsecutils/* -not -path ./cmpossl/*
| xargs -r rm -r
COMMAND find . -type d -empty | xargs -r rmdir
COMMAND find . -type d -empty | xargs -r rmdir
COMMAND find . -type d -empty | xargs -r rmdir
COMMAND find . -type d -empty | xargs -r rmdir
COMMAND find . -type d -empty | xargs -r rmdir
COMMAND find . -type d -empty | xargs -r rmdir
VERBATIM
)
endif()
# =============================
# Packaging
# =============================
# generate Debian etc. packages using CPack, relying on installation defined above
if(NOT DEBIAN_BUILD) # TODO increase indentation below
if(NOT DEFINED CPACK_PACKAGE_NAME) # TODO increase indentation below
SET(CPACK_PACKAGE_NAME ${PROJECT_NAME})
SET(CPACK_PACKAGE_HOMEPAGE_URL "${CMAKE_PROJECT_HOMEPAGE_URL}")
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 <David.von.Oheimb@siemens.com>")
set(CPACK_PACKAGE_VERSION ${GENCMPCLIENT_VERSION})
set(CPACK_PACKAGE_VERSION_MAJOR ${GENCMPCLIENT_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${GENCMPCLIENT_VERSION_MINOR})
set(CPACK_STRIP_FILES ON)
# Ensure installed directories get standard 0755 permissions (non-standard-dir-perm)
set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS
OWNER_READ OWNER_WRITE OWNER_EXECUTE
GROUP_READ GROUP_EXECUTE
WORLD_READ WORLD_EXECUTE)
set(CPACK_COMPONENT_LIB_DESCRIPTION "generic CMP client library
This provides an high-level API for CMP clients based on OpenSSL.
With extended support for certficate status checking using CRLs and/or OCSP")
set(CPACK_COMPONENT_DEV_DESCRIPTION "libgencmp C headers and documentation
Development support for libgencmp and cmpclient")
set(CPACK_COMPONENT_BIN_DESCRIPTION "CMP client command-line interface
A CLI application for exploring the use of the generic CMP client library")
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()
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
set(CPACK_DEBIAN_LIB_PACKAGE_NAME "libgencmp2")
if(NOT DEFINED GENCMP_NO_CLI)
set(CPACK_DEBIAN_BIN_PACKAGE_NAME "cmpclient")
endif()
# set(CPACK_DEBIAN_DEV_PACKAGE_ARCHITECTURE "any")
set(CPACK_DEBIAN_LIB_PACKAGE_SECTION "libs")
set(CPACK_DEBIAN_DEV_PACKAGE_SECTION "devel")
set(CPACK_DEBIAN_BIN_PACKAGE_SECTION "utils")
set(CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS ON)
set(CPACK_DEBIAN_ENABLE_COMPONENT_DEPENDS ON)
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
# set(CPACK_DEBIAN_PACKAGE_DEBUG ON)
# see above assignments
# to CPACK_DEBIAN_PACKAGE_SHLIBDEPS_PRIVATE_DIRS or CMAKE_INSTALL_RPATH
set(CPACK_DEBIAN_LIBRARY_RUNTIME_PACKAGE_NAME "libgencmp2")
set(CPACK_DEBIAN_LIBRARY_RUNTIME_FILE_NAME DEB-DEFAULT)
set(CPACK_DEBIAN_LIBRARY_RUNTIME_PACKAGE_SECTION "libs")
# CPack 4.4+ generates the ldconfig trigger itself; supplying it as an
# extra control file would incorrectly make it executable in strict mode.
if(CMAKE_VERSION VERSION_LESS "4.4")
# Older CPack versions need a custom trigger to avoid direct ldconfig calls.
set(CPACK_DEBIAN_LIBRARY_RUNTIME_PACKAGE_CONTROL_EXTRA
"${CMAKE_CURRENT_SOURCE_DIR}/debian/extra/triggers"
"${CMAKE_CURRENT_SOURCE_DIR}/debian/extra/postinst"
"${CMAKE_CURRENT_SOURCE_DIR}/debian/extra/postrm")
set(CPACK_DEBIAN_LIBRARY_RUNTIME_PACKAGE_CONTROL_STRICT_PERMISSION TRUE)
endif()
if(FALSE) # for now, disable dependencies to libs of subprojectx because their .so files for some reason already get included in the package:
if(NOT DEFINED GENCMP_NO_SECUTILS)
if(DEFINED USE_LIBCMP)
set(CPACK_DEBIAN_PACKAGE_DEPENDS
"libcmp (>= ${CPACK_PACKAGE_VERSION}), libsecutils2 (>= ${CPACK_PACKAGE_VERSION})")
else()
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libsecutils2 (>= ${CPACK_PACKAGE_VERSION})")
endif()
else()
if(DEFINED USE_LIBCMP)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libcmp (>= ${CPACK_PACKAGE_VERSION})")
else()
set(CPACK_DEBIAN_PACKAGE_DEPENDS "")
endif()
endif()
endif(FALSE)
set(CPACK_DEBIAN_DEV_PACKAGE_DEPENDS "libgencmp2 (>= ${CPACK_PACKAGE_VERSION})")
if(DEFINED USE_LIBCMP)
set(CPACK_DEBIAN_DEV_PACKAGE_SUGGESTS "libcmp-dev (>= ${CPACK_PACKAGE_VERSION}), libsecutils-dev (>= ${CPACK_PACKAGE_VERSION}), libssl-dev")
endif()
# 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
# CPack 4.4+ generates the ldconfig trigger itself; supplying it as an
# extra control file would incorrectly make it executable in strict mode.
if(CMAKE_VERSION VERSION_LESS "4.4")
# Older CPack versions need a custom trigger to avoid direct ldconfig calls.
set(CPACK_DEBIAN_LIB_PACKAGE_CONTROL_EXTRA
"${CMAKE_CURRENT_SOURCE_DIR}/debian/extra/triggers"
"${CMAKE_CURRENT_SOURCE_DIR}/debian/extra/postinst"
"${CMAKE_CURRENT_SOURCE_DIR}/debian/extra/postrm")
set(CPACK_DEBIAN_LIB_PACKAGE_CONTROL_STRICT_PERMISSION TRUE)
endif()
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")
# https://stackoverflow.com/questions/6712000/cmake-cpack-package-installation-path-nightmare
#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}
~$
/\\.git/
\\.git$
\\.deb$
\\.gz$
\\.zip$
\\.so
\\.dylib$
\\.diff$
/attic/
/tmp/
/Makefile$
CMakeFiles/
_CPack_Packages/
\\.cmake$
/CMakeCache.txt$
/compile_commands.json$
/install_manifest.*\\.txt$
/changelog\.gz$
/debian/tmp/
\\.substvars$
\\.log$
/debian/\\.debhelper/
/debian/files$
/debian/debhelper-build-stamp
/debian/${PROJECT_NAME}/
/debian/${PROJECT_NAME}-dev/
/debian/cmpclient/
cmpClient$
\\.1$
\\.1\.gz$
\\.crl$
\\.der$
\\.jar$
\\.dsc$
\\.build$
\\.buildinfo$
\\.changes$
libsecutils.*
cmpossl.*/
cmp-ra-component.*/
LightweightCmpRa/
SimpleLra/
credentials
Mock/
Insta/
Simple/
)
set(CPACK_COMPONENTS_ALL lib dev)
if(NOT DEFINED GENCMP_NO_CLI)
list(APPEND CPACK_COMPONENTS_ALL bin)
endif()
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)
string(TOUPPER "${component}" _comp_upper)
if(DEFINED CPACK_DEBIAN_${_comp_upper}_PACKAGE_NAME)
set(_doc_dest "${CMAKE_INSTALL_DATADIR}/doc/${CPACK_DEBIAN_${_comp_upper}_PACKAGE_NAME}")
elseif(${component} STREQUAL "lib" OR ${component} STREQUAL "bin")
set(_doc_dest "${CMAKE_INSTALL_DOCDIR}")
else()
set(_doc_dest "${CMAKE_INSTALL_DOCDIR}-${component}")
endif()
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
install(FILES
"${CMAKE_CURRENT_SOURCE_DIR}/debian/copyright"
"${CMAKE_CURRENT_BINARY_DIR}/changelog.gz"
DESTINATION "${_doc_dest}"
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 "${CMAKE_CPACK_COMMAND}" # --config CPackConfig.cmake
COMMAND "${CMAKE_CPACK_COMMAND}" --config CPackSourceConfig.cmake
)
add_custom_target(${EXT} DEPENDS ${pack_full_name})
endif()
endif(NOT DEFINED CPACK_PACKAGE_NAME)
endif(NOT DEBIAN_BUILD)
# submodules
macro(add_clean_target dir)
add_custom_target(clean-${dir} COMMAND ${CMAKE_MAKE_PROGRAM} clean WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${dir})
endmacro(add_clean_target)
if(NOT YOCTO_BUILD)
# find_package(secutils ${CPACK_PACKAGE_VERSION})
if(NOT DEFINED GENCMP_NO_SECUTILS AND NOT secutils_FOUND)
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/libsecutils/CMakeLists.txt")
if(GIT_FOUND)
message(STATUS "fetching git submodule libsecutils")
execute_process(COMMAND
git submodule update ${GIT_PROGRESS} --init ${GIT_DEPTH} libsecutils
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMODULE_RESULT
)
if(NOT GIT_SUBMODULE_RESULT EQUAL "0")
message(FATAL_ERROR "git failed with exit code ${GIT_SUBMODULE_RESULT}")
endif()
else()
message(FATAL_ERROR "Git not found; please install git.")
endif()
endif()
add_subdirectory(libsecutils)
add_clean_target(libsecutils)
endif()
# find_package(cmp ${CPACK_PACKAGE_VERSION})
if(DEFINED USE_LIBCMP AND NOT libcmp_FOUND)
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/cmpossl/CMakeLists.txt")
if(GIT_FOUND)
message(STATUS "fetching git submodule cmpossl, which is deprecated")
execute_process(COMMAND
git submodule update ${GIT_PROGRESS} --init ${GIT_DEPTH} cmpossl
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMODULE_RESULT
)
if(NOT GIT_SUBMODULE_RESULT EQUAL "0")
message(FATAL_ERROR "git failed with exit code ${GIT_SUBMODULE_RESULT}")
endif()
else()
message(FATAL_ERROR "Git not found; please install git.")
endif()
endif()
# libcmp.so* must not be bundled into libgencmp2.deb: libcmp is a separate
# package declared in Depends. Skip its install rules entirely.
set(LIBCMP_SKIP_INSTALL ON)
add_subdirectory(cmpossl)
add_clean_target(cmpossl)
endif()
endif()