forked from MoarVM/MoarVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigure.pl
506 lines (373 loc) · 12.1 KB
/
Configure.pl
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
#!perl
use 5.010;
use strict;
use warnings;
use Config;
use Getopt::Long;
use Pod::Usage;
use build::setup;
use build::auto;
my $NAME = 'moarvm';
my $GENLIST = 'build/gen.list';
# configuration logic
my $failed = 0;
my %args;
my %defaults;
my %config;
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
big-endian
)) or die "See --help for further information\n";
pod2usage(1) if $args{help};
print "Welcome to MoarVM!\n\n";
# fiddle with flags
$args{debug} //= 0 + !$args{optimize};
$args{optimize} //= 0 + !$args{debug};
$args{instrument} //= 0;
$args{'big-endian'} //= 0;
# fill in C<%defaults>
if (exists $args{build} || exists $args{host}) {
setup_cross($args{build}, $args{host});
}
else {
setup_native($args{os} // {
'MSWin32' => 'win32'
}->{$^O} // $^O);
}
$config{name} = $NAME;
# set options that take priority over all others
my @keys = qw( cc ld make );
@config{@keys} = @args{@keys};
for (keys %defaults) {
next if /^-/;
$config{$_} //= $defaults{$_};
}
# misc defaults
$config{exe} //= '';
$config{defs} //= [];
$config{libs} //= [ qw( m pthread ) ];
$config{platform} //= '$(PLATFORM_POSIX)';
$config{crossconf} //= '';
# assume the compiler can be used as linker frontend
$config{ld} //= $config{cc};
$config{ldout} //= $config{ccout};
$config{ldmiscflags} //= $config{ccmiscflags};
$config{ldoptiflags} //= $config{ccoptiflags};
$config{lddebugflags} //= $config{ccdebugflags};
$config{ldinstflags} //= $config{ccinstflags};
# mangle OS library names
$config{ldlibs} = join ' ', map {
sprintf $config{ldarg}, $_;
} @{$config{libs}};
# generate CFLAGS
my @cflags = ($config{ccmiscflags});
push @cflags, $config{ccoptiflags} if $args{optimize};
push @cflags, $config{ccdebugflags} if $args{debug};
push @cflags, $config{ccinstflags} if $args{instrument};
push @cflags, $config{ccwarnflags};
push @cflags, map { "$config{ccdef}$_" } @{$config{defs}};
$config{cflags} = join ' ', @cflags;
# generate LDFLAGS
my @ldflags = ($config{ldmiscflags});
push @ldflags, $config{ldoptiflags} if $args{optimize};
push @ldflags, $config{lddebugflags} if $args{debug};
push @ldflags, $config{ldinstflags} if $args{instrument};
$config{ldflags} = join ' ', @ldflags;
# some toolchains generate garbage
my @auxfiles = @{ $defaults{-auxfiles} };
$config{clean} = @auxfiles ? '$(RM) ' . join ' ', @auxfiles : '@:';
print "OK\n";
print dots(' assumed byte order');
if ($defaults{-be}) {
print "BE\n";
$config{bedef} = 'define';
}
else {
print "LE\n";
$config{bedef} = 'undef';
}
if ($config{crossconf}) {
build::auto::detect_cross(\%config, \%defaults);
}
else {
build::auto::detect_native(\%config, \%defaults);
}
# dump configuration
print "\n", <<TERM, "\n";
make: $config{make}
compile: $config{cc} $config{cflags}
link: $config{ld} $config{ldflags}
libs: $config{ldlibs}
TERM
print dots('Configuring 3rdparty libs');
my @thirdpartylibs;
my $thirdparty = $defaults{-thirdparty};
for (keys %$thirdparty) {
my $current = $thirdparty->{$_};
my @keys = ( "${_}lib", "${_}objects", "${_}rule", "${_}clean");
# don't build the library (libatomic_ops can be header-only)
unless (defined $current) {
@config{@keys} = ("__${_}__", '', '@:', '@:');
next;
}
my ($lib, $objects, $rule, $clean);
$lib = sprintf "%s/$config{lib}",
$current->{path},
$current->{name};
# C<rule> and C<build> can be used to augment all build types
$rule = $current->{rule};
$clean = $current->{clean};
# select type of build
# dummy build - nothing to do
if (exists $current->{dummy}) {
$clean //= sprintf '$(RM) %s', $lib;
}
# use explicit object list
elsif (exists $current->{objects}) {
$objects = $current->{objects};
$rule //= sprintf '$(AR) $(ARFLAGS) @arout@$@ @%sobjects@', $_;
$clean //= sprintf '$(RM) @%slib@ @%sobjects@', $_, $_;
}
# find *.c files and build objects for those
elsif (exists $current->{src}) {
my @sources = map { glob "$_/*.c" } @{ $current->{src} };
my $globs = join ' ', map { $_ . '/*@obj@' } @{ $current->{src} };
$objects = join ' ', map { s/\.c$/\@obj\@/; $_ } @sources;
$rule //= sprintf '$(AR) $(ARFLAGS) @arout@$@ %s', $globs;
$clean //= sprintf '$(RM) %s %s', $lib, $globs;
}
# use an explicit rule (which has already been set)
elsif (exists $current->{rule}) {}
# give up
else {
softfail("no idea how to build '$lib'");
print dots(' continuing anyway');
}
@config{@keys} = ($lib, $objects // '', $rule // '@:', $clean // '@:');
push @thirdpartylibs, $config{"${_}lib"};
}
$config{thirdpartylibs} = join ' ', @thirdpartylibs;
my $thirdpartylibs = join "\n" . ' ' x 12, sort @thirdpartylibs;
print "OK\n";
# dump 3rdparty libs we need to build
print "\n", <<TERM, "\n";
3rdparty: $thirdpartylibs
TERM
# read list of files to generate
open my $listfile, '<', $GENLIST
or die "$GENLIST: $!\n";
my $target;
while (<$listfile>) {
s/^\s+|\s+$//;
next if /^#|^$/;
$target = $_, next
unless defined $target;
generate($target, $_);
$target = undef;
}
close $listfile;
# configuration completed
print "\n", $failed ? <<TERM1 : <<TERM2;
Configuration FAIL. You can try to salvage the generated Makefile.
TERM1
Configuration SUCCESS. Type '$config{'make'}' to build.
TERM2
exit $failed;
# helper functions
# fill in defaults for native builds
sub setup_native {
my ($os) = @_;
print dots("Configuring native build environment");
if (!exists $::SYSTEMS{$os}) {
softfail("unknown OS '$os'");
print dots(" assuming GNU userland");
$os = 'generic';
}
my ($shell, $toolchain, $compiler, $overrides) = @{$::SYSTEMS{$os}};
$shell = $::SHELLS{$shell};
$toolchain = $::TOOLCHAINS{$toolchain};
$compiler = $::COMPILERS{$compiler};
set_defaults($shell, $toolchain, $compiler, $overrides);
if (exists $args{shell}) {
$shell = $args{shell};
hardfail("unsupported shell '$shell'")
unless exists $::SHELLS{$shell};
$shell = $::SHELLS{$shell};
set_defaults($shell);
}
if (exists $args{toolchain}) {
$toolchain = $args{toolchain};
hardfail("unsupported toolchain '$toolchain'")
unless exists $::TOOLCHAINS{$toolchain};
$toolchain = $::TOOLCHAINS{$toolchain};
$compiler = $::COMPILERS{ $toolchain->{-compiler} };
set_defaults($toolchain, $compiler);
}
if (exists $args{compiler}) {
$compiler = $args{compiler};
hardfail("unsupported compiler '$compiler'")
unless exists $::COMPILERS{$compiler};
$compiler = $::COMPILERS{$compiler};
unless (exists $args{toolchain}) {
$toolchain = $::TOOLCHAINS{ $compiler->{-toolchain} };
set_defaults($toolchain);
}
set_defaults($compiler);
}
my $order = $Config{byteorder};
if ($order eq '1234' || $order eq '12345678') {
$defaults{-be} = 0;
}
elsif ($order eq '4321' || $order eq '87654321') {
$defaults{-be} = 1;
}
else {
::hardfail("unsupported byte order $order");
}
}
# fill in defaults for cross builds
sub setup_cross {
my ($build, $host) = @_;
print dots("Configuring cross build environment");
hardfail("both --build and --host need to be specified")
unless defined $build && defined $host;
my $cc = "$host-gcc";
my $ar = "$host-ar";
my $crossconf = "--build=$build --host=$host";
for (\$build, \$host) {
if ($$_ =~ /-(\w+)$/) {
$$_ = $1;
if (!exists $::SYSTEMS{$1}) {
softfail("unknown OS '$1'");
print dots(" assuming GNU userland");
$$_ = 'generic';
}
}
else { hardfail("failed to parse triple '$$_'") }
}
$build = $::SYSTEMS{$build};
$host = $::SYSTEMS{$host};
my $shell = $::SHELLS{ $build->[0] };
my $toolchain = $::TOOLCHAINS{gnu};
my $compiler = $::COMPILERS{gcc};
my $overrides = $host->[3];
set_defaults($shell, $toolchain, $compiler, $overrides);
$defaults{cc} = $cc;
$defaults{ar} = $ar;
$defaults{crossconf} = $crossconf;
$defaults{-be} = $args{'big-endian'};
}
# sets C<%defaults> from C<@_>
sub set_defaults {
# getting the correct 3rdparty information is somewhat tricky
my $thirdparty = $defaults{-thirdparty} // \%::THIRDPARTY;
@defaults{ keys %$_ } = values %$_ for @_;
$defaults{-thirdparty} = {
%$thirdparty, map{ %{ $_->{-thirdparty} // {} } } @_
};
}
# fill in config values
sub configure {
my ($template) = @_;
while ($template =~ /@(\w+)@/) {
my $key = $1;
return (undef, "unknown configuration key '$key', keys: ".join(',', keys %config))
unless exists $config{$key};
$template =~ s/@\Q$key\E@/$config{$key}/;
}
return $template;
}
# generate files
sub generate {
my ($dest, $src) = @_;
print dots("Generating $dest");
open my $srcfile, '<', $src or hardfail($!);
open my $destfile, '>', $dest or hardfail($!);
while (<$srcfile>) {
my ($line, $error) = configure($_);
hardfail($error)
unless defined $line;
# in-between slashes in makefiles need to be backslashes on Windows
$line =~ s/(\w|\.)\/(\w|\.|\*)/$1\\$2/g
if $dest =~ /Makefile/ && $config{sh} eq 'cmd';
print $destfile $line;
}
close $srcfile;
close $destfile;
print "OK\n";
}
# some dots
sub dots {
my $message = shift;
my $length = shift || 55;
return "$message ". '.' x ($length - length $message) . ' ';
}
# fail but continue
sub softfail {
my ($msg) = @_;
$failed = 1;
print "FAIL\n";
warn " $msg\n";
}
# fail and don't continue
sub hardfail {
softfail(@_);
die "\nConfiguration PANIC. A Makefile could not be generated.\n";
}
__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]
[--big-endian]
=head1 OPTIONS
=over 4
=item -?
=item --help
Show this help information.
=item --debug
=item --no-debug
Toggle debugging flags during compile and link. If C<--optimize> is not
explicitly set, debug defaults to on, and optimize defaults to off.
=item --optimize
=item --no-optimize
Toggle 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
=item --no-instrument
Toggle extra instrumentation flags during compile and link; for example,
turns on Address Sanitizer when compiling with C<clang>. Defaults to off.
=item --os <os>
If not explicitly set, the operating system is provided by the Perl
runtime. In case of unknown operating systems, a GNU userland is assumed.
=item --shell <shell>
Currently supported shells are C<posix> and C<win32>.
=item --toolchain <toolchain>
Currently supported toolchains are C<gnu> and C<msvc>.
=item --compiler <compiler>
Currently supported compilers are C<gcc>, C<clang> and C<cl>.
=item --cc <cc>
Explicitly set the compiler without affecting other configuration
options.
=item --ld <ld>
Explicitly set the linker without affecting other configuration
options.
=item --make <make>
Explicitly set the make tool without affecting other configuration
options.
=item --build <build-triple> --host <host-triple>
Set up cross-compilation.
=item --big-endian
Set byte order of host system in case of cross compilation. With native
builds, the byte order is auto-detected.
=back