1From 4dab11c8abdd1a48cd0df4475ef2f82732c7f7e3 Mon Sep 17 00:00:00 2001
2From: Changqing Li <changqing.li@windriver.com>
3Date: Tue, 7 Dec 2021 04:08:22 +0000
4Subject: [PATCH] Fix lib paths for OpenEmbedded Host
5
6Under OpenEmbedded Host, while building with clang-native, it cannot find
7the GCCInstallPath, which causing following error:
8[snip]
9compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/bin/clang
10-target x86_64-linux
11-isystem/path/to/x86_64-linux/compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/include
12-O2 -pipe
13/path/to/compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/share/cmake-3.21/Modules/CMakeCCompilerABI.c`
14hosttools/ld: cannot find crtbeginS.o: No such file or directory
15[snip]
16
17Before this patch:
18compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/bin/clang
19clang version 13.0.1 (https://github.com/llvm/llvm-project 08e3a5ccd952edee36b3c002e3a29c6b1b5153de)
20Target: x86_64-unknown-linux-gnu
21Thread model: posix
22InstalledDir: /build/tmp-glibc/work/x86_64-linux/compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/bin
23Found candidate GCC installation: /usr/lib/gcc/x86_64-wrs-linux/10.2.0
24
25After this patch:
26compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/bin/clang
27clang version 13.0.1 (https://github.com/llvm/llvm-project 08e3a5ccd952edee36b3c002e3a29c6b1b5153de)
28Thread model: posix
29InstalledDir: /build/tmp-glibc/work/x86_64-linux/compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/bin
30Found candidate GCC installation: /usr/lib/gcc/x86_64-wrs-linux/10.2.0
31Found candidate GCC installation: /usr/lib/x86_64-wrs-linux/10.2.0
32Selected GCC installation: /usr/lib/x86_64-wrs-linux/10.2.0
33Candidate multilib: .;@m64
34Selected multilib: .;@m64
35
36Summary:
37For OpenEmbedded Host, sysroots are of the form<sysroot>/usr/lib/<triple>/x.y.z.
38Take x86-64 as example, the default triple is x86_64-unknown-linux-gnu.
39For clang-native, the target vendor is '-unknown', need to test current distro
40to follow above form.
41
42Upstream-Status: Inappropriate [oe specific]
43
44Signed-off-by: Changqing Li <changqing.li@windriver.com>
45Signed-off-by: Khem Raj <raj.khem@gmail.com>
46---
47 clang/lib/Driver/ToolChains/Gnu.cpp | 5 ++++-
48 1 file changed, 4 insertions(+), 1 deletion(-)
49
50diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
51index 1e5a3cc2c1f1..d3574b86cb26 100644
52--- a/clang/lib/Driver/ToolChains/Gnu.cpp
53+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
54@@ -22,6 +22,7 @@
55 #include "clang/Driver/Options.h"
56 #include "clang/Driver/Tool.h"
57 #include "clang/Driver/ToolChain.h"
58+#include "clang/Driver/Distro.h"
59 #include "llvm/Option/ArgList.h"
60 #include "llvm/Support/CodeGen.h"
61 #include "llvm/Support/Path.h"
62@@ -2518,6 +2519,7 @@ void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
63     const llvm::Triple &TargetTriple, const ArgList &Args,
64     const std::string &LibDir, StringRef CandidateTriple,
65     bool NeedsBiarchSuffix, bool GCCDirExists, bool GCCCrossDirExists) {
66+  Distro Distro(D.getVFS(), TargetTriple);
67   // Locations relative to the system lib directory where GCC's triple-specific
68   // directories might reside.
69   struct GCCLibSuffix {
70@@ -2535,7 +2537,8 @@ void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
71       // files in that location, not just GCC installation data.
72       {CandidateTriple.str(), "..",
73        TargetTriple.getVendor() == llvm::Triple::Freescale ||
74-           TargetTriple.getVendor() == llvm::Triple::OpenEmbedded},
75+           TargetTriple.getVendor() == llvm::Triple::OpenEmbedded ||
76+           Distro.IsOpenEmbedded()},
77
78       // This is the normal place.
79       {"gcc/" + CandidateTriple.str(), "../..", GCCDirExists},
80