@@ -60,26 +60,40 @@ endmacro()
6060
6161function (add_ur_target_compile_options name )
6262 if (NOT MSVC )
63+ target_compile_definitions (${name} PRIVATE -D_FORTIFY_SOURCE=2)
6364 target_compile_options (${name} PRIVATE
64- -fPIC
65+ # Warning options
6566 -Wall
6667 -Wpedantic
6768 -Wempty-body
69+ -Wformat
70+ -Wformat-security
6871 -Wunused-parameter
72+
73+ # Hardening options
74+ -fPIC
75+ -fstack-protector-strong
76+ -fvisibility=hidden # Required for -fsanitize=cfi
77+ # -fsanitize=cfi requires -flto, which breaks a lot of things
78+ # See: https://github.com/oneapi-src/unified-runtime/issues/2120
79+ # -flto
80+ # $<$<CXX_COMPILER_ID:Clang,AppleClang>:-fsanitize=cfi>
81+ # -fcf-protection not supported in GCC < 8
82+ $<$<OR :$<NOT :$<CXX_COMPILER_ID:GNU>>,$<VERSION_GREATER_EQUAL :$<CXX_COMPILER_VERSION>,8>>:-fcf-protection=full>
83+ # -fstack-clash-protection is not supported in apple clang or GCC < 8
84+ $<$<AND :$<CXX_COMPILER_ID:GNU>,$<VERSION_GREATER_EQUAL :$<CXX_COMPILER_VERSION>,8>>:-fstack-clash-protection>
85+ $<$<CXX_COMPILER_ID:Clang>:-fstack-clash-protection>
86+
87+ # Colored output
6988 $<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=always>
7089 $<$<CXX_COMPILER_ID:Clang,AppleClang>:-fcolor-diagnostics>
7190 )
91+ if (UR_DEVELOPER_MODE)
92+ target_compile_options (${name} PRIVATE -Werror)
93+ endif ()
7294 if (CMAKE_BUILD_TYPE STREQUAL "Release" )
73- target_compile_definitions (${name} PRIVATE -D_FORTIFY_SOURCE=2)
7495 target_compile_options (${name} PRIVATE -fvisibility=hidden)
7596 endif ()
76- if (UR_DEVELOPER_MODE)
77- target_compile_options (${name} PRIVATE
78- -Werror
79- -fno-omit-frame-pointer
80- -fstack-protector-strong
81- )
82- endif ()
8397 elseif (MSVC )
8498 target_compile_options (${name} PRIVATE
8599 $<$<CXX_COMPILER_ID:MSVC >:/MP> # clang-cl.exe does not support /MP
@@ -103,7 +117,15 @@ endfunction()
103117function (add_ur_target_link_options name )
104118 if (NOT MSVC )
105119 if (NOT APPLE )
106- target_link_options (${name} PRIVATE "LINKER:-z,relro,-z,now" )
120+ target_link_options (${name} PRIVATE "LINKER:-z,relro,-z,now,-z,noexecstack" )
121+ if (UR_DEVELOPER_MODE)
122+ target_link_options (${name} PRIVATE -Werror)
123+ endif ()
124+ if (CMAKE_BUILD_TYPE STREQUAL "Release" )
125+ target_link_options (${name} PRIVATE
126+ $<$<CXX_COMPILER_ID:GNU>:-pie>
127+ )
128+ endif ()
107129 endif ()
108130 elseif (MSVC )
109131 target_link_options (${name} PRIVATE
0 commit comments