-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
168 lines (144 loc) · 5.49 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
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 -D_GLIBCXX_ASSERTIONS>
$<$<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()
## ============================================================================
## Dependencies and include dirs.
## ============================================================================
# ‘include’ should be an include directory.
target_include_directories(options INTERFACE include)
## Add libbase.
include(FetchContent)
FetchContent_Declare(base
GIT_REPOSITORY https://github.com/Sirraide/libbase
GIT_TAG master
)
FetchContent_MakeAvailable(base)
## ============================================================================
## Executables and libraries.
## ============================================================================
file(GLOB_RECURSE sources src/*.cc)
file(GLOB_RECURSE headers include/*.hh src/*.hh)
## Add the executable.
add_executable(hello ${sources})
target_sources(hello PRIVATE FILE_SET HEADERS FILES ${headers})
## Apply our options.
target_link_libraries(hello PRIVATE options libbase)