-
Notifications
You must be signed in to change notification settings - Fork 21
/
CMakeLists.txt
221 lines (192 loc) · 7.84 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
cmake_minimum_required(VERSION 3.8)
# Joins arguments and places the results in ${result_var}.
function(join result_var)
set(result )
foreach (arg ${ARGN})
set(result "${result}${arg}")
endforeach ()
set(${result_var} "${result}" PARENT_SCOPE)
endfunction()
# Sets a cache variable with a docstring joined from multiple arguments:
# set(<variable> <value>... CACHE <type> <docstring>...)
# This allows splitting a long docstring for readability.
function(set_verbose)
# cmake_parse_arguments is broken in CMake 3.4 (cannot parse CACHE) so use
# list instead.
list(GET ARGN 0 var)
list(REMOVE_AT ARGN 0)
list(GET ARGN 0 val)
list(REMOVE_AT ARGN 0)
list(REMOVE_AT ARGN 0)
list(GET ARGN 0 type)
list(REMOVE_AT ARGN 0)
join(doc ${ARGN})
set(${var} ${val} CACHE ${type} ${doc})
endfunction()
# Set the default CMAKE_BUILD_TYPE to Release.
# This should be done before the project command since the latter can set
# CMAKE_BUILD_TYPE itself (it does so for nmake).
if (NOT CMAKE_BUILD_TYPE)
set_verbose(CMAKE_BUILD_TYPE Release CACHE STRING
"Choose the type of build, options are: None(CMAKE_CXX_FLAGS or "
"CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.")
endif ()
project(FORMAT_BENCHMARKS)
set(CMAKE_MACOSX_RPATH ON)
set(CMAKE_CXX_STANDARD 17)
include(CheckCXXCompilerFlag)
if (NOT DEFINED INTEL)
set(_IS_INTEL_HOST OFF)
if (APPLE)
execute_process(COMMAND sysctl -n machdep.cpu.brand_string
OUTPUT_VARIABLE out)
if (out MATCHES "Intel.*")
set(_IS_INTEL_HOST ON)
endif ()
else ()
file(READ /proc/cpuinfo out)
if (out MATCHES "(.|\n)*GenuineIntel(.|\n)*")
set(_IS_INTEL_HOST ON)
endif ()
endif ()
option(INTEL "Enable Intel JCC bug mitigation." ${_IS_INTEL_HOST})
endif ()
function(check_flags_and_append_on_success test_flags_var_name append_to_var)
check_cxx_compiler_flag("${${test_flags_var_name}_FLAGS}"
${test_flags_var_name}_WORKS)
if (${test_flags_var_name}_WORKS)
list(APPEND ${append_to_var} ${${test_flags_var_name}_FLAGS})
set(${append_to_var} ${${append_to_var}} PARENT_SCOPE)
endif ()
endfunction()
# Workaround a JCC bug in Intel CPUs:
# https://www.intel.com/content/dam/support/us/en/documents/processors/
# mitigations-jump-conditional-code-erratum.pdf
# to get more reliable benchmark results.
#
# Ideally we should use -mbranches-within-32B-boundaries but it's not widely
# available so at least align loops/functions as fallback to prevent unrelated
# code changes from affecting benchmark results.
set(ALIGN_ALL_BLOCKS_FLAGS -mllvm -align-all-blocks=5)
set(ALIGN_32B_BOUNDARIES_FLAGS -Wa,-mbranches-within-32B-boundaries)
set(ALIGN_FUNCTION_FLAGS -falign-functions=32)
check_flags_and_append_on_success(ALIGN_32B_BOUNDARIES ALIGN_OPTIONS)
if (NOT ALIGN_32B_BOUNDARIES_WORKS)
check_flags_and_append_on_success(ALIGN_ALL_BLOCKS ALIGN_OPTIONS)
check_flags_and_append_on_success(ALIGN_FUNCTION ALIGN_OPTIONS)
endif ()
message(STATUS "Align options: ${ALIGN_OPTIONS}")
add_definitions(${ALIGN_OPTIONS})
# Use shared libraries to make comparison with IOStreams and printf
# fair as these use shared libraries too (libstdc++ and libc).
set(BUILD_SHARED_LIBS ON CACHE BOOL
"Build shared library instead of static one")
set(FMT_TEST TRUE CACHE BOOL "Enable fmt tests")
add_subdirectory(fmt)
find_package(Boost CONFIG)
set(FOLLY_DIR HINTS /Users/viz/homebrew/Cellar/folly/2022.11.14.00)
find_path(FOLLY_INCLUDE_DIR folly/Format.h HINTS ${FOLLY_DIR}/include)
find_library(FOLLY_LIB folly HINTS ${FOLLY_DIR}/lib)
if (FOLLY_INCLUDE_DIR AND FOLLY_LIB)
set(EXTRA_LIBS ${FOLLY_LIB})
endif ()
find_library(PROFILER_LIB profiler)
find_path(PROFILER_INCLUDE_DIR gperftools/profiler.h)
if (PROFILER_LIB AND PROFILER_INCLUDE_DIR)
include_directories(${PROFILER_INCLUDE_DIR})
set(HAVE_PROFILER TRUE)
endif ()
add_executable(tinyformat_speed_test src/tinyformat-test.cc)
target_link_libraries(tinyformat_speed_test fmt ${EXTRA_LIBS})
if (HAVE_PROFILER)
target_link_libraries(tinyformat_speed_test ${PROFILER_LIB})
set(PROFILE_DEFS ";FMT_PROFILE")
endif ()
if (TARGET Boost::boost)
target_link_libraries(tinyformat_speed_test Boost::boost)
endif ()
set_target_properties(tinyformat_speed_test PROPERTIES COMPILE_DEFINITIONS
"SPEED_TEST;HAVE_FORMAT;_SCL_SECURE_NO_WARNINGS;${PROFILE_DEFS}")
if (CPP11_FLAG)
set_target_properties(tinyformat_speed_test
PROPERTIES COMPILE_FLAGS ${CPP11_FLAG})
endif ()
if (WIN32)
add_custom_target(speed-test
COMMAND @echo running speed tests...
COMMAND cd ${CMAKE_CFG_INTDIR}
COMMAND @echo printf timings: start %time%
COMMAND .\\tinyformat_speed_test.exe printf >NUL
COMMAND @echo stop %time%
COMMAND @echo iostreams timings: start %time%
COMMAND .\\tinyformat_speed_test.exe iostreams >NUL
COMMAND @echo stop %time%
COMMAND @echo format timings: start %time%
COMMAND .\\tinyformat_speed_test.exe format >NUL
COMMAND @echo stop %time%
COMMAND @echo tinyformat timings: start %time%
COMMAND .\\tinyformat_speed_test.exe tinyformat >NUL
COMMAND @echo stop %time%
COMMAND @echo boost timings: start %time%
COMMAND .\\tinyformat_speed_test.exe boost >NUL
COMMAND @echo stop %time%
COMMAND @echo stb_sprintf timings: start %time%
COMMAND .\\tinyformat_speed_test.exe stb_sprintf >NUL
COMMAND @echo stop %time%
DEPENDS tinyformat_speed_test)
else()
add_custom_target(speed-test
COMMAND @echo running speed tests...
COMMAND @echo printf timings:
COMMAND @time -p ./tinyformat_speed_test printf > /dev/null
COMMAND @echo iostreams timings:
COMMAND @time -p ./tinyformat_speed_test iostreams > /dev/null
COMMAND @echo format timings:
COMMAND @time -p ./tinyformat_speed_test format > /dev/null
COMMAND @echo fmt::compile timings:
COMMAND @time -p ./tinyformat_speed_test fmt::compile > /dev/null
COMMAND @echo tinyformat timings:
COMMAND @time -p ./tinyformat_speed_test tinyformat > /dev/null
COMMAND @echo boost timings:
COMMAND @time -p ./tinyformat_speed_test boost > /dev/null
COMMAND @echo folly timings:
COMMAND @time -p ./tinyformat_speed_test folly > /dev/null
COMMAND @echo stb_sprintf timings:
COMMAND @time -p ./tinyformat_speed_test stb_sprintf > /dev/null
DEPENDS tinyformat_speed_test)
endif ()
add_custom_target(bloat-test
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/bloat-test.py
-I${Boost_INCLUDE_DIRS}
DEPENDS fmt)
add_custom_target(variadic-test
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/variadic-test.py
\${ARGS} -I${Boost_INCLUDE_DIRS}
DEPENDS fmt)
enable_testing()
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Disable benchmark tests.")
add_subdirectory(benchmark)
add_executable(digits10-benchmark src/digits10/digits10.cc
src/digits10/digits10.h src/digits10/digits10-benchmark.cc)
target_link_libraries(digits10-benchmark benchmark)
add_executable(digits10-test src/digits10/digits10.cc
src/digits10/digits10-test.cc)
target_link_libraries(digits10-test gtest benchmark)
add_test(digits10-test digits10-test)
add_executable(vararg-benchmark src/vararg-benchmark.cc)
target_link_libraries(vararg-benchmark benchmark fmt)
add_executable(int-benchmark src/int-benchmark.cc)
target_link_libraries(int-benchmark benchmark fmt)
if (TARGET Boost::boost)
target_link_libraries(int-benchmark Boost::boost)
endif ()
target_compile_features(int-benchmark PRIVATE cxx_relaxed_constexpr)
add_executable(locale-benchmark src/locale-benchmark.cc)
target_link_libraries(locale-benchmark benchmark fmt)
add_executable(concat-benchmark src/concat-benchmark.cc)
target_link_libraries(concat-benchmark benchmark fmt)
add_executable(file-benchmark src/file-benchmark.cc)
target_link_libraries(file-benchmark benchmark fmt)
add_executable(find-pow10-benchmark src/find-pow10-benchmark.cc)
target_link_libraries(find-pow10-benchmark benchmark)
add_subdirectory(src/itoa-benchmark)