blib/lib/Apache/TestBuild.pm untested

filter: Statementstmt 0.0% 0 / 315 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 44 Podpod 0.0% 0 / 31 totaltotal 0.0% 0 / 510 CC 38 SCAR 27.3
CC
38
Cov
0%
merge_build
72.0
cvs_up
20.0
add_ssl_libs
20.0
CRAP 15.3
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 package Apache::TestBuild;
17  
18 use strict;
19 use warnings FATAL => 'all';
20  
21 use subs qw(system chdir
22 info warning);
23  
24 use Config;
25 use File::Spec::Functions;
26 use File::Path ();
27 use Cwd ();
28  
29 use constant DRYRUN => 0;
30  
31 my @min_modules = qw(access auth log-config env mime setenvif
32 mime autoindex dir alias);
33  
34 my %shared_modules = (
35 min => join(' ', @min_modules),
36 );
37  
38 my %configs = (
39 all => {
40 'apache-1.3' => [],
41 'httpd-2.0' => enable20(qw(modules=all proxy)),
42 },
43 most => {
44 'apache-1.3' => [],
45 'httpd-2.0' => enable20(qw(modules=most)),
46 },
47 min => {
48 'apache-1.3' => [],
49 'httpd-2.0' => enable20(@min_modules),
50 },
51 exp => {
52 'apache-1.3' => [],
53 'httpd-2.0' => enable20(qw(example case_filter
54 case_filter_in cache
55 echo deflate bucketeer)),
56 },
57 );
58  
59 my %builds = (
60 default => {
61 cflags => '-Wall',
62 config => {
63 'apache-1.3' => [],
64 'httpd-2.0' => [],
65 },
66 },
67 debug => {
68 cflags => '-g',
69 config => {
70 'apache-1.3' => [],
71 'httpd-2.0' => [qw(--enable-maintainer-mode)],
72 },
73 },
74 prof => {
75 cflags => '-pg -DGPROF',
76 },
77 shared => {
78 config => {
79 'apache-1.3' => [],
80 'httpd-2.0' => enable20_shared('all'),
81 },
82 },
83 mostshared => {
84 config => {
85 'apache-1.3' => [],
86 'httpd-2.0' => enable20_shared('most'),
87 },
88 },
89 minshared => {
90 config => {
91 'apache-1.3' => [],
92 'httpd-2.0' => enable20_shared('min'),
93 },
94 },
95 static => {
96 },
97 );
98  
99 my %mpms = (
100 default => [qw(prefork worker)],
101 MSWin32 => [qw(winnt)],
102 );
103  
104 my @cvs = qw(httpd-2.0 apache-1.3);
105  
106 my @dirs = qw(build tar src install);
107  
108 sub enable20 {
109 [ map { "--enable-$_" } @_ ];
110 }
111  
112 sub enable20_shared {
113 my $name = shift;
114 my $modules = $shared_modules{$name} || $name;
115 enable20(qq(mods-shared="$modules"));
116 }
117  
118 sub default_mpms {
119 $mpms{ $^O } || $mpms{'default'};
120 }
121  
122 sub default_dir {
123 my($self, $dir) = @_;
124 $self->{$dir} ||= catdir $self->{prefix}, $dir,
125 }
126  
127 sub new {
128 my $class = shift;
129  
130 #XXX: not generating a BUILD script yet
131 #this way we can run:
132 #perl Apache-Test/lib/Apache/TestBuild.pm --cvsroot=anon --foo=...
133  
134 require Apache::TestConfig;
135 require Apache::TestTrace;
136 Apache::TestTrace->import;
137  
138 my $self = bless {
139 prefix => '/usr/local/apache',
140 cwd => Cwd::cwd(),
141 cvsroot => 'cvs.apache.org:/home/cvs',
142 cvs => \@cvs,
143 cvstag => "",
144 ssldir => "",
145 mpms => default_mpms(),
146 make => $Config{make},
147 builds => {},
148 name => "",
149 extra_config => {
150 'httpd-2.0' => [],
151 },
152 @_,
153 }, $class;
154  
155 #XXX
156 if (my $c = $self->{extra_config}->{'2.0'}) {
157 $self->{extra_config}->{'httpd-2.0'} = $c;
158 }
159  
160 for my $dir (@dirs) {
161 $self->default_dir($dir);
162 }
163  
164 if ($self->{ssldir}) {
165 push @{ $self->{extra_config}->{'httpd-2.0'} },
166 '--enable-ssl', "--with-ssl=$self->{ssldir}";
167 }
168  
169 $self;
170 }
171  
172 sub init {
173 my $self = shift;
174  
175 for my $dir (@dirs) {
176 mkpath($self->{$dir});
177 }
178 }
179  
180 use subs qw(symlink unlink);
181 use File::Basename;
182 use File::Find;
183  
184 sub symlink_tree {
185 my $self = shift;
186  
187 my $httpd = 'httpd';
188 my $install = "$self->{install}/bin/$httpd";
189 my $source = "$self->{build}/.libs/$httpd";
190  
191 unlink $install;
192 symlink $source, $install;
193  
194 my %dir = (apr => 'apr',
195 aprutil => 'apr-util');
196  
197 for my $libname (qw(apr aprutil)) {
198 my $lib = "lib$libname.so.0.0.0";
199 my $install = "$self->{install}/lib/$lib";
200 my $source = "$self->{build}/srclib/$dir{$libname}/.libs/$lib";
201  
202 unlink $install;
203 symlink $source, $install;
204 }
205  
206 $install = "$self->{install}/modules";
207 $source = "$self->{build}/modules";
208  
209 for (<$install/*.so>) {
210 unlink $_;
211 }
212  
213 finddepth(sub {
214 return unless /\.so$/;
215 my $file = "$File::Find::dir/$_";
216 symlink $file, "$install/$_";
217 }, $source);
218 }
219  
220 sub unlink {
221 my $file = shift;
222  
223 if (-e $file) {
224 print "unlink $file\n";
225 }
226 else {
227 print "$file does not exist\n";
228 }
229 CORE::unlink($file);
230 }
231  
232 sub symlink {
233 my($from, $to) = @_;
234 print "symlink $from => $to\n";
235 unless (-e $from) {
236 print "source $from does not exist\n";
237 }
238 my $base = dirname $to;
239 unless (-e $base) {
240 print "target dir $base does not exist\n";
241 }
242 CORE::symlink($from, $to) or die $!;
243 }
244  
245 sub cvs {
246 my $self = shift;
247  
248 my $cmd = "cvs -d $self->{cvsroot} @_";
249  
250 if (DRYRUN) {
251 info "$cmd";
252 }
253 else {
254 system $cmd;
255 }
256 }
257  
258 my %cvs_names = (
259 '2.0' => 'httpd-2.0',
260 '1.3' => 'apache-1.3',
261 );
262  
263 my %cvs_snames = (
264 '2.0' => 'httpd',
265 '1.3' => 'apache',
266 );
267  
268 sub cvs_up {
269 my($self, $version) = @_;
270  
271 my $name = $cvs_names{$version};
272  
273 my $dir = $self->srcdir($version);
274  
275 if ($self->{cvsroot} eq 'anon') {
276 $self->{cvsroot} = ':pserver:anoncvs@cvs.apache.org:/home/cvspublic';
277 unless (-d $dir) {
278 #XXX do something better than doesn't require prompt if
279 #we already have an entry in ~/.cvspass
280 #$self->cvs('login');
281  
282 warning "may need to run the following command ",
283 "(password is 'anoncvs')";
284 warning "cvs -d $self->{cvsroot} login";
285 }
286 }
287  
288 if (-d $dir) {
289 chdir $dir;
290 $self->cvs(up => "-dP $self->{cvstag}");
291 return;
292 }
293  
294 my $co = checkout($name);
295 $self->$co($name, $dir);
296  
297 my $post = post_checkout($name);
298 $self->$post($name, $dir);
299 }
300  
301 sub checkout_httpd_2_0 {
302 my($self, $name, $dir) = @_;
303  
304 my $tag = $self->{cvstag};
305  
306 $self->cvs(co => "-d $dir $tag $name");
307 chdir "$dir/srclib";
308 $self->cvs(co => "$tag apr apr-util");
309 }
310  
311 sub checkout_apache_1_3 {
312 my($self, $name, $dir) = @_;
313  
314 $self->cvs(co => "-d $dir $self->{cvstag} $name");
315 }
316  
317 sub post_checkout_httpd_2_0 {
318 my($self, $name, $dir) = @_;
319 }
320  
321 sub post_checkout_apache_1_3 {
322 }
323  
324 sub canon {
325 my $name = shift;
326 return $name unless $name;
327 $name =~ s/[.-]/_/g;
328 $name;
329 }
330  
331 sub checkout {
332 my $name = canon(shift);
333 \&{"checkout_$name"};
334 }
335  
336 sub post_checkout {
337 my $name = canon(shift);
338 \&{"post_checkout_$name"};
339 }
340  
341 sub cvs_update {
342 my $self = shift;
343  
344 my $cvs = shift || $self->{cvs};
345  
346 chdir $self->{src};
347  
348 for my $name (@$cvs) {
349 $self->cvs_up($name);
350 }
351 }
352  
353 sub merge_build {
354 my($self, $version, $builds, $configs) = @_;
355  
356 my $b = {
357 cflags => $builds{default}->{cflags},
358 config => [ @{ $builds{default}->{config}->{$version} } ],
359 };
360  
361 for my $name (@$builds) {
362 next if $name eq 'default'; #already have this
363  
364 if (my $flags = $builds{$name}->{cflags}) {
365 $b->{cflags} .= " $flags";
366 }
367 if (my $cfg = $builds{$name}->{config}) {
368 if (my $vcfg = $cfg->{$version}) {
369 push @{ $b->{config} }, @$vcfg;
370 }
371 }
372 }
373  
374 for my $name (@$configs) {
375 my $cfg = $configs{$name}->{$version};
376 next unless $cfg;
377 push @{ $b->{config} }, @$cfg;
378 }
379  
380 if (my $ex = $self->{extra_config}->{$version}) {
381 push @{ $b->{config} }, @$ex;
382 }
383  
384 if (my $ex = $self->{extra_cflags}->{$version}) {
385 $b->{config} .= " $ex";
386 }
387  
388 $b;
389 }
390  
391 my @srclib_dirs = qw(
392 apr apr-util apr-util/xml/expat pcre
393 );
394  
395 sub install_name {
396 my($self, $builds, $configs, $mpm) = @_;
397  
398 return $self->{name} if $self->{name};
399  
400 my $name = join '-', $mpm, @$builds, @$configs;
401  
402 if (my $tag = $self->cvs_name) {
403 $name .= "-$tag";
404 }
405  
406 $name;
407 }
408  
409 #currently the httpd-2.0 build does not properly support static linking
410 #of ssl libs, force the issue
411 sub add_ssl_libs {
412 my $self = shift;
413  
414 my $ssldir = $self->{ssldir};
415  
416 return unless $ssldir and -d $ssldir;
417  
418 my $name = $self->{current_install_name};
419  
420 my $ssl_mod = "$name/modules/ssl";
421 info "editing $ssl_mod/modules.mk";
422  
423 if (DRYRUN) {
424 return;
425 }
426  
427 my $ssl_mk = "$self->{build}/$ssl_mod/modules.mk";
428  
429 open my $fh, $ssl_mk or die "open $ssl_mk: $!";
430 my @lines = <$fh>;
431 close $fh;
432  
433 for (@lines) {
434 next unless /SH_LINK/;
435 chomp;
436 $_ .= " -L$ssldir -lssl -lcrypto\n";
437 info 'added ssl libs';
438 last;
439 }
440  
441 open $fh, '>', $ssl_mk or die $!;
442 print $fh join "\n", @lines;
443 close $fh;
444 }
445  
446 sub cvs_name {
447 my $self = shift;
448  
449 if (my $tag = $self->{cvstag}) {
450 $tag =~ s/^-[DAr]//;
451 $tag =~ s/\"//g;
452 $tag =~ s,[/ :],_,g; #-D"03/29/02 07:00pm"
453 return $tag;
454 }
455  
456 return "";
457 }
458  
459 sub srcdir {
460 my($self, $src) = @_;
461  
462 my $prefix = "";
463 if ($src =~ s/^(apache|httpd)-//) {
464 $prefix = $1;
465 }
466 else {
467 $prefix = $cvs_snames{$src};
468 }
469  
470 if ($src =~ /^\d\.\d$/) {
471 #release version will be \d\.\d\.\d+
472 if (my $tag = $self->cvs_name) {
473 $src .= "-$tag";
474 }
475 $src .= '-cvs';
476 }
477  
478 join '-', $prefix, $src;
479 }
480  
481 sub configure_httpd_2_0 {
482 my($self, $src, $builds, $configs, $mpm) = @_;
483  
484 $src = $self->srcdir($src);
485  
486 chdir $self->{build};
487  
488 my $name = $self->install_name($builds, $configs, $mpm);
489  
490 $self->{current_install_name} = $name;
491  
492 $self->{builds}->{$name} = 1;
493  
494 if ($self->{fresh}) {
495 rmtree($name);
496 }
497 else {
498 if (-e "$name/.DONE") {
499 warning "$name already configured";
500 warning "rm $name/.DONE to force";
501 return;
502 }
503 }
504  
505 my $build = $self->merge_build('httpd-2.0', $builds, $configs);
506  
507 $ENV{CFLAGS} = $build->{cflags};
508 info "CFLAGS=$ENV{CFLAGS}";
509  
510 my $prefix = "$self->{install}/$name";
511  
512 rmtree($prefix) if $self->{fresh};
513  
514 my $source = "$self->{src}/$src";
515  
516 my @args = ("--prefix=$prefix",
517 "--with-mpm=$mpm",
518 "--srcdir=$source",
519 @{ $build->{config} });
520  
521 chdir $source;
522 system "./buildconf";
523  
524 my $cmd = "$source/configure @args";
525  
526 chdir $self->{build};
527  
528 mkpath($name);
529 chdir $name;
530  
531 for my $dir (@srclib_dirs) {
532 mkpath("srclib/$dir");
533 }
534  
535 for my $dir (qw(build docs/conf)) {
536 mkpath($dir);
537 }
538  
539 system $cmd;
540  
541 open FH, ">.DONE" or die "open .DONE: $!";
542 print FH scalar localtime;
543 close FH;
544  
545 chdir $self->{prefix};
546  
547 $self->add_ssl_libs;
548 }
549  
550 sub make {
551 my($self, @cmds) = @_;
552  
553 push @cmds, 'all' unless @cmds;
554  
555 for my $name (keys %{ $self->{builds} }) {
556 chdir "$self->{build}/$name";
557 for my $cmd (@cmds) {
558 system "$self->{make} $cmd";
559 }
560 }
561 }
562  
563 sub system {
564 my $cmd = "@_";
565  
566 info $cmd;
567 return if DRYRUN;
568  
569 unless (CORE::system($cmd) == 0) {
570 my $status = $? >> 8;
571 die "system $cmd failed (exit status=$status)";
572 }
573 }
574  
575 sub chdir {
576 my $dir = shift;
577 info "chdir $dir";
578 CORE::chdir $dir;
579 }
580  
581 sub mkpath {
582 my $dir = shift;
583  
584 return if -d $dir;
585 info "mkpath $dir";
586  
587 return if DRYRUN;
588 File::Path::mkpath([$dir], 1, 0755);
589 }
590  
591 sub rmtree {
592 my $dir = shift;
593  
594 return unless -d $dir;
595 info "rmtree $dir";
596  
597 return if DRYRUN;
598 File::Path::rmtree([$dir], 1, 1);
599 }
600  
601 sub generate_script {
602 my($class, $file) = @_;
603  
604 $file ||= catfile 't', 'BUILD';
605  
606 my $content = join '', <DATA>;
607  
608 Apache::Test::basic_config()->write_perlscript($file, $content);
609 }
610  
611 unless (caller) {
612 $INC{'Apache/TestBuild.pm'} = __FILE__;
613 eval join '', <DATA>;
614 die $@ if $@;
615 }
616  
617 1;
618 __DATA__