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