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