-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCMakeLists.txt
226 lines (198 loc) · 7.1 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
cmake_minimum_required(VERSION 3.16)
project(fzx VERSION 0.1.0 LANGUAGES CXX)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "fzx: Using default build type - RelWithDebInfo")
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Build type" FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"Release" "RelWithDebInfo" "MinSizeRel" "Debug")
endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(FzxGitSubmodule)
option(FZX_BUILD_EXECUTABLE "Build terminal application" OFF)
option(FZX_BUILD_LUA_MODULE "Build Lua module" ON)
option(FZX_BUILD_TESTS "Build tests" OFF)
option(FZX_BUILD_BENCHMARKS "Build benchmarks" OFF)
option(FZX_BUNDLE_DEPENDENCIES "Use bundled dependencies" ON)
option(FZX_BUNDLE_FMT "Use bundled dependency - fmt" ${FZX_BUNDLE_DEPENDENCIES})
option(FZX_BUNDLE_LUA "Use bundled dependency - Lua 5.1" ${FZX_BUNDLE_DEPENDENCIES})
option(FZX_BUNDLE_CATCH2 "Use bundled dependency - Catch2" ${FZX_BUNDLE_DEPENDENCIES})
option(FZX_BUNDLE_BENCHMARK "Use bundled dependency - benchmark" ${FZX_BUNDLE_DEPENDENCIES})
find_package(Threads REQUIRED)
if(FZX_BUILD_LUA_MODULE)
if(FZX_BUNDLE_LUA)
set(LUA_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/deps/lua/include")
else()
find_package(Lua 5.1 EXACT REQUIRED)
endif()
endif()
if(FZX_BUILD_EXECUTABLE)
if(FZX_BUNDLE_FMT)
fzx_git_submodule(deps/fmt)
add_subdirectory(deps/fmt)
else()
find_package(fmt REQUIRED)
endif()
endif()
if(FZX_BUILD_TESTS)
if(FZX_BUNDLE_CATCH2)
fzx_git_submodule(deps/Catch2)
add_subdirectory(deps/Catch2)
else()
find_package(Catch2 3 REQUIRED)
endif()
endif()
if(FZX_BUILD_BENCHMARKS)
if(FZX_BUNDLE_BENCHMARK)
fzx_git_submodule(deps/benchmark)
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "")
set(BENCHMARK_ENABLE_WERROR OFF CACHE BOOL "")
add_subdirectory(deps/benchmark)
else()
find_package(benchmark REQUIRED)
endif()
endif()
option(FZX_ENABLE_NATIVE "Optimize for the current architecture" ON)
option(FZX_ENABLE_LTO "Enable link time optimizations" ON)
option(FZX_ENABLE_SSE2 "Enable SSE2 (x86_64)" ON)
option(FZX_ENABLE_SSE41 "Enable SSE4.1 (x86_64)" ON)
option(FZX_ENABLE_AVX "Enable AVX (x86_64)" ON)
option(FZX_ENABLE_AVX2 "Enable AVX2 (x86_64)" ON)
option(FZX_ENABLE_NEON "Enable NEON (arm64)" OFF)
option(FZX_ENABLE_UBSAN "Enable undefined behavior sanitizer" OFF)
option(FZX_ENABLE_ASAN "Enable address sanitizer" OFF)
option(FZX_ENABLE_TSAN "Enable thread sanitizer" OFF)
if(FZX_ENABLE_ASAN AND FZX_ENABLE_TSAN)
message(FATAL_ERROR "fzx: ASAN and TSAN are not compatible")
endif()
option(FZX_ENABLE_COVERAGE "Enable code coverage" OFF)
option(FZX_ENABLE_CLANG_TIDY "Enable clang-tidy" OFF)
option(FZX_SAVE_OPTIMIZATION_RECORDS "Save Clang optimization records" OFF)
if(FZX_ENABLE_LTO)
include(CheckIPOSupported)
check_ipo_supported(RESULT lto)
if(lto)
message(STATUS "fzx: Link time optimizations enabled")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO ON)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL ON)
endif()
endif()
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
if(FZX_ENABLE_CLANG_TIDY)
find_program(CLANG_TIDY clang-tidy)
if(CLANG_TIDY)
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY}")
endif()
endif()
add_library(fzxopts INTERFACE)
if(FZX_ENABLE_UBSAN OR FZX_ENABLE_ASAN OR FZX_ENABLE_TSAN)
target_compile_options(fzxopts INTERFACE -fno-omit-frame-pointer -fno-optimize-sibling-calls)
endif()
if(FZX_ENABLE_UBSAN)
target_compile_options(fzxopts INTERFACE -fsanitize=undefined)
target_link_options(fzxopts INTERFACE -fsanitize=undefined)
endif()
if(FZX_ENABLE_ASAN)
target_compile_options(fzxopts INTERFACE -fsanitize=address)
target_link_options(fzxopts INTERFACE -fsanitize=address)
endif()
if(FZX_ENABLE_TSAN)
target_compile_options(fzxopts INTERFACE -fsanitize=thread)
target_link_options(fzxopts INTERFACE -fsanitize=thread)
endif()
if(NOT MSVC)
target_compile_options(fzxopts INTERFACE -Wall -Wextra)
endif()
set(compile_options
-Werror=unused-result
-Werror=strict-aliasing
-Werror=pointer-arith
-Werror=char-subscripts
-Werror=vla)
include(CheckCXXCompilerFlag)
foreach(option ${compile_options})
string(TOUPPER "FZX_HAS${option}" option_name)
string(REGEX REPLACE "[=-]" "_" option_name "${option_name}")
check_cxx_compiler_flag("${option}" ${option_name})
if(${option_name})
target_compile_options(fzxopts INTERFACE $<$<CONFIG:Debug>:${option}>)
endif()
endforeach()
if(FZX_ENABLE_COVERAGE)
target_compile_options(fzxopts INTERFACE --coverage)
target_link_options(fzxopts INTERFACE --coverage)
add_custom_target(coverage
COMMAND cmake -E make_directory cov
COMMAND gcovr
--html-details cov/cov.html
--exclude-unreachable-branches
--gcov-ignore-parse-errors
--root "${CMAKE_CURRENT_SOURCE_DIR}"
--filter "${CMAKE_CURRENT_SOURCE_DIR}/src/.*"
.)
add_custom_target(coverage-clean
COMMAND gcovr --delete
COMMAND cmake -E rm -rf -- cov)
endif()
if(FZX_SAVE_OPTIMIZATION_RECORDS)
if(NOT "${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
message(FATAL_ERROR "fzx: FZX_SAVE_OPTIMIZATION_RECORDS is supported only on Clang")
endif()
target_compile_options(fzxopts INTERFACE -fsave-optimization-record=yaml)
endif()
include(FzxCPUFeatures)
if(FZX_ENABLE_NATIVE AND NOT MSVC)
target_compile_options(fzxopts INTERFACE -march=native)
endif()
if(FZX_ENABLE_SSE2 AND FZX_HAS_SSE2)
message(STATUS "fzx: SSE2 enabled")
if(NOT MSVC)
target_compile_options(fzxopts INTERFACE -msse2)
endif()
target_compile_definitions(fzxopts INTERFACE FZX_SSE2)
endif()
if(FZX_ENABLE_SSE41 AND FZX_HAS_SSE41)
message(STATUS "fzx: SSE4.1 enabled")
if(NOT MSVC)
target_compile_options(fzxopts INTERFACE -msse4.1)
endif()
target_compile_definitions(fzxopts INTERFACE FZX_SSE41)
endif()
if(FZX_ENABLE_AVX AND FZX_HAS_AVX)
message(STATUS "fzx: AVX enabled")
if(NOT MSVC)
target_compile_options(fzxopts INTERFACE -mavx)
else()
target_compile_options(fzxopts INTERFACE /arch:AVX)
endif()
target_compile_definitions(fzxopts INTERFACE FZX_AVX)
endif()
if(FZX_ENABLE_AVX2 AND FZX_HAS_AVX2)
message(STATUS "fzx: AVX2 enabled")
if(NOT MSVC)
target_compile_options(fzxopts INTERFACE -mavx2)
else()
target_compile_options(fzxopts INTERFACE /arch:AVX2)
endif()
target_compile_definitions(fzxopts INTERFACE FZX_AVX2)
endif()
if(FZX_ENABLE_NEON)
# TODO: detect
message(STATUS "fzx: NEON enabled")
target_compile_definitions(fzxopts INTERFACE FZX_NEON)
endif()
target_compile_definitions(fzxopts INTERFACE
# Disable DEBUG_ASSERT and enable ASSUME in release builds
$<$<CONFIG:Release,RelWithDebInfo,MinSizeRel>:FZX_OPTIMIZE>)
add_subdirectory(src/fzx)
if(FZX_BUILD_TESTS)
add_subdirectory(test)
endif()
if(FZX_BUILD_BENCHMARKS)
add_subdirectory(benchmark)
endif()