Skip to content

Commit 67c5f05

Browse files
committed
Add recommended security linker flags
1 parent 5b4c3d2 commit 67c5f05

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

cmake/helpers.cmake

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ function(add_ur_target_compile_options name)
6666
$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=always>
6767
$<$<CXX_COMPILER_ID:Clang,AppleClang>:-fcolor-diagnostics>
6868
)
69-
7069
if (CMAKE_BUILD_TYPE STREQUAL "Release")
7170
target_compile_definitions(${name} PRIVATE -D_FORTIFY_SOURCE=2)
7271
endif()
@@ -84,27 +83,46 @@ function(add_ur_target_compile_options name)
8483
/MD$<$<CONFIG:Debug>:d>
8584
/GS
8685
)
87-
add_link_options(
86+
87+
if(UR_DEVELOPER_MODE)
88+
target_compile_options(${name} PRIVATE /WX /GS)
89+
endif()
90+
endif()
91+
endfunction()
92+
93+
function(add_ur_target_link_options name)
94+
if(NOT MSVC)
95+
if (NOT APPLE)
96+
target_link_options(${name} PRIVATE "LINKER:-z,relro,-z,now")
97+
endif()
98+
elseif(MSVC)
99+
target_link_options(${name} PRIVATE
88100
/DYNAMICBASE
89101
/HIGHENTROPYVA
90-
/ALLOWISOLATION
91102
/NXCOMPAT
92103
)
104+
endif()
105+
endfunction()
93106

94-
if(UR_DEVELOPER_MODE)
95-
target_compile_options(${name} PRIVATE /WX /GS)
96-
endif()
107+
function(add_ur_target_exec_options name)
108+
if(MSVC)
109+
target_link_options(${name} PRIVATE
110+
/ALLOWISOLATION
111+
)
97112
endif()
98113
endfunction()
99114

100115
function(add_ur_executable name)
101116
add_executable(${name} ${ARGN})
102117
add_ur_target_compile_options(${name})
118+
add_ur_target_exec_options(${name})
119+
add_ur_target_link_options(${name})
103120
endfunction()
104121

105122
function(add_ur_library name)
106123
add_library(${name} ${ARGN})
107124
add_ur_target_compile_options(${name})
125+
add_ur_target_link_options(${name})
108126
endfunction()
109127

110128
include(FetchContent)

0 commit comments

Comments
 (0)