Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix Hardening options and LTO for multi-configuration generators #260

Merged
merged 6 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/Hardening.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<$<CONFIG:Release,RelWithDebInfo>:_FORTIFY_SOURCE=3>)
endif()

if(${ENABLE_ELF_PROTECTION})
Expand All @@ -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 $<$<CONFIG:Debug>:/RTC1>)

if(${ENABLE_OVERFLOW_PROTECTION})
list(APPEND HARDENING_COMPILE_OPTIONS /sdl)
Expand Down
4 changes: 3 additions & 1 deletion src/Index.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ cmake_minimum_required(VERSION 3.20)

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
Expand Down
38 changes: 19 additions & 19 deletions src/Optimization.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading