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