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