Skip to content

Commit 719b2ac

Browse files
authored
[libc++] Allow testing Apple's system library as it is installed (#99086)
In order to test libc++ under the "Apple System Library" configuration, we need to run the tests using DYLD_LIBRARY_PATH. This is required because libc++ gets an install_name of /usr/lib when built as a system library, which means that we must override the copy of libc++ used by the whole process. This effectively reverts 2cf2f1b, which was the wrong solution for the problem I was having. Of course, this assumes that the just-built libc++ is sufficient to replace the system library, which is not actually the case out-of-the-box. Indeed, the system library contains a few symbols that are not provided by the upstream library, leading to undefined symbols when replacing the system library by the just-built one. To solve this problem, we separately build shims that provide those missing symbols and we manually link against them when we build executables in the tests. While this is somewhat brittle, it provides a localized and unintrusive way to allow testing the Apple system configuration in an upstream environment, which has been a frequent request.
1 parent 06518ce commit 719b2ac

File tree

5 files changed

+177
-4
lines changed

5 files changed

+177
-4
lines changed

libcxx/cmake/caches/Apple.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ set(LIBCXXABI_HERMETIC_STATIC_LIBRARY ON CACHE BOOL "")
1515
set(LIBCXXABI_ENABLE_ASSERTIONS OFF CACHE BOOL "")
1616
set(LIBCXXABI_ENABLE_FORGIVING_DYNAMIC_CAST ON CACHE BOOL "")
1717
set(LIBCXXABI_USE_LLVM_UNWINDER OFF CACHE BOOL "") # libunwind is built separately
18+
19+
set(LIBCXX_TEST_CONFIG "apple-libc++-shared.cfg.in" CACHE STRING "")
20+
set(LIBCXXABI_TEST_CONFIG "apple-libc++abi-shared.cfg.in" CACHE STRING "")
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Testing configuration for Apple's system libc++.
2+
#
3+
# This configuration differs from a normal LLVM shared library configuration in
4+
# that we must use DYLD_LIBRARY_PATH to run the tests against the just-built library,
5+
# since Apple's libc++ has an absolute install_name.
6+
#
7+
# We also don't use a per-target include directory layout, so we have only one
8+
# include directory for the libc++ headers.
9+
#
10+
# Finally, we also link against an artificial shims library that provides
11+
# the functionality necessary for the upstream libc++ to be usable in place
12+
# of a system-provided libc++. Without that, attempting to replace the system
13+
# libc++ with DYLD_LIBRARY_PATH would result in missing symbols and other similar
14+
# issues since the upstream libc++ does not contain all the symbols provided by
15+
# the system library.
16+
17+
lit_config.load_config(config, '@CMAKE_CURRENT_BINARY_DIR@/cmake-bridge.cfg')
18+
19+
import os, site
20+
site.addsitedir(os.path.join('@LIBCXX_SOURCE_DIR@', 'utils'))
21+
import libcxx.test.params, libcxx.test.config, libcxx.test.dsl
22+
ADDITIONAL_PARAMETERS = [
23+
libcxx.test.dsl.Parameter(name='apple_system_shims', type=str,
24+
actions=lambda path: [libcxx.test.dsl.AddSubstitution('%{apple-system-shims}', path)],
25+
help="""
26+
Path to a pre-built object file or static archive that contains shims necessary to
27+
allow replacing the system libc++ by the just-built one.
28+
"""),
29+
]
30+
31+
config.substitutions.append(('%{flags}',
32+
'-isysroot {}'.format('@CMAKE_OSX_SYSROOT@') if '@CMAKE_OSX_SYSROOT@' else ''
33+
))
34+
config.substitutions.append(('%{compile_flags}',
35+
'-nostdinc++ -I %{include-dir} -I %{libcxx-dir}/test/support'
36+
))
37+
config.substitutions.append(('%{link_flags}',
38+
'-nostdlib++ -L %{lib-dir} -lc++ %{apple-system-shims}'
39+
))
40+
config.substitutions.append(('%{exec}',
41+
'%{executor} --execdir %T --env DYLD_LIBRARY_PATH=%{lib-dir} -- '
42+
))
43+
44+
config.stdlib = 'apple-libc++'
45+
46+
libcxx.test.config.configure(
47+
libcxx.test.params.DEFAULT_PARAMETERS + ADDITIONAL_PARAMETERS,
48+
libcxx.test.features.DEFAULT_FEATURES,
49+
config,
50+
lit_config
51+
)

libcxx/utils/ci/apple-install-libcxx.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,13 @@ for arch in ${architectures}; do
114114
# Then LLVM would guess the LLVM_DEFAULT_TARGET_TRIPLE properly and we wouldn't have to specify it.
115115
target=$(xcrun clang -arch ${arch} -xc - -### 2>&1 | grep --only-matching -E '"-triple" ".+?"' | grep --only-matching -E '"[^ ]+-apple-[^ ]+?"' | tr -d '"')
116116
117-
step "Building libc++.dylib and libc++abi.dylib for architecture ${arch}"
118117
mkdir -p "${build_dir}/${arch}"
118+
119+
step "Building shims to make libc++ compatible with the system libc++ on Apple platforms when running the tests"
120+
shims_library="${build_dir}/${arch}/apple-system-shims.a"
121+
xcrun clang++ -c -std=c++2b -target ${target} "${llvm_root}/libcxxabi/src/vendor/apple/shims.cpp" -static -o "${shims_library}"
122+
123+
step "Building libc++.dylib and libc++abi.dylib for architecture ${arch}"
119124
xcrun cmake -S "${llvm_root}/runtimes" \
120125
-B "${build_dir}/${arch}" \
121126
-GNinja \
@@ -127,9 +132,9 @@ for arch in ${architectures}; do
127132
-DCMAKE_OSX_ARCHITECTURES="${arch}" \
128133
-DLIBCXXABI_LIBRARY_VERSION="${version}" \
129134
-DLIBCXX_LIBRARY_VERSION="${version}" \
130-
-DLIBCXX_TEST_PARAMS="target_triple=${target};stdlib=apple-libc++" \
131-
-DLIBCXXABI_TEST_PARAMS="target_triple=${target};stdlib=apple-libc++" \
132-
-DLIBUNWIND_TEST_PARAMS="target_triple=${target};stdlib=apple-libc++"
135+
-DLIBCXX_TEST_PARAMS="target_triple=${target};apple_system_shims=${shims_library}" \
136+
-DLIBCXXABI_TEST_PARAMS="target_triple=${target};apple_system_shims=${shims_library}" \
137+
-DLIBUNWIND_TEST_PARAMS="target_triple=${target};apple_system_shims=${shims_library}"
133138
134139
if [ "$headers_only" = true ]; then
135140
xcrun cmake --build "${build_dir}/${arch}" --target install-cxx-headers install-cxxabi-headers -- -v

libcxxabi/src/vendor/apple/shims.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
//
10+
// These shims implement symbols that are present in the system libc++ on Apple platforms
11+
// but are not implemented in upstream libc++. This allows testing libc++ under a system
12+
// library configuration, which requires the just-built libc++ to be ABI compatible with
13+
// the system library it is replacing.
14+
//
15+
16+
#include <cstddef>
17+
#include <new>
18+
19+
namespace std { // purposefully not versioned, like align_val_t
20+
enum class __type_descriptor_t : unsigned long long;
21+
}
22+
23+
_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::__type_descriptor_t) {
24+
return ::operator new(__sz);
25+
}
26+
27+
_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t& __nt,
28+
std::__type_descriptor_t) noexcept {
29+
return ::operator new(__sz, __nt);
30+
}
31+
32+
_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::__type_descriptor_t) {
33+
return ::operator new[](__sz);
34+
}
35+
36+
_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t& __nt,
37+
std::__type_descriptor_t) noexcept {
38+
return ::operator new(__sz, __nt);
39+
}
40+
41+
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::__type_descriptor_t) noexcept {
42+
return ::operator delete(__p);
43+
}
44+
45+
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t& __nt,
46+
std::__type_descriptor_t) noexcept {
47+
return ::operator delete(__p, __nt);
48+
}
49+
50+
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::__type_descriptor_t) noexcept {
51+
return ::operator delete[](__p);
52+
}
53+
54+
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t& __nt,
55+
std::__type_descriptor_t) noexcept {
56+
return ::operator delete[](__p, __nt);
57+
}
58+
59+
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz, std::__type_descriptor_t) noexcept {
60+
return ::operator delete(__p, __sz);
61+
}
62+
63+
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz, std::__type_descriptor_t) noexcept {
64+
return ::operator delete[](__p, __sz);
65+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Testing configuration for Apple's system libc++abi.
2+
#
3+
# This configuration differs from a normal LLVM shared library configuration in
4+
# that we must use DYLD_LIBRARY_PATH to run the tests against the just-built library,
5+
# since Apple's libc++abi has an absolute install_name.
6+
#
7+
# Finally, we also link against an artificial shims library that provides
8+
# the functionality necessary for the upstream libc++abi to be usable in place
9+
# of a system-provided libc++abi. Without that, attempting to replace the system
10+
# libc++abi with DYLD_LIBRARY_PATH would result in missing symbols and other similar
11+
# issues since the upstream libc++abi does not contain all the symbols provided by
12+
# the system library.
13+
14+
lit_config.load_config(config, '@CMAKE_CURRENT_BINARY_DIR@/cmake-bridge.cfg')
15+
16+
import os, site
17+
site.addsitedir(os.path.join('@LIBCXXABI_LIBCXX_PATH@', 'utils'))
18+
import libcxx.test.params, libcxx.test.config, libcxx.test.dsl
19+
ADDITIONAL_PARAMETERS = [
20+
libcxx.test.dsl.Parameter(name='apple_system_shims', type=str,
21+
actions=lambda path: [libcxx.test.dsl.AddSubstitution('%{apple-system-shims}', path)],
22+
help="""
23+
Path to a pre-built object file or static archive that contains shims necessary to
24+
allow replacing the system libc++abi by the just-built one.
25+
"""),
26+
]
27+
28+
config.substitutions.append(('%{flags}',
29+
'-isysroot {}'.format('@CMAKE_OSX_SYSROOT@') if '@CMAKE_OSX_SYSROOT@' else ''
30+
))
31+
config.substitutions.append(('%{compile_flags}',
32+
'-nostdinc++ -I %{include} -I %{cxx-include} -I %{cxx-target-include} %{maybe-include-libunwind} -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS ' +
33+
'-I %{libcxx}/test/support -I %{libcxx}/src'
34+
))
35+
config.substitutions.append(('%{link_flags}',
36+
'-nostdlib++ -L %{lib} -lc++ %{apple-system-shims}'
37+
))
38+
config.substitutions.append(('%{exec}',
39+
'%{executor} --execdir %T --env DYLD_LIBRARY_PATH=%{lib} -- '
40+
))
41+
42+
config.stdlib = 'apple-libc++'
43+
44+
libcxx.test.config.configure(
45+
libcxx.test.params.DEFAULT_PARAMETERS + ADDITIONAL_PARAMETERS,
46+
libcxx.test.features.DEFAULT_FEATURES,
47+
config,
48+
lit_config
49+
)

0 commit comments

Comments
 (0)