Skip to content

Commit c91254d

Browse files
authored
[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 reduces the fixes check-all failures to one 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 ``` Reviewed By: vitalybuka, delcypher, MaskRay Pull Request: #83088
1 parent a612524 commit c91254d

File tree

7 files changed

+96
-32
lines changed

7 files changed

+96
-32
lines changed

compiler-rt/CMakeLists.txt

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

587+
option(COMPILER_RT_TEST_STANDALONE_BUILD_LIBS
588+
"When set to ON and testing in a standalone build, test the runtime \
589+
libraries built by this standalone build rather than the runtime libraries \
590+
shipped with the compiler (used for testing). When set to OFF and testing \
591+
in a standalone build, test the runtime libraries shipped with the compiler \
592+
(used for testing). This option has no effect if the compiler and this \
593+
build are configured to use the same runtime library path."
594+
ON)
595+
if (COMPILER_RT_TEST_STANDALONE_BUILD_LIBS)
596+
# Ensure that the unit tests can find the sanitizer headers prior to installation.
597+
list(APPEND COMPILER_RT_UNITTEST_CFLAGS "-I${CMAKE_CURRENT_LIST_DIR}/include")
598+
# Ensure that unit tests link against the just-built runtime libraries instead
599+
# of the ones bundled with the compiler by overriding the resource directory.
600+
#
601+
if ("${COMPILER_RT_TEST_COMPILER_ID}" MATCHES "Clang")
602+
list(APPEND COMPILER_RT_UNITTEST_LINK_FLAGS "-resource-dir=${COMPILER_RT_OUTPUT_DIR}")
603+
endif()
604+
get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} rtlib_dir)
605+
list(APPEND COMPILER_RT_UNITTEST_LINK_FLAGS "-Wl,-rpath,${rtlib_dir}")
606+
endif()
607+
587608
if(COMPILER_RT_USE_LLVM_UNWINDER)
588609
# We're linking directly against the libunwind that we're building so don't
589610
# 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
@@ -106,6 +106,7 @@ def generate_compiler_cmd(is_cpp=True, fuzzer_enabled=True, msan_enabled=False):
106106
return " ".join(
107107
[
108108
compiler_cmd,
109+
config.target_cflags,
109110
std_cmd,
110111
"-O2 -gline-tables-only",
111112
sanitizers_cmd,

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

Lines changed: 64 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,51 @@ 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 and config.test_standalone_build_libs:
188+
if 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+
else:
207+
lit_config.warning(
208+
f"Cannot override compiler-rt library directory with non-Clang "
209+
f"compiler: {config.compiler_id}"
210+
)
211+
212+
171213
# Ask the compiler for the path to libraries it is going to use. If this
172214
# doesn't match config.compiler_rt_libdir then it means we might be testing the
173215
# compiler's own runtime libraries rather than the ones we just built.
174-
# Warn about about this and handle appropriately.
216+
# Warn about this and handle appropriately.
175217
compiler_libdir = find_compiler_libdir()
176218
if compiler_libdir:
177219
compiler_rt_libdir_real = os.path.realpath(config.compiler_rt_libdir)
@@ -182,7 +224,7 @@ def push_dynamic_library_lookup_path(config, new_path):
182224
f'compiler-rt libdir: "{compiler_rt_libdir_real}"'
183225
)
184226
if config.test_standalone_build_libs:
185-
# Use just built runtime libraries, i.e. the the libraries this built just built.
227+
# Use just built runtime libraries, i.e. the libraries this build just built.
186228
if not config.test_suite_supports_overriding_runtime_lib_path:
187229
# Test suite doesn't support this configuration.
188230
# 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)