-
Notifications
You must be signed in to change notification settings - Fork 130
/
NQP.pm
316 lines (269 loc) · 9.1 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
## Please see file perltidy.ERR
package NQP::Config::NQP;
use v5.10.1;
use strict;
use warnings;
use Cwd;
use IPC::Cmd qw<run can_run>;
use NQP::Config qw<cmp_rev slurp 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 = can_run( $self->moar_config->{moar} ) || can_run('moar');
if ( $moar_exe || $have_gen_moar ) {
say "===WARNING!===\n",
" No backends specified on the command line.\n",
" Using 'moar' because we found '$moar_exe' executable."
unless $have_gen_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_misc {
my $self = shift;
my $config = $self->{config};
if ( $self->active_backend('moar') ) {
( $config->{moar_want} ) =
split( ' ',
slurp( $self->build_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 );
$config->{nqplibdir} = $self->nfp(
(
$self->opt('libdir')
? $self->opt('libdir') . "/nqp"
: '$(NQP_LANG_DIR)/lib'
),
no_quote => 1,
);
}
sub configure_moar_backend {
my $self = shift;
my $imoar = $self->{impls}{moar};
$self->gen_moar;
$self->{config}{moar} = $imoar->{config}{moar};
}
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;
my $stage0 = $config->{moar_stage0};
$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 >= 8 ) {
$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.8 (got $got)";
}
$self->sorry(@errors) if @errors;
print "Using $got\n";
$j_config->{'runner'} = $self->batch_file('nqp');
}
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->{moar};
my $sdkroot = $self->cfg('sdkroot');
my $prefix = $self->cfg('prefix');
my $moar_prefix =
$sdkroot ? File::Spec->catdir( $sdkroot, $prefix ) : $prefix;
my $moar_exe = $self->opt('with-moar')
|| File::Spec->catfile( $moar_prefix, 'bin', "moar" . $self->cfg('exe') );
return $self->backend_config( 'moar',
{ 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_config;
my $moar_want = $config->{moar_want};
my $moar_have;
my @errors;
my $prefix = $config->{prefix};
my $gen_moar = $options->{'gen-moar'};
my @opts = @{ $options->{'moar-option'} || [] };
push @opts, "--optimize";
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 ( $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 = $gen_moar;
}
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 = $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 \nConfigure.pl and make install again\n";
$try_generate = $gen_moar;
}
$self->sorry(@errors) if @errors && !$gen_moar;
if ( defined $try_generate ) {
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>)"`;
}
# Command line options not to be included into configure_opts macro output
sub ignorable_opt {
my $self = shift;
my $opt = shift;
return $opt =~ /^
(?:
gen-
| (?:
help
| no-clean
| ignore-errors
| make-install
| expand
| out
| backends
)
$
)
/x;
}
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;
}
1;
# vim: ft=perl