diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/clang/lib/Driver/ToolChains/Gnu.cpp @@ -2217,6 +2217,16 @@ return false; } +static bool CmpGCCPrefix(std::string PrefixA, std::string PrefixB) { + StringRef VersionAText = llvm::sys::path::filename(PrefixA); + StringRef VersionBText = llvm::sys::path::filename(PrefixB); + Generic_GCC::GCCVersion VersionA = + Generic_GCC::GCCVersion::Parse(VersionAText); + Generic_GCC::GCCVersion VersionB = + Generic_GCC::GCCVersion::Parse(VersionBText); + return VersionA < VersionB; +} + void Generic_GCC::GCCInstallationDetector::AddDefaultGCCPrefixes( const llvm::Triple &TargetTriple, SmallVectorImpl &Prefixes, StringRef SysRoot) { @@ -2227,6 +2237,7 @@ // so we need to find those /usr/gcc/*/lib/gcc libdirs and go with // /usr/gcc/ as a prefix. + SmallVector SolarisPrefixes; std::string PrefixDir = concat(SysRoot, "/usr/gcc"); std::error_code EC; for (llvm::vfs::directory_iterator LI = D.getVFS().dir_begin(PrefixDir, EC), @@ -2244,8 +2255,11 @@ if (!D.getVFS().exists(CandidateLibPath)) continue; - Prefixes.push_back(CandidatePrefix); + SolarisPrefixes.push_back(CandidatePrefix); } + // Sort in reverse order so GCCInstallationDetector::init picks the latest. + std::sort(SolarisPrefixes.rbegin(), SolarisPrefixes.rend(), CmpGCCPrefix); + Prefixes.append(SolarisPrefixes); return; } diff --git a/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/11/lib/gcc/sparcv9-sun-solaris2.11/11.4.0/crtbegin.o b/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/11/lib/gcc/sparcv9-sun-solaris2.11/11.4.0/crtbegin.o new file mode 100644 diff --git a/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/11/lib/gcc/sparcv9-sun-solaris2.11/11.4.0/sparcv8plus/crtbegin.o b/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/11/lib/gcc/sparcv9-sun-solaris2.11/11.4.0/sparcv8plus/crtbegin.o new file mode 100644 diff --git a/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/11/lib/gcc/x86_64-pc-solaris2.11/11.4.0/32/crtbegin.o b/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/11/lib/gcc/x86_64-pc-solaris2.11/11.4.0/32/crtbegin.o new file mode 100644 diff --git a/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/11/lib/gcc/x86_64-pc-solaris2.11/11.4.0/crtbegin.o b/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/11/lib/gcc/x86_64-pc-solaris2.11/11.4.0/crtbegin.o new file mode 100644 diff --git a/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/4.7/lib/gcc/i386-pc-solaris2.11/4.7.3/amd64/crtbegin.o b/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/4.7/lib/gcc/i386-pc-solaris2.11/4.7.3/amd64/crtbegin.o new file mode 100644 diff --git a/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/4.7/lib/gcc/i386-pc-solaris2.11/4.7.3/crtbegin.o b/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/4.7/lib/gcc/i386-pc-solaris2.11/4.7.3/crtbegin.o new file mode 100644 diff --git a/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/4.7/lib/gcc/sparc-sun-solaris2.11/4.7.3/crtbegin.o b/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/4.7/lib/gcc/sparc-sun-solaris2.11/4.7.3/crtbegin.o new file mode 100644 diff --git a/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/4.7/lib/gcc/sparc-sun-solaris2.11/4.7.3/sparcv9/crtbegin.o b/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/4.7/lib/gcc/sparc-sun-solaris2.11/4.7.3/sparcv9/crtbegin.o new file mode 100644 diff --git a/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/7/lib/gcc/sparcv9-sun-solaris2.11/7.5.0/32/crtbegin.o b/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/7/lib/gcc/sparcv9-sun-solaris2.11/7.5.0/32/crtbegin.o new file mode 100644 diff --git a/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/7/lib/gcc/sparcv9-sun-solaris2.11/7.5.0/crtbegin.o b/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/7/lib/gcc/sparcv9-sun-solaris2.11/7.5.0/crtbegin.o new file mode 100644 diff --git a/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.5.0/32/crtbegin.o b/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.5.0/32/crtbegin.o new file mode 100644 diff --git a/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.5.0/crtbegin.o b/clang/test/Driver/Inputs/solaris_multi_gcc_tree/usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.5.0/crtbegin.o new file mode 100644 diff --git a/clang/test/Driver/solaris-multi-gcc-search.test b/clang/test/Driver/solaris-multi-gcc-search.test new file mode 100644 --- /dev/null +++ b/clang/test/Driver/solaris-multi-gcc-search.test @@ -0,0 +1,51 @@ +/// Check that clang uses the latest available Solaris GCC installation. + +/// Check sparc-sun-solaris2.11, 32-bit +// RUN: %clang -v 2>&1 --target=sparc-sun-solaris2.11 \ +// RUN: --gcc-toolchain="" \ +// RUN: --sysroot=%S/Inputs/solaris_multi_gcc_tree \ +// RUN: | FileCheck --check-prefix=CHECK-SPARC %s +// CHECK-SPARC: Found candidate GCC installation: {{.*}}11/lib/gcc/sparcv9-sun-solaris2.11/11.4.0 +// CHECK-SPARC-NEXT: Selected GCC installation: {{.*}}11/lib/gcc/sparcv9-sun-solaris2.11/11.4.0 +// CHECK-SPARC-NEXT: Candidate multilib: .;@m64 +// CHECK-SPARC-NEXT: Candidate multilib: sparcv8plus;@m32 +// CHECK-SPARC-NEXT: Selected multilib: sparcv8plus;@m32 + +/// Check i386-pc-solaris2.11, 32-bit +// RUN: %clang -v 2>&1 --target=i386-pc-solaris2.11 \ +// RUN: --gcc-toolchain="" \ +// RUN: --sysroot=%S/Inputs/solaris_multi_gcc_tree \ +// RUN: | FileCheck --check-prefix=CHECK-I386 %s +// CHECK-I386: Found candidate GCC installation: {{.*}}11/lib/gcc/x86_64-pc-solaris2.11/11.4.0 +// CHECK-I386-NEXT: Selected GCC installation: {{.*}}11/lib/gcc/x86_64-pc-solaris2.11/11.4.0 +// CHECK-I386-NEXT: Candidate multilib: .;@m64 +// CHECK-I386-NEXT: Candidate multilib: 32;@m32 +// CHECK-I386-NEXT: Selected multilib: 32;@m32 + +/// Check sparcv9-sun-solaris2.11, 64-bit +// RUN: %clang -v 2>&1 --target=sparcv9-sun-solaris2.11 \ +// RUN: --gcc-toolchain="" \ +// RUN: --sysroot=%S/Inputs/solaris_multi_gcc_tree \ +// RUN: | FileCheck --check-prefix=CHECK-SPARCV9 %s +// CHECK-SPARCV9: Found candidate GCC installation: {{.*}}11/lib/gcc/sparcv9-sun-solaris2.11/11.4.0 +// CHECK-SPARCV9-NEXT: Selected GCC installation: {{.*}}11/lib/gcc/sparcv9-sun-solaris2.11/11.4.0 +// CHECK-SPARCV9-NEXT: Candidate multilib: .;@m64 +// CHECK-SPARCV9-NEXT: Candidate multilib: sparcv8plus;@m32 +// CHECK-SPARCV9-NEXT: Selected multilib: .;@m64 + +/// Check x86_64-pc-solaris2.11, 64-bit +// RUN: %clang -v 2>&1 --target=x86_64-pc-solaris2.11 \ +// RUN: --gcc-toolchain="" \ +// RUN: --sysroot=%S/Inputs/solaris_multi_gcc_tree \ +// RUN: | FileCheck --check-prefix=CHECK-X86_64 %s + +/// Check amd64-pc-solaris2.11, 64-bit +// RUN: %clang -v 2>&1 --target=amd64-pc-solaris2.11 \ +// RUN: --gcc-toolchain="" \ +// RUN: --sysroot=%S/Inputs/solaris_multi_gcc_tree \ +// RUN: | FileCheck --check-prefix=CHECK-X86_64 %s +// CHECK-X86_64: Found candidate GCC installation: {{.*}}11/lib/gcc/x86_64-pc-solaris2.11/11.4.0 +// CHECK-X86_64-NEXT: Selected GCC installation: {{.*}}11/lib/gcc/x86_64-pc-solaris2.11/11.4.0 +// CHECK-X86_64-NEXT: Candidate multilib: .;@m64 +// CHECK-X86_64-NEXT: Candidate multilib: 32;@m32 +// CHECK-X86_64-NEXT: Selected multilib: .;@m64