-
Notifications
You must be signed in to change notification settings - Fork 11
/
CMakeLists.txt
5323 lines (4727 loc) · 196 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
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.16 FATAL_ERROR)
if (CMAKE_CURRENT_BINARY_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
message(FATAL_ERROR "In source-tree build not support")
endif()
# Compiler id for Apple Clang is now AppleClang.
# https://cmake.org/cmake/help/latest/policy/CMP0025.html
if (POLICY CMP0025)
cmake_policy(SET CMP0025 NEW)
endif (POLICY CMP0025)
# Project version variables are the empty string if version is unspecified
if (POLICY CMP0048)
cmake_policy(SET CMP0048 NEW)
endif (POLICY CMP0048)
# Honor link flags in try_compile() source-file signature.
if (POLICY CMP0056)
cmake_policy(SET CMP0056 NEW)
endif (POLICY CMP0056)
# Include file check macros honor CMAKE_REQUIRED_LIBRARIES.
set(CMAKE_POLICY_DEFAULT_CMP0075 NEW)
if (POLICY CMP0075)
cmake_policy(SET CMP0075 NEW)
endif (POLICY CMP0075)
# Select the MSVC runtime library for use by compilers targeting the MSVC ABI.
# Use MSVC_CRT_LINKAGE variable internally, option: dynamic and static
#
# Introduced by cmake 3.15
# https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html#variable:CMAKE_MSVC_RUNTIME_LIBRARY
# and https://cmake.org/cmake/help/latest/policy/CMP0091.html
if(POLICY CMP0091)
cmake_policy(SET CMP0091 NEW)
endif()
# The OLD behavior of this policy is to only allow GENERATED to be
# visible from the directory scope for which it was set. The NEW
# behavior on the other hand allows it to be visible from any scope.
#
# Turn on Fix on https://gitlab.kitware.com/cmake/cmake/-/issues/18399
# Introduced by cmake 3.20
if(POLICY CMP0118)
cmake_policy(SET CMP0118 NEW)
endif()
# https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features?redirectedfrom=MSDN&view=msvc-170
set(MSVC_CRT_LINKAGE "dynamic" CACHE STRING "")
if (NOT MSVC_CRT_LINKAGE STREQUAL "static" AND NOT MSVC_CRT_LINKAGE STREQUAL "dynamic")
message(FATAL_ERROR "MSVC_CRT_LINKAGE only accepts: dynamic or static")
endif()
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>$<$<STREQUAL:${MSVC_CRT_LINKAGE},dynamic>:DLL>")
# Use relative paths (May not work!).
set(CMAKE_USE_RELATIVE_PATHS TRUE)
# update LAST_CHANGE file
# format d740da257583289dbebd2eb37e8668928fac5ead-refs/branch-heads/4692{32}
find_program(GIT_EXECUTABLE NAMES git git.exe git.cmd)
if(NOT GIT_EXECUTABLE STREQUAL "GIT_EXECUTABLE-NOTFOUND" AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
execute_process(COMMAND
${GIT_EXECUTABLE} rev-parse HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE _LAST_CHANGE_REF
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND
${GIT_EXECUTABLE} describe --abbrev=0 --tags HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE YASS_APP_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET)
execute_process(COMMAND
${GIT_EXECUTABLE} rev-list ${YASS_APP_TAG}..HEAD --count
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE YASS_APP_SUBTAG
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET)
set(_ABBREV_REF "${YASS_APP_TAG}\{${YASS_APP_SUBTAG}\}")
set(YASS_APP_LAST_CHANGE "${_LAST_CHANGE_REF}-refs/branch-heads/${_ABBREV_REF}")
else()
# read if running from tarball
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/LAST_CHANGE YASS_APP_LAST_CHANGE)
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/TAG YASS_APP_TAG)
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/SUBTAG YASS_APP_SUBTAG)
endif()
set(_YASS_VERSION ${YASS_APP_TAG})
set(_YASS_VERSION_MAJOR 1)
set(_YASS_VERSION_MINOR 3)
set(_YASS_VERSION_PATCH 0)
set(_YASS_VERSION_TWEAK 0)
string(REPLACE "-" "." _YASS_VERSION_SPLIT ${_YASS_VERSION})
string(REPLACE "." ";" _YASS_VERSION_SPLIT ${_YASS_VERSION_SPLIT})
list(GET _YASS_VERSION_SPLIT 0 _YASS_VERSION_MAJOR)
list(GET _YASS_VERSION_SPLIT 1 _YASS_VERSION_MINOR)
list(GET _YASS_VERSION_SPLIT 2 _YASS_VERSION_PATCH)
set(_YASS_VERSION_TWEAK ${YASS_APP_SUBTAG})
set(PACKAGE_NAME "yass")
set(PACKAGE_FULL_NAME "Yet Another Shadow Socket")
set(PACKAGE_VERSION_MAJOR ${_YASS_VERSION_MAJOR})
set(PACKAGE_VERSION_MINOR ${_YASS_VERSION_MINOR})
set(PACKAGE_VERSION_PATCH ${_YASS_VERSION_PATCH})
set(PACKAGE_VERSION_TWEAK ${_YASS_VERSION_TWEAK})
set(PACKAGE_VERSION ${PACKAGE_VERSION_MAJOR}.${PACKAGE_VERSION_MINOR}.${PACKAGE_VERSION_PATCH})
# Defer enabling C and CXX languages.
project(${PACKAGE_NAME} NONE)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
# Set variables used in configure_file
set(YASS_APP_NAME ${PACKAGE_NAME})
set(YASS_APP_FULL_NAME ${PACKAGE_FULL_NAME})
set(YASS_APP_INTERNAL_NAME ${PACKAGE_NAME}_gui)
set(YASS_APP_WEBSITE "https://github.com/Chilledheart/yass")
set(YASS_APP_VERSION ${PACKAGE_VERSION})
set(YASS_APP_VERSION_MAJOR ${PACKAGE_VERSION_MAJOR})
set(YASS_APP_VERSION_MINOR ${PACKAGE_VERSION_MINOR})
set(YASS_APP_VERSION_PATCH ${PACKAGE_VERSION_PATCH})
set(YASS_APP_VERSION_TWEAK ${PACKAGE_VERSION_TWEAK})
set(YASS_APP_FULL_VERSION ${PACKAGE_VERSION}.${PACKAGE_VERSION_TWEAK})
set(YASS_APP_COPYRIGHT "Copyright 2019-2024 Chilledheart. All rights reserved.")
message(STATUS "Source Tag: ${YASS_APP_TAG}-${YASS_APP_SUBTAG}")
message(STATUS "Source LastChange: ${YASS_APP_LAST_CHANGE}")
set(APP_VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/version.h")
configure_file("src/version.h.in" "${APP_VERSION_HEADER}" @ONLY)
# for version.h
include_directories(${CMAKE_CURRENT_BINARY_DIR})
set(YASS_APP_FEATURES)
option(OFFICIAL_BUILD "Build with official build" OFF)
if (OFFICIAL_BUILD)
list(APPEND YASS_APP_FEATURES "official-build")
add_compile_definitions(-DOFFICIAL_BUILD)
endif()
if(MSVC)
# On Windows, prefer cl over gcc if both are available. By default most of
# the CMake generators prefer gcc, even on Windows.
set(CMAKE_GENERATOR_CC cl)
# new in cmake 3.10
# https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ARCHITECTURE_ID.html
# not used in ABI-detection due to the cmake issue https://gitlab.kitware.com/cmake/cmake/-/issues/17702
set(CMAKE_C_COMPILER_ARCHITECTURE_ID "${CMAKE_C_COMPILER_TARGET}" CACHE STRING "")
set(CMAKE_CXX_COMPILER_ARCHITECTURE_ID "${CMAKE_CXX_COMPILER_TARGET}" CACHE STRING "")
endif()
if (APPLE AND NOT IOS AND NOT CMAKE_OSX_DEPLOYMENT_TARGET)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14")
endif()
if (APPLE AND NOT CMAKE_OSX_ARCHITECTURES)
set(CMAKE_OSX_ARCHITECTURES ${CMAKE_SYSTEM_PROCESSOR})
endif()
enable_language(C)
enable_language(CXX)
# required by zstd
if (NOT COMPILER_MSVC)
enable_language(ASM)
endif()
## Setup compiler informations
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR
CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
set(COMPILER_CLANG TRUE)
else()
set(COMPILER_CLANG FALSE)
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
set(COMPILER_APPLE_CLANG TRUE)
endif()
if (${CMAKE_GENERATOR} MATCHES "^Xcode.*")
set(COMPILER_APPLE_CLANG TRUE)
endif()
if (COMPILER_CLANG AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 5.0)
message(FATAL_ERROR "Requires Clang 5.0 or newer to compile")
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(COMPILER_GCC TRUE)
else()
set(COMPILER_GCC FALSE)
endif()
if (COMPILER_GCC AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 7.1)
message(FATAL_ERROR "Requires GCC 7.1 or newer to compile")
endif()
if (MSVC AND NOT COMPILER_CLANG)
set(COMPILER_MSVC TRUE)
else()
set(COMPILER_MSVC FALSE)
endif()
if (COMPILER_MSVC)
message(FATAL_ERROR "Only clang-cl is supported on Windows, see https://crbug.com/988071")
endif()
if((${CMAKE_LINKER} MATCHES "lld") AND (${CMAKE_AR} MATCHES "llvm-lib" OR ${CMAKE_AR} MATCHES "llvm-ar"))
set(LINKER_LLD TRUE)
else()
set(LINKER_LLD FALSE)
endif()
if (MSVC)
if("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "X86")
set(MSVC_PROCESSOR_ARCHITECTURE "x86")
elseif("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "x64")
set(MSVC_PROCESSOR_ARCHITECTURE "amd64")
elseif("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "ARM")
set(MSVC_PROCESSOR_ARCHITECTURE "arm")
elseif("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "ARM64")
set(MSVC_PROCESSOR_ARCHITECTURE "arm64")
else()
message(FATAL_ERROR "Failed to determine the MSVC target architecture: ${MSVC_C_ARCHITECTURE_ID}")
endif()
endif()
if (IOS)
list(APPEND YASS_APP_FEATURES "${SDK_NAME} ${DEPLOYMENT_TARGET}")
elseif(APPLE)
list(APPEND YASS_APP_FEATURES "mac ${CMAKE_OSX_DEPLOYMENT_TARGET}")
elseif(OHOS)
list(APPEND YASS_APP_FEATURES "harmony ${OHOS_APILEVEL}")
elseif(ANDROID)
list(APPEND YASS_APP_FEATURES "android ${ANDROID_API_VERSION}")
elseif(MSVC)
list(APPEND YASS_APP_FEATURES "msvc")
elseif(MINGW)
list(APPEND YASS_APP_FEATURES "mingw")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
list(APPEND YASS_APP_FEATURES "linux")
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
if (NOT CMAKE_CROSSCOMPILING)
execute_process(COMMAND
uname -r
OUTPUT_VARIABLE _ABI_VERSION_STRING
OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REGEX REPLACE "([0-9]+)\.[0-9]-.*" "\\1" FREEBSD_ABI_VERSION "${_ABI_VERSION_STRING}")
endif()
if (FREEBSD_ABI_VERSION)
list(APPEND YASS_APP_FEATURES "freebsd ${FREEBSD_ABI_VERSION}")
else()
list(APPEND YASS_APP_FEATURES "freebsd unknown")
endif()
else()
list(APPEND YASS_APP_FEATURES "${CMAKE_SYSTEM_NAME}")
endif()
if (MSVC)
message(STATUS "Build with MSVC target architecture: ${MSVC_PROCESSOR_ARCHITECTURE}")
message(STATUS "Build with MSVC ${MSVC_CRT_LINKAGE} CRT library")
if (MSVC_PROCESSOR_ARCHITECTURE STREQUAL "x86")
set(OS_X86 TRUE)
endif()
if (MSVC_PROCESSOR_ARCHITECTURE STREQUAL "amd64")
set(OS_X64 TRUE)
endif()
if (MSVC_PROCESSOR_ARCHITECTURE STREQUAL "arm64")
set(OS_AARCH64 TRUE)
endif()
list(APPEND YASS_APP_FEATURES "arch: ${MSVC_PROCESSOR_ARCHITECTURE}")
list(APPEND YASS_APP_FEATURES "${MSVC_CRT_LINKAGE} crt")
elseif(APPLE)
message(STATUS "Build with target architecture: ${CMAKE_OSX_ARCHITECTURES}")
list(LENGTH CMAKE_OSX_ARCHITECTURES OSX_CROSS_ARCHITECTURES_COUNT)
if (OSX_CROSS_ARCHITECTURES_COUNT EQUAL 1 AND CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64")
set(OS_X64 TRUE)
elseif (OSX_CROSS_ARCHITECTURES_COUNT EQUAL 0 AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(OS_X64 TRUE)
endif()
if (OSX_CROSS_ARCHITECTURES_COUNT EQUAL 1 AND CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
set(OS_AARCH64 TRUE)
elseif (OSX_CROSS_ARCHITECTURES_COUNT EQUAL 0 AND CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
set(OS_AARCH64 TRUE)
endif()
list(APPEND YASS_APP_FEATURES "arch: ${CMAKE_OSX_ARCHITECTURES}")
else()
# x86
if ((CMAKE_C_COMPILER_TARGET MATCHES "^i386-.*" OR
CMAKE_C_COMPILER_TARGET MATCHES "^i486-.*" OR
CMAKE_C_COMPILER_TARGET MATCHES "^i586-.*" OR
CMAKE_C_COMPILER_TARGET MATCHES "^i686-.*"))
set(OS_X86 TRUE)
endif()
if (NOT CMAKE_C_COMPILER_TARGET AND
(CMAKE_SYSTEM_PROCESSOR MATCHES "^i386$" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "^i486$" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "^i586$" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "^i686$"))
set(OS_X86 TRUE)
endif()
# x64
if ((CMAKE_C_COMPILER_TARGET MATCHES "^amd64*" OR
CMAKE_C_COMPILER_TARGET MATCHES "^x86_64*" OR
CMAKE_C_COMPILER_TARGET MATCHES "^x64*"))
set(OS_X64 TRUE)
endif()
if (NOT CMAKE_C_COMPILER_TARGET AND
(CMAKE_SYSTEM_PROCESSOR MATCHES "^amd64$" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "^x86_64$" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "^x64$"))
set(OS_X64 TRUE)
endif()
# arm
if (CMAKE_C_COMPILER_TARGET MATCHES "^arm-.*")
set(OS_ARM TRUE)
endif()
if (NOT CMAKE_C_COMPILER_TARGET AND
(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm$" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "^armel$" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "^armhf$"))
set(OS_ARM TRUE)
endif()
# arm64
if ((CMAKE_C_COMPILER_TARGET MATCHES "^arm64-.*" OR
CMAKE_C_COMPILER_TARGET MATCHES "^aarch64-.*"))
set(OS_AARCH64 TRUE)
endif()
if (NOT CMAKE_C_COMPILER_TARGET AND
(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm64$" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64$"))
set(OS_AARCH64 TRUE)
endif()
# mips
if ((CMAKE_C_COMPILER_TARGET MATCHES "^mipsel-.*" OR
CMAKE_C_COMPILER_TARGET MATCHES "^mips-.*"))
set(OS_MIPS TRUE)
endif()
if (NOT CMAKE_C_COMPILER_TARGET AND
(CMAKE_SYSTEM_PROCESSOR MATCHES "^mipsel$" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "^mips$"))
set(OS_MIPS TRUE)
endif()
# mips64
if ((CMAKE_C_COMPILER_TARGET MATCHES "^mips64el-.*" OR
CMAKE_C_COMPILER_TARGET MATCHES "^mips64-.*"))
set(OS_MIPS64 TRUE)
endif()
if (NOT CMAKE_C_COMPILER_TARGET AND
(CMAKE_SYSTEM_PROCESSOR MATCHES "^mips64el$" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "^mips64$"))
set(OS_MIPS64 TRUE)
endif()
# loongarch64
if (CMAKE_C_COMPILER_TARGET MATCHES "^loongarch64-.*")
set(OS_LOONGARCH64 TRUE)
endif()
if (NOT CMAKE_C_COMPILER_TARGET AND
CMAKE_SYSTEM_PROCESSOR MATCHES "^loongarch64$")
set(OS_LOONGARCH64 TRUE)
endif()
# riscv64
if (CMAKE_C_COMPILER_TARGET MATCHES "^riscv64-.*")
set(OS_RISCV64 TRUE)
endif()
if (NOT CMAKE_C_COMPILER_TARGET AND
CMAKE_SYSTEM_PROCESSOR MATCHES "^riscv64$")
set(OS_RISCV64 TRUE)
endif()
# riscv32
if (CMAKE_C_COMPILER_TARGET MATCHES "^riscv32-.*")
set(OS_RISCV32 TRUE)
endif()
if (NOT CMAKE_C_COMPILER_TARGET AND
CMAKE_SYSTEM_PROCESSOR MATCHES "^riscv32$")
set(OS_RISCV32 TRUE)
endif()
# Fix MINGW (native mingw)'s CMAKE_SYSTEM_PROCESSOR
if (MINGW)
if (OS_X86)
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR "i686")
elseif(OS_AARCH64)
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR "aarch64")
else()
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR "x86_64")
endif()
endif()
message(STATUS "Build with target architecture: ${CMAKE_SYSTEM_PROCESSOR}")
list(APPEND YASS_APP_FEATURES "arch: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(LINUX ON)
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
set(FREEBSD ON)
endif()
# handling bug with cmake+clang-cl
# newer clang-cl produces "Note: including file: .\\foo.h"
# unable to parse by cmake's CMakeDetermineCompilerId.cmake
if (MSVC AND COMPILER_CLANG)
set(CMAKE_CL_SHOWINCLUDES_PREFIX "Note: including file:" CACHE STRING "" FORCE)
endif()
## postpone ASM enabling after C/CXX LANG DIALECT otherwise it might fails
if (NOT (WIN32 AND (MSVC_PROCESSOR_ARCHITECTURE STREQUAL "x86" OR MSVC_PROCESSOR_ARCHITECTURE STREQUAL "amd64")))
enable_language(ASM)
endif()
## Export build compilation database if possible
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
include(CMakeDependentOption)
option(CLI "Build with cli." ON)
option(SERVER "Build with server." ON)
cmake_dependent_option(
CLI_STATIC_BUILD "Build with static build cli." OFF
"CLI AND LINUX" OFF)
cmake_dependent_option(
SERVER_STATIC_BUILD "Build with static build server." OFF
"SERVER AND LINUX" OFF)
option(GUI "Build against GUI." OFF)
option(BUILD_BENCHMARKS "Build with benchmark." OFF)
option(BUILD_SHARED_LIBS "Build with shared libraries." OFF)
option(BUILD_TESTS "Build with test." OFF)
option(OPTIMIZED_PROTOC "Force protobuf compiler to be built with optimization" OFF)
cmake_dependent_option(
USE_TCMALLOC "Build with tcmalloc" OFF
"LINUX OR WIN32" OFF)
cmake_dependent_option(
USE_SYSTEM_TCMALLOC "Build with system or vendored tcmalloc" OFF
USE_TCMALLOC OFF)
cmake_dependent_option(
USE_MIMALLOC "Use mimalloc" OFF
"NOT USE_TCMALLOC; NOT IOS; NOT ANDROID; NOT OHOS" OFF)
cmake_dependent_option(
USE_SYSTEM_MIMALLOC "Use system or vendored mimalloc" OFF
USE_MIMALLOC OFF)
option(USE_LIBCXX "Build with custom libc++" OFF)
option(USE_NGHTTP2 "Build with libnghttp2" ON)
cmake_dependent_option(
USE_SYSTEM_NGHTTP2 "Build with system or vendored libnghttp2" OFF
USE_NGHTTP2 OFF)
option(USE_ICF "Build with ICF" OFF)
option(USE_CURL "Build with libcurl (test only)" ON)
option(USE_CARES "Build with c-ares" OFF)
cmake_dependent_option(
USE_SYSTEM_CARES "Build with system or vendored c-ares" OFF
USE_CARES OFF)
option(USE_BUILTIN_CA_BUNDLE_CRT "Build with embedded ca-bundle.crt support" ON)
option(USE_MBEDTLS "Build with mbedtls support" ON)
cmake_dependent_option(
USE_SYSTEM_MBEDTLS "Build with system or vendored mbedtls" OFF
USE_MBEDTLS OFF)
option(USE_QUICHE "Build with quiche support" ON)
cmake_dependent_option(
USE_BALSA_HTTP_PARSER "Build with balsa http parser" ON
USE_QUICHE OFF)
cmake_dependent_option(
USE_SYSTEM_HTTP_PARSER "Build with system or vendored http parser" OFF
"NOT USE_BALSA_HTTP_PARSER" OFF)
option(USE_LEVELDB "Build with leveldb" OFF)
option(USE_SQLITE "Build with sqlite" OFF)
cmake_dependent_option(
USE_SYSTEM_SQLITE "Build with system or vendored sqlite" OFF
USE_SQLITE OFF)
option(USE_OLD_SYSTEMD_SERVICE "Install with old systemd service files" OFF)
cmake_dependent_option(
USE_IOURING "Build with io uring support" OFF
LINUX OFF)
option(USE_ZLIB "Build with zlib (required by quic)" OFF)
cmake_dependent_option(
USE_SYSTEM_ZLIB "Build with system or vendored zlib" OFF
USE_ZLIB OFF)
option(USE_JSONCPP "Build with jsoncpp library" ON)
cmake_dependent_option(
USE_SYSTEM_JSONCPP "Build with system or vendered jsoncpp library" OFF
"USE_JSONCPP AND NOT USE_LIBCXX" OFF)
option(ENABLE_FORTIFY "Enable build with Fortify Source (linux only)" OFF)
option(ENABLE_LTO "Enable build with LTO" OFF)
option(ENABLE_LLD "Enable build with LLD" OFF)
option(USE_MOLD "Build with mold linker" OFF)
option(USE_LLD "Build with lld linker" OFF)
# this temporary fix is not working with tests
# https://discourse.cmake.org/t/dsym-file-not-copied-when-archiving/7691/2
cmake_dependent_option(
IOS_XCODE_DSYM_FOLDER_FIX "Build with xcode dsym folder fixes" OFF
"IOS AND NOT BUILD_TESTS" OFF)
cmake_dependent_option(USE_CET "Build with CET support" OFF
"LINUX OR MSVC; OS_X64 OR OS_X86" OFF)
cmake_dependent_option(USE_QT6
"Build with Qt6 (GUI)" OFF
"GUI AND NOT WIN32 AND NOT APPLE AND NOT OHOS AND NOT ANDROID" OFF)
cmake_dependent_option(USE_QT5
"Build with qt5 (GUI)" OFF
"GUI AND NOT USE_QT6 AND NOT WIN32 AND NOT APPLE AND NOT OHOS AND NOT ANDROID" OFF)
cmake_dependent_option(USE_GTK4
"Build with gtk4 (GUI)" OFF
"GUI AND NOT USE_QT6 AND NOT USE_QT5 AND NOT WIN32 AND NOT APPLE AND NOT OHOS AND NOT ANDROID" OFF)
cmake_dependent_option(USE_GTK3_APP_INDICATOR
"Build with libappindicator3 (gtk3 only)" ON
"GUI AND NOT USE_QT6 AND NOT USE_QT5 AND NOT USE_GTK4 AND NOT WIN32 AND NOT APPLE AND NOT OHOS AND NOT ANDROID" OFF)
cmake_dependent_option(
INSTALL_FLATHUB_METAINFO "Build with flathub metainfo" OFF
"GUI AND LINUX" OFF)
cmake_dependent_option(
FLATPAK_BUILD "Build with flatpak support" OFF
"GUI AND LINUX" OFF)
if (FLATPAK_BUILD)
list(APPEND YASS_APP_FEATURES "flatpak-build")
add_compile_definitions(-DFLATPAK_BUILD)
endif()
cmake_dependent_option(
FLATHUB_BUILD "Build with flathub build" OFF
"GUI AND LINUX" OFF)
if (FLATHUB_BUILD)
list(APPEND YASS_APP_FEATURES "flathub-build")
add_compile_definitions(-DFLATHUB_BUILD)
endif()
# Dynamic users are supported from version 235
# see https://0pointer.net/blog/dynamic-users-with-systemd.html
if (USE_OLD_SYSTEMD_SERVICE)
set(SYSTEMD_SERVICE_SUFFIX ".old")
endif()
if (ENABLE_LLD AND (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR LINKER_LLD) AND NOT COMPILER_APPLE_CLANG)
# skipping apple clang
set(USE_LLD ON)
elseif (COMPILER_GCC)
set(USE_LLD OFF)
endif()
if (USE_MOLD OR USE_LLD)
set(USE_ICF ON)
else()
if (ENABLE_LTO)
set(ENABLE_LTO FALSE)
include(CheckIPOSupported)
check_ipo_supported(RESULT USE_LTO_CMAKE OUTPUT LTO_REASON)
if (NOT USE_LTO_CMAKE)
message(WARNING "LTO is not supported: ${LTO_REASON}, disabling")
endif()
endif()
endif()
if (ENABLE_CLANG_TIDY)
set(ENABLE_LTO OFF)
endif()
if (ASAN OR TSAN OR UBSAN OR MSAN)
# msan gets confused by fortify source level 3
set(ENABLE_FORTIFY OFF)
# -fsanitize-blacklist doesn't work with lto
set(ENABLE_LTO OFF)
endif()
if (USE_MOLD)
set(USE_LLD OFF)
endif()
if (CMAKE_BUILD_TYPE)
string(TOUPPER ${CMAKE_BUILD_TYPE} upper_CMAKE_BUILD_TYPE)
else()
string(TOUPPER "Debug" upper_CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
endif()
string(TOLOWER ${CMAKE_BUILD_TYPE} lower_CMAKE_BUILD_TYPE)
list(APPEND YASS_APP_FEATURES "${lower_CMAKE_BUILD_TYPE}")
if (COMPILER_MSVC)
list(APPEND YASS_APP_FEATURES "msvc ${CMAKE_CXX_COMPILER_VERSION}")
elseif(COMPILER_GCC)
list(APPEND YASS_APP_FEATURES "gcc ${CMAKE_CXX_COMPILER_VERSION}")
elseif(COMPILER_APPLE_CLANG)
list(APPEND YASS_APP_FEATURES "appleclang ${CMAKE_CXX_COMPILER_VERSION}")
elseif(COMPILER_CLANG)
list(APPEND YASS_APP_FEATURES "clang ${CMAKE_CXX_COMPILER_VERSION}")
endif()
if (CMAKE_CROSSCOMPILING)
list(APPEND YASS_APP_FEATURES "crosscompile")
endif()
if (upper_CMAKE_BUILD_TYPE MATCHES "RELEASE|MINSIZEREL|RELWITHDEBINFO")
message(STATUS "Release Build: ${CMAKE_BUILD_TYPE}")
set(IS_RELEASE_BUILD ON)
else()
message(STATUS "Debug Build")
endif()
if (upper_CMAKE_BUILD_TYPE MATCHES "RELWITHDEBINFO|DEBUG" OR NOT OFFICIAL_BUILD)
option(ENABLE_ASSERTIONS "Enable assertions" ON)
else()
option(ENABLE_ASSERTIONS "Enable assertions" OFF)
endif()
if (ENABLE_ASSERTIONS)
message(STATUS "Enabled Assertions")
list(APPEND YASS_APP_FEATURES "assertions")
add_compile_definitions(-DDCHECK_ALWAYS_ON=1)
# MSVC doesn't like _DEBUG on release builds. See PR 4379.
if(NOT MSVC)
add_compile_definitions(_DEBUG)
endif()
# On non-Debug builds cmake automatically defines NDEBUG, so we
# explicitly undefine it:
if(NOT upper_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-UNDEBUG>)
if (MSVC)
# Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
foreach(flags_var_to_scrub
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS_MINSIZEREL)
string(REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
"${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
endforeach()
endif()
endif()
else()
## Optimization flags
add_compile_options($<$<OR:$<CONFIG:RELEASE>,$<CONFIG:MINSIZEREL>>:-DNDEBUG>)
endif()
if (ENABLE_LTO AND IS_RELEASE_BUILD)
if (COMPILER_CLANG)
if (USE_MOLD AND UNIX AND NOT APPLE)
set(LTO ON)
set(LTO_FLAVOUR "thin")
endif()
if (USE_LLD)
set(LTO ON)
set(LTO_FLAVOUR "thin")
endif()
endif()
if (LTO)
message(STATUS "Compiling with ${LTO_FLAVOUR} LTO")
list(APPEND YASS_APP_FEATURES "lto ${LTO_FLAVOUR}")
set(USE_LTO_CMAKE OFF)
endif()
endif()
if (USE_LTO_CMAKE AND ${CMAKE_GENERATOR} MATCHES "^Xcode.*" AND IS_RELEASE_BUILD)
set(USE_LTO_CMAKE OFF)
# https://gitlab.kitware.com/cmake/cmake/-/issues/16749
set(CMAKE_XCODE_ATTRIBUTE_LLVM_LTO "YES_THIN")
message(STATUS "Compiling with Xcode's LTO")
list(APPEND YASS_APP_FEATURES "lto thin")
endif()
if (USE_LTO_CMAKE AND IS_RELEASE_BUILD)
message(STATUS "Compiling with CMake's LTO")
list(APPEND YASS_APP_FEATURES "lto auto")
else()
set(USE_LTO_CMAKE OFF)
endif()
if (NOT MSVC)
if (USE_MOLD)
add_link_options(-fuse-ld=mold -Wl,--gdb-index)
list(APPEND YASS_APP_FEATURES "linker mold")
elseif (USE_LLD)
add_link_options(-fuse-ld=lld)
list(APPEND YASS_APP_FEATURES "linker lld")
endif()
endif()
if (COMPILER_MSVC AND USE_LIBCXX)
message(SEND_ERROR "Microsoft Visual C++ Compiler with libc++ support is not supported, please disabling it with -DUSE_LIBCXX=off")
endif()
include(config-ix)
if (USE_CET)
if (MSVC)
# see https://reviews.llvm.org/D70606
add_link_options(/cetcompat)
elseif (COMPILER_CLANG)
# see https://reviews.llvm.org/D59780
add_compile_options(-fcf-protection=full)
elseif(COMPILER_GCC)
add_compile_options(-fcf-protection)
endif()
add_link_options(-Wl,-z,cet-report=error)
list(APPEND YASS_APP_FEATURES "cet")
endif()
# see "Announcement: ARC is default for Objective-C code"
# https://groups.google.com/a/chromium.org/g/embedder-dev/c/13hNC6w2gGA?pli=1
if (APPLE)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fobjc-arc>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fno-objc-arc-exceptions>)
add_link_options(-fobjc-arc)
endif()
# set default _MSC_VER
if (MSVC AND COMPILER_CLANG)
if (ALLOW_XP)
# look back to vs2017, fix issue of linkage of ___dyn_tls_on_demand_init and ___tls_guard defined in msvcrt/libcmt
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fmsc-version=1916>)
else()
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fmsc-version=1934>)
endif()
endif()
if (CMAKE_OSX_ARCHITECTURES)
if (NOT "${CMAKE_OSX_ARCHITECTURES}" STREQUAL ${CMAKE_SYSTEM_PROCESSOR})
set(USE_HOST_TOOLS ON)
endif()
endif()
if (CMAKE_CROSSCOMPILING OR (OPTIMIZED_PROTOC AND CMAKE_CONFIGURATION_TYPES))
set(USE_HOST_TOOLS ON)
endif()
if (CMAKE_CROSSCOMPILING AND (CMAKE_SYSROOT OR CMAKE_OSX_SYSROOT))
if (CMAKE_OSX_SYSROOT)
set(_SYSROOT ${CMAKE_OSX_SYSROOT})
else()
set(_SYSROOT ${CMAKE_SYSROOT})
endif()
if (NOT EXISTS ${_SYSROOT})
message(FATAL_ERROR "Invalid sysroot ${_SYSROOT}")
endif()
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(ENV{PKG_CONFIG_DIR} "")
set(ENV{PKG_CONFIG_LIBDIR} "${_SYSROOT}/usr/lib64/pkgconfig:${_SYSROOT}/usr/lib/pkgconfig:${_SYSROOT}/lib/pkgconfig:${_SYSROOT}/usr/share/pkgconfig")
if (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
set(ENV{PKG_CONFIG_LIBDIR} "${_SYSROOT}/usr/libdata/pkgconfig:${_SYSROOT}/usr/local/libdata/pkgconfig")
endif()
set(ENV{PKG_CONFIG_SYSROOT_DIR} ${_SYSROOT})
endif()
# cross-compiling mingw and freebsd don't like --gcc-install-dir (non-gnu style)
if (UNIX AND CMAKE_CROSSCOMPILING AND CMAKE_SYSROOT AND COMPILER_CLANG AND NOT CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16.0)
file(GLOB _GCC_SYSROOT "${CMAKE_SYSROOT}/lib/gcc/*/*.*.*")
# fix up for loongarch sysroot
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "loongarch64")
file(GLOB _GCC_SYSROOT "${CMAKE_SYSROOT}/../../lib/gcc/*/*.*.*")
# required by loongarch64 sdk https://github.com/loongson/build-tools/releases
if (NOT _GCC_SYSROOT)
file(GLOB _GCC_SYSROOT "${CMAKE_SYSROOT}/../lib/gcc/*/*.*.*")
endif()
endif()
# fix up for riscv64 and riscv32 sysroot
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv32")
file(GLOB _GCC_SYSROOT "${CMAKE_SYSROOT}/../lib/gcc/*/*.*.*")
endif()
# fix up for raw sysroot from docker
if (NOT _GCC_SYSROOT)
file(GLOB _GCC_SYSROOT "${CMAKE_SYSROOT}/usr/lib/gcc/*/*.*.*")
endif()
if (_GCC_SYSROOT)
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --gcc-install-dir=${_GCC_SYSROOT}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --gcc-install-dir=${_GCC_SYSROOT}")
set(CMAKE_REQUIRED_LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS} --gcc-install-dir=${_GCC_SYSROOT})
endif()
else()
# not useful usage:
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --gcc-toolchain=${CMAKE_SYSROOT}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --gcc-toolchain=${CMAKE_SYSROOT}")
set(CMAKE_REQUIRED_LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS} --gcc-toolchain=${CMAKE_SYSROOT})
endif()
endif()
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "loongarch64")
add_definitions(-D_LOONGARCH_SZLONG=64)
endif()
include(CrossCompile)
# when crosscompiling import the executable targets from a file
if (USE_HOST_TOOLS)
if (CMAKE_OSX_ARCHITECTURES)
set(OSX_NATIVE_ARCH_OPTION "-DCMAKE_OSX_ARCHITECTURES=${CMAKE_SYSTEM_PROCESSOR}")
endif()
create_cross_target(yass NATIVE "" Release ${OSX_NATIVE_ARCH_OPTION})
endif()
if (CMAKE_OSX_ARCHITECTURES)
list(LENGTH CMAKE_OSX_ARCHITECTURES OSX_CROSS_ARCHITECTURES_COUNT)
if(NOT OSX_CROSS_ARCHITECTURES_COUNT EQUAL 1)
if (COMPILER_GCC)
message(FATAL_ERROR "GCC is not supported to compiling universal build")
endif()
set(OSX_UNIVERSALBUILD ON)
foreach (OSX_ARCH ${CMAKE_OSX_ARCHITECTURES})
create_cross_target(yass OSX_${OSX_ARCH} "" ${CMAKE_BUILD_TYPE} -DCMAKE_OSX_ARCHITECTURES=${OSX_ARCH})
set(OSX_CROSS_ARCHITECTURES ${OSX_CROSS_ARCHITECTURES} ${OSX_ARCH})
endforeach()
include(UniversalBuild)
if (CLI)
add_osx_univeral_target(yass_cli "${OSX_CROSS_ARCHITECTURES}" yass)
endif()
if (SERVER)
add_osx_univeral_target(yass_server "${OSX_CROSS_ARCHITECTURES}" yass)
endif()
if (GUI)
# this a dirty check for GUI flavor
if (COMPILER_GCC)
add_osx_univeral_target(yass "${OSX_CROSS_ARCHITECTURES}" yass)
else()
add_osx_univeral_bundle(yass "${OSX_CROSS_ARCHITECTURES}" yass)
endif()
endif()
if (BUILD_TESTS)
add_osx_univeral_target(yass_test "${OSX_CROSS_ARCHITECTURES}" yass)
add_custom_target(check
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/universal/yass_test
DEPENDS yass_test
COMMENT "yass unittests"
USES_TERMINAL
)
endif()
if (BUILD_BENCHMARKS)
add_osx_univeral_target(yass_benchmark "${OSX_CROSS_ARCHITECTURES}" yass)
endif()
# overrided default target with universal ones
# turning them off
set(CLI OFF)
set(SERVER OFF)
set(GUI OFF)
set(BUILD_TESTS OFF)
set(BUILD_BENCHMARKS OFF)
set(CMAKE_OSX_ARCHITECTURES ${CMAKE_SYSTEM_PROCESSOR})
else()
set(OSX_UNIVERSALBUILD OFF)
endif()
endif()
if(YASS_TARGET_IS_CROSSCOMPILE_HOST)
# Dummy use to avoid CMake Warning: Manually-specified variables were not used
# (this is a variable that CrossCompile sets on recursive invocations)
endif()
if (NOT CMAKE_SKIP_INSTALL_RULES)
include(GNUInstallDirs)
endif()
# *****************************************************************************************
# Debug facilities
# *****************************************************************************************
if(MSAN)
if(ASAN)
message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
endif()
if(NOT COMPILER_CLANG)
# experimental
# due to the ms document https://docs.microsoft.com/en-us/cpp/sanitizers/asan?view=msvc-160
if (MSVC AND NOT MSVC_C_ARCHITECTURE_ID STREQUAL "arm64")
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/fsanitize=memory>)
add_link_options(/fsanitize=memory)
else()
message(FATAL_ERROR "Cannot enable MSAN unless using Clang or Vistual Studio 2019 version 16.9")
endif()
else()
add_compile_options(-fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer)
add_link_options(-fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer)
endif()
add_compile_definitions(-DMEMORY_SANITIZER)
list(APPEND YASS_APP_FEATURES "msan")
endif()
if(ASAN)
if(NOT COMPILER_CLANG)
# Current support is limited to x86 and x64 on Windows 10
# due to the ms document https://docs.microsoft.com/en-us/cpp/sanitizers/asan?view=msvc-160
if (MSVC AND NOT MSVC_C_ARCHITECTURE_ID STREQUAL "arm64")
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/fsanitize=address>)
add_link_options(/fsanitize=address)
else()
message(FATAL_ERROR "Cannot enable ASAN unless using Clang or Vistual Studio 2019 version 16.9")
endif()
else()
add_compile_options(-fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer)
add_link_options(-fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer)
endif()
add_compile_definitions(-DADDRESS_SANITIZER)
if (NOT APPLE AND USE_LIBCXX)
add_compile_definitions(-D_LIBCPP_INSTRUMENTED_WITH_ASAN)
endif()
list(APPEND YASS_APP_FEATURES "asan")
endif()
if(CFI)
if(NOT COMPILER_CLANG)
message(FATAL_ERROR "Cannot enable CFI unless using Clang")
endif()
add_compile_options(-fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin)
add_link_options(-fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin)
# We use Chromium's copy of clang, which requires -fuse-ld=lld if building
# with -flto. That, in turn, can't handle -ggdb.
string(REPLACE "-ggdb" "-g" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
string(REPLACE "-ggdb" "-g" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
list(APPEND YASS_APP_FEATURES "cfi")
endif()
if(TSAN)
if(NOT COMPILER_CLANG)
# experimental
# due to the ms document https://docs.microsoft.com/en-us/cpp/sanitizers/asan?view=msvc-160
if (MSVC AND NOT MSVC_C_ARCHITECTURE_ID STREQUAL "arm64")
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/fsanitize=thread>)
add_link_options(/fsanitize=thread)
else()
message(FATAL_ERROR "Cannot enable TSAN unless using Clang or Vistual Studio 2019 version 16.9")
endif()
else()
add_compile_options(-fsanitize=thread)
add_link_options(-fsanitize=thread)
endif()
add_compile_definitions(-DTHREAD_SANITIZER)
list(APPEND YASS_APP_FEATURES "tsan")
endif()
if(UBSAN)
if(NOT COMPILER_CLANG)
# experimental
# due to the ms document https://docs.microsoft.com/en-us/cpp/sanitizers/asan?view=msvc-160
if (MSVC AND NOT MSVC_C_ARCHITECTURE_ID STREQUAL "arm64")
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/fsanitize=undefined>)
add_link_options(/fsanitize=undefined)
else()
message(FATAL_ERROR "Cannot enable UBSAN unless using Clang or Vistual Studio 2019 version 16.9")
endif()
else()
add_compile_options(-fsanitize=undefined)
add_link_options(-fsanitize=undefined)
endif()
add_compile_definitions(-DUNDEFINED_SANITIZER)
list(APPEND YASS_APP_FEATURES "ubsan")
endif()
# apply sanitizer blacklist