-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildtool
executable file
·1388 lines (1244 loc) · 53.7 KB
/
buildtool
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
#
# buildtool -- Product Build Management Tool
#
# Copyright (C) 2000-2007, Michael Jennings
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies of the Software, its documentation and marketing & publicity
# materials, and acknowledgment shall be given in the documentation, materials
# and software packages that this Software was used.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# $Id: buildtool,v 1.216 2011/02/25 03:14:57 mej Exp $
#
use strict;
# Include the Perl Modules we need
use Cwd '&abs_path';
use POSIX;
use Getopt::Long;
use File::Find;
use Mezzanine::Util;
use Mezzanine::Config;
use Mezzanine::PkgVars;
use Mezzanine::Src;
use Mezzanine::Pkg;
use Mezzanine::Build;
use Mezzanine::Prod;
use Mezzanine::Instroot;
my $GLOBAL_LOG = 0; # Overall log file for product build
my $verbosity = 2; # Logging verbosity
my $global_user; # Repository userid
my $global_tree; # Repository to use
my $global_tag; # Tag to get packages with
my $buildtree_layout = "mej"; # The layout style to use for the build tree.
my $prod = "";
my $failure;
my @skipped_pkgs;
my @failed_pkgs;
my @completed_pkgs;
my $num_processes;
my $num_built = 0;
my @instroot_pool;
my $scm;
# RPM/SRPM cache data.
my $rpm_cache;
my ($CFG_RPMCACHE_NAME, $CFG_RPMCACHE_PATH, $CFG_RPMCACHE_MTIME, $CFG_RPMCACHE_SIBLINGS) = (0, 1, 2, 3);
# Configuration data.
my $config;
my @config_vars = ("DEBUG", "VERBOSITY", "TARGET", "BUILDTREE_LAYOUT",
"CLEAN", "LOGFILE", "HINTS", "DEP_INSTALLER",
"PARALLELIZE", "LOCATIONS", "BUILDUSER",
"INSTROOT", "INSTROOT_INIT", "INSTROOT_COPY",
"INSTROOT_RESET", "INSTROOT_SOURCE_RSYNC",
"BUILDROOT", "TMPDIR", "MAKE", "MFLAGS", "CFLAGS",
"PATH", "RETRY", "REBUILD", "PRODUCTS", "LOGDIR",
"ALLOW_EPOCH", "LAMEBRAIN");
# Print usage information
sub
print_usage_info
{
my ($leader, $underbar);
print "\n";
$leader = "$PROGNAME $VERSION Usage Information";
$underbar = $leader;
$underbar =~ s/./-/g;
print "$leader\n$underbar\n";
print "\n";
print " Syntax: buildtool [ options ]\n";
print "\n";
print " -h --help Show this usage information\n";
print " -d --debug Turn on debugging\n";
print " -v --version Show version and copyright\n";
print " -q --quiet Be somewhat quiet. Simply gives basic progress info\n";
print " -Q --really-quiet --silent Be very quiet. Only errors are reported\n";
print " -D --dir <directory> Specify the repository to use (overrides all product files)\n";
print " -l --log <logfile> Specify a log file to send informational output to\n";
print " -P --parallel [expr] Parallelize the build process based on the number of CPU's\n";
print " -t --target <arch> Tell rpm to build for a particular target architecture\n";
print " -C --cflags <flags> Specify the \$CFLAGS variable to use for building\n";
print " -U --repuser <userid> Specify a userid for the repository (overrides product files)\n";
print " -T --tag <tag> Specify a tag to use (overrides all product files)\n";
print " -L --location <location_spec> Specify where packages should be placed (as in product file)\n";
print " -s --srcdir <dir> (Re)build all packages under <dir>\n";
print " -i --instroot --jail <dir> Specify chroot jail to build in (or copy from, if parallel)\n";
print " -H --hints <file_or_dir> Specify location of pre-build hints or hint files\n";
print " -u --user <userid> Build packages as <userid> instead of current user\n";
print " -b --buildtree <style> Use the specified layout for the build tree\n";
print " --di --dep-installer <prog> Specify the mechanism used to install build dependencies\n";
print " --is --instroot-src <path> Path to clean source for chroot jail (replaces next 3 opts)\n";
print " --ii --instroot-init <cmd> Command used to initialize chroot jail\n";
print " --ir --instroot-reset <cmd> Command used to reset chroot jail\n";
print " --ic --instroot-copy <cmd> Command used to copy chroot jail\n";
print " --builddir <path> Use <path> as the root of the build tree\n";
print " --nocache Do not scan the cache (use with care)\n";
print " --retry Retry packages which have previously failed to build\n";
print " --rebuild Rebuild previously successful packages for verification\n";
print " --clean Clean up RPM and buildroot directories when done\n";
print " --allow-epoch Allow Epoch in spec file (prepend \"no\" to disallow)\n";
print " --lamebrain <spec> Enable one or more lamebrain work-arounds\n";
print " --savecfg Preserve current settings for future use\n";
print "\n";
exit(MEZZANINE_SUCCESS);
}
# Normal print (i.e., don't print this in -q or -Q mode)
sub nprintf {printf @_ if ($GLOBAL_LOG || $verbosity >= 2);}
sub nprint {print @_ if ($GLOBAL_LOG || $verbosity >= 2);}
# Quiet print (i.e., print in -q mode but not in -Q mode)
sub qprintf {printf @_ if ($GLOBAL_LOG || $verbosity >= 1);}
sub qprint {print @_ if ($GLOBAL_LOG || $verbosity >= 1);}
# What to do if a package completes successfully
sub
complete_package
{
my $pkg = $_[0];
push @completed_pkgs, $pkg;
nprint "Package build for $pkg completed successfully. (${\(&get_timestamp())})\n";
}
# What to do if a package didn't need to be built.
sub
skipped_package
{
my $pkg = $_[0];
push @skipped_pkgs, $pkg;
nprint "Package build for $pkg was not needed. (${\(&get_timestamp())})\n";
}
# What to do if a package doesn't build on this architecture
sub
arch_incompat_package
{
my $pkg = $_[0];
nprint "Package build for $pkg skipped due to architecture mismatch. (${\(&get_timestamp())})\n";
}
# What to do if a package fails
sub
fail_package
{
my ($pkg, $msg) = @_;
push @failed_pkgs, $pkg;
if ($msg) {
$msg =~ s/\.+$//;
$failure->{$pkg}{"MESSAGE"} = $msg;
eprint "Package \"$pkg\" failed: $msg.\n";
} else {
eprint "Package \"$pkg\" failed with an unknown error.\n";
}
}
# Remove failed packages and packages we don't build from the list
sub
update_package_list
{
my $parray = shift;
my @p = @{$parray};
@{$parray} = ();
foreach my $pkg (@p) {
next if ($pkgs->{$pkg}{"TYPE"} eq "image");
if (!grep($_ eq $pkg, @failed_pkgs)) {
push @{$parray}, $pkg;
}
}
return (@{$parray});
}
# Check to see if we actually need to build this package.
sub
is_build_needed
{
my ($pkg, $pkgfile, $srpm_dir);
my @stat1;
my @srpm_matches;
dprint &print_args(@_);
if ($config->get("REBUILD")) {
dprint "Rebuild forced by configuration.\n";
return 1;
} else {
#foreach my $key ($config->keys()) {
# dprintf("Got config key %s with value %s.\n", $key, $config->get($key));
#}
}
$pkg = &pkgvar_name();
if (!$pkg) {
dprint "Doh! is_build_needed() called with no package name set. I suck!\n";
return -1;
}
$pkgfile = &pkgvar_filename();
if (!$pkgfile) {
dprint "Doh! is_build_needed() called for $pkg with no filename set. I suck!\n";
return -1;
}
# If the file/module does not exist, we definitely need to build it.
if (! -e $pkgfile) {
dprint "Doh! $pkgfile doesn't exist. It must be built.\n";
} elsif (-s $pkgfile) {
@stat1 = stat($pkgfile);
dprintf("$pkgfile is a file. Using its timestamp data: %s\n",
POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime($stat1[9])));
} else {
my $which_one;
$which_one = &newest_file($pkgfile);
if ($which_one && -e $which_one) {
@stat1 = stat($which_one);
} else {
@stat1 = stat($pkgfile);
}
}
# If there is no SRPMS directory, then there are surely no SRPMS to be checked.
$srpm_dir = &pkgvar_topdir() . "/SRPMS";
if (! -d $srpm_dir) {
dprint "SRPM directory $srpm_dir does not exist.\n";
$srpm_dir = &pkgvar_topdir();
dprint "Listing: " . `/bin/ls -Fla $srpm_dir` . "\n\n----\n";
return 1;
}
# For each SRPM that matches the package name, see if it's newer than the directory.
# If so, we do not need to build that package.
dprint "Looking for files matching $pkg.(no)src.rpm in $srpm_dir.\n";
if ($pkgs->{$pkg}{"TYPE"} =~ /^s?rpm$/) {
@srpm_matches = &grepdir(sub {&basename($_) =~ /^\Q$pkg\E\.(no)?src\.rpm$/}, $srpm_dir);
} else {
@srpm_matches = &grepdir(sub {&basename($_) =~ /^\Q$pkg\E-[^-]+-[^-]+\.(no)?src\.rpm$/}, $srpm_dir);
}
dprintf("Got %d matches.\n", scalar(@srpm_matches));
foreach my $srpm (@srpm_matches) {
my @stat2;
dprint "Checking $srpm...\n";
@stat2 = stat($srpm);
if ($stat2[9] >= $stat1[9]) {
# SRPM is newer than (or the same age as) the file/module. Skip it.
dprint "$srpm is newer than $pkgfile; no need to build $pkg.\n";
&pkgvar_filename($srpm);
return 0;
} else {
dprintf("$srpm is older than $pkgfile: %s < %s.\n",
POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime($stat2[9])),
POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime($stat1[9])));
}
}
# Doh, gotta build this one.
dprint "No matching SRPM's found which are newer than $pkgfile. Must build $pkg.\n";
return 1;
}
# Scan the binary RPM directories to see which binaries belong to which SRPM's.
sub
scan_rpm_dirs
{
my $topdir;
my @contents = ();
my %siblings;
nprint "Updating state information....\n";
$topdir = &pkgvar_topdir();
$rpm_cache = Mezzanine::Config->new("$PROGNAME/rpm_cache.cdf");
# Pre-scan all the binary RPM's for future use. We need to know what SRPM
# each binary came from, because some (lame) packages change the base name.
if (-d $topdir) {
if ($buildtree_layout eq "orc") {
find({ "wanted" => sub { ! -d $_ && $_ =~ /\.rpm$/ && push @contents, $_ }, "no_chdir" => 1 },
$topdir);
} else {
find({ "wanted" => sub { ! -d $_ && $_ =~ /\.rpm$/ && push @contents, $_ }, "no_chdir" => 1 },
"$topdir/RPMS", "$topdir/SRPMS");
}
}
foreach my $rpm (sort(@contents)) {
my ($aref, $rpmfile, $name, $path, $mtime);
my $srpm;
my @info;
$rpmfile = &basename($rpm);
$path = &dirname($rpm);
$aref = $rpm_cache->get($rpmfile);
if ($OPTION{"nocache"}) {
dprint "Ignoring any cache info for $rpm.\n";
} elsif (ref($aref) eq "ARRAY") {
my @statinfo;
# Valid array ref. Let's make sure the times match.
@statinfo = stat($rpm);
if ($aref->[$CFG_RPMCACHE_MTIME]) {
$mtime = $aref->[$CFG_RPMCACHE_MTIME];
} else {
$mtime = 0;
}
if ($statinfo[9] > $mtime) {
# Our cache info is outdated. We'll have to update it.
dprint "Out-of-date cache info for $rpm\n";
} elsif ($statinfo[9] < $mtime) {
dprint "Cache data newer than $rpm?!?!\n";
} else {
dprint "Cache data matches, skipping $rpm.\n";
@{$siblings{$rpmfile}} = split(' ', $aref->[$CFG_RPMCACHE_SIBLINGS]);
next;
}
} else {
dprint "Cache lookup for $rpm failed.\n";
}
# If we get here, we need to update our cache info.
# Stat the file
@info = stat($rpm);
$mtime = $info[9];
# Get name and architecture
@info = &parse_rpm_name($rpmfile);
$name = $info[0];
if ($info[3] eq "src" || $info[3] eq "nosrc") {
# It's an SRPM. Do we need to do anything here?
} else {
# It's a binary RPM.
my @tmp;
$srpm = `rpm -qp $rpm --queryformat \"%{SOURCERPM}\"`;
if (! $srpm) {
eprint "Unable to find SRPM for $rpm. This could be bad.\n";
} else {
$siblings{$rpmfile} = [ $srpm ];
}
if (!defined($siblings{$srpm})) {
$siblings{$srpm} = [ $rpmfile ];
} else {
push @{$siblings{$srpm}}, $rpmfile;
}
}
@info = ($name, $path, $mtime, "");
$rpm_cache->set($rpmfile, \@info);
}
# Now that we've got all the data, store it back into the config object.
foreach my $rpm (sort(keys(%siblings))) {
my $aref = $rpm_cache->get($rpm);
dprint "Recording cache info for $rpm.\n";
if (! $aref || ref($aref) ne "ARRAY") {
eprint "EEEEP! Caching mechanism fell apart for $rpm!\n";
next;
} else {
dprint "Recording cache information for $rpm.\n";
}
$aref->[$CFG_RPMCACHE_SIBLINGS] = join(' ', @{$siblings{$rpm}});
# This next part shouldn't be needed, but for paranoia...
$rpm_cache->set($rpm, $aref);
}
$rpm_cache->save();
}
# Build a single package from source (if needed) and place the resulting files where they go.
sub
build_single_package
{
my $pkg = shift;
my ($pkgfile, $err, $msg, $outfiles);
# Binaries or not, we need to know the full path and name of the source package.
dprintf("Building $pkg in %s.\n", &pkgvar_topdir());
&pkgvar_name($pkg);
if (! ($pkgfile = &pkgvar_filename($pkgs->{$pkg}{"MODULE"}, $pkgs->{$pkg}{"FILENAME"}))) {
&fail_package($pkg, "I don't know what file/module to build from!");
return MEZZANINE_NO_SOURCES;
}
# If there are binaries, no point in trying to build source.
if ($pkgs->{$pkg}{"BINS"}) {
$outfiles = join(' ', &pkgvar_filename(), @{$pkgs->{$pkg}{"BINS"}});
} elsif ($pkgs->{$pkg}{"TYPE"} eq "rpm") {
$outfiles = &pkgvar_filename();
} elsif (&is_build_needed()) {
# Build the source
if ($pkgs->{$pkg}{"INSTROOT"}) {
$ENV{"HOME"} = &pkgvar_buildroot();
&pkgvar_instroot($pkgs->{$pkg}{"INSTROOT"});
if (! -e $pkgs->{$pkg}{"INSTROOT"} && $pkgs->{$pkg}{"INSTROOT_INIT"}) {
my @output;
nprint "Initializing chroot jail, please wait.\n";
@output = &run_cmd($pkgs->{$pkg}{"INSTROOT_INIT"}, $pkgs->{$pkg}{"INSTROOT"}, "instroot-init: ");
if ($output[0] != MEZZANINE_SUCCESS) {
wprint "Initialization of install root failed.\n";
}
} elsif (-e $pkgs->{$pkg}{"INSTROOT"} && $pkgs->{$pkg}{"INSTROOT_RESET"}) {
my @output;
nprint "Resetting chroot jail, please wait.\n";
if (&pkgvar_get("lamebrain") =~ /\bNEEDPROC\b/) {
dprint "NEEDPROC LAMEBRAIN workaround active: excluding /proc from instroot-reset.\n";
if ($pkgs->{$pkg}{"INSTROOT_RESET"} !~ /\b--exclude=\/proc\b/) {
$pkgs->{$pkg}{"INSTROOT_RESET"} .= " --exclude=/proc";
}
}
@output = &run_cmd($pkgs->{$pkg}{"INSTROOT_RESET"}, $pkgs->{$pkg}{"INSTROOT"}, "instroot-reset: ");
if ($output[0] != MEZZANINE_SUCCESS) {
wprint "Reset of install root failed.\n";
}
}
if ($pkgs->{$pkg}{"BUILDUSER"}) {
&pkgvar_set("builduser", $pkgs->{$pkg}{"BUILDUSER"});
&file_owner($pkgs->{$pkg}{"BUILDUSER"}, "", $pkgs->{$pkg}{"INSTROOT"});
dprintf("Building as %s (%lu:%lu)\n", &pkgvar_get("builduser"), $mz_uid, $mz_gid);
}
} else {
&pkgvar_instroot("");
if ($pkgs->{$pkg}{"BUILDUSER"}) {
&pkgvar_set("builduser", $pkgs->{$pkg}{"BUILDUSER"});
&file_owner($pkgs->{$pkg}{"BUILDUSER"});
dprintf("Building as %s (%lu:%lu)\n", &pkgvar_get("builduser"), $mz_uid, $mz_gid);
}
}
dprint "Build tree layout style: $buildtree_layout\n";
($err, $msg, $outfiles) = &build_package();
if ($err != MEZZANINE_SUCCESS) {
&fail_package($pkg, $msg);
return $err;
} else {
$num_built++;
}
} else {
my $aref;
my $srpm;
# No build needed. Just get the list of output files.
$srpm = &basename(&pkgvar_filename());
$aref = $rpm_cache->get($srpm);
if (ref($aref) eq "ARRAY") {
my @tmp = split(' ', $aref->[$CFG_RPMCACHE_SIBLINGS]);
for (my $i = 0; $i < scalar(@tmp); $i++) {
my $aref2 = $rpm_cache->get($tmp[$i]);
if (ref($aref2) ne "ARRAY") {
eprint "Bugger. No data for $tmp[$i]. Not good.\n";
&fail_package($pkg, "Cache has gone to pot");
}
$tmp[$i] = $aref2->[$CFG_RPMCACHE_PATH] . '/' . $tmp[$i];
}
$outfiles = &pkgvar_filename() . ' ' . join(' ', @tmp);
dprint "Got cached output files: $outfiles\n";
} else {
eprint "Oh dear. No data for $srpm. This is bad news.\n";
&fail_package($pkg, "Big huge messy caching foul-up occured");
}
}
$err = &place_package_files($pkg, $outfiles, $pkgs->{$pkg}{"LOCATIONS"});
return $err if ($err != MEZZANINE_SUCCESS);
&complete_package($pkg);
return MEZZANINE_SUCCESS;
}
# Spawn a child buildtool
sub
build_package_forked
{
my ($pkg, $logfile) = @_;
my $pid;
do {
if ($pid = fork()) {
# Parent -- return PID
nprint "Spawned child process $pid to build $pkg (log file is $logfile)\n";
return $pid;
} elsif (defined $pid) {
my ($err, $buildroot);
# Child -- reset state as needed and jump to build process
if (-e $logfile) {
#dprint "Log file $logfile exists.\n";
if (! -e "$logfile.save") {
#dprint "Renaming to $logfile.save.\n";
&move_files($logfile, "$logfile.save");
} else {
#dprint "Removing $logfile.\n";
&nuke_tree($logfile);
}
} elsif (-e "$logfile.broken") {
if (! $OPTION{"retry"}) {
my ($pkgfile, $which_one);
my $failed = 0;
# It was broken before. See if it's been updated since then.
$pkgfile = &pkgvar_filename($pkgs->{$pkg}{"MODULE"}, $pkgs->{$pkg}{"FILENAME"});
if ($pkgfile) {
my (@stat1, @stat2);
$which_one = &newest_file($pkgfile);
if ($which_one && -e $which_one) {
@stat1 = stat($which_one);
} else {
@stat1 = stat($pkgfile);
}
@stat2 = stat("$logfile.broken");
if ($stat1[9] <= $stat2[9]) {
# The "broken" log is newer than the module/package. Skip it.
dprint "No updates to $pkg detected since last build failure.\n";
$failed = 1;
} else {
dprint "$pkg has been updated since previous build failure.\n";
}
} else {
wprint "Unable to find package source for $pkg. Cannot check for updates.\n";
}
if ($failed) {
dprint "Package $pkg failed previously. Aborting.\n";
exit MEZZANINE_PACKAGE_FAILED;
}
}
dprint "Retrying build for failed package $pkg.\n";
}
close $GLOBAL_LOG if ($GLOBAL_LOG);
open(STDOUT, ">/dev/null");
open(STDIN, "</dev/null");
if (!open(LOGFILE, ">$logfile")) {
eprint "Unable to open $logfile -- $!\n";
} else {
$GLOBAL_LOG = \*LOGFILE;
#system("chattr +S $logfile"); # Try to set sync on the log file, fail silently
open(STDERR, ">&LOGFILE");
select LOGFILE; $| = 1;
chown($mz_uid, $mz_gid, $logfile);
}
qprintf("Package build of $pkg started. (%s)\n", &get_timestamp());
$num_built = 0;
$buildroot = &pkgvar_buildroot();
if ($buildroot) {
&pkgvar_buildroot("$buildroot/$pkg.$$");
} else {
&pkgvar_buildroot(&create_temp_space($pkg, "dironly"));
}
if ($pkgs->{$pkg}{"INSTROOT"}) {
# This package build is chrooted.
&pkgvar_instroot($pkgs->{$pkg}{"INSTROOT"});
$pkgs->{$pkg}{"INSTROOT_RESET"} = "true";
} else {
&pkgvar_instroot("");
}
$err = &build_single_package($pkg);
&cleanup_build_tree();
if (&pkgvar_instroot() && -d &pkgvar_instroot()) {
### Not here. We use the pool now.
### &nuke_tree(&pkgvar_instroot());
}
if (&pkgvar_buildroot() && -d &pkgvar_buildroot()) {
&nuke_tree(&pkgvar_buildroot());
}
if ($err == MEZZANINE_SUCCESS) {
if (! $num_built) {
$err = MEZZANINE_BUILD_UNNEEDED;
}
if (-e "$logfile.broken") {
wprint "Breakage log file $logfile.broken exists -- Package fixed itself!?\n";
&nuke_tree("$logfile.broken");
&nuke_tree("$logfile.save");
} elsif ((! $num_built) && (-e "$logfile.save")) {
dprint "Log file $logfile.save for actual build exists -- Moving back into place.\n";
&move_files($logfile, "$logfile.latest");
&move_files("$logfile.save", $logfile);
}
} elsif ($err == MEZZANINE_ARCH_MISMATCH) {
if (-e "$logfile.broken") {
&nuke_tree("$logfile.broken");
}
&move_files($logfile, "$logfile.latest");
&move_files("$logfile.save", $logfile);
} else {
dprint "Renaming $logfile to $logfile.broken.\n";
&nuke_tree("$logfile.save");
&nuke_tree("$logfile.broken");
&move_files($logfile, "$logfile.broken");
}
close(LOGFILE) if ($GLOBAL_LOG);
exit ($err);
} elsif ($! !~ /No more processes/ && $! !~ /Resource temporarily unavailable/) {
&fatal_error("fork() failed -- $!\n");
}
# Temporary failure
dprint "Failed temporarily -- $!\n";
sleep 5;
} while (1);
}
# Download packages from the repository
sub
download_packages
{
my @packages = @_;
my ($line, $err, $msg, $package_scm);
nprint "Downloading packages, please wait..." if (! &debug_get());
foreach my $pkg (@packages) {
my ($files, $tag);
$err = 0;
$files = &pkgvar_filename($pkgs->{$pkg}{"MODULE"}, $pkgs->{$pkg}{"FILENAME"});
if (defined($pkgs->{$pkg}{"BINS"})) {
$files = join(' ', $files, @{$pkgs->{$pkg}{"BINS"}});
}
&pkgvar_filename($files);
# Set the proper REPOSITORY and tag for the checkout.
if ($global_tree) {
$package_scm = Mezzanine::SCM->new($scm->scmobj_propget("type"));
$package_scm->scmobj_propset("repository", $scm->scmobj_propget("repository"));
} elsif ($pkgs->{$pkg}{"REPOSITORY"}) {
$package_scm = Mezzanine::SCM->auto_detect($pkgs->{$pkg}{"REPOSITORY"});
if ($package_scm) {
my @tmp;
@tmp = $package_scm->parse_repository_path($pkgs->{$pkg}{"REPOSITORY"});
if (scalar(@tmp) && defined($tmp[0])) {
$package_scm->compose_repository_path(@tmp);
} else {
$package_scm->scmobj_propset("repository", $pkgs->{$pkg}{"REPOSITORY"});
}
} elsif ($scm && $scm->scmobj_propget("repository")) {
$package_scm->scmobj_propset("repository", $scm->scmobj_propget("repository"));
} else {
# FIXME: What to do??
}
} else {
# FIXME: What to do??
}
if ($global_user) {
my @tmp;
@tmp = $package_scm->parse_repository_path();
$tmp[1] = $global_user;
$package_scm->compose_repository_path(@tmp);
}
if ($global_tag) {
$package_scm->scmobj_propset("source_tag", $global_tag);
} elsif ($pkgs->{$pkg}{"TAG"}) {
$package_scm->scmobj_propset("source_tag", $pkgs->{$pkg}{"TAG"});
}
nprint "$pkg..." if (! &debug_get());
($err, $msg) = &fetch_package($package_scm);
if ($err != MEZZANINE_SUCCESS && $err != MEZZANINE_DUPLICATE) {
&fail_package($pkg, $msg);
next;
}
}
nprint "done.\n\n" if (! &debug_get());
}
sub
place_package_files
{
my ($pkg, $outfiles, $locations) = @_;
my $instroot;
my @outfiles;
my @contents;
# If we didn't find any, something is very wrong.
if (! $outfiles) {
&fail_package($pkg, "No output packages were found");
return MEZZANINE_PACKAGE_FAILED;
}
if (! $locations) {
wprint "No locations specified for $pkg.\n";
$locations = "/.*/=.";
}
dprint "Output files for $pkg: $outfiles\n";
$instroot = $pkgs->{$pkg}{"INSTROOT"};
if ($instroot) {
foreach my $file (split(' ', $outfiles)) {
if ((-e "$instroot$file") && (&abs_path($file) ne &abs_path("$instroot$file"))) {
&move_files("$instroot$file", $file);
}
}
}
# Copy each package into the appropriate place
foreach my $file (split(' ', $outfiles)) {
foreach my $loc (split(",", $locations)) {
my ($regex, $stop, $dest);
# Format is: /regexp/.path where . is some delimiter character that
# tells us whether to check other locations or stop once we match
# (':' to continue looking for matches, or '=' to stop if a match is found).
dprint "Testing location \"$loc\"\n";
if ($loc !~ m/^\/([^\/]+)\/(.)(\S+)?$/) {
eprint "Location specifier \"$loc\" is invalid.\n";
next;
}
($regex, $stop, $dest) = ($1, $2, $3);
if ($stop eq "!") {
# A negative match test. If we get a match, don't accept it.
next if ($file =~ $regex);
} else {
# No match. Try next location.
next if ($file !~ $regex);
}
dprint "Match found for $file: $stop$regex ($dest).\n";
if ($dest) {
# If the destination does not contain a filename, add the filename portion of
# $file to the directory path in $dest. The destination could be used to rename
# a file, however; that's why this check is in place.
if (substr($dest, -3, 3) ne substr($file, -3, 3)) {
my $tmp;
($tmp = $file) =~ s/^.*\/([^\/]+)$/$1/;
$dest = "$dest/$tmp";
}
# If it exists, delete it.
if (-e $dest) {
dprint "$dest exists; removing.\n";
&nuke_tree($dest);
}
# Then link it
dprint "ln -f $file $dest\n";
if (!link($file, $dest)) {
if (exists($!{"EXDEV"}) && ($!{"EXDEV"})) {
# Cross-device link attempt. Copy instead.
if (©_files($file, $dest) != 1) {
&fail_package($pkg, "Unable to copy $file to $dest -- $!");
return MEZZANINE_SYSTEM_ERROR;
} else {
dprint "Can't link across devices; had to copy.\n";
}
} else {
&fail_package($pkg, "Unable to create $dest as a link to $file -- $!");
return MEZZANINE_SYSTEM_ERROR;
}
}
}
# If the stop character is '=', stop checking for matches for this package.
# If it's ':' (actually, any other character than '='), keep looking for matches.
last if ($stop eq "=");
dprint "Non-exclusive match. Continuing on....\n";
}
}
return MEZZANINE_SUCCESS;
}
# This routine parallelizes the product build so that each package is built by an individual buildtool process.
sub
parallel_build
{
my @packages = @_;
my ($pwd, $builddir, $logdir, $buildroot, $dummy);
my (@children, @vars);
my %child_pkg;
# Make sure RPM doesn't try to parallelize itself.
$ENV{"RPM_BUILD_NCPUS"} = "1";
# Create the directories for building this product
if ($buildtree_layout eq "orc") {
$builddir = $ENV{"MEZZANINE_BUILDDIR"};
$logdir = $ENV{"MEZZANINE_LOGDIR"};
&pkgvar_topdir($builddir);
} else {
$builddir = &make_build_dir($ENV{"MEZZANINE_BUILDDIR"});
$logdir = &make_log_dir($ENV{"MEZZANINE_LOGDIR"});
&pkgvar_topdir($builddir);
# Need to do this here for parallel builds because it's not parallel-safe. Race condition. :(
&prepare_build_tree();
}
if (! $OPTION{"nocache"}) {
&scan_rpm_dirs();
}
# Download all the packages.
&download_packages(@packages);
&update_package_list(\@packages);
if ($config->get("INSTROOT")) {
for (my $i = 0; $i < $num_processes; $i++) {
$instroot_pool[$i] = Mezzanine::Instroot->new(
"SOURCE" => $config->get("INSTROOT"),
"INIT" => $config->get("INSTROOT_INIT"),
"RESET" => $config->get("INSTROOT_RESET")
);
$instroot_pool[$i]->init();
}
}
qprint "$PROGNAME: Beginning $num_processes-way build of ${\(scalar(@packages))} packages. (${\(&get_timestamp())})\n";
for (; scalar(@packages) || scalar(@children);) {
my ($pid, $line, $left, $failed, $bldg, $err, $pkg);
# As long as there are packages left to build or children left to wait on, we loop.
for (; scalar(@packages) && (scalar(@children) < $num_processes);) {
my ($logfile, $save_builddir);
# Spawn child processes until we fill up our allotment ($num_processes)
$pkg = shift @packages;
if ($pkgs->{$pkg}{"BUILDUSER"}) {
&file_owner($pkgs->{$pkg}{"BUILDUSER"}, "", "");
}
if ($buildtree_layout eq "orc") {
my $dirname = $pkg;
#$dirname = sprintf("/%s-%s-%s", $pkg, $pkgs->{$pkg}{"VERSION"}, $pkgs->{$pkg}{"RELEASE"});
$save_builddir = $builddir;
$builddir .= ((substr($builddir, -1, 1) eq '/') ? ("") : ('/')) . $dirname;
&make_build_dir($builddir);
$logdir = $builddir;
&pkgvar_topdir($builddir);
&pkgvar_set("buildpkglist_filename", "$logdir/$pkg.pkglist");
}
if ($pkgs->{$pkg}{"INSTROOT"}) {
foreach my $ir (@instroot_pool) {
if ($ir->status() eq "available") {
$pkgs->{$pkg}{"INSTROOT"} = $ir->path();
$ir->use();
last;
} elsif ($ir->status() eq "dirty") {
$ir->reset();
$pkgs->{$pkg}{"INSTROOT"} = $ir->path();
$ir->use();
last;
}
}
}
$logfile = "$logdir/$pkg.log";
$pid = &build_package_forked($pkg, $logfile);
push @children, $pid;
$child_pkg{$pid} = $pkg;
if ($save_builddir) {
$builddir = $save_builddir;
}
}
# Okay, we can't build anything more right now. Print some status info.
$line = "";
foreach my $pid (@children) {
$line .= "$child_pkg{$pid} ($pid) ";
}
nprint "$PROGNAME: Status at @{[&get_timestamp()]}:\n";
nprint "$PROGNAME: Currently building: $line\n";
nprintf("$PROGNAME: %d packages built, %d skipped, %d failed, %d building, %d in queue.\n",
scalar(@completed_pkgs), scalar(@skipped_pkgs), scalar(@failed_pkgs),
scalar(@children), scalar(@packages));
# This loop waits until one of our immediate children dies before continuing.
do {
$pid = waitpid(-1, 0);
} while (! ($pkg = $child_pkg{$pid}));
$err = $? >> 8;
if ($pid == -1) {
# This should never happen.
eprint "Ummm, waitpid() returned -1. That wasn't very nice. I'm offended.\n";
next;
}
# Remove the child from our PID list
@children = grep($_ != $pid, @children);
# Check to see how it exited
if ($err == MEZZANINE_BUILD_UNNEEDED) {
&skipped_package($pkg);
} elsif ($err == MEZZANINE_ARCH_MISMATCH) {
&arch_incompat_package($pkg);
} elsif ($err == MEZZANINE_SUCCESS) {
# Yay! :-)
$num_built++;
&complete_package($pkg);
} else {
my $logfile;
my @tmp;
local *ERRLOG;
# Doh, it failed. Look through the log file to try and find the problem.
if ($buildtree_layout eq "orc") {
my $dirname = $pkg;
#$dirname = sprintf("/%s-%s-%s", $pkg, $pkgs->{$pkg}{"VERSION"}, $pkgs->{$pkg}{"RELEASE"});
$logfile = "$builddir/$dirname/$pkg.log";
} else {
$logfile = "$logdir/$pkg.log";
}
if (!open(ERRLOG, $logfile) && !open(ERRLOG, "$logfile.broken")) {
&fail_package($pkg, "Error $err (log file $logfile is missing)");
next;
}
@tmp = <ERRLOG>;
close(ERRLOG);
# If the last line doesn't match, take the last line out of all the lines that do.
if (scalar(@tmp)) {
if ($tmp[$#tmp] !~ /error: /i) {
@tmp = grep($_ =~ /error: /i, @tmp);
if (!scalar(@tmp)) {
&fail_package($pkg, "Error $err (no errors found in log)");
next;
}
}
chomp($line = $tmp[$#tmp]);
# Record that the package failed.
if ($line =~ /^$PROGNAME: Error: Package \S+ failed: (.*)$/) {
&fail_package($pkg, $1);
} else {
$line = "Couldn't find error in log" if (! $line);
$line =~ s/^\w+:\s*Error:\s*//;
$line =~ s/^Package \"[^\"]+\" failed:\s*//;
&fail_package($pkg, "Error $err -- $line");
}
} else {
&fail_package($pkg, "Error $err (log file $logfile is empty)");
}
}
# Return its instroot to the pool, if needed.
if ($pkgs->{$pkg}{"INSTROOT"} && -d $pkgs->{$pkg}{"INSTROOT"}) {
foreach my $ir (@instroot_pool) {
if ($ir->path() eq $pkgs->{$pkg}{"INSTROOT"}) {
if ($err == MEZZANINE_BUILD_UNNEEDED) {
$ir->release_clean();
} else {
$ir->release();
}
last;
}
}
}
# End of loop. Time to spawn the next child.
}
# Remove chroot jail images no longer needed.
foreach my $ir (@instroot_pool) {
$ir->remove();
}
@instroot_pool = ();
&cleanup_build_tree();
if ($num_built > 0) {
return $num_built;
} else {
return (0 - scalar(@failed_pkgs));
}
}
sub
build_product
{
my @packages = @_;
my ($builddir, $logdir);
# Create the directories for building this product
if ($buildtree_layout eq "orc") {
$builddir = $ENV{"MEZZANINE_BUILDDIR"};
$logdir = $ENV{"MEZZANINE_LOGDIR"};
&pkgvar_topdir($builddir);
} else {
$builddir = &make_build_dir($ENV{"MEZZANINE_BUILDDIR"});
$logdir = &make_log_dir($ENV{"MEZZANINE_LOGDIR"});
&pkgvar_topdir($builddir);
}