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