Index: clang/lib/Driver/ToolChains/Gnu.cpp =================================================================== --- clang/lib/Driver/ToolChains/Gnu.cpp +++ clang/lib/Driver/ToolChains/Gnu.cpp @@ -2088,6 +2088,7 @@ TripleNoVendor = TargetTriple.getArchName().str() + "-" + TargetTriple.getOSAndEnvironmentName().str(); StringRef TripleNoVendorRef(TripleNoVendor); + StringRef TripleArg; // If --gcc-install-dir= is specified, skip filesystem detection. if (const Arg *A = @@ -2156,6 +2157,9 @@ return; } + if (const Arg *A = Args.getLastArg(options::OPT_target)) + TripleArg = A->getValue(); + // Loop over the various components which exist and select the best GCC // installation available. GCC installs are ranked by version number. const GCCVersion VersionZero = GCCVersion::Parse("0.0.0"); @@ -2171,6 +2175,10 @@ // Maybe filter out /gcc and /gcc-cross. bool GCCDirExists = VFS.exists(LibDir + "/gcc"); bool GCCCrossDirExists = VFS.exists(LibDir + "/gcc-cross"); + // Try to match the exact target triple passed by -target option. + if (TripleArg.size() > 1) + ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, TripleArg, false, + GCCDirExists, GCCCrossDirExists); // Try to match the exact target triple first. ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, TargetTriple.str(), false, GCCDirExists, GCCCrossDirExists); Index: clang/test/Driver/cross-linux.c =================================================================== --- clang/test/Driver/cross-linux.c +++ clang/test/Driver/cross-linux.c @@ -25,9 +25,15 @@ // RUN: %clang -### -o %t %s 2>&1 -no-integrated-as -fuse-ld=ld \ // RUN: --gcc-toolchain=%S/Inputs/basic_cross_linux_tree/usr \ // RUN: --target=x86_64-unknown-linux-gnu -m32 \ -// RUN: | FileCheck --check-prefix=CHECK-I386 %s +// RUN: | FileCheck --check-prefix=CHECK-X86-64-M32 %s +// CHECK-X86-64-M32: "-cc1" "-triple" "i386-unknown-linux-gnu" +// CHECK-X86-64-M32: "{{.*}}/Inputs/basic_cross_linux_tree/usr/lib/gcc/x86_64-unknown-linux-gnu/10.2.0/../../../../x86_64-unknown-linux-gnu/bin{{/|\\\\}}as" "--32" +// CHECK-X96-64-M32: "{{.*}}/Inputs/basic_cross_linux_tree/usr/lib/gcc/x86_64-unknown-linux-gnu/10.2.0/../../../../x86_64-unknown-linux-gnu/bin{{/|\\\\}}ld" {{.*}} "-m" "elf_i386" // // RUN: %clang -### -o %t %s 2>&1 -no-integrated-as -fuse-ld=ld \ // RUN: --gcc-toolchain=%S/Inputs/basic_cross_linux_tree/usr \ // RUN: --target=i386-unknown-linux-gnu -m64 \ -// RUN: | FileCheck --check-prefix=CHECK-X86-64 %s +// RUN: | FileCheck --check-prefix=CHECK-I386-M64 %s +// CHECK-I386-M64: "-cc1" "-triple" "x86_64-unknown-linux-gnu" +// CHECK-I386-M64: "{{.*}}/Inputs/basic_cross_linux_tree/usr/lib/gcc/i386-unknown-linux-gnu/10.2.0/../../../../i386-unknown-linux-gnu/bin{{/|\\\\}}as" "--64" +// CHECK-I386-M64: "{{.*}}/Inputs/basic_cross_linux_tree/usr/lib/gcc/i386-unknown-linux-gnu/10.2.0/../../../../i386-unknown-linux-gnu/bin{{/|\\\\}}ld" {{.*}} "-m" "elf_x86_64"