Don't treat Visual Studio specially for linker flags.
authorGuy Harris <guy@alum.mit.edu>
Wed, 27 Jan 2016 03:13:44 +0000 (19:13 -0800)
committerGuy Harris <guy@alum.mit.edu>
Wed, 27 Jan 2016 05:24:50 +0000 (05:24 +0000)
Have CHECK_C_LINKER_FLAG pass /WX to the linker if we're using MSVC, to
force the link to fail if the linker doesn't recognize the flag.

Add flags to WIRESHARK_LD_FLAGS even with MSVC; let CHECK_C_LINKER_FLAG
figure out whether the flag should be used.

While we're at it, fix CHECK_C_LINKER_FLAG to save and restore
CMAKE_REQUIRED_LIBRARIES.

Change-Id: I7f73b4cc3a28eb14e46c2e1e9ad69f5303754f01
Reviewed-on: https://code.wireshark.org/review/13558
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>
CMakeLists.txt
cmake/modules/CheckCLinkerFlag.cmake

index 137d52a6709100f2e2b95bc509e7f3c24ee55b90..c9a6400a4212e96bab126bb340d60706cf96ccd4 100644 (file)
@@ -595,34 +595,22 @@ else()
        set (C_UNUSED "" )
 endif()
 
        set (C_UNUSED "" )
 endif()
 
+set(WIRESHARK_LD_FLAGS
+       -Wl,--as-needed
+       # -flto
+       # -fwhopr
+       # -fwhole-program
+)
+# CMAKE_POSITION_INDEPENDENT_CODE is only supported starting with CMake
+# 2.8.9. Do not add -pie automatically for older versions.
 #
 #
-# XXX - at least with the Visual Studio 12 generator, this would test
-# whether the flag works with the compiler, but, at least when building
-# for Win64, the flag "works", in that the link succeeds, but with a
-# warning of an unused linker argument.
-#
-# So we leave it out for now; maybe there's a way to force the linker to
-# fail with unknown arguments, or maybe we have to look for error
-# messages in the linker output, the way CHECK_C_COMPILER_FLAG does.
+# XXX - are there other compilers that don't support -pie?  It's
+# not as if the only platforms we support are Windows and Linux....
 #
 #
-if(NOT CMAKE_C_COMPILER_ID MATCHES "MSVC")
-       set(WIRESHARK_LD_FLAGS
-               -Wl,--as-needed
-               # -flto
-               # -fwhopr
-               # -fwhole-program
-       )
-       # CMAKE_POSITION_INDEPENDENT_CODE is only supported starting with CMake
-       # 2.8.9. Do not add -pie automatically for older versions.
-       #
-       # XXX - are there other compilers that don't support -pie?  It's
-       # not as if the only platforms we support are Windows and Linux....
-       #
-       if(NOT CMAKE_VERSION VERSION_LESS "2.8.9")
-               set(WIRESHARK_LD_FLAGS ${WIRESHARK_LD_FLAGS}
-                       -pie
-               )
-       endif()
+if(NOT CMAKE_VERSION VERSION_LESS "2.8.9")
+       set(WIRESHARK_LD_FLAGS ${WIRESHARK_LD_FLAGS}
+               -pie
+       )
 endif()
 
 include(CheckCLinkerFlag)
 endif()
 
 include(CheckCLinkerFlag)
index 4a07a8612e3e5e09b8c177eab386841517c12a9b..f657a9b88483068c510dedbed8f09e26b093cea9 100644 (file)
@@ -32,9 +32,19 @@ MACRO (CHECK_C_LINKER_FLAG _FLAG _RESULT)
    # With 3.2 and later, we could also set policy CMP0056 to NEW and
    # set CMAKE_EXE_LINKER_FLAGS.
    #
    # 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}")
+   set(save_CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
+   if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
+      #
+      # This means the linker is presumably the Microsoft linker;
+      # we need to pass /WX in order to have the linker fail,
+      # rather than just complaining and driving on, if it's
+      # passed a flag it doesn't handle.
+      #
+      set(CMAKE_REQUIRED_LIBRARIES "/WX")
+   endif()
+   set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES} ${_FLAG}")
+   message(status "check linker flag - test linker flags: ${_FLAG}")
    check_c_source_compiles("int main() { return 0;}" ${_RESULT})
    check_c_source_compiles("int main() { return 0;}" ${_RESULT})
-   set(CMAKE_REQUIRED_LIBRARIES "")
+   set(CMAKE_REQUIRED_LIBRARIES "${save_CMAKE_REQUIRED_LIBRARIES}")
 ENDMACRO (CHECK_C_LINKER_FLAG)
 
 ENDMACRO (CHECK_C_LINKER_FLAG)