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