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