-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
725 lines (616 loc) · 23.4 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
# Copyright (c) 2021 The WhiteBox Authors. All rights reserved.
# Use of this source code is governed by a 3-Clause BSD license that can be
# found in the LICENSE file.
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
# Controls either WhiteBox tests be build or not.
option(WB_BUILD_TESTS "Build WhiteBox tests or not." OFF)
# According to CMake docs if any dependency sets BUILD_SHARED_LIBS root project
# must do it before bringing in dependencies. Failure to do so can lead to
# different behavior between the first and subsequent CMake runs.
#
# See https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html
option(BUILD_SHARED_LIBS "Build the library as a shared library" OFF)
if (WB_BUILD_TESTS)
enable_testing()
endif()
# Setup root directory of the WhiteBox project.
set(WB_ROOT_DIR "${CMAKE_SOURCE_DIR}" CACHE STRING
"Directory root of the WhiteBox project.")
# CMake should find its new includes.
if (CMAKE_MODULE_PATH)
set(CMAKE_MODULE_PATH "${WB_ROOT_DIR}/cmake" "${CMAKE_MODULE_PATH}")
else()
set(CMAKE_MODULE_PATH "${WB_ROOT_DIR}/cmake")
endif()
## Package information.
set(PACKAGE_NAME "WhiteBox")
# Git is required to build software version via Git.
find_package(Git)
set(PACKAGE_VER_MAJOR 1)
set(PACKAGE_VER_MINOR 0)
set(PACKAGE_VER_PATCH 0)
if (NOT DEFINED PACKAGE_VERSION)
if (NOT Git_FOUND)
message(FATAL "[common]: Unable to extract Git software version - no Git.")
endif()
message(STATUS "[common]: Extracting Git software version.")
if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
execute_process(COMMAND cmd /c "git rev-list --branches HEAD | find /v " " /c" OUTPUT_VARIABLE WB_GIT_VERSION WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
else()
execute_process(COMMAND bash "-c" "git rev-list --branches HEAD | wc -l | tr -d ' ' | tr -d '\n'" OUTPUT_VARIABLE WB_GIT_VERSION WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif()
math(EXPR PACKAGE_VER_TWEAK ${WB_GIT_VERSION})
message(STATUS
"[common]: Build version from Git (patch.tweak): ${PACKAGE_VER_PATCH}.${PACKAGE_VER_TWEAK}.")
set(PACKAGE_VER_MAJOR 1)
set(PACKAGE_VER_MINOR 0)
set(PACKAGE_VERSION
"${PACKAGE_VER_MAJOR}.${PACKAGE_VER_MINOR}.${PACKAGE_VER_PATCH}.${PACKAGE_VER_TWEAK}")
endif()
set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
set(PACKAGE_TARNAME "${PACKAGE_NAME}-${PACKAGE_VERSION}")
set(PACKAGE_BUGREPORT "https://github.com/The-White-Box/whitebox/issues")
project(${PACKAGE_NAME}
VERSION ${PACKAGE_VER_MAJOR}.${PACKAGE_VER_MINOR}.${PACKAGE_VER_PATCH}.${PACKAGE_VER_TWEAK}
LANGUAGES CXX)
message(STATUS "[common]: CMake version: ${CMAKE_VERSION}.")
message(STATUS "[common]: ${PACKAGE_NAME} version: ${PACKAGE_VERSION}.")
## Common variables.
set(WB_BINARY_DIR "${CMAKE_BINARY_DIR}" CACHE STRING
"Directory of the WhiteBox binaries output.")
set(WB_SYSTEM_NAME "${CMAKE_SYSTEM_NAME}" CACHE STRING
"Name of the WhiteBox build system.")
if (${WB_SYSTEM_NAME} STREQUAL "Darwin")
set(WB_OS_MACOS ON CACHE BOOL "MacOS is build system.")
else()
set(WB_OS_MACOS OFF CACHE BOOL "MacOS is NOT build system.")
endif()
if (${WB_SYSTEM_NAME} STREQUAL "Linux")
set(WB_OS_LINUX ON CACHE BOOL "Linux distro is build system.")
else()
set(WB_OS_LINUX OFF CACHE BOOL "Linux distro is NOT build system.")
endif()
if (${WB_SYSTEM_NAME} STREQUAL "Windows")
set(WB_OS_WIN ON CACHE BOOL "Windows is build system.")
else()
set(WB_OS_WIN OFF CACHE BOOL "Windows is NOT build system.")
endif()
if (WB_OS_MACOS OR WB_OS_LINUX)
set(WB_OS_POSIX ON CACHE BOOL "POSIX compatible os is build system.")
else()
set(WB_OS_POSIX OFF CACHE BOOL "POSIX compatible os is NOT build system.")
endif()
set(WB_PRODUCT_NAME "WhiteBox Games Collection" CACHE STRING
"Name of the WhiteBox product.")
# Minimum standard level is C++23.
set(CMAKE_CXX_STANDARD 23)
# On g++ this ensures: -std=c++23 and not -std=gnu++23
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(wb_common_functions)
include(wb_build_functions)
wb_check_platform_requirements(${PACKAGE_NAME})
set(WB_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
"CMake build type used to build ${PACKAGE_NAME}.")
set(WB_CMAKE_GENERATOR "${CMAKE_GENERATOR}" CACHE STRING
"CMake generator used to generate build tree." FORCE)
set(WB_CXX_COMPILER "${CMAKE_CXX_COMPILER}" CACHE STRING
"Compiler used to build ${PACKAGE_NAME}.")
set(WB_CXX_COMPILER_ID "${CMAKE_CXX_COMPILER_ID}" CACHE STRING
"Compiler id used to build ${PACKAGE_NAME}.")
set(WB_CXX_COMPILER_FRONTEND_VARIANT "${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}"
CACHE STRING "Compiler frontend used to build ${PACKAGE_NAME}.")
set(WB_EXPORT_COMPILE_COMMANDS ${CMAKE_EXPORT_COMPILE_COMMANDS} CACHE BOOL
"Enable/Disable output of compile commands during generation.")
if (WB_CXX_COMPILER_ID MATCHES "Clang")
set(WB_COMPILER_CLANG ON CACHE BOOL "Compiler is Clang.")
else()
set(WB_COMPILER_CLANG OFF CACHE BOOL "Compiler is NOT Clang.")
endif()
if (WB_CXX_COMPILER_ID STREQUAL "GNU")
set(WB_COMPILER_GCC ON CACHE BOOL "Compiler is GCC.")
else()
set(WB_COMPILER_GCC OFF CACHE BOOL "Compiler is NOT GCC.")
endif()
if (WB_CXX_COMPILER_ID STREQUAL "MSVC")
set(WB_COMPILER_MSVC ON CACHE BOOL "Compiler is MSVC.")
else()
set(WB_COMPILER_MSVC OFF CACHE BOOL "Compiler is NOT MSVC.")
endif()
## Common dependencies.
if (Git_FOUND)
message(STATUS
"[common]: Git version ${GIT_VERSION_STRING} found at '${GIT_EXECUTABLE}'.")
# Get the current working branch.
execute_process(
COMMAND "${GIT_EXECUTABLE}" rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${WB_ROOT_DIR}
OUTPUT_VARIABLE WB_GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE)
# Get the latest commit hash.
execute_process(
COMMAND "${GIT_EXECUTABLE}" rev-parse HEAD
WORKING_DIRECTORY ${WB_ROOT_DIR}
OUTPUT_VARIABLE WB_GIT_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE)
# Get the subject of the commit.
execute_process(
COMMAND "${GIT_EXECUTABLE}" log -1 --format=%s
WORKING_DIRECTORY "${WB_ROOT_DIR}"
OUTPUT_VARIABLE WB_GIT_COMMIT_SUBJECT
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS
"[common]: Git using branch '${WB_GIT_BRANCH}', "
"commit ${WB_GIT_COMMIT_HASH} | ${WB_GIT_COMMIT_SUBJECT}.")
endif()
message(STATUS
"[common]: ${PACKAGE_NAME} using CMake generator: ${WB_CMAKE_GENERATOR}.")
# Select correct threading library.
if (NOT WB_OS_WIN)
set(CMAKE_THREAD_PREFER_PTHREAD ON)
set(THREADS_PREFER_PTHREAD_FLAG ON)
endif()
find_package(Threads REQUIRED)
list(APPEND CMAKE_REQUIRED_LIBRARIES Threads::Threads)
if (NOT CMAKE_THREAD_LIBS_INIT)
set(WB_THREADS_LIBRARY "system default" CACHE STRING
"Threads library used to build ${PACKAGE_NAME}.")
else()
set(WB_THREADS_LIBRARY "${CMAKE_THREAD_LIBS_INIT}" CACHE STRING
"Threads library used to build ${PACKAGE_NAME}.")
endif()
message(STATUS
"[common]: ${PACKAGE_NAME} using threading library: ${WB_THREADS_LIBRARY}.")
if (XCODE_VERSION)
message(STATUS
"[common]: ${PACKAGE_NAME} using XCode version: ${XCODE_VERSION}.")
endif()
## Compiler / linker options.
if (WB_COMPILER_CLANG)
# Clang or AppleClang.
if (WB_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
message(STATUS
"[common]: ${PACKAGE_NAME} use compiler Clang + MSVC frontend.")
# Clang with clang-cl front end.
include(compilers/wb_msvc)
elseif (WB_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU")
message(STATUS
"[common]: ${PACKAGE_NAME} use compiler Clang + GNU frontend.")
# Clang with regular front end.
include(compilers/wb_clang)
elseif (NOT WB_CXX_COMPILER_FRONTEND_VARIANT)
# No compiler frontend means Apple one.
message(STATUS
"[common]: ${PACKAGE_NAME} use compiler Clang + Apple frontend.")
# Clang with regular front end.
include(compilers/wb_clang)
else()
message(FATAL_ERROR
"[common]: Using Clang with unknown frontend "
"'${WB_CXX_COMPILER_FRONTEND_VARIANT}'. "
"Please, define compiler/linker options for it.")
endif()
elseif (WB_COMPILER_GCC)
message(STATUS "[common]: ${PACKAGE_NAME} use compiler GNU.")
include(compilers/wb_gcc)
elseif (WB_COMPILER_MSVC)
message(STATUS "[common]: ${PACKAGE_NAME} use compiler MSVC.")
message(STATUS
"[common]: ${PACKAGE_NAME} MSVC version: ${MSVC_VERSION}")
message(STATUS
"[common]: ${PACKAGE_NAME} MSVC toolset version: ${MSVC_TOOLSET_VERSION}")
include(compilers/wb_msvc)
else()
# Add compilers if needed.
message(FATAL_ERROR
"[common]: '${WB_CXX_COMPILER_ID}' cxx compiler is not supported. "
"Please, define compiler/linker options for it.")
endif()
message(STATUS "[common]: ${PACKAGE_NAME} cxx compiler: ${WB_CXX_COMPILER}.")
message(STATUS "[common]: ${PACKAGE_NAME} build type: ${WB_BUILD_TYPE}.")
# Generate common product version info.
configure_file(
${WB_ROOT_DIR}/build/product_version_config.h.cmake.in
${WB_BINARY_DIR}/product_version_config.h
)
if (WB_OS_WIN)
# Remove clashing min/max macroses from windows headers.
add_definitions(-DNOMINMAX)
endif()
## Product dependencies.
# fmt.
set(FMT_INSTALL OFF CACHE BOOL "Generate the install target." FORCE)
add_subdirectory("deps/fmt" EXCLUDE_FROM_ALL)
if (WB_COMPILER_MSVC)
# Prevent MSVC warning C4530: C++ exception handler used, but unwind
# semantics are not enabled.
target_compile_options(
fmt
PRIVATE
/EH${WB_MSVC_EXCEPTION_HANDLING_MODEL}
)
# Prevent MSVC linker warning as error for __except + /guard:ehcont.
target_compile_options(
fmt
PRIVATE
$<$<BOOL:${WB_MSVC_ENABLE_GUARD_FOR_EH_CONTINUATION}>:/guard:ehcont>
)
elseif (WB_COMPILER_CLANG OR WB_COMPILER_GCC)
# Prevent linker warning "reloc which may overflow at runtime" (-fPIC).
target_compile_options(fmt PRIVATE -fPIC)
endif()
# g3log.
add_subdirectory("deps/g3log" EXCLUDE_FROM_ALL)
if (WB_COMPILER_MSVC)
# Prevent MSVC warning C4530: C++ exception handler used, but unwind
# semantics are not enabled.
target_compile_options(
g3log
PRIVATE
/EH${WB_MSVC_EXCEPTION_HANDLING_MODEL}
)
endif()
if (WB_OS_WIN)
# Adds VERSION resource to g3log.
wb_add_version_resource_to_dll_target(
g3log
deps/g3log
"G3log is an asynchronous, crash-safe logger. See THIRD_PARTY for details.")
endif()
# gtest / gmock.
if (WB_BUILD_TESTS)
# Prevent overriding the parent project's compiler/linker settings.
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
add_subdirectory("deps/googletest" EXCLUDE_FROM_ALL)
# Do not define macroses which can clash with other libs.
target_compile_definitions(gtest
PRIVATE
GTEST_DONT_DEFINE_FAIL=1
GTEST_DONT_DEFINE_SUCCEED=1
GTEST_DONT_DEFINE_TEST=1
)
if (WB_COMPILER_MSVC)
# Prevent MSVC linker warning as error for __except + /guard:ehcont.
target_compile_options(
gtest
PRIVATE
$<$<BOOL:${WB_MSVC_ENABLE_GUARD_FOR_EH_CONTINUATION}>:/guard:ehcont>
)
target_link_options(
gtest
PRIVATE
$<$<BOOL:${WB_MSVC_ENABLE_GUARD_FOR_EH_CONTINUATION}>:/guard:ehcont>
)
target_compile_options(
gmock
PRIVATE
$<$<BOOL:${WB_MSVC_ENABLE_GUARD_FOR_EH_CONTINUATION}>:/guard:ehcont>
)
target_link_options(
gmock
PRIVATE
$<$<BOOL:${WB_MSVC_ENABLE_GUARD_FOR_EH_CONTINUATION}>:/guard:ehcont>
)
target_compile_options(
gmock_main
PRIVATE
$<$<BOOL:${WB_MSVC_ENABLE_GUARD_FOR_EH_CONTINUATION}>:/guard:ehcont>
)
target_link_options(
gmock_main
PRIVATE
$<$<BOOL:${WB_MSVC_ENABLE_GUARD_FOR_EH_CONTINUATION}>:/guard:ehcont>
)
elseif (WB_COMPILER_CLANG OR WB_COMPILER_GCC)
# Prevent linker warning "reloc which may overflow at runtime" (-fPIC).
target_compile_options(gtest PRIVATE -fPIC)
target_compile_options(gmock PRIVATE -fPIC)
endif()
endif()
# abseil.
# Abseil libraries require C++11 as the current minimum standard. Top-level
# application CMake projects should ensure a consistent C++ standard for all
# compiled sources by setting CMAKE_CXX_STANDARD.
set(ABSL_PROPAGATE_CXX_STD ON)
# Abseil will use gtest / gmock targets from source tree.
set(ABSL_USE_EXTERNAL_GOOGLETEST ON)
set(ABSL_FIND_GOOGLETEST OFF)
add_subdirectory("deps/abseil" EXCLUDE_FROM_ALL)
# Get all abseil targets to add overrides.
wb_get_all_targets(wb_all_abseil_targets "deps/abseil")
# Ensure all abseil targets use expected options & macroses.
foreach(wb_abseil_target_name ${wb_all_abseil_targets})
if (${wb_abseil_target_name} MATCHES "absl_" AND
NOT ${wb_abseil_target_name} STREQUAL "absl_civil_time" AND
NOT ${wb_abseil_target_name} STREQUAL "absl_time_zone")
get_target_property(wb_target_type ${wb_abseil_target_name} TYPE)
if (NOT ("${wb_target_type}" STREQUAL "INTERFACE_LIBRARY"))
if (WB_COMPILER_MSVC)
# Warning C4530: C++ exception handler used, but unwind semantics are not
# enabled.
target_compile_options(
${wb_abseil_target_name}
PRIVATE
# Uses std and hence exceptions are required.
/EHsc
$<$<BOOL:${WB_MSVC_ENABLE_GUARD_FOR_EH_CONTINUATION}>:/guard:ehcont>
)
# LNK2047: module contains C++ EH or complex EH metadata but was not
# compiled with /guard:ehcont.
target_link_options(
${wb_abseil_target_name}
PRIVATE
$<$<BOOL:${WB_MSVC_ENABLE_GUARD_FOR_EH_CONTINUATION}>:/guard:ehcont>
)
elseif(WB_COMPILER_CLANG OR WB_COMPILER_GCC)
# Can't create dynamic relocation R_X86_64_32 against local symbol in
# readonly segment.
target_compile_options(${wb_abseil_target_name} PRIVATE -fPIC)
endif()
if (WB_BUILD_TESTS)
# Abseil uses macroses without GTEST_ prefix.
target_compile_definitions(${wb_abseil_target_name}
INTERFACE
GTEST_DONT_DEFINE_FAIL=0
GTEST_DONT_DEFINE_SUCCEED=0
GTEST_DONT_DEFINE_TEST=0
)
endif()
endif()
endif()
endforeach()
if (WB_COMPILER_MSVC)
# absl_civil_time is special and requires PRIVATE options.
target_compile_options(
absl_civil_time
PRIVATE
/EH${WB_MSVC_EXCEPTION_HANDLING_MODEL}
$<$<BOOL:${WB_MSVC_ENABLE_GUARD_FOR_EH_CONTINUATION}>:/guard:ehcont>
)
target_link_options(
absl_civil_time
PRIVATE
$<$<BOOL:${WB_MSVC_ENABLE_GUARD_FOR_EH_CONTINUATION}>:/guard:ehcont>
)
# absl_time_zone is special and requires PRIVATE options.
target_compile_options(
absl_time_zone
PRIVATE
/EH${WB_MSVC_EXCEPTION_HANDLING_MODEL}
$<$<BOOL:${WB_MSVC_ENABLE_GUARD_FOR_EH_CONTINUATION}>:/guard:ehcont>
)
target_link_options(
absl_time_zone
PRIVATE
$<$<BOOL:${WB_MSVC_ENABLE_GUARD_FOR_EH_CONTINUATION}>:/guard:ehcont>
)
elseif(WB_COMPILER_CLANG OR WB_COMPILER_GCC)
# Can't create dynamic relocation R_X86_64_32 against local symbol in readonly
# segment.
target_compile_options(absl_civil_time PRIVATE -fPIC)
target_compile_options(absl_time_zone PRIVATE -fPIC)
endif()
# marl.
set(MARL_BUILD_TESTS ON CACHE BOOL "Build tests" FORCE)
set(MARL_BUILD_SHARED ON CACHE BOOL "Build marl as a shared / dynamic library (default static)" FORCE)
set(MARL_GOOGLETEST_DIR ON CACHE STRING "deps/googletest" FORCE)
set(MARL_WARNINGS_AS_ERRORS ON CACHE BOOL "Treat warnings as errors" FORCE)
add_subdirectory("deps/marl" EXCLUDE_FROM_ALL)
if (WB_OS_WIN)
# Adds VERSION resource.
wb_add_version_resource_to_dll_target(
marl
deps/marl
"Marl is a hybrid thread / fiber task scheduler written in C++ 11. See THIRD_PARTY for details.")
endif()
# mimalloc.
set(MI_BUILD_SHARED ON CACHE BOOL "Build shared library" FORCE)
set(MI_BUILD_STATIC OFF CACHE BOOL "Build static library" FORCE)
if (WB_MSVC_ENABLE_ADDRESS_SANITIZER)
# Enable AddressSanitizer support.
set(MI_TRACK_ASAN ON)
endif()
add_subdirectory("deps/mimalloc" EXCLUDE_FROM_ALL)
if (WB_OS_WIN)
# Adds VERSION resource.
wb_add_version_resource_to_dll_target(
mimalloc
deps/mimalloc
"Mimalloc is a general purpose allocator with excellent performance characteristics. See THIRD_PARTY for details.")
endif()
if (MI_SECURE)
# Inform we has mimalloc in secure mode.
add_definitions(-DWB_MI_SECURE)
endif()
if (WB_OS_WIN)
# Define dependent mimalloc-redirect target to make CMake know about.
add_library(mimalloc-redirect SHARED IMPORTED GLOBAL)
set_property(TARGET mimalloc-redirect PROPERTY IMPORTED_IMPLIB "${WB_ROOT_DIR}/deps/mimalloc/bin/mimalloc-redirect.lib")
set_property(TARGET mimalloc-redirect PROPERTY IMPORTED_LOCATION "${WB_ROOT_DIR}/deps/mimalloc/bin/mimalloc-redirect.dll")
endif()
# SDL expects mimalloc to be exported.
export(TARGETS mimalloc NAMESPACE "mi::" FILE "miSharedTargets.cmake")
# SDL3 / sdl_image.
if (NOT WB_OS_WIN)
set(SDL_STATIC OFF CACHE BOOL "Build static SDL library" FORCE)
set(SDL_SHARED ON CACHE BOOL "Build shared SDL library" FORCE)
set(SDL_TEST_LIBRARY OFF CACHE BOOL "Build the SDL3_test library" FORCE)
add_subdirectory(deps/sdl EXCLUDE_FROM_ALL)
if (WB_CXX_COMPILER_ID STREQUAL "AppleClang")
# Fix for undefined memset_pattern4() on MacOS.
target_compile_definitions(SDL3-shared PUBLIC _DARWIN_C_SOURCE=1)
endif()
set(PNG_SHARED ON CACHE BOOL "Build shared lib" FORCE)
set(PNG_STATIC OFF CACHE BOOL "Build static lib" FORCE)
set(PNG_TESTS OFF CACHE BOOL "Build libpng tests" FORCE)
# These causes errors during CMake configure.
set(SDLIMAGE_AVIF OFF CACHE BOOL "Enable AVIF support" FORCE)
set(SDLIMAGE_TIF OFF CACHE BOOL "Enable TIFF support" FORCE)
set(SDLIMAGE_WEBP OFF CACHE BOOL "Enable WEBP support" FORCE)
add_subdirectory(deps/sdl_image EXCLUDE_FROM_ALL)
endif()
## Libraries.
add_subdirectory("base")
add_subdirectory("boot-manager")
add_subdirectory("ui")
## Hardware Abstraction Layer.
add_subdirectory("hal/drivers/hid")
add_subdirectory("hal/drivers/cpu")
## Kernel.
add_subdirectory("kernel")
## Apps.
add_subdirectory("apps/half-life-2")
## Other stuff.
# Get all targets to add mimalloc allocators overrides.
set(WB_ALL_TARGETS "")
wb_get_all_targets(WB_ALL_TARGETS "${WB_ROOT_DIR}")
# Do some common stuff for all dependencies.
foreach(wb_target_name ${WB_ALL_TARGETS})
get_target_property(wb_target_type ${wb_target_name} TYPE)
# Only code gen targets.
if (((${wb_target_type} MATCHES "LIBRARY" AND
NOT ${wb_target_type} MATCHES "INTERFACE_LIBRARY") OR
${wb_target_type} MATCHES "EXECUTABLE"))
# Ensure all libs use the same STL debug options.
if (WB_COMPILER_CLANG OR WB_COMPILER_GCC)
if (WB_COMPILER_CLANG)
# Prevent tons of variable not defined warnings.
if(NOT DEFINED WB_CLANG_DEFINE__GLIBCXX_ASSERTIONS)
set(WB_CLANG_DEFINE__GLIBCXX_ASSERTIONS OFF)
endif()
if(NOT DEFINED WB_CLANG_DEFINE__LIBCPP_DEBUG)
set(WB_CLANG_DEFINE__LIBCPP_DEBUG OFF)
endif()
endif()
if (WB_COMPILER_GCC)
# Prevent tons of variable not defined warnings.
if(NOT DEFINED WB_GCC_DEFINE__GLIBCXX_ASSERTIONS)
set(WB_GCC_DEFINE__GLIBCXX_ASSERTIONS OFF)
endif()
endif()
target_compile_definitions(${wb_target_name}
PRIVATE
$<$<CONFIG:DEBUG>:
# Run-time bounds checking for C++ strings/containers.
$<$<OR:$<BOOL:${WB_CLANG_DEFINE__GLIBCXX_ASSERTIONS}>,$<BOOL:${WB_GCC_DEFINE__GLIBCXX_ASSERTIONS}>>:_GLIBCXX_ASSERTIONS>
# Catch libstdc++ usage errors by enabling debug mode. When defined,
# _GLIBCXX_ASSERTIONS is defined automatically
$<$<OR:$<BOOL:${WB_CLANG_DEFINE__GLIBCXX_ASSERTIONS}>,$<BOOL:${WB_GCC_DEFINE__GLIBCXX_ASSERTIONS}>>:_GLIBCXX_DEBUG>
# When defined makes the debug mode extremely picky by making the use of
# libstdc++ extensions and libstdc++-specific behavior into errors.
$<$<OR:$<BOOL:${WB_CLANG_DEFINE__GLIBCXX_ASSERTIONS}>,$<BOOL:${WB_GCC_DEFINE__GLIBCXX_ASSERTIONS}>>:_GLIBCXX_DEBUG_PEDANTIC>
# Enables special debugging checks meant to detect incorrect usage of the libc++ standard library.
$<$<NOT:$<STREQUAL:"${WB_CLANG_DEFINE__LIBCPP_DEBUG}","">>:_LIBCPP_DEBUG=${WB_CLANG_DEFINE__LIBCPP_DEBUG}>
>)
endif()
# Need to have pdbs on MSVC to simplify debugging.
if (WB_COMPILER_MSVC)
target_compile_options(${wb_target_name}
PRIVATE
# Enable AddressSanitizer.
$<$<BOOL:${WB_MSVC_ENABLE_ADDRESS_SANITIZER}>:/fsanitize=address>
)
target_link_options(${wb_target_name}
PRIVATE
# Enable address space layout randomization.
/DYNAMICBASE
# ASLR can use the entire 64-bit address space. To have an effect, set the
# option on both the executable and all modules that it depends on.
# This option requires /LARGEADDRESSAWARE. To have an effect at load time,
# /DYNAMICBASE must also be enabled.
/HIGHENTROPYVA
# Application can handle addresses larger than 2 gigabytes.
/LARGEADDRESSAWARE
# Enable linking to the default AddressSanitizer libraries.
$<$<BOOL:${WB_MSVC_ENABLE_ADDRESS_SANITIZER}>:/INFERASANLIBS>
$<$<CONFIG:DEBUG>:
# Generate a partial PDB file that simply references the original object
# and library files.
$<IF:$<BOOL:${WB_MSVC_ENABLE_DEBUG_FAST_LINK}>,/DEBUG:FASTLINK,/DEBUG:FULL>
>
$<$<NOT:$<CONFIG:DEBUG>>:
# Generate full pdbs or not.
$<IF:$<BOOL:${WB_MSVC_ENABLE_RELEASE_DEBUG_INFO}>,/DEBUG:FULL,/DEBUG:NONE>
>
)
endif()
# Do not add mimalloc / custom targets allocators to itself and some specific
# actions.
if (# ASAN intercepts new / delete.
NOT ${wb_target_name} MATCHES "absl_leak_check_test" AND
NOT ${wb_target_name} MATCHES "absl_disabled_leak_check_test" AND
# Exclude itself.
NOT ${wb_target_name} MATCHES "mimalloc")
# Include the directories with the new files.
target_include_directories(${wb_target_name}
PRIVATE
${WB_ROOT_DIR}
"${WB_ROOT_DIR}/deps/mimalloc/include")
# Add new / delete allocator overrides.
target_sources(${wb_target_name}
PRIVATE
${WB_ROOT_DIR}/base/memory/memory_allocators_overrides.cc
)
# Add mimalloc to handle allocators.
target_link_libraries(${wb_target_name} PRIVATE mimalloc)
# mimalloc should be copied by each target individually as haven't found way
# to it here.
endif()
endif()
endforeach()
## Install
# Bundle Windows Universal CRT, too.
set(CMAKE_INSTALL_UCRT_LIBRARIES TRUE)
include(InstallRequiredSystemLibraries)
include(GNUInstallDirs)
set(WB_INSTALL_TARGETS
half-life-2
g3log
marl
mimalloc
whitebox-base
whitebox-boot-manager
whitebox-cpu-driver
whitebox-hid-driver
whitebox-kernel
whitebox-ui)
install(
TARGETS
${WB_INSTALL_TARGETS}
PERMISSIONS
OWNER_READ OWNER_EXECUTE
ARCHIVE
LIBRARY
RUNTIME
BUNDLE
DESTINATION
${CMAKE_INSTALL_BINDIR}
)
if (WB_OS_WIN AND MI_BUILD_SHARED)
# Install mimalloc-redirect as it is imported target.
install(
IMPORTED_RUNTIME_ARTIFACTS
mimalloc-redirect
PERMISSIONS
OWNER_READ OWNER_EXECUTE
RUNTIME
)
endif()
install(
FILES
"${WB_ROOT_DIR}/LICENSE"
"${WB_ROOT_DIR}/THIRD_PARTY"
"${WB_ROOT_DIR}/SECURITY.md"
DESTINATION
.
PERMISSIONS
OWNER_READ OWNER_EXECUTE
)
configure_file(
"${WB_ROOT_DIR}/build/app_cpack_options.cmake.in"
"${WB_BINARY_DIR}/app_cpack_options.cmake"
@ONLY
)
set(CPACK_PROJECT_CONFIG_FILE
"${WB_BINARY_DIR}/app_cpack_options.cmake")
include(CPack)