Thanks James and Rafael for your review and testing.

As I have grepped for GCC_INSTALL_PREFIX to see all the places that
I have to modify, I have missed the changes to CMakeLists.txt.
So I also have added a description for GCC_INSTALL_PREFIX.

As suggested by Rafael, I factored out the other uses of
OPT__sysroot_EQ into a call to Compilation::getSysRoot()

Please let me know if the patch can still be improved before commit.

Thanks again for your very good feedback!
Sebastian
--
Qualcomm Innovation Center, Inc is a member of Code Aurora Forum
From a840bb3b093b7b9aa5a2789cda29325a02eab6f6 Mon Sep 17 00:00:00 2001
From: Sebastian Pop <[email protected]>
Date: Thu, 9 Feb 2012 13:02:01 -0600
Subject: [PATCH] add configure flag --with-default-sysroot

---
 CMakeLists.txt                     |    2 ++
 autoconf/configure.ac              |    7 +++++++
 configure                          |   17 ++++++++++++++++-
 include/llvm/Config/config.h.cmake |    3 +++
 include/llvm/Config/config.h.in    |    3 +++
 5 files changed, 31 insertions(+), 1 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5313d11..eb5c960 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -69,6 +69,8 @@ set(LLVM_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
 set(LLVM_TOOLS_BINARY_DIR ${LLVM_BINARY_DIR}/bin)
 set(LLVM_EXAMPLES_BINARY_DIR ${LLVM_BINARY_DIR}/examples)
 set(LLVM_LIBDIR_SUFFIX "" CACHE STRING "Define suffix of library directory name (32/64)" )
+set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed" )
+set(DEFAULT_SYSROOT "" CACHE PATH "Default <path> to all compiler invocations for --sysroot=<path>" )
 
 set(LLVM_ALL_TARGETS
   ARM
diff --git a/autoconf/configure.ac b/autoconf/configure.ac
index 5ea2e30..6ab1544 100644
--- a/autoconf/configure.ac
+++ b/autoconf/configure.ac
@@ -845,6 +845,13 @@ AC_ARG_WITH(gcc-toolchain,
 AC_DEFINE_UNQUOTED(GCC_INSTALL_PREFIX,"$withval",
                    [Directory where gcc is installed.])
 
+AC_ARG_WITH(sysroot,
+  AS_HELP_STRING([--with-default-sysroot],
+    [Add --sysroot=<path> to all compiler invocations.]),,
+    withval="")
+AC_DEFINE_UNQUOTED(DEFAULT_SYSROOT,"$withval",
+                   [Default <path> to all compiler invocations for --sysroot=<path>.])
+
 dnl Allow linking of LLVM with GPLv3 binutils code.
 AC_ARG_WITH(binutils-include,
   AS_HELP_STRING([--with-binutils-include],
diff --git a/configure b/configure
index 00f49df..254e96b 100755
--- a/configure
+++ b/configure
@@ -1442,6 +1442,7 @@ Optional Packages:
   --with-c-include-dirs   Colon separated list of directories clang will
                           search for headers
   --with-gcc-toolchain    Directory where gcc is installed.
+  --with-default-sysroot  Add --sysroot=<path> to all compiler invocations.
   --with-binutils-include Specify path to binutils/include/ containing
                           plugin-api.h file for gold plugin.
   --with-bug-report-url   Specify the URL where bug reports should be
@@ -5592,6 +5593,20 @@ _ACEOF
 
 
 
+# Check whether --with-sysroot was given.
+if test "${with_sysroot+set}" = set; then
+  withval=$with_sysroot;
+else
+  withval=""
+fi
+
+
+cat >>confdefs.h <<_ACEOF
+#define DEFAULT_SYSROOT "$withval"
+_ACEOF
+
+
+
 # Check whether --with-binutils-include was given.
 if test "${with_binutils_include+set}" = set; then
   withval=$with_binutils_include;
@@ -10395,7 +10410,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<EOF
-#line 10398 "configure"
+#line 10413 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
diff --git a/include/llvm/Config/config.h.cmake b/include/llvm/Config/config.h.cmake
index c475014..e8a3e2b 100644
--- a/include/llvm/Config/config.h.cmake
+++ b/include/llvm/Config/config.h.cmake
@@ -17,6 +17,9 @@
 /* Directories clang will search for headers */
 #define C_INCLUDE_DIRS "${C_INCLUDE_DIRS}"
 
+/* Default <path> to all compiler invocations for --sysroot=<path>. */
+#define DEFAULT_SYSROOT "${DEFAULT_SYSROOT}"
+
 /* Define if CBE is enabled for printf %a output */
 #cmakedefine ENABLE_CBE_PRINTF_A ${ENABLE_CBE_PRINTF_A}
 
diff --git a/include/llvm/Config/config.h.in b/include/llvm/Config/config.h.in
index 1a996a2..62376b1 100644
--- a/include/llvm/Config/config.h.in
+++ b/include/llvm/Config/config.h.in
@@ -12,6 +12,9 @@
 /* Directories clang will search for headers */
 #undef C_INCLUDE_DIRS
 
+/* Default <path> to all compiler invocations for --sysroot=<path>. */
+#undef DEFAULT_SYSROOT
+
 /* Define if CBE is enabled for printf %a output */
 #undef ENABLE_CBE_PRINTF_A
 
-- 
1.7.5.4

From 52df926dc1a905ca22cbd8e3372477e1be492f19 Mon Sep 17 00:00:00 2001
From: Sebastian Pop <[email protected]>
Date: Thu, 9 Feb 2012 13:06:08 -0600
Subject: [PATCH] use DEFAULT_SYSROOT

---
 include/clang/Config/config.h.cmake |    9 ++++++---
 include/clang/Config/config.h.in    |    9 ++++++---
 include/clang/Driver/Compilation.h  |    5 +++++
 lib/Driver/Compilation.cpp          |   19 +++++++++++++++++++
 lib/Driver/Driver.cpp               |   16 +++++++++++++---
 lib/Driver/Tools.cpp                |   10 ++++++----
 6 files changed, 55 insertions(+), 13 deletions(-)

diff --git a/include/clang/Config/config.h.cmake b/include/clang/Config/config.h.cmake
index bd5dc31..c18c4cc 100644
--- a/include/clang/Config/config.h.cmake
+++ b/include/clang/Config/config.h.cmake
@@ -4,8 +4,11 @@
 /* Relative directory for resource files */
 #define CLANG_RESOURCE_DIR "${CLANG_RESOURCE_DIR}"
 
-/* Directory where gcc is installed. */
-#define GCC_INSTALL_PREFIX "${GCC_INSTALL_PREFIX}"
-
 /* Directories clang will search for headers */
 #define C_INCLUDE_DIRS "${C_INCLUDE_DIRS}"
+
+/* Default <path> to all compiler invocations for --sysroot=<path>. */
+#define DEFAULT_SYSROOT "${DEFAULT_SYSROOT}"
+
+/* Directory where gcc is installed. */
+#define GCC_INSTALL_PREFIX "${GCC_INSTALL_PREFIX}"
diff --git a/include/clang/Config/config.h.in b/include/clang/Config/config.h.in
index 3f5d503..24ed6bd 100644
--- a/include/clang/Config/config.h.in
+++ b/include/clang/Config/config.h.in
@@ -9,10 +9,13 @@
 /* Relative directory for resource files */
 #undef CLANG_RESOURCE_DIR
 
-/* Directory where gcc is installed. */
-#undef GCC_INSTALL_PREFIX
-
 /* Directories clang will search for headers */
 #undef C_INCLUDE_DIRS
 
+/* Default <path> to all compiler invocations for --sysroot=<path>. */
+#undef DEFAULT_SYSROOT
+
+/* Directory where gcc is installed. */
+#undef GCC_INSTALL_PREFIX
+
 #endif
diff --git a/include/clang/Driver/Compilation.h b/include/clang/Driver/Compilation.h
index fd88c3a..b4b2a67 100644
--- a/include/clang/Driver/Compilation.h
+++ b/include/clang/Driver/Compilation.h
@@ -92,6 +92,11 @@ public:
     return FailureResultFiles;
   }
 
+  /// Reads the --sysroot flag from the command Args or sets the default
+  /// value of the sysroot from configure flag --with-default-sysroot.
+  /// Retunrs the sysroot path after removing the trailing /.
+  StringRef getSysRoot() const;
+
   /// getArgsForToolChain - Return the derived argument list for the
   /// tool chain \arg TC (or the default tool chain, if TC is not
   /// specified).
diff --git a/lib/Driver/Compilation.cpp b/lib/Driver/Compilation.cpp
index 42c8449..d750df4 100644
--- a/lib/Driver/Compilation.cpp
+++ b/lib/Driver/Compilation.cpp
@@ -10,6 +10,7 @@
 #include "clang/Driver/Compilation.h"
 
 #include "clang/Driver/Action.h"
+#include "clang/Driver/Arg.h"
 #include "clang/Driver/ArgList.h"
 #include "clang/Driver/Driver.h"
 #include "clang/Driver/DriverDiagnostic.h"
@@ -22,6 +23,8 @@
 #include <sys/stat.h>
 #include <errno.h>
 
+#include "clang/Config/config.h"
+
 using namespace clang::driver;
 using namespace clang;
 
@@ -55,6 +58,22 @@ Compilation::~Compilation() {
   }
 }
 
+/// Reads the --sysroot flag from the command Args or sets the default
+/// value of the sysroot from configure flag --with-default-sysroot.
+/// Retunrs the sysroot path after removing the trailing /.
+StringRef Compilation::getSysRoot() const {
+  llvm::SmallString<128> Res;
+  if (const Arg *A = Args->getLastArg(options::OPT__sysroot_EQ))
+    Res = A->getValue(getArgs());
+  else
+    Res = DEFAULT_SYSROOT;
+
+  if (Res != "" && Res.back() == '/')
+    llvm::sys::path::remove_filename(Res); // remove the /
+
+  return Res;
+}
+
 const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
                                                        const char *BoundArch) {
   if (!TC)
diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp
index c985e2f..b83e525 100644
--- a/lib/Driver/Driver.cpp
+++ b/lib/Driver/Driver.cpp
@@ -323,8 +323,20 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
     A->claim();
     PrefixDirs.push_back(A->getValue(*Args, 0));
   }
+
   if (const Arg *A = Args->getLastArg(options::OPT__sysroot_EQ))
     SysRoot = A->getValue(*Args);
+  else
+    SysRoot = DEFAULT_SYSROOT;
+
+  if (SysRoot != "") {
+    llvm::SmallString<128> Prefix(SysRoot);
+    if (Prefix.back() == '/') {
+      llvm::sys::path::remove_filename(Prefix); // remove the /
+      SysRoot = Prefix.str();
+    }
+  }
+
   if (Args->hasArg(options::OPT_nostdlib))
     UseStdLib = false;
 
@@ -659,9 +671,7 @@ bool Driver::HandleImmediateArgs(const Compilation &C) {
     llvm::outs() << "\n";
     llvm::outs() << "libraries: =" << ResourceDir;
 
-    std::string sysroot;
-    if (Arg *A = C.getArgs().getLastArg(options::OPT__sysroot_EQ))
-      sysroot = A->getValue(C.getArgs());
+    StringRef sysroot = C.getSysRoot();
 
     for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
            ie = TC.getFilePaths().end(); it != ie; ++it) {
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index c9db799..64bb6a2 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -373,10 +373,11 @@ void Clang::AddPreprocessingOptions(Compilation &C,
 
   // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
   // -isysroot to the CC1 invocation.
-  if (Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
+  StringRef sysroot = C.getSysRoot();
+  if (sysroot != "") {
     if (!Args.hasArg(options::OPT_isysroot)) {
       CmdArgs.push_back("-isysroot");
-      CmdArgs.push_back(A->getValue(Args));
+      CmdArgs.push_back(sysroot.str().c_str());
     }
   }
   
@@ -3906,9 +3907,10 @@ void darwin::Link::AddLinkArgs(Compilation &C,
 
   // Give --sysroot= preference, over the Apple specific behavior to also use
   // --isysroot as the syslibroot.
-  if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
+  StringRef sysroot = C.getSysRoot();
+  if (sysroot != "") {
     CmdArgs.push_back("-syslibroot");
-    CmdArgs.push_back(A->getValue(Args));
+    CmdArgs.push_back(sysroot.str().c_str());
   } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
     CmdArgs.push_back("-syslibroot");
     CmdArgs.push_back(A->getValue(Args));
-- 
1.7.5.4

_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to