[clang] [Driver] Add the --gcc-triple option (PR #73214)

Tom Stellard via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 13 12:36:17 PST 2023


https://github.com/tstellar updated https://github.com/llvm/llvm-project/pull/73214

>From 72f6f3a611f237f71ce02cfb79620257a9e2d827 Mon Sep 17 00:00:00 2001
From: Tom Stellard <tstellar at redhat.com>
Date: Thu, 16 Nov 2023 05:11:04 +0000
Subject: [PATCH 1/6] [Driver] Add the --gcc-triple option

When --gcc-triple is used, the driver will search for the 'best' gcc
installation that has the given triple.  This is useful for
distributions that want clang to use a specific gcc triple, but do
not want to pin to a specific version as would be required by
using --gcc-install-dir.  Having clang linked to a specific gcc version
can cause clang to stop working when the version of gcc installed on
the system gets updated.
---
 clang/include/clang/Driver/Options.td                     | 2 ++
 clang/lib/Driver/ToolChains/Gnu.cpp                       | 8 ++++++++
 .../usr/lib/gcc/x86_64-linux-gnu/13/crtbegin.o            | 0
 .../usr/lib/gcc/x86_64-linux-gnu/13/crtend.o              | 0
 .../fedora_39_tree/usr/lib/gcc/x86_64-linux-gnu/13/crti.o | 0
 .../fedora_39_tree/usr/lib/gcc/x86_64-linux-gnu/13/crtn.o | 0
 .../usr/lib/gcc/x86_64-redhat-linux/13/crtbegin.o         | 0
 .../usr/lib/gcc/x86_64-redhat-linux/13/crtend.o           | 0
 .../usr/lib/gcc/x86_64-redhat-linux/13/crti.o             | 0
 .../usr/lib/gcc/x86_64-redhat-linux/13/crtn.o             | 0
 clang/test/Driver/gcc-prefix.cpp                          | 8 ++++++++
 11 files changed, 18 insertions(+)
 create mode 100644 clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-linux-gnu/13/crtbegin.o
 create mode 100644 clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-linux-gnu/13/crtend.o
 create mode 100644 clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-linux-gnu/13/crti.o
 create mode 100644 clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-linux-gnu/13/crtn.o
 create mode 100644 clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13/crtbegin.o
 create mode 100644 clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13/crtend.o
 create mode 100644 clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13/crti.o
 create mode 100644 clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13/crtn.o
 create mode 100644 clang/test/Driver/gcc-prefix.cpp

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index b2f2bcb6ac3791..79ced47150b455 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -773,6 +773,8 @@ def gcc_install_dir_EQ : Joined<["--"], "gcc-install-dir=">,
 def gcc_toolchain : Joined<["--"], "gcc-toolchain=">, Flags<[NoXarchOption]>,
   HelpText<"Specify a directory where Clang can find 'include' and 'lib{,32,64}/gcc{,-cross}/$triple/$version'. "
   "Clang will use the GCC installation with the largest version">;
+def gcc_triple_EQ : Joined<["--"], "gcc-triple=">,
+  HelpText<"Search for the GCC installation with the specified triple.">;
 def CC : Flag<["-"], "CC">, Visibility<[ClangOption, CC1Option]>,
   Group<Preprocessor_Group>,
     HelpText<"Include comments from within macros in preprocessed output">,
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 9fb99145d3b909..44b92a879a3a8c 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -2130,6 +2130,14 @@ void Generic_GCC::GCCInstallationDetector::init(
     return;
   }
 
+  // If --gcc-triple is specified use this instead of trying to
+  // auto-detect a triple.
+  if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_gcc_triple_EQ)) {
+    StringRef GCCTriple = A->getValue();
+    CandidateTripleAliases.clear();
+    CandidateTripleAliases.push_back(GCCTriple);
+  }
+
   // Compute the set of prefixes for our search.
   SmallVector<std::string, 8> Prefixes;
   StringRef GCCToolchainDir = getGCCToolchainDir(Args, D.SysRoot);
diff --git a/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-linux-gnu/13/crtbegin.o b/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-linux-gnu/13/crtbegin.o
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-linux-gnu/13/crtend.o b/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-linux-gnu/13/crtend.o
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-linux-gnu/13/crti.o b/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-linux-gnu/13/crti.o
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-linux-gnu/13/crtn.o b/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-linux-gnu/13/crtn.o
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13/crtbegin.o b/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13/crtbegin.o
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13/crtend.o b/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13/crtend.o
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13/crti.o b/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13/crti.o
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13/crtn.o b/clang/test/Driver/Inputs/fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13/crtn.o
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/gcc-prefix.cpp b/clang/test/Driver/gcc-prefix.cpp
new file mode 100644
index 00000000000000..ce1146dc1ded21
--- /dev/null
+++ b/clang/test/Driver/gcc-prefix.cpp
@@ -0,0 +1,8 @@
+// UNSUPPORTED: system-windows
+//
+// RUN: %clang --target=x86_64-redhat-linux-gnu \
+// RUN: --sysroot=%S/Inputs/fedora_39_tree --gcc-triple=x86_64-redhat-linux -v 2>&1 | \
+// RUN: FileCheck %s
+//
+// CHECK: {{^}}Selected GCC installation:
+// CHECK: fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13{{$}}

>From ec346a14b36dab9ed75e6223a3a1b6ec5444aad4 Mon Sep 17 00:00:00 2001
From: Tom Stellard <tstellar at redhat.com>
Date: Sun, 26 Nov 2023 14:53:27 +0000
Subject: [PATCH 2/6] Fix formatting

---
 clang/lib/Driver/ToolChains/Gnu.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 44b92a879a3a8c..24c4d0c30354b9 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -2132,7 +2132,8 @@ void Generic_GCC::GCCInstallationDetector::init(
 
   // If --gcc-triple is specified use this instead of trying to
   // auto-detect a triple.
-  if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_gcc_triple_EQ)) {
+  if (const Arg *A =
+          Args.getLastArg(clang::driver::options::OPT_gcc_triple_EQ)) {
     StringRef GCCTriple = A->getValue();
     CandidateTripleAliases.clear();
     CandidateTripleAliases.push_back(GCCTriple);

>From e0af079cdba52e845211ef9577dc4c2d96fc835b Mon Sep 17 00:00:00 2001
From: Tom Stellard <tstellar at redhat.com>
Date: Wed, 13 Dec 2023 20:00:31 +0000
Subject: [PATCH 3/6] Fix test formatting

---
 clang/test/Driver/gcc-prefix.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/test/Driver/gcc-prefix.cpp b/clang/test/Driver/gcc-prefix.cpp
index ce1146dc1ded21..0eb316b56ca698 100644
--- a/clang/test/Driver/gcc-prefix.cpp
+++ b/clang/test/Driver/gcc-prefix.cpp
@@ -1,8 +1,8 @@
 // UNSUPPORTED: system-windows
-//
+
 // RUN: %clang --target=x86_64-redhat-linux-gnu \
 // RUN: --sysroot=%S/Inputs/fedora_39_tree --gcc-triple=x86_64-redhat-linux -v 2>&1 | \
 // RUN: FileCheck %s
-//
+
 // CHECK: {{^}}Selected GCC installation:
 // CHECK: fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13{{$}}

>From 69349493c9852d5e83197873b532d89de0c268e0 Mon Sep 17 00:00:00 2001
From: Tom Stellard <tstellar at redhat.com>
Date: Wed, 13 Dec 2023 20:00:47 +0000
Subject: [PATCH 4/6] Rename test file

---
 clang/test/Driver/{gcc-prefix.cpp => gcc-triple.cpp} | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename clang/test/Driver/{gcc-prefix.cpp => gcc-triple.cpp} (100%)

diff --git a/clang/test/Driver/gcc-prefix.cpp b/clang/test/Driver/gcc-triple.cpp
similarity index 100%
rename from clang/test/Driver/gcc-prefix.cpp
rename to clang/test/Driver/gcc-triple.cpp

>From 471c4ce281ae5319ac32cb4188d4ee0d45b42f5d Mon Sep 17 00:00:00 2001
From: Tom Stellard <tstellar at redhat.com>
Date: Wed, 13 Dec 2023 20:01:35 +0000
Subject: [PATCH 5/6] More formatting fixes for tests

---
 clang/test/Driver/gcc-triple.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/Driver/gcc-triple.cpp b/clang/test/Driver/gcc-triple.cpp
index 0eb316b56ca698..497871fdabc972 100644
--- a/clang/test/Driver/gcc-triple.cpp
+++ b/clang/test/Driver/gcc-triple.cpp
@@ -1,7 +1,7 @@
 // UNSUPPORTED: system-windows
 
 // RUN: %clang --target=x86_64-redhat-linux-gnu \
-// RUN: --sysroot=%S/Inputs/fedora_39_tree --gcc-triple=x86_64-redhat-linux -v 2>&1 | \
+// RUN:   --sysroot=%S/Inputs/fedora_39_tree --gcc-triple=x86_64-redhat-linux -v 2>&1 | \
 // RUN: FileCheck %s
 
 // CHECK: {{^}}Selected GCC installation:

>From 8a6d5089fc98899c4234c72c628063872273e1e8 Mon Sep 17 00:00:00 2001
From: Tom Stellard <tstellar at redhat.com>
Date: Wed, 13 Dec 2023 20:32:53 +0000
Subject: [PATCH 6/6] Add negative test

---
 clang/test/Driver/gcc-triple.cpp | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/clang/test/Driver/gcc-triple.cpp b/clang/test/Driver/gcc-triple.cpp
index 497871fdabc972..513d3bcdf23021 100644
--- a/clang/test/Driver/gcc-triple.cpp
+++ b/clang/test/Driver/gcc-triple.cpp
@@ -2,7 +2,13 @@
 
 // RUN: %clang --target=x86_64-redhat-linux-gnu \
 // RUN:   --sysroot=%S/Inputs/fedora_39_tree --gcc-triple=x86_64-redhat-linux -v 2>&1 | \
-// RUN: FileCheck %s
+// RUN:   FileCheck %s --check-prefix=TRIPLE_EXISTS
 
-// CHECK: {{^}}Selected GCC installation:
-// CHECK: fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13{{$}}
+// TRIPLE_EXISTS: {{^}}Selected GCC installation:
+// TRIPLE_EXISTS: fedora_39_tree/usr/lib/gcc/x86_64-redhat-linux/13{{$}}
+
+// RUN: %clang --target=x86_64-redhat-linux-gnu \
+// RUN:   --sysroot=%S/Inputs/fedora_39_tree --gcc-triple=x86_64-gentoo-linux -v 2>&1 | \
+// RUN:   FileCheck %s --check-prefix=TRIPLE_DOESNT_EXIST
+//
+// TRIPLE_DOESNT_EXIST-NOT: x86_64-gentoo-linux



More information about the cfe-commits mailing list