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