Skip to content

Conversation

@Sunday111
Copy link
Contributor

This patch improves support for embedded Linux toolchains with multiple sysroots, each containing different built-in libraries. If compiler-rt libraries are not found in the resource directory, the Clang driver will fallback to searching in the sysroot/lib directory.

This change preserves the standard behavior for regular Linux toolchains while allowing greater flexibility in toolchains with multiple sysroots.

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Mar 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Author: Kostiantyn Lazukin (Sunday111)

Changes

This patch improves support for embedded Linux toolchains with multiple sysroots, each containing different built-in libraries. If compiler-rt libraries are not found in the resource directory, the Clang driver will fallback to searching in the sysroot/lib directory.

This change preserves the standard behavior for regular Linux toolchains while allowing greater flexibility in toolchains with multiple sysroots.


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

9 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/Linux.cpp (+2)
  • (added) clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/Scrt1.o ()
  • (added) clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/clang_rt.crtbegin.o ()
  • (added) clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/clang_rt.crtend.o ()
  • (added) clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/crti.o ()
  • (added) clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/crtn.o ()
  • (added) clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/libclang_rt.builtins.a ()
  • (added) clang/test/Driver/Inputs/resource_dir_empty/keep ()
  • (added) clang/test/Driver/linux-musl-library-search.cpp (+14)
diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp
index 1e9bd3de75f04..1bd8daafa8091 100644
--- a/clang/lib/Driver/ToolChains/Linux.cpp
+++ b/clang/lib/Driver/ToolChains/Linux.cpp
@@ -354,6 +354,8 @@ Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
 
   addPathIfExists(D, concat(SysRoot, "/lib"), Paths);
   addPathIfExists(D, concat(SysRoot, "/usr/lib"), Paths);
+
+  addPathIfExists(D, concat(SysRoot, "/lib"), getLibraryPaths());
 }
 
 ToolChain::RuntimeLibType Linux::GetDefaultRuntimeLibType() const {
diff --git a/clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/Scrt1.o b/clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/Scrt1.o
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/clang_rt.crtbegin.o b/clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/clang_rt.crtbegin.o
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/clang_rt.crtend.o b/clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/clang_rt.crtend.o
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/crti.o b/clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/crti.o
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/crtn.o b/clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/crtn.o
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/libclang_rt.builtins.a b/clang/test/Driver/Inputs/musl_sysroot_with_builtins/lib/libclang_rt.builtins.a
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/Inputs/resource_dir_empty/keep b/clang/test/Driver/Inputs/resource_dir_empty/keep
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/linux-musl-library-search.cpp b/clang/test/Driver/linux-musl-library-search.cpp
new file mode 100644
index 0000000000000..a8125aef73ab1
--- /dev/null
+++ b/clang/test/Driver/linux-musl-library-search.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang -### %s 2>&1 \
+// RUN:     --target=aarch64-unknown-linux-musl \
+// RUN:     -ccc-install-dir %S/Inputs/basic_linux_tree/usr/bin \
+// RUN:     -resource-dir=%S/Inputs/resource_dir_empty \
+// RUN:     --sysroot=%S/Inputs/musl_sysroot_with_builtins \
+// RUN:   | FileCheck %s
+
+// CHECK-DAG: "--sysroot={{[^"]+}}/Inputs/musl_sysroot_with_builtins"
+// CHECK-DAG: "{{[^"]+}}/Inputs/musl_sysroot_with_builtins/lib/Scrt1.o"
+// CHECK-DAG: "{{[^"]+}}/Inputs/musl_sysroot_with_builtins/lib/crti.o"
+// CHECK-DAG: "{{[^"]+}}/Inputs/musl_sysroot_with_builtins/lib/clang_rt.crtbegin.o"
+// CHECK-DAG: "{{[^"]+}}/Inputs/musl_sysroot_with_builtins/lib/libclang_rt.builtins.a"
+// CHECK-DAG: "{{[^"]+}}/Inputs/musl_sysroot_with_builtins/lib/clang_rt.crtend.o"
+// CHECK-DAG: "{{[^"]+}}/Inputs/musl_sysroot_with_builtins/lib/crtn.o"

@Sunday111 Sunday111 force-pushed the builtins-from-sysroot branch 3 times, most recently from 1ffc0c0 to 9a88ff0 Compare March 24, 2025 14:57
… in the resource directory

This patch improves support for embedded Linux toolchains with multiple
sysroots, each containing different built-in libraries. If compiler-rt
libraries are not found in the resource directory, the Clang driver will
fallback to searching in the sysroot/lib directory.

This change preserves the standard behavior for regular Linux toolchains
while allowing greater flexibility in toolchains with multiple sysroots.
@Sunday111 Sunday111 force-pushed the builtins-from-sysroot branch from 9a88ff0 to da5c5c6 Compare March 24, 2025 17:24
@MaskRay
Copy link
Member

MaskRay commented Mar 25, 2025

NAK.

(LibraryPaths is primarily intended for compiler-rt libraries.)

Maintaining compatibility by itself isn’t sufficient, as each entry could subtly shift library selection precedence, and the code here reflects a certain level of upstream (llvm-project) preference. We should ensure that the items listed align with typical Linux installations.

I don’t think that finding compiler-rt libraries in $sysroot/lib warrants changes to the Linux toolchain code. Moreover, we discourage such file hierarchies. This change could be problematic for cross compilation.

If your downstream setup demands this, consider installing symlinks.

@MaskRay MaskRay closed this Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants