1
|
1
|
|
2
|
|
3 // Copyright (c) 1999-2007 by Digital Mars
|
|
4 // All Rights Reserved
|
|
5 // written by Walter Bright
|
|
6 // http://www.digitalmars.com
|
|
7 // License for redistribution is by either the Artistic License
|
|
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
|
|
9 // See the included readme.txt for details.
|
|
10
|
|
11
|
|
12 #include <stdio.h>
|
|
13 #include <ctype.h>
|
|
14 #include <assert.h>
|
|
15 #include <stdarg.h>
|
|
16 #include <string.h>
|
|
17 #include <stdlib.h>
|
|
18
|
|
19 #if _WIN32
|
|
20 #include <process.h>
|
|
21 #endif
|
|
22
|
|
23 #if linux
|
|
24 #include <sys/types.h>
|
|
25 #include <sys/wait.h>
|
|
26 #include <unistd.h>
|
|
27 #endif
|
|
28
|
|
29 #include "root.h"
|
|
30
|
|
31 #include "mars.h"
|
|
32
|
|
33 #include "mem.h"
|
|
34
|
|
35 int executecmd(char *cmd, char *args, int useenv);
|
|
36 int executearg0(char *cmd, char *args);
|
|
37
|
|
38 /*****************************
|
|
39 * Run the linker. Return status of execution.
|
|
40 */
|
|
41
|
|
42 int runLINK()
|
|
43 {
|
|
44 #if _WIN32
|
|
45 assert(0 && "linking not done for win32");
|
|
46
|
|
47 char *p;
|
|
48 int i;
|
|
49 int status;
|
|
50 OutBuffer cmdbuf;
|
|
51
|
|
52 global.params.libfiles->push((void *) "user32");
|
|
53 global.params.libfiles->push((void *) "kernel32");
|
|
54
|
|
55 for (i = 0; i < global.params.objfiles->dim; i++)
|
|
56 {
|
|
57 if (i)
|
|
58 cmdbuf.writeByte('+');
|
|
59 p = (char *)global.params.objfiles->data[i];
|
|
60 char *ext = FileName::ext(p);
|
|
61 if (ext)
|
|
62 cmdbuf.write(p, ext - p - 1);
|
|
63 else
|
|
64 cmdbuf.writestring(p);
|
|
65 }
|
|
66 cmdbuf.writeByte(',');
|
|
67 if (global.params.exefile)
|
|
68 cmdbuf.writestring(global.params.exefile);
|
|
69 else
|
|
70 { // Generate exe file name from first obj name
|
|
71 char *n = (char *)global.params.objfiles->data[0];
|
|
72 char *ex;
|
|
73
|
|
74 n = FileName::name(n);
|
|
75 FileName *fn = FileName::forceExt(n, "exe");
|
|
76 global.params.exefile = fn->toChars();
|
|
77 }
|
|
78
|
|
79 // Make sure path to exe file exists
|
|
80 { char *p = FileName::path(global.params.exefile);
|
|
81 FileName::ensurePathExists(p);
|
|
82 mem.free(p);
|
|
83 }
|
|
84
|
|
85 cmdbuf.writeByte(',');
|
|
86 if (global.params.run)
|
|
87 cmdbuf.writestring("nul");
|
|
88 // if (mapfile)
|
|
89 // cmdbuf.writestring(output);
|
|
90 cmdbuf.writeByte(',');
|
|
91
|
|
92 for (i = 0; i < global.params.libfiles->dim; i++)
|
|
93 {
|
|
94 if (i)
|
|
95 cmdbuf.writeByte('+');
|
|
96 cmdbuf.writestring((char *) global.params.libfiles->data[i]);
|
|
97 }
|
|
98
|
|
99 if (global.params.deffile)
|
|
100 {
|
|
101 cmdbuf.writeByte(',');
|
|
102 cmdbuf.writestring(global.params.deffile);
|
|
103 }
|
|
104
|
|
105 /* Eliminate unnecessary trailing commas */
|
|
106 while (1)
|
|
107 { i = cmdbuf.offset;
|
|
108 if (!i || cmdbuf.data[i - 1] != ',')
|
|
109 break;
|
|
110 cmdbuf.offset--;
|
|
111 }
|
|
112
|
|
113 if (global.params.resfile)
|
|
114 {
|
|
115 cmdbuf.writestring("/RC:");
|
|
116 cmdbuf.writestring(global.params.resfile);
|
|
117 }
|
|
118
|
|
119 #if 0
|
|
120 if (mapfile)
|
|
121 cmdbuf.writestring("/m");
|
|
122 if (debuginfo)
|
|
123 cmdbuf.writestring("/li");
|
|
124 if (codeview)
|
|
125 {
|
|
126 cmdbuf.writestring("/co");
|
|
127 if (codeview3)
|
|
128 cmdbuf.writestring(":3");
|
|
129 }
|
|
130 #else
|
|
131 if (global.params.symdebug)
|
|
132 cmdbuf.writestring("/co");
|
|
133 #endif
|
|
134
|
|
135 cmdbuf.writestring("/noi");
|
|
136 for (i = 0; i < global.params.linkswitches->dim; i++)
|
|
137 {
|
|
138 cmdbuf.writestring((char *) global.params.linkswitches->data[i]);
|
|
139 }
|
|
140 cmdbuf.writeByte(';');
|
|
141
|
|
142 p = cmdbuf.toChars();
|
|
143
|
|
144 FileName *lnkfilename = NULL;
|
|
145 size_t plen = strlen(p);
|
|
146 if (plen > 7000)
|
|
147 {
|
|
148 lnkfilename = FileName::forceExt(global.params.exefile, "lnk");
|
|
149 File flnk(lnkfilename);
|
|
150 flnk.setbuffer(p, plen);
|
|
151 flnk.ref = 1;
|
|
152 if (flnk.write())
|
|
153 error("error writing file %s", lnkfilename);
|
|
154 if (lnkfilename->len() < plen)
|
|
155 sprintf(p, "@%s", lnkfilename->toChars());
|
|
156 }
|
|
157
|
|
158 char *linkcmd = getenv("LINKCMD");
|
|
159 if (!linkcmd)
|
|
160 linkcmd = "link";
|
|
161 status = executecmd(linkcmd, p, 1);
|
|
162 if (lnkfilename)
|
|
163 {
|
|
164 remove(lnkfilename->toChars());
|
|
165 delete lnkfilename;
|
|
166 }
|
|
167 return status;
|
|
168 #elif linux
|
|
169 pid_t childpid;
|
|
170 int i;
|
|
171 int status;
|
|
172
|
|
173 // Build argv[]
|
|
174 Array argv;
|
|
175
|
|
176 //char *cc = getenv("CC");
|
|
177 //if (!cc)
|
|
178 //cc = "gcc";
|
|
179 char *cc = "llvm-ld";
|
|
180 argv.push((void *)cc);
|
|
181
|
|
182 // None of that a.out stuff. Use explicit exe file name, or
|
|
183 // generate one from name of first source file.
|
|
184 OutBuffer* exestr = new OutBuffer;
|
|
185 if (global.params.exefile)
|
|
186 {
|
|
187 exestr->printf("-o=%s", global.params.exefile);
|
|
188 argv.push(exestr->toChars());
|
|
189 }
|
|
190 else
|
|
191 { // Generate exe file name from first obj name
|
|
192 char *n = (char *)global.params.objfiles->data[0];
|
|
193 char *e;
|
|
194 char *ex;
|
|
195
|
|
196 n = FileName::name(n);
|
|
197 e = FileName::ext(n);
|
|
198 if (e)
|
|
199 {
|
|
200 e--; // back up over '.'
|
|
201 ex = (char *)mem.malloc(e - n + 1);
|
|
202 memcpy(ex, n, e - n);
|
|
203 ex[e - n] = 0;
|
|
204 }
|
|
205 else
|
|
206 ex = (char *)"a.out"; // no extension, so give up
|
|
207 exestr->printf("-o=%s", ex);
|
|
208 ex = exestr->toChars();
|
|
209 argv.push(ex);
|
|
210 global.params.exefile = ex;
|
|
211 }
|
|
212
|
|
213 // Make sure path to exe file exists
|
|
214 { char *p = FileName::path(global.params.exefile);
|
|
215 FileName::ensurePathExists(p);
|
|
216 mem.free(p);
|
|
217 }
|
|
218
|
|
219 argv.insert(argv.dim, global.params.libfiles);
|
|
220
|
|
221 //if (global.params.symdebug)
|
|
222 //argv.push((void *)"-g");
|
|
223
|
|
224 //argv.push((void *)"-m32");
|
|
225
|
|
226
|
|
227 if (!global.params.optimize)
|
|
228 argv.push((void *)"-disable-opt");
|
|
229 else {
|
|
230 const char* s = 0;
|
|
231 switch(global.params.optimizeLevel) {
|
|
232 case 0:
|
|
233 s = "-O0"; break;
|
|
234 case 1:
|
|
235 s = "-O1"; break;
|
|
236 case 2:
|
|
237 s = "-O2"; break;
|
|
238 case 3:
|
|
239 s = "-O3"; break;
|
|
240 case 4:
|
|
241 s = "-O4"; break;
|
|
242 case 5:
|
|
243 s = "-O5"; break;
|
|
244 default:
|
|
245 assert(0);
|
|
246 }
|
|
247 argv.push((void*)s);
|
|
248 }
|
|
249
|
|
250 #if 0
|
|
251 if (0 && global.params.exefile)
|
|
252 {
|
|
253 /* This switch enables what is known as 'smart linking'
|
|
254 * in the Windows world, where unreferenced sections
|
|
255 * are removed from the executable. It eliminates unreferenced
|
|
256 * functions, essentially making a 'library' out of a module.
|
|
257 * Although it is documented to work with ld version 2.13,
|
|
258 * in practice it does not, but just seems to be ignored.
|
|
259 * Thomas Kuehne has verified that it works with ld 2.16.1.
|
|
260 * BUG: disabled because it causes exception handling to fail
|
|
261 */
|
|
262 argv.push((void *)"-Xlinker");
|
|
263 argv.push((void *)"--gc-sections");
|
|
264 }
|
|
265 #endif
|
|
266
|
|
267 for (i = 0; i < global.params.linkswitches->dim; i++)
|
|
268 { char *p = (char *)global.params.linkswitches->data[i];
|
|
269 //if (!p || !p[0] || !(p[0] == '-' && p[1] == 'l'))
|
|
270 //{ // Don't need -Xlinker if switch starts with -l
|
|
271 // argv.push((void *)"-Xlinker");
|
|
272 //}
|
|
273 argv.push((void *) p);
|
|
274 }
|
|
275
|
|
276 argv.push((void*)"-native");
|
|
277
|
|
278 /* Standard libraries must go after user specified libraries
|
|
279 * passed with -l.
|
|
280 */
|
|
281 //argv.push((void *)"-lphobos"); // turns into /usr/lib/libphobos.a
|
|
282 //argv.push((void *)"-lpthread");
|
|
283 //argv.push((void *)"-lm");
|
|
284
|
|
285 std::string corelibpath = global.params.runtimeImppath;
|
|
286 corelibpath.append("/llvmdcore.bc");
|
|
287 argv.append(global.params.objfiles);
|
|
288 argv.push((void *)corelibpath.c_str());
|
|
289
|
|
290 if (!global.params.quiet)
|
|
291 {
|
|
292 // Print it
|
|
293 for (i = 0; i < argv.dim; i++)
|
|
294 printf("%s ", (char *)argv.data[i]);
|
|
295 printf("\n");
|
|
296 fflush(stdout);
|
|
297 }
|
|
298
|
|
299 argv.push(NULL);
|
|
300 childpid = fork();
|
|
301 if (childpid == 0)
|
|
302 {
|
|
303 execvp((char *)argv.data[0], (char **)argv.data);
|
|
304 perror((char *)argv.data[0]); // failed to execute
|
|
305 return -1;
|
|
306 }
|
|
307
|
|
308 waitpid(childpid, &status, 0);
|
|
309
|
|
310 status=WEXITSTATUS(status);
|
|
311 if (status)
|
|
312 printf("--- errorlevel %d\n", status);
|
|
313 return status;
|
|
314 #else
|
|
315 printf ("Linking is not yet supported for this version of LLVMDMD.\n");
|
|
316 return -1;
|
|
317 #endif
|
|
318 }
|
|
319
|
|
320 /**********************************
|
|
321 * Delete generated EXE file.
|
|
322 */
|
|
323
|
|
324 void deleteExeFile()
|
|
325 {
|
|
326 if (global.params.exefile)
|
|
327 {
|
|
328 //printf("deleteExeFile() %s\n", global.params.exefile);
|
|
329 remove(global.params.exefile);
|
|
330 }
|
|
331 }
|
|
332
|
|
333 /******************************
|
|
334 * Execute a rule. Return the status.
|
|
335 * cmd program to run
|
|
336 * args arguments to cmd, as a string
|
|
337 * useenv if cmd knows about _CMDLINE environment variable
|
|
338 */
|
|
339
|
|
340 #if _WIN32
|
|
341 int executecmd(char *cmd, char *args, int useenv)
|
|
342 {
|
|
343 int status;
|
|
344 char *buff;
|
|
345 size_t len;
|
|
346
|
|
347 if (!global.params.quiet || global.params.verbose)
|
|
348 {
|
|
349 printf("%s %s\n", cmd, args);
|
|
350 fflush(stdout);
|
|
351 }
|
|
352
|
|
353 if ((len = strlen(args)) > 255)
|
|
354 { char *q;
|
|
355 static char envname[] = "@_CMDLINE";
|
|
356
|
|
357 envname[0] = '@';
|
|
358 switch (useenv)
|
|
359 { case 0: goto L1;
|
|
360 case 2: envname[0] = '%'; break;
|
|
361 }
|
|
362 q = (char *) alloca(sizeof(envname) + len + 1);
|
|
363 sprintf(q,"%s=%s", envname + 1, args);
|
|
364 status = putenv(q);
|
|
365 if (status == 0)
|
|
366 args = envname;
|
|
367 else
|
|
368 {
|
|
369 L1:
|
|
370 error("command line length of %d is too long",len);
|
|
371 }
|
|
372 }
|
|
373
|
|
374 status = executearg0(cmd,args);
|
|
375 #if _WIN32
|
|
376 if (status == -1)
|
|
377 status = spawnlp(0,cmd,cmd,args,NULL);
|
|
378 #endif
|
|
379 // if (global.params.verbose)
|
|
380 // printf("\n");
|
|
381 if (status)
|
|
382 {
|
|
383 if (status == -1)
|
|
384 printf("Can't run '%s', check PATH\n", cmd);
|
|
385 else
|
|
386 printf("--- errorlevel %d\n", status);
|
|
387 }
|
|
388 return status;
|
|
389 }
|
|
390 #endif
|
|
391
|
|
392 /**************************************
|
|
393 * Attempt to find command to execute by first looking in the directory
|
|
394 * where DMD was run from.
|
|
395 * Returns:
|
|
396 * -1 did not find command there
|
|
397 * !=-1 exit status from command
|
|
398 */
|
|
399
|
|
400 #if _WIN32
|
|
401 int executearg0(char *cmd, char *args)
|
|
402 {
|
|
403 char *file;
|
|
404 char *argv0 = global.params.argv0;
|
|
405
|
|
406 //printf("argv0='%s', cmd='%s', args='%s'\n",argv0,cmd,args);
|
|
407
|
|
408 // If cmd is fully qualified, we don't do this
|
|
409 if (FileName::absolute(cmd))
|
|
410 return -1;
|
|
411
|
|
412 file = FileName::replaceName(argv0, cmd);
|
|
413
|
|
414 //printf("spawning '%s'\n",file);
|
|
415 #if _WIN32
|
|
416 return spawnl(0,file,file,args,NULL);
|
|
417 #elif linux
|
|
418 char *full;
|
|
419 int cmdl = strlen(cmd);
|
|
420
|
|
421 full = (char*) mem.malloc(cmdl + strlen(args) + 2);
|
|
422 if (full == NULL)
|
|
423 return 1;
|
|
424 strcpy(full, cmd);
|
|
425 full [cmdl] = ' ';
|
|
426 strcpy(full + cmdl + 1, args);
|
|
427
|
|
428 int result = system(full);
|
|
429
|
|
430 mem.free(full);
|
|
431 return result;
|
|
432 #else
|
|
433 assert(0);
|
|
434 #endif
|
|
435 }
|
|
436 #endif
|
|
437
|
|
438 /***************************************
|
|
439 * Run the compiled program.
|
|
440 * Return exit status.
|
|
441 */
|
|
442
|
|
443 int runProgram()
|
|
444 {
|
|
445 //printf("runProgram()\n");
|
|
446 if (global.params.verbose)
|
|
447 {
|
|
448 printf("%s", global.params.exefile);
|
|
449 for (size_t i = 0; i < global.params.runargs_length; i++)
|
|
450 printf(" %s", (char *)global.params.runargs[i]);
|
|
451 printf("\n");
|
|
452 }
|
|
453
|
|
454 // Build argv[]
|
|
455 Array argv;
|
|
456
|
|
457 argv.push((void *)global.params.exefile);
|
|
458 for (size_t i = 0; i < global.params.runargs_length; i++)
|
|
459 { char *a = global.params.runargs[i];
|
|
460
|
|
461 #if _WIN32
|
|
462 // BUG: what about " appearing in the string?
|
|
463 if (strchr(a, ' '))
|
|
464 { char *b = (char *)mem.malloc(3 + strlen(a));
|
|
465 sprintf(b, "\"%s\"", a);
|
|
466 a = b;
|
|
467 }
|
|
468 #endif
|
|
469 argv.push((void *)a);
|
|
470 }
|
|
471 argv.push(NULL);
|
|
472
|
|
473 #if _WIN32
|
|
474 char *ex = FileName::name(global.params.exefile);
|
|
475 if (ex == global.params.exefile)
|
|
476 ex = FileName::combine(".", ex);
|
|
477 else
|
|
478 ex = global.params.exefile;
|
|
479 return spawnv(0,ex,(char **)argv.data);
|
|
480 #elif linux
|
|
481 pid_t childpid;
|
|
482 int status;
|
|
483
|
|
484 childpid = fork();
|
|
485 if (childpid == 0)
|
|
486 {
|
|
487 char *fn = (char *)argv.data[0];
|
|
488 if (!FileName::absolute(fn))
|
|
489 { // Make it "./fn"
|
|
490 fn = FileName::combine(".", fn);
|
|
491 }
|
|
492 execv(fn, (char **)argv.data);
|
|
493 perror(fn); // failed to execute
|
|
494 return -1;
|
|
495 }
|
|
496
|
|
497 waitpid(childpid, &status, 0);
|
|
498
|
|
499 status = WEXITSTATUS(status);
|
|
500 return status;
|
|
501 #else
|
|
502 assert(0);
|
|
503 #endif
|
|
504 }
|