Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit 7e9a135

Browse files
authored
Improve C++ flag handling in CMake (#128)
Signed-off-by: Isaac Hier <[email protected]>
1 parent eec4e01 commit 7e9a135

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

CMakeLists.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ HunterGate(
1818

1919
project(jaegertracing VERSION 0.5.0)
2020

21+
option(JAEGERTRACING_WARNINGS_AS_ERRORS "Treat compiler warnings as errors" OFF)
22+
2123
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
2224
CMAKE_CXX_COMPILER_ID MATCHES "Clang")
23-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -pedantic")
25+
set(cxx_flags -Wall -pedantic)
26+
if(JAEGERTRACING_WARNINGS_AS_ERRORS)
27+
list(APPEND cxx_flags -Werror)
28+
endif()
2429
endif()
2530

2631
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
27-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-private-field")
32+
list(APPEND cxx_flags -Wno-unused-private-field)
2833
endif()
2934

3035
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
@@ -91,7 +96,8 @@ if(BUILD_TESTING)
9196

9297
if(JAEGERTRACING_COVERAGE)
9398
include(CodeCoverage)
94-
append_coverage_compiler_flags()
99+
append_coverage_compiler_flags(cxx_flags)
100+
append_coverage_compiler_flags(link_flags)
95101
set(COVERAGE_EXCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/src/jaegertracing/thrift-gen/*"
96102
"*Test.cpp")
97103
endif()
@@ -200,7 +206,8 @@ function(add_lib_deps lib)
200206
target_include_directories(${lib} PUBLIC
201207
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
202208
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/src>)
203-
target_link_libraries(${lib} PUBLIC ${LIBS})
209+
target_link_libraries(${lib} PUBLIC ${link_flags} ${LIBS})
210+
target_compile_options(${lib} PUBLIC ${cxx_flags})
204211
endfunction()
205212

206213
option(JAEGERTRACING_PLUGIN "Build dynamic plugin" OFF)

cmake/CodeCoverage.cmake

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
8282
if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 3)
8383
message(FATAL_ERROR "Clang version must be 3.0.0 or greater! Aborting...")
8484
endif()
85-
set(COVERAGE_COMPILER_FLAGS "-Qunused-arguments")
85+
list(APPEND COVERAGE_COMPILER_FLAGS -Qunused-arguments)
8686
elseif(NOT CMAKE_COMPILER_IS_GNUCXX)
8787
message(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
8888
endif()
8989

90-
set(COVERAGE_COMPILER_FLAGS "${COVERAGE_COMPILER_FLAGS} -g -O0 --coverage -fprofile-arcs -ftest-coverage"
91-
CACHE INTERNAL "")
90+
list(APPEND COVERAGE_COMPILER_FLAGS
91+
-g -O0 --coverage -fprofile-arcs -ftest-coverage)
92+
set(COVERAGE_COMPILER_FLAGS ${COVERAGE_COMPILER_FLAGS} CACHE INTERNAL "")
9293

9394
set(CMAKE_CXX_FLAGS_COVERAGE
9495
${COVERAGE_COMPILER_FLAGS}
@@ -174,8 +175,6 @@ function(setup_target_for_coverage)
174175
)
175176
endfunction() # setup_target_for_coverage
176177

177-
function(append_coverage_compiler_flags)
178-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
179-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
180-
message(STATUS "Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}")
178+
function(append_coverage_compiler_flags flags_var)
179+
set(${flags_var} ${${flags_var}} ${COVERAGE_COMPILER_FLAGS} PARENT_SCOPE)
181180
endfunction() # append_coverage_compiler_flags

0 commit comments

Comments
 (0)