[PATCH] D97993: [Driver] Suppress GCC detection under -B for non-Android

Fangrui Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 4 17:23:28 PST 2021


MaskRay created this revision.
MaskRay added reviewers: hubert.reinterpretcast, manojgupta, nickdesaulniers, phosek.
Herald added a subscriber: danielkiss.
MaskRay requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

In GCC, if `-B $prefix` is specified, `$prefix` is used to find executable files and startup files.
`$prefix/include` is added as an include search directory.

In Clang, we detect GCC installation in various target dependent directories.
`$sysroot/usr` (sysroot defaults to "") is a common directory used by most targets.
Such a directory is expected to contain something like `lib{,32,64}/gcc{,-cross}/$triple`.
Clang will then construct library/include paths from the directory.
The usage is different from GCC.

This patch drops GCC detection under -B for non-Android targets.
(android-ndk-standalone.cpp & android-stanlone.cpp depend on the -B detection.)
We expect users to specify --gcc-toolchain if they want to customize the detection roots.

Note: without --gcc-toolchain, the detected GCC installation under
`$sysroot/usr` (all targets) or `/opt/rh/devtoolset-*/root/usr` (Linux, sysroot
is empty) can override -B specified GCC installation if the system GCC has a
larger version. So the current -B behavior is not very predictable. Hope
dropping the behavior has little impact as users likely already specify
--gcc-toolchain/--sysroot to make the behavior predictable.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97993

Files:
  clang/lib/Driver/ToolChains/Gnu.cpp
  clang/test/Driver/gcc-toolchain.cpp


Index: clang/test/Driver/gcc-toolchain.cpp
===================================================================
--- clang/test/Driver/gcc-toolchain.cpp
+++ clang/test/Driver/gcc-toolchain.cpp
@@ -29,3 +29,14 @@
 // CHECK: "{{[^"]*}}/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5{{/|\\\\}}crtbegin.o"
 // CHECK: "-L[[TOOLCHAIN]]/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5"
 // CHECK: "-L[[TOOLCHAIN]]/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5/../../../.."
+
+/// Test we don't detect GCC installation under -B.
+// RUN: %clangxx -no-canonical-prefixes %s -### -o %t 2>&1 \
+// RUN:   --target=aarch64-suse-linux --gcc-toolchain=%S/Inputs/opensuse_42.2_aarch64_tree/usr | \
+// RUN:   FileCheck %s --check-prefix=AARCH64
+// RUN: %clangxx -no-canonical-prefixes %s -### -o %t 2>&1 \
+// RUN:   --target=aarch64-suse-linux -B%S/Inputs/opensuse_42.2_aarch64_tree/usr | \
+// RUN:   FileCheck %s --check-prefix=NO_AARCH64
+
+// AARCH64:        Inputs{{[^"]+}}aarch64-suse-linux/{{[^"]+}}crt1.o"
+// NO_AARCH64-NOT: Inputs{{[^"]+}}aarch64-suse-linux/{{[^"]+}}crt1.o"
Index: clang/lib/Driver/ToolChains/Gnu.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Gnu.cpp
+++ clang/lib/Driver/ToolChains/Gnu.cpp
@@ -1907,8 +1907,9 @@
                            CandidateBiarchTripleAliases);
 
   // Compute the set of prefixes for our search.
-  SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
-                                       D.PrefixDirs.end());
+  SmallVector<std::string, 8> Prefixes;
+  if (TargetTriple.isAndroid())
+    Prefixes.assign(D.PrefixDirs.begin(), D.PrefixDirs.end());
 
   StringRef GCCToolchainDir = getGCCToolchainDir(Args, D.SysRoot);
   if (GCCToolchainDir != "") {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97993.328343.patch
Type: text/x-patch
Size: 1754 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210305/1744e281/attachment.bin>


More information about the cfe-commits mailing list