Mercurial > projects > ddmd
annotate dmd/Util.d @ 14:2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Sun, 04 Apr 2010 02:06:32 +0100 |
parents | 7427ded8caf7 |
children | 5c9b78899f5d |
rev | line source |
---|---|
0 | 1 module dmd.Util; |
2 | |
3 import dmd.Loc; | |
4 import dmd.Library; | |
5 import dmd.File; | |
6 import dmd.String; | |
7 import dmd.OutBuffer; | |
8 import dmd.FileName; | |
9 import dmd.Global; | |
10 import dmd.PREC; | |
11 import dmd.TOK; | |
12 | |
13 import std.process : getenv; | |
14 import std.c.string; | |
15 import std.stdio : writef, writefln, write; | |
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:
2
diff
changeset
|
16 version (Windows) |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
2
diff
changeset
|
17 { |
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:
2
diff
changeset
|
18 import std.c.process : spawnl, spawnlp; |
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:
2
diff
changeset
|
19 } |
0 | 20 import core.stdc.stdlib; |
21 import core.stdc.ctype; | |
22 import core.stdc.stdarg; | |
23 import core.stdc.stdio; | |
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:
2
diff
changeset
|
24 version (Bug4054) import core.memory; |
0 | 25 |
26 extern(C) int putenv(char*); | |
27 | |
28 //version = LOG; | |
29 | |
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:
2
diff
changeset
|
30 version (POSIX) |
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:
2
diff
changeset
|
31 { |
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:
2
diff
changeset
|
32 import dmd.Gnuc; |
0 | 33 } |
34 | |
35 enum MAX_PATH = 256; /// | |
36 | |
37 version (Windows) { | |
38 import core.sys.windows.windows : GetModuleFileNameA; | |
39 } | |
40 | |
41 string fromStringz(const(char)* s) | |
42 { | |
43 return s[0..strlen(s)].idup; | |
44 } | |
45 | |
46 void warning(T...)(string format, T t) | |
47 { | |
48 assert(false); | |
49 } | |
50 | |
51 void warning(T...)(Loc loc, string format, T t) | |
52 { | |
53 assert(false); | |
54 } | |
55 | |
56 void error(T...)(Loc loc, string format, T t) | |
57 { | |
58 if (!global.gag) | |
59 { | |
60 string p = loc.toChars(); | |
61 | |
62 if (p.length != 0) | |
63 writef("%s: ", p); | |
64 | |
65 write("Error: "); | |
66 writefln(format, t); | |
67 | |
68 //halt(); | |
69 } | |
70 global.errors++; | |
71 } | |
72 | |
73 char* strupr(char* s) | |
74 { | |
75 char* t = s; | |
76 | |
77 while (*s) | |
78 { | |
79 *s = cast(char)toupper(*s); | |
80 s++; | |
81 } | |
82 | |
83 return t; | |
84 } | |
85 | |
86 char[] skipspace(char[] p) | |
87 { | |
88 foreach (i, c; p) { | |
89 if (!isspace(c)) { | |
90 return p[i..$]; | |
91 } | |
92 } | |
93 | |
94 return null; | |
95 } | |
96 | |
97 char* skipspace(char* p) | |
98 { | |
99 while (isspace(*p)) | |
100 p++; | |
101 | |
102 return p; | |
103 } | |
104 | |
105 void inifile(string argv0, string inifile) | |
106 { | |
107 char *path; // need path for @P macro | |
108 string filename; | |
109 int i; | |
110 int k; | |
111 int envsection = 0; | |
112 | |
113 version (LOG) { | |
114 writef("inifile(argv0 = '%s', inifile = '%s')\n", argv0, inifile); | |
115 } | |
116 if (FileName.absolute(inifile)) { | |
117 filename = inifile; | |
118 } else { | |
119 /* Look for inifile in the following sequence of places: | |
120 * o current directory | |
121 * o home directory | |
122 * o directory off of argv0 | |
123 * o /etc/ | |
124 */ | |
125 if (FileName.exists(inifile)) { | |
126 filename = inifile; | |
127 } else { | |
128 filename = FileName.combine(getenv("HOME"), inifile); | |
129 if (!FileName.exists(filename)) { | |
130 version (_WIN32) { // This fix by Tim Matthews | |
131 char resolved_name_b[MAX_PATH + 1]; | |
132 auto resolved_name = resolved_name_b[].idup; | |
133 if (GetModuleFileNameA(null, resolved_name_b.ptr, MAX_PATH + 1) && FileName.exists(resolved_name)) | |
134 { | |
135 filename = FileName.replaceName(resolved_name, inifile); | |
136 if (FileName.exists(filename)) { | |
137 goto Ldone; | |
138 } | |
139 } | |
140 } | |
141 filename = FileName.replaceName(argv0, inifile); | |
142 if (!FileName.exists(filename)) { | |
143 version (XXX) { /// linux || __APPLE__ || __FreeBSD__ || __sun&&__SVR4 | |
144 version (XXX) { /// __GLIBC__ || __APPLE__ || __FreeBSD__ || __sun&&__SVR4 // This fix by Thomas Kuehne | |
145 /* argv0 might be a symbolic link, | |
146 * so try again looking past it to the real path | |
147 */ | |
148 version (XXX) {/// #if __APPLE__ || __FreeBSD__ || __sun&&__SVR4 | |
149 char resolved_name[PATH_MAX + 1]; | |
150 char* real_argv0 = realpath(argv0, resolved_name); | |
151 } else { | |
152 char* real_argv0 = realpath(argv0, null); | |
153 } | |
154 //printf("argv0 = %s, real_argv0 = %p\n", argv0, real_argv0); | |
155 if (real_argv0) { | |
156 filename = FileName.replaceName(real_argv0, inifile); | |
157 version (linux) { | |
2 | 158 ///free(real_argv0); |
0 | 159 } |
160 if (FileName.exists(filename)) { | |
161 goto Ldone; | |
162 } | |
163 } | |
164 } else { | |
165 static assert (false, "use of glibc non-standard extension realpath(char*, null)"); | |
166 } | |
167 if (true) { | |
168 // Search PATH for argv0 | |
169 const(char)* p = getenv("PATH"); | |
170 version (LOG) { | |
171 writef("\tPATH='%s'\n", p); | |
172 } | |
173 Array paths = FileName.splitPath(p); | |
174 filename = FileName.searchPath(paths, argv0, 0); | |
175 if (!filename) { | |
176 goto Letc; // argv0 not found on path | |
177 } | |
178 | |
179 filename = FileName.replaceName(filename, inifile); | |
180 if (FileName.exists(filename)) { | |
181 goto Ldone; | |
182 } | |
183 } | |
184 } | |
185 // Search /etc/ for inifile | |
186 Letc: | |
187 filename = FileName.combine("/etc/", inifile); | |
188 | |
189 Ldone: | |
190 ; | |
191 } | |
192 } | |
193 } | |
194 } | |
195 | |
196 path = cast(char*)toStringz(FileName.path(filename)); | |
197 | |
198 version (LOG) { | |
199 writef("\tpath = '%s', filename = '%s'\n", fromStringz(path), filename); | |
200 } | |
201 | |
202 scope File file = new File(filename); | |
203 | |
204 if (file.read()) { | |
205 return; // error reading file | |
206 } | |
207 | |
208 scope OutBuffer buf = new OutBuffer(); | |
209 | |
210 // Parse into lines | |
211 int eof = 0; | |
212 for (i = 0; i < file.len && !eof; i++) | |
213 { | |
214 int linestart = i; | |
215 | |
216 for (; i < file.len; i++) | |
217 { | |
218 switch (file.buffer[i]) | |
219 { | |
220 case '\r': | |
221 break; | |
222 | |
223 case '\n': | |
224 // Skip if it was preceded by '\r' | |
225 if (i && file.buffer[i - 1] == '\r') | |
226 goto Lskip; | |
227 break; | |
228 | |
229 case 0: | |
230 case 0x1A: | |
231 eof = 1; | |
232 break; | |
233 | |
234 default: | |
235 continue; | |
236 } | |
237 break; | |
238 } | |
239 | |
240 // The line is file.buffer[linestart..i] | |
241 char *line; | |
242 int len; | |
243 char *p; | |
244 char* pn; | |
245 | |
246 line = cast(char*)&file.buffer[linestart]; | |
247 len = i - linestart; | |
248 | |
249 buf.reset(); | |
250 | |
251 // First, expand the macros. | |
252 // Macros are bracketed by % characters. | |
253 | |
254 for (k = 0; k < len; k++) | |
255 { | |
256 if (line[k] == '%') | |
257 { | |
258 int j; | |
259 | |
260 for (j = k + 1; j < len; j++) | |
261 { | |
262 if (line[j] == '%') | |
263 { | |
264 if (j - k == 3 && memicmp(&line[k + 1], "@P", 2) == 0) | |
265 { | |
266 // %@P% is special meaning the path to the .ini file | |
267 p = path; | |
268 if (!*p) | |
269 p = cast(char*)"."; | |
270 } | |
271 else | |
272 { | |
273 int l = j - k; | |
274 char tmp[10]; // big enough most of the time | |
275 | |
276 if (l <= tmp.sizeof) | |
277 p = tmp.ptr; | |
278 else | |
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:
2
diff
changeset
|
279 { |
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:
2
diff
changeset
|
280 version (Bug4054) |
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:
2
diff
changeset
|
281 p = cast(char*)GC.malloc(l); |
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:
2
diff
changeset
|
282 else |
0 | 283 p = cast(char*)alloca(l); |
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:
2
diff
changeset
|
284 } |
0 | 285 l--; |
286 memcpy(p, &line[k + 1], l); | |
287 p[l] = 0; | |
288 strupr(p); | |
289 p = core.stdc.stdlib.getenv(p); | |
290 if (!p) | |
291 p = cast(char*)""; | |
292 } | |
293 buf.writestring(p[0..strlen(p)]); /// | |
294 k = j; | |
295 goto L1; | |
296 } | |
297 } | |
298 } | |
299 buf.writeByte(line[k]); | |
300 L1: | |
301 ; | |
302 } | |
303 | |
304 // Remove trailing spaces | |
305 while (buf.offset && isspace(buf.data[buf.offset - 1])) | |
306 buf.offset--; | |
307 | |
308 char[] pp = buf.getString(); | |
309 | |
310 // The expanded line is in p. | |
311 // Now parse it for meaning. | |
312 | |
313 pp = skipspace(pp); | |
314 if (pp.length != 0) { | |
315 switch (pp[0]) | |
316 { | |
317 case ';': // comment | |
318 break; | |
319 | |
320 case '[': // look for [Environment] | |
321 pp = skipspace(pp[1..$]); | |
322 for (pn = pp.ptr; isalnum(*pn); pn++) { | |
323 ; | |
324 } | |
325 | |
326 if (pn - pp.ptr == 11 && | |
327 memicmp(pp.ptr, "Environment", 11) == 0 && | |
328 *skipspace(pn) == ']' | |
329 ) | |
330 envsection = 1; | |
331 else | |
332 envsection = 0; | |
333 break; | |
334 | |
335 default: | |
336 if (envsection) | |
337 { | |
338 pn = pp.ptr; | |
339 | |
340 // Convert name to upper case; | |
341 // remove spaces bracketing = | |
342 auto p2 = pn; | |
343 for ( ; *p2; p2++) | |
344 { if (islower(*p2)) | |
345 *p2 &= ~0x20; | |
346 else if (isspace(*p)) | |
347 memmove(p2, p2 + 1, strlen(p2)); | |
348 else if (*p2 == '=') | |
349 { | |
350 p2++; | |
351 while (isspace(*p2)) | |
352 memmove(p2, p2 + 1, strlen(p2)); | |
353 break; | |
354 } | |
355 } | |
356 | |
357 //putenv(pn); | |
358 putenv(cast(char*)toStringz(pp)); | |
359 | |
360 version (LOG) { | |
361 writef("\tputenv('%s')\n", pn[0..strlen(pn)]); | |
362 //printf("getenv(\"TEST\") = '%s'\n",getenv("TEST")); | |
363 } | |
364 } | |
365 break; | |
366 } | |
367 } | |
368 | |
369 Lskip: | |
370 ; | |
371 } | |
372 } | |
373 | |
374 ///int response_expand(int *pargc, char ***pargv); | |
375 void browse(const(char)* url) | |
376 { | |
377 assert(false); | |
378 } | |
379 | |
380 string[] getenv_setargv(string envvar, string[] args) | |
381 { | |
382 char *p; | |
383 | |
384 string[] argv = args.dup; | |
385 int argc = args.length; | |
386 | |
387 int wildcard; // do wildcard expansion | |
388 int instring; | |
389 int slash; | |
390 char c; | |
391 int j; | |
392 | |
393 string ienv = getenv(envvar); | |
394 if (ienv is null) | |
395 return args; | |
396 | |
397 char[] env = ienv.dup; // create our own writable copy | |
398 | |
399 j = 1; // leave argv[0] alone | |
400 char* e = env.ptr; | |
401 while (1) | |
402 { | |
403 wildcard = 1; | |
404 switch (*e) | |
405 { | |
406 case ' ': | |
407 case '\t': | |
408 e++; | |
409 break; | |
410 | |
411 case 0: | |
412 goto Ldone; | |
413 | |
414 case '"': | |
415 wildcard = 0; | |
416 default: | |
417 argv ~= assumeUnique(e[0..strlen(e)]); // append | |
418 //argv.insert(j, env); // insert at position j | |
419 j++; | |
420 argc++; | |
421 p = e; | |
422 slash = 0; | |
423 instring = 0; | |
424 c = 0; | |
425 | |
426 char* ecopy = e; | |
427 | |
428 while (1) | |
429 { | |
430 c = *e++; | |
431 switch (c) | |
432 { | |
433 case '"': | |
434 p -= (slash >> 1); | |
435 if (slash & 1) | |
436 { | |
437 p--; | |
438 goto Laddc; | |
439 } | |
440 instring ^= 1; | |
441 slash = 0; | |
442 continue; | |
443 | |
444 case '\\': | |
445 slash++; | |
446 *p++ = c; | |
447 continue; | |
448 | |
449 case ' ': | |
450 case '\t': | |
451 if (instring) | |
452 goto Laddc; | |
453 case 0: | |
454 *p = 0; | |
455 if (argv.length != 0) { | |
456 argv[$-1].length = p - argv[$-1].ptr; | |
457 } | |
458 //if (wildcard) | |
459 //wildcardexpand(); // not implemented | |
460 if (c == 0) goto Ldone; | |
461 break; | |
462 | |
463 default: | |
464 Laddc: | |
465 slash = 0; | |
466 *p++ = c; | |
467 continue; | |
468 } | |
469 break; | |
470 } | |
471 } | |
472 } | |
473 | |
474 Ldone: | |
475 return argv; | |
476 } | |
477 | |
478 void error(T...)(string format, T t) | |
479 { | |
480 writefln(format, t); | |
481 exit(EXIT_FAILURE); | |
482 } | |
483 | |
484 void usage() | |
485 { | |
486 writef("Digital Mars D Compiler %s\n%s %s\n", global.version_, global.copyright, global.written); | |
487 writef( | |
488 "Documentation: http://www.digitalmars.com/d/2.0/index.html\n" | |
489 "Usage:\n" | |
490 " dmd files.d ... { -switch }\n" | |
491 "\n" | |
492 " files.d D source files\n" | |
493 " @cmdfile read arguments from cmdfile\n" | |
494 " -c do not link\n" | |
495 " -cov do code coverage analysis\n" | |
496 " -D generate documentation\n" | |
497 " -Dddocdir write documentation file to docdir directory\n" | |
498 " -Dffilename write documentation file to filename\n" | |
499 " -d allow deprecated features\n" | |
500 " -debug compile in debug code\n" | |
501 " -debug=level compile in debug code <= level\n" | |
502 " -debug=ident compile in debug code identified by ident\n" | |
503 " -debuglib=name set symbolic debug library to name\n" | |
504 " -defaultlib=name set default library to name\n" | |
505 " -deps=filename write module dependencies to filename\n" | |
506 " -g add symbolic debug info\n" | |
507 " -gc add symbolic debug info, pretend to be C\n" | |
508 " -H generate 'header' file\n" | |
509 " -Hdhdrdir write 'header' file to hdrdir directory\n" | |
510 " -Hffilename write 'header' file to filename\n" | |
511 " --help print help\n" | |
512 " -Ipath where to look for imports\n" | |
513 " -ignore ignore unsupported pragmas\n" | |
514 " -inline do function inlining\n" | |
515 " -Jpath where to look for string imports\n" | |
516 " -Llinkerflag pass linkerflag to link\n" | |
517 " -lib generate library rather than object files\n" | |
518 " -man open web browser on manual page\n" | |
519 " -nofloat do not emit reference to floating point\n" | |
520 " -O optimize\n" | |
521 " -o- do not write object file\n" | |
522 " -odobjdir write object & library files to directory objdir\n" | |
523 " -offilename name output file to filename\n" | |
524 " -op do not strip paths from source file\n" | |
525 " -profile profile runtime performance of generated code\n" | |
526 " -quiet suppress unnecessary messages\n" | |
527 " -release compile release version\n" | |
528 " -run srcfile args... run resulting program, passing args\n" | |
529 " -safe safe memory model\n" | |
530 " -unittest compile in unit tests\n" | |
531 " -v verbose\n" | |
532 " -version=level compile in version code >= level\n" | |
533 " -version=ident compile in version code identified by ident\n" | |
534 " -vtls list all variables going into thread local storage\n" | |
535 " -w enable warnings\n" | |
536 ); | |
537 } | |
538 | |
539 void fatal() | |
540 { | |
541 static if (false) { | |
542 halt(); | |
543 } else { | |
544 exit(EXIT_FAILURE); | |
545 } | |
546 } | |
547 | |
548 void halt() | |
549 { | |
550 assert(false); | |
551 } | |
552 | |
553 void initPrecedence() | |
554 { | |
555 precedence[TOK.TOKdotvar] = PREC.PREC_primary; | |
556 precedence[TOK.TOKimport] = PREC.PREC_primary; | |
557 precedence[TOK.TOKidentifier] = PREC.PREC_primary; | |
558 precedence[TOK.TOKthis] = PREC.PREC_primary; | |
559 precedence[TOK.TOKsuper] = PREC.PREC_primary; | |
560 precedence[TOK.TOKint64] = PREC.PREC_primary; | |
561 precedence[TOK.TOKfloat64] = PREC.PREC_primary; | |
562 precedence[TOK.TOKnull] = PREC.PREC_primary; | |
563 precedence[TOK.TOKstring] = PREC.PREC_primary; | |
564 precedence[TOK.TOKarrayliteral] = PREC.PREC_primary; | |
565 precedence[TOK.TOKtypeid] = PREC.PREC_primary; | |
566 precedence[TOK.TOKis] = PREC.PREC_primary; | |
567 precedence[TOK.TOKassert] = PREC.PREC_primary; | |
568 precedence[TOK.TOKfunction] = PREC.PREC_primary; | |
569 precedence[TOK.TOKvar] = PREC.PREC_primary; | |
570 version (DMDV2) { | |
571 precedence[TOK.TOKdefault] = PREC.PREC_primary; | |
572 } | |
573 | |
574 // post | |
575 precedence[TOK.TOKdotti] = PREC.PREC_primary; | |
576 precedence[TOK.TOKdot] = PREC.PREC_primary; | |
577 // precedence[TOK.TOKarrow] = PREC.PREC_primary; | |
578 precedence[TOK.TOKplusplus] = PREC.PREC_primary; | |
579 precedence[TOK.TOKminusminus] = PREC.PREC_primary; | |
580 precedence[TOK.TOKcall] = PREC.PREC_primary; | |
581 precedence[TOK.TOKslice] = PREC.PREC_primary; | |
582 precedence[TOK.TOKarray] = PREC.PREC_primary; | |
583 | |
584 precedence[TOK.TOKaddress] = PREC.PREC_unary; | |
585 precedence[TOK.TOKstar] = PREC.PREC_unary; | |
586 precedence[TOK.TOKneg] = PREC.PREC_unary; | |
587 precedence[TOK.TOKuadd] = PREC.PREC_unary; | |
588 precedence[TOK.TOKnot] = PREC.PREC_unary; | |
589 precedence[TOK.TOKtobool] = PREC.PREC_add; | |
590 precedence[TOK.TOKtilde] = PREC.PREC_unary; | |
591 precedence[TOK.TOKdelete] = PREC.PREC_unary; | |
592 precedence[TOK.TOKnew] = PREC.PREC_unary; | |
593 precedence[TOK.TOKcast] = PREC.PREC_unary; | |
594 | |
595 precedence[TOK.TOKmul] = PREC.PREC_mul; | |
596 precedence[TOK.TOKdiv] = PREC.PREC_mul; | |
597 precedence[TOK.TOKmod] = PREC.PREC_mul; | |
598 | |
599 precedence[TOK.TOKadd] = PREC.PREC_add; | |
600 precedence[TOK.TOKmin] = PREC.PREC_add; | |
601 precedence[TOK.TOKcat] = PREC.PREC_add; | |
602 | |
603 precedence[TOK.TOKshl] = PREC.PREC_shift; | |
604 precedence[TOK.TOKshr] = PREC.PREC_shift; | |
605 precedence[TOK.TOKushr] = PREC.PREC_shift; | |
606 | |
607 precedence[TOK.TOKlt] = PREC.PREC_rel; | |
608 precedence[TOK.TOKle] = PREC.PREC_rel; | |
609 precedence[TOK.TOKgt] = PREC.PREC_rel; | |
610 precedence[TOK.TOKge] = PREC.PREC_rel; | |
611 precedence[TOK.TOKunord] = PREC.PREC_rel; | |
612 precedence[TOK.TOKlg] = PREC.PREC_rel; | |
613 precedence[TOK.TOKleg] = PREC.PREC_rel; | |
614 precedence[TOK.TOKule] = PREC.PREC_rel; | |
615 precedence[TOK.TOKul] = PREC.PREC_rel; | |
616 precedence[TOK.TOKuge] = PREC.PREC_rel; | |
617 precedence[TOK.TOKug] = PREC.PREC_rel; | |
618 precedence[TOK.TOKue] = PREC.PREC_rel; | |
619 precedence[TOK.TOKin] = PREC.PREC_rel; | |
620 | |
621 static if (false) { | |
622 precedence[TOK.TOKequal] = PREC.PREC_equal; | |
623 precedence[TOK.TOKnotequal] = PREC.PREC_equal; | |
624 precedence[TOK.TOKidentity] = PREC.PREC_equal; | |
625 precedence[TOK.TOKnotidentity] = PREC.PREC_equal; | |
626 } else { | |
627 /* Note that we changed precedence, so that < and != have the same | |
628 * precedence. This change is in the parser, too. | |
629 */ | |
630 precedence[TOK.TOKequal] = PREC.PREC_rel; | |
631 precedence[TOK.TOKnotequal] = PREC.PREC_rel; | |
632 precedence[TOK.TOKidentity] = PREC.PREC_rel; | |
633 precedence[TOK.TOKnotidentity] = PREC.PREC_rel; | |
634 } | |
635 | |
636 precedence[TOK.TOKand] = PREC.PREC_and; | |
637 | |
638 precedence[TOK.TOKxor] = PREC.PREC_xor; | |
639 | |
640 precedence[TOK.TOKor] = PREC.PREC_or; | |
641 | |
642 precedence[TOK.TOKandand] = PREC.PREC_andand; | |
643 | |
644 precedence[TOK.TOKoror] = PREC.PREC_oror; | |
645 | |
646 precedence[TOK.TOKquestion] = PREC.PREC_cond; | |
647 | |
648 precedence[TOK.TOKassign] = PREC.PREC_assign; | |
649 precedence[TOK.TOKconstruct] = PREC.PREC_assign; | |
650 precedence[TOK.TOKblit] = PREC.PREC_assign; | |
651 precedence[TOK.TOKaddass] = PREC.PREC_assign; | |
652 precedence[TOK.TOKminass] = PREC.PREC_assign; | |
653 precedence[TOK.TOKcatass] = PREC.PREC_assign; | |
654 precedence[TOK.TOKmulass] = PREC.PREC_assign; | |
655 precedence[TOK.TOKdivass] = PREC.PREC_assign; | |
656 precedence[TOK.TOKmodass] = PREC.PREC_assign; | |
657 precedence[TOK.TOKshlass] = PREC.PREC_assign; | |
658 precedence[TOK.TOKshrass] = PREC.PREC_assign; | |
659 precedence[TOK.TOKushrass] = PREC.PREC_assign; | |
660 precedence[TOK.TOKandass] = PREC.PREC_assign; | |
661 precedence[TOK.TOKorass] = PREC.PREC_assign; | |
662 precedence[TOK.TOKxorass] = PREC.PREC_assign; | |
663 | |
664 precedence[TOK.TOKcomma] = PREC.PREC_expr; | |
665 } | |
666 | |
667 int runLINK() | |
668 { | |
669 version (_WIN32) { | |
670 string p; | |
671 int i; | |
672 int status; | |
673 scope OutBuffer cmdbuf = new OutBuffer(); | |
674 | |
675 global.params.libfiles.push(cast(void*)new String("user32")); | |
676 global.params.libfiles.push(cast(void*)new String("kernel32")); | |
677 | |
678 for (i = 0; i < global.params.objfiles.dim; i++) | |
679 { | |
680 if (i) | |
681 cmdbuf.writeByte('+'); | |
682 p = (cast(String)global.params.objfiles.data[i]).str; | |
683 string ext = FileName.ext(p); | |
684 if (ext) | |
685 // Write name sans extension | |
686 writeFilename(cmdbuf, p[0..p.length - ext.length - 1]); | |
687 else | |
688 writeFilename(cmdbuf, p); | |
689 } | |
690 cmdbuf.writeByte(','); | |
691 if (global.params.exefile) | |
692 writeFilename(cmdbuf, global.params.exefile); | |
693 else | |
694 { | |
695 /* Generate exe file name from first obj name. | |
696 * No need to add it to cmdbuf because the linker will default to it. | |
697 */ | |
698 string n = (cast(String)global.params.objfiles.data[0]).str; | |
699 n = FileName.name(n); | |
700 FileName fn = FileName.forceExt(n, "exe"); | |
701 global.params.exefile = fn.toChars(); | |
702 } | |
703 | |
704 // Make sure path to exe file exists | |
705 { | |
706 string pp = FileName.path(global.params.exefile); | |
707 FileName.ensurePathExists(pp); | |
708 } | |
709 | |
710 cmdbuf.writeByte(','); | |
711 if (global.params.run) | |
712 cmdbuf.writestring("nul"); | |
713 | |
714 // if (mapfile) | |
715 // cmdbuf.writestring(output); | |
716 cmdbuf.writeByte(','); | |
717 | |
718 for (i = 0; i < global.params.libfiles.dim; i++) | |
719 { | |
720 if (i) | |
721 cmdbuf.writeByte('+'); | |
722 writeFilename(cmdbuf, (cast(String)global.params.libfiles.data[i]).str); | |
723 } | |
724 | |
725 if (global.params.deffile) | |
726 { | |
727 cmdbuf.writeByte(','); | |
728 writeFilename(cmdbuf, global.params.deffile); | |
729 } | |
730 | |
731 /* Eliminate unnecessary trailing commas */ | |
732 while (1) | |
733 { | |
734 i = cmdbuf.offset; | |
735 if (!i || cmdbuf.data[i - 1] != ',') | |
736 break; | |
737 cmdbuf.offset--; | |
738 } | |
739 | |
740 if (global.params.resfile) | |
741 { | |
742 cmdbuf.writestring("/RC:"); | |
743 writeFilename(cmdbuf, global.params.resfile); | |
744 } | |
745 | |
746 static if (false) { | |
747 if (mapfile) | |
748 cmdbuf.writestring("/m"); | |
749 if (debuginfo) | |
750 cmdbuf.writestring("/li"); | |
751 if (codeview) | |
752 { | |
753 cmdbuf.writestring("/co"); | |
754 if (codeview3) | |
755 cmdbuf.writestring(":3"); | |
756 } | |
757 } else { | |
758 if (global.params.symdebug) | |
759 cmdbuf.writestring("/co"); | |
760 } | |
761 | |
762 cmdbuf.writestring("/noi"); | |
763 for (i = 0; i < global.params.linkswitches.dim; i++) | |
764 { | |
765 cmdbuf.writestring((cast(String)global.params.linkswitches.data[i]).str); | |
766 } | |
767 cmdbuf.writeByte(';'); | |
768 | |
769 p = cmdbuf.toChars(); | |
770 | |
771 FileName lnkfilename = null; | |
772 size_t plen = p.length; | |
773 if (plen > 7000) | |
774 { | |
775 lnkfilename = FileName.forceExt(global.params.exefile, "lnk"); | |
776 scope File flnk = new File(lnkfilename); | |
777 flnk.setbuffer(cast(void*)p.ptr, plen); | |
778 flnk.ref_ = 1; | |
779 if (flnk.write()) | |
780 error("error writing file %s", lnkfilename); | |
781 if (lnkfilename.len() < plen) | |
782 p = std.string.format("@%s", lnkfilename.toChars()); | |
783 } | |
784 | |
785 string linkcmd = getenv("LINKCMD"); | |
786 if (!linkcmd) | |
787 linkcmd = "link"; | |
788 | |
789 status = executecmd(linkcmd, p, 1); | |
790 if (lnkfilename) | |
791 { | |
792 remove(toStringz(lnkfilename.toChars())); | |
793 ///delete lnkfilename; | |
794 } | |
795 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:
2
diff
changeset
|
796 } 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:
2
diff
changeset
|
797 assert(false, "Unimplemented"); |
0 | 798 /+ |
799 pid_t childpid; | |
800 int i; | |
801 int status; | |
802 | |
803 // Build argv[] | |
804 Array argv; | |
805 | |
806 const char *cc = getenv("CC"); | |
807 if (!cc) | |
808 cc = "gcc"; | |
809 argv.push((void *)cc); | |
810 argv.insert(1, global.params.objfiles); | |
811 | |
812 // None of that a.out stuff. Use explicit exe file name, or | |
813 // generate one from name of first source file. | |
814 argv.push((void *)"-o"); | |
815 if (global.params.exefile) | |
816 { | |
817 argv.push(global.params.exefile); | |
818 } | |
819 else | |
820 { // Generate exe file name from first obj name | |
821 char *n = (char *)global.params.objfiles.data[0]; | |
822 char *e; | |
823 char *ex; | |
824 | |
825 n = FileName.name(n); | |
826 e = FileName.ext(n); | |
827 if (e) | |
828 { | |
829 e--; // back up over '.' | |
830 ex = (char *)mem.malloc(e - n + 1); | |
831 memcpy(ex, n, e - n); | |
832 ex[e - n] = 0; | |
833 } | |
834 else | |
835 ex = (char *)"a.out"; // no extension, so give up | |
836 argv.push(ex); | |
837 global.params.exefile = ex; | |
838 } | |
839 | |
840 // Make sure path to exe file exists | |
841 { char *p = FileName.path(global.params.exefile); | |
842 FileName.ensurePathExists(p); | |
843 mem.free(p); | |
844 } | |
845 | |
846 if (global.params.symdebug) | |
847 argv.push((void *)"-g"); | |
848 | |
849 if (global.params.isX86_64) | |
850 argv.push((void *)"-m64"); | |
851 else | |
852 argv.push((void *)"-m32"); | |
853 | |
854 if (0 && global.params.exefile) | |
855 { | |
856 /* This switch enables what is known as 'smart linking' | |
857 * in the Windows world, where unreferenced sections | |
858 * are removed from the executable. It eliminates unreferenced | |
859 * functions, essentially making a 'library' out of a module. | |
860 * Although it is documented to work with ld version 2.13, | |
861 * in practice it does not, but just seems to be ignored. | |
862 * Thomas Kuehne has verified that it works with ld 2.16.1. | |
863 * BUG: disabled because it causes exception handling to fail | |
864 */ | |
865 argv.push((void *)"-Xlinker"); | |
866 argv.push((void *)"--gc-sections"); | |
867 } | |
868 | |
869 for (i = 0; i < global.params.linkswitches.dim; i++) | |
870 { char *p = (char *)global.params.linkswitches.data[i]; | |
871 if (!p || !p[0] || !(p[0] == '-' && p[1] == 'l')) | |
872 // Don't need -Xlinker if switch starts with -l | |
873 argv.push((void *)"-Xlinker"); | |
874 argv.push((void *) p); | |
875 } | |
876 | |
877 /* Add each library, prefixing it with "-l". | |
878 * The order of libraries passed is: | |
879 * 1. any libraries passed with -L command line switch | |
880 * 2. libraries specified on the command line | |
881 * 3. libraries specified by pragma(lib), which were appended | |
882 * to global.params.libfiles. | |
883 * 4. standard libraries. | |
884 */ | |
885 for (i = 0; i < global.params.libfiles.dim; i++) | |
886 { char *p = (char *)global.params.libfiles.data[i]; | |
887 size_t plen = strlen(p); | |
888 if (plen > 2 && p[plen - 2] == '.' && p[plen -1] == 'a') | |
889 argv.push((void *)p); | |
890 else | |
891 { | |
892 char *s = (char *)mem.malloc(plen + 3); | |
893 s[0] = '-'; | |
894 s[1] = 'l'; | |
895 memcpy(s + 2, p, plen + 1); | |
896 argv.push((void *)s); | |
897 } | |
898 } | |
899 | |
900 /* Standard libraries must go after user specified libraries | |
901 * passed with -l. | |
902 */ | |
903 const char *libname = (global.params.symdebug) | |
904 ? global.params.debuglibname | |
905 : global.params.defaultlibname; | |
906 char *buf = (char *)malloc(2 + strlen(libname) + 1); | |
907 strcpy(buf, "-l"); | |
908 strcpy(buf + 2, libname); | |
909 argv.push((void *)buf); // turns into /usr/lib/libphobos2.a | |
910 | |
911 // argv.push((void *)"-ldruntime"); | |
912 argv.push((void *)"-lpthread"); | |
913 argv.push((void *)"-lm"); | |
914 | |
915 if (!global.params.quiet || global.params.verbose) | |
916 { | |
917 // Print it | |
918 for (i = 0; i < argv.dim; i++) | |
919 printf("%s ", (char *)argv.data[i]); | |
920 printf("\n"); | |
921 fflush(stdout); | |
922 } | |
923 | |
924 argv.push(null); | |
925 childpid = fork(); | |
926 if (childpid == 0) | |
927 { | |
928 execvp((char *)argv.data[0], (char **)argv.data); | |
929 perror((char *)argv.data[0]); // failed to execute | |
930 return -1; | |
931 } | |
932 | |
933 waitpid(childpid, &status, 0); | |
934 | |
935 status=WEXITSTATUS(status); | |
936 if (status) | |
937 printf("--- errorlevel %d\n", status); | |
938 return status; | |
939 +/ | |
940 } else { | |
941 writef ("Linking is not yet supported for this version of DMD.\n"); | |
942 return -1; | |
943 } | |
944 } | |
945 | |
946 int runProgram() | |
947 { | |
948 assert(false); | |
949 } | |
950 | |
951 void deleteExeFile() | |
952 { | |
953 assert(false); | |
954 } | |
955 | |
956 /**************************************** | |
957 * Write filename to cmdbuf, quoting if necessary. | |
958 */ | |
959 | |
960 void writeFilename(OutBuffer buf, string filename) | |
961 { | |
962 auto len = filename.length; | |
963 /* Loop and see if we need to quote | |
964 */ | |
965 for (size_t i = 0; i < len; i++) | |
966 { | |
967 char c = filename[i]; | |
968 | |
969 if (isalnum(c) || c == '_') | |
970 continue; | |
971 | |
972 /* Need to quote | |
973 */ | |
974 buf.writeByte('"'); | |
975 buf.writestring(filename); | |
976 buf.writeByte('"'); | |
977 return; | |
978 } | |
979 | |
980 /* No quoting necessary | |
981 */ | |
982 buf.writestring(filename); | |
983 } | |
984 | |
985 /****************************** | |
986 * Execute a rule. Return the status. | |
987 * cmd program to run | |
988 * args arguments to cmd, as a string | |
989 * useenv if cmd knows about _CMDLINE environment variable | |
990 */ | |
991 | |
992 version (_WIN32) { | |
993 int executecmd(string cmd, string args, int useenv) | |
994 { | |
995 int status; | |
996 size_t len = args.length; | |
997 | |
998 if (!global.params.quiet || global.params.verbose) | |
999 { | |
1000 printf("%s %s\n", cmd, args); | |
1001 fflush(stdout); | |
1002 } | |
1003 | |
1004 if (len > 255) | |
1005 { | |
1006 char* q; | |
1007 static char[9] envname = "@_CMDLINE"; | |
1008 | |
1009 envname[0] = '@'; | |
1010 switch (useenv) | |
1011 { | |
1012 case 0: goto L1; | |
1013 case 2: envname[0] = '%'; break; | |
1014 default: break; /// | |
1015 } | |
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:
2
diff
changeset
|
1016 version (Bug4054) |
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:
2
diff
changeset
|
1017 q = cast(char*) GC.malloc(envname.sizeof + len + 1); |
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:
2
diff
changeset
|
1018 else |
0 | 1019 q = cast(char*) alloca(envname.sizeof + len + 1); |
1020 sprintf(q, "%s=%s", envname.ptr + 1, args); | |
1021 status = putenv(q); | |
1022 if (status == 0) | |
1023 args = envname[].idup; | |
1024 else | |
1025 { | |
1026 L1: | |
1027 error("command line length of %d is too long",len); | |
1028 } | |
1029 } | |
1030 | |
1031 status = executearg0(cmd, args); | |
1032 version (_WIN32) { | |
1033 if (status == -1) { | |
1034 auto cmdZ = toStringz(cmd); | |
1035 auto argsZ = toStringz(args); | |
1036 status = spawnlp(0, cmdZ, cmdZ, argsZ, null); | |
1037 } | |
1038 } | |
1039 // if (global.params.verbose) | |
1040 // printf("\n"); | |
1041 if (status) | |
1042 { | |
1043 if (status == -1) | |
1044 printf("Can't run '%s', check PATH\n", cmd); | |
1045 else | |
1046 printf("--- errorlevel %d\n", status); | |
1047 } | |
1048 return status; | |
1049 } | |
1050 } | |
1051 | |
1052 /************************************** | |
1053 * Attempt to find command to execute by first looking in the directory | |
1054 * where DMD was run from. | |
1055 * Returns: | |
1056 * -1 did not find command there | |
1057 * !=-1 exit status from command | |
1058 */ | |
1059 | |
1060 version (_WIN32) { | |
1061 int executearg0(string cmd, string args) | |
1062 { | |
1063 string file; | |
1064 string argv0 = global.params.argv0; | |
1065 | |
1066 //printf("argv0='%s', cmd='%s', args='%s'\n",argv0,cmd,args); | |
1067 | |
1068 // If cmd is fully qualified, we don't do this | |
1069 if (FileName.absolute(cmd)) | |
1070 return -1; | |
1071 | |
1072 file = FileName.replaceName(argv0, cmd); | |
1073 | |
1074 //printf("spawning '%s'\n",file); | |
1075 version (_WIN32) { | |
1076 auto fileZ = toStringz(file); | |
1077 auto argsZ = toStringz(args); | |
1078 return spawnl(0, fileZ, fileZ, argsZ, null); | |
1079 } else version (XXX) { ///#elif linux || __APPLE__ || __FreeBSD__ || __sun&&__SVR4 | |
1080 assert(false); | |
1081 /+ | |
1082 char *full; | |
1083 int cmdl = strlen(cmd); | |
1084 | |
1085 full = (char*) mem.malloc(cmdl + strlen(args) + 2); | |
1086 if (full == null) | |
1087 return 1; | |
1088 strcpy(full, cmd); | |
1089 full [cmdl] = ' '; | |
1090 strcpy(full + cmdl + 1, args); | |
1091 | |
1092 int result = system(full); | |
1093 | |
1094 mem.free(full); | |
1095 return result; | |
1096 +/ | |
1097 } else { | |
1098 static assert(false); | |
1099 } | |
1100 } | |
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:
2
diff
changeset
|
1101 } |