diff --git a/compiler-rt/test/CMakeLists.txt b/compiler-rt/test/CMakeLists.txt --- a/compiler-rt/test/CMakeLists.txt +++ b/compiler-rt/test/CMakeLists.txt @@ -1,6 +1,13 @@ # Needed for lit support in standalone builds. include(AddLLVM) +option(COMPILER_RT_TEST_BUILT_LIBS + "When testing use the just built runtime libraries instead of the compiler's. \ + This option has no effect when the compiler-rt build library directory is \ + the same as the compiler's resource library directory." + ON) +pythonize_bool(COMPILER_RT_TEST_BUILT_LIBS) + pythonize_bool(LLVM_ENABLE_EXPENSIVE_CHECKS) configure_compiler_rt_lit_site_cfg( ${CMAKE_CURRENT_SOURCE_DIR}/lit.common.configured.in diff --git a/compiler-rt/test/builtins/Unit/lit.site.cfg.py.in b/compiler-rt/test/builtins/Unit/lit.site.cfg.py.in --- a/compiler-rt/test/builtins/Unit/lit.site.cfg.py.in +++ b/compiler-rt/test/builtins/Unit/lit.site.cfg.py.in @@ -7,6 +7,7 @@ config.is_msvc = @MSVC_PYBOOL@ config.builtins_is_msvc = @BUILTINS_IS_MSVC_PYBOOL@ config.builtins_lit_source_features = "@BUILTINS_LIT_SOURCE_FEATURES@" +config.test_suite_supports_testing_built_libs = True # Load common config for all compiler-rt lit tests. lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/test/lit.common.configured") diff --git a/compiler-rt/test/lit.common.cfg.py b/compiler-rt/test/lit.common.cfg.py --- a/compiler-rt/test/lit.common.cfg.py +++ b/compiler-rt/test/lit.common.cfg.py @@ -21,6 +21,41 @@ import pipes sh_quote = pipes.quote +def find_compiler_libdir(): + """ + Returns the path to library resource directory used + by the compiler. + """ + if config.compiler_id != 'Clang': + # TODO: Support other compilers. + return None + clang_cmd = [ + config.clang.strip(), + '-print-file-name=lib', + ] + try: + result = subprocess.run( + clang_cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + check=True + ) + except subprocess.CalledProcessError as e: + msg = f'Failed to run {clang_cmd}\nrc:{e.returncode}\nstdout:{e.stdout}\ne.stderr{e.stderr}' + lit_config.fatal(msg) + path = result.stdout.decode().strip() + if not os.path.exists(path): + lit_config.fatal(f'Path reported by clang does not exist: {path}') + path = os.path.realpath(path) + + # TODO: Support other platforms + if config.host_os == 'Darwin': + path = os.path.join(path, 'darwin') + else: + return None + return os.path.realpath(path) + + # Choose between lit's internal shell pipeline runner and a real shell. If # LIT_USE_INTERNAL_SHELL is in the environment, we use that as an override. use_lit_shell = os.environ.get("LIT_USE_INTERNAL_SHELL") @@ -63,6 +98,35 @@ # Add compiler ID to the list of available features. config.available_features.add(compiler_id) +# Ask the compiler for the path to libraries it is going to use. If this +# doesn't match config.compiler_rt_libdir then it means we will be testing the +# compiler's own runtime libraries rather than the ones we built. +# Warn about about this and handle appropriately. +compiler_libdir = find_compiler_libdir() +if compiler_libdir: + compiler_rt_libdir_real = os.path.realpath(config.compiler_rt_libdir) + if compiler_libdir != compiler_rt_libdir_real: + lit_config.warning( + 'Compiler lib dir != compiler-rt lib dir\n' + f'Compiler libdir: {compiler_libdir}\n' + f'compiler-rt lib dir: {compiler_rt_libdir_real}') + if config.test_built_libs: + # Use just built runtime libraries + if not config.test_suite_supports_testing_built_libs: + # Test suite doesn't support this configuration. + lit_config.fatal( + 'The compiler being used for testing is not using runtime ' + 'libraries created by this build but testing is configured to test ' + 'the built libraries (COMPILER_RT_TEST_BUILT_LIBS is ON). ' + 'Testing cannot proceed. To fix this modify this test suite to support this testing configuration.' + 'Alternatively set COMPILER_RT_TEST_BUILT_LIBS to OFF to test the ' + 'runtime libraries of the compiler instead.' + ) + else: + # Use Compiler's resource library directory instead. + config.compiler_rt_libdir = compiler_libdir + lit_config.warning(f'Testing using libraries in {config.compiler_rt_libdir}') + # If needed, add cflag for shadow scale. if config.asan_shadow_scale != '': config.target_cflags += " -mllvm -asan-mapping-scale=" + config.asan_shadow_scale diff --git a/compiler-rt/test/lit.common.configured.in b/compiler-rt/test/lit.common.configured.in --- a/compiler-rt/test/lit.common.configured.in +++ b/compiler-rt/test/lit.common.configured.in @@ -46,6 +46,10 @@ set_default("have_rpc_xdr_h", @HAVE_RPC_XDR_H@) set_default("gwp_asan", @COMPILER_RT_HAS_GWP_ASAN_PYBOOL@) set_default("expensive_checks", @LLVM_ENABLE_EXPENSIVE_CHECKS_PYBOOL@) +set_default("test_built_libs", @COMPILER_RT_TEST_BUILT_LIBS_PYBOOL@) +# True iff the test suite will use just built libs even if the compiler +# defaults to using its own different libs. +set_default("test_suite_supports_testing_built_libs", False) config.available_features.add('target-is-%s' % config.target_arch) if config.enable_per_target_runtime_dir: