Skip to content

Instantly share code, notes, and snippets.

@erikerlandson
Created January 15, 2014 22:47
Show Gist options
  • Save erikerlandson/8446284 to your computer and use it in GitHub Desktop.
Save erikerlandson/8446284 to your computer and use it in GitHub Desktop.
A cmake macro, check_gnu_linker_flag(), for checking if the GNU toolchain linker supports a given argument
# This macro is designed to work with the GNU toolchain,
# where the linker is called indirectly via 'gcc'
# You can invoke it as in this example:
# check_gnu_linker_flag("-pie", cxx_linker_pie)
# to test if the linker supports '-pie', and store the result in
# variable 'cxx_linker_pie', in the same fashion as check_cxx_compiler_flag()
# Note, some versions of gnu linker will accept some unknown arguments
# for example -z <arg> may succeed whether <arg> is supported or not,
# although I've found this to work in newer GNU versions.
# In other words, beware of false positives. YMMV.
macro(check_gnu_linker_flag _flag _var)
message("Performing Test ${_var}")
unset(${_var})
set(_stub_code "int main() { return 0; }")
# note, this uses the top build directory,
# one might also use some temp dir, or whatever.
# (could also have compiler read directly from stdin, if that is supported)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/stub_code.cpp "${_stub_code}\n")
execute_process(COMMAND ${CMAKE_CXX_COMPILER} ${_flag} ${CMAKE_CURRENT_BINARY_DIR}/stub_code.cpp
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
ERROR_VARIABLE _stderr)
if (NOT _stderr)
message("Performing Test ${_var} - Success")
set(${_var} 1)
else()
message("Performing Test ${_var} - Failed")
endif()
endmacro(check_gnu_linker_flag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment