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