Skip to content

Commit cc1f65c

Browse files
committed
[libc++] [CMake] Link with /nodefaultlibs on Windows
Summary: This patch attempts to fix the libc++ build/link so that it doesn't use an default C++ libraries on Windows. This is needed to prevent linking to MSVC's STL library. Additionally this patch changes libc++ so that it is always linked with the non-debug DLL's (e.g. `/MD`). This is needed so that the test suite can correctly link the same libraries without needing to know which configuration `c++.dll` was linked with. Reviewers: compnerd, rnk, majnemer, kimgr, awson, halyavin, smeenai Subscribers: cfe-commits, mgorny Differential Revision: https://reviews.llvm.org/D28441 llvm-svn: 292001
1 parent 2f116c4 commit cc1f65c

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

libcxx/CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ MACRO_ENSURE_OUT_OF_SOURCE_BUILD(
3939
build directory and run 'cmake /path/to/${PROJECT_NAME} [options]' there."
4040
)
4141

42+
if (MSVC)
43+
set(LIBCXX_TARGETING_MSVC ON)
44+
else()
45+
set(LIBCXX_TARGETING_MSVC OFF)
46+
endif()
47+
4248
#===============================================================================
4349
# Setup CMake Options
4450
#===============================================================================
@@ -377,6 +383,11 @@ if (NOT LIBCXX_STANDALONE_BUILD)
377383
endif()
378384
remove_flags(-stdlib=libc++ -stdlib=libstdc++)
379385

386+
# FIXME: Remove all debug flags and flags that change which Windows
387+
# default libraries are linked. Currently we only support linking the
388+
# non-debug DLLs
389+
remove_flags("/D_DEBUG" "/MTd" "/MDd" "/MT" "/Md" "/RTC1")
390+
380391
# FIXME(EricWF): See the FIXME on LIBCXX_ENABLE_PEDANTIC.
381392
# Remove the -pedantic flag and -Wno-pedantic and -pedantic-errors
382393
# so they don't get transformed into -Wno and -errors respectivly.
@@ -476,7 +487,7 @@ define_if(LIBCXX_ENABLE_ASSERTIONS -UNDEBUG)
476487
define_if_not(LIBCXX_ENABLE_ASSERTIONS -DNDEBUG)
477488
if (LIBCXX_ENABLE_ASSERTIONS)
478489
# MSVC doesn't like _DEBUG on release builds. See PR 4379.
479-
define_if_not(MSVC -D_DEBUG)
490+
define_if_not(LIBCXX_TARGETING_MSVC -D_DEBUG)
480491
endif()
481492

482493
# Modules flags ===============================================================

libcxx/cmake/Modules/HandleLibcxxFlags.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ endmacro()
2626
# or added in other parts of LLVM's cmake configuration.
2727
macro(remove_flags)
2828
foreach(var ${ARGN})
29+
string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
30+
string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_DEBUG}")
31+
string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_DEBUG}")
32+
string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_DEBUG}")
2933
string(REPLACE "${var}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
3034
string(REPLACE "${var}" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
3135
string(REPLACE "${var}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")

libcxx/lib/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ if (NOT WIN32)
104104
endif()
105105
add_link_flags_if_supported(-nodefaultlibs)
106106

107+
if (LIBCXX_TARGETING_MSVC)
108+
add_compile_flags(/Zl)
109+
add_link_flags(/nodefaultlib)
110+
add_library_flags(ucrt) # Universal C runtime
111+
add_library_flags(vcruntime) # C++ runtime
112+
add_library_flags(msvcrt) # C runtime startup files
113+
# Required for standards-complaint wide character formatting functions
114+
# (e.g. `printfw`/`scanfw`)
115+
add_library_flags(iso_stdio_wide_specifiers)
116+
endif()
117+
107118
if (LIBCXX_OSX_REEXPORT_SYSTEM_ABI_LIBRARY)
108119
if (NOT DEFINED LIBCXX_LIBCPPABI_VERSION)
109120
set(LIBCXX_LIBCPPABI_VERSION "2") # Default value

libcxx/test/libcxx/test/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ def configure_link_flags_abi_library(self):
667667
self.cxx.link_flags += ['-lcxxrt']
668668
elif cxx_abi == 'none' or cxx_abi == 'default':
669669
if self.is_windows:
670-
self.cxx.link_flags += ['-lmsvcrtd']
670+
self.cxx.link_flags += ['-lmsvcrt']
671671
else:
672672
self.lit_config.fatal(
673673
'C++ ABI setting %s unsupported for tests' % cxx_abi)

0 commit comments

Comments
 (0)