[PATCH] D28441: [libc++] [CMake] Link with /nodefaultlibs on Windows

Eric Fiselier via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 13 18:36:03 PST 2017


EricWF updated this revision to Diff 84414.
EricWF added a comment.

Attempt to address review comments

- Rename `LIBCXX_TARGETING_WINDOWS` to `LIBCXX_TARGETING_MSVC`. It is set to on when CMake defines `MSVC` to `ON`. This macro is intented to represent cases where we are targeting native Windows and the native CRT.

There has been a lot of noise on this patch. What else needs to be done before committing?


https://reviews.llvm.org/D28441

Files:
  CMakeLists.txt
  cmake/Modules/HandleLibcxxFlags.cmake
  lib/CMakeLists.txt
  test/libcxx/test/config.py


Index: test/libcxx/test/config.py
===================================================================
--- test/libcxx/test/config.py
+++ test/libcxx/test/config.py
@@ -667,7 +667,7 @@
             self.cxx.link_flags += ['-lcxxrt']
         elif cxx_abi == 'none' or cxx_abi == 'default':
             if self.is_windows:
-                self.cxx.link_flags += ['-lmsvcrtd']
+                self.cxx.link_flags += ['-lmsvcrt']
         else:
             self.lit_config.fatal(
                 'C++ ABI setting %s unsupported for tests' % cxx_abi)
Index: lib/CMakeLists.txt
===================================================================
--- lib/CMakeLists.txt
+++ lib/CMakeLists.txt
@@ -104,6 +104,16 @@
 endif()
 add_link_flags_if_supported(-nodefaultlibs)
 
+if (LIBCXX_TARGETING_MSVC)
+  add_compile_flags(/Zl)
+  add_link_flags(/nodefaultlib)
+  add_library_flags(ucrt) # Universal C runtime
+  add_library_flags(vcruntime) # C++ runtime
+  add_library_flags(msvcrt) # C runtime startup files
+  # Required for wide character formatting functions (e.g. `printfw`/`scanfw`)
+  add_library_flags(iso_stdio_wide_specifiers)
+endif()
+
 if (LIBCXX_OSX_REEXPORT_SYSTEM_ABI_LIBRARY)
   if (NOT DEFINED LIBCXX_LIBCPPABI_VERSION)
     set(LIBCXX_LIBCPPABI_VERSION "2") # Default value
Index: cmake/Modules/HandleLibcxxFlags.cmake
===================================================================
--- cmake/Modules/HandleLibcxxFlags.cmake
+++ cmake/Modules/HandleLibcxxFlags.cmake
@@ -26,6 +26,10 @@
 # or added in other parts of LLVM's cmake configuration.
 macro(remove_flags)
   foreach(var ${ARGN})
+    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
+    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_DEBUG}")
+    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_DEBUG}")
+    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_DEBUG}")
     string(REPLACE "${var}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
     string(REPLACE "${var}" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
     string(REPLACE "${var}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt
+++ CMakeLists.txt
@@ -39,6 +39,12 @@
  build directory and run 'cmake /path/to/${PROJECT_NAME} [options]' there."
  )
 
+if (MSVC)
+  set(LIBCXX_TARGETING_MSVC ON)
+else()
+  set(LIBCXX_TARGETING_MSVC OFF)
+endif()
+
 #===============================================================================
 # Setup CMake Options
 #===============================================================================
@@ -377,6 +383,11 @@
 endif()
 remove_flags(-stdlib=libc++ -stdlib=libstdc++)
 
+# FIXME: Remove all debug flags and flags that change which Windows
+# default libraries are linked. Currently we only support linking the
+# non-debug DLLs
+remove_flags("/D_DEBUG" "/MTd" "/MDd" "/MT" "/Md" "/RTC1")
+
 # FIXME(EricWF): See the FIXME on LIBCXX_ENABLE_PEDANTIC.
 # Remove the -pedantic flag and -Wno-pedantic and -pedantic-errors
 # so they don't get transformed into -Wno and -errors respectivly.
@@ -474,7 +485,7 @@
 # Assertion flags =============================================================
 define_if(LIBCXX_ENABLE_ASSERTIONS -UNDEBUG)
 define_if_not(LIBCXX_ENABLE_ASSERTIONS -DNDEBUG)
-if (LIBCXX_ENABLE_ASSERTIONS)
+if (LIBCXX_ENABLE_ASSERTIONS AND NOT LIBCXX_TARGETING_MSVC)
   # MSVC doesn't like _DEBUG on release builds. See PR 4379.
   define_if_not(MSVC -D_DEBUG)
 endif()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28441.84414.patch
Type: text/x-patch
Size: 3573 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170114/44f077e7/attachment-0001.bin>


More information about the cfe-commits mailing list