]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
cmake: prefer `COMPILE_OPTIONS` over `CMAKE_C_FLAGS` for custom C options
authorViktor Szakats <commit@vsz.me>
Mon, 14 Apr 2025 07:50:30 +0000 (09:50 +0200)
committerViktor Szakats <commit@vsz.me>
Mon, 14 Apr 2025 19:31:39 +0000 (21:31 +0200)
Also:
- pass `-D_GNU_SOURCE` via `COMPILE_DEFINITIONS`.
- make it explicit to pass these C flags to feature checks.
- update `_GNU_SOURCE` comment with `pipe2()`.
- enable `-pedantic-errors` picky option for GCC with CMake <3.23.
- drop redundant condition when stripping existing MSVC `/Wn` options.

CMake passes `CMAKE_C_FLAGS` to targets, feature checks and raw
`try_compile()` calls. With `COMPILE_OPTIONS`, this is limited to
targets, and we must explicitly pass them to feature checks. This
makes the build logic clearer, and offers more control. It also
reduces log noise by omitting these options from linker commands,
and from `CMAKE_C_FLAGS` dumps in feature checks.

Closes #17047

CMake/Macros.cmake
CMake/PickyWarnings.cmake
CMakeLists.txt

index fcc8f53e91012adc6a0bf21d7c89de76f0831dcf..14b82c309eb9f3aec710291b1e0ff14cf163b62c 100644 (file)
@@ -40,6 +40,7 @@ set(CURL_TEST_DEFINES "")  # Initialize global variable
 # Return result in variable: CURL_TEST_OUTPUT
 macro(curl_internal_test _curl_test)
   if(NOT DEFINED "${_curl_test}")
+    string(REPLACE ";" " " _cmake_required_flags "${CMAKE_REQUIRED_FLAGS}")
     string(REPLACE ";" " " _cmake_required_definitions "${CMAKE_REQUIRED_DEFINITIONS}")
     set(_curl_test_add_libraries "")
     if(CMAKE_REQUIRED_LIBRARIES)
@@ -52,7 +53,7 @@ macro(curl_internal_test _curl_test)
       ${PROJECT_BINARY_DIR}
       "${CMAKE_CURRENT_SOURCE_DIR}/CMake/CurlTests.c"
       CMAKE_FLAGS
-        "-DCOMPILE_DEFINITIONS:STRING=-D${_curl_test} ${CURL_TEST_DEFINES} ${_cmake_required_definitions}"
+        "-DCOMPILE_DEFINITIONS:STRING=-D${_curl_test} ${CURL_TEST_DEFINES} ${_cmake_required_flags} ${_cmake_required_definitions}"
         "${_curl_test_add_libraries}"
       OUTPUT_VARIABLE CURL_TEST_OUTPUT)
     if(${_curl_test})
index a41b71ef89976b6df2a1ff40b6aa4e8dc718de7b..e2af24e4e5ecd31478ac90f30e5b348f2535f467 100644 (file)
@@ -28,8 +28,7 @@ set(_picky "")
 if(CURL_WERROR AND
    ((CMAKE_C_COMPILER_ID STREQUAL "GNU" AND
      NOT DOS AND  # Watt-32 headers use the '#include_next' GCC extension
-     CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 5.0 AND
-     CMAKE_VERSION VERSION_GREATER_EQUAL 3.23.0) OR  # to avoid check_symbol_exists() conflicting with GCC -pedantic-errors
+     CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 5.0) OR
    CMAKE_C_COMPILER_ID MATCHES "Clang"))
   list(APPEND _picky "-pedantic-errors")
 endif()
@@ -45,9 +44,7 @@ if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
 endif()
 
 if(MSVC)
-  if(CMAKE_C_FLAGS MATCHES "[/-]W[0-4]")
-    string(REGEX REPLACE "[/-]W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
-  endif()
+  string(REGEX REPLACE "[/-]W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
   list(APPEND _picky "-W4")
 elseif(BORLAND)
   list(APPEND _picky "-w-")  # Disable warnings on Borland to avoid changing 3rd party code.
@@ -294,7 +291,15 @@ if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND MSVC)
 endif()
 
 if(_picky)
-  string(REPLACE ";" " " _picky "${_picky}")
-  string(APPEND CMAKE_C_FLAGS " ${_picky}")
-  message(STATUS "Picky compiler options: ${_picky}")
+  string(REPLACE ";" " " _picky_tmp "${_picky}")
+  message(STATUS "Picky compiler options: ${_picky_tmp}")
+  set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS "${_picky}")
+
+  # Apply to all feature checks
+  list(REMOVE_ITEM _picky "-pedantic-errors")  # Must not pass to feature checks
+  string(REPLACE ";" " " _picky_tmp "${_picky}")
+  list(APPEND CMAKE_REQUIRED_FLAGS "${_picky_tmp}")
+
+  unset(_picky)
+  unset(_picky_tmp)
 endif()
index df23fab2405054c21e91011d42dacfcd50d345f0..c25d823ef950be704a75d41a94f677c78a181669 100644 (file)
@@ -282,7 +282,8 @@ endif()
 include(PickyWarnings)
 
 if(CYGWIN OR CMAKE_SYSTEM_NAME STREQUAL "Linux")
-  string(APPEND CMAKE_C_FLAGS " -D_GNU_SOURCE")  # Required for sendmmsg() and accept4()
+  set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS "_GNU_SOURCE")  # Required for accept4(), pipe2(), sendmmsg()
+  list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")  # Apply to all feature checks
 endif()
 
 option(ENABLE_DEBUG "Enable curl debug features (for developing curl itself)" OFF)
@@ -360,8 +361,8 @@ if(WIN32)
   if(CURL_STATIC_CRT AND MSVC)
     if(MSVC_VERSION GREATER_EQUAL 1900 OR BUILD_STATIC_CURL OR NOT BUILD_CURL_EXE)
       set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
-      string(APPEND CMAKE_C_FLAGS_RELEASE " -MT")
-      string(APPEND CMAKE_C_FLAGS_DEBUG " -MTd")
+      set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS "$<$<CONFIG:Release>:-MT>")
+      set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS "$<$<CONFIG:Debug>:-MTd>")
     else()
       message(WARNING "Static CRT requires UCRT, static libcurl or no curl executable.")
     endif()
@@ -2005,14 +2006,14 @@ if(WIN32)
 endif()
 
 if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")  # MSVC but exclude clang-cl
-  string(APPEND CMAKE_C_FLAGS " -MP")  # Parallel compilation
+  set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS "-MP")  # Parallel compilation
 endif()
 
 if(CURL_WERROR)
   if(MSVC)
-    string(APPEND CMAKE_C_FLAGS " -WX")
+    set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS "-WX")
   else()
-    string(APPEND CMAKE_C_FLAGS " -Werror")  # This assumes clang or gcc style options
+    set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS "-Werror")  # This assumes clang or gcc style options
   endif()
 endif()