generated from mcmarius/oop-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
110 lines (86 loc) · 4.52 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
cmake_minimum_required(VERSION 3.24)
project(oop)
cmake_policy(SET CMP0135 NEW)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
option(WARNINGS_AS_ERRORS "Treat warnings as errors" OFF)
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "install_dir" CACHE PATH "..." FORCE)
endif ()
set(is_debug "$<CONFIG:Debug>")
set(is_rel_with_deb "$<CONFIG:RelWithDebInfo>")
set(debug_mode "$<OR:${is_debug},${is_rel_with_deb}>")
include(cmake/dependencies.cmake)
include(cmake/googleapis.cmake)
add_subdirectory(proto)
add_subdirectory(src)
###############################################################################
function(set_custom_stdlib_and_sanitizers target add_apple_asan)
if (MSVC)
# see https://gitlab.kitware.com/cmake/cmake/-/issues/24922
set_target_properties(${target} PROPERTIES VS_USER_PROPS "${CMAKE_SOURCE_DIR}/disable_modules.props")
target_compile_options(${target} PRIVATE "$<${debug_mode}:/fsanitize=address>" /experimental:module-)
return()
endif ()
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
target_compile_options(${target} PRIVATE -stdlib=libc++)
target_link_options(${target} PRIVATE -stdlib=libc++)
endif ()
if (APPLE)
# first check Apple since Apple is also a kind of Unix
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND add_apple_asan MATCHES true)
target_compile_options(${target} PRIVATE "$<${debug_mode}:-fsanitize=address,undefined>")
target_link_options(${target} PRIVATE "$<${debug_mode}:-fsanitize=address,undefined>")
endif ()
elseif (UNIX)
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
if ("${CMAKE_CXX_COMPILER_VERSION}" MATCHES "12.")
# check leaks on Linux since macOS does not support the leaks sanitizer yet
# leaks sanitizer is enabled by default on Linux, so we do not need to enable it explicitly
target_compile_options(${target} PRIVATE "$<${debug_mode}:-fsanitize=address,undefined>")
target_link_options(${target} PRIVATE "$<${debug_mode}:-fsanitize=address,undefined>")
endif ()
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
if ("${CMAKE_CXX_COMPILER_VERSION}" MATCHES "14.")
# use semi-colons instead of spaces to separate arguments
# it is recommended to quote generator expressions in order to avoid unintentional splitting
target_compile_options(${target} PRIVATE "$<${debug_mode}:-fsanitize=memory,undefined;-fsanitize-recover=memory,undefined;-fsanitize-memory-track-origins>")
target_link_options(${target} PRIVATE "$<${debug_mode}:-fsanitize=memory,undefined;-fsanitize-recover=memory,undefined;-fsanitize-memory-track-origins;-Wl,-rpath,tools/llvm-project/build/lib>")
else ()
message("No matching Clang version to add sanitizer flags!")
endif ()
endif ()
endif ()
endfunction()
###############################################################################
# target definitions
if (GITHUB_ACTIONS)
message("NOTE: GITHUB_ACTIONS defined")
target_compile_definitions(${PROJECT_NAME} PRIVATE GITHUB_ACTIONS)
endif ()
###############################################################################
if (WARNINGS_AS_ERRORS)
set_property(TARGET ${PROJECT_NAME} PROPERTY COMPILE_WARNING_AS_ERROR ON)
endif ()
# custom compiler flags
message("Compiler: ${CMAKE_CXX_COMPILER_ID} version ${CMAKE_CXX_COMPILER_VERSION}")
if (MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /permissive- /wd4244 /wd4267 /wd4996 /external:anglebrackets /external:W0)
else ()
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -pedantic)
endif ()
###############################################################################
# sanitizers
set_custom_stdlib_and_sanitizers(${PROJECT_NAME} true)
###############################################################################
###############################################################################
# copy binaries to "bin" folder; these are uploaded as artifacts on each release
# update name in .github/workflows/cmake.yml:29 when changing "bin" name here
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
# install(DIRECTORY some_dir1 some_dir2 DESTINATION bin)
# install(FILES some_file1.txt some_file2.md DESTINATION bin)
if (APPLE)
install(FILES launcher.command DESTINATION bin)
endif ()