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