From affcd7f14bca2d9ee95352cd979f66f55901272a Mon Sep 17 00:00:00 2001
From: Martin Kelly <mkelly@xevo.com>
Date: Fri, 19 May 2017 00:22:57 -0700
Subject: [PATCH] llvm: allow env override of exe and libdir path

When using a native llvm-config from inside a sysroot, we need llvm-config to
return the libraries, include directories, etc. from inside the sysroot rather
than from the native sysroot. Thus provide an env override for calling
llvm-config from a target sysroot.

Add YOCTO_ALTERNATE_LIBDIR and YOCTO_ALTERNATE_EXE_PATH env variables

Upstream-Status: Inappropriate [OE-specific]

Signed-off-by: Martin Kelly <mkelly@xevo.com>
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 llvm/tools/llvm-config/llvm-config.cpp | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/llvm/tools/llvm-config/llvm-config.cpp b/llvm/tools/llvm-config/llvm-config.cpp
index 8ed88f33ead4..9e26a2b41409 100644
--- a/llvm/tools/llvm-config/llvm-config.cpp
+++ b/llvm/tools/llvm-config/llvm-config.cpp
@@ -247,6 +247,13 @@ Typical components:\n\
 
 /// Compute the path to the main executable.
 std::string GetExecutablePath(const char *Argv0) {
+  // Hack for Yocto: we need to override the root path when we are using
+  // llvm-config from within a target sysroot.
+  const char *Sysroot = std::getenv("YOCTO_ALTERNATE_EXE_PATH");
+  if (Sysroot != nullptr) {
+    return Sysroot;
+  }
+
   // This just needs to be some symbol in the binary; C++ doesn't
   // allow taking the address of ::main however.
   void *P = (void *)(intptr_t)GetExecutablePath;
@@ -326,7 +333,7 @@ int main(int argc, char **argv) {
   // Compute various directory locations based on the derived location
   // information.
   std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir,
-              ActiveCMakeDir;
+              ActiveCMakeDir, BaseLibDir;
   std::string ActiveIncludeOption;
   if (IsInDevelopmentTree) {
     ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
@@ -367,7 +374,17 @@ int main(int argc, char **argv) {
       sys::fs::make_absolute(ActivePrefix, Path);
       ActiveBinDir = std::string(Path.str());
     }
-    ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
+    // Hack for Yocto: we need to override the lib path when we are using
+    // llvm-config from within a target sysroot since LLVM_LIBDIR_SUFFIX
+    // maybe different for host llvm vs target e.g. ppc64 Libdir=lib64 but
+    // x86_64 Libdir = lib
+    const char *YoctoLibDir = std::getenv("YOCTO_ALTERNATE_LIBDIR");
+    if (YoctoLibDir != nullptr) {
+      BaseLibDir = std::string(YoctoLibDir);
+    } else {
+      BaseLibDir = std::string("/lib") + LLVM_LIBDIR_SUFFIX;
+    }
+    ActiveLibDir = ActivePrefix + BaseLibDir;
     ActiveCMakeDir = ActiveLibDir + "/cmake/llvm";
     ActiveIncludeOption = "-I" + ActiveIncludeDir;
   }
