Skip to content

Commit 3cb2852

Browse files
authored
Reapply "[runtimes] Allow building against an installed LLVM tree"
This relands #86209 which was reverted because ./bin/llvm no longer accepted test paths in the source tree instead of the build tree. This was happening because `add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit` was called before all tsst suites were registered, and therefore it was missing the source->build dir mappings. Original commit message: I am currently trying to test the LLVM runtimes (including compiler-rt) against an installed LLVM tree rather than a build tree (since that is no longer available). Currently, the runtimes build of compiler-rt assumes that LLVM_BINARY_DIR is writable since it uses configure_file() to write there during the CMake configure stage. Instead, generate this file inside CMAKE_CURRENT_BINARY_DIR, which will match LLVM_BINARY_DIR when invoked from llvm/runtimes/CMakeLists.txt. I also needed to make a minor change to the hwasan tests: hwasan_symbolize was previously found in the LLVM_BINARY_DIR, but since it is generated as part of the compiler-rt build it is now inside the CMake build directory instead. I fixed this by passing the output directory to lit as config.compiler_rt_bindir and using llvm_config.add_tool_substitutions(). For testing that we no longer write to the LLVM install directory as part of testing or configuration, I created a read-only bind mount and configured the runtimes builds as follows: ``` $ sudo mount --bind --read-only ~/llvm-install /tmp/upstream-llvm-readonly $ cmake -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_C_COMPILER=/tmp/upstream-llvm-readonly/bin/clang \ -DCMAKE_CXX_COMPILER=/tmp/upstream-llvm-readonly/bin/clang++ \ -DLLVM_INCLUDE_TESTS=TRUE -DLLVM_ENABLE_ASSERTIONS=TRUE \ -DCOMPILER_RT_INCLUDE_TESTS=TRUE -DCOMPILER_RT_DEBUG=OFF \ -DLLVM_ENABLE_RUNTIMES=compiler-rt \ -DCMAKE_DISABLE_FIND_PACKAGE_LLVM=TRUE \ -DCMAKE_DISABLE_FIND_PACKAGE_Clang=TRUE \ -G Ninja -S ~/upstream-llvm-project/runtimes \ -B ~/upstream-llvm-project/runtimes/cmake-build-debug-llvm-git ``` Pull Request: #114307
1 parent 1a86d44 commit 3cb2852

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

compiler-rt/cmake/Modules/AddCompilerRT.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ function(configure_compiler_rt_lit_site_cfg input output)
790790

791791
string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_TEST_COMPILER ${COMPILER_RT_TEST_COMPILER})
792792
string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_OUTPUT_DIR ${COMPILER_RT_OUTPUT_DIR})
793+
string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_EXEC_OUTPUT_DIR ${COMPILER_RT_EXEC_OUTPUT_DIR})
793794
string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR ${output_dir})
794795

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

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import os
44

5+
from lit.llvm import llvm_config
6+
from lit.llvm.subst import ToolSubst, FindTool
7+
58
# Setup config name.
69
config.name = "HWAddressSanitizer" + getattr(config, "name_suffix", "default")
710

@@ -74,6 +77,12 @@ def build_invocation(compile_flags):
7477
("%env_hwasan_opts=", "env HWASAN_OPTIONS=" + default_hwasan_opts_str)
7578
)
7679

80+
# Ensure that we can use hwasan_symbolize from the expected location
81+
llvm_config.add_tool_substitutions(
82+
[ToolSubst("hwasan_symbolize", unresolved="fatal")],
83+
search_dirs=[config.compiler_rt_bindir],
84+
)
85+
7786
# Default test suffixes.
7887
config.suffixes = [".c", ".cpp"]
7988

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ 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@)
3030
set_default("compiler_rt_output_dir", "@COMPILER_RT_RESOLVED_OUTPUT_DIR@")
31+
set_default("compiler_rt_bindir", "@COMPILER_RT_RESOLVED_EXEC_OUTPUT_DIR@")
3132
set_default("compiler_rt_libdir", "@COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR@")
3233
set_default("emulator", "@COMPILER_RT_EMULATOR@")
3334
set_default("asan_shadow_scale", "@COMPILER_RT_ASAN_SHADOW_SCALE@")

runtimes/CMakeLists.txt

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
# This file handles building LLVM runtime sub-projects.
22
cmake_minimum_required(VERSION 3.20.0)
33

4+
# This file can be used in two ways: the bootstrapping build calls it from
5+
# llvm/runtimes/CMakeLists.txt where we reuse the build tree of the top-level
6+
# build or it can be directly invoked in this directory. In the latter case we
7+
# might be building against a LLVM install tree and might not have a valid build
8+
# tree set up yet. We can detect whether we are using the bootstrapping build
9+
# by checking for the HAVE_LLVM_LIT flag that is passed explicitly to
10+
# llvm_ExternalProject_Add().
11+
if (HAVE_LLVM_LIT)
12+
message(STATUS "Performing bootstrapping runtimes build.")
13+
else()
14+
message(STATUS "Performing standalone runtimes build.")
15+
endif()
416
# Add path for custom and the LLVM build's modules to the CMake module path.
517
set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
618
include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
@@ -236,6 +248,25 @@ foreach(entry ${runtimes})
236248
endforeach()
237249

238250
if(LLVM_INCLUDE_TESTS)
251+
# If built with the runtimes build (rooted at runtimes/CMakeLists.txt), we
252+
# won't have llvm-lit. If built with the bootstrapping build (rooted at
253+
# llvm/CMakeLists.txt), the top-level llvm CMake invocation already generated
254+
# the llvm-lit script.
255+
if (NOT HAVE_LLVM_LIT)
256+
# Ensure that the appropriate variables for lit are set before adding any
257+
# runtimes since their CMake tests configuration might depend on lit being
258+
# present. This ensures that the testsuites use a local lit from the build
259+
# dir rather than ${LLVM_INSTALL_DIR}/bin/llvm-lit (which may not exist if
260+
# LLVM_BINARY_DIR points at an installed LLVM tree rather than a build tree).
261+
set(LLVM_LIT_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin)
262+
get_llvm_lit_path(_base_dir _file_name)
263+
set(LLVM_EXTERNAL_LIT "${_base_dir}/${_file_name}" CACHE STRING "Command used to spawn lit" FORCE)
264+
# Avoid warning about missing llvm-lit from runtimes CMake files. This is
265+
# fine since we call configure_file() to create llvm-lit at the end of this
266+
# file (after recursing into all runtimes' CMake logic), so it will exist.
267+
set(LLVM_EXTERNAL_LIT_MISSING_WARNED_ONCE YES CACHE INTERNAL "")
268+
endif()
269+
239270
set(LIT_ARGS_DEFAULT "-sv --show-xfail --show-unsupported")
240271
if (MSVC OR XCODE)
241272
set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
@@ -273,6 +304,8 @@ if(LLVM_INCLUDE_TESTS)
273304
# If built by manually invoking cmake on this directory, we don't have
274305
# llvm-lit. If invoked via llvm/runtimes, the toplevel llvm cmake
275306
# invocation already generated the llvm-lit script.
307+
# NOTE: this must be called after all testsuites have been added, since
308+
# otherwise the generated llvm-lit does not have all required path mappings.
276309
add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit
277310
${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
278311
endif()
@@ -306,10 +339,10 @@ if(SUB_COMPONENTS)
306339
if(LLVM_RUNTIMES_TARGET)
307340
configure_file(
308341
${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
309-
${LLVM_BINARY_DIR}/runtimes/${LLVM_RUNTIMES_TARGET}/Components.cmake)
342+
${CMAKE_CURRENT_BINARY_DIR}/runtimes/${LLVM_RUNTIMES_TARGET}/Components.cmake)
310343
else()
311344
configure_file(
312345
${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
313-
${LLVM_BINARY_DIR}/runtimes/Components.cmake)
346+
${CMAKE_CURRENT_BINARY_DIR}/runtimes/Components.cmake)
314347
endif()
315348
endif()

0 commit comments

Comments
 (0)