Skip to content

Commit d3faf36

Browse files
[SYCL][CMAKE] Fix _FORTIFY_SOURCE=3 (#19268)
The problem here is that the macro is actually handled by glibc and value `3` isn't supported with older compiler/glibc combinations, causing warnings about the macro redefinition. We still have to support older compilers/glibc and therefore two changes were made: - UR skips setting their own `_FORTIFY_SOURCE` in favor of a global one if it is built as part of LLVM (i.e. not standalone) - Before setting `_FORTIFY_SOURCE` globally we check the compiler and fallback to value `2` for older gcc
1 parent 19d83d5 commit d3faf36

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

llvm/cmake/modules/AddSecurityFlags.cmake

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,24 +164,39 @@ macro(append_common_extra_security_flags)
164164
endif()
165165
endif()
166166

167-
if(LLVM_ON_UNIX)
168-
# Fortify Source (strongly recommended):
167+
# Fortify Source (strongly recommended):
168+
if (NOT WIN32)
169+
# Strictly speaking, _FORTIFY_SOURCE is a glibc feature and not a compiler
170+
# feature. However, we experienced some issues (warnings about redefined macro
171+
# which are problematic under -Werror) when setting it to value '3' with older
172+
# gcc versions. Hence the check.
173+
# Value '3' became supported in glibc somewhere around gcc 12, so that is
174+
# what we are looking for.
175+
if (is_gcc AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 12)
176+
set(FORTIFY_SOURCE "-D_FORTIFY_SOURCE=2")
177+
else()
178+
# Assuming that the problem is not reproducible with other compilers
179+
set(FORTIFY_SOURCE "-D_FORTIFY_SOURCE=3")
180+
endif()
181+
169182
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
170-
message(WARNING "-D_FORTIFY_SOURCE=3 can only be used with optimization.")
171-
message(WARNING "-D_FORTIFY_SOURCE=3 is not supported.")
183+
message(WARNING "${FORTIFY_SOURCE} can only be used with optimization.")
184+
message(WARNING "${FORTIFY_SOURCE} is not supported.")
172185
else()
173186
# Sanitizers do not work with checked memory functions, such as
174187
# __memset_chk. We do not build release packages with sanitizers, so just
175-
# avoid -D_FORTIFY_SOURCE=3 under LLVM_USE_SANITIZER.
188+
# avoid -D_FORTIFY_SOURCE=N under LLVM_USE_SANITIZER.
176189
if(NOT LLVM_USE_SANITIZER)
177-
message(STATUS "Building with -D_FORTIFY_SOURCE=3")
178-
add_definitions(-D_FORTIFY_SOURCE=3)
190+
message(STATUS "Building with ${FORTIFY_SOURCE}")
191+
add_definitions(${FORTIFY_SOURCE})
179192
else()
180193
message(
181-
WARNING "-D_FORTIFY_SOURCE=3 dropped due to LLVM_USE_SANITIZER.")
194+
WARNING "${FORTIFY_SOURCE} dropped due to LLVM_USE_SANITIZER.")
182195
endif()
183196
endif()
197+
endif()
184198

199+
if(LLVM_ON_UNIX)
185200
if(LLVM_ENABLE_ASSERTIONS)
186201
add_definitions(-D_GLIBCXX_ASSERTIONS)
187202
endif()

unified-runtime/cmake/helpers.cmake

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ endif()
8888

8989
function(add_ur_target_compile_options name)
9090
if(NOT MSVC)
91-
target_compile_definitions(${name} PRIVATE -D_FORTIFY_SOURCE=2)
91+
if (NOT LLVM_ENABLE_PROJECTS)
92+
# If UR is built as part of LLVM (i.e. as part of SYCL), then
93+
# _FORTIFY_SOURCE will be set globally in advance to a potentially
94+
# different value. To avoid redefinition errors, only set the
95+
# macro for a "standalone" build.
96+
target_compile_definitions(${name} PRIVATE -D_FORTIFY_SOURCE=2)
97+
endif()
9298
target_compile_options(${name} PRIVATE
9399
# Warning options
94100
-Wall

0 commit comments

Comments
 (0)