cmake: prefer COMPILE_OPTIONS over CMAKE_C_FLAGS for custom C options

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
This commit is contained in:
Viktor Szakats
2025-04-14 09:50:30 +02:00
parent d9ca7ad5cb
commit e86542038d
3 changed files with 22 additions and 15 deletions

View 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})

View 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()

View 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()