eb4455cc5d5984e585000eff14d1ea4e939da8e6
[nethome.git] / bin / hammock
1 #! /usr/bin/perl
2 # $Id$
3
4 use strict;
5 use warnings;
6 use POSIX qw(&strftime);
7 use Getopt::Long qw(:config no_ignore_case);
8 use Carp qw(&carp);
9 my $start=time();
10
11 my $gdbcvsmaster=$ENV{"HOME"}."/redhat/gdb-cvs-master";
12 my $binutilscvsmaster=$ENV{"HOME"}."/redhat/binutils-cvs-master";
13 my $archermaster=$ENV{"HOME"}."/redhat/archer-master";
14 my $fedoragitroot=q{git://pkgs.fedoraproject.org};
15 #Unknown host cvs.devel.redhat.com.
16 #my $rhelcvsroot=q{:pserver:anonymous:@cvs.devel.redhat.com:/cvs/dist};
17 my $rhelcvsroot=q{:ext:cvs.devel.redhat.com:/cvs/dist};
18 my $arch_i386=qr{(?:x86|i\d86|ia32)}io;
19 my $arch_x86_64=qr{(?:x8664|x86_64|em64t)}io;
20 my $resultdir=$ENV{"HOME"}."/.hammock-result";
21 my @arches=qw(i386 x86_64);
22
23 my $error=0;
24 sub error
25 {
26   carp @_;
27   $error++;
28 }
29
30 my $userid;
31 my $force;
32 my $parallel=9; # 1 or 2 or 3
33 my @distro;
34 my @componentdistro;
35 my @path;
36 my @arch;
37 my $component;
38 my $srcrpm;
39 my @file;
40 my @target;
41 my $configure;
42 my $branch;
43 # FIXME: Connect make paralellization to the children.
44 my $distrojobs;
45 my $gdbcvspie;
46 my $strip;
47 die if !GetOptions(
48   "i|userid=s"=>\$userid,
49     "force"=>\$force,
50   "1|serial"=>sub { $parallel=1; },
51   "2|standard"=>sub { $parallel=2; },
52   "3|parallel"=>sub { $parallel=3; },
53   "d|distro=s{,}"=>\@distro,
54     "cd|componentdistro=s{,}"=>\@componentdistro,
55   "p|path=s{,}"=>\@path,
56   "a|arch=s{,}"=>\@arch,
57   "c|component=s"=>\$component,
58   "s|srcrpm=s"=>\$srcrpm,
59     "file=s{,}"=>\@file,
60     "target=s{,}"=>\@target,
61   "D|distrojobs=s"=>\$distrojobs,
62     "configure=s"=>\$configure,
63     "branch=s"=>\$branch,
64     "gdbcvspie"=>\$gdbcvspie,
65     "strip"=>\$strip,
66 );
67 $component and ($component=~m{^(?:(?:fedora|rhel)(?:gdb|binutils|glibc)|gdbcvs|binutilscvs|archer-.*|/home/.*)$} or die "-c|--component required to be: fedoragdb|rhelgdb|fedorabinutils|rhelbinutils|fedoraglibc|rhelglibc|gdbcvs|binutilscvs|archer-*|/home/*");
68 $component and $component=~m{^/home/} and (-f "$component/gdb/gdbtypes.c" or die "$component/gdb/gdbtypes.c not found");
69 $component and $srcrpm and die "-c|--component excludes -s|--srcrpm";
70 $component or $srcrpm or die "-c|--component or -s|--srcrpm required";
71 $component||="";  # Make `eq' not complaining.
72 my $cvsbranch;
73 if ($component=~/^(gdb|binutils)cvs$/) {
74   $cvsbranch=(!$branch?"-A":"-r $branch");
75 } else {
76   die "--branch currently unsupported for non-CVS sources" if $branch;
77 }
78 !$gdbcvspie or $component eq "gdbcvs" or die "--gdbcvspie requires -c gdbcvs";
79 @target and ($srcrpm or $component=~/^(?:fedora|rhel)/) and die "--target is available only for baretestsuite modes";
80 $strip and ($srcrpm or $component=~/^(?:fedora|rhel)/) and die "--strip is available only for baretestsuite modes";
81 my %target;
82 do { error "Duplicate target: $_\n" if $target{$_}++; } for @target;
83 @arch="x86_64" if @target&&!@arch;
84 for my $file (@file) {
85   -f $file and -r $file or error "-f|--file $file not readable: $!";
86 }
87 $distrojobs=(@target?1:2) if !defined $distrojobs;
88 $distrojobs=~/^\d+$/ or die "-D|distrojobs must be a number: $distrojobs";
89 $distrojobs>=1 or die "-D|distrojobs must be positive: $distrojobs";
90 # User may want to modify ASAP her files submitted for the test.
91 my $will_copy=@file || $component=~m{^/home/};
92 $parallel||=$will_copy ? 2 : 1;
93 error "Excessive arguments: @ARGV" if @ARGV;
94 @arch=@arches if !@arch;
95 my $path=join(":",@path) if @path;
96 @componentdistro and @componentdistro!=@distro and die "--cd|--componentdistro must have the same elements count as -d|--distro";
97
98 sub distro_normalize($;$)
99 {
100   my($name,$force)=@_;
101   local $_=$name;
102
103   s{^/var/lib/mock/+}{};
104   s{/+$}{};
105   s/^.*$/\L$&/s;
106   s/^(?:devel|rawhide)\b/fedora-rawhide/;
107   s/^(?:epel|centos)-?(\d)/epel-$1/;
108   s/^(?:rhel)-?(\d)/rhel-$1/;
109   s/^(?:f|fedora)-?(\d)/fedora-$1/;
110   my @archuse;
111   @archuse="" if -d "/var/lib/mock/$_";
112   @archuse="i386" if s/-$arch_i386$//o;
113   @archuse="x86_64" if s/-$arch_x86_64$//o;
114
115   for my $arch (@archuse?@archuse:@arch) {
116     my $dir="/var/lib/mock/$_".(!$arch?"":"-$arch");
117     $force or -d $dir or error "No distro: $dir";
118   }
119
120   return ($_,@archuse);
121 }
122
123 # epel-\d-i386|fedora-\d-i386|fedora-rawhide-i386
124 my @distrouse;
125 for my $distroi (0..$#distro) {
126   my $distro=$distro[$distroi];
127   my $componentdistro=$componentdistro[$distroi];
128   my @archuse;
129   if ($componentdistro) {
130     my $force=($componentdistro=~s/!$//);
131     ($componentdistro,@archuse)=distro_normalize $componentdistro,$force;
132     @archuse and die "--cd|--componentdistro must have no arch: ".join(" ",@archuse);
133   }
134   ($distro,@archuse)=distro_normalize $distro;
135   $componentdistro||=$distro;
136   @archuse=@arch if !@archuse;
137   for my $archuse (@archuse) {
138     for my $target (@target?@target:undef()) {
139       push @distrouse,{"distro"=>$distro.(!$archuse?"":"-$archuse"),"componentdistro"=>$componentdistro,"target"=>$target};
140     }
141   }
142 }
143
144 # "-p", "mayexist"
145 sub newdir($;@)
146 {
147   my($dir,@opt)=@_;
148
149   my %opt=map(($_=>1),@opt);
150   warn "+ mkdir".($opt{"-p"} ? " -p" : "")." $dir\n";
151   mkdir $dir or ($opt{"mayexist"} && $!{EEXIST}) or die "mkdir $dir: $!";
152 }
153
154 my $log;
155 # "bare"
156 sub spawn($;%)
157 {
158   my($cmd,@opt)=@_;
159
160   my %opt=map(($_=>1),@opt);
161   my $ok;
162   if (!$opt{"bare"}) {
163     $cmd="set -ex; $cmd";
164     $ok="$log.ok" if $log;
165     $cmd="($cmd; touch $ok) 2>&1|tee -a $log; test -f $ok" if $log;
166     unlink $ok if $ok;
167   } else {
168     warn "+ $cmd\n";
169   }
170   # warn "+ $cmd\n";
171   system $cmd and die "$cmd: $!";
172   unlink $ok if $ok;
173 }
174
175 my $basedir=$ENV{"HOME"}."/hammock";
176 newdir $basedir,"mayexist";
177 my $idbase=strftime("%Y%m%d",localtime());
178 my $id;
179 my $dir;
180 for my $seq (defined $userid ? $userid : (0..99)) {
181   $id=$idbase.(defined $userid ? $seq : sprintf("%02d",$seq));
182   $dir="$basedir/$id";
183   last if ! -e $dir;
184 }
185 spawn "chmod -R u+w $dir; rm -rf $dir" if -d $dir && $force && defined $userid;
186 error "Directory not free: $dir" if !$id || !$dir || -e $dir;
187 print STDERR "ID = $id | dir = $dir\n";
188 error "No distros specified" if !@distrouse;
189 die "$error errors seen, aborted" if $error;
190
191 sub writefile
192 {
193   my($fname,$content)=@_;
194
195   local *F;
196   open F,">$fname" or die $fname;
197   print F $content or die $fname;
198   close F or die $fname;
199 }
200
201 spawn "renice +19 -p $$";
202 spawn "ionice -c3 -p $$";
203 newdir $dir;
204 $log="$dir/log";
205 my $resultid="$resultdir/$id";
206 my $resultidxz="$resultid.tar.xz";
207 newdir $resultdir,"mayexist";
208 spawn "rm -rf $resultid" if -d $resultid && $force;
209 newdir $resultid;
210 unlink $resultidxz or $!{ENOENT} or die "unlink $resultidxz: $!";
211 spawn "uname -r >$dir/kernel";
212 my %dump=(
213   "path"=>$path,
214   "component"=>$component,
215   "branch"=>$branch,
216   "srcrpm"=>$srcrpm,
217   "file"=>join("\n",@file),
218   "configure"=>$configure,
219   "gdbcvspie"=>$gdbcvspie,
220 );
221 while (my($name,$val)=each(%dump)) {
222   next if !$val;
223   writefile "$dir/$name","$val\n";
224   link "$dir/$name","$resultid/$name" or warn "link $dir/$name $resultid/$name: $!";
225 }
226 for my $file (@file) {
227   newdir "$dir/file.d","mayexist";
228   (my $base=$file)=~s{^.*/}{};
229   my $d="$dir/file.d/$base";
230   link $file,$d or die "link $file $d: $!";
231   newdir "$resultid/file.d","mayexist";
232   link $d,"$resultid/file.d/$base" or warn "link $file $resultid/file.d/$base: $!";
233 }
234
235 sub subst
236 {
237   my($sub,$in,$out)=@_;
238
239   $out||=$in;
240
241   local *F;
242   open F,$in or die $in;
243   local $_=do { local $/; <F>; } or die $in;
244   close F or die $in;
245
246   &{$sub}() or die $_."\nError substituting $in";
247
248   writefile $out,$_;
249 }
250
251 sub copyfiles($)
252 {
253   my($targetdir)=@_;
254
255   for my $file (@file) {
256     my $filebase=$file;
257     $filebase=~s{^.*/}{};
258     my $target="$targetdir/$filebase";
259     # Some *.patch files may be new.
260     # -f $target or die "File $file does not exist at $target";
261     spawn "rm -f $target; cp -p $file $target";
262   }
263 }
264
265 # PID->distro
266 my %child;
267 while (@distrouse || keys(%child)) {
268   while (keys(%child)<$distrojobs && @distrouse) {
269     my $distrouse=shift @distrouse;
270     my $distro=$distrouse->{"distro"};
271     my $componentdistro=$distrouse->{"componentdistro"};
272     my $target=$distrouse->{"target"};
273     my $rpmbuild="rpmbuild";
274
275     my $cvsbasedir;
276     my $gitbranch;
277     my $cvsroot;
278     my $gitroot;
279     my $cvsrepo;
280     my $gitrepo;
281     if ($component=~/^fedora(.*)$/) {
282       $gitrepo=$1;
283       $gitbranch="f$1/master" if $componentdistro=~/^fedora-(\d+)$/;
284       $gitbranch="master" if $componentdistro=~/^fedora-rawhide$/;
285       die "$component vs. $componentdistro" if !$gitbranch;
286       $gitroot=$fedoragitroot;
287     }
288     if ($component=~/^rhel(.*)$/) {
289       $cvsrepo=$1;
290       $cvsbasedir="RHEL-$1" if $componentdistro=~/^(?:epel|rhel)-(\d+)$/;
291       die "$component vs. $componentdistro" if !$cvsbasedir;
292       $cvsroot=$rhelcvsroot;
293       # EPEL still uses Berkeley DB version 8 while F-11+ (F-10?) uses version 9.
294       # Using db_dump and db_load would no longer make it mock compatible.
295       $rpmbuild.=q{ --dbpath $PWD --nodeps};
296     }
297
298     my $distrodirbase=$distro;
299     $distrodirbase.="-$target" if $target;
300     my $distrodir="$dir/$distrodirbase";
301     newdir $distrodir;
302     $log="$distrodir/log";
303     my $out="$distrodir/out";
304     newdir $out;
305
306     if ($parallel>1) {
307       my $pid=fork();
308       die if !defined $pid;
309       if ($pid) {
310         $child{$pid}=$distrodirbase;
311         next;
312       }
313     }
314
315     my $builddir="$distrodir/build";
316     newdir $builddir;
317
318     # Do not use mockrun as the rpm database may be in a different version.
319     spawn "rpm -r /var/lib/mock/$distro/root -qa|sort >$out/rpm-qa";
320
321     $::distro=$distro;
322     sub mockrun($)
323     {
324       my($c)=@_;
325
326       $c="export PATH=\"$path:\$PATH\"; $c" if $path;
327       $c="export MAKEFLAGS=\"-j\$[`getconf _NPROCESSORS_ONLN`*3/2]\"; $c";
328       $c="export http_proxy=http://127.0.0.1:3128/; $c";
329       $c="set -ex; cd $builddir; $c";
330       die "found ': $c" if $c=~/'/;
331       spawn "mockrun $::distro '$c'";
332     }
333
334     if ($gitbranch) {
335       die if !$gitroot;
336       die if !$gitrepo;
337       die if !$gitbranch;
338       spawn "cd $distrodir; git clone -b $gitbranch $gitroot/$gitrepo $component";
339       my $componentdir="$distrodir/$component";
340       -d $componentdir or die "Failed checkout to: $componentdir";
341       copyfiles $componentdir;
342       spawn "cd $componentdir; fedpkg verrel >$out/verrel";
343       my $glob="$componentdir/*.src.rpm";
344       @{[glob $glob]}==0 or die "Found some before test-srpm: $glob";
345       # No `spawn' as we could get:
346       # error: unpacking of archive failed on file X;4a56efef: cpio: MD5 sum mismatch
347       mockrun "cd $componentdir; fedpkg srpm";
348       my @srcrpm=(glob $glob);
349       @srcrpm==1 or die "Did not find 1 srcrpm: @srcrpm";
350       $srcrpm=$srcrpm[0];
351     }
352
353     if ($cvsbasedir) {
354       die if !$cvsroot;
355       die if !$cvsrepo;
356       die if !$cvsbasedir;
357       spawn "cd $distrodir; cvs -q -z3 -d $cvsroot co rpms/$cvsrepo/$cvsbasedir";
358       my $componentdir="$distrodir/rpms/$cvsrepo/$cvsbasedir";
359       -d $componentdir or die "Failed checkout to: $componentdir";
360       # Required for RHEL; Fedora does so automatically.
361       spawn "cd $componentdir/..; cvs -q -z3 -d $cvsroot co common";
362       # Workaround (RHEL-5?) curl which uses `Pragma: nocache' on $http_proxy.
363       subst sub { s{echo "curl }{$&-H 'Pragma: cache' }; },"$componentdir/../common/Makefile.common";
364       copyfiles $componentdir;
365       spawn "cd $componentdir; make verrel >$out/verrel";
366       my $glob="$componentdir/*.src.rpm";
367       @{[glob $glob]}==0 or die "Found some before test-srpm: $glob";
368       # No `spawn' as we could get:
369       # error: unpacking of archive failed on file X;4a56efef: cpio: MD5 sum mismatch
370       mockrun "cd $componentdir; make test-srpm";
371       my @srcrpm=(glob $glob);
372       @srcrpm==1 or die "Did not find 1 srcrpm: @srcrpm";
373       $srcrpm=$srcrpm[0];
374     }
375
376     if ($srcrpm) {
377       my $srcrpmbasename=$srcrpm;
378       $srcrpmbasename=~s{^.*/}{};
379       spawn "cp -p $srcrpm $builddir/$srcrpmbasename";
380
381       my $rpmbuildlocal=$rpmbuild.q{ --define "_topdir $PWD" --define "_builddir $PWD" --define "_rpmdir $PWD" --define "_sourcedir $PWD" --define "_specdir $PWD" --define "_srcrpmdir $PWD" --define "_build_name_fmt %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm"};
382       $rpmbuildlocal="orphanripper $rpmbuildlocal" if $cvsrepo && $cvsrepo eq "glibc";
383       mockrun $rpmbuildlocal." --rebuild --with testsuite".($parallel<2 ? "" : " --with parallel")." $srcrpmbasename";
384     }
385
386     my $baretestsuite;
387
388     if ($component=~/^(gdb|binutils)cvs$/) {
389       my $which=$1;
390       my $cvsmaster=$which eq "gdb" ? $gdbcvsmaster : $binutilscvsmaster;
391       if (-d $cvsmaster) {
392         spawn "cp -a $cvsmaster $builddir/src; cd $builddir/src; cvs update $cvsbranch";
393       } else {
394         spawn "cd $builddir; cvs -q -z3 -d :pserver:anoncvs:\@sourceware.org:/cvs/src co $cvsbranch $which";
395       }
396       spawn "cd $builddir/src; test -z \"\$(cvs update $cvsbranch)\"";
397       $baretestsuite="$builddir/src";
398     }
399
400     if ($component=~/^archer-/) {
401       spawn "git clone ".(!-d $archermaster ? "" : "--reference $archermaster")." git://sourceware.org/git/archer.git $builddir/$component";
402       spawn "cd $builddir/$component; git fetch";
403       spawn "cd $builddir/$component; git checkout -b $component origin/$component; [ \"`git status`\" = \"# On branch $component\nnothing to commit (working directory clean)\" ]";
404       $baretestsuite="$builddir/$component";
405     }
406
407     if ($component=~m{^/home/}) {
408       spawn "cp -a $component $builddir/src; cd $builddir/src; find -name \"*.[oa]\" -o -name \"*.l[oa]\" -o -name gdb.sum -o -name gdb.log|xargs rm -f; make clean || :";
409       $baretestsuite="$builddir/src";
410     }
411
412     if ($baretestsuite) {
413       for my $file (@file) {
414         my $target="$baretestsuite/$file";
415         if ($file=~m{[.](R?)patch$}) {
416           my $R=$1;
417           my $fileabs=$file;
418           $fileabs=$ENV{"PWD"}."/$fileabs" if $fileabs!~m{^/};
419           spawn "cd $baretestsuite; patch -${R}p1 <$fileabs";
420         } else {
421           -f $target or $file=~m{/testsuite/} or die "File $file does not exist at $target";
422           spawn "rm -f $target; cp -p $file $target";
423         }
424       }
425
426       my $errs12="errs12";
427       $errs12.=" -s" if $strip;
428       $errs12.=" --target=$target" if $target;
429       $errs12.=" $configure" if $configure;
430
431       if ($component eq "binutilscvs") {
432         mockrun "cd $baretestsuite; $errs12; ulimit -c unlimited; orphanripper make -k check || :; ".q{for file in {gas/testsuite/gas,ld/ld,binutils/binutils}.{sum,log};do ln $file}." $out/binutils-$distro-".q{$(basename $file) || :; done;};
433       } else {
434         my @check=($distro=~/-x86_64/ ? qw(-m64 -m32) : -m32);
435         @check=map("check//unix/$_",@check);
436         @check=map({($_,"$_/-fPIE/-pie");} @check) if $gdbcvspie;
437         # for i in ".join(" ",@check).";do orphanripper make -k \$i || :;done
438         mockrun "cd $baretestsuite; $errs12; cd gdb; ulimit -c unlimited; orphanripper make -k ".join(" ",@check)." || :; ".q{for t in sum log;do for file in testsuite*/gdb.$t;do suffix="${file#testsuite.unix.}"; suffix="${suffix%/gdb.$t}"; ln $file}." $out/gdb".q{$suffix.$t || :; done; done;};
439       }
440       # gdbunpack does:
441       #         perl -i -pe 's{\Q'"$HOME"'\E/.*?/build/[^/]*/}{}g' "$base"/*
442       my $HOME=$ENV{"HOME"};
443       for my $file (glob("$out/*.sum"),glob("$out/*.log")) {
444         subst sub { s{\Q$HOME\E/.*?/build/[^/]*/}{}g; },$file;
445       }
446     } else {
447       die "internal error" if @target;
448     }
449
450     # Call gdbunpack only if no direct $out directory will be created.
451     # It is needed only for .src.rpm-built testsuites, no matter how .src.rpm
452     # got created.
453     my @testinlog;
454     if ($component=~/^(?:fedora|rhel)glibc$/) {
455       @testinlog=(qr/={20}TESTING DETAILS={17}/,qr/={20}PLT RELOCS END={18}/);
456     }
457     if (@testinlog) {
458       subst sub { s{^.*?\n($testinlog[0]\n.*\n$testinlog[1]\n).*$}{$1}s; },$log,$out;
459     } elsif ($cvsbasedir || $srcrpm) {
460       # Applies both to gdb and binutils.
461       spawn "mv $out $out.x; gdbunpack $log; mv $out.x/* $out/; rmdir $out.x";
462     }
463
464     my $resultout="$resultdir/$id/$distrodirbase";
465     newdir $resultout;
466     $resultout.="/out";
467     newdir $resultout;
468     for my $fname (glob "$out/*") {
469       (my $base=$fname)=~s{^.*/}{};
470       my $d="$resultout/$base";
471       warn "+ link $fname $d\n";
472       link $fname,$d or warn $!;
473     }
474
475     exit 0 if $parallel>1;
476   }
477
478   print STDERR "waiting for ".scalar(keys(%child))." children, ".scalar(@distrouse)." distros to go...\n";
479   my $pid=wait();
480   next if $pid==-1 && $!==10; # 10==No child processes
481   die "wait()==-1: $!" if $pid==-1;
482   die "not found pid $pid" if !$child{$pid};
483   error "weird status $? for pid $pid: ".$child{$pid} if $?;
484   print STDERR "finished: $pid ".$child{$pid}."\n";
485   delete $child{$pid};
486 }
487 die if keys(%child);
488 die if @distrouse;
489
490 spawn "(set -e -o pipefail;cd $resultdir;tar cf - $id|xz -9e >$resultidxz;rm -rf $id)&","bare";
491
492 sub timestr($)
493 {
494   my($sec)=@_;
495   my $r="";
496
497   if ($sec>=60*60) {
498     $r.=int($sec/(60*60))."h";
499     $sec%=60*60;
500   }
501   if ($r || $sec>=60) {
502     $r.=int($sec/60)."m";
503     $sec%=60;
504   }
505   $r.=$sec."s";
506
507   return $r;
508 }
509
510 print STDERR "ID = $id | dir = $dir\n";
511 my $time=timestr(time()-$start);
512 print STDERR "total time=$time\n";
513 writefile "$dir/time","$time\n";
514 die "$error errors seen, aborted" if $error;
515 print STDERR "done\n";