<div style="font-family: arial, helvetica, sans-serif; font-size: 10pt"><div dir="ltr">On Mon, Dec 10, 2012 at 10:45 PM, Kito Cheng <span dir="ltr"><<a href="mailto:kito@0xlab.org" target="_blank">kito@0xlab.org</a>></span> wrote:<br>
<div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi chapuni, chandlerc, rafael.espindola,<br>
<br>
Make default GCC install prefix and sysroot relocatable, they are use absolute path now, so this patch can relocated the path if their prefix is install prefix (LLVM_PREFIX).<br>
<br>
It's useful for build stand along toolchain, especially for build toolchain distribution:)<br></blockquote><div><br></div><div style>I don't understand. Neither GCC_INSTALL_PREFIX nor DEFAULT_SYSROOT are set by default. If you can set LLVM_PREFIX, you can just as easily set those two variables.</div>
<div style><br></div><div style>Note that the standard build doesn't set either variable *and* it produces a relocatable clang installation, if that's really what you're after: it uses the path of the clang executable itself to detect a prefix within which GCC might be installed.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<a href="http://llvm-reviews.chandlerc.com/D203" target="_blank">http://llvm-reviews.chandlerc.com/D203</a><br>
<br>
Files:<br>
  include/clang/Driver/Driver.h<br>
  lib/Driver/Driver.cpp<br>
  lib/Driver/ToolChains.cpp<br>
<br>
Index: include/clang/Driver/Driver.h<br>
===================================================================<br>
--- include/clang/Driver/Driver.h<br>
+++ include/clang/Driver/Driver.h<br>
@@ -81,6 +81,9 @@<br>
   /// sysroot, if present<br>
   std::string SysRoot;<br>
<br>
+  /// Default GCC install path<br>
+  std::string DefaultGCCInstallPath;<br>
+<br>
   /// If the standard library is used<br>
   bool UseStdLib;<br>
<br>
@@ -197,6 +200,11 @@<br>
   const std::string &getTitle() { return DriverTitle; }<br>
   void setTitle(std::string Value) { DriverTitle = Value; }<br>
<br>
+  /// \brief Get the default path of GCC install prefix<br>
+  const char *getDefaultGCCInstallPath() const {<br>
+    return DefaultGCCInstallPath.c_str();<br>
+  }<br>
+<br>
   /// \brief Get the path to the main clang executable.<br>
   const char *getClangProgramPath() const {<br>
     return ClangExecutable.c_str();<br>
Index: lib/Driver/Driver.cpp<br>
===================================================================<br>
--- lib/Driver/Driver.cpp<br>
+++ lib/Driver/Driver.cpp<br>
@@ -36,16 +36,26 @@<br>
 // FIXME: It would prevent to include llvm-config.h<br>
 // if it were included before system_error.h.<br>
 #include "clang/Config/config.h"<br>
+#include "llvm/Config/config.h" // for LLVM_PREFIX<br>
<br>
 using namespace clang::driver;<br>
 using namespace clang;<br>
<br>
+static bool isPrefixFolderOf(StringRef prefix, StringRef path) {<br>
+  bool IsEndWithBackslash = ((path.size() >= prefix.size()) &&<br>
+                              path[prefix.size()] == '/') ||<br>
+                            prefix[prefix.size()-1] == '/';<br>
+  bool IsPrefix = path.startswith(prefix);<br>
+  return (IsEndWithBackslash && IsPrefix) || prefix.equals(path);<br>
+}<br>
+<br>
 Driver::Driver(StringRef ClangExecutable,<br>
                StringRef DefaultTargetTriple,<br>
                StringRef DefaultImageName,<br>
                DiagnosticsEngine &Diags)<br>
   : Opts(createDriverOptTable()), Diags(Diags),<br>
     ClangExecutable(ClangExecutable), SysRoot(DEFAULT_SYSROOT),<br>
+    DefaultGCCInstallPath(GCC_INSTALL_PREFIX),<br>
     UseStdLib(true), DefaultTargetTriple(DefaultTargetTriple),<br>
     DefaultImageName(DefaultImageName),<br>
     DriverTitle("clang LLVM compiler"),<br>
@@ -59,6 +69,33 @@<br>
   Name = llvm::sys::path::stem(ClangExecutable);<br>
   Dir  = llvm::sys::path::parent_path(ClangExecutable);<br>
<br>
+  StringRef DefaultSysroot = DEFAULT_SYSROOT;<br>
+  StringRef DefaultGCCInstallPathRef = GCC_INSTALL_PREFIX;<br>
+  if (!DefaultSysroot.empty() || !DefaultGCCInstallPathRef.empty()) {<br>
+    // Try to relocate the sysroot/gcc install path when prefix of<br>
+    // ClangExecutable not equal to LLVM_PREFIX.<br>
+    StringRef LLVMPrefix = LLVM_PREFIX;<br>
+    if (!isPrefixFolderOf(LLVMPrefix, ClangExecutable)) {<br>
+      // We only recognize the case for default sysroot/gcc install path is<br>
+      // sub-folder of install prefix.<br>
+      StringRef UpperFolder = "/../";<br>
+      Twine RelocatedBase = Dir + UpperFolder;<br>
+      if (isPrefixFolderOf(LLVMPrefix, SysRoot)) {<br>
+        // Need relocate sysroot !<br>
+        StringRef RelativePath = DefaultSysroot.data() + LLVMPrefix.size();<br>
+        Twine RelocatedPath = RelocatedBase + RelativePath;<br>
+        SysRoot = RelocatedPath.str();<br>
+      }<br>
+      if (isPrefixFolderOf(LLVMPrefix, DefaultGCCInstallPath)) {<br>
+        // Need relocate default gcc install path !<br>
+        StringRef RelativePath = DefaultGCCInstallPathRef.data() +<br>
+                                 LLVMPrefix.size();<br>
+        Twine RelocatedPath = RelocatedBase + RelativePath;<br>
+        DefaultGCCInstallPath = RelocatedPath.str();<br>
+      }<br>
+    }<br>
+  }<br>
+<br>
   // Compute the path to the resource directory.<br>
   StringRef ClangResourceDir(CLANG_RESOURCE_DIR);<br>
   SmallString<128> P(Dir);<br>
Index: lib/Driver/ToolChains.cpp<br>
===================================================================<br>
--- lib/Driver/ToolChains.cpp<br>
+++ lib/Driver/ToolChains.cpp<br>
@@ -33,6 +33,7 @@<br>
 // FIXME: This needs to be listed last until we fix the broken include guards<br>
 // in these files and the LLVM config.h files.<br>
 #include "clang/Config/config.h" // for GCC_INSTALL_PREFIX<br>
+#include "llvm/Config/config.h" // for LLVM_PREFIX<br>
<br>
 #include <cstdlib> // ::getenv<br>
<br>
@@ -978,11 +979,11 @@<br>
   return false;<br>
 }<br>
<br>
-static StringRef getGCCToolchainDir(const ArgList &Args) {<br>
+static StringRef getGCCToolchainDir(const ArgList &Args, const Driver &D) {<br>
   const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);<br>
   if (A)<br>
     return A->getValue();<br>
-  return GCC_INSTALL_PREFIX;<br>
+  return D.getDefaultGCCInstallPath();<br>
 }<br>
<br>
 /// \brief Construct a GCCInstallationDetector from the driver.<br>
@@ -1017,7 +1018,7 @@<br>
   SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),<br>
                                        D.PrefixDirs.end());<br>
<br>
-  StringRef GCCToolchainDir = getGCCToolchainDir(Args);<br>
+  StringRef GCCToolchainDir = getGCCToolchainDir(Args, D);<br>
   if (GCCToolchainDir != "") {<br>
     if (GCCToolchainDir.back() == '/')<br>
       GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /<br>
<br>_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
<br></blockquote></div><br></div></div></div>