https://github.com/am11 updated https://github.com/llvm/llvm-project/pull/206477

>From 6081fc2e68d139919ee3de71f5d5ae76f536ae56 Mon Sep 17 00:00:00 2001
From: Adeel Mujahid <[email protected]>
Date: Mon, 29 Jun 2026 16:02:53 +0300
Subject: [PATCH] [Driver] Find vendor-prefixed musl GCC toolchains

---
 clang/docs/ReleaseNotes.rst                   |  5 +++
 clang/lib/Driver/ToolChains/Gnu.cpp           | 32 +++++++++++++++++++
 .../Driver/linux-musl-alpine-gcc-detect.c     | 23 +++++++++++++
 3 files changed, 60 insertions(+)
 create mode 100644 clang/test/Driver/linux-musl-alpine-gcc-detect.c

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a2eef75e2a719..28b34c345eed1 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -346,6 +346,11 @@ Non-comprehensive list of changes in this release
 - Linux and Windows toolchains now support Clang multilibs using
   ``-fmultilib-flag=``.
 
+- The GCC installation detector now finds vendor-prefixed musl toolchains, such
+  as Alpine Linux's ``<arch>-alpine-linux-musl[abi]``, when given a neutral
+  ``--target=<arch>-linux-musl[abi]``. Previously this only worked when the
+  exact distribution triple was passed. (#GH89146)
+
 - The SafeStack builtins ``__builtin___get_unsafe_stack_ptr``,
   ``__builtin___get_unsafe_stack_bottom``, 
``__builtin___get_unsafe_stack_top``,
   and ``__builtin___get_unsafe_stack_start`` are now deprecated. Use the
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp 
b/clang/lib/Driver/ToolChains/Gnu.cpp
index b58c10145e7f3..0f42ec188424d 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -2206,6 +2206,38 @@ void Generic_GCC::GCCInstallationDetector::init(
       for (StringRef Candidate : CandidateTripleAliases)
         ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, Candidate, false,
                                GCCDirExists, GCCCrossDirExists);
+
+      // Some musl distributions embed a vendor name in their GCC triple that a
+      // neutral --target cannot spell. For example Alpine ships its toolchain
+      // under <arch>-alpine-linux-musl[abi] (e.g. aarch64-alpine-linux-musl,
+      // armv7-alpine-linux-musleabihf), so --target=aarch64-linux-musl or
+      // --target=armv7-linux-musleabihf would otherwise fail to find it.
+      // Enumerate the GCC triple directories and try any entry whose
+      // architecture, OS and environment match the target, ignoring the vendor
+      // field. ScanLibDirForGCCTriple already de-duplicates installations, so
+      // candidates also covered above are harmless. Skip this when the user
+      // pinned an explicit --gcc-triple.
+      if (TargetTriple.isMusl() && !Args.hasArg(options::OPT_gcc_triple_EQ)) {
+        for (const char *GCCDir : {"/gcc", "/gcc-cross"}) {
+          if ((StringRef(GCCDir) == "/gcc" && !GCCDirExists) ||
+              (StringRef(GCCDir) == "/gcc-cross" && !GCCCrossDirExists))
+            continue;
+          std::error_code EC;
+          for (llvm::vfs::directory_iterator
+                   LI = VFS.dir_begin(LibDir + GCCDir, EC),
+                   LE;
+               !EC && LI != LE; LI = LI.increment(EC)) {
+            StringRef Name = llvm::sys::path::filename(LI->path());
+            llvm::Triple CandidateTriple(Name);
+            if (CandidateTriple.getArch() == TargetTriple.getArch() &&
+                CandidateTriple.getOS() == TargetTriple.getOS() &&
+                CandidateTriple.getEnvironment() ==
+                    TargetTriple.getEnvironment())
+              ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, Name, false,
+                                     GCCDirExists, GCCCrossDirExists);
+          }
+        }
+      }
     }
     for (StringRef Suffix : CandidateBiarchLibDirs) {
       const std::string LibDir = Prefix + Suffix.str();
diff --git a/clang/test/Driver/linux-musl-alpine-gcc-detect.c 
b/clang/test/Driver/linux-musl-alpine-gcc-detect.c
new file mode 100644
index 0000000000000..a902bad65f442
--- /dev/null
+++ b/clang/test/Driver/linux-musl-alpine-gcc-detect.c
@@ -0,0 +1,23 @@
+// Alpine Linux ships its GCC toolchain under a vendor-prefixed musl triple
+// (e.g. aarch64-alpine-linux-musl, armv7-alpine-linux-musleabihf). Verify that
+// a neutral --target=<arch>-linux-musl[abi] still locates it, matching the
+// behavior of the exact Alpine triple.
+// See https://github.com/llvm/llvm-project/issues/89146
+
+// RUN: %clang -### %s --target=aarch64-linux-musl --rtlib=libgcc -no-pie \
+// RUN:     --sysroot=%S/Inputs/alpine_aarch64_musl_tree 2>&1 \
+// RUN:   | FileCheck --check-prefix=AARCH64 %s
+// RUN: %clang -### %s --target=aarch64-alpine-linux-musl --rtlib=libgcc 
-no-pie \
+// RUN:     --sysroot=%S/Inputs/alpine_aarch64_musl_tree 2>&1 \
+// RUN:   | FileCheck --check-prefix=AARCH64 %s
+// AARCH64: 
"{{[^"]*}}/usr/lib/gcc/aarch64-alpine-linux-musl/13.2.1{{/|\\\\}}crtbegin.o"
+
+// RUN: %clang -### %s --target=armv7-linux-musleabihf --rtlib=libgcc -no-pie \
+// RUN:     --sysroot=%S/Inputs/alpine_armv7_musl_tree 2>&1 \
+// RUN:   | FileCheck --check-prefix=ARMHF %s
+// RUN: %clang -### %s --target=armv7-alpine-linux-musleabihf --rtlib=libgcc 
-no-pie \
+// RUN:     --sysroot=%S/Inputs/alpine_armv7_musl_tree 2>&1 \
+// RUN:   | FileCheck --check-prefix=ARMHF %s
+// ARMHF: 
"{{[^"]*}}/usr/lib/gcc/armv7-alpine-linux-musleabihf/13.2.1{{/|\\\\}}crtbegin.o"
+
+int main(void) {}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to