Skip to content

Commit

Permalink
EnableCompilerFlag: Support multiple space-separated flags
Browse files Browse the repository at this point in the history
This allows correct detection of linker noexecstack support.

When testing for support of multiple flags, the flags must be a list of
values separated by semicolons, not a string of values separated by
spaces. See https://gitlab.kitware.com/cmake/cmake/-/issues/26024

Enclose the flaglist variable in quotation marks when calling
CHECK_*_FLAG so that it is passed through unmodified.

After it is determined that a flag is supported, it is still the string
version of the flag variable that is appended to the corresponding
CMAKE_*_FLAGS variable.

Closes facebook#4056
  • Loading branch information
ryandesign committed Jun 3, 2024
1 parent f70fb7c commit 9854451
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions build/cmake/CMakeModules/AddZstdCompilationFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ if (ZSTD_HAVE_CHECK_LINKER_FLAG)
endif()

function(EnableCompilerFlag _flag _C _CXX _LD)
string(REGEX REPLACE " " ";" flaglist "${_flag}")
string(REGEX REPLACE "\\+" "PLUS" varname "${_flag}")
string(REGEX REPLACE "[^A-Za-z0-9]+" "_" varname "${varname}")
string(REGEX REPLACE "^_+" "" varname "${varname}")
string(TOUPPER "${varname}" varname)
if (_C)
CHECK_C_COMPILER_FLAG(${_flag} C_FLAG_${varname})
CHECK_C_COMPILER_FLAG("${flaglist}" C_FLAG_${varname})
if (C_FLAG_${varname})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_flag}" PARENT_SCOPE)
endif ()
endif ()
if (_CXX)
CHECK_CXX_COMPILER_FLAG(${_flag} CXX_FLAG_${varname})
CHECK_CXX_COMPILER_FLAG("${flaglist}" CXX_FLAG_${varname})
if (CXX_FLAG_${varname})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}" PARENT_SCOPE)
endif ()
Expand All @@ -39,7 +40,7 @@ function(EnableCompilerFlag _flag _C _CXX _LD)
# results for this configuration,
# see: https://gitlab.kitware.com/cmake/cmake/-/issues/22023
if (ZSTD_HAVE_CHECK_LINKER_FLAG AND NOT MSVC)
CHECK_LINKER_FLAG(C ${_flag} LD_FLAG_${varname})
CHECK_LINKER_FLAG(C "${flaglist}" LD_FLAG_${varname})
else ()
set(LD_FLAG_${varname} false)
endif ()
Expand Down

0 comments on commit 9854451

Please sign in to comment.