Skip to content

Instantly share code, notes, and snippets.

@gerdr
Last active December 20, 2015 20:39
Show Gist options
  • Save gerdr/6192075 to your computer and use it in GitHub Desktop.
Save gerdr/6192075 to your computer and use it in GitHub Desktop.
MoarVM Configure.pl
#!perl
use 5.010;
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
my %SHELLS = (
posix => {
cat => 'cat',
rm => 'rn -f',
},
win32 => {
cat => 'type',
rm => 'del',
},
);
my %TOOLCHAINS = (
gnu => {
-compiler => 'gcc',
make => 'make',
ccout => '-o',
ccinc => '-I',
ldout => undef,
},
msvc => {
-compiler => 'cl',
make => 'nmake',
ccout => '/Fo',
ccinc => '/I',
ldout => '/out:',
},
);
my %COMPILERS = (
gcc => {
-toolchain => 'gnu',
cc => 'gcc',
ld => undef,
},
clang => {
-toolchain => 'gnu',
cc => 'clang',
ld => undef,
},
cl => {
-toolchain => 'msvc',
cc => 'cl',
ld => 'link',
},
);
my %SYSTEMS = (
generic => [ qw( posix gnu gcc ), {} ],
linux => [ qw( posix gnu gcc ), {} ],
darwin => [ qw( posix gnu clang ), {} ],
freebsd => [ qw( posix gnu clang ), {} ],
cygwin => [ qw( posix gnu gcc ), {} ],
win32 => [ qw( win32 msvc cl ), {} ],
mingw32 => [ qw( win32 gnu gcc ), { make => 'gmake' } ],
);
my %args;
my %defaults;
my %config;
sub cross_setup {
my ($build, $host) = @_;
die "Both --build and --host need to be specified\n"
unless defined $build && defined $host;
my $cc = "$host-gcc";
for (\$build, \$host) {
if ($$_ =~ /-(\w+)$/) {
$$_ = $1;
if (!exists $SYSTEMS{$1}) {
warn "Unknown OS $1 - assuming GNU userland\n";
$$_ = 'generic';
}
}
else { die "Failed to parse triple $$_\n" }
}
$build = $SYSTEMS{$build};
$host = $SYSTEMS{$host};
my $shell = $SHELLS{ $build->[0] };
my $toolchain = $TOOLCHAINS{gnu};
@defaults{ keys %$shell } = values %$shell;
@defaults{ keys %$toolchain } = values %$toolchain;
$defaults{cc} = $cc;
}
sub native_setup {
my ($os) = @_;
if (!exists $SYSTEMS{$os}) {
warn "Unknown OS $os - assuming GNU userland\n";
$os = 'generic';
}
my ($shell, $toolchain, $compiler, $overrides) = @{$SYSTEMS{$os}};
$shell = $SHELLS{$shell};
$toolchain = $TOOLCHAINS{$toolchain};
$compiler = $COMPILERS{$compiler};
@defaults{ keys %$shell } = values %$shell;
@defaults{ keys %$toolchain } = values %$toolchain;
@defaults{ keys %$compiler } = values %$compiler;
@defaults{ keys %$overrides } = values %$overrides;
if (exists $args{shell}) {
$shell = $args{shell};
die "Unsupported shell $shell\n"
unless exists $SHELLS{$shell};
$shell = $SHELLS{$shell};
@defaults{ keys %$shell } = values %$shell;
}
if (exists $args{toolchain}) {
$toolchain = $args{toolchain};
die "Unsupported toolchain $toolchain\n"
unless exists $TOOLCHAINS{$toolchain};
$toolchain = $TOOLCHAINS{$toolchain};
$compiler = $COMPILERS{ $toolchain->{-compiler} };
@defaults{ keys %$toolchain } = values %$toolchain;
@defaults{ keys %$compiler } = values %$compiler;
}
if (exists $args{compiler}) {
$compiler = $args{compiler};
die "Unsupported compiler $compiler\n"
unless exists $COMPILERS{$compiler};
$compiler = $COMPILERS{$compiler};
$toolchain = $TOOLCHAINS{ $compiler->{-toolchain} };
@defaults{ keys %$toolchain } = values %$toolchain;
@defaults{ keys %$compiler } = values %$compiler;
}
}
my $fail = !GetOptions(\%args, qw(
help|?
debug! optimize! instrument!
os=s shell=s toolchain=s compiler=s
cc=s ld=s make=s
build=s host=s
));
die "See --help for further information\n"
if $fail;
pod2usage(1)
if $args{help};
$args{debug} //= 0 + !$args{optimize};
$args{optimize} //= 0 + !$args{debug};
$args{instrument} //= 0;
if (exists $args{build} || exists $args{host}) {
cross_setup $args{build}, $args{host};
}
else {
native_setup $args{os} // {
'MSWin32' => 'win32'
}->{$^O} // $^O;
}
my @keys = qw( cc ld make );
@config{@keys} = @args{@keys};
for (keys %defaults) {
next if /^-/;
$config{$_} //= $defaults{$_};
}
$config{ld} //= $config{cc};
$config{ldout} //= $config{ccout};
{
use Data::Dumper;
print Dumper { args => \%args, config => \%config };
}
__END__
=head1 SYNOPSIS
./Configure.pl -?|--help
./Configure.pl [--os <os>] [--shell <shell>]
[--toolchain <toolchain>] [--compiler <compiler>]
[--cc <cc>] [--ld <ld>] [--make <make>]
[--debug] [--optimize] [--instrument]
./Configure.pl --build <build-triple> --host <host-triple>
[--cc <cc>] [--ld <ld>] [--make <make>]
[--debug] [--optimize] [--instrument]
=head1 OPTIONS
=over 4
Except for C<-?|--help>, any option can be explicitly turned off by
preceding it with C<no->, as in C<--no-optimize>.
=item -?|--help
Show this help information.
=item --debug
Turn on debugging flags during compile and link. If C<--optimize> is not
explicitly set, debug defaults to on, and optimize defaults to off.
=item --optimize
Turn on optimization flags during compile and link. If C<--debug> is not
explicitly set, turning this on defaults debugging off; otherwise this
defaults to the opposite of C<--debug>.
=item --instrument
Turn on extra instrumentation flags during compile and link; for example,
turns on Address Sanitizer when compiling with F<clang>. Defaults to off.
=item TODO
=back
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment