-
Notifications
You must be signed in to change notification settings - Fork 131
/
NQP.pm
553 lines (479 loc) · 16.7 KB
/
NQP.pm
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
package NQP::Config::NQP;
use v5.10.1;
use strict;
use warnings;
use Cwd;
use IPC::Cmd qw<run>;
use NQP::Config qw<slurp read_config_from_command cmp_rev system_or_die
run_or_die>;
use base qw<NQP::Config>;
sub configure_backends {
my $self = shift;
my $options = $self->{options};
my $passed_backends = $self->opt('backends');
if ($passed_backends) {
my @use_backend = $self->parse_backends($passed_backends);
for my $be (@use_backend) {
unless ( $self->known_backend($be) ) {
die "Unknown backend: '$be'; Known backends: "
. join( ', ', $self->known_backends ) . "\n";
}
$self->use_backend($be);
}
}
else {
my $have_gen_moar = defined $options->{'gen-moar'};
my $moar_exe = $self->is_executable( $self->moar_config->{moar} );
if ( $moar_exe || $have_gen_moar ) {
$self->note(
"WARNING!",
"No backends specified on the command line.\n",
"Using 'moar' because we found '$moar_exe' executable."
) unless $have_gen_moar || $options->{'with-moar'};
$self->use_backend('moar');
}
else {
$self->sorry( "No backends specified on the command line.\n"
. "Please use --backends or --gen-moar" );
}
}
if ( $self->active_backend('js') and !$self->active_backend('moar') ) {
$self->sorry(
"When building the js backend you must also build moar\n"
. "Please build with --backends=moar,js\n" );
}
}
sub configure_refine_vars {
my $self = shift;
my $config = $self->{config};
unless ( $self->cfg('prefix') ) {
my $moar_conf = $self->moar_config;
my $moar_prefix = $moar_conf->{moar_prefix};
my $moar = $moar_conf->{moar};
if ($moar_prefix) {
$self->set( prefix => $moar_prefix );
$self->note(
'ATTENTION',
"No --prefix supplied, ",
"building and installing to '$moar_prefix'\n",
"Based on found executable $moar"
);
}
}
$self->SUPER::configure_refine_vars(@_);
$config->{nqp_home} = $self->nfp(
File::Spec->rel2abs(
$config->{nqp_home}
|| File::Spec->catdir( $config->{'prefix'}, 'share', 'nqp' )
)
);
$config->{static_nqp_home} = $config->{relocatable} eq 'reloc' ? '' : $config->{nqp_home};
}
sub configure_misc {
my $self = shift;
$self->SUPER::configure_misc(@_);
my $config = $self->{config};
if ( $self->active_backend('moar') ) {
( $config->{moar_want} ) =
split(
' ',
slurp(
$self->template_file_path( 'MOAR_REVISION', required => 1 )
)
);
}
$config->{moar_stage0} = $self->nfp( "src/vm/moar/stage0", no_quote => 1 );
$config->{jvm_stage0} = $self->nfp( "src/vm/jvm/stage0", no_quote => 1 );
}
sub configure_moar_backend {
my $self = shift;
my $imoar = $self->{impls}{moar};
my $config = $self->{config};
$self->gen_moar;
my $moar_config = $self->moar_config;
$self->{config}{moar} = $imoar->{config}{moar};
$self->sorry( "No moar executable found in " . $self->cfg('prefix') )
unless $self->is_executable( $self->{config}{moar} );
if ( $config->{relocatable} eq 'reloc' ) {
$imoar->{config}{static_nqp_home} = '';
$imoar->{config}{static_nqp_home_define} = '';
}
else {
my $qchar = $config->{quote};
$imoar->{config}{static_nqp_home} = $config->{static_nqp_home};
$imoar->{config}{static_nqp_home_define} =
'-DSTATIC_NQP_HOME='
. $qchar
. $self->c_escape_string( $imoar->{config}{static_nqp_home} )
. $qchar;
}
# Strip rpath from ldflags so we can set it differently ourself.
$imoar->{config}{ldflags} = $moar_config->{'moar::ldflags'};
$imoar->{config}{ldflags} =~ s/\Q$moar_config->{'moar::ldrpath'}\E ?//;
$imoar->{config}{ldflags} =~
s/\Q$moar_config->{'moar::ldrpath_relocatable'}\E ?//;
if ( $self->cfg('prefix') ne '/usr' ) {
$imoar->{config}{ldflags} .= ' '
. (
$config->{relocatable} eq 'reloc'
? $moar_config->{'moar::ldrpath_relocatable'}
: $moar_config->{'moar::ldrpath'}
);
}
$imoar->{config}{ldlibs} = $moar_config->{'moar::ldlibs'};
$imoar->{config}{mingw_unicode} = '';
my @c_runner_libs;
if ( $self->is_win ) {
if ( File::Spec->catdir( $config->{prefix}, 'bin' ) ne
$moar_config->{'moar::libdir'} )
{
$config->{m_install} = "\t"
. q<$(CP) @nfpq(@moar::libdir@/@moar::moar@)@ @nfpq($(DESTDIR)$(PREFIX)/bin)@>;
}
if ( $moar_config->{'moar::os'} eq 'mingw32' ) {
$imoar->{config}{mingw_unicode} = '-municode';
}
push @c_runner_libs,
sprintf( $moar_config->{'moar::ldusr'}, 'Shlwapi' );
}
$imoar->{config}{c_runner_libs} = join( " ", @c_runner_libs );
$imoar->{config}{moar_lib} = sprintf(
(
$moar_config->{'moar::ldimp'}
? $moar_config->{'moar::ldimp'}
: $moar_config->{'moar::ldusr'}
),
$moar_config->{'moar::name'}
);
}
sub configure_js_backend {
my $self = shift;
my $config = $self->{config};
my $options = $self->{options};
my $ijs = $self->{impls}{js};
my $js_config = $ijs->{config};
$ijs->{ok} = 1;
$js_config->{link} = $options->{link};
my $node = $self->probe_node;
unless ($node) {
$self->sorry("No node.js found. Please, install it first.");
$ijs->{ok} = 0;
}
if ( $node eq 'nodejs' ) {
$self->sorry( 'You have a broken node.js.'
. ' Please install node.js as node instead of nodejs.' );
$ijs->{ok} = 0;
}
$js_config->{node} = $node;
my $node_version = run_or_die( [ $node, q<--version> ] );
my ( $major, $minor, $path ) = $node_version =~ /v(\d+)\.(\d+)\.(\d+)/;
unless ( $major > 10 || $major == 10 && $minor >= 10 ) {
chomp($node_version);
$self->sorry("Need at least node.js v10.10.0 (got $node_version)");
$ijs->{ok} = 0;
}
$self->backend_config(
'js',
js_build_dir =>
$self->nfp( "$config->{base_dir}/gen/js", no_quote => 1 ),
js_blib => "node_modules",
);
}
sub configure_jvm_backend {
my $self = shift;
my $ijvm = $self->{impls}{jvm};
my $j_config = $ijvm->{config};
my @errors;
my $got;
my @jvm_info = `java -showversion 2>&1`;
my $jvm_found = 0;
my $jvm_ok = 0;
for (@jvm_info) {
print "got: $_";
if (/(?:java|jdk) version "(\d+)(?:\.(\d+))?/) {
$jvm_found = 1;
if ( $1 > 1 || $1 == 1 && $2 >= 9 ) {
$ijvm->{ok} = $jvm_ok = 1;
}
$got = $_;
last;
}
}
if ( !$jvm_found ) {
push @errors, "No JVM (java executable) in path; cannot continue";
}
elsif ( !$jvm_ok ) {
push @errors, "Need at least JVM 1.9 (got $got)";
}
$self->sorry(@errors) if @errors;
print "Using $got\n";
$j_config->{'runner'} = $self->batch_file('nqp');
}
sub configure_jars {
my $self = shift;
$self->SUPER::configure_jars(
{
asm => [qw<3rdparty asm asm-4.1.jar>],
'asm-tree' => [qw<3rdparty asm asm-tree-4.1.jar>],
jline => [qw<3rdparty jline jline-1.0.jar>],
jna => [qw<3rdparty jna jna-4.5.0.jar>],
'jna-platform' => [qw<3rdparty jna jna-platform-4.5.0.jar>],
lz4 => [qw<3rdparty lz4 lz4-java-1.8.0.jar>],
}
);
}
sub post_active_backends {
my $self = shift;
my @errors;
for my $b ( $self->active_backends ) {
push @errors, $self->backend_errors($b);
}
$self->sorry(@errors) if @errors;
}
sub moar_config {
my $self = shift;
my $moar_config = $self->backend_config('moar');
return $moar_config if $moar_config && keys %{$moar_config} > 2;
my $prefix = $self->cfg('prefix');
my $moar_prefix;
my $moar_exe;
if ( $self->opt('with-moar') ) {
$moar_exe = File::Spec->rel2abs( $self->opt('with-moar') );
}
else {
if ($prefix) {
my $sdkroot = $self->cfg('sdkroot');
$moar_prefix =
$sdkroot ? File::Spec->catdir( $sdkroot, $prefix ) : $prefix;
$moar_exe ||= File::Spec->catfile( $moar_prefix, 'bin',
"moar" . $self->cfg('exe') );
}
#elsif ( !defined $self->opt('sysroot') ) { # Pick from PATH
# $moar_exe = can_run('moar');
#}
}
if ( !$moar_prefix && $moar_exe ) {
my ( $vol, $dir, undef ) = File::Spec->splitpath($moar_exe);
$moar_prefix = File::Spec->catpath( $vol,
File::Spec->catdir( $dir, File::Spec->updir ) );
}
my %c;
if ( $self->is_executable( $self->nfp($moar_exe) ) ) {
%c =
read_config_from_command( '"'
. $self->nfp($moar_exe) . '"'
. ' --libpath='
. $self->nfp('src/vm/moar/stage0') . ' '
. $self->nfp('src/vm/moar/stage0/nqp.moarvm')
. ' --bootstrap --show-config' );
%c = map { rindex( $_, 'moar::', 0 ) == 0 ? ( $_ => $c{$_} ) : () }
keys %c;
}
return $self->backend_config( 'moar',
{ %c, moar => $moar_exe, moar_prefix => $moar_prefix, } );
}
sub gen_moar {
my $self = shift;
return unless $self->active_backend('moar');
my $options = $self->{options};
my $config = $self->config;
my $moar_want = $config->{moar_want};
my $moar_have;
my @errors;
my $prefix = $config->{prefix};
my $gen_moar = $options->{'gen-moar'};
my $force_rebuild = $options->{'force-rebuild'};
my $has_gen_moar = defined $gen_moar;
my @opts = @{ $options->{'moar-option'} || [] };
push @opts, "--optimize";
push @opts, '--relocatable' if $options->{relocatable};
push @opts,
'--git-cache-dir=' . File::Spec->rel2abs($options->{'git-cache-dir'})
if $options->{'git-cache-dir'};
my $startdir = $config->{base_dir};
my $git_protocol = $options->{'git-protocol'} || 'https';
my $try_generate;
my $moar_exe = $self->moar_config->{moar};
my $moar_version_output = "";
if ( $force_rebuild && !$has_gen_moar ) {
$self->note( "WARNING",
"Command line option --force-rebuild is ineffective without --gen-moar"
);
}
if ( $self->is_executable($moar_exe) ) {
$moar_version_output = run_or_die( [ $moar_exe, '--version' ] );
if ($moar_version_output) {
$moar_have = $moar_version_output =~ /version (\S+)/ ? $1 : undef;
}
}
else {
$try_generate = $has_gen_moar;
}
unless ($force_rebuild) {
my $moar_ok =
$moar_have && cmp_rev( $moar_have, $moar_want, "MoarVM" ) >= 0;
if ($moar_ok) {
$self->msg(
"Found $moar_exe version $moar_have, which is new enough.\n");
}
elsif ($moar_have) {
push @errors,
"Found $moar_exe version $moar_have, which is too old. "
. "Wanted at least $moar_want\n";
$try_generate = $has_gen_moar unless $options->{'ignore-errors'};
}
elsif ($moar_version_output
&& $moar_version_output =~ /This is MoarVM version/i )
{
push @errors,
"Found a MoarVM binary "
. " but was not able to get its version number.\n"
. "If running `git describe` inside the MoarVM repository does not work,\n"
. "you need to make sure to checkout tags of the repository and run \n"
. "Configure.pl and make install again\n";
$try_generate = $has_gen_moar;
}
}
if (@errors) {
if ($try_generate) {
$self->note( "ATTENTION!", $_ ) foreach @errors;
}
else {
$self->sorry(@errors);
}
}
if ( $try_generate || ( $has_gen_moar && $force_rebuild ) ) {
# Don't expect any particular commit in MoarVM/ if the repo is already
# checked out.
my $expected_spec =
-d File::Spec->catdir( $self->cfg('base_dir'), 'MoarVM', '.git' )
? undef
: $moar_want;
my $moar_repo =
$self->git_checkout( 'moar', 'MoarVM', $gen_moar || $moar_want );
unless ( 0 <= cmp_rev( $moar_repo, $moar_want, "moar" )
and !$options->{'ignore-errors'} )
{
# But, now you come to me, and you say: "NQP, give me the MoarVM."
# But you don't ask with respect.
die "You asked me to build MoarVM"
. ", but $moar_repo is not new enough to satisfy version "
. "$moar_want\n";
}
my $cwd = cwd;
chdir( $self->base_path('MoarVM') ) or die $!;
print "\nConfiguring and building MoarVM ...\n";
my @cmd = ( $^X, "Configure.pl", @opts, qq{--prefix=$prefix},
'--make-install' );
print "@cmd\n";
system_or_die(@cmd);
chdir($cwd);
}
$self->{impls}{moar}{ok} = 1;
# XXX What'd be the point of overriding the autodetected make with
# autodetected by MoarVM Configure?
# my $libpath = $self->base_path("src/vm/moar/stage0");
# my $nqp_moarvm = $self->base_path("$libpath/nqp.moarvm");
# my $make =
#`$moar_path --libpath="$libpath" "$nqp_moarvm" -e "print(nqp::backendconfig()<make>)"`;
}
sub probe_node {
my $self = shift;
# Debian ships a 'node' binary that is related to amateur radio.
# the javascript thingy is called 'nodejs' there
for my $binary (qw/node nodejs /) {
my $version_str;
my $ok = run(
command => [ $binary, '-v' ],
buffer => \$version_str
);
if ( $ok && $version_str =~ /^v\d/ ) {
return $binary;
}
}
return;
}
package NQP::Macros::NQP;
use strict;
use warnings;
sub _m_for_stages {
my $self = shift;
my $text = shift;
my $out = "";
my $bpfx = $self->cfg->cfg('backend_prefix');
foreach my $stage ( 1 .. 2 ) {
my $prev_stage = $stage - 1;
my $lc_stage = "stage$stage";
my $lc_prev = "stage$prev_stage";
my %config = (
stage => $stage,
ucstage => uc($lc_stage),
lcstage => $lc_stage,
prev_stage => $prev_stage,
ucprev_stage => uc($lc_prev),
lcprev_stage => $lc_prev,
stage_dir => '$(' . uc($bpfx) . "_STAGE${stage}_DIR)",
prev_stage_dir => '$(' . uc($bpfx) . "_STAGE${prev_stage}_DIR)",
);
my $stage_ctx = {
stage => $stage,
configs => [ \%config ],
};
my $s = $self->cfg->push_ctx($stage_ctx);
$out .= $self->_expand($text);
}
return $out;
}
sub _m_stage_gencat {
my $self = shift;
my $text = shift;
return $self->expand(<<TPL);
$text
\t\@echo(+++ Generating\t\$\@)@
\t\$(NOECHO)\@bpm(\@ucstage\@_GEN_CAT)\@ \@prereqs\@ > \$\@
TPL
}
sub _m_stage_precomp {
my $self = shift;
my $text = shift;
$self->set_in_receipe( 'nqp_setting', 'NQPCORE' );
$self->set_in_receipe( 'setting_path_param',
' --setting-path=@stage_dir@' );
$self->set_in_receipe( 'module_path_param', ' --module-path=@stage_dir@' );
return $self->expand(<<TPL);
$text
\t\@echo(+++ Compiling\t\$\@)@
\t\$(NOECHO)\@bpm(STAGE\@prev_stage\@_NQP)\@\@expand(\@setting_path_param\@\@module_path_param\@)\@ --no-regex-lib --target=\@btarget\@ --setting=\@nqp_setting\@ \@bpm(PRECOMP_\@ucstage\@_FLAGS)\@ --output=\$\@ \@prereqs\@
TPL
}
# Set nqp_setting variable for precomp receipe
sub _m_setting {
my $self = shift;
my $text = shift;
$self->set_in_receipe( 'nqp_setting', $text );
if ( $self->expand($text) eq 'NULL' ) {
$self->set_in_receipe( 'setting_path_param', '' );
}
return "";
}
# Set module_path_param variable for precomp receipe
sub _m_no_module_path {
my $self = shift;
$self->set_in_receipe( 'module_path_param', '' );
return "";
}
NQP::Macros->register_macro(
'stage_gencat', \&_m_stage_gencat,
in_receipe => 1,
preexapnd => 0,
);
NQP::Macros->register_macro(
'stage_precomp', \&_m_stage_precomp,
in_receipe => 1,
preexpand => 0,
);
NQP::Macros->register_macro( 'for_stages', \&_m_for_stages, preexapnd => 0 );
NQP::Macros->register_macro( 'setting', \&_m_setting, preexapnd => 1 );
NQP::Macros->register_macro( 'no_module_path', \&_m_no_module_path );
1;
# vim: ft=perl