From 7cff6198ade4591b8bf2bfe4e94d7fe57241f926 Mon Sep 17 00:00:00 2001
From: Hongxu Jia <hongxu.jia@windriver.com>
Date: Mon, 25 Jan 2021 16:14:35 +0800
Subject: [PATCH] llvm/clang: Insert anchor for adding OE distro vendor names

This helps in making right detection for OE built gcc toolchains

The //CLANG_EXTRA_OE_VENDORS_CASES string is replaced with list of
additional Ceses based on CLANG_EXTRA_OE_VENDORS variable in
recipes-devtools/clang/llvm-project-source.inc:add_distro_vendor()

* For x86_64, set Yocto based GCC install search path

Under Yocto host, while using clang-native to build, it searches
install host gcc failed which causing the include file not found
[snip]
|clang++ -target x86_64-linux  -MMD -MF src/base/files/file_path_constants.o.d -I../../../tools/gn/src -I. \
-isystem/tmp-glibc/work/x86_64-linux/gn-native/87.0.4280.141-r0/recipe-sysroot-native/usr/include -O2 -pipe \
-std=c++17 -c ../../../tools/gn/src/base/files/file_path_constants.cc -o src/base/files/file_path_constants.o
|../../../tools/gn/src/base/files/file_path_constants.cc:7:10: fatal error: 'iterator' file not found
|#include <iterator>
|         ^~~~~~~~
[snip]

* Set three Yocto based GCC triple: poky, oe-core and wind river

Before aplly the patch
[snip]
$ ../recipe-sysroot-native/usr/bin/clang++ -v
clang version 11.0.1 (https://github.com/llvm/llvm-project 43ff75f2c3feef64f9d73328230d34dac8832a91)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir:tmp-glibc/work/x86_64-linux/gn-native/87.0.4280.141-r0/chromium-87.0.4280.141/../recipe-sysroot-native/usr/bin
[snip]

After this patch:
[snip]
$ ../recipe-sysroot-native/usr/bin/clang++ -v
clang version 11.0.1 (https://github.com/llvm/llvm-project 22c3241ff9a6224261df48d0258957fd8acc3d64)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir:tmp-glibc/work/x86_64-linux/gn-native/87.0.4280.141-r0/chromium-87.0.4280.141/../recipe-sysroot-native/usr/bin
Found candidate GCC installation: /usr/lib//x86_64-wrs-linux/10.1.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-wrs-linux/10.1.0
Selected GCC installation: /usr/lib//x86_64-wrs-linux/10.1.0
Candidate multilib: .;@m64
Selected multilib: .;@m64
[snip]

BTW, it is hardly to insert a triple by the replacement of TARGET_SYS
(=${TARGET_ARCH}${TARGET_VENDOR}-${TARGET_OS}), since TARGET_VENDOR
is different between clang and clang-native

* The //CLANG_EXTRA_OE_VENDORS_TRIPLES string is replaced with list of
additional triples based on CLANG_EXTRA_OE_VENDORS variable in
recipes-devtools/clang/llvm-project-source.inc:add_distro_vendor()

* Correct library search path for OpenEmbedded Host

For OpenEmbedded Host, the gcc install path is
/usr/lib/x86_64-[distroname]-linux/[gcc-version].
So the library search path is not found with default triple
'x86_64-linux-gnu' for x86_64. Causing following error:
[snip]
compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/bin/clang
-target x86_64-linux
-isystem/path/to/x86_64-linux/compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/include
-O2 -pipe
/path/to/compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/share/cmake-3.21/Modules/CMakeCCompilerABI.c`
|     /build/tmp-glibc/hosttools/ld: cannot find -lgcc
|     /build/tmp-glibc/hosttools/ld: cannot find -lgcc
|     clang-13: error: linker command failed with exit code 1 (use -v to see invocation)
[snip]

before this patch:
b59da142f2b0:$ /path/to/x86_64-linux/compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/bin/clang --print-search-dirs
programs: =/build/tmp-glibc/work/x86_64-linux/compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/bin
libraries: =/build/tmp-glibc/work/x86_64-linux/compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/lib/clang/13.0.1:/build/tmp-glibc/work/x86_64-linux/compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/bin/../lib://lib://usr/lib

after this patch:
b59da142f2b0:$ /path/to/x86_64-linux/compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/bin/clang --print-search-dirs
programs: =/build/tmp-glibc/work/x86_64-linux/compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/bin
libraries: =/build/tmp-glibc/work/x84_64-linux/compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/lib/clang/13.0.1:/usr/lib/x86_64-wrs-linux/10.2.0://lib/x86_64-wrs-linux://usr/lib/x86_64-wrs-linux:/build/tmp-glibc/work/x86_64-linux/compiler-rt-native/13.0.1-r0/recipe-sysroot-native/usr/bin/../lib://lib://usr/lib

Upstream-Status: Inappropriate [oe specific]

Signed-off-by: Martin Jansa <martin.jansa@gmail.com>
Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
Signed-off-by: Changqing Li <changqing.li@windriver.com>
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 clang/include/clang/Driver/Distro.h   | 2 ++
 clang/lib/Driver/Distro.cpp           | 1 +
 clang/lib/Driver/ToolChains/Gnu.cpp   | 1 +
 clang/lib/Driver/ToolChains/Linux.cpp | 1 +
 llvm/lib/TargetParser/Triple.cpp      | 2 +-
 5 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Driver/Distro.h b/clang/include/clang/Driver/Distro.h
index 5c25592e68ad..122095f98bca 100644
--- a/clang/include/clang/Driver/Distro.h
+++ b/clang/include/clang/Driver/Distro.h
@@ -47,6 +47,7 @@ public:
     RHEL7,
     Fedora,
     Gentoo,
+    //CLANG_EXTRA_OE_DISTRO_NAME
     OpenSUSE,
     UbuntuHardy,
     UbuntuIntrepid,
@@ -142,6 +143,7 @@ public:
 
   bool IsGentoo() const { return DistroVal == Gentoo; }
 
+  //CLANG_EXTRA_OE_DISTRO_CHECK
   /// @}
 };
 
diff --git a/clang/lib/Driver/Distro.cpp b/clang/lib/Driver/Distro.cpp
index 90e5a390be7e..1246236d62ef 100644
--- a/clang/lib/Driver/Distro.cpp
+++ b/clang/lib/Driver/Distro.cpp
@@ -43,6 +43,7 @@ static Distro::DistroType DetectOsRelease(llvm::vfs::FileSystem &VFS) {
                     .Case("sles", Distro::OpenSUSE)
                     .Case("opensuse", Distro::OpenSUSE)
                     .Case("exherbo", Distro::Exherbo)
+                    //CLANG_EXTRA_OE_DISTRO_CASE
                     .Default(Distro::UnknownDistro);
   return Version;
 }
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 6c52b337c16d..df84e07da1fc 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -2374,6 +2374,7 @@ void Generic_GCC::GCCInstallationDetector::AddDefaultGCCPrefixes(
       "x86_64-pc-linux-gnu",    "x86_64-redhat-linux6E",
       "x86_64-redhat-linux",    "x86_64-suse-linux",
       "x86_64-manbo-linux-gnu", "x86_64-slackware-linux",
+      "x86_64-oe-linux",//CLANG_EXTRA_OE_VENDORS_TRIPLES
       "x86_64-unknown-linux",   "x86_64-amazon-linux"};
   static const char *const X32Triples[] = {"x86_64-linux-gnux32",
                                            "x86_64-pc-linux-gnux32"};
diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp
index 3ee2d56df00f..bb7270bb08ba 100644
--- a/clang/lib/Driver/ToolChains/Linux.cpp
+++ b/clang/lib/Driver/ToolChains/Linux.cpp
@@ -81,6 +81,7 @@ std::string Linux::getMultiarchTriple(const Driver &D,
       return "x86_64-linux-android";
     if (TargetEnvironment == llvm::Triple::GNUX32)
       return "x86_64-linux-gnux32";
+    //CLANG_EXTRA_OE_DISTRO_TRIPLE
     return "x86_64-linux-gnu";
   case llvm::Triple::aarch64:
     if (IsAndroid)
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index 0584c941d2e6..6b9567ad0b8d 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -668,7 +668,7 @@ static Triple::VendorType parseVendor(StringRef VendorName) {
       .Case("mesa", Triple::Mesa)
       .Case("suse", Triple::SUSE)
       .Case("oe", Triple::OpenEmbedded)
-      .Case("intel", Triple::Intel)
+      .Case("intel", Triple::Intel) //CLANG_EXTRA_OE_VENDORS_CASES
       .Default(Triple::UnknownVendor);
 }
 
