-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectMacros.cmake
More file actions
2378 lines (2111 loc) · 101 KB
/
Copy pathProjectMacros.cmake
File metadata and controls
2378 lines (2111 loc) · 101 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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Common Project Macros to help setup a CMake project
# Global variables (will be used by all projects), must be set after a global project() call (no language or version required though) and before including ProjectProperties.cmake
# Mandatory variables:
# CU_COMPANY_NAME: Name of your company
# CU_PROJECT_PRODUCTDESCRIPTION: Short description of your project
# CU_PROJECT_STARTING_YEAR: Year of first release
# [macOS] CU_TEAM_IDENTIFIER: Team identifier for your company
# [macOS] CU_BINARY_SIGNING_IDENTITY: Code signing identity for binaries
# [macOS] CU_INSTALLER_SIGNING_IDENTITY: Code signing identity for the installer
# [windows] CU_SIGNTOOL_OPTIONS: Signing options for binaries
# Optional variables:
# CU_PROJECT_FULL_NAME (Defaults to '${PROJECT_NAME}'): Full name of your project
# CU_COMPANY_DOMAIN (Defaults to 'com'): Domain name of your company
# CU_COMPANY_URL (Defaults to 'https://www.${LOWER:CU_COMPANY_NAME}.${LOWER:CU_COMPANY_DOMAIN}'): URL of your company
# CU_PROJECT_URLABOUTINFO (Defaults to '${CU_COMPANY_URL}'): URL of your project
# CU_PROJECT_CONTACT (Defautls to '${LOWER:PROJECT_NAME}@${LOWER:CU_COMPANY_NAME}.com'): Contact email of your project
# CU_COPYRIGHT_HOLDER (Defaults to '${CU_COMPANY_NAME}'): Copyright holder of your project
# CU_BETA_TAG (Defaults to '-beta'): Tag to append to the version number to indicate a beta version
# [windows] CU_SIGNING_TOOL: Tool to use for signing binaries (signtool or azuresigntool, defaults to signtool)
# cu_setup_project method
# This method is used to setup a project that can contain one or more targets. Some variables can be overridden before the call, otherwise the global variables are used.
# Mandatory variables:
# 3rd argument to cu_setup_project: Overrides CU_PROJECT_PRODUCTDESCRIPTION
# Optional variables:
# CU_PROJECT_COMPANYNAME (Defaults to '${CU_COMPANY_NAME}'): Company name to use just for this project
# CU_PROJECT_LEGALCOPYRIGHT (Defaults to '${CU_COPYRIGHT_HOLDER}'): Legal copyright holder to use just for this project
# CU_PROJECT_READABLE_COPYRIGHT (Defaults to 'Copyright ${CU_PROJECT_STARTING_YEAR}-${CU_YEAR}, ${CU_COPYRIGHT_HOLDER}'): Copyright holder to use just for this project
# cu_setup_executable_options method
# This method is used to setup the options for an executable target. Some variables can be overridden before the call, otherwise the global variables are used.
# Optional variables:
# CU_TARGET_BUNDLE_IDENTIFIER (Defaults to '${CU_REVERSE_DOMAIN_NAME}.\${TARGET_NAME}'): Bundle identifier for the executable
# cu_set_warning_flags method
# This method is used to add specific warning flags to one or more targets.
# Mandatory variables:
# TARGETS <targets...>: List of targets to which the warning flags will be added. If 'ALL' is specified, all targets will be affected.
# COMPILER <compiler>: Compiler to which the warning flags will be added (MSVC, CLANG, GCC)
# Optional variables:
# PRIVATE <flags...>: List of flags to add to the target's PRIVATE compile options
# PUBLIC <flags...>: List of flags to add to the target's PUBLIC compile options
# Set this variable before the include guard so it's always correctly defined for the current repository
set(CU_ROOT_DIR "${PROJECT_SOURCE_DIR}") # Folder containing the main CMakeLists.txt for the current repository including this file
# Avoid multi inclusion of this file (cannot use include_guard as multiple copies of this file are included from multiple places)
if(CU_PROJECT_MACROS_INCLUDED)
# We still want to include the local_definitions file if it exists
include("local_definitions.cmake" OPTIONAL)
return()
endif()
set(CU_PROJECT_MACROS_INCLUDED true)
# Some global variables
set(CU_TOP_LEVEL_SOURCE_DIR "${PROJECT_SOURCE_DIR}") # Folder containing the main CMakeLists.txt for the first repository including this file
set(CU_TOP_LEVEL_BINARY_DIR "${PROJECT_BINARY_DIR}") # Folder containing the top level binary files (CMake root output folder)
set(CMAKE_MACROS_FOLDER "${CMAKE_CURRENT_LIST_DIR}")
set(CU_TARGET_ARCH "32") # Legacy variable
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(CU_TARGET_ARCH "64")
endif()
set(CU_ARCH "UNKNOWN") # New variable to replace CU_TARGET_ARCH (will be computed later in the file)
set(CU_DOTNET_PLATFORM_TARGET "") # csproj "PlatformTarget" used for C# application projects (will be computed later in the file)
set(CU_DOTNET_RID_APP "") # csproj "RuntimeIdentifier" used for C# application projects, which must be an exact RID entry (will be computed later in the file)
set(CU_DOTNET_RID_NUGET "") # csproj "RuntimeIdentifier" used for C# NuGets, which can use RID Graph (will be computed later in the file)
if(NOT DEFINED PROJECT_NAME)
message(FATAL_ERROR "project() must be called before including ProjectMacros.cmake")
endif()
# Default Component
set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "${PROJECT_NAME}")
# Enable cmake folders
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# Configure installation path: we override the default installation path.
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "./Install" CACHE PATH "default install path" FORCE)
endif()
# Convert installation path to absolute path (if not already)
if(NOT IS_ABSOLUTE "${CMAKE_INSTALL_PREFIX}")
# Check for cmake minimum version
cmake_minimum_required(VERSION 3.20) # cmake_path added in cmake 3.20
cmake_path(ABSOLUTE_PATH CMAKE_INSTALL_PREFIX BASE_DIRECTORY "${CU_TOP_LEVEL_BINARY_DIR}" NORMALIZE OUTPUT_VARIABLE CMAKE_INSTALL_PREFIX)
endif()
# Setup "Release" build type, if not specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Specifies the build type." FORCE)
endif()
# Include TargetSetupDeploy script
include(${CMAKE_CURRENT_LIST_DIR}/helpers/TargetSetupDeploy.cmake)
###############################################################################
# Internal functions
function(cu_private_detect_arch)
# Compute build architecture
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
# Currently only detecting x86 and x64 on Windows (no ARM support yet)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(CU_ARCH "x64")
else()
set(CU_ARCH "x86")
endif()
set(CU_DOTNET_PLATFORM_TARGET "${CU_ARCH}")
set(CU_DOTNET_RID_APP "win-${CU_ARCH}")
set(CU_DOTNET_RID_NUGET "win-${CU_ARCH}") # To be changed to "win" if we support multi-arch someday
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
list(LENGTH CMAKE_OSX_ARCHITECTURES COUNT_ARCHS)
# Extract the first architecture from CMAKE_OSX_ARCHITECTURES
list(GET CMAKE_OSX_ARCHITECTURES 0 _OSX_ARCH)
if(_OSX_ARCH STREQUAL "x86_64")
set(CU_ARCH "x64")
elseif(_OSX_ARCH STREQUAL "arm64")
set(CU_ARCH "arm64")
elseif(_OSX_ARCH STREQUAL "armv7")
set(CU_ARCH "arm")
elseif(_OSX_ARCH STREQUAL "armv7s")
set(CU_ARCH "arm")
else()
message(FATAL_ERROR "Unsupported CMAKE_OSX_ARCHITECTURES: ${_OSX_ARCH}")
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(OS_NAME "osx")
else()
set(OS_NAME "ios") # Should actually be 'iossimulator' instead of 'ios' for iOS simulator
endif()
# If we build for multi-arch
if(COUNT_ARCHS GREATER 1)
set(CU_DOTNET_PLATFORM_TARGET "AnyCPU")
set(CU_DOTNET_RID_APP "") # Not specifying a RID for multi-arch seems to be the best option, we let the runtime choose the best one (can still be overridden from the dotnet command line)
set(CU_DOTNET_RID_NUGET "${OS_NAME}") # We should use the RID graph for this one, targetting all architectures for that OS
else()
set(CU_DOTNET_PLATFORM_TARGET "${CU_ARCH}")
set(CU_DOTNET_RID_APP "${OS_NAME}-${CU_ARCH}") # Specifying the exact RID for the application
set(CU_DOTNET_RID_NUGET "${OS_NAME}-${CU_ARCH}") # Specifying the exact RID for the NuGet
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
# Currently only detecting system architecture on Linux (not cross-compiling). We usually compile for the same architecture as the system.
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(CU_ARCH "x64")
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
set(CU_ARCH "x86")
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
set(CU_ARCH "arm64")
else()
message(FATAL_ERROR "Unsupported CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
set(CU_DOTNET_PLATFORM_TARGET "${CU_ARCH}")
set(CU_DOTNET_RID_APP "linux-${CU_ARCH}")
set(CU_DOTNET_RID_NUGET "linux-${CU_ARCH}") # To be changed to "linux" if we support multi-arch someday
elseif(CMAKE_SYSTEM_NAME STREQUAL "Android")
# Extract the first architecture from ANDROID_ABI
list(GET ANDROID_ABI 0 _ANDROID_ARCH)
if(_ANDROID_ARCH STREQUAL "x86_64")
set(CU_ARCH "x64")
elseif(_ANDROID_ARCH STREQUAL "arm64-v8a")
set(CU_ARCH "arm64")
else()
message(FATAL_ERROR "Unsupported ANDROID_ABI: ${_ANDROID_ARCH}")
endif()
set(CU_DOTNET_PLATFORM_TARGET "${CU_ARCH}")
set(CU_DOTNET_RID_APP "android-${CU_ARCH}")
set(CU_DOTNET_RID_NUGET "android-${CU_ARCH}") # To be changed to "android" if we support multi-arch someday
else()
message(FATAL_ERROR "Unsupported CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
endif()
# Check for cmake minimum version
cmake_minimum_required(VERSION 3.25) # return(PROPAGATE) added in cmake 3.25
cmake_policy(SET CMP0140 NEW)
return(PROPAGATE CU_ARCH CU_DOTNET_PLATFORM_TARGET CU_DOTNET_RID_APP CU_DOTNET_RID_NUGET)
endfunction()
#
function(cu_private_set_warning_flags TARGET_NAME)
# Get the compiler
if(MSVC)
set(COMPILER "MSVC")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(COMPILER "CLANG")
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
set(COMPILER "GCC")
else()
message(WARNING "cu_private_set_warning_flags: Unknown compiler")
return()
endif()
# Get the ALL target flags and apply (if any)
get_property(all_private_options GLOBAL PROPERTY CUWF_ALL_${COMPILER}_PRIVATE_COMPILE_OPTIONS)
get_property(all_public_options GLOBAL PROPERTY CUWF_ALL_${COMPILER}_PUBLIC_COMPILE_OPTIONS)
if(all_private_options)
target_compile_options(${TARGET_NAME} PRIVATE ${all_private_options})
endif()
if(all_public_options)
target_compile_options(${TARGET_NAME} PUBLIC ${all_public_options})
endif()
# Get the target flags and apply (if any)
get_property(private_options GLOBAL PROPERTY CUWF_${TARGET_NAME}_${COMPILER}_PRIVATE_COMPILE_OPTIONS)
get_property(public_options GLOBAL PROPERTY CUWF_${TARGET_NAME}_${COMPILER}_PUBLIC_COMPILE_OPTIONS)
if(private_options)
target_compile_options(${TARGET_NAME} PRIVATE ${private_options})
endif()
if(public_options)
target_compile_options(${TARGET_NAME} PUBLIC ${public_options})
endif()
endfunction()
#
function(cu_set_output_colorization TARGET_NAME)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(${TARGET_NAME} PRIVATE -fcolor-diagnostics)
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
target_compile_options(${TARGET_NAME} PRIVATE -fdiagnostics-color=always)
endif()
endfunction()
#
function(cu_private_set_default_warning_flags TARGET_NAME)
if(MSVC)
# Don't use Wall on MSVC, it prints too many stupid warnings
target_compile_options(${TARGET_NAME} PRIVATE
/WX # Treat warnings as errors
/W4 # Warning level 4
$<$<COMPILE_LANGUAGE:CXX>:/w14265> # Warn if a class has virtual functions but no virtual destructor
)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(${TARGET_NAME} PRIVATE
-Wall # Enable all warnings
-Werror # Treat warnings as errors
-Wextra # Enable extra warnings
-Wpedantic # Enable pedantic warnings
$<$<COMPILE_LANGUAGE:CXX>:-Wnon-virtual-dtor> # Warn if a class has virtual functions but no virtual destructor
-Wfloat-conversion # Warn about float conversions
)
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
target_compile_options(${TARGET_NAME} PRIVATE
-Wall # Enable all warnings
-Werror # Treat warnings as errors
-Wextra # Enable extra warnings
-Wpedantic # Enable pedantic warnings
$<$<COMPILE_LANGUAGE:CXX>:-Wnon-virtual-dtor> # Warn if a class has virtual functions but no virtual destructor
-Wfloat-conversion # Warn about float conversions
)
else()
message(WARNING "cu_private_set_default_warning_flags: Unknown compiler")
endif()
# Check for overrides
cu_private_set_warning_flags(${TARGET_NAME})
endfunction()
#
function(cu_get_sign_command_options OUT_VAR)
if(WIN32)
set(SIGN_TOOL "signtool")
set(SIGNING_DESCRIPTION_SELECTOR "/d")
if(DEFINED CU_SIGNING_TOOL)
if(CU_SIGNING_TOOL STREQUAL "azuresigntool")
set(SIGN_TOOL "azuresigntool")
set(SIGNING_DESCRIPTION_SELECTOR "-d")
elseif(CU_SIGNING_TOOL STREQUAL "signtool")
set(SIGN_TOOL "signtool")
set(SIGNING_DESCRIPTION_SELECTOR "/d")
else()
message(FATAL_ERROR "Unknown signing tool specified in CU_SIGNING_TOOL: ${CU_SIGNING_TOOL}")
endif()
endif()
elseif(CMAKE_HOST_APPLE)
# On macOS, we use the built-in codesign tool
set(SIGN_TOOL "codesign")
set(SIGNING_DESCRIPTION_SELECTOR "")
else()
message(FATAL_ERROR "Code signing is only supported on Windows and macOS")
set(SIGN_TOOL "none")
set(SIGNING_DESCRIPTION_SELECTOR "")
endif()
set(${OUT_VAR} SIGN_COMMAND \"${SIGN_TOOL}\" SIGNTOOL_OPTIONS sign ${CU_SIGNTOOL_OPTIONS} ${SIGNING_DESCRIPTION_SELECTOR} "\"${CU_COMPANY_NAME} ${PROJECT_NAME}\"" CODESIGN_OPTIONS --timestamp --deep --strict --force --options=runtime CODESIGN_IDENTITY \"${CU_BINARY_SIGNING_IDENTITY}\" PARENT_SCOPE)
endfunction()
# Extract the codesign identity from a provisioning profile by matching its DeveloperCertificates
# against the available codesigning identities in the keychain (macOS only).
# Sets ${OUT_VAR} in PARENT_SCOPE to the SHA-1 hash of the first matching identity, or empty if none found.
# Requires: security, python3 (both available by default on macOS).
function(cu_get_identity_from_profile PROFILE_PATH OUT_VAR)
if(NOT CMAKE_HOST_APPLE)
message(WARNING "cu_get_identity_from_profile is only supported on macOS")
set(${OUT_VAR} "" PARENT_SCOPE)
return()
endif()
# Decode the provisioning profile and extract certificate SHA-1 hashes
execute_process(
COMMAND bash -c "security cms -D -i '${PROFILE_PATH}' 2>/dev/null | python3 -c \"\nimport plistlib, sys, hashlib\nplist = plistlib.loads(sys.stdin.buffer.read())\nfor cert in plist.get('DeveloperCertificates', []):\n print(hashlib.sha1(cert).hexdigest().upper())\n\""
OUTPUT_VARIABLE PROFILE_CERT_HASHES
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
RESULT_VARIABLE RESULT
)
if(NOT RESULT EQUAL 0 OR NOT PROFILE_CERT_HASHES)
message(WARNING "cu_get_identity_from_profile: Failed to extract certificates from profile: ${PROFILE_PATH}")
set(${OUT_VAR} "" PARENT_SCOPE)
return()
endif()
# Get available codesigning identities from the keychain
execute_process(
COMMAND security find-identity -v -p codesigning
OUTPUT_VARIABLE KEYCHAIN_IDENTITIES
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
RESULT_VARIABLE RESULT
)
if(NOT RESULT EQUAL 0)
message(WARNING "cu_get_identity_from_profile: Failed to query keychain for codesigning identities")
set(${OUT_VAR} "" PARENT_SCOPE)
return()
endif()
# Match profile certificate hashes against keychain identities
string(REPLACE "\n" ";" CERT_HASH_LIST "${PROFILE_CERT_HASHES}")
foreach(HASH IN LISTS CERT_HASH_LIST)
string(REGEX MATCH "${HASH} \"[^\"]+\"" MATCH_LINE "${KEYCHAIN_IDENTITIES}")
if(MATCH_LINE)
string(REGEX MATCH "\"[^\"]+\"" IDENTITY_NAME "${MATCH_LINE}")
string(REPLACE "\"" "" IDENTITY_NAME "${IDENTITY_NAME}")
message(STATUS "Auto-detected signing identity from profile: ${IDENTITY_NAME} (${HASH})")
set(${OUT_VAR} "${HASH}" PARENT_SCOPE)
return()
endif()
endforeach()
message(WARNING "cu_get_identity_from_profile: No keychain identity matches the certificates in profile: ${PROFILE_PATH}")
set(${OUT_VAR} "" PARENT_SCOPE)
endfunction()
# Get sign command options for a specific target, with support for per-target entitlements and signing identity overrides.
# This function first checks for target-specific properties, then falls back to the global options.
# Supported target properties (set via set_target_properties):
# - CU_CODESIGN_ENTITLEMENTS: Path to an entitlements plist to embed in the codesign signature (macOS only).
# - CU_CODESIGN_IDENTITY: Override the codesign identity for this specific target (macOS only).
function(cu_get_sign_command_options_for_target TARGET_NAME OUT_VAR)
# Start with the global sign command options
cu_get_sign_command_options(_BASE_OPTIONS)
if(APPLE)
# Check for per-target entitlements
get_target_property(_ENTITLEMENTS ${TARGET_NAME} CU_CODESIGN_ENTITLEMENTS)
if(_ENTITLEMENTS)
# Inject --entitlements right after the CODESIGN_OPTIONS keyword in the options list
string(REPLACE "CODESIGN_OPTIONS" "CODESIGN_OPTIONS --entitlements;\"${_ENTITLEMENTS}\"" _BASE_OPTIONS "${_BASE_OPTIONS}")
endif()
# Check for per-target identity override
get_target_property(_IDENTITY ${TARGET_NAME} CU_CODESIGN_IDENTITY)
if(_IDENTITY)
# CODESIGN_IDENTITY is always the last key in the options list, so replace the last element
list(LENGTH _BASE_OPTIONS _len)
math(EXPR _last "${_len} - 1")
list(REMOVE_AT _BASE_OPTIONS ${_last})
list(APPEND _BASE_OPTIONS "\"${_IDENTITY}\"")
endif()
endif()
set(${OUT_VAR} ${_BASE_OPTIONS} PARENT_SCOPE)
endfunction()
#
function(cu_private_get_target_resource_folder_name TARGET_NAME OUT_VAR)
cu_is_macos_bundle(${TARGET_NAME} isBundle)
if(${isBundle})
set(${OUT_VAR} "Resources" PARENT_SCOPE)
else()
set(${OUT_VAR} "resources" PARENT_SCOPE)
endif()
endfunction()
#
function(cu_private_get_target_resource_path_string TARGET_NAME OUT_VAR)
cu_private_get_target_resource_folder_name(${TARGET_NAME} folderName)
cu_is_macos_bundle(${TARGET_NAME} isBundle)
if(${isBundle})
set(${OUT_VAR} "$<TARGET_BUNDLE_CONTENT_DIR:${TARGET_NAME}>/${folderName}" PARENT_SCOPE)
else()
set(${OUT_VAR} "$<TARGET_FILE_DIR:${TARGET_NAME}>/${folderName}" PARENT_SCOPE)
endif()
endfunction()
# Sign a binary after build, using POST_BUILD rules
# BINARY_NAME is only used to generate a unique cmake file
function(cu_private_sign_postbuild_binary TARGET_NAME BINARY_PATH BINARY_NAME)
# Get signing options (global, not per-target: this signs dependency binaries, not the target itself)
cu_get_sign_command_options(SIGN_COMMAND_OPTIONS)
# Expand options to individual parameters
string(REPLACE ";" " " SIGN_COMMAND_OPTIONS "${SIGN_COMMAND_OPTIONS}")
cu_is_macos_bundle(${TARGET_NAME} isBundle)
if(${isBundle})
set(binary_path "$<TARGET_BUNDLE_DIR:${TARGET_NAME}>")
else()
set(binary_path "$<TARGET_FILE:${TARGET_NAME}>")
endif()
# Generate code-signing code
string(CONCAT CODESIGNING_CODE
"include(\"${CMAKE_MACROS_FOLDER}/helpers/SignBinary.cmake\")\n"
"cu_sign_binary(BINARY_PATH \"${BINARY_PATH}\" ${SIGN_COMMAND_OPTIONS})\n"
)
# Write to a cmake file
string(REPLACE ":" "_" SANITIZED_BINARY_NAME "${BINARY_NAME}")
set(CODESIGN_SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/codesign_$<CONFIG>_${SANITIZED_BINARY_NAME}.cmake)
file(GENERATE
OUTPUT ${CODESIGN_SCRIPT}
CONTENT ${CODESIGNING_CODE}
)
# Run the codesign script as POST_BUILD command on the target
add_custom_command(TARGET ${TARGET_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -P ${CODESIGN_SCRIPT}
VERBATIM
)
endfunction()
# Sign a binary after installation, using install rules
function(cu_private_sign_installed_binary BINARY_PATH)
# Xcode already forces automatic signing, so only sign for the other cases
if(NOT "${CMAKE_GENERATOR}" STREQUAL "Xcode")
# Get signing options
cu_get_sign_command_options(SIGN_COMMAND_OPTIONS)
# Expand options to individual parameters
string(REPLACE ";" " " SIGN_COMMAND_OPTIONS "${SIGN_COMMAND_OPTIONS}")
# Generate code-signing code
string(CONCAT CODESIGNING_CODE
"include(\"${CMAKE_MACROS_FOLDER}/helpers/SignBinary.cmake\")\n"
"get_filename_component(INSTALLED_PATH \"\${CMAKE_INSTALL_PREFIX}/${BINARY_PATH}\" ABSOLUTE BASE_DIR \"${CMAKE_BINARY_DIR}\")\n"
"cu_sign_binary(BINARY_PATH \"\${INSTALLED_PATH}\" ${SIGN_COMMAND_OPTIONS})\n"
)
# Write as install rule
install(CODE
"${CODESIGNING_CODE}"
)
endif()
endfunction()
#
function(cu_private_setup_signing_command TARGET_NAME)
# Parse optional arguments
cmake_parse_arguments(CUPSSC "INSTALL;NO_POST_BUILD" "" "" ${ARGN})
# Xcode already forces automatic signing, so only sign for the other cases
if(NOT "${CMAKE_GENERATOR}" STREQUAL "Xcode")
# Get signing options (with per-target entitlements/identity support)
cu_get_sign_command_options_for_target(${TARGET_NAME} SIGN_COMMAND_OPTIONS)
# Expand options to individual parameters
string(REPLACE ";" " " SIGN_COMMAND_OPTIONS "${SIGN_COMMAND_OPTIONS}")
cu_is_macos_bundle(${TARGET_NAME} isBundle)
if(${isBundle})
set(binary_path "$<TARGET_BUNDLE_DIR:${TARGET_NAME}>")
else()
set(binary_path "$<TARGET_FILE:${TARGET_NAME}>")
endif()
# Generate code-signing code
string(CONCAT CODESIGNING_CODE
"include(\"${CMAKE_MACROS_FOLDER}/helpers/SignBinary.cmake\")\n"
"cu_sign_binary(BINARY_PATH \"${binary_path}\" ${SIGN_COMMAND_OPTIONS})\n"
)
# Write to a cmake file
if(NOT ${CUPSSC_NO_POST_BUILD})
string(REPLACE ":" "_" SANITIZED_TARGET_NAME "${TARGET_NAME}")
set(CODESIGN_SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/codesign_$<CONFIG>_${SANITIZED_TARGET_NAME}.cmake)
file(GENERATE
OUTPUT ${CODESIGN_SCRIPT}
CONTENT ${CODESIGNING_CODE}
)
# Run the codesign script as POST_BUILD command on the target
add_custom_command(TARGET ${TARGET_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -P ${CODESIGN_SCRIPT}
VERBATIM
)
endif()
# Write as install rule
if(CUPSSC_INSTALL)
install(CODE
"${CODESIGNING_CODE}"
)
endif()
endif()
endfunction()
###############################################################################
# Set parallel build
# Sets the parallel build option for IDE that supports it
# This is overridden when compiling from command line with "cmake --build"
function(cu_set_parallel_build TARGET_NAME)
if(MSVC)
target_compile_options(${TARGET_NAME} PRIVATE /MP)
endif()
endfunction()
###############################################################################
# Set TARGET_SYSTEM_xxx compile definition
function(cu_set_target_system_definition TARGET_NAME)
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(TARGET_SYSTEM_NAME "TARGET_SYSTEM_WINDOWS")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(TARGET_SYSTEM_NAME "TARGET_SYSTEM_DARWIN")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(TARGET_SYSTEM_NAME "TARGET_SYSTEM_LINUX")
elseif(CMAKE_SYSTEM_NAME STREQUAL "iOS")
set(TARGET_SYSTEM_NAME "TARGET_SYSTEM_IOS")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Android")
set(TARGET_SYSTEM_NAME "TARGET_SYSTEM_ANDROID")
else()
message(FATAL_ERROR "Unknown CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
endif()
target_compile_definitions(${TARGET_NAME} PRIVATE ${TARGET_SYSTEM_NAME})
endfunction()
###############################################################################
# Set maximum warning level, and treat warnings as errors
# Applies on a target, must be called after target has been defined with
# 'add_library' or 'add_executable'.
function(cu_set_maximum_warnings TARGET_NAME)
if(MSVC)
# Don't use Wall on MSVC, it prints too many stupid warnings
target_compile_options(${TARGET_NAME} PRIVATE /W4 /WX)
# Using clang-cl with MSVC (special case as MSBuild will convert MSVC Flags to Clang flags automatically)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(${TARGET_NAME} PRIVATE -Wno-nonportable-include-path -Wno-microsoft-include)
endif()
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Werror -g)
endif()
endfunction()
###############################################################################
# Set the warning flags for the target
function(cu_set_warning_flags)
cmake_parse_arguments(CUSWF "" "COMPILER" "TARGETS;PRIVATE;PUBLIC" ${ARGN})
# Check that mandatory arguments are set (ie. COMPILER and TARGETS)
if(NOT CUSWF_COMPILER)
message(FATAL_ERROR "cu_set_warning_flags: COMPILER argument is mandatory")
endif()
if(NOT CUSWF_TARGETS)
message(FATAL_ERROR "cu_set_warning_flags: TARGETS argument is mandatory")
endif()
# Check that COMPILER is valid (ie. MSVC, CLANG, GCC)
if(NOT CUSWF_COMPILER STREQUAL "MSVC" AND NOT CUSWF_COMPILER STREQUAL "CLANG" AND NOT CUSWF_COMPILER STREQUAL "GCC")
message(FATAL_ERROR "cu_set_warning_flags: COMPILER argument must be MSVC, CLANG or GCC")
endif()
foreach(target ${CUSWF_TARGETS})
# Store the flags in global properties (only if set)
if(CUSWF_PRIVATE)
set_property(GLOBAL PROPERTY CUWF_${target}_${CUSWF_COMPILER}_PRIVATE_COMPILE_OPTIONS ${CUSWF_PRIVATE})
endif()
if(CUSWF_PUBLIC)
set_property(GLOBAL PROPERTY CUWF_${target}_${CUSWF_COMPILER}_PUBLIC_COMPILE_OPTIONS ${CUSWF_PUBLIC})
endif()
endforeach()
endfunction()
###############################################################################
# Set the DEBUG define in debug mode
# Applies on a target, must be called after target has been defined with
# 'add_library' or 'add_executable'.
function(cu_set_debug_define TARGET_NAME)
# Flags to add for DEBUG
target_compile_options(${TARGET_NAME} PRIVATE $<$<CONFIG:Debug>:-DDEBUG>)
endfunction()
###############################################################################
# Remove VisualStudio useless deprecated warnings (CRT, CRT_SECURE, WINSOCK)
# Applies on a target, must be called after target has been defined with
# 'add_library' or 'add_executable'.
function(cu_remove_vs_deprecated_warnings TARGET_NAME)
if(MSVC)
target_compile_options(${TARGET_NAME} PRIVATE -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_WINSOCK_DEPRECATED_NO_WARNINGS)
endif()
endfunction()
###############################################################################
# Returns TRUE if TARGET is a macOS/iOS framework library
function(cu_is_macos_framework TARGET_NAME IS_FRAMEWORK)
if(APPLE)
get_target_property(isFramework ${TARGET_NAME} FRAMEWORK)
if(${isFramework})
set(${IS_FRAMEWORK} TRUE PARENT_SCOPE)
return()
endif()
endif()
set(${IS_FRAMEWORK} FALSE PARENT_SCOPE)
endfunction()
###############################################################################
# Returns TRUE if TARGET is a macOS bundle application
function(cu_is_macos_bundle TARGET_NAME IS_BUNDLE)
if(APPLE)
get_target_property(isBundle ${TARGET_NAME} MACOSX_BUNDLE)
if(${isBundle})
set(${IS_BUNDLE} TRUE PARENT_SCOPE)
return()
endif()
endif()
set(${IS_BUNDLE} FALSE PARENT_SCOPE)
endfunction()
###############################################################################
# Force symbols file generation for build configs (pdb or dSYM)
# Applies on a target, must be called after target has been defined with
# 'add_library' or 'add_executable'.
function(cu_force_symbols_file TARGET_NAME)
get_target_property(targetType ${TARGET_NAME} TYPE)
if(MSVC)
target_compile_options(${TARGET_NAME} PRIVATE /Zi)
set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS_RELEASE "/DEBUG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
elseif(APPLE)
target_compile_options(${TARGET_NAME} PRIVATE -g)
if("${CMAKE_GENERATOR}" STREQUAL "Xcode")
if(${targetType} STREQUAL "STATIC_LIBRARY")
# macOS do not support dSYM file for static libraries
set_target_properties(${TARGET_NAME} PROPERTIES
XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT[variant=Debug] "dwarf"
XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT[variant=Release] "dwarf"
XCODE_ATTRIBUTE_DEPLOYMENT_POSTPROCESSING[variant=Debug] "NO"
XCODE_ATTRIBUTE_DEPLOYMENT_POSTPROCESSING[variant=Release] "NO"
)
else()
# Currently Xcode does not inject the get-task-allow entitlement for executables that have
# DEPLOYMENT_POSTPROCESSING set to YES. This prevents debugging, which is extremely annoying.
# So set DEPLOYMENT_POSTPROCESSING to NO for debug builds (this means the binary won't be striped)
# Bug report submited to Apple: https://feedbackassistant.apple.com/feedback/9219851
set_target_properties(${TARGET_NAME} PROPERTIES
XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT[variant=Debug] "dwarf-with-dsym"
XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT[variant=Release] "dwarf-with-dsym"
XCODE_ATTRIBUTE_DEPLOYMENT_POSTPROCESSING[variant=Debug] "NO"
XCODE_ATTRIBUTE_DEPLOYMENT_POSTPROCESSING[variant=Release] "YES"
)
endif()
else()
# If not using Xcode, we have to do the dSYM/strip steps manually (but only for binary targets)
if(${targetType} STREQUAL "SHARED_LIBRARY")
cu_is_macos_framework(${TARGET_NAME} isFramework)
if(${isFramework})
set(DSYM_DST "$<TARGET_BUNDLE_DIR:${TARGET_NAME}>.dSYM")
else()
set(DSYM_DST "$<TARGET_FILE:${TARGET_NAME}>.dSYM")
endif()
elseif(${targetType} STREQUAL "EXECUTABLE")
cu_is_macos_bundle(${TARGET_NAME} isBundle)
if(${isBundle})
set(DSYM_DST "$<TARGET_BUNDLE_DIR:${TARGET_NAME}>.dSYM")
else()
set(DSYM_DST "$<TARGET_FILE:${TARGET_NAME}>.dSYM")
endif()
endif()
if(DEFINED DSYM_DST)
add_custom_command(
TARGET ${TARGET_NAME}
POST_BUILD
COMMAND dsymutil "$<TARGET_FILE:${TARGET_NAME}>" -o "${DSYM_DST}"
COMMENT "Extracting dSYM for ${TARGET_NAME}"
VERBATIM
)
add_custom_command(
TARGET ${TARGET_NAME}
POST_BUILD
COMMAND strip -x "$<TARGET_FILE:${TARGET_NAME}>"
COMMENT "Stripping symbols from ${TARGET_NAME}"
VERBATIM
)
endif()
endif()
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(${TARGET_NAME} PRIVATE -g)
endif()
endfunction()
###############################################################################
# Copy symbol files to a common location.
function(cu_copy_symbols TARGET_NAME)
set(SYMBOLS_DEST_PATH "${CMAKE_BINARY_DIR}/Symbols/$<CONFIG>/")
get_target_property(targetType ${TARGET_NAME} TYPE)
if(MSVC)
# No pdb files for static libraries, symbols are embedded in the lib
if(NOT ${targetType} STREQUAL "STATIC_LIBRARY")
add_custom_command(
TARGET ${TARGET_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "${SYMBOLS_DEST_PATH}"
COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_PDB_FILE:${TARGET_NAME}>" "${SYMBOLS_DEST_PATH}"
COMMENT "Copying ${TARGET_NAME} symbols"
VERBATIM
)
endif()
elseif(APPLE)
if(${targetType} STREQUAL "SHARED_LIBRARY")
cu_is_macos_framework(${TARGET_NAME} isFramework)
if(${isFramework})
set(DSYM_SRC "$<TARGET_BUNDLE_DIR:${TARGET_NAME}>.dSYM")
set(DSYM_DST_NAME "$<TARGET_BUNDLE_DIR_NAME:${TARGET_NAME}>.dSYM")
# Check for cmake minimum version
cmake_minimum_required(VERSION 3.24) # TARGET_BUNDLE_DIR_NAME added in cmake 3.24
else()
set(DSYM_SRC "$<TARGET_FILE:${TARGET_NAME}>.dSYM")
set(DSYM_DST_NAME "$<TARGET_FILE_NAME:${TARGET_NAME}>.dSYM")
endif()
elseif(${targetType} STREQUAL "EXECUTABLE")
cu_is_macos_bundle(${TARGET_NAME} isBundle)
if(${isBundle})
set(DSYM_SRC "$<TARGET_BUNDLE_DIR:${TARGET_NAME}>.dSYM")
set(DSYM_DST_NAME "$<TARGET_BUNDLE_DIR_NAME:${TARGET_NAME}>.dSYM")
# Check for cmake minimum version
cmake_minimum_required(VERSION 3.24) # TARGET_BUNDLE_DIR_NAME added in cmake 3.24
else()
set(DSYM_SRC "$<TARGET_FILE:${TARGET_NAME}>.dSYM")
set(DSYM_DST_NAME "$<TARGET_FILE_NAME:${TARGET_NAME}>.dSYM")
endif()
endif()
# Ignoring iOS until https://gitlab.kitware.com/cmake/cmake/-/issues/24161 is fixed
if(DEFINED DSYM_SRC AND NOT CMAKE_SYSTEM_NAME STREQUAL "iOS")
add_custom_command(
TARGET ${TARGET_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "${SYMBOLS_DEST_PATH}"
COMMAND ${CMAKE_COMMAND} -E copy_directory "${DSYM_SRC}" "${SYMBOLS_DEST_PATH}${DSYM_DST_NAME}"
COMMENT "Copying ${TARGET_NAME} symbols and extracting dSYM for ${TARGET_NAME}"
VERBATIM
)
endif()
endif()
endfunction()
###############################################################################
# Setup symbols for a target.
function(cu_setup_symbols TARGET_NAME)
# Temporary workaround for Ninja always rebuilding the target when using POST_BUILD commands (https://gitlab.kitware.com/cmake/cmake/-/issues/26585)
# Disable symbols when using Ninja on a SHARED_LIBRARY target for a Debug build
if("${CMAKE_GENERATOR}" STREQUAL "Ninja" AND "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
get_target_property(targetType ${TARGET_NAME} TYPE)
if(${targetType} STREQUAL "SHARED_LIBRARY")
message(WARNING "Disabling symbols for ${TARGET_NAME} in Debug build with Ninja generator")
return()
endif()
endif()
cmake_parse_arguments(CUSS "NO_COPY_DEBUG_SYMBOLS" "" "" ${ARGN})
# Force symbols file generation
cu_force_symbols_file(${TARGET_NAME})
# Copy symbols to a common location
if (NOT ${CUSS_NO_COPY_DEBUG_SYMBOLS})
cu_copy_symbols(${TARGET_NAME})
endif()
endfunction()
###############################################################################
# Setup Xcode automatic codesigning (required since Catalina).
function(cu_setup_xcode_codesigning TARGET_NAME)
# Set codesigning for macOS
if(APPLE)
if("${CMAKE_GENERATOR}" STREQUAL "Xcode")
# Force Xcode signing identity but only if defined to something valid (we will re-sign later anyway)
if("${CU_TEAM_IDENTIFIER}" STREQUAL "-")
set_target_properties(${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_DEVELOPMENT_TEAM "")
set_target_properties(${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "YES")
set_target_properties(${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "-")
set_target_properties(${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_CODE_SIGN_STYLE "Automatic")
else()
set_target_properties(${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_DEVELOPMENT_TEAM "${CU_TEAM_IDENTIFIER}")
endif()
# For xcode code signing to go deeply so all our dylibs are signed as well (will fail with xcode >= 11 otherwise)
# We also need to force the signing identity here, as xcode sometimes choose the wrong one when only given the DEVELOPMENT_TEAM value
set_target_properties(${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_OTHER_CODE_SIGN_FLAGS "--timestamp --deep --strict --force --options=runtime -s \"${CU_BINARY_SIGNING_IDENTITY}\"")
# Enable Hardened Runtime (required to notarize applications)
set_target_properties(${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_ENABLE_HARDENED_RUNTIME YES)
else()
# Silence CU_TEAM_IDENTIFIER unused variable warning
if(CU_TEAM_IDENTIFIER)
endif()
endif()
endif()
endfunction()
###############################################################################
# Setup BITCODE for iOS.
function(cu_setup_bitcode TARGET_NAME)
if(APPLE)
if(CMAKE_SYSTEM_NAME STREQUAL "iOS")
# Always force bitcode generation (not using the marker). Disable BITCODE if this is problematic
if("${CMAKE_GENERATOR}" STREQUAL "Xcode")
set_target_properties(${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_ENABLE_BITCODE YES)
set_target_properties(${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_BITCODE_GENERATION_MODE bitcode)
else()
target_compile_options(${TARGET_NAME} PRIVATE -fembed-bitcode)
endif()
endif()
endif()
endfunction()
###############################################################################
# Setup xcode scheme
function(cu_setup_xcode_scheme TARGET_NAME)
if(APPLE)
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
# Enable scheme generation
set_target_properties(${TARGET_NAME} PROPERTIES XCODE_GENERATE_SCHEME YES)
# Disable "Allow debugging when using document Versions Browser" which prevents xcode from passing args to the target
set_target_properties(${TARGET_NAME} PROPERTIES XCODE_SCHEME_DEBUG_DOCUMENT_VERSIONING NO)
endif()
endif()
endfunction()
###############################################################################
# Set Precompiled Headers on a target
function(cu_set_precompiled_headers TARGET_NAME HEADER_NAME)
# Currently, only activating for MSVC
# gcc is actually 2x slower when activating precompiled headers
# xcode doesn't need it, and it actually fails when compiling objective-c++ files
if(CMAKE_HOST_WIN32 AND MSVC AND NOT "${CMAKE_GENERATOR}" STREQUAL "Fastbuild")
target_precompile_headers(${TARGET_NAME} PRIVATE ${HEADER_NAME})
target_sources(${TARGET_NAME} PRIVATE ${HEADER_NAME})
endif()
endfunction()
###############################################################################
# Setup the sanitizers (ASAN, TSAN, UBSAN)
# Reads CU_ENABLE_ASAN, CU_ENABLE_TSAN and CU_ENABLE_UBSAN directly.
# Some combinations (flags + platform) may not be compatible
function(cu_setup_sanitizers TARGET_NAME)
set(ENABLE_ASAN ${CU_ENABLE_ASAN})
set(ENABLE_TSAN ${CU_ENABLE_TSAN})
set(ENABLE_UBSAN ${CU_ENABLE_UBSAN})
if (APPLE AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# Compatibility checks:
# - ASAN + TSAN -> NOK
# - Otherwise -> OK
if(ENABLE_ASAN AND ENABLE_TSAN)
message(FATAL_ERROR "ASAN and TSAN cannot be enabled together on Clang")
endif()
# Build LLVM sanitizer args
set(LLVM_SAN "")
if(ENABLE_ASAN)
list(APPEND LLVM_SAN "address")
endif()
if(ENABLE_TSAN)
list(APPEND LLVM_SAN "thread")
endif()
if(ENABLE_UBSAN)
list(APPEND LLVM_SAN "undefined")
endif()
list(JOIN LLVM_SAN "," LLVM_SAN_ARG)
# Nothing to do?
if(NOT LLVM_SAN_ARG)
return()
endif()
get_target_property(targetType ${TARGET_NAME} TYPE)
# Apply flags
target_compile_options(${TARGET_NAME} PRIVATE
$<$<CONFIG:Debug>:-fsanitize=${LLVM_SAN_ARG}>
$<$<CONFIG:Debug>:-fno-omit-frame-pointer>
)
if(NOT ${targetType} STREQUAL "STATIC_LIBRARY")
target_link_options(${TARGET_NAME} PRIVATE
$<$<CONFIG:Debug>:-fsanitize=${LLVM_SAN_ARG}>
)
endif()
elseif(MSVC)
# Compatibility checks:
# - Only ASAN available
if(ENABLE_ASAN)
target_compile_options(${TARGET_NAME} PRIVATE
$<$<CONFIG:Debug>:-fsanitize=address>
)
get_target_property(targetType ${TARGET_NAME} TYPE)
if(NOT ${targetType} STREQUAL "STATIC_LIBRARY")
target_link_options(${TARGET_NAME} PRIVATE
$<$<CONFIG:Debug>:/INCREMENTAL:NO>
)
endif()
# We have to change global flags as there is no cl.exe flag to cancel /RTC
string(REPLACE "/RTC1" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}" CACHE STRING "Force C flags for ASAN" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}" CACHE STRING "Force C++ flags for ASAN" FORCE)
endif()
endif()
endfunction()
###############################################################################
# Setup minimum version for Apple platforms
# Optional parameters:
# - "MACOS <Min version>" => Set the minimum version for macOS platform
# - "IOS <Min version>" => Set the minimum version for iOS platform
function(cu_setup_apple_minimum_versions)
# Parse arguments
cmake_parse_arguments(CUSAMV "" "MACOS;IOS" "" ${ARGN})
# Get the correct variable to check
if(CMAKE_SYSTEM_NAME AND CMAKE_SYSTEM_NAME STREQUAL "iOS")
set(PARAM_TO_READ "CUSAMV_IOS")
elseif(CMAKE_SYSTEM_NAME AND CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(PARAM_TO_READ "CUSAMV_MACOS")
endif()
if(${PARAM_TO_READ})
# We only want to override the minimum version if it is not already set (by this very function) or if it is set to a lower version
# This is to override the default minimum version set by the first project() call that automatically sets CMAKE_OSX_DEPLOYMENT_TARGET if not defined
# We use a global property to store the minimum version set by this function
get_property(minimumVersion GLOBAL PROPERTY CU_APPLE_MINIMUM_VERSION)
if(NOT DEFINED minimumVersion OR minimumVersion VERSION_LESS ${${PARAM_TO_READ}})
set(CMAKE_OSX_DEPLOYMENT_TARGET ${${PARAM_TO_READ}} CACHE INTERNAL "Force minimum target version" FORCE)
set_property(GLOBAL PROPERTY CU_APPLE_MINIMUM_VERSION ${${PARAM_TO_READ}})
endif()
endif()
endfunction()
###############################################################################
# Compile a macOS Asset Catalog and copy it to the resource folder.
# Does nothing if not on Apple platform or target is not a bundle.
# Mandatory parameters:
# - "TARGET_NAME" => The target name to which the resource will be added during its
# - PLATFORM "<platform>" => The platform to compile the asset catalog for (eg. macosx, iphoneos, ...)
# - SOURCES "<source folders>" => One or more folders to compile into the catalog file (eg. Assets.xcassets AppIcon.icon ...)
# Optional parameters:
# - APP_ICON "<icon name>" => The app icon name to use (default is AppIcon)
function(cu_compile_asset_catalog TARGET_NAME)
if(APPLE)
cu_is_macos_bundle(${TARGET_NAME} isBundle)
if(NOT ${isBundle})
return()
endif()
# Parse arguments
cmake_parse_arguments(CUAC "" "PLATFORM" "SOURCES" ${ARGN})
if(NOT CUAC_SOURCES)
message(FATAL_ERROR "cu_compile_asset_catalog: SOURCES argument is mandatory")
endif()
if(NOT CUAC_PLATFORM)
message(FATAL_ERROR "cu_compile_asset_catalog: PLATFORM argument is mandatory")
endif()
if(CUAC_APP_ICON)
set(APP_ICON_NAME ${CUAC_APP_ICON})
else()
set(APP_ICON_NAME "AppIcon")
endif()
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Assets.car
COMMAND xcrun actool --compile "${CMAKE_CURRENT_BINARY_DIR}/" --platform ${CUAC_PLATFORM} --app-icon ${APP_ICON_NAME} --minimum-deployment-target ${CMAKE_OSX_DEPLOYMENT_TARGET} --output-partial-info-plist "${CMAKE_CURRENT_BINARY_DIR}/dummy.plist" --enable-icon-stack-fallback-generation=disabled --include-all-app-icons ${CUAC_SOURCES}
DEPENDS ${CUAC_SOURCES}
VERBATIM
)
# Add a custom target
add_custom_target(GenerateAppleCatalog_${TARGET_NAME} DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/Assets.car SOURCES ${CUAC_SOURCES})
# Copy generated catalog to resource folder