cmake: misc tidy-ups

- replace `add_compile_options()`,  `add_definitions()` with directory
  properties. To harmonize this across all scripts. The new commands are
  verbose, but describe better how they work. The syntax is also closer
  to setting target properties, helps grepping.

- prefer `CMAKE_INSTALL_PREFIX` over `--prefix` (in tests, CI).

- tidy up cmake invocations.

- formatting.

Closes #16238
This commit is contained in:
Viktor Szakats 2025-02-07 13:44:39 +01:00
parent ca2f49ded0
commit 45f7cb7695
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201
8 changed files with 15 additions and 14 deletions

View File

@ -589,6 +589,7 @@ jobs:
fi fi
if [ -n '${{ matrix.build.generate }}' ]; then if [ -n '${{ matrix.build.generate }}' ]; then
cmake -B . -G Ninja \ cmake -B . -G Ninja \
-DCMAKE_INSTALL_PREFIX="$HOME/curl" \
-DCMAKE_C_COMPILER_TARGET=$(uname -m)-pc-linux-gnu -DBUILD_STATIC_LIBS=ON \ -DCMAKE_C_COMPILER_TARGET=$(uname -m)-pc-linux-gnu -DBUILD_STATIC_LIBS=ON \
-DCMAKE_UNITY_BUILD=ON -DCURL_TEST_BUNDLES=ON -DCURL_WERROR=ON \ -DCMAKE_UNITY_BUILD=ON -DCURL_TEST_BUNDLES=ON -DCURL_WERROR=ON \
${{ matrix.build.generate }} ${{ matrix.build.generate }}
@ -637,7 +638,7 @@ jobs:
- name: 'cmake install' - name: 'cmake install'
if: ${{ matrix.build.generate }} if: ${{ matrix.build.generate }}
run: cmake --install . --prefix $HOME/curl --strip run: cmake --install . --strip
- name: 'build tests' - name: 'build tests'
if: ${{ matrix.build.install_steps != 'skipall' }} if: ${{ matrix.build.install_steps != 'skipall' }}

View File

@ -311,7 +311,7 @@ jobs:
https://github.com/libressl/portable/releases/download/v${{ env.libressl-version }}/libressl-${{ env.libressl-version }}.tar.gz | tar -x https://github.com/libressl/portable/releases/download/v${{ env.libressl-version }}/libressl-${{ env.libressl-version }}.tar.gz | tar -x
cd libressl-${{ env.libressl-version }} cd libressl-${{ env.libressl-version }}
# FIXME: on the 4.0.1 release, delete '-DHAVE_ENDIAN_H=0' # FIXME: on the 4.0.1 release, delete '-DHAVE_ENDIAN_H=0'
cmake . \ cmake -B . \
-DHAVE_ENDIAN_H=0 \ -DHAVE_ENDIAN_H=0 \
-DCMAKE_INSTALL_PREFIX="$HOME/libressl" \ -DCMAKE_INSTALL_PREFIX="$HOME/libressl" \
-DCMAKE_SYSTEM_NAME=iOS \ -DCMAKE_SYSTEM_NAME=iOS \

View File

@ -202,9 +202,9 @@ if(WIN32)
set(ENABLE_UNICODE ON) set(ENABLE_UNICODE ON)
endif() endif()
if(ENABLE_UNICODE) if(ENABLE_UNICODE)
add_definitions("-DUNICODE" "-D_UNICODE") set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS "UNICODE" "_UNICODE")
if(MINGW) if(MINGW)
add_compile_options("-municode") set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS "-municode")
endif() endif()
endif() endif()
@ -216,7 +216,7 @@ if(WIN32)
set(CURL_TARGET_WINDOWS_VERSION "" CACHE STRING "Minimum target Windows version as hex string") set(CURL_TARGET_WINDOWS_VERSION "" CACHE STRING "Minimum target Windows version as hex string")
if(CURL_TARGET_WINDOWS_VERSION) if(CURL_TARGET_WINDOWS_VERSION)
add_definitions("-D_WIN32_WINNT=${CURL_TARGET_WINDOWS_VERSION}") set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS "_WIN32_WINNT=${CURL_TARGET_WINDOWS_VERSION}")
list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_WIN32_WINNT=${CURL_TARGET_WINDOWS_VERSION}") # Apply to all feature checks list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_WIN32_WINNT=${CURL_TARGET_WINDOWS_VERSION}") # Apply to all feature checks
endif() endif()
@ -513,7 +513,7 @@ endif()
# If we are on AIX, do the _ALL_SOURCE magic # If we are on AIX, do the _ALL_SOURCE magic
if(CMAKE_SYSTEM_NAME STREQUAL "AIX") if(CMAKE_SYSTEM_NAME STREQUAL "AIX")
add_definitions("-D_ALL_SOURCE") set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS "_ALL_SOURCE")
endif() endif()
# If we are on Haiku, make sure that the network library is brought in. # If we are on Haiku, make sure that the network library is brought in.
@ -605,8 +605,8 @@ if(ENABLE_IPV6)
if(WIN32) if(WIN32)
check_struct_has_member("struct sockaddr_in6" "sin6_scope_id" "winsock2.h;ws2tcpip.h" HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID) check_struct_has_member("struct sockaddr_in6" "sin6_scope_id" "winsock2.h;ws2tcpip.h" HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID)
else() else()
check_struct_has_member("struct sockaddr_in6" "sin6_addr" "netinet/in.h" HAVE_SOCKADDR_IN6_SIN6_ADDR)
check_struct_has_member("struct sockaddr_in6" "sin6_scope_id" "netinet/in.h" HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID) check_struct_has_member("struct sockaddr_in6" "sin6_scope_id" "netinet/in.h" HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID)
check_struct_has_member("struct sockaddr_in6" "sin6_addr" "netinet/in.h" HAVE_SOCKADDR_IN6_SIN6_ADDR)
if(NOT HAVE_SOCKADDR_IN6_SIN6_ADDR) if(NOT HAVE_SOCKADDR_IN6_SIN6_ADDR)
if(NOT DOS AND NOT AMIGA) if(NOT DOS AND NOT AMIGA)
message(WARNING "struct sockaddr_in6 not available, disabling IPv6 support") message(WARNING "struct sockaddr_in6 not available, disabling IPv6 support")
@ -1932,7 +1932,7 @@ endif()
include(CMake/OtherTests.cmake) include(CMake/OtherTests.cmake)
add_definitions("-DHAVE_CONFIG_H") set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS "HAVE_CONFIG_H")
if(WIN32) if(WIN32)
list(APPEND CURL_LIBS "ws2_32" "bcrypt") list(APPEND CURL_LIBS "ws2_32" "bcrypt")

View File

@ -56,7 +56,7 @@ if [ "${BUILD_SYSTEM}" = 'CMake' ]; then
[ "${PRJ_CFG}" = 'Release' ] && options+=' -DCMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE=' [ "${PRJ_CFG}" = 'Release' ] && options+=' -DCMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE='
[[ "${PRJ_GEN}" = *'Visual Studio'* ]] && options+=' -DCMAKE_VS_GLOBALS=TrackFileAccess=false' [[ "${PRJ_GEN}" = *'Visual Studio'* ]] && options+=' -DCMAKE_VS_GLOBALS=TrackFileAccess=false'
# shellcheck disable=SC2086 # shellcheck disable=SC2086
cmake -B "_bld${_chkprefill}" "-G${PRJ_GEN}" ${TARGET} \ cmake -B "_bld${_chkprefill}" -G "${PRJ_GEN}" ${TARGET} \
-DCURL_USE_OPENSSL="${OPENSSL}" \ -DCURL_USE_OPENSSL="${OPENSSL}" \
-DCURL_USE_SCHANNEL="${SCHANNEL}" \ -DCURL_USE_SCHANNEL="${SCHANNEL}" \
-DHTTP_ONLY="${HTTP_ONLY}" \ -DHTTP_ONLY="${HTTP_ONLY}" \

View File

@ -251,7 +251,7 @@ You can build curl with cmake:
% cd .. % cd ..
% git clone https://github.com/curl/curl % git clone https://github.com/curl/curl
% cd curl % cd curl
% cmake . -B bld -DCURL_USE_OPENSSL=ON -DUSE_OPENSSL_QUIC=ON % cmake -B bld -DCURL_USE_OPENSSL=ON -DUSE_OPENSSL_QUIC=ON
% cmake --build bld % cmake --build bld
% cmake --install bld % cmake --install bld

View File

@ -23,7 +23,7 @@
########################################################################### ###########################################################################
set(LIB_NAME "libcurl") set(LIB_NAME "libcurl")
set(LIBCURL_OUTPUT_NAME "libcurl" CACHE STRING "Basename of the curl library") set(LIBCURL_OUTPUT_NAME "libcurl" CACHE STRING "Basename of the curl library")
add_definitions("-DBUILDING_LIBCURL") set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS "BUILDING_LIBCURL")
configure_file("curl_config.h.cmake" "${CMAKE_CURRENT_BINARY_DIR}/curl_config.h") configure_file("curl_config.h.cmake" "${CMAKE_CURRENT_BINARY_DIR}/curl_config.h")

View File

@ -22,7 +22,7 @@
# #
########################################################################### ###########################################################################
set(EXE_NAME curl) set(EXE_NAME curl)
add_definitions("-DBUILDING_CURL") set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS "BUILDING_CURL")
set(_curl_cfiles_gen "") set(_curl_cfiles_gen "")
set(_curl_hfiles_gen "") set(_curl_hfiles_gen "")

View File

@ -28,9 +28,9 @@ fi
if [ "${mode}" = 'all' ] || [ "${mode}" = 'find_package' ]; then if [ "${mode}" = 'all' ] || [ "${mode}" = 'find_package' ]; then
rm -rf bld-curl rm -rf bld-curl
cmake ../.. -B bld-curl cmake ../.. -B bld-curl -DCMAKE_INSTALL_PREFIX="${PWD}/bld-curl/_pkg"
cmake --build bld-curl cmake --build bld-curl
cmake --install bld-curl --prefix bld-curl/_pkg cmake --install bld-curl
rm -rf bld-find_package rm -rf bld-find_package
cmake -B bld-find_package \ cmake -B bld-find_package \
-DTEST_INTEGRATION_MODE=find_package \ -DTEST_INTEGRATION_MODE=find_package \