mariadb/cmake/check_linker_flag.cmake
Daniel Black b4c225ac35 MDEV-23887: check_linker_flags correct for old cmake compatibility
It was only from CMake-3.14.0 that CMAKE_REQUIRED_LINK_OPTIONS
was used in CHECK_CXX_SOURCE_COMPILES. Without this, it could be
the case (as was on OSX) that a flag was never checked in
CHECK_CXX_SOURCE_COMPILES, the CHECK successfully passed, but
failed at link time.

As such we use CMAKE_REQUIRED_LIBRARIES to include the flags to check
as its compatible enough with the cmake versions for non-Windows
compilers/linkers.

Tested on x86_64 with:
* 3.11.4
* 3.17.4

Corrects: 7473e1841c

In the future:
* cmake >=3.14.0 can use CMAKE_REQUIRED_LINK_OPTIONS
* cmake >=3.18.0 can use CHECK_LINKER_FLAG (with policy CMP0057 NEW)
(e.g: commit c7ac2deff9a2c965887dcc67cbf2a3a7c3e0123d)

CMAKE_REQUIRED_LIBRARIES suggested by serg@mariadb.com

Reviewed-by: anel@mariadb.org
2020-10-21 19:11:02 +11:00

27 lines
1 KiB
CMake

include(CheckCXXSourceCompiles)
FUNCTION(MY_CHECK_AND_SET_LINKER_FLAG flag_to_set)
# Let's avoid expensive compiler tests on Windows:
IF(WIN32)
RETURN()
ENDIF()
STRING(REGEX REPLACE "[-,= +]" "_" result "HAVE_LINK_FLAG_${flag_to_set}")
SET(SAVE_CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
STRING(REGEX REPLACE "^-Wno-" "-W" flag_to_check ${flag_to_set})
SET(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${flag_to_check})
CHECK_CXX_SOURCE_COMPILES("int main(void) { return 0; }" ${result})
SET(CMAKE_REQUIRED_LIBRARIES "${SAVE_CMAKE_REQUIRED_LIBRARIES}")
IF (${result})
FOREACH(linktype SHARED MODULE EXE)
IF(ARGN)
FOREACH(type ${ARGN})
SET(CMAKE_${linktype}_LINKER_FLAGS_${type}
"${CMAKE_${linktype}_LINKER_FLAGS_${type}} ${flag_to_set}" PARENT_SCOPE)
ENDFOREACH()
ELSE()
SET(CMAKE_${linktype}_LINKER_FLAGS
"${CMAKE_${linktype}_LINKER_FLAGS} ${flag_to_set}" PARENT_SCOPE)
ENDIF()
ENDFOREACH()
ENDIF()
ENDFUNCTION()