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