-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
200 lines (171 loc) · 6.46 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
cmake_minimum_required(VERSION 3.27)
project(hello VERSION 0.1.0 LANGUAGES CXX)
## ============================================================================
## Global CMake Variables.
## ============================================================================
set(CMAKE_CXX_STANDARD 26)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})
set(CMAKE_CXX_STANDARD_REQUIRED ON)
## ============================================================================
## Global compiler options.
## ============================================================================
## Turn on diagnostics colours.
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(-fdiagnostics-color=always)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options(-fcolor-diagnostics)
endif()
## Use mold as the default linker, if it exists.
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
find_program(MOLD_LINKER "mold")
if (MOLD_LINKER)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(-fuse-ld=mold)
endif()
add_link_options(-fuse-ld=mold)
endif()
endif()
## ============================================================================
## Compiler options.
## ============================================================================
add_library(options INTERFACE)
## Flags for Clang and GCC.
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(options INTERFACE
## Warnings.
-Wall -Wextra # Enable ‘all’ warnings.
-Wundef # Invalid #undef or undefined macro in #if.
-Wcast-align # Casting that changes alignment.
-Wconversion # Implicit conversions.
-Wsign-conversion # Implicit sign conversions.
-Wformat=2 # Stricter format checking.
## Disabled warnings.
-Wno-unused-function
-Wno-unused-local-typedefs
## NULL Errors.
-Werror=nonnull # Passing NULL to nonnull parameter.
## Memory Errors.
-Werror=address # Suspicious use of addresses.
-Werror=init-self # Initialization of a variable with itself.
-Werror=uninitialized
## Return type.
-Werror=return-type
-Wmissing-noreturn
## C/C++.
-Werror=implicit-fallthrough
-Werror=pointer-arith # Disallow void* and function pointer arithmetic.
-Werror=string-compare # Nonsensical string comparisons.
-Werror=switch # Missing switch cases.
# -Werror=switch-enum # Switch on enum (even if there is a default case).
-Werror=write-strings # Strings in C should be const char*.
## C++.
-Werror=missing-field-initializers
-Werror=non-virtual-dtor
-Werror=pessimizing-move
)
endif()
## Additional flags for GCC.
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(options INTERFACE
-Wlogical-op # Duplicate or unintended logical operators.
-Werror=invalid-memory-model # For atomics.
-Werror=maybe-uninitialized
-Werror=missing-requires
-Werror=return-local-addr
)
endif()
## Additional flags for Clang.
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(options INTERFACE
-Werror=dangling
-Werror=return-stack-address
)
endif()
## Flags for MSVC.
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_compile_options(options INTERFACE
/W4 # Enable ‘all’ warnings.
# Allow unnamed structs/unions.
/wd4201
# Source code is UTF-8.
/utf-8
)
endif()
## On Windows, don’t suggest the _s nonsense functions.
if (WIN32)
target_compile_definitions(options INTERFACE
_CRT_SECURE_NO_WARNINGS
_CRT_SECURE_NO_WARNINGS_GLOBALS
_CRT_NONSTDC_NO_WARNINGS
)
endif()
## Address Sanitiser.
if (ENABLE_ASAN)
target_compile_options(options INTERFACE -fsanitize=address)
target_link_options(options INTERFACE -fsanitize=address)
endif()
## Debug/Release flags.
if (NOT MSVC)
target_compile_options(options INTERFACE
$<$<CONFIG:DEBUG>:-O0 -g3 -ggdb3>
$<$<CONFIG:RELEASE>:-O3 -march=native>
)
target_link_options(options INTERFACE
$<$<CONFIG:DEBUG>:-O0 -g3 -ggdb3 -rdynamic>
$<$<CONFIG:RELEASE>:-O3 -march=native>
)
else()
target_compile_options(options INTERFACE
$<$<CONFIG:DEBUG>:/Od>
$<$<CONFIG:RELEASE>:/O2>
)
endif()
## ============================================================================
## Submodules and include dirs.
## ============================================================================
# ‘src’ should be an include directory.
target_include_directories(options INTERFACE src)
## ‘include’ too, if it exists.
if (EXISTS ${PROJECT_SOURCE_DIR}/include)
target_include_directories(options INTERFACE include)
endif()
## As well as everything in ‘libs’.
if (EXISTS ${PROJECT_SOURCE_DIR}/libs)
file(GLOB libs ${PROJECT_SOURCE_DIR}/libs/*)
## Add the include directory to the include path, or the root
## directory if there is no include directory.
foreach(lib ${libs})
if (IS_DIRECTORY ${lib}/include)
target_include_directories(options INTERFACE ${lib}/include)
else()
target_include_directories(options INTERFACE ${lib})
endif()
endforeach()
## Also add all of them as subdirectories if they have a CMakeLists.txt.
foreach (lib ${libs})
if (EXISTS ${lib}/CMakeLists.txt)
add_subdirectory(${lib})
endif()
endforeach()
endif()
## Add libbase.
include(FetchContent)
FetchContent_Declare(base
GIT_REPOSITORY https://github.com/Sirraide/libbase
GIT_TAG master
)
FetchContent_MakeAvailable(base)
## ============================================================================
## Executables and libraries.
## ============================================================================
## Everything in ‘src’ is part of the project.
file(GLOB_RECURSE sources src/*.cc src/*.hh)
## As well as everything in ‘include’, if it exists.
if (EXISTS ${PROJECT_SOURCE_DIR}/include)
file(GLOB_RECURSE includes include/*.hh)
endif()
## Add the executable.
add_executable(hello ${sources})
target_sources(hello PRIVATE FILE_SET HEADERS FILES ${includes})
## Apply our options.
target_link_libraries(hello PRIVATE options libbase)