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