From 9f298dd31a9937cd0bff5963d6e9cd6caa89a9b5 Mon Sep 17 00:00:00 2001 From: Michal Gorny Date: Tue, 25 Oct 2016 15:07:41 +0000 Subject: [PATCH] [Driver] Support obtaining active toolchain from gcc-config on Gentoo Support using gcc-config to determine the correct GCC toolchain location on Gentoo. In order to do that, attempt to read gcc-config configuration form [[sysroot]]/etc/env.d/gcc, if no custom toolchain location is provided. Differential Revision: https://reviews.llvm.org/D25661 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@285074 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Driver/ToolChains.cpp | 37 +++++++++++++++++++ .../etc/env.d/gcc/config-x86_64-pc-linux-gnu | 1 + .../etc/env.d/gcc/x86_64-pc-linux-gnu-4.9.3 | 10 +++++ .../etc/gentoo-release | 1 + .../usr/include/.keep | 0 .../gcc/x86_64-pc-linux-gnu/4.9.3/crtbegin.o | 0 .../4.9.3/include/g++-v4.9.3/.keep | 0 .../gcc/x86_64-pc-linux-gnu/5.4.0/crtbegin.o | 0 .../5.4.0/include/g++-v5.4.0/.keep | 0 .../usr/x86_64-pc-linux-gnu/lib/.keep | 0 test/Driver/linux-header-search.cpp | 9 +++++ 11 files changed, 58 insertions(+) create mode 100644 test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/env.d/gcc/config-x86_64-pc-linux-gnu create mode 100644 test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/env.d/gcc/x86_64-pc-linux-gnu-4.9.3 create mode 100644 test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/gentoo-release create mode 100644 test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/include/.keep create mode 100644 test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/crtbegin.o create mode 100644 test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4.9.3/.keep create mode 100644 test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/crtbegin.o create mode 100644 test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/include/g++-v5.4.0/.keep create mode 100644 test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/x86_64-pc-linux-gnu/lib/.keep diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index 131cdae56d..f04c443a88 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -1438,6 +1438,43 @@ void Generic_GCC::GCCInstallationDetector::init( } } + // Try to respect gcc-config on Gentoo. However, do that only + // if --gcc-toolchain is not provided or equal to the Gentoo install + // in /usr. This avoids accidentally enforcing the system GCC version + // when using a custom toolchain. + if (GCCToolchainDir == "" || GCCToolchainDir == D.SysRoot + "/usr") { + for (StringRef CandidateTriple : CandidateTripleAliases) { + llvm::ErrorOr> File = + D.getVFS().getBufferForFile(D.SysRoot + "/etc/env.d/gcc/config-" + + CandidateTriple.str()); + if (File) { + SmallVector Lines; + File.get()->getBuffer().split(Lines, "\n"); + for (StringRef Line : Lines) { + // CURRENT=triple-version + if (Line.consume_front("CURRENT=")) { + const std::pair ActiveVersion = + Line.rsplit('-'); + // Note: Strictly speaking, we should be reading + // /etc/env.d/gcc/${CURRENT} now. However, the file doesn't + // contain anything new or especially useful to us. + const std::string GentooPath = D.SysRoot + "/usr/lib/gcc/" + + ActiveVersion.first.str() + "/" + + ActiveVersion.second.str(); + if (D.getVFS().exists(GentooPath + "/crtbegin.o")) { + Version = GCCVersion::Parse(ActiveVersion.second); + GCCInstallPath = GentooPath; + GCCParentLibPath = GentooPath + "/../../.."; + GCCTriple.setTriple(ActiveVersion.first); + IsValid = true; + return; + } + } + } + } + } + } + // Loop over the various components which exist and select the best GCC // installation available. GCC installs are ranked by version number. Version = GCCVersion::Parse("0.0.0"); diff --git a/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/env.d/gcc/config-x86_64-pc-linux-gnu b/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/env.d/gcc/config-x86_64-pc-linux-gnu new file mode 100644 index 0000000000..5ef56a33a6 --- /dev/null +++ b/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/env.d/gcc/config-x86_64-pc-linux-gnu @@ -0,0 +1 @@ +CURRENT=x86_64-pc-linux-gnu-4.9.3 diff --git a/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/env.d/gcc/x86_64-pc-linux-gnu-4.9.3 b/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/env.d/gcc/x86_64-pc-linux-gnu-4.9.3 new file mode 100644 index 0000000000..9ff406284f --- /dev/null +++ b/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/env.d/gcc/x86_64-pc-linux-gnu-4.9.3 @@ -0,0 +1,10 @@ +PATH="/usr/x86_64-pc-linux-gnu/gcc-bin/4.9.3" +ROOTPATH="/usr/x86_64-pc-linux-gnu/gcc-bin/4.9.3" +GCC_PATH="/usr/x86_64-pc-linux-gnu/gcc-bin/4.9.3" +LDPATH="/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3:/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/32" +MANPATH="/usr/share/gcc-data/x86_64-pc-linux-gnu/4.9.3/man" +INFOPATH="/usr/share/gcc-data/x86_64-pc-linux-gnu/4.9.3/info" +STDCXX_INCDIR="g++-v4" +CTARGET="x86_64-pc-linux-gnu" +GCC_SPECS="" +MULTIOSDIRS="../lib64:../lib32" diff --git a/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/gentoo-release b/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/gentoo-release new file mode 100644 index 0000000000..5f0944f7d9 --- /dev/null +++ b/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/gentoo-release @@ -0,0 +1 @@ +Gentoo Base System release 2.3 diff --git a/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/include/.keep b/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/include/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/crtbegin.o b/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/crtbegin.o new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4.9.3/.keep b/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4.9.3/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/crtbegin.o b/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/crtbegin.o new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/include/g++-v5.4.0/.keep b/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/include/g++-v5.4.0/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/x86_64-pc-linux-gnu/lib/.keep b/test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/x86_64-pc-linux-gnu/lib/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/Driver/linux-header-search.cpp b/test/Driver/linux-header-search.cpp index 5f6ac504a0..491bb4d0e2 100644 --- a/test/Driver/linux-header-search.cpp +++ b/test/Driver/linux-header-search.cpp @@ -301,6 +301,15 @@ // CHECK-GENTOO-4-9-3: "-internal-externc-isystem" "[[SYSROOT]]/include" // CHECK-GENTOO-4-9-3: "-internal-externc-isystem" "[[SYSROOT]]/usr/include" // +// Test support for Gentoo's gcc-config -- clang should prefer the older +// (4.9.3) version over the newer (5.4.0) due to preference specified +// in /etc/env.d/gcc/x86_64-pc-linux-gnu. +// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \ +// RUN: -target x86_64-unknown-linux-gnu -stdlib=libstdc++ \ +// RUN: --sysroot=%S/Inputs/gentoo_linux_gcc_multi_version_tree \ +// RUN: --gcc-toolchain="" \ +// RUN: | FileCheck --check-prefix=CHECK-GENTOO-4-9-3 %s +// // Check header search on Debian 6 / MIPS64 // RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \ // RUN: -target mips64-unknown-linux-gnuabi64 -stdlib=libstdc++ \ -- 2.50.1