Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
1221 lines (1085 loc) · 52.8 KB
/
Copy pathCMakeLists.txt
File metadata and controls
1221 lines (1085 loc) · 52.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
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.18)
project(libmimalloc C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(MI_SECURE "OFF" CACHE STRING "Use security mitigations (like meta data guard pages, allocation randomization, double-free mitigation, and free-list corruption detection): OFF, ON, FULL. The FULL setting adds guard pages at the end of each mimalloc page (which may be expensive)")
set_property(CACHE MI_SECURE PROPERTY STRINGS "OFF" "ON" "FULL")
option(MI_OVERRIDE "Override the standard malloc interface (i.e. define entry points for 'malloc', 'free', etc)" ON)
option(MI_XMALLOC "Enable abort() call on memory allocation failure by default" OFF)
option(MI_SHOW_ERRORS "Show error and warning messages by default (only enabled by default in DEBUG mode)" OFF)
set(MI_GUARDED "DEFAULT" CACHE STRING "Build with ability to put guard pages behind certain object allocations")
set_property(CACHE MI_GUARDED PROPERTY STRINGS "OFF" "ON" "FULL" "DEFAULT")
set(MI_USE_CXX "DEFAULT" CACHE STRING "Use the C++ compiler to compile the library (instead of the C compiler)")
set_property(CACHE MI_USE_CXX PROPERTY STRINGS "OFF" "ON" "DEFAULT")
set(MI_OPT_ARCH "DEFAULT" CACHE STRING "Only for optimized builds: turn on architecture specific optimizations (for x64: '-march=haswell;-mavx2' (2013), for arm64: '-march=armv8.1-a' (2016), for riscV: '-march=rv64gcb_zacas' (2023))")
set_property(CACHE MI_OPT_ARCH PROPERTY STRINGS "OFF" "ON" "avx2" "armv8.1" "armv8.3" "rva22" "rva23" "rv64gcb_zacas" "rv64gcb_zacas_zalasr" "DEFAULT")
set(MI_DEBUG "DEFAULT" CACHE STRING "Enable assertion checks: OFF, ON, INTERNAL, FULL, DEFAULT. INTERNAL adds extra internal invariant checks, while FULL adds expensive heap invariant checks")
set_property(CACHE MI_DEBUG PROPERTY STRINGS "OFF" "ON" "INTERNAL" "FULL" "DEFAULT")
set(MI_TRACK "OFF" CACHE STRING "Enable tracking: OFF, ASAN (address sanitizer), VALGRIND, ETW (Windows event tracing). Adds a small overhead.")
set_property(CACHE MI_TRACK PROPERTY STRINGS "OFF" "ASAN" "VALGRIND" "ETW")
set(MI_STATS "DEFAULT" CACHE STRING "Enable statistics.")
set_property(CACHE MI_STATS PROPERTY STRINGS "OFF" "ON" "FULL" "DEFAULT")
set(MI_PROFILE "DEFAULT" CACHE STRING "Enable profiling.")
set_property(CACHE MI_PROFILE PROPERTY STRINGS "OFF" "ON" "FULL" "DEFAULT")
set(MI_FREE_IS_CHECKED "DEFAULT" CACHE STRING "Check for invalid pointers in 'mi_free': OFF, SHARED, ON, DEFAULT. 'SHARED' enables it only for the dynamic/shared library when MI_OVERRIDE is ON")
set_property(CACHE MI_FREE_IS_CHECKED PROPERTY STRINGS "OFF" "SHARED" "ON" "DEFAULT")
set(MI_ALLOW_THP "DEFAULT" CACHE STRING "Enable transparent huge pages on Linux: OFF, ON, FULL, DEFAULT. 'FULL' enables THP and also avoids splitting up huge pages during purging.")
set_property(CACHE MI_ALLOW_THP PROPERTY STRINGS "OFF" "ON" "FULL" "DEFAULT")
option(MI_OPT_SIMD "Use SIMD instructions (requires MI_OPT_ARCH to be enabled)" OFF)
option(MI_DEBUG_TSAN "Build with thread sanitizer (needs clang)" OFF)
option(MI_DEBUG_UBSAN "Build with undefined-behavior sanitizer (needs clang++)" OFF)
option(MI_BUILD_SHARED "Build shared library" ON)
option(MI_BUILD_STATIC "Build static library" ON)
option(MI_BUILD_OBJECT "Build object library" ON)
option(MI_BUILD_TESTS "Build test executables" ON)
# OS specific
option(MI_OSX_INTERPOSE "Use interpose to override standard malloc on macOS" ON)
option(MI_OSX_ZONE "Use malloc zone to override standard malloc on macOS" ON)
option(MI_WIN_REDIRECT "Use redirection module ('mimalloc-redirect') on Windows if compiling mimalloc as a DLL" ON)
option(MI_LIBC_MUSL "Enable this when linking with musl libc" OFF)
# advanced
option(MI_INSTALL_TOPLEVEL "Install directly into $CMAKE_INSTALL_PREFIX instead of PREFIX/lib/mimalloc-version" OFF)
option(MI_NO_THP "Disable transparent huge pages support on Linux/Android for the mimalloc process only" OFF)
option(MI_FREE_USE_PAGEMAP "Always use the page map when free-ing a pointer -- enable only for systems that don't support large OS aligned allocations well." OFF)
option(MI_PADDING "Enable padding to detect heap block overflow (always on in DEBUG or SECURE mode, or with Valgrind/ASAN)" OFF)
option(MI_NO_PADDING "Force no use of padding even in DEBUG mode etc." OFF)
option(MI_SKIP_COLLECT_ON_EXIT "Skip collecting memory on program exit" OFF)
option(MI_EXTRA_CPPDEFS "Extra pre-processor definitions (use as `-DMI_EXTRA_CPPDEFS=\"opt1=val1;opt2=val2\"`)" "")
option(MI_SEE_ASM "Generate assembly files" OFF)
# negated options for vcpkg features
option(MI_NO_USE_CXX "Use plain C compilation (has priority over MI_USE_CXX)" OFF)
option(MI_NO_OPT_ARCH "Do not use architecture specific optimizations (like '-march=armv8.3-a' for example) (has priority over MI_OPT_ARCH)" OFF)
option(MI_NO_DEBUG "Disable all assertion checks even in debug builds" OFF)
# experimental
option(MI_TLS_RECURSE_GUARD "Force a recursion check on thread local access (for MI_TLS_MODEL_LOCAL only)" OFF)
set(MI_WIN_INIT "DEFAULT" CACHE STRING "Force Windows intialization strategy: CRT_TLS (default), RAW_DLLMAIN (use raw DLL main entry point), TLS_DLLMAIN (TLS entries with DllMain), FLS (legacy fiber API), DEFAULT")
set_property(CACHE MI_WIN_INIT PROPERTY STRINGS "CRT_TLS" "RAW_DLLMAIN" "TLS_DLLMAIN" "FLS" "DEFAULT")
set(MI_TLS_MODEL "DEFAULT" CACHE STRING "Force TLS model: LOCAL (thread local variables), PTHREADS (pthread locals), LOCAL_DYNAMIC (dlopen-compatible thread local storage mechanism (Unix)), WIN32 (direct Windows TLS slots), FIXED (fixed thread local slots), DEFAULT.")
set_property(CACHE MI_TLS_MODEL PROPERTY STRINGS "LOCAL" "PTHREADS" "LOCAL_DYNAMIC" "WIN32" "FIXED" "DEFAULT")
# deprecated options
option(MI_USE_LIBATOMIC "Deprecated: Explicitly link with -latomic (on older systems) (deprecated and detected automatically)" OFF)
option(MI_DEBUG_INTERNAL "Deprecated: Enable assertion and internal invariant checks (use MI_DEBUG=INTERNAL instead)" OFF)
option(MI_DEBUG_FULL "Deprecated: Enable assertion checks and expensive internal heap invariant checking (use MI_DEBUG=FULL instead)" OFF)
option(MI_CHECK_FULL "Deprecated: Use full internal invariant checking in DEBUG mode (use MI_DEBUG=FULL instead)" OFF)
option(MI_SECURE_FULL "Deprecated: Use full security mitigations including guard pages at the end of each mimalloc page (may be expensive) (use MI_SECURE=FULL instead)" OFF)
option(MI_TLS_MODEL_LOCAL "Deprecated: Force TLS model based on thread local variables (use MI_TLS_MODEL=LOCAL instead)" OFF)
option(MI_TLS_MODEL_PTHREADS "Deprecated: Force TLS model based on pthreads (use MI_TLS_MODEL=PTHREADS instead)" OFF)
option(MI_TLS_MODEL_FIXED "Deprecated: Force TLS model based on fixed thread local slots (use MI_TLS_MODEL=FIXED instead)" OFF)
option(MI_LOCAL_DYNAMIC_TLS "Deprecated: Use local-dynamic-tls, a slightly slower but dlopen-compatible thread local storage mechanism (Unix) (use MI_TLS_MODEL=LOCAL_DYNAMIC instead)" OFF)
option(MI_WIN_USE_FLS "Deprecated: Use Fiber local storage on Windows to detect thread termination (use MI_WIN_INIT=FLS instead)" OFF)
option(MI_WIN_INIT_USE_RAW_DLLMAIN "Deprecated: Use the raw DLL main entry point for mimalloc initialization; can be more robust but can also lead to link errors with other libraries (use MI_WIN_INIT=RAW_DLLMAIN instead)" OFF)
option(MI_WIN_INIT_USE_TLS_DLLMAIN "Deprecated: Use legacy TLS entries with DllMain for mimalloc initialization (use MI_WIN_INIT=TLS_DLLMAIN instead)" OFF)
option(MI_WIN_DIRECT_TLS "Deprecated: Use only direct TLS slots on Windows to avoid extra tests in the malloc fast path (only works if the program uses less than 64 TlsAlloc'd slots in total)" OFF)
option(MI_TRACK_VALGRIND "Deprecated: Compile with Valgrind support (adds a small overhead) (use MI_TRACK=VALGRIND instead)" OFF)
option(MI_TRACK_ASAN "Deprecated: Compile with address sanitizer support (adds a small overhead) (use MI_TRACK=ASAN instead)" OFF)
option(MI_TRACK_ETW "Deprecated: Compile with Windows event tracing (ETW) support (adds a small overhead) (use MI_TRACK=ETW instead)" OFF)
set(MI_OPT_FREE_SMALL "DEFAULT" CACHE STRING "Deprecated: Use aligned `mi_free_small` without pagemap lookup (can only be used if SECURE or GUARDED mode are off)")
set_property(CACHE MI_OPT_FREE_SMALL PROPERTY STRINGS "OFF" "ON" "DEFAULT")
include(CheckLinkerFlag) # requires cmake 3.18
include(CheckIncludeFiles)
include(GNUInstallDirs)
include("cmake/mimalloc-config-version.cmake")
set(mi_sources
src/alloc.c
src/alloc-aligned.c
src/alloc-posix.c
src/arena.c
src/bitmap.c
src/heap.c
src/init.c
src/libc.c
src/options.c
src/os.c
src/page.c
src/page-map.c
src/prim/prim.c
src/prim/prim-tls.c
src/random.c
src/sample-guarded.c
src/sample-profile.c
src/stats.c
src/subproc.c
src/theap.c
src/threadlocal.c)
set(mi_cflags "")
set(mi_cflags_static "") # extra flags for a static library build
set(mi_cflags_dynamic "") # extra flags for a shared-object library build
set(mi_libraries "")
if(MI_EXTRA_CPPDEFS)
set(mi_defines ${MI_EXTRA_CPPDEFS})
else()
set(mi_defines "")
endif()
# pass git revision as a define
if(EXISTS "${CMAKE_SOURCE_DIR}/.git/index")
find_package(Git)
if(GIT_FOUND)
execute_process(COMMAND ${GIT_EXECUTABLE} "describe" OUTPUT_VARIABLE mi_git_describe RESULT_VARIABLE mi_git_res ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
if(mi_git_res EQUAL "0")
list(APPEND mi_defines "MI_GIT_DESCRIBE=${mi_git_describe}")
# add to dependencies so we rebuild if the git head commit changes
set_property(GLOBAL APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/.git/index")
endif()
endif()
endif()
# case insensitive comparison
function(STRIEQUAL str1 str2 is_equal)
string(TOUPPER "${str1}" str1_upper)
string(TOUPPER "${str2}" str2_upper)
if(str1_upper STREQUAL str2_upper)
set(${is_equal} TRUE PARENT_SCOPE)
else()
set(${is_equal} FALSE PARENT_SCOPE)
endif()
endfunction()
# -----------------------------------------------------------------------------
# Convenience: set default build type and compiler depending on the build directory
# -----------------------------------------------------------------------------
message(STATUS "")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
if ("${CMAKE_BINARY_DIR}" MATCHES ".*((D|d)ebug|asan|tsan|ubsan|valgrind)$")
message(STATUS "No build type selected, default to 'Debug'")
set(CMAKE_BUILD_TYPE "Debug")
else()
message(STATUS "No build type selected, default to 'Release'")
set(CMAKE_BUILD_TYPE "Release")
endif()
endif()
if(CMAKE_GENERATOR MATCHES "^Visual Studio.*$")
message(STATUS "Note: when building with Visual Studio the build type is specified when building.")
message(STATUS "For example: 'cmake --build . --config=Release")
endif()
if(NOT MI_SECURE AND NOT MI_SECURE_FULL AND "${CMAKE_BINARY_DIR}" MATCHES ".*(S|s)ecure$")
message(STATUS "Default to secure build")
set(MI_SECURE "ON")
endif()
# Determine architecture
if(CMAKE_SIZEOF_VOID_P EQUAL 16)
set(MI_ARCH_BITS "128")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(MI_ARCH_BITS "64")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(MI_ARCH_BITS "32")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 2)
set(MI_ARCH_BITS "16")
else()
set(MI_ARCH_BITS "")
endif()
set(MI_ARCH "unknown")
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86|i[3456]86)$" OR CMAKE_GENERATOR_PLATFORM MATCHES "^(x86|Win32)$")
set(MI_ARCH "x86")
elseif((CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|x64|amd64|AMD64)$" OR CMAKE_GENERATOR_PLATFORM STREQUAL "x64" OR "x86_64" IN_LIST CMAKE_OSX_ARCHITECTURES) AND NOT CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64") # must be before arm64
set(MI_ARCH "x64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|armv[89].?|ARM64)$" OR CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64" OR "arm64" IN_LIST CMAKE_OSX_ARCHITECTURES)
set(MI_ARCH "arm64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm|armv[34567].?|ARM)$")
set(MI_ARCH "arm32")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(riscv|riscv32|riscv64)$")
set(MI_ARCH "riscv${MI_ARCH_BITS}")
else()
set(MI_ARCH ${CMAKE_SYSTEM_PROCESSOR})
endif()
message(STATUS "Architecture: ${MI_ARCH} (${MI_ARCH_BITS} bit)") # (${CMAKE_SYSTEM_PROCESSOR}, ${CMAKE_GENERATOR_PLATFORM}, ${CMAKE_GENERATOR})")
# negative overrides (mainly to support vcpkg features)
if(MI_NO_USE_CXX)
set(MI_USE_CXX "OFF")
endif()
if(MI_NO_OPT_ARCH)
set(MI_OPT_ARCH "OFF")
endif()
if(MI_OPT_ARCH STREQUAL "DEFAULT")
if(MI_ARCH STREQUAL "arm64")
set(MI_OPT_ARCH "ON") # enable by default for arm64 (defaults to armv8.1-a which has fast atomics)
else()
set(MI_OPT_ARCH "OFF")
endif()
endif()
if(DEFINED ENV{MI_TEST_LIGHT})
list(APPEND mi_defines MI_TEST_LIGHT=1) # to lighten test load in test integration
endif()
# -----------------------------------------------------------------------------
# Enable the C++ compiler early on if needed
# -----------------------------------------------------------------------------
# clang-cl detection on windows
if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_C_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
set(MI_CLANG_CL "ON")
endif()
if(MI_USE_CXX STREQUAL "DEFAULT")
if(CMAKE_C_COMPILER_ID MATCHES "MSVC|Intel" OR MI_CLANG_CL)
# force C++ compilation with msvc or clang-cl to use modern C++ atomics
set(MI_USE_CXX "ON")
elseif(MI_DEBUG_UBSAN AND (CMAKE_BUILD_TYPE MATCHES "Debug" OR NOT CMAKE_CONFIGURATION_TYPES)) # ubsan needs C++
set(MI_USE_CXX "ON")
else()
set(MI_USE_CXX "OFF")
endif()
endif()
# enable C++ ?
if(MI_USE_CXX)
enable_language(CXX)
message(STATUS "Use the C++ compiler to compile (MI_USE_CXX=ON)")
if(CMAKE_CXX_COMPILER_ID MATCHES "Intel" AND NOT CMAKE_CXX_COMPILER_ID MATCHES "IntelLLVM")
list(APPEND mi_cflags -Kc++)
elseif(MSVC AND MSVC_VERSION GREATER_EQUAL 1914)
list(APPEND mi_cflags /Zc:__cplusplus)
endif()
endif()
# -----------------------------------------------------------------------------
# Process options
# -----------------------------------------------------------------------------
# put -Wall early so other warnings can be disabled selectively
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang")
if (MI_CLANG_CL)
list(APPEND mi_cflags -W)
else()
list(APPEND mi_cflags -Wall -Wextra -Wpedantic)
endif()
endif()
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
list(APPEND mi_cflags -Wall -Wextra)
endif()
if(CMAKE_C_COMPILER_ID MATCHES "Intel")
list(APPEND mi_cflags -Wall)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "AppleClang|Clang")
list(APPEND mi_cflags -Wno-deprecated)
endif()
if(MI_OVERRIDE)
message(STATUS "Override standard malloc (MI_OVERRIDE=ON)")
if(APPLE)
if(MI_OSX_ZONE)
# use zone's on macOS
message(STATUS " Use malloc zone to override malloc (MI_OSX_ZONE=ON)")
list(APPEND mi_sources src/prim/osx/alloc-override-zone.c)
list(APPEND mi_defines MI_OSX_ZONE=1)
if (NOT MI_OSX_INTERPOSE)
message(STATUS " WARNING: zone overriding usually also needs interpose (use -DMI_OSX_INTERPOSE=ON)")
endif()
endif()
if(MI_OSX_INTERPOSE)
# use interpose on macOS
message(STATUS " Use interpose to override malloc (MI_OSX_INTERPOSE=ON)")
list(APPEND mi_defines MI_OSX_INTERPOSE=1)
if (NOT MI_OSX_ZONE)
message(STATUS " WARNING: interpose usually also needs zone overriding (use -DMI_OSX_ZONE=ON)")
endif()
endif()
if(MI_USE_CXX AND MI_OSX_INTERPOSE)
message(STATUS " WARNING: if dynamically overriding malloc/free, it is more reliable to build mimalloc as C code (use -DMI_USE_CXX=OFF)")
endif()
endif()
endif()
if(WIN32)
if (NOT MI_WIN_REDIRECT)
# use a negative define for backward compatibility
list(APPEND mi_defines MI_WIN_NOREDIRECT=1)
endif()
endif()
if(MI_SECURE_FULL)
message(STATUS "MI_SECURE_FULL is deprecated, use MI_SECURE=FULL instead")
set(MI_SECURE "FULL")
endif()
if(MI_SECURE STREQUAL "FULL")
message(STATUS "Use full secure build (MI_SECURE=FULL, may be expensive)")
list(APPEND mi_defines MI_SECURE=5)
elseif(MI_SECURE)
message(STATUS "Use secure build (MI_SECURE=${MI_SECURE})")
list(APPEND mi_defines MI_SECURE=4)
endif()
if(MI_TRACK_VALGRIND)
message(STATUS "Use of MI_TRACK_VALGRIND is deprecated, use MI_TRACK=VALGRIND instead")
set(MI_TRACK "VALGRIND")
elseif(MI_TRACK_ASAN)
message(STATUS "Use of MI_TRACK_ASAN is deprecated, use MI_TRACK=ASAN instead")
set(MI_TRACK "ASAN")
elseif(MI_TRACK_ETW)
message(STATUS "Use of MI_TRACK_ETW is deprecated, use MI_TRACK=ETW instead")
set(MI_TRACK "ETW")
endif()
if(MI_TRACK STREQUAL "VALGRIND")
CHECK_INCLUDE_FILES("valgrind/valgrind.h;valgrind/memcheck.h" MI_HAS_VALGRINDH)
if (NOT MI_HAS_VALGRINDH)
set(MI_TRACK OFF)
message(WARNING "Cannot find the 'valgrind/valgrind.h' and 'valgrind/memcheck.h' -- install valgrind first?")
message(STATUS "Disabling Valgrind support (MI_TRACK=OFF)")
else()
message(STATUS "Compile with Valgrind support (MI_TRACK=VALGRIND)")
list(APPEND mi_defines MI_TRACK_VALGRIND=1)
endif()
elseif(MI_TRACK STREQUAL "ASAN")
if (APPLE AND MI_OVERRIDE)
set(MI_TRACK OFF)
message(WARNING "Cannot enable address sanitizer support on macOS if MI_OVERRIDE is ON (MI_TRACK=OFF)")
else()
CHECK_INCLUDE_FILES("sanitizer/asan_interface.h" MI_HAS_ASANH)
if (NOT MI_HAS_ASANH)
set(MI_TRACK OFF)
message(WARNING "Cannot find the 'sanitizer/asan_interface.h' -- install address sanitizer support first")
message(STATUS "Compile **without** address sanitizer support (MI_TRACK=OFF)")
else()
message(STATUS "Compile with address sanitizer support (MI_TRACK=ASAN)")
list(APPEND mi_defines MI_TRACK_ASAN=1)
list(APPEND mi_cflags -fsanitize=address)
list(APPEND mi_libraries -fsanitize=address)
endif()
endif()
elseif(MI_TRACK STREQUAL "ETW")
if(NOT WIN32)
set(MI_TRACK OFF)
message(WARNING "Can only enable ETW support on Windows (MI_TRACK=OFF)")
else()
message(STATUS "Compile with Windows event tracing support (MI_TRACK=ETW)")
list(APPEND mi_defines MI_TRACK_ETW=1)
endif()
elseif(MI_TRACK)
message(STATUS "Invalid option value: MI_TRACK=${MI_TRACK} (ignored)")
endif()
if(MI_SEE_ASM)
message(STATUS "Generate assembly listings (MI_SEE_ASM=ON)")
if(CMAKE_C_COMPILER_ID MATCHES "MSVC" OR MI_CLANG_CL)
list(APPEND mi_cflags -FA)
else()
list(APPEND mi_cflags -save-temps=obj)
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER 14)
message(STATUS "No GNU Line marker")
list(APPEND mi_cflags -Wno-gnu-line-marker)
endif()
endif()
endif()
if (MI_SKIP_COLLECT_ON_EXIT)
message(STATUS "Skip collecting memory on program exit (MI_SKIP_COLLECT_ON_EXIT=ON)")
list(APPEND mi_defines MI_SKIP_COLLECT_ON_EXIT=1)
endif()
if(MI_CHECK_FULL OR MI_DEBUG_FULL)
message(STATUS "Use of MI_CHECK_FULL/MI_DEBUG_FULL is deprecated, use MI_DEBUG=FULL instead")
if(MI_DEBUG STREQUAL "DEFAULT")
set(MI_DEBUG "FULL")
endif()
elseif(MI_DEBUG_INTERNAL)
message(STATUS "Use of MI_DEBUG_INTERNAL is deprecated, use MI_DEBUG=INTERNAL instead")
if(MI_DEBUG STREQUAL "DEFAULT")
set(MI_DEBUG "INTERNAL")
endif()
endif()
if(MI_DEBUG STREQUAL "DEFAULT")
if(CMAKE_CONFIGURATION_TYPES)
# Multi-config generators: Choose debug flags using generator expression
list(APPEND mi_defines $<IF:$<CONFIG:Debug>,MI_DEBUG=2,MI_DEBUG=0 NDEBUG=1>)
set(MI_DEBUG OFF) # don't go into further MI_DEBUG checks below...
else()
if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(MI_DEBUG "INTERNAL")
else()
set(MI_DEBUG "OFF")
endif()
endif()
endif()
if(MI_NO_DEBUG)
message(STATUS "Disable assertion checks (MI_NO_DEBUG=ON)")
list(APPEND mi_defines NDEBUG=1)
set(MI_DEBUG OFF)
elseif(MI_DEBUG STREQUAL "FULL")
message(STATUS "Set debug level to full assertion and internal invariant checking (MI_DEBUG=FULL, expensive)")
list(APPEND mi_defines MI_DEBUG=3) # full invariant checking (mi_assert, mi_assert_internal, and mi_assert_expensive)
elseif(MI_DEBUG STREQUAL "INTERNAL")
message(STATUS "Set debug level to internal assertion and invariant checking (MI_DEBUG=INTERNAL)")
list(APPEND mi_defines MI_DEBUG=2) # invariant checking (mi_assert and mi_assert_internal)
elseif(MI_DEBUG)
message(STATUS "Set debug level to assertion checking (MI_DEBUG=${MI_DEBUG})")
list(APPEND mi_defines MI_DEBUG=1) # assertion checking (mi_assert)
elseif(NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Disable assertion checks (MI_DEBUG=${MI_DEBUG})")
list(APPEND mi_defines NDEBUG=1)
endif()
if(MI_GUARDED STREQUAL "DEFAULT")
if(MI_DEBUG)
set(MI_GUARDED "FULL")
else()
set(MI_GUARDED "OFF")
endif()
endif()
if(MI_GUARDED STREQUAL "FULL")
message(STATUS "Enable fine-grained sampling for guard pages (MI_GUARDED=FULL)")
list(APPEND mi_defines MI_GUARDED=2)
elseif(MI_GUARDED)
message(STATUS "Enable sampled guard pages (MI_GUARDED=${MI_GUARDED})")
list(APPEND mi_defines MI_GUARDED=1)
else()
# message(STATUS "Disable guard pages (MI_GUARDED=${MI_GUARDED})")
list(APPEND mi_defines MI_GUARDED=0)
endif()
if(MI_NO_PADDING)
message(STATUS "Suppress any padding of heap blocks (MI_NO_PADDING=ON)")
list(APPEND mi_defines MI_PADDING=0)
else()
if(MI_PADDING)
message(STATUS "Enable explicit padding of heap blocks (MI_PADDING=ON)")
list(APPEND mi_defines MI_PADDING=1)
endif()
endif()
if(MI_FREE_IS_CHECKED STREQUAL "DEFAULT")
if (MI_SECURE)
set(MI_FREE_IS_CHECKED ON)
else()
set(MI_FREE_IS_CHECKED OFF)
endif()
endif()
if(MI_FREE_IS_CHECKED STREQUAL "ON")
message(STATUS "Check validity eof pointers passed to 'mi_free' (MI_FREE_IS_CHECKED=ON)")
list(APPEND mi_defines MI_FREE_IS_CHECKED=1)
elseif(MI_SECURE)
message(STATUS "Check validity of pointers passed to 'mi_free' (MI_SECURE=ON, MI_FREE_IS_CHECKED=ON)")
elseif(MI_FREE_IS_CHECKED STREQUAL "SHARED")
message(STATUS "Check validity of pointers passed to 'mi_free' only in the dynamic/shared link library (MI_FREE_IS_CHECKED=SHARED)")
list(APPEND mi_cflags_dynamic -DMI_FREE_IS_CHECKED=1)
elseif(MI_FREE_IS_CHECKED STREQUAL "OFF")
# message(STATUS "Do not check validity of pointers passed to 'mi_free' (MI_FREE_IS_CHECKED=OFF)")
else()
message(STATUS "Invalid value for MI_FREE_IS_CHECKED: ${MI_FREE_IS_CHECKED} (expecting: DEFAULT, OFF, SHARED, or ON)")
endif()
if(MI_STATS STREQUAL "DEFAULT")
if(MI_DEBUG)
set(MI_STATS "FULL")
else()
set(MI_STATS "ON")
endif()
endif()
if(MI_STATS STREQUAL "FULL")
message(STATUS "Enable detailed statistics including precise requested bytes (expensive) (MI_STATS=FULL)")
list(APPEND mi_defines MI_STATS=2)
elseif(MI_STATS)
message(STATUS "Enable detailed statistics (MI_STATS=${MI_STATS})")
list(APPEND mi_defines MI_STATS=1)
else()
message(STATUS "Disable detailed statistics (MI_STATS=${MI_STATS})")
list(APPEND mi_defines MI_STATS=0)
endif()
if(MI_PROFILE STREQUAL "DEFAULT")
if(MI_DEBUG)
set(MI_PROFILE "FULL")
else()
set(MI_PROFILE "ON")
endif()
endif()
if(MI_PROFILE STREQUAL "FULL")
message(STATUS "Enable fine-grained sampled profiling (MI_PROFILE=FULL)")
list(APPEND mi_defines MI_PROFILE=2)
elseif(MI_PROFILE)
message(STATUS "Enable sampled profiling (MI_PROFILE=${MI_PROFILE})")
list(APPEND mi_defines MI_PROFILE=1)
else()
message(STATUS "Disable profiling (MI_PROFILE=${MI_PROFILE})")
list(APPEND mi_defines MI_PROFILE=0)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
message(STATUS "Enabling MI_FREE_USE_PAGEMAP since the target (WASI) does not support large OS aligned allocations well")
set(MI_FREE_USE_PAGEMAP ON)
endif()
if(MI_FREE_USE_PAGEMAP)
message(STATUS "Always use the pagemap in mi_free (MI_FREE_USE_PAGEMAP=ON)")
list(APPEND mi_defines MI_FREE_USE_PAGEMAP=1)
endif()
# deprecated
if(MI_OPT_FREE_SMALL STREQUAL "DEFAULT")
# do nothing
elseif(MI_OPT_FREE_SMALL)
if(MI_GUARDED OR MI_SECURE)
message(STATUS "Cannot enable MI_OPT_FREE_SMALL in secure or guarded mode (MI_OPT_FREE_SMALL=OFF)")
else()
message(STATUS "Use aligned `mi_free_small` (MI_OPT_FREE_SMALL=ON)")
list(APPEND mi_defines MI_OPT_FREE_SMALL=1)
endif()
else()
message(STATUS "Disable aligned `mi_free_small` (MI_OPT_FREE_SMALL=OFF)")
list(APPEND mi_defines MI_OPT_FREE_SMALL=0)
endif()
if(MI_XMALLOC)
message(STATUS "Enable abort() calls on memory allocation failure (MI_XMALLOC=ON)")
list(APPEND mi_defines MI_XMALLOC=1)
endif()
if(MI_SHOW_ERRORS)
message(STATUS "Enable printing of error and warning messages by default (MI_SHOW_ERRORS=ON)")
list(APPEND mi_defines MI_SHOW_ERRORS=1)
endif()
if(MI_DEBUG_TSAN)
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
message(STATUS "Build with thread sanitizer (MI_DEBUG_TSAN=ON)")
list(APPEND mi_defines MI_TSAN=1)
list(APPEND mi_cflags -fsanitize=thread -g -O1)
list(APPEND mi_libraries -fsanitize=thread)
else()
message(WARNING "Can only use thread sanitizer with clang (MI_DEBUG_TSAN=ON but ignored)")
endif()
endif()
if(MI_DEBUG_UBSAN)
if(CMAKE_BUILD_TYPE MATCHES "Debug" OR CMAKE_CONFIGURATION_TYPES)
if(MI_USE_CXX)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
message(STATUS "Build with undefined-behavior sanitizer (MI_DEBUG_UBSAN=ON)")
list(APPEND mi_defines MI_UBSAN=1)
list(APPEND mi_cflags -fsanitize=undefined -g -fno-sanitize-recover=undefined)
list(APPEND mi_libraries -fsanitize=undefined)
else()
message(WARNING "Can only use undefined-behavior sanitizer with clang++ (MI_DEBUG_UBSAN=ON but ignored)")
endif()
else()
message(WARNING "Can only use undefined-behavior sanitizer with a C++ build (MI_USE_CXX=ON)")
endif()
else()
message(WARNING "Can only use undefined-behavior sanitizer with a debug build (CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})")
endif()
endif()
if(CMAKE_SYSTEM_NAME MATCHES "Linux|Android")
if(MI_NO_THP)
set(MI_ALLOW_THP "OFF")
endif()
if(MI_ALLOW_THP STREQUAL "DEFAULT")
if(CMAKE_SYSTEM_NAME MATCHES "Android")
set(MI_ALLOW_THP "OFF")
else()
set(MI_ALLOW_THP "FULL")
endif()
endif()
if(MI_ALLOW_THP STREQUAL "FULL")
message(STATUS "Enable full transparent huge pages support, where purging avoids splitting huge pages (MI_ALLOW_THP=FULL)")
list(APPEND mi_defines MI_DEFAULT_ALLOW_THP=2)
elseif (MI_ALLOW_THP)
message(STATUS "Enable transparent huge pages support (MI_ALLOW_THP=${MI_ALLOW_THP}) (also consider using FULL for better performance)")
list(APPEND mi_defines MI_DEFAULT_ALLOW_THP=1)
else()
message(STATUS "Disable transparent huge pages support (MI_ALLOW_THP=${MI_ALLOW_THP})")
list(APPEND mi_defines MI_DEFAULT_ALLOW_THP=0)
endif()
endif()
if(MI_LIBC_MUSL)
message(STATUS "Assume using musl libc (MI_LIBC_MUSL=ON)")
list(APPEND mi_defines MI_LIBC_MUSL=1)
endif()
if(MI_TLS_MODEL STREQUAL "DEFAULT")
if(MI_TLS_MODEL_PTHREADS)
message(STATUS "Use of MI_TLS_MODEL_PTHREADS is deprecated, use MI_TLS_MODEL=PTHREADS instead")
set(MI_TLS_MODEL "PTHREADS")
elseif(MI_TLS_MODEL_FIXED)
message(STATUS "Use of MI_TLS_MODEL_FIXED is deprecated, use MI_TLS_MODEL=FIXED instead")
set(MI_TLS_MODEL "FIXED")
elseif(MI_TLS_MODEL_LOCAL)
message(STATUS "Use of MI_TLS_MODEL_LOCAL is deprecated, use MI_TLS_MODEL=LOCAL instead")
set(MI_TLS_MODEL "LOCAL")
elseif(MI_LOCAL_DYNAMIC_TLS)
message(STATUS "Use of MI_LOCAL_DYNAMIC_TLS is deprecated, use MI_TLS_MODEL=LOCAL_DYNAMIC instead")
set(MI_TLS_MODEL "LOCAL_DYNAMIC")
endif()
endif()
if(MI_TLS_MODEL STREQUAL "PTHREADS")
message(STATUS "Use TLS model based on pthreads (MI_TLS_MODEL=PTHREADS)")
list(APPEND mi_defines MI_TLS_MODEL_PTHREADS=1)
elseif(MI_TLS_MODEL STREQUAL "FIXED")
message(STATUS "Use TLS model based on fixed thread local slots (MI_TLS_MODEL=FIXED)")
message(STATUS "Note: fixed TLS slots do not allow multiple mimalloc instances in a single process.")
list(APPEND mi_defines MI_TLS_MODEL=FIXED=1)
elseif(MI_TLS_MODEL STREQUAL "LOCAL")
message(STATUS "Use TLS model based on thread local (MI_TLS_MODEL=LOCAL)")
if(APPLE)
message(WARNING "On macOS the MI_TLS_MODEL=LOCAL does not work reliably when dynamically overriding mimalloc. MI_TLS_MODEL=PTHREADS is recommended instead.")
endif()
list(APPEND mi_defines MI_TLS_MODEL_LOCAL=1)
elseif(MI_TLS_MODEL STREQUAL "LOCAL_DYNAMIC")
message(STATUS "Use TLS model based on dlopen compatible dynamic thread locals (MI_TLS_MODEL=LOCAL_DYNAMIC)")
list(APPEND mi_defines MI_TLS_MODEL_LOCAL=1)
elseif(MI_TLS_MODEL STREQUAL "WIN32")
message(STATUS "Use TLS model based on direct Win32 TlsAlloc'd slots (MI_TLS_MODEL=WIN32)")
list(APPEND mi_defines MI_TLS_MODEL_WIN32=1)
elseif(NOT (MI_TLS_MODEL STREQUAL "DEFAULT"))
message(STATUS "Invalid option value: MI_TLS_MODEL=${MI_TLS_MODEL} (ignored)")
endif()
if(MI_TLS_RECURSE_GUARD)
message(STATUS "Use TLS recursion guard (for MI_TLS_MODEL_LOCAL only)")
list(APPEND mi_defines MI_TLS_RECURSE_GUARD=1)
endif()
if("$ENV{MSYSTEM}" STREQUAL "UCRT64")
message(STATUS "Detected MSYS2 UCRT64 environment")
set(MI_MINGW_UCRT64 "ON")
list(APPEND mi_defines MI_MINGW_UCRT64=1)
elseif("$ENV{MSYSTEM}" STREQUAL "MSYS")
message(STATUS "Detected MSYS2 MSYS/Cygwin environment")
set(MI_CYGWIN "ON")
endif()
if(MI_WIN_DIRECT_TLS)
message(STATUS "Use only direct TLS slots on Windows to avoid extra tests in the malloc fast path -- this only works if the program uses less than 64 TlsAlloc'd slots in total! (MI_WIN_USE_ONLY_DIRECT_TLS=ON)")
list(APPEND mi_defines MI_WIN_DIRECT_TLS=1)
endif()
if(MI_WIN_INIT STREQUAL "DEFAULT")
if(MI_WIN_INIT_USE_RAW_DLLMAIN)
message(STATUS "Use of MI_WIN_INIT_USE_RAW_DLLMAIN is deprecated, use MI_WIN_INIT=RAW_DLLMAIN instead")
set(MI_WIN_INIT "RAW_DLLMAIN")
elseif(MI_WIN_INIT_USE_TLS_DLLMAIN)
message(STATUS "Use of MI_WIN_INIT_USE_TLS_DLLMAIN is deprecated, use MI_WIN_INIT=TLS_DLLMAIN instead")
set(MI_WIN_INIT "TLS_DLLMAIN")
elseif(MI_WIN_USE_FLS)
message(STATUS "Use of MI_WIN_USE_FLS is deprecated, use MI_WIN_INIT=FLS instead")
set(MI_WIN_INIT "FLS")
endif()
endif()
if(MI_WIN_INIT STREQUAL "RAW_DLLMAIN")
message(STATUS "Use raw Dll main entry on Windows to initialize and finalize mimalloc (MI_WIN_INIT=RAW_DLLMAIN)")
message(STATUS "Note: this can lead to link errors if other libraries also try to define a raw Dll main entry point")
list(APPEND mi_defines MI_WIN_INIT_USE_RAW_DLLMAIN=1)
elseif(MI_WIN_INIT STREQUAL "TLS_DLLMAIN")
message(STATUS "Use Dll main entry with TLS entries on Windows to initialize and finalize mimalloc (MI_WIN_INIT=TLS_DLLMAIN)")
list(APPEND mi_defines MI_WIN_INIT_USE_TLS_DLLMAIN=1)
elseif(MI_WIN_INIT STREQUAL "FLS")
message(STATUS "Use the Fiber API to detect thread termination (deprecated) (MI_WIN_INIT=FLS)")
list(APPEND mi_defines MI_WIN_INIT_USE_FLS=1)
elseif(NOT (MI_WIN_INIT STREQUAL "DEFAULT"))
message(STATUS "Invalid option value: MI_WIN_INIT=${MI_WIN_INIT} (ignored)")
endif()
if(MI_ARCH MATCHES "riscv")
CHECK_INCLUDE_FILES("asm/hwprobe.h" MI_HAS_ASM_HWPROBEH)
if (MI_HAS_ASM_HWPROBEH)
list(APPEND mi_defines MI_HAS_ASM_HWPROBEH=1)
endif()
CHECK_INCLUDE_FILES("sys/hwprobe.h" MI_HAS_SYS_HWPROBEH)
if (MI_HAS_SYS_HWPROBEH)
list(APPEND mi_defines MI_HAS_SYS_HWPROBEH=1)
endif()
endif()
if(NOT WIN32)
CHECK_INCLUDE_FILES("execinfo.h" MI_HAS_EXECINFOH)
if(MI_HAS_EXECINFOH)
list(APPEND mi_defines MI_HAS_EXECINFOH=1)
endif()
endif()
# On Haiku use `-DCMAKE_INSTALL_PREFIX` instead, issue #788
# if(CMAKE_SYSTEM_NAME MATCHES "Haiku")
# SET(CMAKE_INSTALL_LIBDIR ~/config/non-packaged/lib)
# SET(CMAKE_INSTALL_INCLUDEDIR ~/config/non-packaged/headers)
# endif()
# Compiler flags
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU" AND NOT MI_CLANG_CL)
list(APPEND mi_cflags -Wno-unknown-pragmas -fvisibility=hidden)
if(NOT MI_USE_CXX)
list(APPEND mi_cflags -Wstrict-prototypes)
endif()
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang")
list(APPEND mi_cflags -Wno-static-in-inline -mno-outline)
endif()
if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND MI_ARCH STREQUAL "arm64")
list(APPEND mi_cflags -Xclang -fno-split-cold-code)
endif()
endif()
if(CMAKE_C_COMPILER_ID MATCHES "Intel")
list(APPEND mi_cflags -fvisibility=hidden)
endif()
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU|Intel" AND NOT CMAKE_SYSTEM_NAME MATCHES "Haiku" AND NOT MI_CLANG_CL)
if(MI_TLS_MODEL STREQUAL "LOCAL_DYNAMIC")
list(APPEND mi_cflags -ftls-model=local-dynamic)
else()
if(MI_LIBC_MUSL)
# with musl we use local-dynamic for the static build, see issue #644
list(APPEND mi_cflags_static -ftls-model=local-dynamic)
list(APPEND mi_cflags_dynamic -ftls-model=initial-exec)
message(STATUS "Use local dynamic TLS for the static build (since MI_LIBC_MUSL=ON)")
else()
list(APPEND mi_cflags -ftls-model=initial-exec)
endif()
endif()
endif()
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU|Intel")
if(MI_OVERRIDE)
list(APPEND mi_cflags -fno-builtin-malloc)
endif()
endif()
# Compiler and architecture specific flags
if(MI_OPT_ARCH MATCHES "^(1|ON|YES|TRUE|Y)$")
if(APPLE AND CMAKE_OSX_ARCHITECTURES AND CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang" )
set(MI_OPT_ARCH "ON")
elseif(MI_ARCH STREQUAL "arm64")
if(APPLE)
set(MI_OPT_ARCH "armv8.3") # M1+ supports arm8.3-a (which includes LDAPR (load-aquire))
else()
set(MI_OPT_ARCH "armv8.1") # enable armv8.1-a by default (which has fast atomics)
endif()
elseif(MI_ARCH STREQUAL "arm32")
set(MI_OPT_ARCH "armv8.1") # enable armv8.1-a by default (which has fast atomics)
elseif(MI_ARCH STREQUAL "x64")
set(MI_OPT_ARCH "avx2")
elseif(MI_ARCH MATCHES "riscv")
set(MI_OPT_ARCH "rv${MI_ARCH_BITS}gcb_zacas") # fast bit scan and atomics (since 2023)
# set(MI_OPT_ARCH "rv${MI_ARCH_BITS}gcb_zacas_zalasr") # fast bit scan and atomics (since 2025)
else()
set(MI_OPT_ARCH OFF)
endif()
endif()
# Compiler and architecture specific flags
set(MI_OPT_ARCH_FLAGS "")
if(NOT MI_OPT_ARCH MATCHES "^(0|OFF|NO|FALSE|N)$")
# apple support for multi-arch binaries (#999)
if(APPLE AND CMAKE_OSX_ARCHITECTURES AND CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang")
if("arm64" IN_LIST CMAKE_OSX_ARCHITECTURES)
list(APPEND MI_OPT_ARCH_FLAGS "-Xarch_arm64;-march=armv8.3-a")
endif()
if("x86_64" IN_LIST CMAKE_OSX_ARCHITECTURES)
list(APPEND MI_OPT_ARCH_FLAGS "-Xarch_x86_64;-march=haswell;-Xarch_x86_64;-mavx2")
endif()
# clang/gcc
elseif(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU|Intel" AND NOT CMAKE_SYSTEM_NAME MATCHES "Haiku")
if(MI_OPT_ARCH STREQUAL "avx2")
set(MI_OPT_ARCH_FLAGS "-march=haswell;-mavx2") # fast bit scan (since 2013)
elseif(MI_OPT_ARCH MATCHES "^armv[89].*$")
set(MI_OPT_ARCH_FLAGS "-march=${MI_OPT_ARCH}-a") # fast atomics
elseif(MI_OPT_ARCH MATCHES "^rva[0-9][0-9]$")
set(MI_OPT_ARCH_FLAGS "-march=${MI_OPT_ARCH}u${MI_ARCH_BITS}")
elseif(MI_OPT_ARCH MATCHES "^rv.*$")
set(MI_OPT_ARCH_FLAGS "-march=${MI_OPT_ARCH}")
else()
set(MI_OPT_ARCH_FLAGS "-march=${MI_OPT_ARCH}")
message(STATUS "warning: unknown MI_OPT_ARCH=${MI_OPT_ARCH} passed as '-march=${MI_OPT_ARCH}'")
endif()
# msvc
elseif (MSVC AND MSVC_VERSION GREATER_EQUAL 1914 AND NOT MI_CLANG_CL) # vs2017+
if(MI_OPT_ARCH STREQUAL "avx2")
set(MI_OPT_ARCH_FLAGS "/arch:AVX2")
elseif(MI_OPT_ARCH MATCHES "^armv[89].*$")
set(MI_OPT_ARCH_FLAGS "/arch:${MI_OPT_ARCH}") # fast atomics
else()
message(STATUS "warning: unknown MI_OPT_ARCH=${MI_OPT_ARCH} passed as '/arch:${MI_OPT_ARCH}'")
endif()
endif()
endif()
if(MINGW)
add_definitions(-D_WIN32_WINNT=0x600) # issue #976
endif()
if(MI_OPT_ARCH_FLAGS)
list(APPEND mi_cflags ${MI_OPT_ARCH_FLAGS})
message(STATUS "Architecture specific optimization is enabled (with ${MI_OPT_ARCH_FLAGS}) (MI_OPT_ARCH=${MI_OPT_ARCH})")
if (MI_OPT_SIMD)
list(APPEND mi_defines "MI_OPT_SIMD=1")
message(STATUS "SIMD instructions are enabled (MI_OPT_SIMD=ON)")
endif()
elseif(MI_OPT_SIMD)
message(STATUS "SIMD instructions are not enabled (either MI_OPT_ARCH=OFF or this architecture has no SIMD support)")
endif()
# extra needed libraries
# we prefer -l<lib> test over `find_library` as sometimes core libraries
# like `libatomic` are not on the system path (see issue #898)
function(find_link_library libname outlibname)
check_linker_flag(C "-l${libname}" mi_has_lib${libname})
if (mi_has_lib${libname})
message(VERBOSE "link library: -l${libname}")
set(${outlibname} ${libname} PARENT_SCOPE)
else()
find_library(MI_LIBPATH_${libname} ${libname})
if (MI_LIBPATH_${libname})
message(VERBOSE "link library ${libname} at ${MI_LIBPATH_${libname}}")
set(${outlibname} ${MI_LIBPATH_${libname}} PARENT_SCOPE)
else()
message(VERBOSE "link library not found: ${libname}")
set(${outlibname} "" PARENT_SCOPE)
endif()
endif()
endfunction()
if(WIN32)
list(APPEND mi_libraries psapi shell32 user32 advapi32 bcrypt)
else()
find_link_library("pthread" MI_LIB_PTHREAD)
if(MI_LIB_PTHREAD)
list(APPEND mi_libraries "${MI_LIB_PTHREAD}")
endif()
find_link_library("rt" MI_LIB_RT)
if(MI_LIB_RT)
list(APPEND mi_libraries "${MI_LIB_RT}")
endif()
find_link_library("atomic" MI_LIB_ATOMIC)
if(MI_LIB_ATOMIC)
list(APPEND mi_libraries "${MI_LIB_ATOMIC}")
endif()
endif()
# set language for source files now
if(MI_USE_CXX)
set_source_files_properties(${mi_sources} PROPERTIES LANGUAGE CXX )
set_source_files_properties(src/static.c test/test-api.c test/test-api-fill.c test/test-stress.c test/test-stress-heaps.c test/test-stress-subprocs.c test/test-profile.c PROPERTIES LANGUAGE CXX )
endif()
# -----------------------------------------------------------------------------
# Install and output names
# -----------------------------------------------------------------------------
# dynamic/shared library and symlinks always go to /usr/local/lib equivalent
# we use ${CMAKE_INSTALL_BINDIR} and ${CMAKE_INSTALL_LIBDIR}.
# static libraries and object files, includes, and cmake config files
# are either installed at top level, or use versioned directories for side-by-side installation (default)
if (MI_INSTALL_TOPLEVEL)
set(mi_install_objdir "${CMAKE_INSTALL_LIBDIR}")
set(mi_install_incdir "${CMAKE_INSTALL_INCLUDEDIR}")
set(mi_install_cmakedir "${CMAKE_INSTALL_LIBDIR}/cmake/mimalloc")
else()
set(mi_install_objdir "${CMAKE_INSTALL_LIBDIR}/mimalloc-${mi_version}") # for static library and object files
set(mi_install_incdir "${CMAKE_INSTALL_INCLUDEDIR}/mimalloc-${mi_version}") # for includes
set(mi_install_cmakedir "${CMAKE_INSTALL_LIBDIR}/cmake/mimalloc-${mi_version}") # for cmake package info
endif()
set(mi_libname "mimalloc")
if(MI_SECURE)
set(mi_libname "${mi_libname}-secure")
endif()
if(MI_TRACK STREQUAL "VALGRIND")
set(mi_libname "${mi_libname}-valgrind")
endif()
if(MI_TRACK STREQUAL "ASAN")
set(mi_libname "${mi_libname}-asan")
endif()
set(mi_libname_base "${mi_libname}") # capture the base name (without any per-config suffix)
if (CMAKE_CONFIGURATION_TYPES)
# multi-config builds
# generator expression lower-case build type
set(mi_gx_build_type_lc $<LOWER_CASE:$<CONFIG>>)
# generator expression for "is this a release build?"
set(mi_gx_release $<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>,$<CONFIG:MinSizeRel>,$<CONFIG:none>>)
list(APPEND mi_defines "MI_CMAKE_BUILD_TYPE=${mi_gx_build_type_lc}")
list(APPEND mi_defines $<${mi_gx_release}:MI_BUILD_RELEASE>)
# append build type (e.g. -debug) if not a release version
set(mi_libname "${mi_libname}$<$<NOT:${mi_gx_release}>:-${mi_gx_build_type_lc}>")
else()
# single-config builds
string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LC)
list(APPEND mi_defines "MI_CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE_LC}") #todo: multi-config project needs $<CONFIG> ?
if(CMAKE_BUILD_TYPE_LC MATCHES "^(release|relwithdebinfo|minsizerel|none)$")
list(APPEND mi_defines MI_BUILD_RELEASE)
else()
set(mi_libname "${mi_libname}-${CMAKE_BUILD_TYPE_LC}") #append build type (e.g. -debug) if not a release version
endif()
endif()
string(REGEX REPLACE "^mimalloc[-]?" "" mi_libsuffix "${mi_libname}")
if(MI_BUILD_SHARED)
list(APPEND mi_build_targets "shared")
endif()
if(MI_BUILD_STATIC)
list(APPEND mi_build_targets "static")
endif()
if(MI_BUILD_OBJECT)
list(APPEND mi_build_targets "object")
endif()
if(MI_BUILD_TESTS)
list(APPEND mi_build_targets "tests")
endif()
message(STATUS "")
message(STATUS "Library name : ${mi_libname}")
message(STATUS "Version : ${mi_version}.${mi_version_patch}")
if(CMAKE_BUILD_TYPE_LC)
message(STATUS "Build type : ${CMAKE_BUILD_TYPE_LC}")
endif()
if(MI_USE_CXX)
message(STATUS "C++ Compiler : ${CMAKE_CXX_COMPILER}")
else()
message(STATUS "C Compiler : ${CMAKE_C_COMPILER}")
endif()
message(STATUS "Compiler flags : ${mi_cflags}")
message(STATUS "Compiler defines : ${mi_defines}")
message(STATUS "Link libraries : ${mi_libraries}")
message(STATUS "Build targets : ${mi_build_targets}")
message(STATUS "")
# -----------------------------------------------------------------------------
# Main targets
# -----------------------------------------------------------------------------
# shared library
if(MI_BUILD_SHARED)
add_library(mimalloc SHARED ${mi_sources})
set_target_properties(mimalloc PROPERTIES VERSION ${mi_version} SOVERSION ${mi_version_major} OUTPUT_NAME ${mi_libname} )
target_compile_definitions(mimalloc PRIVATE ${mi_defines} MI_SHARED_LIB MI_SHARED_LIB_EXPORT)
target_compile_options(mimalloc PRIVATE ${mi_cflags} ${mi_cflags_dynamic})
target_link_libraries(mimalloc PRIVATE ${mi_libraries})