From 925c23b1c9669d1be4b657b6f2e853298e87b9f8 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Sun, 19 May 2024 15:11:18 +0800 Subject: [PATCH 1/6] Fix setting of config-specific hardening options --- src/Hardening.cmake | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Hardening.cmake b/src/Hardening.cmake index 6c6e26c1..a239a6dd 100644 --- a/src/Hardening.cmake +++ b/src/Hardening.cmake @@ -44,9 +44,7 @@ function( list(APPEND HARDENING_COMPILE_OPTIONS -Wstringop-overflow=4 -Wformat-overflow=2) endif() - if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") - target_compile_definitions(${_project_name} INTERFACE _FORTIFY_SOURCE=3) - endif() + target_compile_definitions(${_project_name} INTERFACE $<$:DEBUG_FORTIFY_SOURCE=3>) endif() if(${ENABLE_ELF_PROTECTION}) @@ -64,9 +62,7 @@ function( list(APPEND HARDENING_LINK_OPTIONS /guard:cf) endif() - if(${ENABLE_STACK_PROTECTION} AND CMAKE_BUILD_TYPE STREQUAL "Debug") - list(APPEND HARDENING_COMPILE_OPTIONS /RTC1) - endif() + list(APPEND HARDENING_COMPILE_OPTIONS $<$:/RTC1>) if(${ENABLE_OVERFLOW_PROTECTION}) list(APPEND HARDENING_COMPILE_OPTIONS /sdl) From fa01d39017eaae562fcd44d5ec6f2e4fc6be8e00 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Sun, 19 May 2024 15:20:50 +0800 Subject: [PATCH 2/6] Fix setting of release only LTO --- src/Optimization.cmake | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Optimization.cmake b/src/Optimization.cmake index 609fecf5..7e5d934a 100644 --- a/src/Optimization.cmake +++ b/src/Optimization.cmake @@ -2,25 +2,25 @@ include_guard() # Enable Interprocedural Optimization (Link Time Optimization, LTO) in the release build macro(enable_interprocedural_optimization _project_name) - if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") - include(CheckIPOSupported) - check_ipo_supported(RESULT result OUTPUT output) - is_mingw(_is_mingw) - if(result AND NOT ${_is_mingw}) - # If a static library of this project is used in another project that does not have `CMAKE_INTERPROCEDURAL_OPTIMIZATION` enabled, a linker error might happen. - # TODO set this option in `package_project` function. - message( - STATUS - "Interprocedural optimization is enabled. In other projects, linking with the compiled libraries of this project might require `set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)`" - ) - set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON) - set_target_properties(${_project_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON) - else() - message( - WARNING - "Interprocedural Optimization is not supported. Not using it. Here is the error log: ${output}" - ) - endif() + include(CheckIPOSupported) + check_ipo_supported(RESULT result OUTPUT output) + is_mingw(_is_mingw) + if(result AND NOT ${_is_mingw}) + # If a static library of this project is used in another project that does not have `CMAKE_INTERPROCEDURAL_OPTIMIZATION` enabled, a linker error might happen. + # TODO set this option in `package_project` function. + message( + STATUS + "Interprocedural optimization is enabled. In other projects, linking with the compiled libraries of this project might require `set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)`" + ) + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON) + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO ON) + set_target_properties(${_project_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE ON) + set_target_properties(${_project_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO ON) + else() + message( + WARNING + "Interprocedural Optimization is not supported. Not using it. Here is the error log: ${output}" + ) endif() endmacro() From b00eecb356bd550eaae55a8c8c8b3e8fbde01530 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Sun, 19 May 2024 15:24:52 +0800 Subject: [PATCH 3/6] typo: DEBUG_FORTIFY_SOURCE -> _FORTIFY_SOURCE --- src/Hardening.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Hardening.cmake b/src/Hardening.cmake index a239a6dd..eb8c985d 100644 --- a/src/Hardening.cmake +++ b/src/Hardening.cmake @@ -44,7 +44,7 @@ function( list(APPEND HARDENING_COMPILE_OPTIONS -Wstringop-overflow=4 -Wformat-overflow=2) endif() - target_compile_definitions(${_project_name} INTERFACE $<$:DEBUG_FORTIFY_SOURCE=3>) + target_compile_definitions(${_project_name} INTERFACE $<$:_FORTIFY_SOURCE=3>) endif() if(${ENABLE_ELF_PROTECTION}) From 661d47f511ff26ea14ab24878cf45c3c710cd405 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Sun, 19 May 2024 16:15:31 +0800 Subject: [PATCH 4/6] Require CMake 3.24 as the minium version --- src/Index.cmake | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Index.cmake b/src/Index.cmake index 1a8b890a..454ff450 100644 --- a/src/Index.cmake +++ b/src/Index.cmake @@ -1,4 +1,5 @@ -cmake_minimum_required(VERSION 3.20) +cmake_minimum_required(VERSION 3.24) +# 3.24 is required to make CheckIPOSupported prefer to honor the calling project's flags, see https://cmake.org/cmake/help/latest/policy/CMP0138.html # 3.20 is required by the windows toolchain and cmake_path. It also has a more reliable building functionality. # 3.18 required by package_project and interprocedural optimization. It also has a more reliable building functionality (no errors during the linking stage). From df4898c78ef4dcd92a627482bfd01c4c1f797512 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Sat, 1 Jun 2024 10:37:08 +0800 Subject: [PATCH 5/6] Trigger ci From fb70b92e09fe7f3a9fdb4e2120fc29adeca80d01 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 10 Jun 2024 16:29:42 +0800 Subject: [PATCH 6/6] Enable Policy 138 instead of bumping cmake version --- src/Index.cmake | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Index.cmake b/src/Index.cmake index 454ff450..94d4b936 100644 --- a/src/Index.cmake +++ b/src/Index.cmake @@ -1,13 +1,14 @@ -cmake_minimum_required(VERSION 3.24) -# 3.24 is required to make CheckIPOSupported prefer to honor the calling project's flags, see https://cmake.org/cmake/help/latest/policy/CMP0138.html +cmake_minimum_required(VERSION 3.20) # 3.20 is required by the windows toolchain and cmake_path. It also has a more reliable building functionality. # 3.18 required by package_project and interprocedural optimization. It also has a more reliable building functionality (no errors during the linking stage). include_guard() -# fix DOWNLOAD_EXTRACT_TIMESTAMP warning in FetchContent if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0") + # fix DOWNLOAD_EXTRACT_TIMESTAMP warning in FetchContent cmake_policy(SET CMP0135 NEW) + # make CheckIPOSupported prefer to honor the calling project's flags + cmake_policy(SET CMP0138 NEW) endif() # only useable here