manojgupta updated this revision to Diff 141099. manojgupta added a comment.
Avoid double appending of sysroot prefix to base path. Repository: rC Clang https://reviews.llvm.org/D45233 Files: lib/Driver/ToolChains/Gnu.cpp lib/Driver/ToolChains/Gnu.h test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/env.d/gcc/x86_64-pc-linux-gnu-4.9.3 test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/32/crtbegin.o test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/crtbegin.o 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 test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/x32/crtbegin.o test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/32/crtbegin.o test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/crtbegin.o test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/include/g++-v4.9.3/.keep test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/x32/crtbegin.o test/Driver/linux-header-search.cpp
Index: test/Driver/linux-header-search.cpp =================================================================== --- test/Driver/linux-header-search.cpp +++ test/Driver/linux-header-search.cpp @@ -308,40 +308,51 @@ // 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 +// RUN: | FileCheck --check-prefix=CHECK-GENTOO-4-9-X %s +// +// CHECK-GENTOO-4-9-X: "{{.*}}clang{{.*}}" "-cc1" +// CHECK-GENTOO-4-9-X: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" +// CHECK-GENTOO-4-9-X: "-isysroot" "[[SYSROOT:[^"]+]]" +// CHECK-GENTOO-4-9-X: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/include/g++-v4.9.3" +// CHECK-GENTOO-4-9-X: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/include/g++-v4.9.3/x86_64-pc-linux-gnu" +// CHECK-GENTOO-4-9-X: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/include/g++-v4.9.3/backward" +// CHECK-GENTOO-4-9-X: "-internal-isystem" "[[SYSROOT]]/usr/local/include" +// CHECK-GENTOO-4-9-X: "-internal-isystem" "[[RESOURCE_DIR]]{{/|\\\\}}include" +// CHECK-GENTOO-4-9-X: "-internal-externc-isystem" "[[SYSROOT]]/include" +// CHECK-GENTOO-4-9-X: "-internal-externc-isystem" "[[SYSROOT]]/usr/include" // // Test that gcc-config support does not break multilib. // RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \ // RUN: -target x86_64-unknown-linux-gnux32 -stdlib=libstdc++ \ // RUN: --sysroot=%S/Inputs/gentoo_linux_gcc_multi_version_tree \ // RUN: --gcc-toolchain="" \ -// RUN: | FileCheck --check-prefix=CHECK-GENTOO-4-9-3-X32 %s -// CHECK-GENTOO-4-9-3-X32: "{{.*}}clang{{.*}}" "-cc1" -// CHECK-GENTOO-4-9-3-X32: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" -// CHECK-GENTOO-4-9-3-X32: "-isysroot" "[[SYSROOT:[^"]+]]" -// CHECK-GENTOO-4-9-3-X32: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4.9.3" -// CHECK-GENTOO-4-9-3-X32: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4.9.3/x86_64-pc-linux-gnu/x32" -// CHECK-GENTOO-4-9-3-X32: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4.9.3/backward" -// CHECK-GENTOO-4-9-3-X32: "-internal-isystem" "[[SYSROOT]]/usr/local/include" -// CHECK-GENTOO-4-9-3-X32: "-internal-isystem" "[[RESOURCE_DIR]]{{/|\\\\}}include" -// CHECK-GENTOO-4-9-3-X32: "-internal-externc-isystem" "[[SYSROOT]]/include" -// CHECK-GENTOO-4-9-3-X32: "-internal-externc-isystem" "[[SYSROOT]]/usr/include" +// RUN: | FileCheck --check-prefix=CHECK-GENTOO-4-9-X-X32 %s +// CHECK-GENTOO-4-9-X-X32: "{{.*}}clang{{.*}}" "-cc1" +// CHECK-GENTOO-4-9-X-X32: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" +// CHECK-GENTOO-4-9-X-X32: "-isysroot" "[[SYSROOT:[^"]+]]" +// CHECK-GENTOO-4-9-X-X32: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/include/g++-v4.9.3" +// CHECK-GENTOO-4-9-X-X32: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/include/g++-v4.9.3/x86_64-pc-linux-gnu/x32" +// CHECK-GENTOO-4-9-X-X32: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/include/g++-v4.9.3/backward" +// CHECK-GENTOO-4-9-X-X32: "-internal-isystem" "[[SYSROOT]]/usr/local/include" +// CHECK-GENTOO-4-9-X-X32: "-internal-isystem" "[[RESOURCE_DIR]]{{/|\\\\}}include" +// CHECK-GENTOO-4-9-X-X32: "-internal-externc-isystem" "[[SYSROOT]]/include" +// CHECK-GENTOO-4-9-X-X32: "-internal-externc-isystem" "[[SYSROOT]]/usr/include" // // RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \ // RUN: -target i386-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-32 %s -// CHECK-GENTOO-4-9-3-32: "{{.*}}clang{{.*}}" "-cc1" -// CHECK-GENTOO-4-9-3-32: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" -// CHECK-GENTOO-4-9-3-32: "-isysroot" "[[SYSROOT:[^"]+]]" -// CHECK-GENTOO-4-9-3-32: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4.9.3" -// CHECK-GENTOO-4-9-3-32: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4.9.3/x86_64-pc-linux-gnu/32" -// CHECK-GENTOO-4-9-3-32: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4.9.3/backward" -// CHECK-GENTOO-4-9-3-32: "-internal-isystem" "[[SYSROOT]]/usr/local/include" -// CHECK-GENTOO-4-9-3-32: "-internal-isystem" "[[RESOURCE_DIR]]{{/|\\\\}}include" -// CHECK-GENTOO-4-9-3-32: "-internal-externc-isystem" "[[SYSROOT]]/include" -// CHECK-GENTOO-4-9-3-32: "-internal-externc-isystem" "[[SYSROOT]]/usr/include" +// RUN: | FileCheck --check-prefix=CHECK-GENTOO-4-9-X-32 %s +// CHECK-GENTOO-4-9-X-32: "{{.*}}clang{{.*}}" "-cc1" +// CHECK-GENTOO-4-9-X-32: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" +// CHECK-GENTOO-4-9-X-32: "-isysroot" "[[SYSROOT:[^"]+]]" +// CHECK-GENTOO-4-9-X-32: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/include/g++-v4.9.3" +// CHECK-GENTOO-4-9-X-32: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/include/g++-v4.9.3/x86_64-pc-linux-gnu/32" +// CHECK-GENTOO-4-9-X-32: "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/include/g++-v4.9.3/backward" +// CHECK-GENTOO-4-9-X-32: "-internal-isystem" "[[SYSROOT]]/usr/local/include" +// CHECK-GENTOO-4-9-X-32: "-internal-isystem" "[[RESOURCE_DIR]]{{/|\\\\}}include" +// CHECK-GENTOO-4-9-X-32: "-internal-externc-isystem" "[[SYSROOT]]/include" +// CHECK-GENTOO-4-9-X-32: "-internal-externc-isystem" "[[SYSROOT]]/usr/include" // // Check header search on Debian 6 / MIPS64 // RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \ Index: test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/env.d/gcc/x86_64-pc-linux-gnu-4.9.3 =================================================================== --- test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/env.d/gcc/x86_64-pc-linux-gnu-4.9.3 +++ test/Driver/Inputs/gentoo_linux_gcc_multi_version_tree/etc/env.d/gcc/x86_64-pc-linux-gnu-4.9.3 @@ -1,9 +1,9 @@ -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" +PATH="/usr/x86_64-pc-linux-gnu/gcc-bin/4.9.x" +ROOTPATH="/usr/x86_64-pc-linux-gnu/gcc-bin/4.9.x" +GCC_PATH="/usr/x86_64-pc-linux-gnu/gcc-bin/4.9.x" +LDPATH="/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x:/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/32:/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/x32" +MANPATH="/usr/share/gcc-data/x86_64-pc-linux-gnu/4.9.x/man" +INFOPATH="/usr/share/gcc-data/x86_64-pc-linux-gnu/4.9.x/info" STDCXX_INCDIR="g++-v4" CTARGET="x86_64-pc-linux-gnu" GCC_SPECS="" Index: lib/Driver/ToolChains/Gnu.h =================================================================== --- lib/Driver/ToolChains/Gnu.h +++ lib/Driver/ToolChains/Gnu.h @@ -265,9 +265,15 @@ StringRef CandidateTriple, bool NeedsBiarchSuffix = false); + bool ScanGentooConfigs(const llvm::Triple &TargetTriple, + const llvm::opt::ArgList &Args, + const SmallVectorImpl<StringRef> &CandidateTriples, + const SmallVectorImpl<StringRef> &BiarchTriples); + bool ScanGentooGccConfig(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args, StringRef CandidateTriple, + const std::string& SysRootPrefix, bool NeedsBiarchSuffix = false); }; Index: lib/Driver/ToolChains/Gnu.cpp =================================================================== --- lib/Driver/ToolChains/Gnu.cpp +++ lib/Driver/ToolChains/Gnu.cpp @@ -1707,18 +1707,21 @@ // in /usr. This avoids accidentally enforcing the system GCC version // when using a custom toolchain. if (GCCToolchainDir == "" || GCCToolchainDir == D.SysRoot + "/usr") { - for (StringRef CandidateTriple : ExtraTripleAliases) { - if (ScanGentooGccConfig(TargetTriple, Args, CandidateTriple)) - return; - } - for (StringRef CandidateTriple : CandidateTripleAliases) { - if (ScanGentooGccConfig(TargetTriple, Args, CandidateTriple)) - return; - } - for (StringRef CandidateTriple : CandidateBiarchTripleAliases) { - if (ScanGentooGccConfig(TargetTriple, Args, CandidateTriple, true)) - return; - } + SmallVector<StringRef, 16> GentooTestTriples; + // Try to match an exact triple as target triple first. + // e.g. crossdev -S x86_64-gentoo-linux-gnu will install gcc libs for + // x86_64-gentoo-linux-gnu. But "clang -target x86_64-gentoo-linux-gnu" + // may pick the libraries for x86_64-pc-linux-gnu even when exact matching + // triple x86_64-gentoo-linux-gnu is present. + GentooTestTriples.push_back(TargetTriple.str()); + // Check rest of triples. + GentooTestTriples.append(ExtraTripleAliases.begin(), + ExtraTripleAliases.end()); + GentooTestTriples.append(CandidateTripleAliases.begin(), + CandidateTripleAliases.end()); + if (ScanGentooConfigs(TargetTriple, Args, GentooTestTriples, + CandidateBiarchTripleAliases)) + return; } // Loop over the various components which exist and select the best GCC @@ -2224,35 +2227,98 @@ } } +bool Generic_GCC::GCCInstallationDetector::ScanGentooConfigs( + const llvm::Triple &TargetTriple, const ArgList &Args, + const SmallVectorImpl<StringRef> &CandidateTriples, + const SmallVectorImpl<StringRef> &CandidateBiarchTriples) { + // First scan libraries from the sysroot directory if specified. + // If not successful, scan the root directories. This is needed to find + // cross-compilation files/libraries installed by crossdev. For instance, + // crossdev -S aarch64-gentoo-linux-gnu installs GCC libraries in + // /usr/lib/gcc/version/aarch64-gentoo-linux-gnu and the headers are + // installed at /usr/aarch64-gentoo-linux-gnu. Sysroot argument is + // needed to find the headers installed in /usr/aarch64-gentoo-linux-gnu. + SmallVector<std::string, 2> SysRootPrefixes; + if (!D.SysRoot.empty()) { + SysRootPrefixes.push_back(D.SysRoot); + } + SysRootPrefixes.push_back(""); + + for (const std::string &SysRoot : SysRootPrefixes) { + for (StringRef CandidateTriple : CandidateTriples) { + if (ScanGentooGccConfig(TargetTriple, Args, CandidateTriple, SysRoot)) + return true; + } + + for (StringRef CandidateTriple : CandidateBiarchTriples) { + if (ScanGentooGccConfig(TargetTriple, Args, CandidateTriple, SysRoot, + true)) + return true; + } + } + return false; +} + bool Generic_GCC::GCCInstallationDetector::ScanGentooGccConfig( const llvm::Triple &TargetTriple, const ArgList &Args, - StringRef CandidateTriple, bool NeedsBiarchSuffix) { + StringRef CandidateTriple, const std::string &SysRootPrefix, + bool NeedsBiarchSuffix) { llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File = - D.getVFS().getBufferForFile(D.SysRoot + "/etc/env.d/gcc/config-" + + D.getVFS().getBufferForFile(SysRootPrefix + "/etc/env.d/gcc/config-" + CandidateTriple.str()); if (File) { SmallVector<StringRef, 2> Lines; File.get()->getBuffer().split(Lines, "\n"); for (StringRef Line : Lines) { Line = Line.trim(); // CURRENT=triple-version - if (Line.consume_front("CURRENT=")) { - const std::pair<StringRef, StringRef> 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 (!Line.consume_front("CURRENT=")) + continue; + // Process the config file pointed to by CURRENT. + llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ConfigFile = + D.getVFS().getBufferForFile(SysRootPrefix + "/etc/env.d/gcc/" + + Line.str()); + std::pair<StringRef, StringRef> ActiveVersion = Line.rsplit('-'); + // List of paths to scan for libraries. + SmallVector<StringRef, 4> GentooScanPaths; + // Scan the Config file to find installed GCC libraries path. + // Typical content of the GCC config file: + // LDPATH="/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x:/usr/lib/gcc/ + // (continued from previous line) x86_64-pc-linux-gnu/4.9.x/32" + // MANPATH="/usr/share/gcc-data/x86_64-pc-linux-gnu/4.9.x/man" + // INFOPATH="/usr/share/gcc-data/x86_64-pc-linux-gnu/4.9.x/info" + // STDCXX_INCDIR="/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/include/g++-v4" + // We are looking for the paths listed in LDPATH=... . + if (ConfigFile) { + SmallVector<StringRef, 2> ConfigLines; + ConfigFile.get()->getBuffer().split(ConfigLines, "\n"); + for (StringRef ConfLine : ConfigLines) { + ConfLine = ConfLine.trim(); + if (ConfLine.consume_front("LDPATH=")) { + // Drop '"' from front and back if present. + ConfLine.consume_back("\""); + ConfLine.consume_front("\""); + // Get all paths sperated by ':' + ConfLine.split(GentooScanPaths, ':', -1, /*AllowEmpty*/ false); + } + } + } + // Test the path based on the version in /etc/env.d/gcc/config-{tuple}. + std::string basePath = "/usr/lib/gcc/" + ActiveVersion.first.str() + "/" + + ActiveVersion.second.str(); + GentooScanPaths.push_back(StringRef(basePath)); + + // Scan all paths for GCC libraries. + for (const auto &GentooScanPath : GentooScanPaths) { + std::string GentooPath = SysRootPrefix + std::string(GentooScanPath); if (D.getVFS().exists(GentooPath + "/crtbegin.o")) { if (!ScanGCCForMultilibs(TargetTriple, Args, GentooPath, NeedsBiarchSuffix)) - return false; + continue; Version = GCCVersion::Parse(ActiveVersion.second); GCCInstallPath = GentooPath; - GCCParentLibPath = GentooPath + "/../../.."; + GCCParentLibPath = GentooPath + std::string("/../../.."); GCCTriple.setTriple(ActiveVersion.first); IsValid = true; return true;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits