fix haddock submodule pointer
[ghc-hetmet.git] / driver / gcc / gcc.c
index cd8a511..ea1f480 100644 (file)
@@ -1,95 +1,55 @@
 
+/* gcc on mingw is hardcoded to use /mingw (which is c:/mingw) to
+   find various files. If this is a different version of mingw to the
+   one that we have in the GHC tree then things can go wrong. We
+   therefore need to add various -B flags to the gcc commandline,
+   so that it uses our in-tree mingw. Hence this wrapper. */
+
+#include "cwrapper.h"
 #include "getLocation.h"
-#include <process.h>
+
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdarg.h>
-
-static void die(char *msg) {
-    fprintf(stderr, "%s", msg);
-    exit(1);
-}
-
-static char *mkString(const char *fmt, ...) {
-    char *p;
-    int i, j;
-    va_list argp;
-
-    va_start(argp, fmt);
-    i = vsnprintf(p, 0, fmt, argp);
-    va_end(argp);
-
-    if (i < 0) {
-        die("snprintf failed\n");
-    }
-
-    p = malloc(i + 1);
-    if (p == NULL) {
-        die("malloc failed\n");
-    }
-
-    va_start(argp, fmt);
-    j = vsnprintf(p, i + 1, fmt, argp);
-    va_end(argp);
-    if (i < 0) {
-        die("snprintf failed\n");
-    }
-
-    return p;
-}
-
-char *quote(char *str) {
-    char *quotedStr;
-    char *p;
-    int i;
-
-    quotedStr = malloc(2 * strlen(str) + 2 + 1);
-    if (quotedStr == NULL) {
-        die("malloc failed\n");
-    }
-    p = quotedStr;
-    *p++ = '"';
-    while (*str) {
-        if (*str == '"') {
-            *p++ = '\\';
-        }
-        *p++ = *str++;
-    }
-    *p++ = '"';
-    *p = '\0';
-
-    return quotedStr;
-}
 
 int main(int argc, char** argv) {
-    char *p;
     char *binDir;
     char *exePath;
-    char *bArg;
-    char **newArgv;
-    int i, j, ret;
+    char *preArgv[4];
+    char *oldPath;
+    char *newPath;
+    int n;
 
     binDir = getExecutablePath();
     exePath = mkString("%s/realgcc.exe", binDir);
 
+    /* We need programs like
+           inplace/mingw/libexec/gcc/mingw32/4.5.0/cc1.exe
+       to be able to find the DLLs in inplace/mingw/bin, so we need to
+       add it to $PATH */
+    oldPath = getenv("PATH");
+    if (!oldPath) {
+        die("Couldn't read PATH\n");
+    }
+    n = snprintf(NULL, 0, "PATH=%s;%s", binDir, oldPath);
+    n++;
+    newPath = malloc(n);
+    if (!newPath) {
+        die("Couldn't allocate space for PATH\n");
+    }
+    snprintf(newPath, n, "PATH=%s;%s", binDir, oldPath);
+    n = putenv(newPath);
+    if (n) {
+        die("putenv failed\n");
+    }
+
     /* Without these -B args, gcc will still work. However, if you
        have a mingw installation in c:/mingw then it will use files
        from that in preference to the in-tree files. */
+    preArgv[0] = mkString("-B%s", binDir);
+    preArgv[1] = mkString("-B%s/../lib", binDir);
+    preArgv[2] = mkString("-B%s/../lib/gcc/mingw32/4.5.2", binDir);
+    preArgv[3] = mkString("-B%s/../libexec/gcc/mingw32/4.5.2", binDir);
 
-    newArgv = malloc(sizeof(char *) * (argc + 4 + 1));
-    newArgv[0] = quote(exePath);
-    newArgv[1] = quote(mkString("-B%s", binDir));
-    newArgv[2] = quote(mkString("-B%s/../lib", binDir));
-    newArgv[3] = quote(mkString("-B%s/../lib/gcc/mingw32/3.4.5", binDir));
-    newArgv[4] = quote(mkString("-B%s/../libexec/gcc/mingw32/3.4.5", binDir));
-    for (i = 1; i < argc; i++) {
-        newArgv[4 + i] = quote(argv[i]);
-    }
-    newArgv[4 + argc] = NULL;
-    // execv(exePath, argv);
-    ret = spawnv(_P_WAIT, exePath, (const char* const*)newArgv);
-    if (errno) {
-        die("Spawn failed\n");
-    }
-    exit(ret);
+    run(exePath, 4, preArgv, argc - 1, argv + 1);
 }
+