Skip to content

Commit fd5bbd4

Browse files
committed
[compiler-rt] Allow running tests without installing first
Currently, the testsuite uses the default runtimes path to find the runtimes libraries which may or may not match the just-built runtimes. This change uses the -resource-dir flag for clang whenever COMPILER_RT_TEST_STANDALONE_BUILD_LIBS is set to ensure that we are actually testing the currently built libraries rather than the ones bundled with ${COMPILER_RT_TEST_COMPILER}. The existing logic works fine when clang and compiler-rt share the same build directory -DLLVM_ENABLE_PROJECTS=clang;compiler-rt, but when building compiler-rt separately we need to tell the compiler used for the tests where it can find the just-built libraries. This mostly fixes check-all in my configuration: ``` cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -G Ninja -DCMAKE_C_COMPILER=$HOME/output/upstream-llvm/bin/clang -DCMAKE_CXX_COMPILER=$HOME/output/upstream-llvm/bin/clang++ -DCOMPILER_RT_INCLUDE_TESTS=ON -DLLVM_EXTERNAL_LIT=$HOME/build/upstream-llvm-project-build/bin/llvm-lit -DLLVM_CMAKE_DIR=$HOME/output/upstream-llvm -DCOMPILER_RT_DEBUG=OFF -S $HOME/src/upstream-llvm-project/compiler-rt -B $HOME/src/upstream-llvm-project/compiler-rt/cmake-build-all-sanitizers ``` I am still seeing two test failures due to an msan error inside __gxx_personality_v0, but that is most likely due to an uninstrumented `/lib/x86_64-linux-gnu/libgcc_s.so.1` being used by these tests. Pull Request: llvm#83088
1 parent f52a294 commit fd5bbd4

File tree

7 files changed

+90
-32
lines changed

7 files changed

+90
-32
lines changed

compiler-rt/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,27 @@ string(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS " ${stdlib_flag}")
571571
string(REPLACE " " ";" COMPILER_RT_UNITTEST_CFLAGS "${COMPILER_RT_TEST_COMPILER_CFLAGS}")
572572
set(COMPILER_RT_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_CFLAGS})
573573

574+
option(COMPILER_RT_TEST_STANDALONE_BUILD_LIBS
575+
"When set to ON and testing in a standalone build, test the runtime \
576+
libraries built by this standalone build rather than the runtime libraries \
577+
shipped with the compiler (used for testing). When set to OFF and testing \
578+
in a standalone build, test the runtime libraries shipped with the compiler \
579+
(used for testing). This option has no effect if the compiler and this \
580+
build are configured to use the same runtime library path."
581+
ON)
582+
if (COMPILER_RT_TEST_STANDALONE_BUILD_LIBS)
583+
# Ensure that the unit tests can find the sanitizer headers prior to installation.
584+
list(APPEND COMPILER_RT_UNITTEST_CFLAGS "-I${CMAKE_CURRENT_LIST_DIR}/include")
585+
# Ensure that unit tests link against the just-built runtime libraries instead
586+
# of the ones bundled with the compiler by overriding the resource directory.
587+
#
588+
if ("${COMPILER_RT_TEST_COMPILER_ID}" MATCHES "Clang")
589+
list(APPEND COMPILER_RT_UNITTEST_LINK_FLAGS "-resource-dir=${CMAKE_CURRENT_BINARY_DIR}")
590+
endif()
591+
get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} output_dir)
592+
list(APPEND COMPILER_RT_UNITTEST_LINK_FLAGS "-Wl,-rpath,${output_dir}")
593+
endif()
594+
574595
if(COMPILER_RT_USE_LLVM_UNWINDER)
575596
# We're linking directly against the libunwind that we're building so don't
576597
# try to link in the toolchain's default libunwind which may be missing.

compiler-rt/cmake/Modules/AddCompilerRT.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ function(configure_compiler_rt_lit_site_cfg input output)
768768
get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} output_dir)
769769

770770
string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_TEST_COMPILER ${COMPILER_RT_TEST_COMPILER})
771+
string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_OUTPUT_DIR ${COMPILER_RT_OUTPUT_DIR})
771772
string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR ${output_dir})
772773

773774
configure_lit_site_cfg(${input} ${output})

compiler-rt/test/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
# Needed for lit support in standalone builds.
22
include(AddLLVM)
33

4-
option(COMPILER_RT_TEST_STANDALONE_BUILD_LIBS
5-
"When set to ON and testing in a standalone build, test the runtime \
6-
libraries built by this standalone build rather than the runtime libraries \
7-
shipped with the compiler (used for testing). When set to OFF and testing \
8-
in a standalone build, test the runtime libraries shipped with the compiler \
9-
(used for testing). This option has no effect if the compiler and this \
10-
build are configured to use the same runtime library path."
11-
ON)
124
pythonize_bool(COMPILER_RT_TEST_STANDALONE_BUILD_LIBS)
135

146
pythonize_bool(LLVM_ENABLE_EXPENSIVE_CHECKS)

compiler-rt/test/fuzzer/lit.cfg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def generate_compiler_cmd(is_cpp=True, fuzzer_enabled=True, msan_enabled=False):
101101
return " ".join(
102102
[
103103
compiler_cmd,
104+
config.target_cflags,
104105
std_cmd,
105106
"-O2 -gline-tables-only",
106107
sanitizers_cmd,

compiler-rt/test/lit.common.cfg.py

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@
1414
import lit.util
1515

1616

17+
def get_path_from_clang(args, allow_failure):
18+
clang_cmd = [
19+
config.clang.strip(),
20+
f"--target={config.target_triple}",
21+
*args,
22+
]
23+
path = None
24+
try:
25+
result = subprocess.run(
26+
clang_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True
27+
)
28+
path = result.stdout.decode().strip()
29+
except subprocess.CalledProcessError as e:
30+
msg = f"Failed to run {clang_cmd}\nrc:{e.returncode}\nstdout:{e.stdout}\ne.stderr{e.stderr}"
31+
if allow_failure:
32+
lit_config.warning(msg)
33+
else:
34+
lit_config.fatal(msg)
35+
return path, clang_cmd
36+
37+
1738
def find_compiler_libdir():
1839
"""
1940
Returns the path to library resource directory used
@@ -26,26 +47,6 @@ def find_compiler_libdir():
2647
# TODO: Support other compilers.
2748
return None
2849

29-
def get_path_from_clang(args, allow_failure):
30-
clang_cmd = [
31-
config.clang.strip(),
32-
f"--target={config.target_triple}",
33-
]
34-
clang_cmd.extend(args)
35-
path = None
36-
try:
37-
result = subprocess.run(
38-
clang_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True
39-
)
40-
path = result.stdout.decode().strip()
41-
except subprocess.CalledProcessError as e:
42-
msg = f"Failed to run {clang_cmd}\nrc:{e.returncode}\nstdout:{e.stdout}\ne.stderr{e.stderr}"
43-
if allow_failure:
44-
lit_config.warning(msg)
45-
else:
46-
lit_config.fatal(msg)
47-
return path, clang_cmd
48-
4950
# Try using `-print-runtime-dir`. This is only supported by very new versions of Clang.
5051
# so allow failure here.
5152
runtime_dir, clang_cmd = get_path_from_clang(
@@ -168,10 +169,45 @@ def push_dynamic_library_lookup_path(config, new_path):
168169
r"/i386(?=-[^/]+$)", "/x86_64", config.compiler_rt_libdir
169170
)
170171

172+
173+
# Check if the test compiler resource dir matches the local build directory
174+
# (which happens with -DLLVM_ENABLE_PROJECTS=clang;compiler-rt) or if we are
175+
# using an installed clang to test compiler-rt standalone. In the latter case
176+
# we may need to override the resource dir to match the path of the just-built
177+
# compiler-rt libraries.
178+
test_cc_resource_dir, _ = get_path_from_clang(
179+
shlex.split(config.target_cflags) + ["-print-resource-dir"], allow_failure=True
180+
)
181+
# Normalize the path for comparison
182+
if test_cc_resource_dir is not None:
183+
test_cc_resource_dir = os.path.realpath(test_cc_resource_dir)
184+
if lit_config.debug:
185+
lit_config.note(f"Resource dir for {config.clang} is {test_cc_resource_dir}")
186+
local_build_resource_dir = os.path.realpath(config.compiler_rt_output_dir)
187+
if test_cc_resource_dir != local_build_resource_dir:
188+
if config.test_standalone_build_libs and config.compiler_id == "Clang":
189+
if lit_config.debug:
190+
lit_config.note(
191+
f"Overriding test compiler resource dir to use "
192+
f'libraries in "{config.compiler_rt_libdir}"'
193+
)
194+
# Ensure that we use the just-built static libraries when linking by
195+
# overriding the Clang resource directory. Additionally, we want to use
196+
# the builtin headers shipped with clang (e.g. stdint.h), so we
197+
# explicitly add this as an include path (since the headers are not
198+
# going to be in the current compiler-rt build directory).
199+
# We also tell the linker to add an RPATH entry for the local library
200+
# directory so that the just-built shared libraries are used.
201+
config.target_cflags += f" -nobuiltininc"
202+
config.target_cflags += f" -I{config.compiler_rt_src_root}/include"
203+
config.target_cflags += f" -idirafter {test_cc_resource_dir}/include"
204+
config.target_cflags += f" -resource-dir={config.compiler_rt_output_dir}"
205+
config.target_cflags += f" -Wl,-rpath,{config.compiler_rt_libdir}"
206+
171207
# Ask the compiler for the path to libraries it is going to use. If this
172208
# doesn't match config.compiler_rt_libdir then it means we might be testing the
173209
# compiler's own runtime libraries rather than the ones we just built.
174-
# Warn about about this and handle appropriately.
210+
# Warn about this and handle appropriately.
175211
compiler_libdir = find_compiler_libdir()
176212
if compiler_libdir:
177213
compiler_rt_libdir_real = os.path.realpath(config.compiler_rt_libdir)
@@ -182,7 +218,7 @@ def push_dynamic_library_lookup_path(config, new_path):
182218
f'compiler-rt libdir: "{compiler_rt_libdir_real}"'
183219
)
184220
if config.test_standalone_build_libs:
185-
# Use just built runtime libraries, i.e. the the libraries this built just built.
221+
# Use just built runtime libraries, i.e. the libraries this build just built.
186222
if not config.test_suite_supports_overriding_runtime_lib_path:
187223
# Test suite doesn't support this configuration.
188224
# TODO(dliew): This should be an error but it seems several bots are

compiler-rt/test/lit.common.configured.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ set_default("compiler_id", "@COMPILER_RT_TEST_COMPILER_ID@")
2727
set_default("python_executable", "@Python3_EXECUTABLE@")
2828
set_default("compiler_rt_debug", @COMPILER_RT_DEBUG_PYBOOL@)
2929
set_default("compiler_rt_intercept_libdispatch", @COMPILER_RT_INTERCEPT_LIBDISPATCH_PYBOOL@)
30+
set_default("compiler_rt_output_dir", "@COMPILER_RT_RESOLVED_OUTPUT_DIR@")
3031
set_default("compiler_rt_libdir", "@COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR@")
3132
set_default("emulator", "@COMPILER_RT_EMULATOR@")
3233
set_default("asan_shadow_scale", "@COMPILER_RT_ASAN_SHADOW_SCALE@")

compiler-rt/test/safestack/lit.cfg.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@
1313

1414
# Add clang substitutions.
1515
config.substitutions.append(
16-
("%clang_nosafestack ", config.clang + " -O0 -fno-sanitize=safe-stack ")
16+
(
17+
"%clang_nosafestack ",
18+
config.clang + config.target_cflags + " -O0 -fno-sanitize=safe-stack ",
19+
)
1720
)
1821
config.substitutions.append(
19-
("%clang_safestack ", config.clang + " -O0 -fsanitize=safe-stack ")
22+
(
23+
"%clang_safestack ",
24+
config.clang + config.target_cflags + " -O0 -fsanitize=safe-stack ",
25+
)
2026
)
2127

2228
if config.lto_supported:

0 commit comments

Comments
 (0)