Skip to content

Commit 7e3ee09

Browse files
committed
[libc++][CMake] Use debug MSVC runtimes when libc++ is built in debug mode
Summary: This patch allows libc++ to be built against the debug MSVC runtimes instead of just the release ones. Reviewers: rnk, majnemer, compnerd, smeenai Subscribers: mgorny, cfe-commits Differential Revision: https://reviews.llvm.org/D28725 llvm-svn: 292006
1 parent 63e2cd6 commit 7e3ee09

File tree

6 files changed

+68
-8
lines changed

6 files changed

+68
-8
lines changed

libcxx/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ if (LIBCXX_GENERATE_COVERAGE)
369369
endif()
370370

371371
string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
372+
if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
373+
set(LIBCXX_DEBUG_BUILD ON)
374+
else()
375+
set(LIBCXX_DEBUG_BUILD OFF)
376+
endif()
372377

373378
#===============================================================================
374379
# Setup Compiler Flags
@@ -386,7 +391,7 @@ remove_flags(-stdlib=libc++ -stdlib=libstdc++)
386391
# FIXME: Remove all debug flags and flags that change which Windows
387392
# default libraries are linked. Currently we only support linking the
388393
# non-debug DLLs
389-
remove_flags("/D_DEBUG" "/MTd" "/MDd" "/MT" "/Md" "/RTC1")
394+
remove_flags("/D_DEBUG" "/MTd" "/MDd" "/MT" "/Md")
390395

391396
# FIXME(EricWF): See the FIXME on LIBCXX_ENABLE_PEDANTIC.
392397
# Remove the -pedantic flag and -Wno-pedantic and -pedantic-errors
@@ -485,7 +490,8 @@ endif()
485490
# Assertion flags =============================================================
486491
define_if(LIBCXX_ENABLE_ASSERTIONS -UNDEBUG)
487492
define_if_not(LIBCXX_ENABLE_ASSERTIONS -DNDEBUG)
488-
if (LIBCXX_ENABLE_ASSERTIONS)
493+
define_if(LIBCXX_DEBUG_BUILD -D_DEBUG)
494+
if (LIBCXX_ENABLE_ASSERTIONS AND NOT LIBCXX_DEBUG_BUILD)
489495
# MSVC doesn't like _DEBUG on release builds. See PR 4379.
490496
define_if_not(LIBCXX_TARGETING_MSVC -D_DEBUG)
491497
endif()

libcxx/lib/CMakeLists.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,21 @@ endif()
102102
if (NOT WIN32)
103103
add_flags_if_supported(-fPIC)
104104
endif()
105+
105106
add_link_flags_if_supported(-nodefaultlibs)
106107

107108
if (LIBCXX_TARGETING_MSVC)
109+
if (LIBCXX_DEBUG_BUILD)
110+
set(LIB_SUFFIX "d")
111+
else()
112+
set(LIB_SUFFIX "")
113+
endif()
108114
add_compile_flags(/Zl)
109115
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
116+
117+
add_library_flags(ucrt${LIB_SUFFIX}) # Universal C runtime
118+
add_library_flags(vcruntime${LIB_SUFFIX}) # C++ runtime
119+
add_library_flags(msvcrt${LIB_SUFFIX}) # C runtime startup files
113120
# Required for standards-complaint wide character formatting functions
114121
# (e.g. `printfw`/`scanfw`)
115122
add_library_flags(iso_stdio_wide_specifiers)

libcxx/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pythonize_bool(LIBCXXABI_USE_LLVM_UNWINDER)
3535
pythonize_bool(LIBCXX_HAS_ATOMIC_LIB)
3636
pythonize_bool(LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB)
3737
pythonize_bool(LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY)
38+
pythonize_bool(LIBCXX_DEBUG_BUILD)
3839

3940
# By default, for non-standalone builds, libcxx and libcxxabi share a library
4041
# directory.

libcxx/test/libcxx/test/config.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def __init__(self, lit_config, config):
6868
self.cxx_runtime_root = None
6969
self.abi_library_root = None
7070
self.link_shared = self.get_lit_bool('enable_shared', default=True)
71+
self.debug_build = self.get_lit_bool('debug_build', default=False)
7172
self.exec_env = {}
7273
self.use_target = False
7374
self.use_system_cxx_lib = False
@@ -148,6 +149,7 @@ def print_config_info(self):
148149
self.lit_config.note('Using available_features: %s' %
149150
list(self.config.available_features))
150151
self.lit_config.note('Using environment: %r' % self.exec_env)
152+
sys.stderr.flush() # Force flushing to avoid broken output on Windows
151153

152154
def get_test_format(self):
153155
return LibcxxTestFormat(
@@ -438,13 +440,20 @@ def configure_default_compile_flags(self):
438440
['-target', self.config.target_triple]):
439441
self.lit_config.warning('use_target is true but -target is '\
440442
'not supported by the compiler')
443+
if self.is_windows and self.debug_build:
444+
self.cxx.compile_flags += ['-D_DEBUG']
441445

442446
def configure_compile_flags_header_includes(self):
443-
support_path = os.path.join(self.libcxx_src_root, 'test/support')
447+
support_path = os.path.join(self.libcxx_src_root, 'test', 'support')
444448
if self.cxx_stdlib_under_test != 'libstdc++' and \
445449
not self.is_windows:
446450
self.cxx.compile_flags += [
447451
'-include', os.path.join(support_path, 'nasty_macros.hpp')]
452+
if self.is_windows and self.debug_build:
453+
self.cxx.compile_flags += [
454+
'-include', os.path.join(support_path,
455+
'set_windows_crt_report_mode.h')
456+
]
448457
self.configure_config_site_header()
449458
cxx_headers = self.get_lit_conf('cxx_headers')
450459
if cxx_headers == '' or (cxx_headers is None
@@ -667,7 +676,8 @@ def configure_link_flags_abi_library(self):
667676
self.cxx.link_flags += ['-lcxxrt']
668677
elif cxx_abi == 'none' or cxx_abi == 'default':
669678
if self.is_windows:
670-
self.cxx.link_flags += ['-lmsvcrt']
679+
debug_suffix = 'd' if self.debug_build else ''
680+
self.cxx.link_flags += ['-lmsvcrt%s' % debug_suffix]
671681
else:
672682
self.lit_config.fatal(
673683
'C++ ABI setting %s unsupported for tests' % cxx_abi)

libcxx/test/lit.site.cfg.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ config.executor = "@LIBCXX_EXECUTOR@"
2626
config.llvm_unwinder = "@LIBCXXABI_USE_LLVM_UNWINDER@"
2727
config.has_libatomic = "@LIBCXX_HAS_ATOMIC_LIB@"
2828
config.use_libatomic = "@LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB@"
29-
29+
config.debug_build = "@LIBCXX_DEBUG_BUILD@"
3030
config.libcxxabi_shared = "@LIBCXXABI_ENABLE_SHARED@"
3131
config.cxx_ext_threads = "@LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY@"
3232

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// -*- C++ -*-
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// The LLVM Compiler Infrastructure
5+
//
6+
// This file is dual licensed under the MIT and the University of Illinois Open
7+
// Source Licenses. See LICENSE.TXT for details.
8+
//
9+
//===----------------------------------------------------------------------===//
10+
#ifndef SUPPORT_SET_WINDOWS_CRT_REPORT_MODE_H
11+
#define SUPPORT_SET_WINDOWS_CRT_REPORT_MODE_H
12+
13+
#ifndef _DEBUG
14+
#error _DEBUG must be defined when using this header
15+
#endif
16+
17+
#ifndef _WIN32
18+
#error This header can only be used when targeting Windows
19+
#endif
20+
21+
#include <crtdbg.h>
22+
23+
// On Windows in debug builds the default assertion handler opens a new dialog
24+
// window which must be dismissed manually by the user. This function overrides
25+
// that setting and instead changes the assertion handler to log to stderr
26+
// instead.
27+
inline int init_crt_report_mode() {
28+
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
29+
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
30+
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
31+
return 0;
32+
}
33+
34+
static int init_crt_anchor = init_crt_report_mode();
35+
36+
#endif // SUPPORT_SET_WINDOWS_CRT_REPORT_MODE_H

0 commit comments

Comments
 (0)