Mercurial > projects > ddmd
annotate main.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 | 2cc604139636 |
children | 3f834bed4f13 |
rev | line source |
---|---|
0 | 1 module main; |
2 | |
3 import dmd.Array; | |
4 import dmd.Module; | |
5 import dmd.Global; | |
6 import dmd.VersionCondition; | |
7 import dmd.DebugCondition; | |
8 import dmd.Loc; | |
9 import dmd.Lexer; | |
10 import dmd.OutBuffer; | |
11 import dmd.FileName; | |
12 import dmd.Type; | |
13 import dmd.File; | |
14 import dmd.Id; | |
15 import dmd.Identifier; | |
16 import dmd.Library; | |
17 import dmd.TOK; | |
18 import dmd.String; | |
19 import dmd.backend.glue; | |
20 | |
21 import std.stdarg; | |
22 import std.string : toStringz; | |
23 import std.contracts; | |
24 | |
25 import core.stdc.string; | |
26 import core.stdc.stdio; | |
27 import core.stdc.ctype; | |
28 import core.stdc.errno; | |
29 import core.stdc.stdlib; | |
30 import core.stdc.limits; | |
31 | |
32 import core.memory; | |
33 | |
34 import dlib.CrashHandler; | |
35 import dbg.ui.CrashWindow; | |
36 | |
37 import dmd.Util; | |
38 | |
39 bool loop = true; | |
40 | |
41 enum ExitCode | |
42 { | |
43 EXIT_SUCCESS = 0, | |
44 } | |
45 | |
46 version (Windows) | |
47 { | |
48 private import core.stdc.wchar_; | |
49 } | |
50 | |
51 extern (C) void gc_init(); | |
52 extern (C) void gc_term(); | |
53 extern (C) void _minit(); | |
54 extern (C) void _moduleCtor(); | |
55 extern (C) void _moduleDtor(); | |
56 extern (C) void thread_joinAll(); | |
57 | |
58 shared bool _d_isHalting = false; | |
59 | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
60 version (Windows) |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
61 { |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
62 import win32.windows; |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
63 } |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
64 /* Link to druntimes main() */ |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
65 /+ |
0 | 66 extern (C) int main(int argc, char **argv) |
67 { | |
68 char[][] args; | |
69 int result; | |
70 | |
71 version (OSX) | |
72 { /* OSX does not provide a way to get at the top of the | |
73 * stack, except for the magic value 0xC0000000. | |
74 * But as far as the gc is concerned, argv is at the top | |
75 * of the main thread's stack, so save the address of that. | |
76 */ | |
77 __osx_stack_end = cast(void*)&argv; | |
78 } | |
79 | |
80 version (Posix) | |
81 { | |
82 _STI_monitor_staticctor(); | |
83 _STI_critical_init(); | |
84 } | |
85 | |
86 version (Windows) | |
87 { | |
88 wchar_t* wcbuf = GetCommandLineW(); | |
89 size_t wclen = wcslen(wcbuf); | |
90 int wargc = 0; | |
91 wchar_t** wargs = CommandLineToArgvW(wcbuf, &wargc); | |
92 assert(wargc == argc); | |
93 | |
94 char* cargp = null; | |
95 size_t cargl = WideCharToMultiByte(65001, 0, wcbuf, wclen, null, 0, null, null); | |
96 | |
97 cargp = cast(char*) alloca(cargl); | |
98 args = ((cast(char[]*) alloca(wargc * (char[]).sizeof)))[0 .. wargc]; | |
99 | |
100 for (size_t i = 0, p = 0; i < wargc; i++) | |
101 { | |
102 int wlen = wcslen(wargs[i]); | |
103 int clen = WideCharToMultiByte(65001, 0, &wargs[i][0], wlen, null, 0, null, null); | |
104 args[i] = cargp[p .. p+clen]; | |
105 p += clen; assert(p <= cargl); | |
106 WideCharToMultiByte(65001, 0, &wargs[i][0], wlen, &args[i][0], clen, null, null); | |
107 } | |
108 LocalFree(cast(HANDLE)wargs); | |
109 wargs = null; | |
110 wargc = 0; | |
111 } | |
112 else version (Posix) | |
113 { | |
114 char[]* am = cast(char[]*) malloc(argc * (char[]).sizeof); | |
115 scope(exit) free(am); | |
116 | |
117 for (size_t i = 0; i < argc; i++) | |
118 { | |
119 auto len = strlen(argv[i]); | |
120 am[i] = argv[i][0 .. len]; | |
121 } | |
122 args = am[0 .. argc]; | |
123 } | |
124 | |
125 gc_init(); | |
126 version (Windows) | |
127 _minit(); | |
128 _moduleCtor(); | |
129 | |
4 | 130 GC.disable(); |
0 | 131 CrashHandlerInit(); |
132 | |
133 result = main(cast(string[])args); | |
134 | |
135 thread_joinAll(); | |
136 _d_isHalting = true; | |
137 _moduleDtor(); | |
138 gc_term(); | |
139 | |
140 version (Posix) | |
141 { | |
142 _STD_critical_term(); | |
143 _STD_monitor_staticdtor(); | |
144 } | |
145 | |
146 return result; | |
147 } | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
148 +/ |
0 | 149 int main(string[] args) |
150 { | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
151 GC.disable(); |
0 | 152 Array files = new Array(); |
153 Array libmodules = new Array(); | |
154 Module m; | |
155 int status = ExitCode.EXIT_SUCCESS; | |
156 int argcstart = args.length; | |
157 int setdebuglib = 0; | |
158 | |
159 global = new Global(); | |
160 | |
161 /// if (response_expand(&argc,&argv)) // expand response files | |
162 /// error("can't open response file"); | |
163 | |
164 files.reserve(args.length - 1); | |
165 | |
166 // Set default values | |
167 global.params.argv0 = args[0]; | |
168 global.params.link = 1; | |
169 global.params.useAssert = 1; | |
170 global.params.useInvariants = 1; | |
171 global.params.useIn = 1; | |
172 global.params.useOut = 1; | |
173 global.params.useArrayBounds = 1; | |
174 global.params.useSwitchError = 1; | |
175 global.params.useInline = 0; | |
176 global.params.obj = 1; | |
177 global.params.Dversion = 2; | |
178 global.params.quiet = 1; | |
179 | |
180 global.params.linkswitches = new Array(); | |
181 global.params.libfiles = new Array(); | |
182 global.params.objfiles = new Array(); | |
183 global.params.ddocfiles = new Array(); | |
184 | |
185 version (TARGET_WINDOS) { | |
186 global.params.defaultlibname = "phobos"; | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
187 } else version (POSIX) { //#elif TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS |
0 | 188 global.params.defaultlibname = "phobos2"; |
189 } else version (TARGET_NET) { | |
190 } else { | |
191 static assert(false, "fix this"); | |
192 } | |
193 | |
194 // Predefine version identifiers | |
195 VersionCondition.addPredefinedGlobalIdent("DigitalMars"); | |
196 | |
197 version (TARGET_WINDOS) { | |
198 VersionCondition.addPredefinedGlobalIdent("Windows"); | |
199 global.params.isWindows = 1; | |
200 version (TARGET_NET) { | |
201 // TARGET_NET macro is NOT mutually-exclusive with TARGET_WINDOS | |
202 VersionCondition.addPredefinedGlobalIdent("D_NET"); | |
203 } | |
204 } else version (TARGET_LINUX) { | |
205 VersionCondition.addPredefinedGlobalIdent("Posix"); | |
206 VersionCondition.addPredefinedGlobalIdent("linux"); | |
207 global.params.isLinux = 1; | |
208 } else version (TARGET_OSX) { | |
209 VersionCondition.addPredefinedGlobalIdent("Posix"); | |
210 VersionCondition.addPredefinedGlobalIdent("OSX"); | |
211 global.params.isOSX = 1; | |
212 | |
213 // For legacy compatibility | |
214 VersionCondition.addPredefinedGlobalIdent("darwin"); | |
215 } else version (TARGET_FREEBSD) { | |
216 VersionCondition.addPredefinedGlobalIdent("Posix"); | |
217 VersionCondition.addPredefinedGlobalIdent("FreeBSD"); | |
218 global.params.isFreeBSD = 1; | |
219 } else version (TARGET_SOLARIS) { | |
220 VersionCondition.addPredefinedGlobalIdent("Posix"); | |
221 VersionCondition.addPredefinedGlobalIdent("Solaris"); | |
222 global.params.isSolaris = 1; | |
223 } else { | |
224 static assert (false, "fix this"); | |
225 } | |
226 | |
227 VersionCondition.addPredefinedGlobalIdent("LittleEndian"); | |
228 //VersionCondition.addPredefinedGlobalIdent("D_Bits"); | |
229 version (DMDV2) { | |
230 VersionCondition.addPredefinedGlobalIdent("D_Version2"); | |
231 } | |
232 VersionCondition.addPredefinedGlobalIdent("all"); | |
233 | |
234 version (_WIN32) { | |
235 inifile(args[0], "sc.ini"); | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
236 } else version (POSIX) {///linux || __APPLE__ || __FreeBSD__ || __sun&&__SVR4 |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
237 inifile(args[0], "dmd.conf"); |
0 | 238 } else { |
239 static assert (false, "fix this"); | |
240 } | |
241 args = getenv_setargv("DFLAGS", args); | |
242 | |
243 version (disabled) { | |
244 for (i = 0; i < argc; i++) | |
245 { | |
246 writef("argv[%d] = '%s'\n", i, argv[i]); | |
247 } | |
248 } | |
249 | |
250 foreach(i; 1..args.length) | |
251 { | |
252 auto arg = args[i]; | |
253 auto p = arg.ptr; | |
254 if (*p == '-') | |
255 { | |
256 arg = arg[1..$]; | |
257 if (arg == "d") | |
258 global.params.useDeprecated = 1; | |
259 else if (arg == "c") | |
260 global.params.link = 0; | |
261 else if (arg == "cov") | |
262 global.params.cov = 1; | |
263 ///version (XXX) {// TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS | |
264 /// else if (arg == "fPIC") | |
265 /// global.params.pic = 1; | |
266 ///} | |
267 else if (arg == "multiobj") | |
268 global.params.multiobj = 1; | |
269 else if (arg == "g") | |
270 global.params.symdebug = 1; | |
271 else if (arg == "gc") | |
272 global.params.symdebug = 2; | |
273 else if (arg == "gt") | |
274 { error("use -profile instead of -gt\n"); | |
275 global.params.trace = 1; | |
276 } | |
277 else if (arg == "m64") | |
278 global.params.isX86_64 = 1; | |
279 else if (arg == "profile") | |
280 global.params.trace = 1; | |
281 else if (arg == "v") | |
282 global.params.verbose = 1; | |
283 ///version (DMDV2) { | |
284 else if (arg == "vtls") | |
285 global.params.vtls = 1; | |
286 ///} | |
287 else if (arg == "v1") | |
288 { | |
289 version (DMDV1) { | |
290 global.params.Dversion = 1; | |
291 } else { | |
292 error("use DMD 1.0 series compilers for -v1 switch"); | |
293 break; | |
294 } | |
295 } | |
296 else if (arg == "w") | |
297 global.params.warnings = 1; | |
298 else if (arg == "O") | |
299 global.params.optimize = 1; | |
300 else if (p[1] == 'o') | |
301 { | |
302 switch (p[2]) | |
303 { | |
304 case '-': | |
305 global.params.obj = 0; | |
306 break; | |
307 | |
308 case 'd': | |
309 if (!p[3]) | |
310 goto Lnoarg; | |
311 global.params.objdir = arg[(p - arg.ptr) + 3..$]; | |
312 break; | |
313 | |
314 case 'f': | |
315 { | |
316 if (!p[3]) | |
317 goto Lnoarg; | |
318 | |
319 global.params.objname = arg[(p - arg.ptr) + 3..$]; | |
320 break; | |
321 } | |
322 | |
323 case 'p': | |
324 if (p[3]) | |
325 goto Lerror; | |
326 global.params.preservePaths = 1; | |
327 break; | |
328 | |
329 case 0: | |
330 error("-o no longer supported, use -of or -od"); | |
331 break; | |
332 | |
333 default: | |
334 goto Lerror; | |
335 } | |
336 } | |
337 else if (p[1] == 'D') | |
338 { global.params.doDocComments = 1; | |
339 switch (p[2]) | |
340 { | |
341 case 'd': | |
342 if (!p[3]) | |
343 goto Lnoarg; | |
344 global.params.docdir = arg[(p - arg.ptr) + 3..$]; | |
345 break; | |
346 case 'f': | |
347 if (!p[3]) | |
348 goto Lnoarg; | |
349 global.params.docname = arg[(p - arg.ptr) + 3..$]; | |
350 break; | |
351 | |
352 case 0: | |
353 break; | |
354 | |
355 default: | |
356 goto Lerror; | |
357 } | |
358 } | |
359 ///version (_DH) { | |
360 else if (p[1] == 'H') | |
361 { global.params.doHdrGeneration = 1; | |
362 switch (p[2]) | |
363 { | |
364 case 'd': | |
365 if (!p[3]) | |
366 goto Lnoarg; | |
367 global.params.hdrdir = arg[(p - arg.ptr) + 3..$]; | |
368 break; | |
369 | |
370 case 'f': | |
371 if (!p[3]) | |
372 goto Lnoarg; | |
373 global.params.hdrname = arg[(p - arg.ptr) + 3..$]; | |
374 break; | |
375 | |
376 case 0: | |
377 break; | |
378 | |
379 default: | |
380 goto Lerror; | |
381 } | |
382 } | |
383 ///} | |
384 else if (arg == "ignore") | |
385 global.params.ignoreUnsupportedPragmas = 1; | |
386 else if (arg == "inline") | |
387 global.params.useInline = 1; | |
388 else if (arg == "lib") | |
389 global.params.lib = 1; | |
390 else if (arg == "nofloat") | |
391 global.params.nofloat = 1; | |
392 else if (arg == "quiet") | |
393 global.params.quiet = 1; | |
394 else if (arg == "release") | |
395 global.params.release = 1; | |
396 ///version (DMDV2) { | |
397 else if (arg == "safe") | |
398 global.params.safe = 1; | |
399 ///} | |
400 else if (arg == "unittest") | |
401 global.params.useUnitTests = 1; | |
402 else if (p[1] == 'I') | |
403 { | |
404 if (!global.params.imppath) | |
405 global.params.imppath = new Array(); | |
406 global.params.imppath.push(cast(void*)new String(arg[(p - arg.ptr) + 2..$])); /// | |
407 } | |
408 else if (p[1] == 'J') | |
409 { | |
410 if (!global.params.fileImppath) | |
411 global.params.fileImppath = new Array(); | |
412 global.params.fileImppath.push(cast(void*)new String(arg[(p - arg.ptr) + 2..$])); | |
413 } | |
414 else if (memcmp(p + 1, "debug".ptr, 5) == 0 && p[6] != 'l') | |
415 { | |
416 // Parse: | |
417 // -debug | |
418 // -debug=number | |
419 // -debug=identifier | |
420 if (p[6] == '=') | |
421 { | |
422 if (isdigit(p[7])) | |
423 { long level; | |
424 | |
425 errno = 0; | |
426 level = strtol(p + 7, cast(char**)&p, 10); | |
427 if (*p || errno || level > INT_MAX) | |
428 goto Lerror; | |
429 DebugCondition.setGlobalLevel(cast(int)level); | |
430 } | |
431 else if (Lexer.isValidIdentifier(arg[(p - arg.ptr) + 7..$])) /// | |
432 DebugCondition.addGlobalIdent(p + 7); | |
433 else | |
434 goto Lerror; | |
435 } | |
436 else if (p[6]) | |
437 goto Lerror; | |
438 else | |
439 global.params.debuglevel = 1; | |
440 } | |
441 else if (memcmp(p + 1, "version".ptr, 5) == 0) | |
442 { | |
443 // Parse: | |
444 // -version=number | |
445 // -version=identifier | |
446 if (p[8] == '=') | |
447 { | |
448 if (isdigit(p[9])) | |
449 { long level; | |
450 | |
451 errno = 0; | |
452 level = strtol(p + 9, cast(char**)&p, 10); /// | |
453 if (*p || errno || level > INT_MAX) | |
454 goto Lerror; | |
455 VersionCondition.setGlobalLevel(cast(int)level); | |
456 } | |
457 else if (Lexer.isValidIdentifier(arg[(p - arg.ptr) + 9..$])) /// | |
458 VersionCondition.addGlobalIdent(arg[(p - arg.ptr) + 9..$]); | |
459 else | |
460 goto Lerror; | |
461 } | |
462 else | |
463 goto Lerror; | |
464 } | |
465 else if (arg == "-b") | |
466 global.params.debugb = 1; | |
467 else if (arg == "-c") | |
468 global.params.debugc = 1; | |
469 else if (arg == "-f") | |
470 global.params.debugf = 1; | |
471 else if (arg == "-help") | |
472 { usage(); | |
473 exit(EXIT_SUCCESS); | |
474 } | |
475 else if (arg == "-r") | |
476 global.params.debugr = 1; | |
477 else if (arg == "-x") | |
478 global.params.debugx = 1; | |
479 else if (arg == "-y") | |
480 global.params.debugy = 1; | |
481 else if (p[1] == 'L') | |
482 { | |
483 global.params.linkswitches.push(cast(void*)p + 2); | |
484 } | |
485 else if (memcmp(p + 1, "defaultlib=".ptr, 11) == 0) | |
486 { | |
487 global.params.defaultlibname = p + 1 + 11; | |
488 } | |
489 else if (memcmp(p + 1, "debuglib=".ptr, 9) == 0) | |
490 { | |
491 setdebuglib = 1; | |
492 global.params.debuglibname = p + 1 + 9; | |
493 } | |
494 else if (memcmp(p + 1, "deps=".ptr, 5) == 0) | |
495 { | |
496 global.params.moduleDepsFile = arg[(p - arg.ptr) + 1 + 5..$]; | |
497 if (!global.params.moduleDepsFile[0]) | |
498 goto Lnoarg; | |
499 global.params.moduleDeps = new OutBuffer; | |
500 } | |
501 else if (memcmp(p + 1, "man".ptr, 3) == 0) | |
502 { | |
503 version (_WIN32) { | |
504 version (DMDV1) { | |
505 browse("http://www.digitalmars.com/d/1.0/dmd-windows.html"); | |
506 } else { | |
507 browse("http://www.digitalmars.com/d/2.0/dmd-windows.html"); | |
508 } | |
509 } | |
510 version (linux) { | |
511 version (DMDV1) { | |
512 browse("http://www.digitalmars.com/d/1.0/dmd-linux.html"); | |
513 } else { | |
514 browse("http://www.digitalmars.com/d/2.0/dmd-linux.html"); | |
515 } | |
516 } | |
517 version (__APPLE__) { | |
518 version (DMDV1) { | |
519 browse("http://www.digitalmars.com/d/1.0/dmd-osx.html"); | |
520 } else { | |
521 browse("http://www.digitalmars.com/d/2.0/dmd-osx.html"); | |
522 } | |
523 } | |
524 version (__FreeBSD__) { | |
525 version (DMDV1) { | |
526 browse("http://www.digitalmars.com/d/1.0/dmd-freebsd.html"); | |
527 } else { | |
528 browse("http://www.digitalmars.com/d/2.0/dmd-freebsd.html"); | |
529 } | |
530 } | |
531 exit(EXIT_SUCCESS); | |
532 } | |
533 else if (arg == "run") | |
534 { global.params.run = 1; | |
535 global.params.runargs_length = ((i >= argcstart) ? args.length : argcstart) - i - 1; | |
536 if (global.params.runargs_length) | |
537 { | |
538 files.push(cast(void*)args[i + 1].ptr); | |
539 global.params.runargs = args[i + 2..$]; | |
540 i += global.params.runargs_length; | |
541 global.params.runargs_length--; | |
542 } | |
543 else | |
544 { global.params.run = 0; | |
545 goto Lnoarg; | |
546 } | |
547 } | |
548 else | |
549 { | |
550 Lerror: | |
551 error("unrecognized switch '%s'", args[i]); | |
552 continue; | |
553 | |
554 Lnoarg: | |
555 error("argument expected for switch '%s'", args[i]); | |
556 continue; | |
557 } | |
558 } | |
559 else | |
560 { | |
561 version (TARGET_WINDOS) { | |
562 string ext = FileName.ext(p[0..arg.length]); | |
563 if (ext != null && FileName.compare(ext, "exe") == 0) | |
564 { | |
565 global.params.objname = arg[(p - arg.ptr)..$]; | |
566 continue; | |
567 } | |
568 } | |
569 files.push(cast(void*)new String(arg[(p - arg.ptr)..$])); | |
570 } | |
571 } | |
572 if (global.errors) | |
573 { | |
574 fatal(); | |
575 } | |
576 if (files.dim == 0) | |
577 { usage(); | |
578 return EXIT_FAILURE; | |
579 } | |
580 | |
581 if (!setdebuglib) | |
582 global.params.debuglibname = global.params.defaultlibname; | |
583 | |
584 version (TARGET_OSX) { | |
585 global.params.pic = 1; | |
586 } | |
587 | |
588 if (global.params.release) | |
589 { global.params.useInvariants = 0; | |
590 global.params.useIn = 0; | |
591 global.params.useOut = 0; | |
592 global.params.useAssert = 0; | |
593 global.params.useArrayBounds = 0; | |
594 global.params.useSwitchError = 0; | |
595 } | |
596 | |
597 if (global.params.run) | |
598 global.params.quiet = 1; | |
599 | |
600 if (global.params.useUnitTests) | |
601 global.params.useAssert = 1; | |
602 | |
603 if (!global.params.obj || global.params.lib) | |
604 global.params.link = 0; | |
605 | |
606 if (global.params.link) | |
607 { | |
608 global.params.exefile = global.params.objname; | |
609 global.params.oneobj = 1; | |
610 if (global.params.objname) | |
611 { | |
612 /* Use this to name the one object file with the same | |
613 * name as the exe file. | |
614 */ | |
615 global.params.objname = FileName.forceExt(global.params.objname, global.obj_ext).toChars(); | |
616 | |
617 /* If output directory is given, use that path rather than | |
618 * the exe file path. | |
619 */ | |
620 if (global.params.objdir) | |
621 { | |
622 string name = FileName.name(global.params.objname); | |
623 global.params.objname = FileName.combine(global.params.objdir, name); | |
624 } | |
625 } | |
626 } | |
627 else if (global.params.lib) | |
628 { | |
629 global.params.libname = global.params.objname; | |
630 global.params.objname = null; | |
631 | |
632 // Haven't investigated handling these options with multiobj | |
633 if (!global.params.cov && !global.params.trace) | |
634 global.params.multiobj = 1; | |
635 } | |
636 else if (global.params.run) | |
637 { | |
638 error("flags conflict with -run"); | |
639 fatal(); | |
640 } | |
641 else | |
642 { | |
643 if (global.params.objname && files.dim > 1) | |
644 { | |
645 global.params.oneobj = 1; | |
646 //error("multiple source files, but only one .obj name"); | |
647 //fatal(); | |
648 } | |
649 } | |
650 if (global.params.isX86_64) | |
651 { | |
652 VersionCondition.addPredefinedGlobalIdent("D_InlineAsm_X86_64"); | |
653 VersionCondition.addPredefinedGlobalIdent("X86_64"); | |
654 VersionCondition.addPredefinedGlobalIdent("D_LP64"); | |
655 version (TARGET_WINDOS) { | |
656 VersionCondition.addPredefinedGlobalIdent("Win64"); | |
657 } | |
658 } | |
659 else | |
660 { | |
661 VersionCondition.addPredefinedGlobalIdent("D_InlineAsm"); | |
662 VersionCondition.addPredefinedGlobalIdent("D_InlineAsm_X86"); | |
663 VersionCondition.addPredefinedGlobalIdent("X86"); | |
664 version (TARGET_WINDOS) { | |
665 VersionCondition.addPredefinedGlobalIdent("Win32"); | |
666 } | |
667 } | |
668 if (global.params.doDocComments) | |
669 VersionCondition.addPredefinedGlobalIdent("D_Ddoc"); | |
670 if (global.params.cov) | |
671 VersionCondition.addPredefinedGlobalIdent("D_Coverage"); | |
672 if (global.params.pic) | |
673 VersionCondition.addPredefinedGlobalIdent("D_PIC"); | |
674 version (DMDV2) { | |
675 if (global.params.useUnitTests) | |
676 VersionCondition.addPredefinedGlobalIdent("unittest"); | |
677 } | |
678 | |
679 // Initialization | |
680 Type.init(); | |
681 Id.initialize(); | |
682 Module.init(); | |
683 initPrecedence(); | |
684 | |
685 backend_init(); | |
686 | |
687 //printf("%d source files\n",files.dim); | |
688 | |
689 // Build import search path | |
690 if (global.params.imppath) | |
691 { | |
692 for (int i = 0; i < global.params.imppath.dim; i++) | |
693 { | |
694 string path = (cast(String)global.params.imppath.data[i]).str; | |
695 string[] a = FileName.splitPath(path); | |
696 | |
697 global.path ~= a; | |
698 } | |
699 } | |
700 | |
701 // Build string import search path | |
702 if (global.params.fileImppath) | |
703 { | |
704 for (int i = 0; i < global.params.fileImppath.dim; i++) | |
705 { | |
706 string path = (cast(String)global.params.fileImppath.data[i]).str; | |
707 string[] a = FileName.splitPath(path); | |
708 | |
709 global.filePath ~= a; | |
710 } | |
711 } | |
712 | |
713 // Create Modules | |
714 Array modules = new Array(); | |
715 modules.reserve(files.dim); | |
716 int firstmodule = 1; | |
717 for (int i = 0; i < files.dim; i++) | |
718 { | |
719 string ext; | |
720 string name; | |
721 | |
722 String s = cast(String) files.data[i]; | |
723 string mp = s.str; | |
724 | |
725 version (_WIN32) { | |
726 char[] copy = null; | |
727 // Convert / to \ so linker will work | |
728 foreach (j, c; mp) | |
729 { | |
730 if (c == '/') { | |
731 if (copy is null) copy = mp.dup; | |
732 copy[j] = '\\'; | |
733 } | |
734 } | |
735 | |
736 if (copy !is null) mp = assumeUnique(copy); | |
737 } | |
738 string p = mp; | |
739 | |
740 p = FileName.name(p); // strip path | |
741 ext = FileName.ext(p); | |
742 | |
743 if (ext.length != 0) | |
744 { /* Deduce what to do with a file based on its extension | |
745 */ | |
746 if (FileName.equals(ext, global.obj_ext)) | |
747 { | |
748 global.params.objfiles.push(files.data[i]); | |
749 libmodules.push(files.data[i]); | |
750 continue; | |
751 } | |
752 | |
753 if (FileName.equals(ext, global.lib_ext)) | |
754 { | |
755 global.params.libfiles.push(files.data[i]); | |
756 libmodules.push(files.data[i]); | |
757 continue; | |
758 } | |
759 | |
760 if (ext == global.ddoc_ext) | |
761 { | |
762 global.params.ddocfiles.push(files.data[i]); | |
763 continue; | |
764 } | |
765 | |
766 version (TARGET_WINDOS) { | |
767 if (FileName.equals(ext, "res")) | |
768 { | |
769 global.params.resfile = (cast(immutable(char)*)files.data[i])[0..0]; /// !!! | |
770 continue; | |
771 } | |
772 | |
773 if (FileName.equals(ext, "def")) | |
774 { | |
775 global.params.deffile = (cast(immutable(char)*)files.data[i])[0..0]; | |
776 continue; | |
777 } | |
778 | |
779 if (FileName.equals(ext, "exe")) | |
780 { | |
781 assert(0); // should have already been handled | |
782 } | |
783 } | |
784 | |
785 /* Examine extension to see if it is a valid | |
786 * D source file extension | |
787 */ | |
788 if (FileName.equals(ext, global.mars_ext) || | |
789 FileName.equals(ext, global.hdr_ext) || | |
790 FileName.equals(ext, "dd") || | |
791 FileName.equals(ext, "htm") || | |
792 FileName.equals(ext, "html") || | |
793 FileName.equals(ext, "xhtml")) | |
794 { | |
795 immutable(char)* e = ext.ptr; | |
796 e--; // skip onto '.' | |
797 assert(*e == '.'); | |
798 | |
799 immutable(char)* n = p.ptr; | |
800 | |
801 name = n[0..(e-n)]; // strip extension | |
802 | |
803 if (name.length == 0 || name == ".." || name == ".") | |
804 { | |
805 Linvalid: | |
806 error("invalid file name '%s'", (cast(String)files.data[i]).str); | |
807 fatal(); | |
808 } | |
809 } | |
810 else | |
811 { error("unrecognized file extension %s\n", ext); | |
812 fatal(); | |
813 } | |
814 } | |
815 else | |
816 { | |
817 name = p; | |
818 if (!*name) | |
819 goto Linvalid; | |
820 } | |
821 | |
822 /* At this point, name is the D source file name stripped of | |
823 * its path and extension. | |
824 */ | |
825 | |
826 Identifier id = new Identifier(name, TOK.TOKreserved); | |
827 m = new Module((cast(String) files.data[i]).str, id, global.params.doDocComments, global.params.doHdrGeneration); | |
828 modules.push(cast(void*)m); | |
829 | |
830 if (firstmodule) | |
831 { | |
832 global.params.objfiles.push(cast(void*)m.objfile.name); | |
833 firstmodule = 0; | |
834 } | |
835 } | |
836 | |
837 // Read files | |
838 //version = ASYNCREAD; | |
839 version (ASYNCREAD) { | |
840 // Multi threaded | |
841 AsyncRead *aw = AsyncRead.create(modules.dim); | |
842 for (i = 0; i < modules.dim; i++) | |
843 { | |
844 m = cast(Module *)modules.data[i]; | |
845 aw.addFile(m.srcfile); | |
846 } | |
847 aw.start(); | |
848 } else { | |
849 // Single threaded | |
850 for (int i = 0; i < modules.dim; i++) | |
851 { | |
852 m = cast(Module)modules.data[i]; | |
853 m.read(Loc(0)); | |
854 } | |
855 } | |
856 | |
857 // Parse files | |
858 int anydocfiles = 0; | |
859 for (int i = 0; i < modules.dim; i++) | |
860 { | |
861 m = cast(Module)modules.data[i]; | |
862 if (global.params.verbose) | |
863 writef("parse %s\n", m.toChars()); | |
864 if (!Module.rootModule) | |
865 Module.rootModule = m; | |
866 m.importedFrom = m; | |
867 if (!global.params.oneobj || i == 0 || m.isDocFile) | |
868 m.deleteObjFile(); | |
869 version (ASYNCREAD) { | |
870 if (aw.read(i)) | |
871 { | |
872 error("cannot read file %s", m.srcfile.name.toChars()); | |
873 } | |
874 } | |
875 | |
876 //while (loop) {} | |
877 m.parse(); | |
878 if (m.isDocFile) | |
879 { | |
880 anydocfiles = 1; | |
881 m.gendocfile(); | |
882 | |
883 // Remove m from list of modules | |
884 modules.remove(i); | |
885 i--; | |
886 | |
887 // Remove m's object file from list of object files | |
888 for (int j = 0; j < global.params.objfiles.dim; j++) | |
889 { | |
890 if (m.objfile.name.str == (cast(FileName)global.params.objfiles.data[j]).str) | |
891 { | |
892 global.params.objfiles.remove(j); | |
893 break; | |
894 } | |
895 } | |
896 | |
897 if (global.params.objfiles.dim == 0) | |
898 global.params.link = 0; | |
899 } | |
900 } | |
901 version (ASYNCREAD) { | |
902 AsyncRead.dispose(aw); | |
903 } | |
904 | |
905 if (anydocfiles && modules.dim && | |
906 (global.params.oneobj || global.params.objname)) | |
907 { | |
908 error("conflicting Ddoc and obj generation options"); | |
909 fatal(); | |
910 } | |
911 if (global.errors) | |
912 fatal(); | |
913 version (_DH) { | |
914 if (global.params.doHdrGeneration) | |
915 { | |
916 /* Generate 'header' import files. | |
917 * Since 'header' import files must be independent of command | |
918 * line switches and what else is imported, they are generated | |
919 * before any semantic analysis. | |
920 */ | |
921 for (i = 0; i < modules.dim; i++) | |
922 { | |
923 m = cast(Module *)modules.data[i]; | |
924 if (global.params.verbose) | |
925 writef("import %s\n", m.toChars()); | |
926 m.genhdrfile(); | |
927 } | |
928 } | |
929 if (global.errors) | |
930 fatal(); | |
931 } | |
932 | |
933 // Do semantic analysis | |
934 for (int i = 0; i < modules.dim; i++) | |
935 { | |
936 m = cast(Module)modules.data[i]; | |
937 if (global.params.verbose) | |
938 writef("semantic %s\n", m.toChars()); | |
939 m.semantic(); | |
940 } | |
941 if (global.errors) | |
942 fatal(); | |
943 | |
944 // Do pass 2 semantic analysis | |
945 for (int i = 0; i < modules.dim; i++) | |
946 { | |
947 m = cast(Module)modules.data[i]; | |
948 if (global.params.verbose) | |
949 writef("semantic2 %s\n", m.toChars()); | |
950 m.semantic2(); | |
951 } | |
952 if (global.errors) | |
953 fatal(); | |
954 | |
955 // Do pass 3 semantic analysis | |
956 for (int i = 0; i < modules.dim; i++) | |
957 { | |
958 m = cast(Module)modules.data[i]; | |
959 if (global.params.verbose) | |
960 writef("semantic3 %s\n", m.toChars()); | |
961 m.semantic3(); | |
962 } | |
963 if (global.errors) | |
964 fatal(); | |
965 | |
966 if (global.params.moduleDeps !is null) | |
967 { | |
968 assert(global.params.moduleDepsFile !is null); | |
969 | |
970 File deps = new File(global.params.moduleDepsFile); | |
971 OutBuffer ob = global.params.moduleDeps; | |
972 deps.setbuffer(cast(void*)ob.data, ob.offset); | |
973 deps.writev(); | |
974 } | |
975 | |
976 | |
977 // Scan for functions to inline | |
978 if (global.params.useInline) | |
979 { | |
980 /* The problem with useArrayBounds and useAssert is that the | |
981 * module being linked to may not have generated them, so if | |
982 * we inline functions from those modules, the symbols for them will | |
983 * not be found at link time. | |
984 */ | |
985 if (!global.params.useArrayBounds && !global.params.useAssert) | |
986 { | |
987 // Do pass 3 semantic analysis on all imported modules, | |
988 // since otherwise functions in them cannot be inlined | |
989 for (int i = 0; i < Module.amodules.dim; i++) | |
990 { | |
991 m = cast(Module)Module.amodules.data[i]; | |
992 if (global.params.verbose) | |
993 writef("semantic3 %s\n", m.toChars()); | |
994 m.semantic3(); | |
995 } | |
996 if (global.errors) | |
997 fatal(); | |
998 } | |
999 | |
1000 for (int i = 0; i < modules.dim; i++) | |
1001 { | |
1002 m = cast(Module)modules.data[i]; | |
1003 if (global.params.verbose) | |
1004 writef("inline scan %s\n", m.toChars()); | |
1005 | |
1006 m.inlineScan(); | |
1007 } | |
1008 } | |
1009 if (global.errors) | |
1010 fatal(); | |
1011 | |
1012 Library library = null; | |
1013 if (global.params.lib) | |
1014 { | |
1015 library = new Library(); | |
1016 library.setFilename(global.params.objdir, global.params.libname); | |
1017 | |
1018 // Add input object and input library files to output library | |
1019 for (int i = 0; i < libmodules.dim; i++) | |
1020 { | |
1021 string p = (cast(String)libmodules.data[i]).str; | |
1022 library.addObject(p, null, 0); | |
1023 } | |
1024 } | |
1025 | |
1026 // Generate output files | |
1027 if (global.params.oneobj) | |
1028 { | |
1029 for (int i = 0; i < modules.dim; i++) | |
1030 { | |
1031 m = cast(Module)modules.data[i]; | |
1032 if (global.params.verbose) | |
1033 writef("code %s\n", m.toChars()); | |
1034 if (i == 0) | |
1035 obj_start(cast(char*)toStringz(m.srcfile.toChars())); | |
1036 m.genobjfile(0); | |
1037 if (!global.errors && global.params.doDocComments) | |
1038 m.gendocfile(); | |
1039 } | |
1040 if (!global.errors && modules.dim) | |
1041 { | |
1042 obj_end(library, (cast(Module)modules.data[0]).objfile); | |
1043 } | |
1044 } | |
1045 else | |
1046 { | |
1047 for (int i = 0; i < modules.dim; i++) | |
1048 { | |
1049 m = cast(Module)modules.data[i]; | |
1050 if (global.params.verbose) | |
1051 writef("code %s\n", m.toChars()); | |
1052 if (global.params.obj) | |
1053 { | |
1054 obj_start(cast(char*)toStringz(m.srcfile.toChars())); | |
1055 m.genobjfile(global.params.multiobj); | |
1056 obj_end(library, m.objfile); | |
1057 obj_write_deferred(library); | |
1058 } | |
1059 if (global.errors) | |
1060 { | |
1061 if (!global.params.lib) | |
1062 m.deleteObjFile(); | |
1063 } | |
1064 else | |
1065 { | |
1066 if (global.params.doDocComments) | |
1067 m.gendocfile(); | |
1068 } | |
1069 } | |
1070 } | |
1071 | |
1072 if (global.params.lib && !global.errors) | |
1073 library.write(); | |
1074 | |
1075 backend_term(); | |
1076 if (global.errors) | |
1077 fatal(); | |
1078 | |
1079 if (!global.params.objfiles.dim) | |
1080 { | |
1081 if (global.params.link) | |
1082 error("no object files to link"); | |
1083 } | |
1084 else | |
1085 { | |
1086 if (global.params.link) | |
1087 status = runLINK(); | |
1088 | |
1089 if (global.params.run) | |
1090 { | |
1091 if (!status) | |
1092 { | |
1093 status = runProgram(); | |
1094 | |
1095 /* Delete .obj files and .exe file | |
1096 */ | |
1097 for (int i = 0; i < modules.dim; i++) | |
1098 { | |
1099 m = cast(Module)modules.data[i]; | |
1100 m.deleteObjFile(); | |
1101 if (global.params.oneobj) | |
1102 break; | |
1103 } | |
1104 deleteExeFile(); | |
1105 } | |
1106 } | |
1107 } | |
1108 | |
1109 return status; | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1110 } |