forked from openSUSE/obs-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbuild
executable file
·330 lines (288 loc) · 10.9 KB
/
pbuild
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
#!/usr/bin/perl
################################################################
#
# Copyright (c) 2021 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or 3 as
# published by the Free Software Foundation.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################
BEGIN {
if (!$::ENV{'BUILD_DIR'} && $0 ne '-' && $0 ne '-e' && -e $0 && ! -e '/etc/build.conf') {
use Cwd ();
my $p = Cwd::abs_path($0);
$::ENV{'BUILD_DIR'} = $p if $p =~ s/\/[^\/]+$// && $p ne '/usr/lib/build' && -d "$p/PBuild";
}
unshift @INC, ($::ENV{'BUILD_DIR'} && ! -e '/etc/build.conf' ? $::ENV{'BUILD_DIR'} : '/usr/lib/build');
}
use Data::Dumper;
use POSIX;
use Cwd ();
use Build;
use PBuild::Source;
use PBuild::Recipe;
use PBuild::RepoMgr;
use PBuild::LocalRepo;
use PBuild::Multibuild;
use PBuild::Link;
use PBuild::Checker;
use PBuild::Options;
use PBuild::BuildConfig;
use PBuild::Result;
use PBuild::Download;
use PBuild::Preset;
# parse options
my ($opts, @dirs) = PBuild::Options::parse_options(@ARGV);
PBuild::Options::usage(0) if $opts->{'help'};
die("Usage: pbuild [options] [dir]\n") if @dirs > 1;
my $dir = @dirs ? $dirs[0] : '.';
$dir = Cwd::abs_path($dir) if $dir !~ /^\//;
# set defaults
my $libbuild = $INC[0];
$opts->{'libbuild'} = $libbuild;
$opts->{'root'} ||= '/var/tmp/build-root';
$opts->{'root'} = Cwd::abs_path($opts->{'root'}) if $opts->{'root'} !~ /^\//;
$opts->{'configdir'} ||= "$libbuild/configs";
if (!$opts->{'hostarch'}) {
$opts->{'hostarch'} = (POSIX::uname())[4];
die("cannot determine hostarch\n") unless $opts->{'hostarch'};
$opts->{'hostarch'} = 'armv6hl' if $opts->{'hostarch'} eq 'armv6l';
$opts->{'hostarch'} = 'armv7hl' if $opts->{'hostarch'} eq 'armv7l';
}
$opts->{'arch'} ||= $opts->{'hostarch'};
$opts->{'buildjobs'} = 1 unless $opts->{'buildjobs'};
$opts->{'buildjobs'} = 32 if $opts->{'buildjobs'} > 32;
# read presets
my $preset = PBuild::Preset::read_presets($dir, $opts->{'preset'});
PBuild::Preset::apply_preset($opts, $preset) if $preset;
my $reponame = $opts->{'reponame'};
if (!$reponame && $opts->{'dist'}) {
$reponame = $opts->{'dist'}->[-1];
$reponame =~ s/.*\///;
$reponame =~ s/\.conf$//;
}
my $myarch = $opts->{'arch'};
my $builddir = $reponame ? "$dir/_build.$reponame.$myarch" : "$dir/_build.$myarch";
if ($opts->{'result-code'} || $opts->{'result-pkg'}) {
PBuild::Result::print_result($opts, $builddir);
exit;
}
my @baseconfigs;
my $distcnt = 0;
my @baseobsrepos;
for my $dist (@{$opts->{'dist'} || []}) {
$distcnt++;
if ($dist =~ /^https?:\/\//) {
my ($config) = PBuild::Download::fetch($dist);
push @baseconfigs, $config;
} elsif ($dist =~ /^obs:\//) {
my $islast = $distcnt == @{$opts->{'dist'} || []} ? 1 : 0;
my ($obsconfigs, $obsrepos) = PBuild::OBS::fetch_all_configs($dist, $opts, $islast);
push @baseconfigs, reverse @$obsconfigs;
push @baseobsrepos, @$obsrepos;
} elsif ($dist =~ /^empty:/) {
next;
} elsif (-e "$dir/_configs/$opts->{'dist'}.conf") {
push @baseconfigs, PBuild::Util::readstr("$dir/_configs/$dist.conf");
} else {
my $baseconfigfile = Build::find_config_file($dist, $opts->{'configdir'});
push @baseconfigs, PBuild::Util::readstr($baseconfigfile);
}
}
# set repos from obs, does not work well with "mixed" configs
push @{$opts->{'repo'}}, @baseobsrepos if @baseobsrepos && !$opts->{'repo'};
my $localconfig = -s "$dir/_config" ? PBuild::Util::readstr("$dir/_config") : '';
$localconfig = "\n%define _repository $reponame\n\n$localconfig" if $localconfig ne '';
my $buildconfig = PBuild::BuildConfig::combineconfigs(@baseconfigs, $localconfig);
if (!$buildconfig) {
print("Please specify a distribution!\n\n");
PBuild::Options::usage(1);
}
my $bconf = Build::read_config($myarch, [ split("\n", $buildconfig) ]);
# default to repo/registry from config if not set
push @{$opts->{'repo'}}, 'config:' unless @{$opts->{'repo'} || []};
push @{$opts->{'registry'}}, 'config:' unless @{$opts->{'registry'} || []};
# substitute config: with values from config
for (splice(@{$opts->{'repo'}})) {
push @{$opts->{'repo'}}, $_;
splice(@{$opts->{'repo'}}, -1, 1, @{$bconf->{'repourl'}}) if $_ eq 'config:';
}
for (splice(@{$opts->{'registry'}})) {
push @{$opts->{'registry'}}, $_;
splice(@{$opts->{'registry'}}, -1, 1, @{$bconf->{'registryurl'}}) if $_ eq 'config:';
}
# expand the zypp:// repo
PBuild::RemoteRepo::expand_zypp_repo($opts) if $opts->{'repo'};
print "searching for packages\n";
my @pkgs = PBuild::Source::find_packages($dir);
die("no packages found in '$dir'\n") unless @pkgs;
my $npkgs = @pkgs;
print "found $npkgs packages\n";
print "getting package information\n";
my %pkgsrc;
for my $pkg (@pkgs) {
my $files = PBuild::Source::list_package("$dir/$pkg");
$pkgsrc{$pkg} = {
'pkg' => $pkg,
'dir' => "$dir/$pkg",
'files' => $files,
'srcmd5' => PBuild::Source::calc_srcmd5($files),
};
}
# handle local links and multibuild packages
print "expanding package links\n";
PBuild::Link::expand_links(\%pkgsrc);
print "expanding multibuild packages\n";
PBuild::Multibuild::expand_multibuilds(\%pkgsrc);
@pkgs = sort keys %pkgsrc;
# handle onlybuild/excludebuild from the build config
if (exists $bconf->{'buildflags:excludebuild'}) {
my %excludebuild;
/^excludebuild:(.*)$/s && ($excludebuild{$1} = 1) for @{$bconf->{'buildflags'} || []};
if (%excludebuild) {
for my $pkg (@pkgs) {
my $p = $pkgsrc{$pkg};
my $releasename = $p->{'releasename'} || $pkg;
$p->{'error'} = "excluded:project config excludebuild list" if $excludebuild{$pkg} || $excludebuild{$releasename};
}
}
}
if (exists $bconf->{'buildflags:onlybuild'}) {
my %onlybuild;
/^onlybuild:(.*)$/s && ($onlybuild{$1} = 1) for @{$bconf->{'buildflags'} || []};
if (%onlybuild) {
for my $pkg (@pkgs) {
my $p = $pkgsrc{$pkg};
my $releasename = $p->{'releasename'} || $pkg;
$p->{'error'} = "excluded:project config onlybuild list" unless $onlybuild{$pkg} || $onlybuild{$releasename};
}
}
}
# parse all recipes in the packages to get dependency information
print "parsing recipe files\n";
my %containertags;
my $buildtype = $bconf->{'type'} || '';
$buildtype = 'spec' if !$buildtype || $buildtype eq 'UNDEFINED';
for my $pkg (@pkgs) {
my $p = $pkgsrc{$pkg};
PBuild::Recipe::parse($bconf, $p, $buildtype, $myarch);
if ($p->{'buildtype'} && ($p->{'buildtype'} eq 'kiwi' || $p->{'buildtype'} eq 'docker') && !$p->{'error'}) {
my @containerdeps = grep {/^container:/} @{$p->{'dep'} || []};
next unless @containerdeps;
$containertags{substr($_, 10)} = 1 for @containerdeps;
}
}
#FIXME
for my $pkg (@pkgs) {
my $p = $pkgsrc{$pkg};
$p->{'useforbuildenabled'} = 1;
}
# delete obsolete entries from builddir
PBuild::LocalRepo::cleanup_builddir($builddir, \%pkgsrc);
# setup the repositories and registries
my $repomgr = PBuild::RepoMgr::create();
my @repos;
print "fetching local repo metadata\n";
push @repos, $repomgr->addlocalrepo($bconf, $myarch, $builddir, \%pkgsrc);
print "fetching remote repo metadata\n";
for my $repourl (@{$opts->{'repo'}}) {
push @repos, $repomgr->addremoterepo($bconf, $myarch, $builddir, $repourl, $buildtype, $opts);
}
if (@{$opts->{'registry'} || []} && %containertags) {
print "fetching remote registry metadata\n";
my @containertags = sort keys %containertags;
for my $registry (@{$opts->{'registry'} || []}) {
push @repos, $repomgr->addremoteregistry($bconf, $myarch, $builddir, $registry, \@containertags);
}
}
# load lastcheck cache
my %lastcheck;
if (-s "$builddir/.pbuild/_lastcheck") {
my $oldlastcheck = PBuild::Util::retrieve("$builddir/.pbuild/_lastcheck", 1) || {};
for my $pkg (@pkgs) {
my $old = $oldlastcheck->{$pkg};
$lastcheck{$pkg} = $old if $old && length($old) > 96;
}
}
# setup builders
my @builders;
for my $no (1..$opts->{'buildjobs'}) {
my $broot = $opts->{'root'};
if ($opts->{'buildjobs'} > 1) {
$broot .= '/%I' if $broot !~ /%I/;
$broot =~ s/%I/$no/g;
}
push @builders, {
'name' => $no,
'root' => $broot,
'idx' => scalar(@builders),
'nbuilders' => $opts->{'buildjobs'},
};
}
my $ctx;
my $runs = 0;
# the big loop: while there is something to do
while (1) {
# create and setup checker
if (!$ctx) {
$ctx = PBuild::Checker::create($bconf, $myarch, $buildtype, \%pkgsrc, $builddir, $opts, $repomgr);
print "preparing package pool\n" unless $runs;
$ctx->prepare(\@repos);
print "expanding dependencies\n" unless $runs;
$ctx->pkgexpand(@pkgs);
print "sorting packages\n" unless $runs;
@pkgs = $ctx->pkgsort(@pkgs);
}
$runs++;
$ctx->{'buildconfig'} = $buildconfig;
$ctx->{'lastcheck'} = \%lastcheck;
# check status of all packages
my $result = $ctx->pkgcheck(\@builders, @pkgs);
# update on-disk data
PBuild::Util::mkdir_p("$builddir/.pbuild");
PBuild::Util::store("$builddir/.pbuild/._result.$$", "$builddir/.pbuild/_result", $result);
PBuild::Util::store("$builddir/.pbuild/._lastcheck.$$", "$builddir/.pbuild/_lastcheck", \%lastcheck);
# get list of building jobs
my @building = map {$_->{'job'}} grep {$_->{'job'}} @builders;
last unless @building;
# wait for one job to finish
my $job = PBuild::Job::waitjob(@building);
for (@builders) {
delete $_->{'job'} if $_->{'job'} && $_->{'job'} == $job;
}
# process finished job
my ($code, $buildresult) = PBuild::Job::finishjob($job);
my $p = $job->{'pdata'};
my $duration = $job->{'endtime'} - $job->{'starttime'};
$duration = sprintf("%d:%02d", int($duration / 60), $duration % 60);
my $bid = ($job->{'nbuilders'} || 1) > 1 ? "$job->{'name'}: " : '';
print "${bid}finished $p->{'pkg'}/$p->{'recipe'} after ${duration}s: $code\n";
my $jobhist = PBuild::BuildResult::makejobhist($p, $code, $job->{'readytime'}, $job->{'starttime'}, $job->{'endtime'}, $job->{'reason'}, $job->{'hostarch'});
PBuild::BuildResult::addjobhist($builddir, $jobhist);
# integrate build artifacts and extra files
my $bininfo = PBuild::BuildResult::integrate_job($builddir, $job, $code, $buildresult);
# if the build was successful, update artifact information and the local repo
if ($bininfo) {
PBuild::LocalRepo::update_gbininfo($builddir, $p->{'pkg'}, $bininfo);
if ($p->{'useforbuildenabled'}) {
# update with new local bin information
$repomgr->updatelocalrepo($bconf, $myarch, $builddir, \%pkgsrc);
# we also need a new checker
undef $ctx;
}
}
}
# say goodbye
print "\npbuild is done:\n";
exit PBuild::Result::print_result($opts, $builddir);