/
build.p6
executable file
·241 lines (205 loc) · 9.74 KB
/
build.p6
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
#!/usr/bin/env perl6
# Copyright © 2016
# Aleks-Daniel Jakimenko-Aleksejev <alex.jakimenko@gmail.com>
# Daniel Green <ddgreen@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This script will build something for all commits that it can find
use File::Temp;
use File::Directory::Tree;
enum Project <Rakudo-Moar Rakudo-JVM Rakudo-JS MoarVM>;
my \PROJECT = do given @*ARGS[0] // ‘’ {
when /:i ^‘moarvm’$ / { MoarVM }
default { Rakudo-Moar }
}
my \RAKUDOISH = PROJECT == Rakudo-Moar | Rakudo-JVM | Rakudo-JS;
my \DIR-BASE = PROJECT.lc;
my \PARALLEL-COUNT = 1;
my \TAGS-SINCE = ‘2014-01-01’; # to build all tags
my \COMMIT-RANGE = ‘2015.07^..HEAD’; # to build recent commits
my \ALL-SINCE = ‘2017-01-01’; # to catch branches that are flapping in the breeze
my \EVERYTHING-RANGE = ‘2014.01^..HEAD’; # to build everything, but in historical order
my \WORKING-DIRECTORY = ‘.’; # TODO not supported yet
my \RAKUDO-NQP-ORIGIN = ‘https://github.com/perl6/nqp.git’;
my \RAKUDO-NQP-LATEST = “/tmp/whateverable/rakudo-triple-nqp-repo”;
my \RAKUDO-MOAR-ORIGIN = ‘https://github.com/MoarVM/MoarVM.git’;
my \RAKUDO-MOAR-LATEST = “/tmp/whateverable/rakudo-triple-moar-repo”;
my \REPO-ORIGIN = RAKUDOISH
?? ‘https://github.com/rakudo/rakudo.git’
!! ‘https://github.com/MoarVM/MoarVM.git’;
my \REPO-LATEST = “/tmp/whateverable/{DIR-BASE}-repo”;
# ↑ yes, separate cloned repo for every backend to prevent several
# instances of this script fighting with each other
my \REPO-CURRENT = “{WORKING-DIRECTORY}/data/{DIR-BASE}”.IO.absolute;
my \ARCHIVES-LOCATION = “{WORKING-DIRECTORY}/data/builds/{DIR-BASE}”.IO.absolute;
my \BUILDS-LOCATION = “/tmp/whateverable/{DIR-BASE}”;
my \BUILD-LOCK = “{BUILDS-LOCATION}/build-lock”;
my \GIT-REFERENCE = “{WORKING-DIRECTORY}/data”.IO.absolute;
mkdir BUILDS-LOCATION;
mkdir ARCHIVES-LOCATION;
# TODO IO::Handle.lock ? run ‘flock’? P5 modules?
exit 0 unless run ‘mkdir’, :err(Nil), ‘--’, BUILD-LOCK; # only one instance running
my $locked = True;
END BUILD-LOCK.IO.rmdir if $locked;
sub pull-or-clone($repo-origin, $repo-path) {
if $repo-path.IO ~~ :d {
my $old-dir = $*CWD;
run :cwd($repo-path), ‘git’, ‘pull’;
} else {
exit unless run ‘git’, ‘clone’, ‘--’, $repo-origin, $repo-path;
}
}
pull-or-clone REPO-ORIGIN, REPO-LATEST;
if RAKUDOISH {
pull-or-clone RAKUDO-NQP-ORIGIN, RAKUDO-NQP-LATEST;
pull-or-clone RAKUDO-MOAR-ORIGIN, RAKUDO-MOAR-LATEST;
}
if REPO-CURRENT.IO !~~ :d {
run ‘git’, ‘clone’, ‘--’, REPO-LATEST, REPO-CURRENT;
}
my $channel = Channel.new;
my @git-log = ‘git’, ‘log’, ‘-z’, ‘--pretty=%H’;
my @args-tags = |@git-log, ‘--tags’, ‘--no-walk’, ‘--since’, TAGS-SINCE;
my @args-latest = |@git-log, COMMIT-RANGE;
my @args-recent = |@git-log, ‘--all’, ‘--since’, ALL-SINCE;
my @args-old = |@git-log, ‘--reverse’, EVERYTHING-RANGE;
my %commits;
# Normal Rakudo commits
for @args-tags, @args-latest, @args-recent, @args-old -> @_ {
for run(:cwd(REPO-LATEST), :out, |@_).out.split(0.chr, :skip-empty) {
next if %commits{$_}:exists;
%commits{$_}++;
$channel.send: $_
}
}
sub get-build-revision($repo, $on-commit, $file) {
run(:cwd($repo), :out, ‘git’, ‘show’,
“{$on-commit}:tools/build/$file”).out.slurp-rest.trim
}
# Rakudo-NQP-Moar triples (for bumps)
#`「「「
if PROJECT == Rakudo-Moar {
my @args-bumps = ‘git’, ‘log’, ‘-z’, ‘--pretty=%x00%H’,
‘--follow’, ‘--reverse’,
EVERYTHING-RANGE, ‘tools/build/NQP_REVISION’;
for run(:cwd(REPO-LATEST), :out, |@args-bumps)
.out.split(0.chr, :skip-empty).rotor(2) -> ($rakudo-sha, $diff) {
#my $nqp-old = get-build-revision REPO-LATEST, “$rakudo-sha^”, ‘NQP_REVISION’;
#my $nqp-new = get-build-revision REPO-LATEST, “$rakudo-sha”, ‘NQP_REVISION’;
#say “$rakudo-sha”;
for run(:cwd(RAKUDO-NQP-LATEST), :out, |@git-log, “$nqp-old..$nqp-new”)
.out.split(0.chr, :skip-empty) -> $nqp-sha {
my $moar-sha = get-build-revision RAKUDO-NQP-LATEST, “$nqp-sha”, ‘MOAR_REVISION’;
# TODO shas are not shas
say “|- $nqp-sha - $moar-sha”;
}
for run(:cwd(RAKUDO-NQP-LATEST), :out, |@git-log, ‘--follow’, ‘--reverse’,
“$nqp-old..$nqp-new”, ‘tools/build/MOAR_REVISION’)
.out.split(0.chr, :skip-empty) -> $nqp-sha {
my $moar-old = get-build-revision RAKUDO-NQP-LATEST, “$nqp-sha^”, ‘MOAR_REVISION’;
my $moar-new = get-build-revision RAKUDO-NQP-LATEST, “$nqp-sha”, ‘MOAR_REVISION’;
for run(:cwd(RAKUDO-MOAR-LATEST), :out, |@git-log, “$moar-old..$moar-new”)
.out.split(0.chr, :skip-empty) -> $moar-sha {
say “ |- $moar-sha”;
}
}
}
}
# 」」」
await (for ^PARALLEL-COUNT { # TODO rewrite when .race starts working in rakudo
start loop {
my $commit = $channel.poll;
last unless $commit;
try { process-commit($commit) }
}
});
# update repo so that bots know about latest commits
run ‘git’, ‘--git-dir’, “{REPO-CURRENT}/.git”, ‘--work-tree’, REPO-CURRENT, ‘pull’, ‘--tags’, REPO-LATEST;
sub process-commit($commit) {
return if “{ARCHIVES-LOCATION}/$commit.zst”.IO ~~ :e; # already exists
return if “{ARCHIVES-LOCATION}/$commit”.IO ~~ :e; # already exists (long-term storage)
return if $++ ≥ 10; # refuse to build too many commits at once
my ($temp-folder,) = tempdir, :!unlink;
my $build-path = “{BUILDS-LOCATION}/$commit”.IO.absolute;
my $log-path = $build-path;
my $archive-path = “{ARCHIVES-LOCATION}/$commit.zst”.IO.absolute;
# ⚡ clone
run ‘git’, ‘clone’, ‘-q’, ‘--’, REPO-LATEST, $temp-folder;
# ⚡ checkout to $commit
my @git-temp = ‘git’, ‘--git-dir’, “$temp-folder/.git”, ‘--work-tree’, $temp-folder;
run |@git-temp, ‘reset’, ‘-q’, ‘--hard’, $commit;
# No :merge for log files because RT #125756 RT #128594
my $config-ok;
mkdir $build-path;
{
# ⚡ configure
my $old-dir = $*CWD;
LEAVE chdir $old-dir;
chdir $temp-folder;
say “»»»»» $commit: configure”;
my $configure-log-fh = open :w, “$log-path/configure.log”;
my $configure-err-fh = open :w, “$log-path/configure.err”;
my @args = do given PROJECT {
when MoarVM {
‘perl’, ‘--’, ‘Configure.pl’, “--prefix=$build-path”,
‘--debug=3’
}
when Rakudo-Moar {
‘perl’, ‘--’, ‘Configure.pl’, “--prefix=$build-path”,
‘--gen-moar’, ‘--gen-nqp’, ‘--backends=moar’
}
}
if PROJECT == Rakudo-Moar and run ‘grep’, ‘-m1’, ‘-q’, ‘--’,
‘--git-reference’, ‘Configure.pl’ {
@args.push: “--git-reference={GIT-REFERENCE}”
}
$config-ok = run :out($configure-log-fh), :err($configure-err-fh), |@args;
$configure-log-fh.close;
$configure-err-fh.close;
say “»»»»» Cannot configure $commit” unless $config-ok;
}
my $make-ok;
if $config-ok {
# ⚡ make
say “»»»»» $commit: make”;
my $make-log-fh = open :w, “$log-path/make.log”;
my $make-err-fh = open :w, “$log-path/make.err”;
my @args = do given PROJECT {
when MoarVM { ‘make’, ‘-j’, ‘7’, ‘-C’, $temp-folder }
when Rakudo-Moar { ‘make’, ‘-C’, $temp-folder }
}
$make-ok = run :out($make-log-fh), :err($make-err-fh), |@args;
$make-log-fh.close;
$make-err-fh.close;
say “»»»»» Cannot make $commit” unless $make-ok;
}
if $make-ok {
# ⚡ make install
say “»»»»» $commit: make install”;
my $install-log-fh = open :w, “$log-path/make-install.log”;
my $install-err-fh = open :w, “$log-path/make-install.err”;
my $install-ok = run(:out($install-log-fh), :err($install-err-fh),
‘make’, ‘-C’, $temp-folder, ‘install’);
$install-log-fh.close;
$install-err-fh.close;
say “»»»»» Cannot install $commit” unless $install-ok;
}
# ⚡ compress
# No matter what we got, compress it
say “»»»»» $commit: compressing”;
my $proc = run(:out, :bin, ‘tar’, ‘cf’, ‘-’, ‘--absolute-names’, ‘--remove-files’, ‘--’, $build-path);
run(:in($proc.out), :bin, ‘zstd’, ‘-c’, ‘-19’, ‘-q’, ‘-o’, $archive-path);
rmtree $temp-folder;
}
# vim: expandtab shiftwidth=4 ft=perl6