diff --git a/CMakeLists.txt b/CMakeLists.txt index da6c8e5bb9f..26b632ac8ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -272,7 +272,8 @@ if(UA_ENABLE_CLANG_COV) if(UA_ENABLE_COVERAGE) message(FATAL_ERROR "Only either clang cov or normal coverage is allowed.") endif() - add_compile_options(-fprofile-instr-generate -fcoverage-mapping) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-instr-generate -fcoverage-mapping") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping") endif() if(UA_ENABLE_DISCOVERY_MULTICAST AND NOT UA_ENABLE_DISCOVERY) @@ -550,12 +551,12 @@ endif() # Taken from https://stackoverflow.com/a/33266748 include(CheckCCompilerFlag) function(check_add_cc_flag CC_FLAG) - list(FIND COMPILE_OPTIONS ${CC_FLAG} flag_already_set) + string(FIND "${CMAKE_C_FLAGS}" "${CC_FLAG}" flag_already_set) if(flag_already_set EQUAL -1) message(STATUS "Test CC flag ${CC_FLAG}") - check_c_compiler_flag(${CC_FLAG} flag_supported) + check_c_compiler_flag("${CC_FLAG}" flag_supported) if(flag_supported) - add_compile_options(${CC_FLAG}) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CC_FLAG}" PARENT_SCOPE) endif() unset(flag_supported CACHE) endif() @@ -664,7 +665,8 @@ if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID STREQUAL "Clang") if(CMAKE_CXX_COMPILER_VERSION AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10.0) set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -fsanitize-coverage=trace-pc-guard") endif() - add_compile_options(${SANITIZER_FLAGS}) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SANITIZER_FLAGS}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SANITIZER_FLAGS}") endif() if(NOT MINGW AND UA_ENABLE_HARDENING AND ((CMAKE_BUILD_TYPE STREQUAL "Release") OR (CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo"))) @@ -683,25 +685,28 @@ if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID STREQUAL "Clang") # remove stack-protector with MinSizeRel if(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel") check_add_cc_flag("-fno-stack-protector") - if(NOT WIN32 AND NOT CYGWIN AND NOT APPLE) - # these settings reduce the binary size by ~2kb - add_link_options(-Wl,-z,norelro -Wl,--hash-style=gnu -Wl,--build-id=none) - endif() endif() if(NOT OS9) - add_link_options(-s) + set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -s") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s") endif() if(APPLE) - add_link_options(-Wl,-dead_strip) + set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-dead_strip") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-dead_strip") else() - add_link_options(-Wl,--gc-sections) + set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--gc-sections") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections") + endif() + if(NOT WIN32 AND NOT CYGWIN AND NOT APPLE) + # these settings reduce the binary size by ~2kb + set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-z,norelro -Wl,--hash-style=gnu -Wl,--build-id=none") endif() endif() endif() if(APPLE) set(CMAKE_MACOSX_RPATH 1) - add_compile_options(-D_DARWIN_C_SOURCE=1) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DARWIN_C_SOURCE=1") endif() if(MSVC)