-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCC.cmake
2013 lines (1812 loc) · 73.7 KB
/
CC.cmake
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
cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
# CSI escape sequences
if(NOT WIN32)
# cmake-format: off
string(ASCII 27 Esc)
set(CSI_Reset "${Esc}[m")
set(CSI_Bold "${Esc}[1m")
set(CSI_Red "${Esc}[31m")
set(CSI_Green "${Esc}[32m")
set(CSI_Yellow "${Esc}[33m")
set(CSI_Blue "${Esc}[34m")
set(CSI_Magenta "${Esc}[35m")
set(CSI_Cyan "${Esc}[36m")
set(CSI_White "${Esc}[37m")
set(CSI_BoldRed "${Esc}[1;31m")
set(CSI_BoldGreen "${Esc}[1;32m")
set(CSI_BoldYellow "${Esc}[1;33m")
set(CSI_BoldBlue "${Esc}[1;34m")
set(CSI_BoldMagenta "${Esc}[1;35m")
set(CSI_BoldCyan "${Esc}[1;36m")
set(CSI_BoldWhite "${Esc}[1;37m")
# cmake-format: on
# Replaces ANSI CSI sequences in a string or list with placeholders.
# This avoids issues when processing text with CSI sequences, such as
# when iterating over lists in CMake.
macro(csi_replace text)
string(REPLACE "${CSI_Reset}" "_CSI_Reset_" ${text} "${${text}}")
string(REPLACE "${CSI_Bold}" "_CSI_Bold_" ${text} "${${text}}")
string(REPLACE "${CSI_Red}" "_CSI_Red_" ${text} "${${text}}")
string(REPLACE "${CSI_Green}" "_CSI_Green_" ${text} "${${text}}")
string(REPLACE "${CSI_Yellow}" "_CSI_Yellow_" ${text} "${${text}}")
string(REPLACE "${CSI_Blue}" "_CSI_Blue_" ${text} "${${text}}")
string(REPLACE "${CSI_Magenta}" "_CSI_Magenta_" ${text} "${${text}}")
string(REPLACE "${CSI_Cyan}" "_CSI_Cyan_" ${text} "${${text}}")
string(REPLACE "${CSI_White}" "_CSI_White_" ${text} "${${text}}")
string(REPLACE "${CSI_BoldRed}" "_CSI_BoldRed_" ${text} "${${text}}")
string(REPLACE "${CSI_BoldGreen}" "_CSI_BoldGreen_" ${text} "${${text}}")
string(REPLACE "${CSI_BoldYellow}" "_CSI_BoldYellow_" ${text} "${${text}}")
string(REPLACE "${CSI_BoldBlue}" "_CSI_BoldBlue_" ${text} "${${text}}")
string(REPLACE "${CSI_BoldMagenta}" "_CSI_BoldMagenta_" ${text} "${${text}}")
string(REPLACE "${CSI_BoldCyan}" "_CSI_BoldCyan_" ${text} "${${text}}")
string(REPLACE "${CSI_BoldWhite}" "_CSI_BoldWhite_" ${text} "${${text}}")
endmacro()
# Restores ANSI CSI sequences from placeholders in a string or list.
# Reverses the changes made by csi_replace.
macro(csi_restore text)
string(REPLACE "_CSI_Reset_" "${CSI_Reset}" ${text} "${${text}}")
string(REPLACE "_CSI_Bold_" "${CSI_Bold}" ${text} "${${text}}")
string(REPLACE "_CSI_Red_" "${CSI_Red}" ${text} "${${text}}")
string(REPLACE "_CSI_Green_" "${CSI_Green}" ${text} "${${text}}")
string(REPLACE "_CSI_Yellow_" "${CSI_Yellow}" ${text} "${${text}}")
string(REPLACE "_CSI_Blue_" "${CSI_Blue}" ${text} "${${text}}")
string(REPLACE "_CSI_Magenta_" "${CSI_Magenta}" ${text} "${${text}}")
string(REPLACE "_CSI_Cyan_" "${CSI_Cyan}" ${text} "${${text}}")
string(REPLACE "_CSI_White_" "${CSI_White}" ${text} "${${text}}")
string(REPLACE "_CSI_BoldRed_" "${CSI_BoldRed}" ${text} "${${text}}")
string(REPLACE "_CSI_BoldGreen_" "${CSI_BoldGreen}" ${text} "${${text}}")
string(REPLACE "_CSI_BoldYellow_" "${CSI_BoldYellow}" ${text} "${${text}}")
string(REPLACE "_CSI_BoldBlue_" "${CSI_BoldBlue}" ${text} "${${text}}")
string(REPLACE "_CSI_BoldMagenta_" "${CSI_BoldMagenta}" ${text} "${${text}}")
string(REPLACE "_CSI_BoldCyan_" "${CSI_BoldCyan}" ${text} "${${text}}")
string(REPLACE "_CSI_BoldWhite_" "${CSI_BoldWhite}" ${text} "${${text}}")
endmacro()
endif()
# cmake-format: off
macro(cc_set_policies)
# the policy allows us to change options without caching
cmake_policy(SET CMP0077 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
# the policy allows us to change set(CACHE) without caching
if(POLICY CMP0126)
cmake_policy(SET CMP0126 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0126 NEW)
endif()
# the policy uses the download time for timestamp, instead of the timestamp in the archive. This allows for proper
# rebuilds when a projects url changes
if(POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0135 NEW)
endif()
# treat relative git repository paths as being relative to the parent project's remote
if(POLICY CMP0150)
cmake_policy(SET CMP0150 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0150 NEW)
endif()
# the policy convert relative paths to absolute according to above rules
cmake_policy(SET CMP0076 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0076 NEW)
# the policy does not dereference variables or interpret keywords that have been quoted or bracketed.
cmake_policy(SET CMP0054 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0054 NEW)
# keep relying on CMake FindBoost module instead of the one shipping with Boost itself.
# Required by our "hack" to support a mix of shared and static boost components.
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.30.0)
cmake_policy(SET CMP0167 OLD)
set(CMAKE_POLICY_DEFAULT_CMP0167 OLD)
endif()
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28.0)
cmake_policy(SET CMP0169 OLD)
set(CMAKE_POLICY_DEFAULT_CMP0169 OLD)
endif()
# no up-to-date messages on installation
if(NOT DEFINED CMAKE_INSTALL_MESSAGE)
set(CMAKE_INSTALL_MESSAGE "LAZY")
endif()
set(CMAKE_WARN_DEPRECATED OFF CACHE BOOL "Disable deprecated warnings" FORCE)
endmacro()
cc_set_policies()
if(NOT CC_MESSAGE_MAX_LENGTH)
set(CC_MESSAGE_MAX_LENGTH 360 CACHE INTERNAL "Max length of one line message")
endif()
if(NOT DEFINED CC_IMPORTED_PACKAGES)
set(CC_IMPORTED_PACKAGES "" CACHE INTERNAL "All imported packages")
endif()
if(NOT DEFINED CC_IMPORT_LINK_DIRECTORIES)
set(CC_IMPORT_LINK_DIRECTORIES "" CACHE INTERNAL "All link directories of imported packages")
endif()
if(NOT DEFINED CC_IMPORT_INCLUDE_DIRECTORIES)
set(CC_IMPORT_INCLUDE_DIRECTORIES "" CACHE INTERNAL "All include directories of imported packages")
endif()
if(DEFINED ENV{CC_SOURCE_CACHE})
set(CC_SOURCE_CACHE_DEFAULT $ENV{CC_SOURCE_CACHE})
else()
set(CC_SOURCE_CACHE_DEFAULT ${CMAKE_BINARY_DIR}/_deps)
endif()
set(CC_SOURCE_CACHE ${CC_SOURCE_CACHE_DEFAULT} CACHE PATH "Directory to download CC dependencies")
# Namespace
macro(cc_get_parent_namespace variable)
get_directory_property(__PARENT_DIR__ PARENT_DIRECTORY)
get_property(${variable} DIRECTORY ${__PARENT_DIR__} PROPERTY __CURRENT_NAMESPACE__)
if(${variable} STREQUAL "")
message(FATAL_ERROR "Can not get namespace for ${CMAKE_CURRENT_SOURCE_DIR}")
endif()
endmacro()
macro(cc_get_current_namespace variable)
get_property(${variable} DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" PROPERTY __CURRENT_NAMESPACE__)
endmacro()
function(cc_set_namespace)
cmake_parse_arguments(CC_ARGS "ROOT" "" "" ${ARGV})
# Get the unparsed arguments as the namespace name
set(namespace "${CC_ARGS_UNPARSED_ARGUMENTS}")
if(CC_ARGS_ROOT)
# If 'ROOT' option is used, ensure a namespace is provided
if(NOT namespace)
message(FATAL_ERROR "${CSI_BoldRed}When using the ROOT option, you must provide a root namespace.${CSI_Reset}")
endif()
# Set the root namespace for the current directory
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" PROPERTY __CURRENT_NAMESPACE__ "${namespace}")
else()
cc_get_parent_namespace(__PARENT_NAMESPACE__)
if(namespace)
# Use the provided namespace
set(current_namespace "${namespace}")
else()
# If no namespace is provided, use the current directory name
get_filename_component(current_namespace "${CMAKE_CURRENT_SOURCE_DIR}" NAME)
endif()
if(__PARENT_NAMESPACE__)
# Combine the parent namespace and current namespace
set(full_namespace "${__PARENT_NAMESPACE__}::${current_namespace}")
else()
# Use the current namespace as the full namespace
set(full_namespace "${current_namespace}")
endif()
# Set the namespace property for the current directory
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" PROPERTY __CURRENT_NAMESPACE__ "${full_namespace}")
endif()
endfunction()
# logs
function(prompt_once type message_text)
get_property(existing_messages GLOBAL PROPERTY PROMPTED_MESSAGES)
string(FIND ";${existing_messages};" ";${type}:${message_text};" index)
if(index EQUAL -1)
message(${type} "${message_text}")
set_property(GLOBAL APPEND PROPERTY PROMPTED_MESSAGES "${type}:${message_text}")
endif()
endfunction()
function(cc_truncate_list OUTPUT REMOVE_SOURCE_PREFIX)
set(items ${ARGN})
if(REMOVE_SOURCE_PREFIX)
set(processed_items)
foreach(item IN LISTS items)
string(REGEX REPLACE "^${CMAKE_SOURCE_DIR}/?" "" item "${item}")
list(APPEND processed_items "${item}")
endforeach()
set(items "${processed_items}")
endif()
set(final_items)
set(current_length 0)
foreach(elem IN LISTS items)
string(LENGTH ${elem} len)
if(final_items STREQUAL "")
math(EXPR new_length "${current_length} + ${len}")
else()
math(EXPR new_length "${current_length} + ${len} + 1")
endif()
# message(STATUS "[DEBUG] new_length='${new_length}' CC_MESSAGE_MAX_LENGTH='${CC_MESSAGE_MAX_LENGTH}'")
if(DEFINED CC_MESSAGE_MAX_LENGTH AND new_length GREATER CC_MESSAGE_MAX_LENGTH)
list(APPEND final_items "...")
break()
else()
list(APPEND final_items "${elem}")
set(current_length "${new_length}")
endif()
endforeach()
set(${OUTPUT} ${final_items} PARENT_SCOPE)
endfunction()
function(cc_log_get_id target_name output_variable)
get_property(logid_value TARGET ${target_name} PROPERTY LOGID)
set(${output_variable} "${logid_value}" PARENT_SCOPE)
endfunction()
function(cc_log_set_id logid)
set(all_targets ${ARGN})
foreach(target_name IN LISTS all_targets)
set_property(TARGET ${target_name} PROPERTY LOGID ${logid})
endforeach()
endfunction()
function(cc_log_append target_or_logid logmessage)
if(TARGET target_or_logid)
cc_log_get_id(${target_or_logid} logid)
elseif(target_or_logid STREQUAL "")
return()
else()
set(logid ${target_or_logid})
endif()
set(global_logid "CC_LOG_${logid}")
string(REPLACE ";" ", " logmessage "${logmessage}")
set_property(GLOBAL APPEND PROPERTY ${global_logid} "${logmessage}")
endfunction()
function(cc_log_flush logid)
if(logid STREQUAL "")
return()
endif()
set(global_logid "CC_LOG_${logid}")
get_property(all_logs GLOBAL PROPERTY ${global_logid})
if(NOT DEFINED all_logs OR all_logs STREQUAL "")
message(STATUS "No logs for ${logid}")
return()
endif()
csi_replace(all_logs)
message(STATUS "${logid}:")
list(TRANSFORM all_logs PREPEND " + ")
foreach(restored_log IN LISTS all_logs)
csi_restore(restored_log)
message(STATUS "${restored_log}")
endforeach()
set_property(GLOBAL PROPERTY ${global_logid} "")
endfunction()
# glob
function(cc_execlude_incompatible files output_files delete_files)
set(PLATFORM)
if(WIN32)
set(PLATFORM "windows")
elseif(APPLE)
set(PLATFORM "macos")
elseif(UNIX)
set(PLATFORM "linux")
endif()
set(FILTERED_FILES)
set(EXCLUDED_FILES)
set(PLATFORM_SPECIFIC_FILES)
set(DUMMY_FILES)
set(UNRELATED_FILES)
foreach(file ${files})
if(file MATCHES "-${PLATFORM}\\.")
list(APPEND PLATFORM_SPECIFIC_FILES ${file})
elseif(file MATCHES "-generic\\.")
list(APPEND DUMMY_FILES ${file})
elseif(file MATCHES "-(linux|macos|windows)\\.")
list(APPEND EXCLUDED_FILES ${file})
else()
list(APPEND UNRELATED_FILES ${file})
endif()
endforeach()
set(PROCESSED_PREFIXES)
foreach(file ${PLATFORM_SPECIFIC_FILES})
string(REGEX REPLACE "-${PLATFORM}\\..*" "" prefix ${file})
list(APPEND PROCESSED_PREFIXES ${prefix})
list(APPEND FILTERED_FILES ${file})
endforeach()
foreach(file ${DUMMY_FILES})
string(REGEX REPLACE "-generic\\..*" "" prefix ${file})
if(prefix IN_LIST PROCESSED_PREFIXES)
list(APPEND EXCLUDED_FILES ${file})
else()
list(APPEND FILTERED_FILES ${file})
endif()
endforeach()
list(APPEND FILTERED_FILES ${UNRELATED_FILES})
set(${output_files} ${FILTERED_FILES} PARENT_SCOPE)
set(${delete_files} ${EXCLUDED_FILES} PARENT_SCOPE)
endfunction()
function(cc_glob OUTPUTS PATTERNS)
if(NOT ${PATTERNS})
return()
endif()
set(options EXCLUDE FILTER_OS_ARCH)
set(oneValueArgs LOGID)
set(multiValueArgs)
# Parse additional options
cmake_parse_arguments(CC_ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
set(MATCHED_FILES)
# Loop through each pattern in the provided PATTERNS list
foreach(pattern ${${PATTERNS}})
set(CURRENT_FILES)
# Perform recursive search if pattern contains '**'
if(${pattern} MATCHES ".*\\*\\*.*")
file(GLOB_RECURSE CURRENT_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${pattern})
# Perform single-directory search if pattern contains '*'
elseif(${pattern} MATCHES ".*\\*.*")
file(GLOB CURRENT_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${pattern})
# If no glob pattern is detected, directly append the file
elseif(IS_DIRECTORY ${pattern})
file(GLOB_RECURSE CURRENT_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${pattern}/*.*)
else()
list(APPEND CURRENT_FILES ${pattern})
endif()
list(APPEND MATCHED_FILES ${CURRENT_FILES})
endforeach()
cc_execlude_incompatible("${MATCHED_FILES}" FILTERED_FILES EXCLUDED_FILES)
if(EXCLUDED_FILES AND CC_ARGS_LOGID)
cc_log_append(${CC_ARGS_LOGID} "${CSI_Yellow}Incompatible platform files: ${EXCLUDED_FILES}${CSI_Reset}")
endif()
if(CC_ARGS_EXCLUDE)
# cc_truncate_list(truncated_files TRUE ${FILTERED_FILES})
# cc_log_append(${CC_ARGS_LOGID} "Excludes: ${truncated_files}")
list(REMOVE_ITEM ${OUTPUTS} ${FILTERED_FILES})
set(${OUTPUTS} ${${OUTPUTS}} PARENT_SCOPE)
else()
set(${OUTPUTS} ${FILTERED_FILES} PARENT_SCOPE)
endif()
endfunction()
function(cc_warnings)
set(all_targets ${ARGN})
foreach(target IN LISTS all_targets)
target_compile_options(
${target} PRIVATE
$<$<COMPILE_LANGUAGE:C,CXX,ASM>:
-Wformat=2
-Wsign-compare
-Wmissing-field-initializers
-Wwrite-strings
-Wvla
-Wcast-align
-Wcast-qual
-Wswitch-enum
-Wundef
-Wdouble-promotion
-Wdate-time
-Wfloat-equal
-fno-strict-aliasing
-pipe
-Wunused-const-variable
-Wall
-Wextra
-fno-common
-fvisibility=default
>
)
target_compile_options(
${target} PRIVATE
$<$<AND:$<COMPILE_LANGUAGE:C,CXX,ASM>,$<CXX_COMPILER_ID:GNUCC,GNUCXX>>:
-Wtrampolines
-Wl,-z,relro,-z,now
-Wmissing-format-attribute
-Wstrict-overflow=2
-Wswitch-default
-Wconversion
-Wunused
-Wpointer-arith
>
)
if(CMAKE_COMPILER_IS_GNUCC)
target_compile_options(
${target} PRIVATE
$<$<VERSION_GREATER:$<C_COMPILER_VERSION>,4.3.0>:-Wlogical-op>
$<$<VERSION_GREATER:$<C_COMPILER_VERSION>,4.8.0>:-Wno-array-bounds>
# GCC (at least 4.8.4) has a bug where it'll find unreachable free() calls and declare that the code is
# trying to free a stack pointer.
$<$<VERSION_GREATER:$<C_COMPILER_VERSION>,4.8.4>:-Wno-free-nonheap-object>
$<$<VERSION_GREATER:$<C_COMPILER_VERSION>,6.0.0>:-Wduplicated-cond -Wnull-dereference>
$<$<VERSION_GREATER:$<C_COMPILER_VERSION>,7.0.0>:-Wduplicated-branches -Wrestrict>
)
# shared or module
target_link_options(${target} PRIVATE $<$<NOT:$<BOOL:${APPLE}>>:-Wl,--fatal-warnings -Wl,--no-undefined>)
endif()
if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
target_compile_options(
${target} PRIVATE
$<$<COMPILE_LANGUAGE:C,CXX,ASM>:
-Wmissing-variable-declarations -Wcomma
-Wused-but-marked-unused -Wnewline-eof -fcolor-diagnostics
>
$<$<VERSION_GREATER:$<C_COMPILER_VERSION>,7.0.0>:-Wimplicit-fallthrough>
)
endif()
target_compile_options(
${target} PRIVATE
$<$<COMPILE_LANG_AND_ID:C,AppleClang,Clang,GNUCC,GNUCXX>:
-Wmissing-prototypes
-Wold-style-definition
-Wstrict-prototypes
>
$<$<COMPILE_LANG_AND_ID:CXX,AppleClang,Clang,GNUCC,GNUCXX>:
-Wmissing-declarations
-Weffc++
>
)
# In GCC, -Wmissing-declarations is the C++ spelling of -Wmissing-prototypes and using the wrong one is an error. In
# Clang, -Wmissing-prototypes is the spelling for both and -Wmissing-declarations is some other warning.
#
# https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Warning-Options.html#Warning-Options
# https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-prototypes
# https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-declarations
target_compile_options(
${target} PRIVATE
$<$<COMPILE_LANG_AND_ID:CXX,AppleClang,Clang>:-Wmissing-prototypes>
$<$<COMPILE_LANG_AND_ID:C,GNUCC>:-Wc++-compat>
$<$<VERSION_GREATER:$<C_COMPILER_VERSION>,4.7.99>:-Wshadow>
$<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,4.7.99>:-Wshadow>
$<$<VERSION_GREATER:$<ASM_COMPILER_VERSION>,4.7.99>:-Wshadow>
)
endforeach()
endfunction()
function(cc_small_footprint)
set(all_targets ${ARGN})
foreach(target IN LISTS all_targets)
cc_log_append(${target} "${CSI_Blue}Small footprint${CSI_Reset}")
target_compile_options(
${target} PRIVATE
-Os
$<
$<AND:
$<OR:$<COMPILE_LANGUAGE:C>, $<COMPILE_LANGUAGE:CXX>, $<COMPILE_LANGUAGE:ASM>>,
$<OR:$<CXX_COMPILER_ID:GNUCC>, $<CXX_COMPILER_ID:GNUCXX>>
>:
--specs=nosys.specs --specs=nano.specs
>
$<
$<AND:
$<OR:$<COMPILE_LANGUAGE:C>, $<COMPILE_LANGUAGE:CXX>, $<COMPILE_LANGUAGE:ASM>>,
$<OR:$<CXX_COMPILER_ID:AppleClang>, $<CXX_COMPILER_ID:Clang>>
>:
-flto=thin
>
$<
$<OR:$<CXX_COMPILER_ID:GNU>, $<CXX_COMPILER_ID:Clang>>:
-ffunction-sections -fdata-sections
>
)
target_link_options(
${target} PRIVATE
$<$<OR:$<LINKER_ID:GNU>,$<LINKER_ID:LLD>>:-Wl,--gc-sections>
)
endforeach()
endfunction()
function(cc_append_if_absent LIST VALUE)
get_property(CACHE_TYPE CACHE ${LIST} PROPERTY TYPE)
if(NOT "${VALUE}" IN_LIST ${LIST})
if(CACHE_TYPE)
get_property(VAR_HELPSTRING CACHE ${LIST} PROPERTY HELPSTRING)
if(VAR_HELPSTRING)
set(${LIST} "${${LIST}};${VALUE}" CACHE ${CACHE_TYPE} "${VAR_HELPSTRING}")
else()
set(${LIST} "${${LIST}};${VALUE}" CACHE ${CACHE_TYPE} "")
endif()
else()
list(APPEND ${LIST} "${VALUE}")
set(${LIST} "${${LIST}}" PARENT_SCOPE)
endif()
endif()
endfunction()
function(cc_get_first_defined OUTPUT)
foreach(varname ${ARGN})
if(DEFINED ${varname})
set(${OUTPUT} "${${varname}}" PARENT_SCOPE)
return()
endif()
endforeach()
endfunction()
function(cc_find_package Name found)
string(TOUPPER "${Name}" upper_case_name)
string(TOLOWER "${Name}" lower_case_name)
string(REPLACE "-" "_" lower_case_name "${lower_case_name}")
string(REPLACE "-" "_" upper_case_name "${upper_case_name}")
set(flags "NO_CMAKE;USE_PKG_CONFIG")
cmake_parse_arguments(CC "${flags}" "" "" "${ARGN}")
string(REPLACE " " ";" EXTRA_ARGS "${CC_UNPARSED_ARGUMENTS}")
if(NOT CC_NO_CMAKE)
find_package(${Name} ${EXTRA_ARGS})
endif()
if(NOT ${Name}_FOUND AND CC_USE_PKG_CONFIG)
find_package(PkgConfig)
if(PKGCONFIG_FOUND)
set(OLD_PKG_CONFIG_PATH $ENV{PKG_CONFIG_PATH})
set(ENV{PKG_CONFIG_PATH} "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig")
pkg_check_modules(${Name} ${lower_case_name})
set(ENV{PKG_CONFIG_PATH} ${OLD_PKG_CONFIG_PATH})
endif()
endif()
set(${found} ${${Name}_FOUND} PARENT_SCOPE)
if(${Name}_FOUND)
cc_append_if_absent(CC_IMPORTED_PACKAGES ${Name})
message(STATUS "CC::${Name} imported:")
cc_get_first_defined(_version
${Name}_VERSION
${lower_case_name}_VERSION
${upper_case_name}_VERSION
)
if(_version)
message(STATUS " + Version: ${_version}")
endif()
cc_get_first_defined(_include_dirs
${Name}_INCLUDE_DIRS
${lower_case_name}_INCLUDE_DIRS
${upper_case_name}_INCLUDE_DIRS
${Name}_INCLUDE_DIR
${lower_case_name}_INCLUDE_DIR
${upper_case_name}_INCLUDE_DIR
)
if(_include_dirs)
message(STATUS " + Include directories: ${_include_dirs}")
cc_append_if_absent(CC_IMPORT_INCLUDE_DIRECTORIES "${_include_dirs}")
endif()
cc_get_first_defined(_libraries
${Name}_LIBRARIES
${lower_case_name}_LIBRARIES
${upper_case_name}_LIBRARIES
)
if(_libraries)
cc_truncate_list(truncated_libraries TRUE ${_libraries})
message(STATUS " + Include directories: ${truncated_libraries}")
endif()
cc_get_first_defined(_library_dirs
${Name}_LIBRARY_DIRS
${lower_case_name}_LIBRARY_DIRS
${upper_case_name}_LIBRARY_DIRS
${Name}_LIBRARY_DIR
${lower_case_name}_LIBRARY_DIR
${upper_case_name}_LIBRARY_DIR
)
if(_library_dirs)
cc_truncate_list(truncated_directories TRUE ${_library_dirs})
message(STATUS " + Library directories: ${truncated_directories}")
cc_append_if_absent(CC_IMPORT_LINK_DIRECTORIES "${_library_dirs}")
endif()
endif()
endfunction()
function(cc_declare_fetch Name)
FetchContent_Declare(${Name} ${ARGN})
endfunction()
function(cc_fetch_package Name DOWNLOAD_ONLY populated)
set(${populated} FALSE PARENT_SCOPE)
string(TOLOWER "${Name}" lower_case_name)
if(DOWNLOAD_ONLY)
FetchContent_Populate(${Name})
else()
FetchContent_MakeAvailable(${Name})
endif()
set(${populated} TRUE PARENT_SCOPE)
endfunction()
function(cc_import Name)
set(flags QUIET REQUIRED PRECOMPILED DOWNLOAD_ONLY)
set(oneValueArgs URI REPO SOURCE_DIR BINARY_DIR SUBBUILD_DIR)
set(multiValueArgs CONFIGURE_COMMAND BUILD_COMMAND INSTALL_COMMAND)
cmake_parse_arguments(CC "${flags}" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}")
if(NOT CC_REPO AND NOT CC_URI)
set(find_args)
if(CC_QUIET)
list(APPEND find_args QUIET)
endif()
if(CC_REQUIRED)
list(APPEND find_args REQUIRED)
endif()
if(CC_UNPARSED_ARGUMENTS)
list(APPEND find_args ${CC_UNPARSED_ARGUMENTS})
endif()
cc_find_package(${Name} found ${find_args})
return()
endif()
include(FetchContent)
string(TOLOWER "${Name}" lower_case_name)
string(TOUPPER "${Name}" upper_case_name)
set(source_dir "${CC_SOURCE_CACHE}/${lower_case_name}-src")
set(binary_dir "${CMAKE_BINARY_DIR}/deps/${lower_case_name}-build")
set(subbuild_dir "${CMAKE_BINARY_DIR}/deps/${lower_case_name}-subbuild")
set(CC_PACKAGE_${Name}_SOURCE_DIR "${source_dir}" CACHE INTERNAL "")
set(CC_PACKAGE_${Name}_BINARY_DIR "${binary_dir}" CACHE INTERNAL "")
if(CC_REPO)
cmake_parse_arguments(CC_REPO "" "VERSION;COMMIT;TAG:BRANCH" "" "${CC_UNPARSED_ARGUMENTS}")
set(CC_UNPARSED_ARGUMENTS ${CC_REPO_UNPARSED_ARGUMENTS})
set(git_repo)
if(CC_REPO MATCHES "^github:")
string(REGEX REPLACE "^github:" "" _gh "${CC_REPO}")
set(git_repo "https://github.com/${_gh}.git")
else()
set(git_repo "${CC_REPO}")
endif()
set(git_tag)
if(CC_REPO_COMMIT)
set(git_tag ${CC_REPO_COMMIT})
elseif(CC_REPO_BRANCH)
set(git_tag ${CC_REPO_BRANCH})
elseif(CC_REPO_TAG)
set(git_tag ${CC_REPO_TAG})
elseif(CC_REPO_VERSION)
set(git_tag "v${CC_REPO_VERSION}")
else()
set(git_tag "master")
endif()
message(STATUS "CC::${Name} Using GIT_REPOSITORY=${git_repo} GIT_TAG=${git_tag}")
cc_declare_fetch(
${Name}
GIT_REPOSITORY "${git_repo}"
GIT_TAG "${git_tag}"
GIT_SHALLOW ON
GIT_PROGRESS ON
SOURCE_DIR "${source_dir}"
BINARY_DIR "${binary_dir}"
SUBBUILD_DIR "${subbuild_dir}"
${CC_REPO_UNPARSED_ARGUMENTS}
)
if(NOT DEFINED CC_DOWNLOAD_ONLY)
set(CC_DOWNLOAD_ONLY FALSE)
endif()
cc_fetch_package(${Name} ${CC_DOWNLOAD_ONLY} populated)
if(populated)
cc_append_if_absent(CC_IMPORTED_PACKAGES ${Name})
endif()
return()
endif()
if(NOT CC_PRECOMPILED)
if(CC_CONFIGURE_COMMAND OR CC_BUILD_COMMAND OR CC_INSTALL_COMMAND)
cc_declare_fetch(
${Name}
URL "${CC_URI}"
SOURCE_DIR "${source_dir}"
BINARY_DIR "${binary_dir}"
SUBBUILD_DIR "${subbuild_dir}"
${CC_UNPARSED_ARGUMENTS}
)
cc_fetch_package(${Name} TRUE populated)
if(NOT populated)
return()
endif()
set(CC_PACKAGE_${Name}_POPULATED CACHE INTERNAL TRUE "")
file(TOUCH ${subbuild_dir}/.POPULATED)
if(NOT CC_PACKAGE_${Name}_CONFIGURED AND NOT EXISTS ${subbuild_dir}/.CONFIGURED)
string(REPLACE ";" " " CC_CONFIGURE_COMMAND "${CC_CONFIGURE_COMMAND}")
string(REGEX REPLACE "^['\"]|['\"]$" "" CC_CONFIGURE_COMMAND "${CC_CONFIGURE_COMMAND}")
string(FIND "${CC_CONFIGURE_COMMAND}" " " space_index)
if(space_index EQUAL -1)
set(configure_executable "${CC_CONFIGURE_COMMAND}")
else()
string(SUBSTRING "${CC_CONFIGURE_COMMAND}" 0 ${space_index} configure_executable)
endif()
if(NOT IS_ABSOLUTE ${configure_executable})
set(configure_executable "${source_dir}/${configure_executable}")
endif()
if(space_index EQUAL -1)
set(CC_CONFIGURE_COMMAND ${configure_executable})
else()
string(SUBSTRING "${CC_CONFIGURE_COMMAND}" ${space_index} -1 remaining_command)
string(STRIP "${remaining_command}" remaining_command)
set(CC_CONFIGURE_COMMAND ${configure_executable} ${remaining_command})
endif()
string(REGEX MATCH "--prefix[= ]([^ ]+)" match_result "${CC_CONFIGURE_COMMAND}")
if(match_result)
string(REGEX REPLACE "--prefix[= ]" "" prefix_path "${match_result}")
if(NOT EXISTS "${prefix_path}")
message(WARNING "Prefix path does not exist. Creating: ${prefix_path}")
file(MAKE_DIRECTORY "${prefix_path}")
if(EXISTS "${prefix_path}")
message(STATUS "Prefix path successfully created: ${prefix_path}")
else()
message(FATAL_ERROR "Failed to create prefix path: ${prefix_path}")
endif()
endif()
endif()
execute_process(
COMMAND ${CC_CONFIGURE_COMMAND}
WORKING_DIRECTORY "${binary_dir}"
RESULT_VARIABLE configure_result
OUTPUT_VARIABLE configure_output
ERROR_VARIABLE configure_error
ECHO_OUTPUT_VARIABLE ECHO_ERROR_VARIABLE
)
if(NOT configure_result EQUAL 0)
message(FATAL_ERROR "Configuring ${Name} failed")
endif()
file(TOUCH ${subbuild_dir}/.CONFIGURED)
set(CC_PACKAGE_${Name}_CONFIGURED CACHE INTERNAL TRUE "")
endif()
if(NOT CC_PACKAGE_${Name}_COMPILED AND NOT EXISTS ${subbuild_dir}/.COMPILED)
# string(REPLACE ";" " " CC_BUILD_COMMAND "${CC_BUILD_COMMAND}")
# string(REGEX REPLACE "^['\"]|['\"]$" "" CC_BUILD_COMMAND "${CC_BUILD_COMMAND}")
message(STATUS "Build: ${CC_BUILD_COMMAND}")
execute_process(
COMMAND ${CC_BUILD_COMMAND}
WORKING_DIRECTORY "${binary_dir}"
RESULT_VARIABLE build_result
OUTPUT_VARIABLE build_output
ERROR_VARIABLE build_error
ECHO_OUTPUT_VARIABLE ECHO_ERROR_VARIABLE
)
if(NOT build_result EQUAL 0)
message(FATAL_ERROR "Building ${Name} failed")
endif()
file(TOUCH ${subbuild_dir}/.COMPILED)
set(CC_PACKAGE_${Name}_COMPILED CACHE INTERNAL TRUE "")
endif()
if(NOT CC_PACKAGE_${Name}_INSTALLED AND NOT EXISTS ${subbuild_dir}/.INSTALLED)
# string(REPLACE ";" " " CC_INSTALL_COMMAND "${CC_INSTALL_COMMAND}")
# string(REGEX REPLACE "^['\"]|['\"]$" "" CC_INSTALL_COMMAND "${CC_INSTALL_COMMAND}")
execute_process(
COMMAND ${CC_INSTALL_COMMAND}
WORKING_DIRECTORY "${binary_dir}"
RESULT_VARIABLE install_result
OUTPUT_VARIABLE install_output
ERROR_VARIABLE install_error
ECHO_OUTPUT_VARIABLE ECHO_ERROR_VARIABLE
)
if(NOT install_result EQUAL 0)
message(FATAL_ERROR "Installing ${Name} failed")
endif()
file(TOUCH ${subbuild_dir}/.INSTALLED)
set(CC_PACKAGE_${Name}_INSTALLED CACHE INTERNAL TRUE "")
endif()
cc_find_package(
${Name} found
QUIET
USE_PKG_CONFIG
NO_DEFAULT_PATH
HINTS ${CMAKE_INSTALL_PREFIX}
)
else()
cc_declare_fetch(
${Name}
URL "${CC_URI}"
SOURCE_DIR "${source_dir}"
BINARY_DIR "${binary_dir}"
${CC_UNPARSED_ARGUMENTS}
)
FetchContent_MakeAvailable(${Name})
cc_append_if_absent(CC_IMPORTED_PACKAGES ${Name})
endif()
else()
cc_declare_fetch(
${Name}
URL "${CC_URI}"
SOURCE_DIR "${source_dir}"
${CC_UNPARSED_ARGUMENTS}
)
cc_fetch_package(${Name} TRUE populated)
if(NOT populated)
return()
endif()
if(EXISTS ${source_dir}/lib)
cc_append_if_absent(CC_IMPORT_LINK_DIRECTORIES ${source_dir}/lib)
endif()
if(EXISTS ${source_dir}/include)
cc_append_if_absent(CC_IMPORT_INCLUDE_DIRECTORIES ${source_dir}/include)
endif()
cc_find_package(${Name} found QUIET NO_DEFAULT_PATH HINTS ${source_dir})
endif()
endfunction()
cc_import(
doctest
REPO "github:doctest/doctest" VERSION 2.4.11
)
# dpendency formats:
# single package or target: pthread, your_cmake_target
# path to precompiled library: /path/to/compied_library
# single package can be found via find_package: Boost::system
# multiple package can be found via find_package: Boost::system,filesystem
function(cc_depends target)
set(all_depends ${ARGN})
if(CC_IMPORT_LINK_DIRECTORIES)
target_link_directories(${target} PUBLIC ${CC_IMPORT_LINK_DIRECTORIES})
endif()
if(CC_IMPORT_INCLUDE_DIRECTORIES)
target_include_directories(${target} PUBLIC ${CC_IMPORT_INCLUDE_DIRECTORIES})
endif()
foreach(DEPNEDS IN LISTS all_depends)
# Find the position of "::" in the dependencies string
string(FIND "${DEPNEDS}" "::" pos)
set(linkage_type "PUBLIC")
get_target_property(TARGET_TYPE ${target} TYPE)
if("${TARGET_TYPE}" STREQUAL "INTERFACE_LIBRARY")
set(linkage_type "INTERFACE")
endif()
cc_log_get_id(${target} logid)
if(pos GREATER -1)
# Separate the package name and the components part
math(EXPR cstart "${pos} + 2")
string(SUBSTRING "${DEPNEDS}" 0 ${pos} package_name)
string(SUBSTRING "${DEPNEDS}" ${cstart} -1 components_str)
# Handle the components list (split by commas or spaces)
string(REPLACE "," ";" components_list "${components_str}")
string(REPLACE " " ";" components_list "${components_list}")
# List to store missing components
set(missing_components)
# Check if each component already exists as a target
foreach(component IN LISTS components_list)
if(NOT TARGET ${package_name}::${component})
prompt_once(STATUS "Component ${package_name}::${component} not found, will attempt to find it.")
list(APPEND missing_components ${component}) # Add missing component to the list
else()
prompt_once(STATUS "Package ${package_name}::${component} already found, skipping.")
endif()
endforeach()
# If there are missing components, call find_package to locate them
if(missing_components)
cc_log_append(${target} "${package_name} with missing components: ${missing_components}")
find_package(${package_name} QUIET COMPONENTS ${missing_components})
endif()
# Link all components (including already found and newly found)
foreach(component IN LISTS components_list)
set(link_behavior)
if(TARGET ${package_name}::${component})
# For non-cmake target, get_target_property will throw an error
get_target_property(link_behavior ${package_name}::${component} LINK_BEHAVIOR)
if("${link_behavior}" STREQUAL "WHOLE_ARCHIVE")
cc_log_append(${target} "${CSI_Yellow}${depend} Link whole archive${CSI_Reset}")
# TODO(Oakley): fix this issue link whole-archive of static library will cause the multiple defined symbols error,
# those also defined in libgcc. allow multiple definition is not good idea. it will cause constructors defined in
# libraries execute more than twice times. build shared library can temporarily fix this problem
if(APPLE)
target_link_libraries(${target} ${linkage_type} -Wl,-force_load ${depend})
elseif(MSVC)
# In MSVC, we will add whole archive in default.
target_link_libraries(${target} ${linkage_type} -WHOLEARCHIVE:${depend})
else()
# Assume everything else is like gcc
target_link_libraries(${target} ${linkage_type} -Wl,--allow-multiple-definition)
target_link_libraries(${target} ${linkage_type} -Wl,--whole-archive ${depend} -Wl,--no-whole-archive)
endif()
else()
target_link_libraries(${target} ${linkage_type} ${package_name}::${component})
endif()
else()
target_link_libraries(${target} ${linkage_type} ${package_name}::${component})
endif()
endforeach()
else()
# If no "::" is found, treat it as a single library
if(NOT TARGET ${DEPNEDS})
cc_log_append(${target} "add_deps: ${DEPNEDS}")
find_package(${DEPNEDS} QUIET)
endif()
set(link_behavior)
if(TARGET ${DEPNEDS})
# For non-cmake target, get_target_property will throw an error
get_target_property(link_behavior ${DEPNEDS} LINK_BEHAVIOR)
if("${link_behavior}" STREQUAL "WHOLE_ARCHIVE")
cc_log_append(${target} "${CSI_Yellow}${depend} Link whole archive${CSI_Reset}")
# TODO(Oakley): fix this issue link whole-archive of static library will cause the multiple defined symbols error,
# those also defined in libgcc. allow multiple definition is not good idea. it will cause constructors defined in
# libraries execute more than twice times. build shared library can temporarily fix this problem
if(APPLE)
target_link_libraries(${target} ${linkage_type} -Wl,-force_load ${depend})
elseif(MSVC)
# In MSVC, we will add whole archive in default.
target_link_libraries(${target} ${linkage_type} -WHOLEARCHIVE:${depend})
else()
# Assume everything else is like gcc
target_link_libraries(${target} ${linkage_type} -Wl,--allow-multiple-definition)
target_link_libraries(${target} ${linkage_type} -Wl,--whole-archive ${depend} -Wl,--no-whole-archive)
endif()
else()
target_link_libraries(${target} ${linkage_type} ${DEPNEDS})
endif()
else()
target_link_libraries(${target} ${linkage_type} ${DEPNEDS})
endif()
endif()
endforeach()
endfunction()
function(cc_features targetname)
set(all_targets)
foreach(target IN LISTS ARGN)
if(TARGET "${target}")
get_target_property(alias_prop "${target}" ALIASED_TARGET)