Skip to content

Commit

Permalink
[libcxx] [test] Fix running tests with Clang-cl in Debug mode
Browse files Browse the repository at this point in the history
When building in debug mode, the debug version of the MSVC CRT
gets linked in. (This is the default in CMake in general. In the case
of libcxx, we manually link the CRT though - and in debug mode,
we pick the debug version of the CRT.) When building the tests,
we need to use the same version of the CRT as was used for building
the library.

Additionally; the debug CRT defaults to pop up a dialog box when
asserts fail, which blocks running tests. By including the
set_windows_crt_report_mode.h helper header, we change the assert
behaviour back to that of release mode - printing a message and
exiting immediately.

This was supported by the old libcxx test system, where support for
it was added in 7e3ee09. When porting
over to the newer test setup, this mechanism wasn't brought over (and the
old test infrastructure was removed in
a48f018).

Thus: In debug mode, link against the debug versions of msvcrt and
msvcprt, define _DEBUG (enabling CRT debug mode code patterns),
and include the set_windows_crt_report_mode.h header.

Based on a patch by Andrew Ng.

Linking of the debug version of the CRT can also be done by using
the new -fms-runtime-lib= Clang option. However that option was
added in Clang 16, and libcxx only requires Clang 15 for now;
therefore doing the CRT linking entirely manually for now (just as
before).

Additionally, adjust set_windows_crt_report_mode.h to avoid including
the body of the file when building in C mode or in C++03 mode.
This fixes the following two tests:
  libcxx/include_as_c.sh.cpp
  libcxx/selftest/dsl/dsl.sh.py

The former test is built in C mode. The latter tries compiling things
as C++03. Some of the vcruntime headers that we include break in
C++03 mode when MS CRT debug mode is enabled.

Differential Revision: https://reviews.llvm.org/D155554
  • Loading branch information
mstorsjo committed Jul 28, 2023
1 parent c57d249 commit e346fd8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 6 deletions.
11 changes: 9 additions & 2 deletions libcxx/test/configs/llvm-libc++-shared-clangcl.cfg.in
Expand Up @@ -3,12 +3,19 @@

lit_config.load_config(config, '@CMAKE_CURRENT_BINARY_DIR@/cmake-bridge.cfg')

dbg_include = ''
lib_suffix = ''

if '@uppercase_CMAKE_BUILD_TYPE@' == 'DEBUG':
dbg_include = ' -D_DEBUG -include set_windows_crt_report_mode.h'
lib_suffix = 'd'

config.substitutions.append(('%{flags}', '--driver-mode=g++'))
config.substitutions.append(('%{compile_flags}',
'-nostdinc++ -I %{include} -I %{target-include} -I %{libcxx}/test/support -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_STDIO_ISO_WIDE_SPECIFIERS -DNOMINMAX'
'-nostdinc++ -I %{include} -I %{target-include} -I %{libcxx}/test/support -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_STDIO_ISO_WIDE_SPECIFIERS -DNOMINMAX' + dbg_include
))
config.substitutions.append(('%{link_flags}',
'-nostdlib -L %{lib} -lc++ -lmsvcrt -lmsvcprt -loldnames'
'-nostdlib -L %%{lib} -lc++ -lmsvcrt%s -lmsvcprt%s -loldnames' % (lib_suffix, lib_suffix)
))
config.substitutions.append(('%{exec}',
'%{executor} --execdir %T --prepend_env PATH=%{lib} -- '
Expand Down
Expand Up @@ -4,12 +4,19 @@

lit_config.load_config(config, '@CMAKE_CURRENT_BINARY_DIR@/cmake-bridge.cfg')

dbg_include = ''
lib_suffix = ''

if '@uppercase_CMAKE_BUILD_TYPE@' == 'DEBUG':
dbg_include = ' -D_DEBUG -include set_windows_crt_report_mode.h'
lib_suffix = 'd'

config.substitutions.append(('%{flags}', '--driver-mode=g++'))
config.substitutions.append(('%{compile_flags}',
'-nostdinc++ -I %{include} -I %{target-include} -I %{libcxx}/test/support -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_STDIO_ISO_WIDE_SPECIFIERS -DNOMINMAX -D_HAS_EXCEPTIONS=0'
'-nostdinc++ -I %{include} -I %{target-include} -I %{libcxx}/test/support -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_STDIO_ISO_WIDE_SPECIFIERS -DNOMINMAX -D_HAS_EXCEPTIONS=0' + dbg_include
))
config.substitutions.append(('%{link_flags}',
'-nostdlib -L %{lib} -lc++ -lmsvcrt -lmsvcprt -loldnames'
'-nostdlib -L %%{lib} -lc++ -lmsvcrt%s -lmsvcprt%s -loldnames' % (lib_suffix, lib_suffix)
))
config.substitutions.append(('%{exec}',
'%{executor} --execdir %T --prepend_env PATH=%{lib} -- '
Expand Down
11 changes: 9 additions & 2 deletions libcxx/test/configs/llvm-libc++-static-clangcl.cfg.in
Expand Up @@ -3,12 +3,19 @@

lit_config.load_config(config, '@CMAKE_CURRENT_BINARY_DIR@/cmake-bridge.cfg')

dbg_include = ''
lib_suffix = ''

if '@uppercase_CMAKE_BUILD_TYPE@' == 'DEBUG':
dbg_include = ' -D_DEBUG -include set_windows_crt_report_mode.h'
lib_suffix = 'd'

config.substitutions.append(('%{flags}', '--driver-mode=g++'))
config.substitutions.append(('%{compile_flags}',
'-nostdinc++ -I %{include} -I %{target-include} -I %{libcxx}/test/support -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_STDIO_ISO_WIDE_SPECIFIERS -DNOMINMAX'
'-nostdinc++ -I %{include} -I %{target-include} -I %{libcxx}/test/support -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_STDIO_ISO_WIDE_SPECIFIERS -DNOMINMAX' + dbg_include
))
config.substitutions.append(('%{link_flags}',
'-nostdlib -L %{lib} -llibc++ -lmsvcrt -lmsvcprt -loldnames'
'-nostdlib -L %%{lib} -llibc++ -lmsvcrt%s -lmsvcprt%s -loldnames' % (lib_suffix, lib_suffix)
))
config.substitutions.append(('%{exec}',
'%{executor} --execdir %T -- '
Expand Down
9 changes: 9 additions & 0 deletions libcxx/test/support/set_windows_crt_report_mode.h
Expand Up @@ -10,6 +10,14 @@
#ifndef SUPPORT_SET_WINDOWS_CRT_REPORT_MODE_H
#define SUPPORT_SET_WINDOWS_CRT_REPORT_MODE_H

// A few tests are built in C mode or in C++03 mode. The initialization
// of init_crt_anchor is a C++ feature, and <crtdbg.h> ends up including
// MSVC header code which breaks in C++03 mode. Therefore, only expand
// the body of this header when included in C++ >= 11 mode. As this file
// is included in every single translation unit, we're intentionally not
// including test_macros.h (for TEST_STD_VER) but try to keep it to the
// bare minimum.
#if defined(__cplusplus) && __cplusplus > 199711L
#ifndef _DEBUG
#error _DEBUG must be defined when using this header
#endif
Expand All @@ -32,5 +40,6 @@ inline int init_crt_report_mode() {
}

static int init_crt_anchor = init_crt_report_mode();
#endif // defined(__cplusplus) && __cplusplus > 199711L

#endif // SUPPORT_SET_WINDOWS_CRT_REPORT_MODE_H
13 changes: 13 additions & 0 deletions libcxx/utils/ci/buildkite-pipeline.yml
Expand Up @@ -764,6 +764,19 @@ steps:
- exit_status: -1 # Agent was lost
limit: 2

- label: "Clang-cl (Debug mode)"
command: "bash libcxx/utils/ci/run-buildbot clang-cl-debug"
artifact_paths:
- "**/test-results.xml"
- "**/*.abilist"
agents:
queue: "windows"
retry:
automatic:
- exit_status: -1 # Agent was lost
limit: 2
timeout_in_minutes: 120

- label: "MinGW (DLL, x86_64)"
command: "bash libcxx/utils/ci/run-buildbot mingw-dll"
artifact_paths:
Expand Down
7 changes: 7 additions & 0 deletions libcxx/utils/ci/run-buildbot
Expand Up @@ -639,6 +639,13 @@ clang-cl-no-vcruntime)
echo "+++ Running the libc++ tests"
${NINJA} -vC "${BUILD_DIR}" check-cxx
;;
clang-cl-debug)
clean
generate-cmake-libcxx-win -DLIBCXX_TEST_PARAMS="enable_experimental=False" \
-DCMAKE_BUILD_TYPE=Debug
echo "+++ Running the libc++ tests"
${NINJA} -vC "${BUILD_DIR}" check-cxx
;;
mingw-dll)
clean
# Explicitly specify the compiler with a triple prefix. The CI
Expand Down

0 comments on commit e346fd8

Please sign in to comment.