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