66c50f61e66ec96bb24cf8ab4f7ab3a569d2ca06
[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     $c="export http_proxy=http://127.0.0.1:3128/; $c";
213     $c="set -ex; cd $builddir; $c";
214     die "found ': $c" if $c=~/'/;
215     spawn "mockrun $::distro '$c'";
216   }
217
218   if ($cvsbasedir) {
219     die if !$cvsroot;
220     die if !$cvsrepo;
221     spawn "cd $distrodir; cvs -q -z3 -d $cvsroot co rpms/$cvsrepo/$cvsbasedir";
222     my $componentdir="$distrodir/rpms/$cvsrepo/$cvsbasedir";
223     -d $componentdir or die "Failed checkout to: $componentdir";
224     # Required for RHEL; Fedora does so automatically.
225     spawn "cd $componentdir/..; cvs -q -z3 -d $cvsroot co common";
226     # Workaround (RHEL-5?) curl which uses `Pragma: nocache' on $http_proxy.
227     subst sub { s{echo "curl }{$&-H 'Pragma: cache' }; },"$componentdir/../common/Makefile.common";
228
229     for my $file (@file) {
230       my $filebase=$file;
231       $filebase=~s{^.*/}{};
232       my $target="$componentdir/$filebase";
233       # Some *.patch files may be new.
234       # -f $target or die "File $file does not exist at $target";
235       spawn "rm -f $target; cp -p $file $target";
236     }
237
238     my $glob="$componentdir/*.src.rpm";
239     @{[glob $glob]}==0 or die "Found some before test-srpm: $glob";
240     # No `spawn' as we could get:
241     # error: unpacking of archive failed on file X;4a56efef: cpio: MD5 sum mismatch
242     mockrun "cd $componentdir; make test-srpm";
243     my @srcrpm=(glob $glob);
244     @srcrpm==1 or die "Did not find 1 srcrpm: @srcrpm";
245     $srcrpm=$srcrpm[0];
246   }
247
248   if ($srcrpm) {
249     my $srcrpmbasename=$srcrpm;
250     $srcrpmbasename=~s{^.*/}{};
251     spawn "cp -p $srcrpm $builddir/$srcrpmbasename";
252
253     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"};
254     $rpmbuildlocal="orphanripper $rpmbuildlocal" if $cvsrepo && $cvsrepo eq "glibc";
255     mockrun $rpmbuildlocal." --rebuild --with testsuite".($parallel<2 ? "" : " --with parallel")." $srcrpmbasename";
256   }
257
258   my $baretestsuite;
259
260   if ($component eq "gdbcvs") {
261     if (-d $gdbcvsbare) {
262       spawn "cp -a $gdbcvsbare $builddir/src; cd $builddir/src; cvs update -A";
263     } else {
264       spawn "cd $builddir; cvs -q -z3 -d :pserver:anoncvs:\@sourceware.org:/cvs/src co gdb";
265     }
266     spawn "cd $builddir/src".q{; test -z "$(cvs update -A)"};
267     $baretestsuite="$builddir/src";
268   }
269
270   if ($component=~/^archer-/) {
271     if (-d $archermaster) {
272       spawn "cp -a $archermaster $builddir/$component; cd $builddir/$component; git pull"
273     } else {
274       spawn "cd $builddir; git clone git://sourceware.org/git/archer.git; mv -f archer $component";
275     }
276     spawn "cd $builddir/$component; git checkout -b $component origin/$component; [ \"`git status`\" = \"# On branch $component\nnothing to commit (working directory clean)\" ]";
277     $baretestsuite="$builddir/$component";
278   }
279
280   if ($component=~m{^/home/}) {
281     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 || :";
282     $baretestsuite="$builddir/src";
283   }
284
285   if ($baretestsuite) {
286     for my $file (@file) {
287       my $target="$baretestsuite/$file";
288       if ($file=~m{[.](R?)patch$}) {
289         my $R=$1;
290         my $fileabs=$file;
291         $fileabs=$ENV{"PWD"}."/$fileabs" if $fileabs!~m{^/};
292         spawn "cd $baretestsuite; patch -${R}p1 <$fileabs";
293       } else {
294         -f $target or $file=~m{/testsuite/} or die "File $file does not exist at $target";
295         spawn "rm -f $target; cp -p $file $target";
296       }
297     }
298
299     my @check=($distro=~/-x86_64/ ? qw(-m64 -m32) : -m32);
300     @check=map("check//unix/$_",@check);
301     # FSF GDB has no PIE support.
302     # @check=map({($_,"$_/-fPIE/-pie");} @check);
303     # for i in ".join(" ",@check).";do orphanripper make -k \$i || :;done
304     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;};
305   }
306
307   # Call gdbunpack only if no direct $out directory will be created.
308   # It is needed only for .src.rpm-built testsuites, no matter how .src.rpm
309   # got created.
310   my @testinlog;
311   if ($component=~/^(?:fedora|rhel)glibc$/) {
312     @testinlog=(qr/={20}TESTING DETAILS={17}/,qr/={20}PLT RELOCS END={18}/);
313   }
314   if (@testinlog) {
315     subst sub { s{^.*?\n($testinlog[0]\n.*\n$testinlog[1]\n).*$}{$1}s; },$log,$out;
316   } elsif ($cvsbasedir || $srcrpm) {
317     # Applies both to gdb and binutils.
318     spawn "gdbunpack $log";
319   }
320
321   exit 0 if $parallel>1;
322 }
323
324 while (1) {
325   print STDERR "waiting for ".scalar(keys(%child))." children...\n";
326   my $pid=wait();
327   last if $pid==-1 && $!==10; # 10==No child processes
328   die "wait()==-1: $!" if $pid==-1;
329   die "not found pid $pid" if !$child{$pid};
330   error "weird status $? for pid $pid: ".$child{$pid} if $?;
331   print STDERR "finished: $pid ".$child{$pid}."\n";
332   delete $child{$pid};
333 }
334 die "Uncollected children: ".keys(%child) if keys(%child);
335
336 sub timestr($)
337 {
338   my($sec)=@_;
339   my $r="";
340
341   if ($sec>=60*60) {
342     $r.=int($sec/(60*60))."h";
343     $sec%=60*60;
344   }
345   if ($r || $sec>=60) {
346     $r.=int($sec/60)."m";
347     $sec%=60;
348   }
349   $r.=$sec."s";
350
351   return $r;
352 }
353
354 print STDERR "ID = $id | dir = $dir\n";
355 print STDERR "total time=".timestr(time()-$start)."\n";
356 die "$error errors seen, aborted" if $error;
357
358 print STDERR "done\n";