| 1 |
/* choose a default D compiler |
|---|
| 2 |
* Copyright (C) 2007 Gregor Richards |
|---|
| 3 |
* You may do whatever you want with this code. |
|---|
| 4 |
* THERE IS NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. |
|---|
| 5 |
*/ |
|---|
| 6 |
|
|---|
| 7 |
#include <stdio.h> |
|---|
| 8 |
#include <stdlib.h> |
|---|
| 9 |
#include <string.h> |
|---|
| 10 |
#include <unistd.h> |
|---|
| 11 |
|
|---|
| 12 |
#include "whereami.h" |
|---|
| 13 |
|
|---|
| 14 |
int main(int argc, char **argv) |
|---|
| 15 |
{ |
|---|
| 16 |
char *compiler, *os, *corelib, *testfile, *cmd, *defaultfname, *dmddir, |
|---|
| 17 |
*gdcdir, *dir, *fil; |
|---|
| 18 |
int haveDMD, haveGDC, inSourceDir, res; |
|---|
| 19 |
FILE *fout; |
|---|
| 20 |
|
|---|
| 21 |
#ifdef __WIN32 |
|---|
| 22 |
os = "win"; |
|---|
| 23 |
#else |
|---|
| 24 |
os = "posix"; |
|---|
| 25 |
#endif |
|---|
| 26 |
|
|---|
| 27 |
// figure out our own path |
|---|
| 28 |
if (!whereAmI(argv[0], &dir, &fil)) { |
|---|
| 29 |
// assume "." |
|---|
| 30 |
dir = "."; |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
// check if we're running out of the sourcedir |
|---|
| 34 |
testfile = (char *) malloc(strlen(dir) + 8); |
|---|
| 35 |
sprintf(testfile, "%s/mars.c", dir); |
|---|
| 36 |
if (access(testfile, F_OK) == 0) { |
|---|
| 37 |
inSourceDir = 1; |
|---|
| 38 |
} else { |
|---|
| 39 |
inSourceDir = 0; |
|---|
| 40 |
} |
|---|
| 41 |
free(testfile); |
|---|
| 42 |
|
|---|
| 43 |
// check for DMD |
|---|
| 44 |
if (whereAmI("dmd", &dmddir, &fil)) { |
|---|
| 45 |
haveDMD = 1; |
|---|
| 46 |
} else { |
|---|
| 47 |
haveDMD = 0; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
// check for GDC |
|---|
| 51 |
if (whereAmI("gdmd", &gdcdir, &fil)) { |
|---|
| 52 |
haveGDC = 1; |
|---|
| 53 |
} else { |
|---|
| 54 |
haveGDC = 0; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
// choose the compiler |
|---|
| 58 |
if (haveDMD && haveGDC) { |
|---|
| 59 |
// some random choice ^^ |
|---|
| 60 |
#ifdef __WIN32 |
|---|
| 61 |
compiler = "dmd"; |
|---|
| 62 |
haveGDC = 0; |
|---|
| 63 |
#else |
|---|
| 64 |
compiler = "gdc"; |
|---|
| 65 |
haveDMD = 0; |
|---|
| 66 |
#endif |
|---|
| 67 |
} else if (haveDMD) { |
|---|
| 68 |
compiler = "dmd"; |
|---|
| 69 |
} else if (haveGDC) { |
|---|
| 70 |
compiler = "gdc"; |
|---|
| 71 |
} else { |
|---|
| 72 |
printf("Neither DMD nor GDC found in $PATH. Not configuring a default.\n" |
|---|
| 73 |
"Please add ONE of the following lines to your rebuild.conf/default file:\n" |
|---|
| 74 |
"profile=gdc-posix\n" |
|---|
| 75 |
"profile=gdc-posix-tango\n" |
|---|
| 76 |
"profile=gdc-win\n" |
|---|
| 77 |
"profile=gdc-win-tango\n" |
|---|
| 78 |
"profile=dmd-posix\n" |
|---|
| 79 |
"profile=dmd-posix-tango\n" |
|---|
| 80 |
"profile=dmd-win\n" |
|---|
| 81 |
"profile=dmd-win-tango\n"); |
|---|
| 82 |
return 0; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
// check which corelib is available |
|---|
| 86 |
if (inSourceDir) { |
|---|
| 87 |
testfile = (char *) malloc(strlen(dir) + 13); |
|---|
| 88 |
sprintf(testfile, "%s/testtango.d", dir); |
|---|
| 89 |
} else { |
|---|
| 90 |
testfile = (char *) malloc(strlen(dir) + 30); |
|---|
| 91 |
sprintf(testfile, "%s/../share/rebuild/testtango.d", dir); |
|---|
| 92 |
} |
|---|
| 93 |
printf("Ignore any error from GDC or DMD in the following lines.\n"); |
|---|
| 94 |
if (haveGDC) { |
|---|
| 95 |
cmd = (char *) malloc(strlen(testfile) + 22); |
|---|
| 96 |
sprintf(cmd, "gdc -c -fsyntax-only %s", testfile); |
|---|
| 97 |
res = system(cmd); |
|---|
| 98 |
} else if (haveDMD) { |
|---|
| 99 |
cmd = (char *) malloc(strlen(testfile) + 12); |
|---|
| 100 |
sprintf(cmd, "dmd -c -o- %s", testfile); |
|---|
| 101 |
res = system(cmd); |
|---|
| 102 |
} |
|---|
| 103 |
if (res == 0) { |
|---|
| 104 |
corelib = "-tango"; |
|---|
| 105 |
} else { |
|---|
| 106 |
corelib = ""; |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
// output the default file |
|---|
| 110 |
if (inSourceDir) { |
|---|
| 111 |
defaultfname = (char *) malloc(strlen(dir) + 22); |
|---|
| 112 |
sprintf(defaultfname, "%s/rebuild.conf/default", dir); |
|---|
| 113 |
} else if (strcmp(dir, "/usr/bin") == 0) { |
|---|
| 114 |
defaultfname = (char *) malloc(21); |
|---|
| 115 |
sprintf(defaultfname, "/etc/rebuild/default"); |
|---|
| 116 |
} else { |
|---|
| 117 |
defaultfname = (char *) malloc(strlen(dir) + 24); |
|---|
| 118 |
sprintf(defaultfname, "%s/../etc/rebuild/default", dir); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
fout = fopen(defaultfname, "w"); |
|---|
| 122 |
if (!fout) { perror("fopen"); return 1; } |
|---|
| 123 |
fprintf(fout, "profile=%s-%s%s\n", compiler, os, corelib); |
|---|
| 124 |
fclose(fout); |
|---|
| 125 |
|
|---|
| 126 |
return 0; |
|---|
| 127 |
} |
|---|