gollvm: change handling of system install dir for package search

Revamp the way the system install directory is used when forming the
Go package search path. Specifically, avoid hard-coding the install
prefix into the search path, and instead locate the installed std
packages relative to the path of the compiler binary itself.

Change-Id: I3c10596118ece320c90ed17032b432f486e3cc04
Reviewed-on: https://go-review.googlesource.com/116655
Reviewed-by: Cherry Zhang <cherryyz@google.com>
diff --git a/driver/CompileGo.cpp b/driver/CompileGo.cpp
index e790391..75c2d94 100644
--- a/driver/CompileGo.cpp
+++ b/driver/CompileGo.cpp
@@ -223,6 +223,11 @@
     if (!args_.hasArg(gollvm::options::OPT_S))
       errs() << " " << "-S";
     for (auto arg : args_) {
+      // Special case for -L. Here even if the user said "-L /x"
+      // we render it as -L/x so as to be compatible with existing
+      // code in the imported that expects the former and not the latter.
+      if (arg->getOption().matches(gollvm::options::OPT_L))
+        errs() << " -L" << arg->getValue();
       if (arg->getOption().getGroup().isValid() &&
           (arg->getOption().getGroup().getID() ==
            gollvm::options::OPT_Link_Group))
@@ -235,7 +240,8 @@
         continue;
       dumpArg(*arg, hashHashHash);
     }
-    errs() << " " << "-L" << GOLLVM_INSTALL_LIBDIR << " " << "-o";
+
+    errs() << " " << "-L" << driver_.installedLibDir() << " " << "-o";
     quoteDump(output.file(), hashHashHash);
     errs() << "\n";
   }
@@ -554,8 +560,8 @@
     dirs.push_back(pdir);
   }
 
-  // Finish up with system install dir.
-  dirs.push_back(GOLLVM_INSTALL_LIBDIR);
+  // Finish up with the library dir from the install.
+  dirs.push_back(driver_.installedLibDir());
 
   // First pass to add go/<version> and go/<version>/triple variants
   for (auto &dir : dirs) {
diff --git a/driver/Driver.cpp b/driver/Driver.cpp
index cc1be39..3b31b7f 100644
--- a/driver/Driver.cpp
+++ b/driver/Driver.cpp
@@ -56,6 +56,13 @@
 {
 }
 
+std::string Driver::installedLibDir()
+{
+  llvm::SmallString<256> ldir(installDir_);
+  llvm::sys::path::append(ldir, "../lib64");
+  return std::string(ldir.str());
+}
+
 // TODO: create a mechanism for capturing release tag/branch, and/or
 // git/svn revision for LLVM, gollvm, and so on.
 
diff --git a/driver/Driver.h b/driver/Driver.h
index 6e42123..60dbf7d 100644
--- a/driver/Driver.h
+++ b/driver/Driver.h
@@ -94,6 +94,9 @@
   // Install directory of compiler binary.
   std::string installDir() { return installDir_; }
 
+  // Installed lib dir (binary dir above plus ../lib64)
+  std::string installedLibDir();
+
   // Prefix directories (supplied via -B args)
   const std::vector<std::string> &prefixes() const { return prefixes_; }