init
[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|glibc)|gdbcvs|archer-.*|/home/.*)$} or die "-c|--component required to be: fedoragdb|rhelgdb|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 # PID->distro
143 my %child;
144 for my $distro (@distrouse) {
145   my $cvsbasedir;
146   my $cvsroot;
147   my $cvsrepo;
148   if ($component=~/^fedora(gdb|glibc)$/) {
149     $cvsrepo=$1;
150     $cvsbasedir="F-$1" if $distro=~/^fedora-(\d+)-$arches_re$/;
151     $cvsbasedir="devel" if $distro=~/^fedora-rawhide-$arches_re$/;
152     die "$component vs. $distro" if !$cvsbasedir;
153     $cvsroot=$fedoracvsroot;
154   }
155   if ($component=~/^rhel(gdb|glibc)$/) {
156     $cvsrepo=$1;
157     $cvsbasedir="RHEL-$1" if $distro=~/^epel-(\d+)-$arches_re$/;
158     die "$component vs. $distro" if !$cvsbasedir;
159     $cvsroot=$rhelcvsroot;
160   }
161
162   my $distrodir="$dir/$distro";
163   newdir $distrodir;
164   $log="$distrodir/log";
165   my $out="$distrodir/out";
166
167   if ($parallel>1) {
168     my $pid=fork();
169     die if !defined $pid;
170     if ($pid) {
171       $child{$pid}=$distro;
172       next;
173     }
174   }
175
176   my $builddir="$distrodir/build";
177   newdir $builddir;
178
179   $::distro=$distro;
180   sub mockrun($)
181   {
182     my($c)=@_;
183
184     $c="export PATH=\"$path:\$PATH\"; $c" if $path;
185     $c="export MAKEFLAGS=\"-j\$[`getconf _NPROCESSORS_ONLN`*3/2]\"; $c";
186     $c="set -ex; cd $builddir; $c";
187     die "found ': $c" if $c=~/'/;
188     spawn "mockrun $::distro '$c'";
189   }
190
191   mockrun "rpm -qa|sort >$distrodir/rpm-qa";
192
193   if ($cvsbasedir) {
194     die if !$cvsroot;
195     die if !$cvsrepo;
196     spawn "cd $distrodir; cvs -q -z3 -d $cvsroot co rpms/$cvsrepo/$cvsbasedir";
197     my $componentdir="$distrodir/rpms/$cvsrepo/$cvsbasedir";
198     -d $componentdir or die "Failed checkout to: $componentdir";
199     # Required for RHEL; Fedora does so automatically.
200     spawn "cd $componentdir/..; cvs -q -z3 -d $cvsroot co common";
201
202     for my $file (@file) {
203       my $filebase=$file;
204       $filebase=~s{^.*/}{};
205       my $target="$componentdir/$filebase";
206       # Some *.patch files may be new.
207       # -f $target or die "File $file does not exist at $target";
208       spawn "rm -f $target; cp -p $file $target";
209     }
210
211     my $glob="$componentdir/*.src.rpm";
212     @{[glob $glob]}==0 or die "Found some before test-srpm: $glob";
213     spawn "cd $componentdir; make test-srpm";
214     my @srcrpm=(glob $glob);
215     @srcrpm==1 or die "Did not find 1 srcrpm: @srcrpm";
216     $srcrpm=$srcrpm[0];
217   }
218
219   if ($srcrpm) {
220     my $srcrpmbasename=$srcrpm;
221     $srcrpmbasename=~s{^.*/}{};
222     spawn "cp -p $srcrpm $builddir/$srcrpmbasename";
223
224     my $rpmbuildlocal=q{rpmbuild --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"};
225     $rpmbuildlocal="orphanripper $rpmbuildlocal" if $cvsrepo && $cvsrepo eq "glibc";
226     mockrun $rpmbuildlocal." --rebuild --with testsuite".($parallel<2 ? "" : " --with parallel")." $srcrpmbasename";
227   }
228
229   my $baretestsuite;
230
231   if ($component eq "gdbcvs") {
232     if (-d $gdbcvsbare) {
233       spawn "cp -a $gdbcvsbare $builddir/src; cd $builddir/src; cvs update -A";
234     } else {
235       spawn "cd $builddir; cvs -q -z3 -d :pserver:anoncvs:\@sourceware.org:/cvs/src co gdb";
236     }
237     spawn "cd $builddir/src".q{; test -z "$(cvs update -A)"};
238     $baretestsuite="$builddir/src";
239   }
240
241   if ($component=~/^archer-/) {
242     if (-d $archermaster) {
243       spawn "cp -a $archermaster $builddir/$component; cd $builddir/$component; git pull"
244     } else {
245       spawn "cd $builddir; git clone git://sourceware.org/git/archer.git; mv -f archer $component";
246     }
247     spawn "cd $builddir/$component; git checkout -b $component origin/$component; [ \"`git status`\" = \"# On branch $component\nnothing to commit (working directory clean)\" ]";
248     $baretestsuite="$builddir/$component";
249   }
250
251   if ($component=~m{^/home/}) {
252     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 || :";
253     $baretestsuite="$builddir/src";
254   }
255
256   if ($baretestsuite) {
257     for my $file (@file) {
258       my $target="$baretestsuite/$file";
259       if ($file=~m{\Q.patch\E$}) {
260         my $fileabs=$file;
261         $fileabs=$ENV{"PWD"}."/$fileabs" if $fileabs!~m{^/};
262         spawn "cd $baretestsuite; patch -p1 <$fileabs";
263       } else {
264         -f $target or $file=~m{/testsuite/} or die "File $file does not exist at $target";
265         spawn "rm -f $target; cp -p $file $target";
266       }
267     }
268
269     my @check=($distro=~/-x86_64/ ? qw(-m64 -m32) : -m32);
270     @check=map("check//unix/$_",@check);
271     # FSF GDB has no PIE support.
272     # @check=map({($_,"$_/-fPIE/-pie");} @check);
273     # for i in ".join(" ",@check).";do orphanripper make -k \$i || :;done
274     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;};
275   }
276
277   # Call gdbunpack only if no direct $out directory will be created.
278   # It is needed only for .src.rpm-built testsuites, no matter how .src.rpm
279   # got created.
280   if ($component=~/^(?:fedora|rhel)glibc$/) {
281     local *F;
282     open F,$log or die $log;
283     my $F=do { local $/; <F>; } or die $log;
284     close F or die $log;
285     $F=~s{^.*?\n(={20}TESTING DETAILS={17}\n.*\n={20}PLT RELOCS END={18}\n).*$}{$1}s or die "No TESTING part found: $log";
286
287     open F,">$out" or die $out;
288     print F $F or die $out;
289     close F or die $out;
290   } elsif ($cvsbasedir || $srcrpm) {
291     spawn "gdbunpack $log";
292   }
293
294   exit 0 if $parallel>1;
295 }
296
297 while (1) {
298   print STDERR "waiting for ".scalar(keys(%child))." children...\n";
299   my $pid=wait();
300   last if $pid==-1 && $!==10; # 10==No child processes
301   die "wait()==-1: $!" if $pid==-1;
302   die "not found pid $pid" if !$child{$pid};
303   error "weird status $? for pid $pid: ".$child{$pid} if $?;
304   print STDERR "finished: $pid ".$child{$pid}."\n";
305   delete $child{$pid};
306 }
307 die "Uncollected children: ".keys(%child) if keys(%child);
308
309 sub timestr($)
310 {
311   my($sec)=@_;
312   my $r="";
313
314   if ($sec>=60*60) {
315     $r.=int($sec/(60*60))."h";
316     $sec%=60*60;
317   }
318   if ($r || $sec>=60) {
319     $r.=int($sec/60)."m";
320     $sec%=60;
321   }
322   $r.=$sec."s";
323
324   return $r;
325 }
326
327 print STDERR "ID = $id | dir = $dir\n";
328 print STDERR "total time=".timestr(time()-$start)."\n";
329 die "$error errors seen, aborted" if $error;
330
331 print STDERR "done\n";