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