[PATCH] D102049: [BareMetal] Ensure that sysroot always comes after library paths
Petr Hosek via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu May 6 23:17:14 PDT 2021
phosek created this revision.
phosek added reviewers: abidh, jroelofs.
phosek added a project: clang.
Herald added a subscriber: ki.stfu.
phosek requested review of this revision.
Herald added a subscriber: cfe-commits.
This addresses an issue introduced in D91559 <https://reviews.llvm.org/D91559>. We would invoke the
compiler with -Lpath/to/lib --sysroot=path/to/sysroot where both
locations contain libraries with the same name, but we expect linker
to pick up the library in path/to/lib since that version is more
specialized. This was the case before D91559 <https://reviews.llvm.org/D91559> where the sysroot path
would be ignored, but after that change linker would now pick up the
library from the sysroot which resulted in unexpected behavior.
The sysroot path should always come after any user provided library
paths, followed by compiler runtime paths. We want for libraries in user
provided library paths to always take precedence over sysroot libraries.
This matches the behavior of other toolchains used with other targets.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D102049
Files:
clang/lib/Driver/ToolChains/BareMetal.cpp
clang/test/Driver/baremetal-sysroot.cpp
Index: clang/test/Driver/baremetal-sysroot.cpp
===================================================================
--- clang/test/Driver/baremetal-sysroot.cpp
+++ clang/test/Driver/baremetal-sysroot.cpp
@@ -1,6 +1,23 @@
// REQUIRES: shell
// UNSUPPORTED: system-windows
+// Test that --sysroot always comes after any library paths.
+
+// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
+// RUN: -target armv6m-none-eabi -fuse-ld=lld \
+// RUN: -resource-dir=%S/Inputs/resource_dir \
+// RUN: --sysroot=%S/sys-root -L%S/lib \
+// RUN: | FileCheck --check-prefix=CHECK-V6M -DLIB_DIR=%S/lib %s
+// CHECK-V6M: "{{.*}}clang{{.*}}" "-cc1" "-triple" "thumbv6m-none-unknown-eabi"
+// CHECK-V6M-SAME: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
+// CHECK-V6M-SAME: "-isysroot" "[[SYSROOT:[^"]+]]"
+// CHECK-V6M-NEXT: "{{[^"]*}}ld{{(\.(lld|bfd|gold))?}}{{(\.exe)?}}" "{{.*}}.o" "-Bstatic"
+// CHECK-V6M-SAME: "-L[[LIB_DIR]]"
+// CHECK-V6M-SAME: "-L[[SYSROOT]]{{/|\\\\}}lib"
+// CHECK-V6M-SAME: "-L[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}baremetal"
+// CHECK-V6M-SAME: "-lc" "-lm" "-lclang_rt.builtins-armv6m"
+// CHECK-V6M-SAME: "-o" "{{.*}}.o"
+
// Test that when a --sysroot is not provided, driver picks the default
// location correctly if available.
@@ -11,12 +28,12 @@
// RUN: %T/baremetal_default_sysroot/bin/clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
// RUN: -target armv6m-none-eabi \
-// RUN: | FileCheck --check-prefix=CHECK-V6M-C %s
-// CHECK-V6M-C: "{{.*}}clang{{.*}}" "-cc1" "-triple" "thumbv6m-none-unknown-eabi"
-// CHECK-V6M-C-SAME: "-internal-isystem" "{{.*}}/baremetal_default_sysroot{{[/\\]+}}bin{{[/\\]+}}..{{[/\\]+}}lib{{[/\\]+}}clang-runtimes{{[/\\]+}}armv6m-none-eabi{{[/\\]+}}include{{[/\\]+}}c++{{[/\\]+}}v1"
-// CHECk-V6M-C-SAME: "-internal-isystem" "{{.*}}/baremetal_default_sysroot{{[/\\]+}}bin{{[/\\]+}}..{{[/\\]+}}lib{{[/\\]+}}clang-runtimes{{[/\\]+}}armv6m-none-eabi{{[/\\]+}}include"
-// CHECK-V6M-C-SAME: "-x" "c++" "{{.*}}baremetal-sysroot.cpp"
-// CHECK-V6M-C-NEXT: "{{[^"]*}}ld{{(\.(lld|bfd|gold))?}}{{(\.exe)?}}" "{{.*}}.o" "-Bstatic"
-// CHECK-V6M-C-SAME: "-L{{.*}}/baremetal_default_sysroot{{[/\\]+}}bin{{[/\\]+}}..{{[/\\]+}}lib{{[/\\]+}}clang-runtimes{{[/\\]+}}armv6m-none-eabi{{[/\\]+}}lib"
-// CHECK-V6M-C-SAME: "-lc" "-lm" "-lclang_rt.builtins-armv6m"
-// CHECK-V6M-C-SAME: "-o" "{{.*}}.o"
+// RUN: | FileCheck --check-prefix=CHECK-V6M-DEFAULT-SYSROOT %s
+// CHECK-V6M-DEFAULT-SYSROOT: "{{.*}}clang{{.*}}" "-cc1" "-triple" "thumbv6m-none-unknown-eabi"
+// CHECK-V6M-DEFAULT-SYSROOT-SAME: "-internal-isystem" "{{.*}}/baremetal_default_sysroot{{[/\\]+}}bin{{[/\\]+}}..{{[/\\]+}}lib{{[/\\]+}}clang-runtimes{{[/\\]+}}armv6m-none-eabi{{[/\\]+}}include{{[/\\]+}}c++{{[/\\]+}}v1"
+// CHECk-V6M-DEFAULT-SYSROOT-SAME: "-internal-isystem" "{{.*}}/baremetal_default_sysroot{{[/\\]+}}bin{{[/\\]+}}..{{[/\\]+}}lib{{[/\\]+}}clang-runtimes{{[/\\]+}}armv6m-none-eabi{{[/\\]+}}include"
+// CHECK-V6M-DEFAULT-SYSROOT-SAME: "-x" "c++" "{{.*}}baremetal-sysroot.cpp"
+// CHECK-V6M-DEFAULT-SYSROOT-NEXT: "{{[^"]*}}ld{{(\.(lld|bfd|gold))?}}{{(\.exe)?}}" "{{.*}}.o" "-Bstatic"
+// CHECK-V6M-DEFAULT-SYSROOT-SAME: "-L{{.*}}/baremetal_default_sysroot{{[/\\]+}}bin{{[/\\]+}}..{{[/\\]+}}lib{{[/\\]+}}clang-runtimes{{[/\\]+}}armv6m-none-eabi{{[/\\]+}}lib"
+// CHECK-V6M-DEFAULT-SYSROOT-SAME: "-lc" "-lm" "-lclang_rt.builtins-armv6m"
+// CHECK-V6M-DEFAULT-SYSROOT-SAME: "-o" "{{.*}}.o"
Index: clang/lib/Driver/ToolChains/BareMetal.cpp
===================================================================
--- clang/lib/Driver/ToolChains/BareMetal.cpp
+++ clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -299,13 +299,14 @@
CmdArgs.push_back("-Bstatic");
- CmdArgs.push_back(Args.MakeArgString("-L" + TC.getRuntimesDir()));
-
- TC.AddFilePathLibArgs(Args, CmdArgs);
Args.AddAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
options::OPT_e, options::OPT_s, options::OPT_t,
options::OPT_Z_Flag, options::OPT_r});
+ TC.AddFilePathLibArgs(Args, CmdArgs);
+
+ CmdArgs.push_back(Args.MakeArgString("-L" + TC.getRuntimesDir()));
+
if (TC.ShouldLinkCXXStdlib(Args))
TC.AddCXXStdlibLibArgs(Args, CmdArgs);
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102049.343588.patch
Type: text/x-patch
Size: 4305 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210507/ea187430/attachment.bin>
More information about the cfe-commits
mailing list