if(CMAKE_C_COMPILER_ID MATCHES "^Intel")
if(CMAKE_HOST_UNIX)
- set(WARNFLAGS "-Wall")
- set(WARNFLAGS_MAINTAINER "-Wall -Wcheck -Wremarks")
- set(WARNFLAGS_DISABLE "")
+ set(WARNFLAGS -Wall)
+ set(WARNFLAGS_MAINTAINER -Wall -Wcheck -Wremarks)
+ set(WARNFLAGS_DISABLE)
else()
- set(WARNFLAGS "/Wall")
- set(WARNFLAGS_MAINTAINER "/W5")
- set(WARNFLAGS_DISABLE "")
- endif()
+ set(WARNFLAGS /Wall)
+ set(WARNFLAGS_MAINTAINER /W5)
+ set(WARNFLAGS_DISABLE)
+ endif()
if(WITH_NATIVE_INSTRUCTIONS)
message(STATUS "Ignoring WITH_NATIVE_INSTRUCTIONS; not supported on this configuration")
set(WITH_NATIVE_INSTRUCTIONS OFF)
# (who'd use cmake from an IDE...) but checking for ICC before checking for MSVC should
# avoid mistakes.
# /Oi ?
- set(WARNFLAGS "/W3")
- set(WARNFLAGS_MAINTAINER "/W4")
- set(WARNFLAGS_DISABLE "")
+ set(WARNFLAGS /W3)
+ set(WARNFLAGS_MAINTAINER /W4)
+ set(WARNFLAGS_DISABLE)
if(BASEARCH_ARM_FOUND)
add_definitions(-D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE)
if(NOT "${ARCH}" MATCHES "aarch64")
endif()
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
# Enable warnings in GCC and Clang
- set(WARNFLAGS "-Wall")
- set(WARNFLAGS_MAINTAINER "-Wextra -Wpedantic")
- set(WARNFLAGS_DISABLE "-Wno-implicit-fallthrough")
+ set(WARNFLAGS -Wall)
+ set(WARNFLAGS_MAINTAINER -Wextra -Wpedantic)
+ set(WARNFLAGS_DISABLE -Wno-implicit-fallthrough)
if(WITH_NATIVE_INSTRUCTIONS)
if(BASEARCH_PPC_FOUND)
set(NATIVEFLAG "-mcpu=native")
if(BASEARCH_ARM_FOUND)
if("${ARCH}" MATCHES "arm" AND NOT CMAKE_C_FLAGS MATCHES "-mfloat-abi")
# Auto-detect support for ARM floating point ABI
- set(CMAKE_REQUIRED_FLAGS "-mfloat-abi=softfp")
+ set(CMAKE_REQUIRED_FLAGS -mfloat-abi=softfp)
check_c_source_compiles(
"#include <features.h>
int main() { return 0; }"
HAVE_FLOATABI_SOFTFP)
if(HAVE_FLOATABI_SOFTFP)
- set(FLOATABI "-mfloat-abi=softfp")
+ set(FLOATABI -mfloat-abi=softfp)
else()
- set(CMAKE_REQUIRED_FLAGS "-mfloat-abi=hard")
+ set(CMAKE_REQUIRED_FLAGS -mfloat-abi=hard)
check_c_source_compiles(
"#include <features.h>
int main() { return 0; }"
HAVE_FLOATABI_HARD)
if(HAVE_FLOATABI_HARD)
- set(FLOATABI "-mfloat-abi=hard")
+ set(FLOATABI -mfloat-abi=hard)
endif()
endif()
set(CMAKE_REQUIRED_FLAGS)
if(FLOATABI)
message(STATUS "ARM floating point arch: ${FLOATABI}")
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLOATABI}")
+ add_compile_options(${FLOATABI})
else()
message(STATUS "ARM floating point arch not auto-detected")
endif()
endif()
endif()
-# Replace optimization level 3 added by default with level 2
-if(NOT MSVC AND NOT CMAKE_C_FLAGS MATCHES "([\\/\\-]O)3")
- string(REGEX REPLACE "([\\/\\-]O)3" "\\12"
- CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
-endif()
-
# Set architecture alignment requirements
if(WITH_UNALIGNED)
if((BASEARCH_ARM_FOUND AND NOT "${ARCH}" MATCHES "armv[2-7]") OR (BASEARCH_PPC_FOUND AND "${ARCH}" MATCHES "powerpc64le") OR BASEARCH_X86_FOUND)
# Apply warning compiler flags
if(WITH_MAINTAINER_WARNINGS)
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNFLAGS} ${WARNFLAGS_MAINTAINER} ${WARNFLAGS_DISABLE}")
+ add_compile_options(${WARNFLAGS} ${WARNFLAGS_MAINTAINER} ${WARNFLAGS_DISABLE})
else()
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNFLAGS} ${WARNFLAGS_DISABLE}")
+ add_compile_options(${WARNFLAGS} ${WARNFLAGS_DISABLE})
endif()
# Set code coverage compiler flags
add_code_coverage()
endif()
+# Replace optimization level 3 added by default with level 2
+if(NOT WITH_CODE_COVERAGE AND NOT MSVC AND NOT CMAKE_C_FLAGS MATCHES "([\\/\\-]O)3")
+ string(REGEX REPLACE "([\\/\\-]O)3" "\\12"
+ CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
+endif()
+
# Set native instruction set compiler flag
if(WITH_NATIVE_INSTRUCTIONS AND DEFINED NATIVEFLAG)
# Apply flag to all source files and compilation checks
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${NATIVEFLAG}")
+ add_compile_options(${NATIVEFLAG})
endif()
#
endif()
endmacro()
-set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DZLIB_DEBUG")
+add_compile_options($<$<CONFIG:Debug>:-DZLIB_DEBUG>)
if(MSVC)
set(CMAKE_DEBUG_POSTFIX "d")
set(CMAKE_REQUIRED_LINK_OPTIONS)
if(HAVE_COVERAGE)
- set(CMAKE_C_FLAGS "-O0 ${CMAKE_C_FLAGS} -coverage")
+ add_compile_options(-coverage)
add_link_options(-coverage)
else()
# Some versions of GCC don't support -coverage shorthand
set(CMAKE_REQUIRED_LINK_OPTIONS)
if(HAVE_TEST_COVERAGE)
- set(CMAKE_C_FLAGS "-O0 ${CMAKE_C_FLAGS} -ftest-coverage -fprofile-arcs -fprofile-values")
+ add_compile_options(-ftest-coverage -fprofile-arcs -fprofile-values)
add_link_options(-lgcov -fprofile-arcs)
else()
message(WARNING "Compiler does not support code coverage")
+ set(WITH_CODE_COVERAGE OFF)
endif()
endif()
+
+ # Set optimization level to zero for code coverage builds
+ if (WITH_CODE_COVERAGE)
+ # Use CMake compiler flag variables due to add_compile_options failure on Windows GCC
+ set(CMAKE_C_FLAGS "-O0 ${CMAKE_C_FLAGS}")
+ set(CMAKE_CXX_FLAGS "-O0 ${CMAKE_CXX_FLAGS}")
+ endif()
endmacro()
check_sanitizer_support("${known_checks}" supported_checks)
if(NOT ${supported_checks} STREQUAL "")
message(STATUS "Address sanitizer is enabled: ${supported_checks}")
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=${supported_checks}")
+ add_compile_options(-fsanitize=${supported_checks})
add_link_options(-fsanitize=${supported_checks})
else()
message(STATUS "Address sanitizer is not supported")
check_sanitizer_support("leak" supported_checks)
if(NOT ${supported_checks} STREQUAL "")
message(STATUS "Leak sanitizer is enabled: ${supported_checks}")
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=${supported_checks}")
+ add_compile_options(-fsanitize=${supported_checks})
add_link_options(-fsanitize=${supported_checks})
else()
message(STATUS "Leak sanitizer is not supported")
check_sanitizer_support("memory" supported_checks)
if(NOT ${supported_checks} STREQUAL "")
message(STATUS "Memory sanitizer is enabled: ${supported_checks}")
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=${supported_checks}")
+ add_compile_options(-fsanitize=${supported_checks})
add_link_options(-fsanitize=${supported_checks})
else()
message(STATUS "Memory sanitizer is not supported")
check_sanitizer_support("thread" supported_checks)
if(NOT ${supported_checks} STREQUAL "")
message(STATUS "Thread sanitizer is enabled: ${supported_checks}")
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=${supported_checks}")
+ add_compile_options(-fsanitize=${supported_checks})
add_link_options(-fsanitize=${supported_checks})
else()
message(STATUS "Thread sanitizer is not supported")
if(NOT ${supported_checks} STREQUAL "")
message(STATUS "Undefined behavior sanitizer is enabled: ${supported_checks}")
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=${supported_checks}")
+ add_compile_options(-fsanitize=${supported_checks})
add_link_options(-fsanitize=${supported_checks})
# Group sanitizer flag -fsanitize=undefined will automatically add alignment, even if
# it is not in our sanitize flag list, so we need to explicitly disable alignment sanitizing.
if(UNALIGNED_OK)
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize=alignment")
+ add_compile_options(-fno-sanitize=alignment)
endif()
else()
message(STATUS "UNdefined behavior sanitizer is not supported")