[PATCH] D134454: [Driver][Distro] Fix ArchLinux triplet and sysroot detection

Adrian Ratiu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 22 10:05:10 PDT 2022


10ne1 created this revision.
10ne1 added reviewers: nickdesaulniers, nathanchance, manojgupta.
Herald added a subscriber: kristof.beyls.
Herald added a project: All.
10ne1 requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay.
Herald added a project: clang.

While cross-compiling Linux kernel tools with Clang on ArchLinux, it
was revealed the sysroot and triplet are not properly detected and
instead of fixing the root cause in Clang, kernel developers started
doing workarounds [1] upon workarounds [2] in the kernel build to be
able to pass a working sysroot and triplet during Clang invocation.

To give a concrete example, ArchLinux installs its cross-compliation
sysroot in /usr/aarch64-linux-gnu for triplet aarch64-linux-gnu.
This causes two problems which this commit fixes:

1. The triplet results in an Unknown OS & Unknown distro. To fix this

error, we call Triplet::normalize() which does the following transform
aarch64-linux-gnu -> aarch64-unknown-linux-gnu, then the OS & Distro
are properly detected.

2. The sysroot under /usr/<triplet> itself is not properly detected

in Linux::computeSysRoot() so we add a relative path to the ArchLinux
installed GCC toolchain.

Fixing these two bugs allows to significantly clean up the kernel
build as well as fix the cross-compilation detection on ArchLinux.

[1] https://github.com/torvalds/linux/commit/cebdb7374577ac6e14afb11311af8c2c44a259fa
[2] https://github.com/torvalds/linux/commit/7fd9fd46a459272e641be78c1cc36baab1921fa1


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134454

Files:
  clang/include/clang/Driver/Distro.h
  clang/lib/Driver/Distro.cpp
  clang/lib/Driver/ToolChains/Linux.cpp


Index: clang/lib/Driver/ToolChains/Linux.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Linux.cpp
+++ clang/lib/Driver/ToolChains/Linux.cpp
@@ -370,6 +370,16 @@
     return std::string();
   }
 
+  const Distro Distro(getDriver().getVFS(), getTriple());
+  const StringRef InstallDir = GCCInstallation.getInstallPath();
+  const StringRef TripleStr = GCCInstallation.getTriple().str();
+
+  if (Distro.IsArchLinux()) {
+    std::string Path = (InstallDir + "/../../../../"  + TripleStr).str();
+    if (getVFS().exists(Path))
+      return Path;
+  }
+
   if (!GCCInstallation.isValid() || !getTriple().isMIPS())
     return std::string();
 
@@ -377,8 +387,6 @@
   // and put it into different places. Here we try to check some known
   // variants.
 
-  const StringRef InstallDir = GCCInstallation.getInstallPath();
-  const StringRef TripleStr = GCCInstallation.getTriple().str();
   const Multilib &Multilib = GCCInstallation.getMultilib();
 
   std::string Path =
Index: clang/lib/Driver/Distro.cpp
===================================================================
--- clang/lib/Driver/Distro.cpp
+++ clang/lib/Driver/Distro.cpp
@@ -205,9 +205,16 @@
 
 static Distro::DistroType GetDistro(llvm::vfs::FileSystem &VFS,
                                     const llvm::Triple &TargetOrHost) {
+
+  // Sometimes the OS can't be detected from the triplet due to ambiguity, for
+  // eg. ArchLinux uses aarch64-linux-gnu which results in Uknonwn OS & distro,
+  // so normalize the triplet which results in aarch64-unknown-linux-gnu, such
+  // that the Linux OS and distro are properly detected in this cases.
+  llvm::Triple NormTargetOrHost = llvm::Triple(Twine(TargetOrHost.normalize()));
+
   // If we don't target Linux, no need to check the distro. This saves a few
   // OS calls.
-  if (!TargetOrHost.isOSLinux())
+  if (!NormTargetOrHost.isOSLinux())
     return Distro::UnknownDistro;
 
   // True if we're backed by a real file system.
Index: clang/include/clang/Driver/Distro.h
===================================================================
--- clang/include/clang/Driver/Distro.h
+++ clang/include/clang/Driver/Distro.h
@@ -134,6 +134,8 @@
 
   bool IsGentoo() const { return DistroVal == Gentoo; }
 
+  bool IsArchLinux() const { return DistroVal == ArchLinux; }
+
   /// @}
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134454.462220.patch
Type: text/x-patch
Size: 2359 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220922/dfd5d5ca/attachment.bin>


More information about the cfe-commits mailing list