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