/
MakeFromJSON.pm6
158 lines (125 loc) · 4.38 KB
/
MakeFromJSON.pm6
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
use v6;
unit class Distribution::Builder::MakeFromJSON;
use System::Query;
has $.meta;
has $!collapsed-meta;
method collapsed-meta() {
$!collapsed-meta //= $!meta<build> ?? system-collapse($!meta<build>) !! {};
}
method can-build(--> Bool) {
self.collapsed-meta();
return True;
CATCH {
default {
note $_;
return False;
}
}
}
method build() {
my $dest-dir = '.';
my $workdir = '.';
my $meta = $.collapsed-meta;
my $src-dir = ($meta<src-dir> || '.').IO;
configure($meta, $src-dir, $dest-dir) if $meta<configure-bin>:exists;
process-makefile-template($meta, $src-dir, $dest-dir) if $src-dir.child('Makefile.in').e;
mkdir "$workdir/resources" unless "$workdir/resources".IO.e;
mkdir "$workdir/resources/libraries" unless "$workdir/resources/libraries".IO.e;
temp $*CWD = $src-dir;
run 'make'; # check for gmake here
}
sub configure($meta, $src-dir, $dest-dir) {
temp $*CWD = $src-dir;
run $meta<configure-bin>;
}
sub process-makefile-template($meta, $src-dir, $dest-dir) {
my %vars = backend-values();
%vars<DESTDIR> = $*CWD;
my %makefile-variables = $meta<makefile-variables> if $meta<makefile-variables>;
for %makefile-variables.values -> $value is rw {
next unless $value ~~ Map;
if $value<resource>:exists and $value<resource>.starts-with('libraries/')
{
my $path = $value<resource>.substr(10); # strip off libraries/
$value = $dest-dir.IO.child('resources').child('libraries')
.child($*VM.platform-library-name($path.IO)).Str;
}
if $value<platform-library-name>:exists {
$value = $*VM.platform-library-name($value<platform-library-name>.IO);
}
if $value<run>:exists {
$value = chomp run(|$value<run>, :out).out.slurp;
}
if $value<env>:exists {
$value = %*ENV{$value<env>};
}
}
%vars.push: %makefile-variables;
my $makefile = $src-dir.child('Makefile.in').slurp;
for %vars.kv -> $k, $v {
$makefile ~~ s:g/\%$k\%/$v/;
}
$src-dir.child('Makefile').spurt: $makefile;
}
sub backend-values() {
my %vars;
# Code lifted from LibraryMake
if $*VM.name eq 'moar' {
%vars<O> = $*VM.config<obj>;
my $so = $*VM.config<dll>;
$so ~~ s/^.*\%s//;
%vars<SO> = $so;
%vars<CC> = $*VM.config<cc>;
%vars<CCSHARED> = $*VM.config<ccshared>;
%vars<CCOUT> = $*VM.config<ccout>;
%vars<CCFLAGS> = $*VM.config<cflags>;
%vars<LD> = $*VM.config<ld>;
%vars<LDSHARED> = $*VM.config<ldshared>;
%vars<LDFLAGS> = $*VM.config<ldflags>;
%vars<LIBS> = $*VM.config<ldlibs>;
%vars<LDOUT> = $*VM.config<ldout>;
my $ldusr = $*VM.config<ldusr>;
$ldusr ~~ s/\%s//;
%vars<LDUSR> = $ldusr;
%vars<MAKE> = $*VM.config<make>;
%vars<EXE> = $*VM.config<exe>;
}
elsif $*VM.name eq 'jvm' {
%vars<O> = $*VM.config<nativecall.o>;
%vars<SO> = '.' ~ $*VM.config<nativecall.so>;
%vars<CC> = $*VM.config<nativecall.cc>;
%vars<CCSHARED> = $*VM.config<nativecall.ccdlflags>;
%vars<CCOUT> = "-o"; # this looks wrong?
%vars<CCFLAGS> = $*VM.config<nativecall.ccflags>;
%vars<LD> = $*VM.config<nativecall.ld>;
%vars<LDSHARED> = $*VM.config<nativecall.lddlflags>;
%vars<LDFLAGS> = $*VM.config<nativecall.ldflags>;
%vars<LIBS> = $*VM.config<nativecall.perllibs>;
%vars<LDOUT> = $*VM.config<nativecall.ldout>;
%vars<MAKE> = 'make';
%vars<LDUSR> = '-l';
# this is copied from moar - probably wrong
#die "Don't know how to get platform independent '-l' (LDUSR) on JVM";
#my $ldusr = $*VM.config<ldusr>;
#$ldusr ~~ s/\%s//;
#%vars<LDUSR> = $ldusr;
%vars<EXE> = $*VM.config<exe>;
}
else {
die "Unknown VM; don't know how to build";
}
return %vars;
}
=begin pod
=head1 NAME
Distribution::Builder::MakeFromJSON - blah blah blah
=head1 SYNOPSIS
use Distribution::Builder::MakeFromJSON;
=head1 DESCRIPTION
Distribution::Builder::MakeFromJSON is ...
=head1 AUTHOR
Stefan Seifert <nine@detonation.org>
=head1 COPYRIGHT AND LICENSE
Copyright 2017 Stefan Seifert
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.
=end pod