Make CHECK_C_LINKER_FLAG work with, at least, the Xcode generator.
authorGuy Harris <guy@alum.mit.edu>
Tue, 26 Jan 2016 23:36:00 +0000 (15:36 -0800)
committerGuy Harris <guy@alum.mit.edu>
Wed, 27 Jan 2016 00:55:07 +0000 (00:55 +0000)
As per a suggestion from a Kitware person in bug 0015934:

https://cmake.org/Bug/view.php?id=15934

We also don't need to check whether the resulting code *runs* -
check_c_source_compiles not only compiles, it links as well.

Change-Id: Ied09b9da7c88ac46f14df14f6a6260037abdbef4
Reviewed-on: https://code.wireshark.org/review/13556
Petri-Dish: Guy Harris <guy@alum.mit.edu>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Guy Harris <guy@alum.mit.edu>
cmake/modules/CheckCLinkerFlag.cmake

index abe4a3a283d24036091337cefc14ea5ef98ee84b..4a07a8612e3e5e09b8c177eab386841517c12a9b 100644 (file)
 INCLUDE(CheckCSourceRuns)
 
 MACRO (CHECK_C_LINKER_FLAG _FLAG _RESULT)
-   SET(CMAKE_REQUIRED_FLAGS "${_FLAG}")
-   message(status "check linker flag - test linker flags: ${CMAKE_REQUIRED_FLAGS}")
-   CHECK_C_SOURCE_RUNS("int main() { return 0;}" ${_RESULT})
-   SET(CMAKE_REQUIRED_FLAGS "")
+   #
+   # This is ugly.
+   #
+   # See CMake bug 0015934:
+   #
+   #    https://cmake.org/Bug/view.php?id=15934
+   #
+   # So we add the flags to CMAKE_REQUIRED_LIBRARIES, to sneak it into
+   # the linker flags.
+   #
+   # This may or may not work with versions earlier than 2.8.11, although
+   # 2.8.10's Xcode generator doesn't appear to work at all - it fails
+   # with an internal CMake error.
+   #
+   # With 3.2 and later, we could also set policy CMP0056 to NEW and
+   # set CMAKE_EXE_LINKER_FLAGS.
+   #
+   set(CMAKE_REQUIRED_LIBRARIES "${_FLAG}")
+   message(status "check linker flag - test linker flags: ${CMAKE_REQUIRED_LIBRARIES}")
+   check_c_source_compiles("int main() { return 0;}" ${_RESULT})
+   set(CMAKE_REQUIRED_LIBRARIES "")
 ENDMACRO (CHECK_C_LINKER_FLAG)