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