-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
342 lines (302 loc) · 11.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
cmake_minimum_required(VERSION 3.2)
cmake_policy(SET CMP0003 NEW)
if(POLICY CMP0056) # Honor link flags in try_compile() source-file signature.
cmake_policy(SET CMP0056 NEW) # since 3.2
endif()
if(POLICY CMP0065)
cmake_policy(SET CMP0065 NEW) # since 3.4. ENABLE_EXPORTS.
endif()
if(POLICY CMP0066) # honor per-config flags in try_compile() source-file
# signature.
cmake_policy(SET CMP0066 NEW) # since 3.7
endif()
if(POLICY CMP0067) # honor C++ standard requirement in try_compile() source-file
# signature.
cmake_policy(SET CMP0067 NEW) # since 3.8
endif()
if(POLICY CMP0069) # Honor IPO setting for all supported compilers, not just icc
cmake_policy(SET CMP0069 NEW) # since 3.9
endif()
set(CMAKE_CXX_STANDARD 11)
set(CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# "C" needs to be enabled for some builtin CMake tests :-/
project(SMHasher3 C CXX)
include(CheckCCompilerFlag)
# Check if the same compile family is used for both C and CXX
if(NOT (CMAKE_C_COMPILER_ID STREQUAL CMAKE_CXX_COMPILER_ID))
message(WARNING "CMAKE_C_COMPILER_ID (${CMAKE_C_COMPILER_ID}) is different "
"from CMAKE_CXX_COMPILER_ID (${CMAKE_CXX_COMPILER_ID}). "
"The final binary may be unusable.")
endif()
########################################
# Compiler flags (optimization, debug, etc.)
########################################
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE
"Release"
CACHE STRING "Choose the type of build, options are: Release Debug Asan" FORCE)
endif(NOT CMAKE_BUILD_TYPE)
if(MSVC)
# /bigobj required because of wordlist in array.h
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Zc:__cplusplus -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -DNOMINMAX -bigobj")
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
# -MP allows to MSVC to build project with multiple processes - running multiple cl.exe in parallel
# clang does support this, the buildsystem (make/ninja) controls explicitly how compiler is launched
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -MP")
endif()
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
# using Visual Studio C++, already the default with VS17
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:SSE2")
endif()
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL GNU
OR (CMAKE_CXX_COMPILER_ID STREQUAL AppleClang)
OR (CMAKE_CXX_COMPILER_ID STREQUAL Clang)
OR (CMAKE_CXX_COMPILER_ID STREQUAL Intel))
# For clang-cl, CMAKE_CXX_COMPILER_ID is Clang but MSVC is set to true
if(NOT MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -ggdb3")
endif()
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-O1 -DNDEBUG")
set(CMAKE_CXX_FLAGS_ASAN "${CMAKE_CXX_FLAGS_DEBUG} -Og -ggdb3 \
-fsanitize=address,undefined -fno-optimize-sibling-calls \
-fsanitize-address-use-after-scope -fno-omit-frame-pointer")
if(NOT CMAKE_CROSSCOMPILING)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wvla -Wpedantic \
-Wno-unused-function -Wno-unknown-pragmas")
if(CMAKE_CXX_COMPILER_ID STREQUAL Clang)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-lambda-capture \
-Wno-dollar-in-identifier-extension")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL GNU)
set(ACTUAL_GCC TRUE)
# GCC versions before 9.0 don't handle _Pragma for ignoring -Wvla :-(
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-vla")
endif()
# For my development system
if(EXISTS "${CMAKE_SOURCE_DIR}/.devbox")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gdwarf-4 -fvar-tracking-assignments")
endif()
endif()
endif()
function(whole_archive_link_flags lib var)
if(MSVC)
set(${var} -WHOLEARCHIVE:$<TARGET_FILE:${lib}> PARENT_SCOPE)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(${var} -Wl,-force_load,$<TARGET_FILE:${lib}> PARENT_SCOPE)
else()
set(${var} -Wl,--whole-archive ${lib} -Wl,--no-whole-archive PARENT_SCOPE)
endif()
endfunction()
########################################
# Platform detection things
########################################
set(DETECT_DIR ${CMAKE_SOURCE_DIR}/platform)
set(CMAKE_REQUIRED_QUIET true)
set(AUTOGEN_WARNING
"/*\n\
* DO NOT EDIT THIS FILE\n\
*\n\
* It is generated by CMake during build configuration.\n\
* See ${DETECT_DIR}/functions.cmake\n\
* for documentation on how and why this is generated.\n\
*/"
)
include(${DETECT_DIR}/functions.cmake)
include(${DETECT_DIR}/family.cmake)
include(${DETECT_DIR}/intsize.cmake)
include(${DETECT_DIR}/endian.cmake)
include(${DETECT_DIR}/builtins.cmake)
include(${DETECT_DIR}/isa.cmake)
include(${DETECT_DIR}/threads.cmake)
include(${DETECT_DIR}/timing.cmake)
configure_file(${DETECT_DIR}/Timing.h.in ${CMAKE_BINARY_DIR}/include/Timing.h)
configure_file(${DETECT_DIR}/Platform.h.in ${CMAKE_BINARY_DIR}/include/Platform.h)
include_directories(${CMAKE_BINARY_DIR}/include)
########################################
# Specific source file requirements :-/
########################################
if(NOT MSVC)
set_source_files_properties(hashes/halftimehash.cpp PROPERTIES
COMPILE_FLAGS "-flax-vector-conversions -Wno-ignored-attributes")
if (CMAKE_CXX_COMPILER_ID STREQUAL Intel)
set_source_files_properties(hashes/floppsyhash.cpp PROPERTIES
COMPILE_FLAGS "-fp-model strict")
else()
set_source_files_properties(hashes/floppsyhash.cpp PROPERTIES
COMPILE_FLAGS "-ffp-contract=off")
endif()
endif(NOT MSVC)
########################################
# The list of hash source files
########################################
include(hashes/Hashsrc.cmake)
########################################
# Generate the list of USE_FAMILY() calls
########################################
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/Hashrefs.cpp
IMPLICIT_DEPENDS CXX ${CMAKE_SOURCE_DIR}/hashes/Hashrefs.cpp.in
DEPENDS ${HASH_SRC_FILES} hashes/Hashsrc.cmake
COMMAND ${CMAKE_COMMAND}
-D SRCDIR=${CMAKE_SOURCE_DIR}
-D SRC=${CMAKE_SOURCE_DIR}/hashes/Hashrefs.cpp.in
-D DST=${CMAKE_BINARY_DIR}/Hashrefs.cpp
-P ${CMAKE_SOURCE_DIR}/hashes/Hashrefs.cmake
)
########################################
# Build all the hashes
########################################
add_library(
SMHasher3Hashlib STATIC
#
${CMAKE_BINARY_DIR}/Hashrefs.cpp
#
lib/Hashlib.cpp
lib/Hashinfo.cpp
lib/AEStables.cpp
lib/AEStest.cpp
lib/Mathmult.cpp
#
${HASH_SRC_FILES}
)
target_include_directories(SMHasher3Hashlib PRIVATE include/hashlib PUBLIC include/common)
# Hashinfo.cpp has special dispensation to access util/ for VCode.h.
# Different CMake versions do this different ways
if(${CMAKE_VERSION} VERSION_LESS "3.11.0")
set_source_files_properties(lib/Hashinfo.cpp PROPERTIES COMPILE_FLAGS "-I${CMAKE_SOURCE_DIR}/util")
else()
set_source_files_properties(lib/Hashinfo.cpp PROPERTIES INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/util")
endif()
if(WIN32 AND CMAKE_CXX_COMPILER_ID STREQUAL Clang)
target_link_libraries(SMHasher3Hashlib PUBLIC clang_rt.builtins-x86_64.lib)
endif()
########################################
# Build all the tests
########################################
add_library(
SMHasher3Tests STATIC
#
util/Platform.cpp
util/Analyze.cpp
util/Blob.cpp
util/Blobsort.cpp
util/Reporting.cpp
util/Random.cpp
util/Stats.cpp
util/VCode.cpp
util/Wordlist.cpp
util/TestGlobals.cpp
#
tests/SanityTest.cpp
tests/AvalancheTest.cpp
tests/BitflipTest.cpp
tests/BitIndependenceTest.cpp
tests/HashMapTest.cpp
tests/SparseKeysetTest.cpp
tests/ZeroesKeysetTest.cpp
tests/CyclicKeysetTest.cpp
tests/TwoBytesKeysetTest.cpp
tests/TextKeysetTest.cpp
tests/PermutationKeysetTest.cpp
tests/SeedTest.cpp
tests/SeedZeroesTest.cpp
tests/SeedSparseTest.cpp
tests/SeedBitflipTest.cpp
tests/SeedBlockLenTest.cpp
tests/SeedBlockOffsetTest.cpp
tests/SeedAvalancheTest.cpp
tests/SeedBitIndependenceTest.cpp
tests/BadSeedsTest.cpp
tests/PerlinNoiseTest.cpp
tests/SpeedTest.cpp
)
target_include_directories(SMHasher3Tests PRIVATE util PUBLIC include/common)
add_executable(SMHasher3 main.cpp)
target_include_directories(SMHasher3 PUBLIC include/common PRIVATE util tests include/hashlib)
target_link_libraries(SMHasher3 SMHasher3Tests SMHasher3Hashlib ${threads_link_command})
########################################
# Miscellany
########################################
find_package(Git)
add_custom_target(SMHasher3Version ${CMAKE_COMMAND}
-D SRC=${CMAKE_SOURCE_DIR}/version.h.in
-D DST=${CMAKE_BINARY_DIR}/include/version.h
-D GIT_EXECUTABLE=${GIT_EXECUTABLE}
-P ${CMAKE_SOURCE_DIR}/version.cmake
)
add_dependencies(SMHasher3 SMHasher3Version)
add_executable(rngstream EXCLUDE_FROM_ALL misc/rngstream.cpp)
target_include_directories(rngstream PRIVATE util)
# IPO/LTO is a giant pain, and does not seem to increase performance at
# all (--extra runs 3s faster without it!). Disable it completely for now.
if(FALSE)
# Only do IPO/LTO if we are not on a dev branch
if((CMAKE_MAJOR_VERSION EQUAL 3 AND CMAKE_MINOR_VERSION GREATER_EQUAL 9)
AND (CMAKE_BUILD_TYPE STREQUAL "Release"))
include(CheckIPOSupported)
check_ipo_supported(RESULT ipo_supported OUTPUT error)
endif()
execute_process(
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE GIT_BRANCHISH
RESULT_VARIABLE GIT_BRANCHISH_ERROR_CODE
COMMAND ${GIT_EXECUTABLE} describe --contains --always --all HEAD
)
if(NOT GIT_BRANCHISH_ERROR_CODE)
string(FIND "${GIT_BRANCHISH}" "dev" offset)
if("${offset}" EQUAL 0)
set(ipo_supported FALSE)
set(error "disabled on dev branch")
endif()
endif()
if(ipo_supported)
message(STATUS "IPO / LTO enabled")
set_property(TARGET SMHasher3Tests PROPERTY INTERPROCEDURAL_OPTIMIZATION True)
set_property(TARGET SMHasher3 PROPERTY INTERPROCEDURAL_OPTIMIZATION True)
# Workaround CMake bugs 17781, 23136, and 23640 at the least :-/
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
if(CMAKE_CXX_COMPILE_OPTIONS_IPO MATCHES "-fno-fat-lto-objects")
message(STATUS " Enabling LTO fat objects")
string(REGEX REPLACE "-fno-fat-lto-objects" "-ffat-lto-objects" CMAKE_CXX_COMPILE_OPTIONS_IPO "${CMAKE_CXX_COMPILE_OPTIONS_IPO}")
endif()
# We get a warning forced on us even if we specify 1 explictly! Thanks, gcc!
if(CMAKE_CXX_COMPILE_OPTIONS_IPO MATCHES "-flto=")
message(STATUS " Forcing 2 LTO processes")
string(REGEX REPLACE "-flto=[a-zA-Z0-9]+" "-flto=2" CMAKE_CXX_COMPILE_OPTIONS_IPO "${CMAKE_CXX_COMPILE_OPTIONS_IPO}")
endif()
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if(CMAKE_CXX_COMPILE_OPTIONS_IPO MATCHES "-flto=")
message(STATUS "Forcing LTO with fat objects")
string(REGEX REPLACE "-flto=[a-zA-Z0-9]+" "-flto=full" CMAKE_CXX_COMPILE_OPTIONS_IPO "${CMAKE_CXX_COMPILE_OPTIONS_IPO}")
endif()
endif()
else()
message(STATUS "IPO / LTO not supported: <${error}>")
endif()
endif()
enable_testing()
add_test(VerifyAll SMHasher3 --test=VerifyAll)
add_test(InternalTests SMHasher3 --InternalTests)
add_test(Sanity SMHasher3 --test=Sanity)
add_test(Cyclic SMHasher3 --test=Cyclic)
add_test(Zeroes SMHasher3 --test=Zeroes)
if(MSVC)
add_custom_target(
TAGS
COMMAND dir /b /s *.cpp *.cc *.h | etags --language=c++ -
DEPENDS ${SRCS}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
else()
add_custom_target(
TAGS
COMMAND find . -name *.cpp -or -name *.cc -or -name *.h -print | etags --language=c++ -
DEPENDS ${SRCS}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
endif()