Mercurial > projects > ddmd
diff dmd/Util.d @ 22:fd4acc376c45
Implemented object file output and linking on linux.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Thu, 08 Apr 2010 04:21:03 +0100 |
parents | 5c9b78899f5d |
children | cfa3747ebe4c |
line wrap: on
line diff
--- a/dmd/Util.d Wed Apr 07 00:51:29 2010 +0100 +++ b/dmd/Util.d Thu Apr 08 04:21:03 2010 +0100 @@ -17,6 +17,10 @@ { import std.c.process : spawnl, spawnlp; } +version (POSIX) +{ + import core.sys.posix.unistd; +} import core.stdc.stdlib; import core.stdc.ctype; import core.stdc.stdarg; @@ -814,62 +818,54 @@ } return status; } else version (POSIX) {/// linux || __APPLE__ || __FreeBSD__ || __sun&&__SVR4 - assert(false, "Unimplemented"); - /+ pid_t childpid; int i; int status; // Build argv[] - Array argv; + Array argv = new Array(); - const char *cc = getenv("CC"); + const(char)* cc = core.stdc.stdlib.getenv("CC"); if (!cc) cc = "gcc"; - argv.push((void *)cc); - argv.insert(1, global.params.objfiles); + argv.push(cast(void *)cc); + Array objfiles = new Array; + for( i = 0; i < global.params.objfiles.dim; i++ ) + { string str = (cast(String)global.params.objfiles.data[i]).str; + objfiles.push(cast(void*)toStringz(str)); + } + argv.insert(1, objfiles); // None of that a.out stuff. Use explicit exe file name, or // generate one from name of first source file. - argv.push((void *)"-o"); + argv.push(cast(void *)cast(char*)"-o"); if (global.params.exefile) { - argv.push(global.params.exefile); + argv.push(cast(void*)toStringz(global.params.exefile)); } else { // Generate exe file name from first obj name - char *n = (char *)global.params.objfiles.data[0]; - char *e; - char *ex; - + string n = (cast(String)global.params.objfiles.data[0]).str; n = FileName.name(n); - e = FileName.ext(n); - if (e) - { - e--; // back up over '.' - ex = (char *)mem.malloc(e - n + 1); - memcpy(ex, n, e - n); - ex[e - n] = 0; - } - else - ex = (char *)"a.out"; // no extension, so give up - argv.push(ex); + string e = FileName.ext(n); + string ex = e ? n[0..$-(e.length+1)] : "a.out"; + + argv.push(cast(void*)toStringz(ex)); global.params.exefile = ex; } // Make sure path to exe file exists - { char *p = FileName.path(global.params.exefile); + { string p = FileName.path(global.params.exefile); FileName.ensurePathExists(p); - mem.free(p); } if (global.params.symdebug) - argv.push((void *)"-g"); + argv.push(cast(void *)cast(char*)"-g"); if (global.params.isX86_64) - argv.push((void *)"-m64"); + argv.push(cast(void *)cast(char*)"-m64"); else - argv.push((void *)"-m32"); + argv.push(cast(void *)cast(char*)"-m32"); if (0 && global.params.exefile) { @@ -882,16 +878,16 @@ * Thomas Kuehne has verified that it works with ld 2.16.1. * BUG: disabled because it causes exception handling to fail */ - argv.push((void *)"-Xlinker"); - argv.push((void *)"--gc-sections"); + argv.push(cast(void *)cast(char*)"-Xlinker"); + argv.push(cast(void *)cast(char*)"--gc-sections"); } for (i = 0; i < global.params.linkswitches.dim; i++) - { char *p = (char *)global.params.linkswitches.data[i]; + { char *p = cast(char *)global.params.linkswitches.data[i]; if (!p || !p[0] || !(p[0] == '-' && p[1] == 'l')) // Don't need -Xlinker if switch starts with -l - argv.push((void *)"-Xlinker"); - argv.push((void *) p); + argv.push(cast(void *)cast(char*)"-Xlinker"); + argv.push(cast(void *) p); } /* Add each library, prefixing it with "-l". @@ -903,17 +899,17 @@ * 4. standard libraries. */ for (i = 0; i < global.params.libfiles.dim; i++) - { char *p = (char *)global.params.libfiles.data[i]; + { char *p = cast(char *)global.params.libfiles.data[i]; size_t plen = strlen(p); if (plen > 2 && p[plen - 2] == '.' && p[plen -1] == 'a') - argv.push((void *)p); + argv.push(cast(void *)p); else { - char *s = (char *)mem.malloc(plen + 3); + char *s = cast(char *)malloc(plen + 3); s[0] = '-'; s[1] = 'l'; memcpy(s + 2, p, plen + 1); - argv.push((void *)s); + argv.push(cast(void *)s); } } @@ -923,20 +919,20 @@ const char *libname = (global.params.symdebug) ? global.params.debuglibname : global.params.defaultlibname; - char *buf = (char *)malloc(2 + strlen(libname) + 1); + char *buf = cast(char *)malloc(2 + strlen(libname) + 1); strcpy(buf, "-l"); strcpy(buf + 2, libname); - argv.push((void *)buf); // turns into /usr/lib/libphobos2.a + argv.push(cast(void *)buf); // turns into /usr/lib/libphobos2.a // argv.push((void *)"-ldruntime"); - argv.push((void *)"-lpthread"); - argv.push((void *)"-lm"); + argv.push(cast(void *)cast(char*)"-lpthread"); + argv.push(cast(void *)cast(char*)"-lm"); if (!global.params.quiet || global.params.verbose) { // Print it for (i = 0; i < argv.dim; i++) - printf("%s ", (char *)argv.data[i]); + printf("%s ", cast(char *)argv.data[i]); printf("\n"); fflush(stdout); } @@ -945,8 +941,8 @@ childpid = fork(); if (childpid == 0) { - execvp((char *)argv.data[0], (char **)argv.data); - perror((char *)argv.data[0]); // failed to execute + execvp(cast(char *)argv.data[0], cast(char **)argv.data); + perror(cast(char *)argv.data[0]); // failed to execute return -1; } @@ -956,7 +952,7 @@ if (status) printf("--- errorlevel %d\n", status); return status; - +/ + } else { writef ("Linking is not yet supported for this version of DMD.\n"); return -1;