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