]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Use add_compile_options in cmake which sets both C and CXX flags.
authorNathan Moinvaziri <nathan@nathanm.com>
Wed, 26 Jan 2022 01:56:12 +0000 (17:56 -0800)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Sat, 29 Jan 2022 15:44:21 +0000 (16:44 +0100)
It is necessary to break apart strings that contain multiple flags so they are processed by add_compile_options properly.

CMakeLists.txt
cmake/detect-coverage.cmake
cmake/detect-sanitizer.cmake

index 72d43416553d597df6a21127978437da793c998e..b9de19a7552657c26fd5929cda43d354b9b4a4a7 100644 (file)
@@ -153,14 +153,14 @@ endif()
 
 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)
@@ -175,9 +175,9 @@ elseif(MSVC)
     # (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")
@@ -190,9 +190,9 @@ elseif(MSVC)
     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")
@@ -203,27 +203,27 @@ elseif(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
         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()
@@ -246,12 +246,6 @@ else()
     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)
@@ -294,9 +288,9 @@ endif()
 
 # 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
@@ -304,10 +298,16 @@ if(WITH_CODE_COVERAGE)
     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()
 
 #
@@ -465,7 +465,7 @@ macro(check_c_source_compile_or_run source flag)
     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")
index ba5e8af948a209ee006d708a10ce0c24568c0c6f..8688716c44e970739c5dc103c9b06654115834e8 100644 (file)
@@ -13,7 +13,7 @@ macro(add_code_coverage)
     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
@@ -27,10 +27,18 @@ macro(add_code_coverage)
         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()
index 93b437454de92e09ffee25780362e4037dd4a224..344427c44fdf6be703ad7112e8b5b633cc391ba6 100644 (file)
@@ -39,7 +39,7 @@ macro(add_address_sanitizer)
     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")
@@ -53,7 +53,7 @@ macro(add_address_sanitizer)
         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")
@@ -65,7 +65,7 @@ macro(add_memory_sanitizer)
     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")
@@ -76,7 +76,7 @@ macro(add_thread_sanitizer)
     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")
@@ -124,13 +124,13 @@ macro(add_undefined_sanitizer)
 
     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")