Skip to content

[runtimes] Allow building against an installed LLVM tree #86209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

arichardson
Copy link
Member

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 \
  -DLLVM_BINARY_DIR=/tmp/upstream-llvm-readonly \
  -G Ninja -S ~/upstream-llvm-project/runtimes \
  -B ~/upstream-llvm-project/runtimes/cmake-build-debug-llvm-git

Created using spr 1.3.6-beta.1

[skip ci]
Created using spr 1.3.6-beta.1
@arichardson arichardson requested a review from a team as a code owner March 21, 2024 22:23
@llvmbot llvmbot added cmake Build system in general and CMake in particular compiler-rt compiler-rt:hwasan Hardware-assisted address sanitizer compiler-rt:sanitizer labels Mar 21, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Alexander Richardson (arichardson)

Changes

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 \
  -DLLVM_BINARY_DIR=/tmp/upstream-llvm-readonly \
  -G Ninja -S ~/upstream-llvm-project/runtimes \
  -B ~/upstream-llvm-project/runtimes/cmake-build-debug-llvm-git

Full diff: https://github.com/llvm/llvm-project/pull/86209.diff

4 Files Affected:

  • (modified) compiler-rt/cmake/Modules/AddCompilerRT.cmake (+1)
  • (modified) compiler-rt/test/hwasan/lit.cfg.py (+7)
  • (modified) compiler-rt/test/lit.common.configured.in (+1)
  • (modified) runtimes/CMakeLists.txt (+18-10)
diff --git a/compiler-rt/cmake/Modules/AddCompilerRT.cmake b/compiler-rt/cmake/Modules/AddCompilerRT.cmake
index 567b123b7abcac..4c3752d14a4789 100644
--- a/compiler-rt/cmake/Modules/AddCompilerRT.cmake
+++ b/compiler-rt/cmake/Modules/AddCompilerRT.cmake
@@ -769,6 +769,7 @@ function(configure_compiler_rt_lit_site_cfg input output)
 
   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_TEST_COMPILER ${COMPILER_RT_TEST_COMPILER})
   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_OUTPUT_DIR ${COMPILER_RT_OUTPUT_DIR})
+  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_EXEC_OUTPUT_DIR ${COMPILER_RT_EXEC_OUTPUT_DIR})
   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR ${output_dir})
 
   configure_lit_site_cfg(${input} ${output})
diff --git a/compiler-rt/test/hwasan/lit.cfg.py b/compiler-rt/test/hwasan/lit.cfg.py
index 594f3294a84ac1..96cc2f477ad445 100644
--- a/compiler-rt/test/hwasan/lit.cfg.py
+++ b/compiler-rt/test/hwasan/lit.cfg.py
@@ -2,6 +2,9 @@
 
 import os
 
+from lit.llvm import llvm_config
+from lit.llvm.subst import ToolSubst, FindTool
+
 # Setup config name.
 config.name = "HWAddressSanitizer" + getattr(config, "name_suffix", "default")
 
@@ -74,6 +77,10 @@ def build_invocation(compile_flags):
     ("%env_hwasan_opts=", "env HWASAN_OPTIONS=" + default_hwasan_opts_str)
 )
 
+# Ensure that we can use hwasan_symbolize from the expected location
+llvm_config.add_tool_substitutions([ToolSubst("hwasan_symbolize", unresolved="fatal")],
+                                   search_dirs=[config.compiler_rt_bindir])
+
 # Default test suffixes.
 config.suffixes = [".c", ".cpp"]
 
diff --git a/compiler-rt/test/lit.common.configured.in b/compiler-rt/test/lit.common.configured.in
index 8955efa3eafd18..e146e847aa00e6 100644
--- a/compiler-rt/test/lit.common.configured.in
+++ b/compiler-rt/test/lit.common.configured.in
@@ -28,6 +28,7 @@ set_default("python_executable", "@Python3_EXECUTABLE@")
 set_default("compiler_rt_debug", @COMPILER_RT_DEBUG_PYBOOL@)
 set_default("compiler_rt_intercept_libdispatch", @COMPILER_RT_INTERCEPT_LIBDISPATCH_PYBOOL@)
 set_default("compiler_rt_output_dir", "@COMPILER_RT_RESOLVED_OUTPUT_DIR@")
+set_default("compiler_rt_bindir", "@COMPILER_RT_RESOLVED_EXEC_OUTPUT_DIR@")
 set_default("compiler_rt_libdir", "@COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR@")
 set_default("emulator", "@COMPILER_RT_EMULATOR@")
 set_default("asan_shadow_scale", "@COMPILER_RT_ASAN_SHADOW_SCALE@")
diff --git a/runtimes/CMakeLists.txt b/runtimes/CMakeLists.txt
index 6f24fbcccec955..3195a293c0adfa 100644
--- a/runtimes/CMakeLists.txt
+++ b/runtimes/CMakeLists.txt
@@ -218,6 +218,22 @@ foreach(entry ${runtimes})
 endforeach()
 
 if(LLVM_INCLUDE_TESTS)
+  # Add lit if needed before adding any runtimes since their CMake tests
+  # configuration might depend on lit being present.
+  if (NOT HAVE_LLVM_LIT)
+    # If built by manually invoking cmake on this directory, we don't have
+    # llvm-lit. If invoked via llvm/runtimes, the toplevel llvm cmake
+    # invocation already generated the llvm-lit script.
+    set(LLVM_LIT_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin)
+    add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit
+                     ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
+    # Ensure that the testsuites use the local lit rather than
+    # LLVM_INSTALL_DIR/bin/llvm-lit (which may not exist if LLVM_BINARY_DIR
+    # points at an installed LLVM tree rather than a build tree.
+    get_llvm_lit_path(_base_dir _file_name)
+    set(LLVM_EXTERNAL_LIT "${_base_dir}/${_file_name}" CACHE STRING "Command used to spawn lit" FORCE)
+  endif()
+
   set(LIT_ARGS_DEFAULT "-sv --show-xfail --show-unsupported")
   if (MSVC OR XCODE)
     set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
@@ -251,14 +267,6 @@ if(LLVM_INCLUDE_TESTS)
   # and we know the total set of lit testsuites.
   umbrella_lit_testsuite_end(check-runtimes)
 
-  if (NOT HAVE_LLVM_LIT)
-    # If built by manually invoking cmake on this directory, we don't have
-    # llvm-lit. If invoked via llvm/runtimes, the toplevel llvm cmake
-    # invocation already generated the llvm-lit script.
-    add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit
-                     ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
-  endif()
-
   get_property(LLVM_RUNTIMES_LIT_TESTSUITES GLOBAL PROPERTY LLVM_RUNTIMES_LIT_TESTSUITES)
   string(REPLACE ";" "\n" LLVM_RUNTIMES_LIT_TESTSUITES "${LLVM_RUNTIMES_LIT_TESTSUITES}")
   file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/lit.tests ${LLVM_RUNTIMES_LIT_TESTSUITES})
@@ -288,10 +296,10 @@ if(SUB_COMPONENTS)
   if(LLVM_RUNTIMES_TARGET)
     configure_file(
       ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
-      ${LLVM_BINARY_DIR}/runtimes/${LLVM_RUNTIMES_TARGET}/Components.cmake)
+      ${CMAKE_CURRENT_BINARY_DIR}/runtimes/${LLVM_RUNTIMES_TARGET}/Components.cmake)
   else()
     configure_file(
       ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
-      ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
+      ${CMAKE_CURRENT_BINARY_DIR}/runtimes/Components.cmake)
   endif()
 endif()

clementval and others added 2 commits April 12, 2024 14:37
Created using spr 1.3.6-beta.1

[skip ci]
Created using spr 1.3.6-beta.1
Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but I think @petrhosek should also have a look.

@vitalybuka vitalybuka removed their request for review July 17, 2024 22:51
vporpo and others added 2 commits August 26, 2024 15:38
Created using spr 1.3.6-beta.1

[skip ci]
Created using spr 1.3.6-beta.1
@arichardson
Copy link
Member Author

@petrhosek does this look ok to you?

Copy link

github-actions bot commented Aug 26, 2024

✅ With the latest revision this PR passed the Python code formatter.

Copy link

⚠️ Python code formatter, darker found issues in your code. ⚠️

You can test this locally with the following command:
darker --check --diff -r e288141b33c763e22329e74af1e2cf1d3e7f2912...5e4d38daffa93fa758a13faffdbeefa21ddec6c8 compiler-rt/test/hwasan/lit.cfg.py
View the diff from darker here.
--- lit.cfg.py	2024-03-21 22:23:13.000000 +0000
+++ lit.cfg.py	2024-08-26 22:42:44.210846 +0000
@@ -76,12 +76,14 @@
 config.substitutions.append(
     ("%env_hwasan_opts=", "env HWASAN_OPTIONS=" + default_hwasan_opts_str)
 )
 
 # Ensure that we can use hwasan_symbolize from the expected location
-llvm_config.add_tool_substitutions([ToolSubst("hwasan_symbolize", unresolved="fatal")],
-                                   search_dirs=[config.compiler_rt_bindir])
+llvm_config.add_tool_substitutions(
+    [ToolSubst("hwasan_symbolize", unresolved="fatal")],
+    search_dirs=[config.compiler_rt_bindir],
+)
 
 # Default test suffixes.
 config.suffixes = [".c", ".cpp"]
 
 if config.host_os not in ["Linux", "Android"] or not config.has_lld:

arichardson and others added 4 commits August 26, 2024 15:50
Created using spr 1.3.6-beta.1
Created using spr 1.3.6-beta.1

[skip ci]
Created using spr 1.3.6-beta.1
Created using spr 1.3.6-beta.1
@arichardson arichardson changed the base branch from users/arichardson/spr/main.runtimes-allow-building-against-an-installed-llvm-tree to main October 25, 2024 00:35
@arichardson arichardson merged commit b1be213 into main Oct 25, 2024
60 of 62 checks passed
@arichardson arichardson deleted the users/arichardson/spr/runtimes-allow-building-against-an-installed-llvm-tree branch October 25, 2024 00:35
@huixie90
Copy link
Contributor

Hello, After this commit, I can no longer run libc++ tests with llvm-lit anymore.

fatal: You seem to be running Lit directly -- you should be running Lit through /bin/llvm-lit, which will ensure that the right Lit configuration file is used. See https://libcxx.llvm.org/TestingLibcxx.html#usage for how to run the libc++ tests.

@arichardson
Copy link
Member Author

Hello, After this commit, I can no longer run libc++ tests with llvm-lit anymore.

fatal: You seem to be running Lit directly -- you should be running Lit through /bin/llvm-lit, which will ensure that the right Lit configuration file is used. See https://libcxx.llvm.org/TestingLibcxx.html#usage for how to run the libc++ tests.

Can you send me the commands you are running? Also let me know what ninja check-all -v -n prints.

@huixie90
Copy link
Contributor

huixie90 commented Oct 26, 2024

Hello, After this commit, I can no longer run libc++ tests with llvm-lit anymore.

fatal: You seem to be running Lit directly -- you should be running Lit through /bin/llvm-lit, which will ensure that the right Lit configuration file is used. See https://libcxx.llvm.org/TestingLibcxx.html#usage for how to run the libc++ tests.

Can you send me the commands you are running? Also let me know what ninja check-all -v -n prints.

I usually run this under my build directory

./bin/llvm-lit -sv  --param std=c++23  ../libcxx/test/std

It no longer works.

Also I tried to use the libcxx/utils/libcxx-lit it failed with the same error.

But if I revert your commit, everything works again

I don't use ninja. I just used plain cmake -G "Unix Makefiles"

basically I did this
https://libcxx.llvm.org/TestingLibcxx.html

@arichardson
Copy link
Member Author

Hello, After this commit, I can no longer run libc++ tests with llvm-lit anymore.

fatal: You seem to be running Lit directly -- you should be running Lit through /bin/llvm-lit, which will ensure that the right Lit configuration file is used. See https://libcxx.llvm.org/TestingLibcxx.html#usage for how to run the libc++ tests.

Can you send me the commands you are running? Also let me know what ninja check-all -v -n prints.

I usually run this under my build directory

./bin/llvm-lit -sv  --param std=c++23  ../libcxx/test/std

It no longer works.

Also I tried to use the libcxx/utils/libcxx-lit it failed with the same error.

But if I revert your commit, everything works again

I don't use ninja. I just used plain cmake -G "Unix Makefiles"

basically I did this https://libcxx.llvm.org/TestingLibcxx.html

I'd need your cmake command line as well to reproduce this.

@arichardson
Copy link
Member Author

Or at least the output from make -n check-all>

@huixie90
Copy link
Contributor

Basically the steps in
https://libcxx.llvm.org/VendorDocumentation.html#vendordocumentation
https://libcxx.llvm.org/TestingLibcxx.html

cmake -G "Unix Makefiles" -S runtimes -B build_flat_multimap -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" -DLIBCXX_INCLUDE_TESTS:BOOL=TRUE -DLIBCXX_TEST_PARAMS="enable_modules=clang" -DCMAKE_CXX_COMPILER="/usr/local/bin/clang++" -DPython3_EXECUTABLE="/usr/local/bin/python"

cd build_flat_multimap
make cxx cxxabi cxx_experimental -j12
make cxx-test-depends
./bin/llvm-lit -sv  --param std=c++23  ../libcxx/test/std

The output of make -n check-all

make -n check-all
make: *** No rule to make target `check-all'.  Stop.

@arichardson
Copy link
Member Author

Basically the steps in https://libcxx.llvm.org/VendorDocumentation.html#vendordocumentation https://libcxx.llvm.org/TestingLibcxx.html

cmake -G "Unix Makefiles" -S runtimes -B build_flat_multimap -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" -DLIBCXX_INCLUDE_TESTS:BOOL=TRUE -DLIBCXX_TEST_PARAMS="enable_modules=clang" -DCMAKE_CXX_COMPILER="/usr/local/bin/clang++" -DPython3_EXECUTABLE="/usr/local/bin/python"

cd build_flat_multimap
make cxx cxxabi cxx_experimental -j12
make cxx-test-depends
./bin/llvm-lit -sv  --param std=c++23  ../libcxx/test/std

The output of make -n check-all

make -n check-all
make: *** No rule to make target `check-all'.  Stop.

Thanks, I'll take a look and will try to fix this. Unrelated but if you use -G Ninja the build should be a little bit faster.

philnik777 added a commit that referenced this pull request Oct 28, 2024
…13920)

Reverts #86209

This patch breaks running tests locally, which is extremely disruptive
to libc++ development.
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
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 \
  -DLLVM_BINARY_DIR=/tmp/upstream-llvm-readonly \
  -G Ninja -S ~/upstream-llvm-project/runtimes \
  -B ~/upstream-llvm-project/runtimes/cmake-build-debug-llvm-git
```

Reviewed By: ldionne

Pull Request: llvm#86209
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
…vm#113920)

Reverts llvm#86209

This patch breaks running tests locally, which is extremely disruptive
to libc++ development.
arichardson added a commit that referenced this pull request Nov 25, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmake Build system in general and CMake in particular compiler-rt:hwasan Hardware-assisted address sanitizer compiler-rt:sanitizer compiler-rt
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants