changeset 1299:847b767b2d0b

Factor out the code to find gcc, and add a command-line option to override the default executable name. This should help cross-compilation by allowing a cross-gcc to be specified from the configuration file instead of only through the CC environment variable.
author Frits van Bommel <fvbommel wxs.nl>
date Mon, 04 May 2009 13:32:16 +0200
parents 7e303f9f16c7
children 329e45865d84
files gen/linker.cpp gen/programs.cpp gen/programs.h gen/toobj.cpp
diffstat 4 files changed, 56 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/gen/linker.cpp	Mon May 04 12:08:30 2009 +0200
+++ b/gen/linker.cpp	Mon May 04 13:32:16 2009 +0200
@@ -14,6 +14,7 @@
 #include "gen/logger.h"
 #include "gen/cl_options.h"
 #include "gen/optimizer.h"
+#include "gen/programs.h"
 
 //////////////////////////////////////////////////////////////////////////////
 
@@ -216,25 +217,16 @@
     // error string
     std::string errstr;
 
-    const char *cc;
-#if !_WIN32
-    cc = getenv("CC");
-    if (!cc)
-#endif
-	cc = "gcc";
-
     // find gcc for linking
-    llvm::sys::Path gcc = llvm::sys::Program::FindProgramByName(cc);
-    if (gcc.isEmpty())
-    {
-        gcc.set(cc);
-    }
+    llvm::sys::Path gcc = getGcc();
+    // get a string version for argv[0]
+    std::string gccStr = gcc.toString();
 
     // build arguments
     std::vector<const char*> args;
 
     // first the program name ??
-    args.push_back(cc);
+    args.push_back(gccStr.c_str());
 
     // object files
     for (int i = 0; i < global.params.objfiles->dim; i++)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gen/programs.cpp	Mon May 04 13:32:16 2009 +0200
@@ -0,0 +1,41 @@
+#include "gen/programs.h"
+
+#include "llvm/Support/CommandLine.h"
+#include "llvm/System/Program.h"
+
+#include "root.h"       // error(char*)
+#include "mars.h"       // fatal()
+
+using namespace llvm;
+
+static cl::opt<std::string> gcc("gcc",
+    cl::desc("GCC to use for assembling and linking"),
+    cl::Hidden,
+    cl::ZeroOrMore);
+
+
+sys::Path getGcc() {
+    const char *cc = NULL;
+    
+    if (gcc.getNumOccurrences() > 0 && gcc.length() > 0)
+        cc = gcc.c_str();
+    
+    if (!cc)
+#if !_WIN32
+        cc = getenv("CC");
+    if (!cc)
+#endif
+        cc = "gcc";
+    
+    sys::Path path = sys::Program::FindProgramByName(cc);
+    if (path.empty() && !cc) {
+        if (cc) {
+            path.set(cc);
+        } else {
+            error("failed to locate gcc");
+            fatal();
+        }
+    }
+    
+    return path;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gen/programs.h	Mon May 04 13:32:16 2009 +0200
@@ -0,0 +1,8 @@
+#ifndef LDC_GEN_PROGRAMS_H
+#define LDC_GEN_PROGRAMS_H
+
+#include "llvm/System/Path.h"
+
+llvm::sys::Path getGcc();
+
+#endif
--- a/gen/toobj.cpp	Mon May 04 12:08:30 2009 +0200
+++ b/gen/toobj.cpp	Mon May 04 13:32:16 2009 +0200
@@ -18,7 +18,6 @@
 #include "llvm/PassManager.h"
 #include "llvm/LinkAllPasses.h"
 #include "llvm/System/Program.h"
-#include "llvm/System/Path.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Target/TargetMachine.h"
@@ -51,6 +50,7 @@
 #include "gen/cl_options.h"
 #include "gen/optimizer.h"
 #include "gen/llvm-version.h"
+#include "gen/programs.h"
 
 #include "ir/irvar.h"
 #include "ir/irmodule.h"
@@ -310,19 +310,7 @@
 {
     using namespace llvm;
 
-    const char *cc;
-#if !_WIN32
-    cc = getenv("CC");
-    if (!cc)
-#endif
-	cc = "gcc";
-
-    sys::Path gcc = llvm::sys::Program::FindProgramByName(cc);
-    if (gcc.empty())
-    {
-        error("failed to locate gcc");
-        fatal();
-    }
+    sys::Path gcc = getGcc();
 
     // Run GCC to assemble and link the program into native code.
     //