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