[cmake] Get the MSVC version by running cl rather than relying on MSVC_VERSION
authorReid Kleckner <rnk@google.com>
Wed, 30 Mar 2016 18:31:14 +0000 (18:31 +0000)
committerReid Kleckner <rnk@google.com>
Wed, 30 Mar 2016 18:31:14 +0000 (18:31 +0000)
MSVC_VERSION comes from the _MSC_VER macro, which won't correspond to
the STL version if the host compiler is clang-cl.

llvm-svn: 264901

compiler-rt/cmake/config-ix.cmake

index 28eb54b..d4a176b 100644 (file)
@@ -139,6 +139,27 @@ if (NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND
   message(FATAL_ERROR "Please use architecture with 4 or 8 byte pointers.")
 endif()
 
+# Find and run MSVC (not clang-cl) and get its version. This will tell clang-cl
+# what version of MSVC to pretend to be so that the STL works.
+set(MSVC_VERSION_FLAG "")
+if (MSVC)
+  # Find and run MSVC (not clang-cl) and get its version. This will tell
+  # clang-cl what version of MSVC to pretend to be so that the STL works.
+  execute_process(COMMAND "$ENV{VSINSTALLDIR}/VC/bin/cl.exe"
+    OUTPUT_QUIET
+    ERROR_VARIABLE MSVC_COMPAT_VERSION
+    )
+  string(REGEX REPLACE "^.*Compiler Version ([0-9.]+) for .*$" "\\1"
+    MSVC_COMPAT_VERSION "${MSVC_COMPAT_VERSION}")
+  if (MSVC_COMPAT_VERSION MATCHES "^[0-9].+$")
+    set(MSVC_VERSION_FLAG "-fms-compatibility-version=${MSVC_COMPAT_VERSION}")
+    # Add this flag into the host build if this is clang-cl.
+    if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+      append("${MSVC_VERSION_FLAG}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
+    endif()
+  endif()
+endif()
+
 # Generate the COMPILER_RT_SUPPORTED_ARCH list.
 if(ANDROID)
   # Examine compiler output to determine target architecture.
@@ -154,15 +175,10 @@ elseif(NOT APPLE) # Supported archs for Apple platforms are generated later
       test_target_arch(i686 __i686__ "-m32")
       test_target_arch(i386 __i386__ "-m32")
     else()
-      # Extract the major version from CMake's MSVC_VERSION variable and pass
-      # it to -fms-compatibility-version. The major version controls the most
-      # important parts of clang's compatibility.
-      string(SUBSTRING "${MSVC_VERSION}" 0 2 MSVC_MAJOR_VERSION)
-      set(flags "-fms-compatibility-version=${MSVC_MAJOR_VERSION}")
       if (CMAKE_SIZEOF_VOID_P EQUAL 4)
-        test_target_arch(i386 "" "${flags}")
+        test_target_arch(i386 "" "${MSVC_VERSION_FLAG}")
       else()
-        test_target_arch(x86_64 "" "${flags}")
+        test_target_arch(x86_64 "" "${MSVC_VERSION_FLAG}")
       endif()
     endif()
   elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc")