Skip to content

Commit 57c4fa8

Browse files
Use a more effective approach to handling GCC compatible flags in GoogleBenchmark
1 parent 18197ae commit 57c4fa8

File tree

2 files changed

+54
-38
lines changed

2 files changed

+54
-38
lines changed

third-party/benchmark/CMakeLists.txt

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,6 @@ endif()
147147
set(CMAKE_CXX_STANDARD ${BENCHMARK_CXX_STANDARD})
148148
set(CMAKE_CXX_STANDARD_REQUIRED YES)
149149
set(CMAKE_CXX_EXTENSIONS OFF)
150-
option(BENCHMARK_ENABLE_EXPENSIVE_CMAKE_CHECKS "Enable use of expensive CMake checks for unused source files" ON)
151-
152-
macro(handle_flag flag)
153-
if(LLVM_COMPILER_IS_GCC_COMPATIBLE AND NOT BENCHMARK_ENABLE_EXPENSIVE_CMAKE_CHECKS)
154-
list(APPEND CMAKE_CXX_FLAGS ${flag})
155-
else()
156-
add_cxx_compiler_flag(${flag})
157-
endif()
158-
endmacro()
159150

160151
if (MSVC)
161152
# Turn compiler warnings up to 11
@@ -194,49 +185,49 @@ else()
194185
add_definitions(-D_LARGEFILE64_SOURCE)
195186
add_definitions(-D_LARGEFILE_SOURCE)
196187
# Turn compiler warnings up to 11
197-
handle_flag(-Wall)
198-
handle_flag(-Wextra)
199-
handle_flag(-Wshadow)
200-
handle_flag(-Wfloat-equal)
201-
handle_flag(-Wold-style-cast)
188+
add_cxx_compiler_flag(-Wall)
189+
add_cxx_compiler_flag(-Wextra)
190+
add_cxx_compiler_flag(-Wshadow)
191+
add_cxx_compiler_flag(-Wfloat-equal)
192+
add_cxx_compiler_flag(-Wold-style-cast)
202193
if(BENCHMARK_ENABLE_WERROR)
203-
handle_flag(-Werror)
194+
add_cxx_compiler_flag(-Werror)
204195
endif()
205196
if (NOT BENCHMARK_ENABLE_TESTING)
206197
# Disable warning when compiling tests as gtest does not use 'override'.
207-
handle_flag(-Wsuggest-override)
198+
add_cxx_compiler_flag(-Wsuggest-override)
208199
endif()
209-
handle_flag(-pedantic)
210-
handle_flag(-pedantic-errors)
211-
handle_flag(-Wshorten-64-to-32)
212-
handle_flag(-fstrict-aliasing)
200+
add_cxx_compiler_flag(-pedantic)
201+
add_cxx_compiler_flag(-pedantic-errors)
202+
add_cxx_compiler_flag(-Wshorten-64-to-32)
203+
add_cxx_compiler_flag(-fstrict-aliasing)
213204
# Disable warnings regarding deprecated parts of the library while building
214205
# and testing those parts of the library.
215-
handle_flag(-Wno-deprecated-declarations)
206+
add_cxx_compiler_flag(-Wno-deprecated-declarations)
216207
if (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" OR CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
217208
# Intel silently ignores '-Wno-deprecated-declarations',
218209
# warning no. 1786 must be explicitly disabled.
219210
# See #631 for rationale.
220-
handle_flag(-wd1786)
221-
handle_flag(-fno-finite-math-only)
211+
add_cxx_compiler_flag(-wd1786)
212+
add_cxx_compiler_flag(-fno-finite-math-only)
222213
endif()
223214
# Disable deprecation warnings for release builds (when -Werror is enabled).
224215
if(BENCHMARK_ENABLE_WERROR)
225-
handle_flag(-Wno-deprecated)
216+
add_cxx_compiler_flag(-Wno-deprecated)
226217
endif()
227218
if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
228-
handle_flag(-fno-exceptions)
219+
add_cxx_compiler_flag(-fno-exceptions)
229220
endif()
230221

231222
if (HAVE_CXX_FLAG_FSTRICT_ALIASING)
232223
if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel" AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM") #ICC17u2: Many false positives for Wstrict-aliasing
233-
handle_flag(-Wstrict-aliasing)
224+
add_cxx_compiler_flag(-Wstrict-aliasing)
234225
endif()
235226
endif()
236227
# ICC17u2: overloaded virtual function "benchmark::Fixture::SetUp" is only partially overridden
237228
# (because of deprecated overload)
238-
handle_flag(-wd654)
239-
handle_flag(-Wthread-safety)
229+
add_cxx_compiler_flag(-wd654)
230+
add_cxx_compiler_flag(-Wthread-safety)
240231
if (HAVE_CXX_FLAG_WTHREAD_SAFETY)
241232
cxx_feature_check(THREAD_SAFETY_ATTRIBUTES "-DINCLUDE_DIRECTORIES=${PROJECT_SOURCE_DIR}/include")
242233
endif()
@@ -255,8 +246,8 @@ else()
255246

256247
# Link time optimisation
257248
if (BENCHMARK_ENABLE_LTO)
258-
handle_flag(-flto)
259-
handle_flag(-Wno-lto-type-mismatch)
249+
add_cxx_compiler_flag(-flto)
250+
add_cxx_compiler_flag(-Wno-lto-type-mismatch)
260251
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
261252
find_program(GCC_AR gcc-ar)
262253
if (GCC_AR)
@@ -287,7 +278,7 @@ else()
287278
BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE)
288279
set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
289280
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
290-
handle_flag(--coverage COVERAGE)
281+
add_cxx_compiler_flag(--coverage COVERAGE)
291282
endif()
292283

293284
if (BENCHMARK_USE_LIBCXX)

third-party/benchmark/cmake/AddCXXCompilerFlag.cmake

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,45 @@ function(mangle_compiler_flag FLAG OUTPUT)
2727
set(${OUTPUT} "${SANITIZED_FLAG}" PARENT_SCOPE)
2828
endfunction(mangle_compiler_flag)
2929

30+
set(BENCHMARK_GCC_COMPATIBLE_FLAGS
31+
-fno-exceptions
32+
-fstrict-aliasing
33+
-pedantic
34+
-pedantic-errors
35+
-Wall
36+
-Werror
37+
-Wextra
38+
-Wfloat-equal
39+
-Wno-deprecated
40+
-Wno-deprecated-declarations
41+
-Wold-style-cast
42+
-Wshadow
43+
-Wstrict-aliasing
44+
-Wsuggest-override
45+
)
46+
47+
macro(_benchmark_populate_cxx_compiler_flag)
48+
if(ARGC GREATER 1)
49+
set(VARIANT ${ARGV1})
50+
string(TOUPPER "_${VARIANT}" VARIANT)
51+
else()
52+
set(VARIANT "")
53+
endif()
54+
set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${BENCHMARK_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE)
55+
endmacro()
56+
3057
function(add_cxx_compiler_flag FLAG)
3158
mangle_compiler_flag("${FLAG}" MANGLED_FLAG)
59+
if(LLVM_COMPILER_IS_GCC_COMPATIBLE AND "${FLAG}" IN_LIST BENCHMARK_GCC_COMPATIBLE_FLAGS)
60+
_benchmark_populate_cxx_compiler_flag(${FLAG} ${VARIANT})
61+
return()
62+
endif()
3263
set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
3364
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}")
3465
check_cxx_compiler_flag("${FLAG}" ${MANGLED_FLAG})
3566
set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}")
3667
if(${MANGLED_FLAG})
37-
if(ARGC GREATER 1)
38-
set(VARIANT ${ARGV1})
39-
string(TOUPPER "_${VARIANT}" VARIANT)
40-
else()
41-
set(VARIANT "")
42-
endif()
43-
set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${BENCHMARK_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE)
68+
_benchmark_populate_cxx_compiler_flag(${FLAG} ${VARIANT})
4469
endif()
4570
endfunction()
4671

0 commit comments

Comments
 (0)