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