-
Notifications
You must be signed in to change notification settings - Fork 17
/
dmd-script
executable file
·777 lines (710 loc) · 23.2 KB
/
dmd-script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#! /usr/bin/perl -w
# GDC -- D front-end for GCC
# Copyright (C) 2011, 2012 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GCC; see the file COPYING3. If not see
# <http://www.gnu.org/licenses/>.
# This is a wrapper script for gdc that emulates the dmd command.
# -f and -m options are passed to gdc. Extra options are:
#
# -vdmd Print commands executed by this wrapper script
# -q,<arg1>[,<arg2>,<arg3>,...] Pass the comma-separated arguments to gdc
use strict;
use warnings;
use Cwd qw(abs_path);
use FindBin qw($Bin);
use File::Basename;
use File::Spec;
use File::Path;
use File::Temp qw(tempdir);
my $output_directory;
my $output_parents;
my $output_file;
my $header_directory;
my $header_file;
my $documentation_directory;
my $documentation_file;
my $debug = 0;
my $link = 1;
my $header = 0;
my $documentation = 0;
my $json = 0;
my $json_file;
my $map = 0;
my $map_file;
my $run = 0;
my $verbose = 0;
my $show_commands = 0;
my $print_version = 0;
my $seen_all_sources_flag = 0;
my $first_input_file;
my $combine = 0; # Compile multiple sources into a single object file
my $lib = 0;
my $tmpdir;
my %tmpdir_objs;
my $stdin = 0;
my @sources;
my @objects;
my @dobjects;
my @out;
my @link_out;
my @run_args;
# Use the gdc executable in the same directory as this script and account
# for the target prefix.
basename($0) =~ m/^(.*-)?g?dmd(-.*)?$/;
my $target_prefix = $1?$1:"";
my $gdc_dir = abs_path(dirname($0));
my $gdc = File::Spec->catfile( $gdc_dir, $target_prefix . "gdc" . ($2?$2:""));
sub osHasEXE() {
return $^O =~ m/MS(DOS|Win32)|os2/i; # taken from File::Basename
}
sub targetHasEXE() {
my $target = `$gdc -dumpmachine`;
return $target =~ m/mingw/ || $target =~ m/cygwin/;
}
sub pathSep() {
return ";" if $^O =~ m/MS(DOS|Win32)/i;
return "," if $^O =~ m/MacOS/i;
return ":";
}
sub expandHome($) {
my ($path) = (@_);
if ( $^O !~ m/MS(DOS|Win32)|MacOS/i ) {
$path =~ s/^~/$ENV{HOME}/;
}
return $path;
}
sub printUsage() {
print <<EOF
Documentation: http://dlang.org/
http://www.gdcproject.org/
Usage:
gdmd files.d ... { -switch }
files.d D source files
\@cmdfile read arguments from cmdfile
-arch ... pass an -arch ... option to gdc
-c do not link
-cov do code coverage analysis
-D generate documentation
-Dddocdir write documentation file to docdir directory
-Dffilename write documentation file to filename
-d silently allow deprecated features
-dw show use of deprecated features as warnings (default)
-de show use of deprecated features as errors (halt compilation)
-debug compile in debug code
-debug=level compile in debug code <= level
-debug=ident compile in debug code identified by ident
-debuglib=lib debug library to use instead of phobos
-defaultlib=lib default library to use instead of phobos
-deps=filename write module dependencies to filename
-f... pass an -f... option to gdc
-fall-sources for every source file, semantically process each file preceding it
-framework ... pass a -framework ... option to gdc
-g add symbolic debug info
-gs always emit stack frame
-gx add stack stomp code
-H generate 'header' file
-Hdhdrdir write 'header' file to hdrdir directory
-Hffilename write 'header' file to filename
--help|-h print help
-Ipath where to look for imports
-ignore ignore unsupported pragmas
-inline do function inlining
-Jpath where to look for string imports
-Llinkerflag pass linkerflag to link
-lib generate library rather than object files
-m... pass an -m... option to gdc
-main add default main() (e.g. for unittesting)
-man open web browser on manual page
-map generate linker .map file
-noboundscheck turns off array bounds checking for all functions
-O optimize
-o- do not write object file
-odobjdir write object files to directory objdir
-offilename name output file to filename
-op do not strip paths from source file
-pipe use pipes rather than intermediate files
-profile profile runtime performance of generated code
-quiet suppress unnecessary messages
-q,arg1,... pass arg1, arg2, etc. to to gdc
-release compile release version
-run srcfile args... run resulting program, passing args
-shared generate shared library (DLL)
-unittest compile in unit tests
-v verbose
-vdmd print commands run by this script
-verrors=<num> limit the number of error messages (0 means unlimited)
-verrors=context (enabled by default) show diagnostic messages with context
-verrors=spec show errors from speculative compiles such as __traits(compiles,...)
--version print compiler version and exit
-version=level compile in version code >= level
-version=ident compile in version code identified by ident
-vtls list all variables going into thread local storage
-w enable warnings
-wi enable informational warnings
-X generate JSON file
-Xffilename write JSON file to filename
EOF
;
}
sub errorExit(@) {
print STDERR "gdmd: ", @_, "\n" if @_;
exit 1;
}
use subs qw(errorExit);
sub readINI {
# look for dmd.conf in the following sequence of directories:
# - current working directory
# - directory specified by the HOME environment variable
# - directory gdmd resides in
# - /etc directory
my @confpaths = ("./", "$ENV{HOME}/", "$Bin/", "/etc/");
my $dmdconfpath = "";
my $dmdconf = "";
foreach my $confpath (@confpaths) {
if (-e $confpath."dmd.conf") {
$dmdconfpath = $confpath;
$dmdconf = $confpath."dmd.conf";
last;
}
}
if (-e $dmdconf) {
open(DMDCONF, "<$dmdconf");
my $envsection = 0;
while(<DMDCONF>) {
# Ignore all lines up to [Environment] section
if ($_ =~ /^\s*\[\s*Environment\s*\]\s*$/) {
$envsection = 1;
next;
}
next if (!$envsection);
# Ignore comments
next if ($_ =~ /^\s*;/);
# Ignore empty lines
next if ($_ =~ /^\s*$/);
# Check correct syntax
$_ =~ /^\s*(\S+?)\s*=\s*(.*)\s*$/;
if ($&) {
my $VAR = $1;
my $VAL = $2;
# The special name %@P% is replaced with the path to dmd.conf
$VAL =~ s/%\@P%/$dmdconfpath/g;
# Names enclosed by %% are searched for in the existing environment and inserted
while ($VAL =~ /%(\S+?)%/) {
my $envp = $1;
if ($ENV{$envp}) {
$VAL =~ s/%$envp%/$ENV{$envp}/g;
} else {
$VAL =~ s/%$envp%//g;
}
}
$ENV{$VAR} = "$VAL";
} else {
errorExit "syntax error at line $. in file $dmdconf";
}
}
close DMDCONF;
}
}
my $gcc_version = `$gdc -dumpversion`;
my $gcc_maj;
my $gcc_min;
chomp $gcc_version;
if ($gcc_version =~ m/^\d+$/) {
($gcc_maj, $gcc_min) = ($gcc_version, 0);
} else {
($gcc_maj, $gcc_min) = ($gcc_version =~ m/^(\d+)\.(\d+)/);
}
#my $target_machine = `$gdc -dumpmachine`;
#chomp $target_machine;
sub addSourceFile($) {
my ($arg) = @_;
$first_input_file = $arg if ! $first_input_file;
push @sources, $arg;
}
sub argCheck($$) {
my ($name,$arg) = @_;
errorExit "argument expected for switch '$name'" unless defined $arg;
}
sub determineARexe() {
my $name = $target_prefix . 'ar';
$name .= '.exe' if (osHasEXE());
# Prefer the 'ar' in the same directory as gdc even if there is no
# target prefix.
my $path = File::Spec->catfile( $gdc_dir, $name );
return $path if -x $path;
if ( length $target_prefix ) {
foreach my $dir (split pathSep, $ENV{PATH}) {
$path = File::Spec->catfile( $dir, $name );
return $name if -x $path; # Could return $path, but this looks better
}
errorExit "Could not find archiver command '$name'.";
} else {
return "ar";
}
}
sub determineARcommand() {
my @exe = determineARexe();
return (@exe, 'cru');
}
sub browse($) {
my ($url) = @_;
my @cmd;
if ($^O =~ m/MSWin32/i) {
@cmd = qw(cmd /c start);
} elsif ($^O =~ m/darwin/i &&
-x '/usr/bin/open') { # MacOS X vs. just Darwin
@cmd = 'open';
} elsif ($ENV{KDE_FULL_SESSION} eq 'true') {
@cmd = qw(kfmclient exec);
} elsif ($ENV{GNOME_DESKTOP_SESSION_ID} ne '') {
@cmd = 'gnome-open';
} else {
errorExit "Sorry, I do not know how to start your browser.\nManual URL: $url"
}
push @cmd, $url;
system @cmd;
print "Opening documentation page.";
exit 0;
}
# Load dmd.conf before before parsing arguments.
readINI();
if ($ENV{DFLAGS}) {
push @ARGV, split /\s+/, $ENV{DFLAGS};
}
my $arg_i = 0;
while ( $arg_i < scalar(@ARGV) ) {
my $arg = $ARGV[$arg_i++];
if ($arg eq '-arch' ) {
push @out, '-arch', $ARGV[$arg_i++];
} elsif ( $arg =~ m/^(-betterC|-vcolumns)$/ ) {
# ignored
} elsif ($arg =~ m/^-c$/ ) {
$link = 0;
} elsif ( $arg eq '-cov' ) {
push @out, '-fprofile-arcs', '-ftest-coverage';
} elsif ( $arg =~ m/^-D$/ ) {
$documentation = 1;
} elsif ( $arg =~ m/^-Dd(.*)$/ ) {
$documentation = 1;
$documentation_directory = $1;
} elsif ( $arg =~ m/^-Df(.*)$/ ) {
$documentation = 1;
$documentation_file = $1;
} elsif ( $arg =~ m/^-d$/ ) {
push @out, '-Wno-deprecated';
} elsif ( $arg =~ m/^-de$/ ) {
push @out, '-Wdeprecated', '-Werror';
} elsif ( $arg =~ m/^-dw$/ ) {
push @out, '-Wdeprecated';
} elsif ( $arg =~ m/^-debug(?:=(.*))?$/ ) {
push @out, (defined($1) ? "-fdebug=$1" : '-fdebug');
} elsif ( $arg =~ m/^-debuglib=(.*)$/ ) {
push @link_out, '-debuglib', $1;
} elsif ( $arg =~ m/^-debug.*$/ ) {
# Passing this to gdc only gives warnings; exit with an error here
errorExit "unrecognized switch '$arg'";
} elsif ( $arg =~ m/^-defaultlib=(.*)$/ ) {
push @link_out, '-defaultlib', $1;
} elsif ( $arg =~ m/^-deps=(.*)$/ ) {
push @out, (defined($1) ? "-fdeps=$1" : '-fdeps');
} elsif ( $arg =~ m/^-g$/ ) {
$debug = 1;
push @out, '-g';
} elsif ( $arg =~ m/^-gs$/ ) {
push @out, '-fno-omit-frame-pointer';
} elsif ( $arg =~ m/^-gt$/ ) {
errorExit "use -profile instead of -gt";
push @out, '-pg';
} elsif ( $arg =~ m/^-gx$/ ) {
push @out, '-fstack-protector';
} elsif ( $arg =~ m/^-H$/ ) {
$header = 1;
} elsif ( $arg =~ m/^-Hd(.*)$/ ) {
$header = 1;
$header_directory = $1;
} elsif ( $arg =~ m/^-Hf(.*)$/ ) {
$header = 1;
$header_file = $1;
} elsif ( $arg eq '--help' || $arg eq '-h' ) {
printUsage;
exit 0;
} elsif ($arg eq '-framework' ) {
push @link_out, '-framework', $ARGV[$arg_i++];
} elsif ( $arg eq '-ignore' ) {
push @out, '-fignore-unknown-pragmas';
} elsif ( $arg =~ m/^-inline$/ ) {
push @out, '-finline-functions';
} elsif ( $arg =~ m/^-I(.*)$/ ) {
foreach my $i (split pathSep, $1) {
push @out, '-I', expandHome $i;
}
} elsif ( $arg =~ m/^-J(.*)$/ ) {
foreach my $i (split pathSep, $1) {
push @out, '-J', expandHome $i;
}
} elsif ( $arg =~ m/^-L(.*)$/ ) {
push @link_out, '-Wl,' . $1;
} elsif ( $arg eq '-lib' ) {
$lib = 1;
$link = 0;
$tmpdir = tempdir(CLEANUP => 1);
} elsif ( $arg =~ m/^-O$/ ) {
push @out, '-O2';
} elsif ( $arg =~ m/^-o-$/ ) {
push @out, '-fsyntax-only';
$link = 0;
} elsif ( $arg =~ m/^-od(.*)$/ ) {
$output_directory = $1;
} elsif ( $arg =~ m/^-of(.*)$/ ) {
$output_file = $1;
} elsif ( $arg =~ m/^-op$/ ) {
$output_parents = 1;
} elsif ( $arg =~ m/^-pipe$/ ) {
push @out, '-pipe';
} elsif ( $arg =~ m/^-profile$/ ) {
# there is more to profiling than this ... -finstrument-functions?
push @out, '-pg';
} elsif ( $arg =~ m/^-release$/ ) {
push @out, '-frelease';
} elsif ( $arg eq '-run' ) {
$run = 1;
$arg = $ARGV[$arg_i++];
argCheck '-run', $arg;
addSourceFile $arg;
push @run_args, @ARGV[$arg_i..$#ARGV];
last;
} elsif ( $arg =~ m/^-shared$/ ) {
push @out, '-shared';
} elsif ( $arg =~ m/^-noboundscheck$/ ) {
push @out, '-fno-bounds-check';
} elsif ( $arg =~ m/^-unittest$/ ) {
push @out, '-funittest';
} elsif ( $arg =~ m/^-v$/ ) {
$verbose = 1;
if ($gcc_maj >= 8) {
push @out, '--verbose';
} else {
push @out, '-fd-verbose';
}
} elsif ( $arg =~ m/^-vtls$/ ) {
if ($gcc_maj >= 8) {
push @out, '-ftransition=tls';
} else {
push @out, '-fd-vtls';
}
} elsif ( $arg =~ m/^-v1$/ ) {
push @out, '-fd-version=1';
} elsif ( $arg =~ m/^-verrors$/ ) {
errorExit "switch -verrors expects a value";
} elsif ( $arg =~ m/^-verrors=(\d*)$/ ) {
push @out, "-fmax-errors=" . ( $1 || 0 );
} elsif ( $arg =~ m/^-verrors=(.+)$/ ) {
if ( $1 eq "context" ) {
# ignored
} elsif ( $1 eq "spec" ) {
push @out, '-Wspeculative';
} else {
errorExit "unrecongnized value passed to -verrors '$1'";
}
} elsif ( $arg =~ m/^--version$/ ) {
$print_version = 1;
} elsif ( $arg =~ m/^-version=(.*)$/ ) {
push @out, "-fversion=$1";
} elsif ( $arg =~ m/^-version.*$/ ) {
errorExit "unrecognized switch '$arg'";
} elsif ( $arg =~ m/^-vdmd$/ ) {
$show_commands = 1;
} elsif ( $arg =~ m/^-w$/ ) {
push @out, "-Werror";
} elsif ( $arg =~ m/^-wi$/ ) {
push @out, "-Wall";
} elsif ( $arg =~ m/^-quiet$/ ) {
# ignored
} elsif ( $arg =~ m/^-q,(.*)$/ ) {
push @out, split(qr/,/, $1);
} elsif ( $arg =~ m/^-X$/ ) {
$json = 1;
} elsif ( $arg =~ m/^-Xf(.*)$/ ) {
$json = 1;
$json_file = $1;
} elsif ( $arg eq '-fall-sources' ) {
$seen_all_sources_flag = 1;
} elsif ( $arg =~ m/^-f.+/ ) {
# Pass -fxxx options
push @out, $arg;
} elsif ($arg eq '-main') {
push @out, '-fmain';
} elsif ($arg eq '-man') {
browse("http://www.gdcproject.org/wiki/UserDocumentation");
exit 0;
} elsif ( $arg =~ m/^-map$/ ) {
$map = 1;
if ($ARGV[$arg_i] =~ m/.map$/ ) {
$map_file = $ARGV[$arg_i++];
}
} elsif ( $arg =~ m/^-m.+/ ) {
# Pass -mxxx options
push @out, $arg;
} elsif ( $arg =~ m/^-.+$/ ) {
errorExit "unrecognized switch '$arg'";
} elsif ( $arg =~ m/^\@(.+)$/i ) {
# Append response file to end of ARGV.
open(my $rsp_fh, "<", $1) or die("Can't read response file: $!");
my $rsp = do { local $/; <$rsp_fh> };
close($rsp_fh);
while (length $rsp) {
if ($rsp =~ m/^"(([^\\"]|\\.)*(\\\\)*)"\s*/ ) {
$arg = $1;
$rsp = $';
$arg =~ s/\\"/"/g;
$arg =~ s/(\\+)\1$/$1/g;
} else {
$rsp =~ m/^(\S*)\s*/;
$arg = $1;
$rsp = $';
}
push @ARGV, $arg;
}
} elsif ( $arg =~ m/^.+\.d$/i ||
$arg =~ m/^.+\.dd$/i ||
$arg =~ m/^.+\.di$/i) {
addSourceFile $arg;
} elsif ( $arg =~ m/^.+\.ddoc/i ) {
push @out, "-fdoc-inc=$arg";
} elsif ( $arg eq '-' ) {
push @out, "-x";
push @out, 'd';
push @out, '-';
$stdin = 1;
} elsif ( $arg !~ m/\./ ) {
addSourceFile $arg . ".d";
} elsif ( $arg =~ m/^(.+)(\.exe)$/i ) {
$first_input_file = $arg if ! $first_input_file;
$output_file = $1;
if ( targetHasEXE() ) {
$output_file .= $2;
}
} else {
push @objects, $arg
}
}
if ($link || (! $lib && $output_file && scalar(@sources) > 1)) {
$combine = 1;
}
if ( $run && ! $link ) {
errorExit "flags conflict with -run";
}
if ( $stdin && ! $output_file && $output_directory ) {
$output_file = "$output_directory/a.out"
}
if ( $output_file ) {
my $dir = dirname( $output_file );
eval { mkpath( $dir ) };
if ($@) {
errorExit "could not create $dir: $@";
}
}
if ( ($link || $lib) && ! $output_file && $first_input_file ) {
$output_file = fileparse( $first_input_file, qr/\..*$/ );
if ( $link && targetHasEXE() ) {
$output_file .= '.exe';
} elsif ( $lib ) {
$output_file .= '.a';
}
}
if ( $print_version ) {
my @cmd = ($gdc, '--version', @out);
my $result = system(@cmd);
errorExit if $result & 0xff; # Give up if can't exec or gdc exited with a signal
exit 0;
} elsif (! scalar(@sources) && ! $stdin && ! (($link || $lib) && scalar(@objects))) {
my @cmd = ($gdc, '--version', @out);
my $result = system(@cmd);
errorExit if $result & 0xff; # Give up if can't exec or gdc exited with a signal
printUsage;
exit 1;
}
my $ok = 1;
foreach my $srcf_i (@sources) {
# Step 1: Determine the object file path
my $outf;
my $hdrd;
my $docd;
my $srcf = $srcf_i; # To avoid modifying elements of @sources
my @outbits;
my @hdrbits;
my @docbits;
if ( $lib ) {
# Generate a unique name in the temporary directory. The -op argument
# is ignored in this case and there could very well be duplicate base
# names.
my $base = basename( $srcf, '.d' );
my $i = 1;
$outf = $base . '.o';
while ( defined $tmpdir_objs{$outf} ) {
$outf = $base . '-' . $i++ . '.o';
}
$tmpdir_objs{$outf} = 1;
$outf = File::Spec->catfile( $tmpdir, $outf );
} elsif ( ! ($link || $lib) && $output_file ) {
$outf = $output_file;
} else {
if ( $output_directory ) {
push @outbits, $output_directory;
}
if ( $output_parents ) {
push @outbits, dirname( $srcf );
}
if ( scalar( @outbits )) {
my $dir = File::Spec->catfile( @outbits );
eval { mkpath($dir) };
if ($@) {
errorExit "could not create $dir: $@";
}
}
# Note: There is currently no ($combine && $lib) case to check
if ( $combine && $link) {
push @outbits, basename( $output_file, '.exe' ) . '.o';
} else {
push @outbits, basename( $srcf, '.d' ) . '.o';
}
$outf = File::Spec->catfile( @outbits );
if ( $combine && $link && $outf eq $output_file) {
$outf .= '.o';
}
}
if ($header) {
if ( $header_directory ) {
push @hdrbits, $header_directory;
}
if ( $output_parents ) {
push @hdrbits, dirname( $srcf );
}
if ( scalar( @hdrbits )) {
$hdrd = File::Spec->catfile( @hdrbits );
eval { mkpath($hdrd) };
if ($@) {
errorExit "could not create $hdrd: $@";
}
}
}
if ($documentation) {
if ( $documentation_directory ) {
push @docbits, $documentation_directory;
}
if ( $output_parents ) {
push @docbits, dirname( $srcf );
}
if ( scalar( @docbits )) {
$docd = File::Spec->catfile( @docbits );
eval { mkpath($docd) };
if ($@) {
errorExit "could not create $docd: $@";
}
}
}
if ($json) {
if (! $json_file) {
$json_file = substr($first_input_file, 0, length($first_input_file)-2) . ".json";
}
if ($gcc_maj >= 8) {
push @out, '-Xf' . $json_file;
} else {
push @out, '-fXf=' . $json_file;
}
}
if ($map) {
if (! $map_file) {
$map_file = substr($first_input_file, 0, length($first_input_file)-2) . ".map";
}
# Check for Mac (Untested)
if ($^O =~ m/darwin/i) {
push @link_out, '-Wl,-map=' . $map_file;
} else {
push @link_out, '-Wl,-Map=' . $map_file;
}
}
push @dobjects, $outf;
my @source_args;
if ( $combine ) {
if ($gcc_maj == 4 && $gcc_min <= 5) {
push @source_args, "-combine";
}
push @source_args, @sources;
} elsif ( $seen_all_sources_flag ) {
@source_args = (@sources, "-fonly=$srcf");
} else {
@source_args = $srcf;
}
my @interface;
if ( $header ) {
push @interface, '-fintfc';
push @interface, "-fintfc-dir=$hdrd" if $hdrd;
push @interface, "-fintfc-file=$header_file" if $header_file;
}
my @documentation;
if ( $documentation ) {
push @documentation, '-fdoc';
push @documentation, "-fdoc-dir=$docd" if $docd;
push @documentation, "-fdoc-file=$documentation_file" if $documentation_file;
}
# Step 2: Run the compiler driver
my @cmd = ($gdc, @out, '-c', @source_args, '-o', $outf, @interface, @documentation);
if ( $show_commands ) {
print join(' ', @cmd), "\n";
}
my $result = system(@cmd);
errorExit if $result & 0xff; # Give up if can't exec or gdc exited with a signal
$ok = $ok && $result == 0;
last if $combine;
}
if ($ok && ($link || $stdin)) {
my @cmd = ($gdc, @out, @dobjects, @objects, @link_out);
if ( $output_file ) {
push @cmd, '-o', $output_file;
}
if ( $show_commands ) {
print join(' ', @cmd), "\n";
}
$ok = $ok && system(@cmd) == 0;
} elsif ($ok && $lib) {
my @ar_cmd = determineARcommand();
my $outf = $output_file;
if ( $output_directory ) {
$outf = File::Spec->catfile($output_directory, $output_file);
}
my @cmd = (@ar_cmd, $outf, @dobjects, @objects);
if ( $show_commands ) {
print join(' ', @cmd), "\n";
}
$ok = $ok && system(@cmd) == 0;
}
if ($ok && $run) {
my @cmd = (abs_path($output_file), @run_args);
if ($verbose) {
print join(' ', @cmd), "\n";
}
my $result = system @cmd;
unlink ($output_file, @dobjects);
if ($result == -1) {
print STDERR "$output_file: $!\n";
exit 127;
} elsif ($result & 127) {
exit 128 + ($result & 127);
} else {
exit $result >> 8;
}
}
exit ($ok ? 0 : 1);