home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / TestConfigC.pm < prev    next >
Encoding:
Perl POD Document  |  2004-03-22  |  11.4 KB  |  474 lines

  1. # Copyright 2001-2004 The Apache Software Foundation
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. #     http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. package Apache::TestConfig; #not TestConfigC on purpose
  16.  
  17. use strict;
  18. use warnings FATAL => 'all';
  19.  
  20. use Config;
  21. use Apache::TestConfig ();
  22. use Apache::TestConfigPerl ();
  23. use Apache::TestTrace;
  24. use File::Find qw(finddepth);
  25.  
  26. sub cmodule_find {
  27.     my($self, $mod) = @_;
  28.  
  29.     return unless $mod =~ /^mod_(\w+)\.c$/;
  30.     my $sym = $1;
  31.  
  32.     my $dir = $File::Find::dir;
  33.     my $file = catfile $dir, $mod;
  34.  
  35.     unless ($self->{APXS}) {
  36.         $self->{cmodules_disabled}->{$mod} = "no apxs configured";
  37.         return;
  38.     }
  39.  
  40.     my $fh = Symbol::gensym();
  41.     open $fh, $file or die "open $file: $!";
  42.     my $v = <$fh>;
  43.     if ($v =~ /^\#define\s+HTTPD_TEST_REQUIRE_APACHE\s+(\d+)\s*$/) {
  44.         #define HTTPD_TEST_REQUIRE_APACHE 1
  45.         unless ($self->{server}->{rev} == $1) {
  46.             my $reason = "requires Apache version $1";
  47.             $self->{cmodules_disabled}->{$mod} = $reason;
  48.             notice "$mod $reason, skipping.";
  49.             return;
  50.         }
  51.     }
  52.     elsif ($v =~ /^\#define\s+HTTPD_TEST_REQUIRE_APACHE\s+(\d\.\d+(\.\d+)?)/) {
  53.         #define HTTPD_TEST_REQUIRE_APACHE 2.1
  54.         my $wanted = $1;
  55.         (my $current) = $self->{server}->{version} =~ m:^Apache/(\d\.\d+\.\d+):;
  56.  
  57.         if (Apache::Test::normalize_vstring($current) < 
  58.             Apache::Test::normalize_vstring($wanted)) {
  59.             my $reason = "requires Apache version $wanted";
  60.             $self->{cmodules_disabled}->{$mod} = $reason;
  61.             notice "$mod $reason, skipping.";
  62.             return;
  63.         }
  64.     }
  65.     close $fh;
  66.  
  67.     push @{ $self->{cmodules} }, {
  68.         name => "mod_$sym",
  69.         sym => "${sym}_module",
  70.         dir  => $dir,
  71.         subdir => basename $dir,
  72.     };
  73. }
  74.  
  75. sub cmodules_configure {
  76.     my($self, $dir) = @_;
  77.  
  78.     $self->{cmodules_disabled} = {}; #for have_module to check
  79.  
  80.     $dir ||= catfile $self->{vars}->{top_dir}, 'c-modules';
  81.  
  82.     unless (-d $dir) {
  83.         return;
  84.     }
  85.  
  86.     $self->{cmodules_dir} = $dir;
  87.  
  88.     finddepth(sub { cmodule_find($self, $_) }, $dir);
  89.  
  90.     unless ($self->{APXS}) {
  91.         warning "cannot build c-modules without apxs";
  92.         return;
  93.     }
  94.  
  95.     $self->cmodules_generate_include;
  96.     $self->cmodules_write_makefiles;
  97.     $self->cmodules_compile;
  98.     $self->cmodules_httpd_conf;
  99. }
  100.  
  101. sub cmodules_makefile_vars {
  102.     return <<EOF;
  103. MAKE = $Config{make}
  104. EOF
  105. }
  106.  
  107. my %lib_dir = Apache::TestConfig::WIN32
  108.     ? (1 => "", 2 => "")
  109.     : (1 => "", 2 => ".libs/");
  110.  
  111. sub cmodules_build_so {
  112.     my($self, $name) = @_;
  113.     $name = "mod_$name" unless $name =~ /^mod_/;
  114.     my $libdir = $self->server->version_of(\%lib_dir);
  115.     my $lib = "$libdir$name.so";
  116. }
  117.  
  118. sub cmodules_write_makefiles {
  119.     my $self = shift;
  120.  
  121.     my $modules = $self->{cmodules};
  122.  
  123.     for (@$modules) {
  124.         $self->cmodules_write_makefile($_);
  125.     }
  126.  
  127.     my $file = catfile $self->{cmodules_dir}, 'Makefile';
  128.     my $fh = Symbol::gensym();
  129.     open $fh, ">$file" or die "open $file: $!";
  130.  
  131.     print $fh $self->cmodules_makefile_vars;
  132.  
  133.     my @dirs = map { $_->{subdir} } @$modules;
  134.  
  135.     my @targets = qw(clean);
  136.     my @libs;
  137.  
  138.     for my $dir (@dirs) {
  139.         for my $targ (@targets) {
  140.             print $fh "$dir-$targ:\n\tcd $dir && \$(MAKE) $targ\n\n";
  141.         }
  142.  
  143.         my $lib = $self->cmodules_build_so($dir);
  144.         my $cfile = "$dir/mod_$dir.c";
  145.         push @libs, "$dir/$lib";
  146.         print $fh "$libs[-1]: $cfile\n\tcd $dir && \$(MAKE) $lib\n\n";
  147.     }
  148.  
  149.     for my $targ (@targets) {
  150.         print $fh "$targ: ", (map { "$_-$targ " } @dirs), "\n\n";
  151.     }
  152.  
  153.     print $fh "all: @libs\n\n";
  154.  
  155.     close $fh or die "close $file: $!";
  156. }
  157.  
  158. sub cmodules_write_makefile {
  159.     my ($self, $mod) = @_;
  160.     my $write = \&{"cmodules_write_makefile_$^O"};
  161.     $write = \&cmodules_write_makefile_default unless defined &$write;
  162.     $write->($self, $mod);
  163. }
  164.  
  165. sub cmodules_write_makefile_default {
  166.     my($self, $mod) = @_;
  167.  
  168.     my $dversion = $self->server->dversion;
  169.     my $name = $mod->{name};
  170.     my $makefile = catfile $mod->{dir}, 'Makefile';
  171.     debug "writing $makefile";
  172.  
  173.     my $lib = $self->cmodules_build_so($name);
  174.  
  175.     my $fh = Symbol::gensym();
  176.     open $fh, ">$makefile" or die "open $makefile: $!";
  177.  
  178.     print $fh <<EOF;
  179. APXS=$self->{APXS}
  180. all: $lib
  181.  
  182. $lib: $name.c
  183.     \$(APXS) $dversion -I$self->{cmodules_dir} -c $name.c
  184.  
  185. clean:
  186.     -rm -rf $name.o $name.lo $name.slo $name.la .libs
  187. EOF
  188.  
  189.     close $fh or die "close $makefile: $!";
  190. }
  191.  
  192. sub cmodules_write_makefile_aix {
  193.     my($self, $mod) = @_;
  194.  
  195.     my $dversion = $self->server->dversion;
  196.     my $name = $mod->{name};
  197.     my $makefile = catfile $mod->{dir}, 'Makefile';
  198.     my $apxsflags = '';
  199.  
  200.     #
  201.     # Only do this for Apache 1.*
  202.     #
  203.     if ($self->server->{rev} == 1) {
  204.         $apxsflags = "-Wl,-bE:$name.exp";
  205.         my $expfile = catfile $mod->{dir}, "$name.exp";
  206.         if (! -f $expfile) {
  207.             my $fh = Symbol::gensym();
  208.             $name =~ /^mod_(\w+)(?:\.c)?$/;
  209.             my $sym = $1 . '_module';
  210.             open $fh, ">$expfile" or die "open $expfile: $!";
  211.             print $fh "$sym\n";
  212.             close $fh;
  213.         }
  214.     }
  215.     debug "writing $makefile";
  216.  
  217.     my $lib = $self->cmodules_build_so($name);
  218.  
  219.     my $fh = Symbol::gensym();
  220.     open $fh, ">$makefile" or die "open $makefile: $!";
  221.  
  222.     print $fh <<EOF;
  223. APXS=$self->{APXS}
  224. APXSFLAGS=$apxsflags
  225. all: $lib
  226.  
  227. $lib: $name.c
  228.     \$(APXS) $dversion -I$self->{cmodules_dir} \$(APXSFLAGS) -c $name.c
  229.  
  230. clean:
  231.     -rm -rf $name.o $name.lo $name.slo $name.la .libs
  232. EOF
  233.  
  234.     close $fh or die "close $makefile: $!";
  235. }
  236.  
  237. sub cmodules_write_makefile_MSWin32 {
  238.     my($self, $mod) = @_;
  239.  
  240.     my $dversion = $self->server->dversion;
  241.     my $name = $mod->{name};
  242.     my $makefile = "$mod->{dir}/Makefile";
  243.     debug "writing $makefile";
  244.     my $extras = '';
  245.  
  246.     my $lib = $self->cmodules_build_so($name);
  247.     $extras = ' -llibhttpd -p ' if ($self->server->{rev} != 1);
  248.     my $goners = join ' ', (map {$name . '.' . $_} qw(exp lib so lo));
  249.  
  250.     my $fh = Symbol::gensym();
  251.     open $fh, ">$makefile" or die "open $makefile: $!";
  252.  
  253.     print $fh <<EOF;
  254. APXS=$self->{APXS}
  255. all: $lib
  256.  
  257. $lib: $name.c
  258.     \$(APXS) $dversion -I$self->{cmodules_dir} $extras -c $name.c
  259.  
  260. clean:
  261.     -erase $goners
  262. EOF
  263.  
  264.     close $fh or die "close $makefile: $!";
  265. }
  266.  
  267. sub cmodules_make {
  268.     my $self = shift;
  269.     my $targ = shift || 'all';
  270.  
  271.     my $cmd = "cd $self->{cmodules_dir} && $Config{make} $targ";
  272.     debug $cmd;
  273.     system $cmd;
  274.     if ($?) {
  275.         die "Failed to build c-modules";
  276.     }
  277. }
  278.  
  279. sub cmodules_compile {
  280.     shift->cmodules_make('all');
  281. }
  282.  
  283. sub cmodules_httpd_conf {
  284.     my $self = shift;
  285.  
  286.     my @args;
  287.  
  288.     for my $mod (@{ $self->{cmodules} }) {
  289.         my $dir = $mod->{dir};
  290.         my $lib = $self->cmodules_build_so($mod->{name});
  291.         my $so  = "$dir/$lib";
  292.  
  293.         next unless -e $so;
  294.  
  295.         $self->preamble(LoadModule => "$mod->{sym} $so");
  296.  
  297.         my $cname = "$mod->{name}.c";
  298.         my $cfile = "$dir/$cname";
  299.         $self->{modules}->{$cname} = 1;
  300.  
  301.         $self->add_module_config($cfile, \@args);
  302.     }
  303.  
  304.     $self->postamble(\@args) if @args;
  305. }
  306.  
  307. sub cmodules_clean {
  308.     my $self = shift;
  309.  
  310.     my $dir = $self->{cmodules_dir};
  311.     return unless $dir and -e "$dir/Makefile";
  312.  
  313.     unless ($self->{clean_level} > 1) {
  314.         #skip t/TEST -conf
  315.         warning "skipping rebuild of c-modules; run t/TEST -clean to force";
  316.         return;
  317.     }
  318.  
  319.     $self->cmodules_make('clean');
  320.  
  321.     for my $mod (@{ $self->{cmodules} }) {
  322.         my $makefile = "$mod->{dir}/Makefile";
  323.         debug "unlink $makefile";
  324.         unlink $makefile;
  325.     }
  326.  
  327.     unlink "$dir/Makefile";
  328. }
  329.  
  330. #try making it easier for test modules to compile with both 1.x and 2.x
  331. sub cmodule_define_name {
  332.     my $name = shift;
  333.     $name eq 'NULL' ? $name : "APACHE_HTTPD_TEST_\U$name";
  334. }
  335.  
  336. sub cmodule_define {
  337.     my $hook = cmodule_define_name(@_);
  338.     "#ifndef $hook\n#define $hook NULL\n#endif\n";
  339. }
  340.  
  341. my @cmodule_config_names = qw(per_dir_create per_dir_merge
  342.                               per_srv_create per_srv_merge
  343.                               commands);
  344.  
  345. my @cmodule_config_defines = map {
  346.     cmodule_define($_);
  347. } @cmodule_config_names;
  348.  
  349. my $cmodule_config_hooks = join ",\n    ", map {
  350.     cmodule_define_name($_);
  351. } @cmodule_config_names;
  352.  
  353. my @cmodule_phases = qw(post_read_request translate_name header_parser
  354.                         access_checker check_user_id auth_checker
  355.                         type_checker fixups handler log_transaction
  356.                         child_init);
  357.  
  358. my $cmodule_hooks_1 = join ",\n    ", map {
  359.     cmodule_define_name($_);
  360. } qw(translate_name check_user_id auth_checker access_checker
  361.      type_checker fixups log_transaction header_parser
  362.      child_init NULL post_read_request);
  363.  
  364. my $cmodule_template_1 = <<"EOF",
  365. static const handler_rec name ## _handlers[] =
  366. {
  367.     {#name, APACHE_HTTPD_TEST_HANDLER}, /* ok if handler is NULL */
  368.     {NULL}
  369. };
  370.  
  371. module MODULE_VAR_EXPORT name ## _module =
  372. {
  373.     STANDARD_MODULE_STUFF,
  374.     NULL,            /* initializer */
  375.     $cmodule_config_hooks,
  376.     name ## _handlers,            /* handlers */
  377.     $cmodule_hooks_1
  378. }
  379. EOF
  380.  
  381. my @cmodule_hooks = map {
  382.     my $hook = cmodule_define_name($_);
  383.     <<EOF;
  384.     if ($hook != NULL)
  385.         ap_hook_$_($hook,
  386.                    NULL, NULL,
  387.                    APACHE_HTTPD_TEST_HOOK_ORDER);
  388. EOF
  389. } @cmodule_phases;
  390.  
  391. my @cmodule_hook_defines = map {
  392.     cmodule_define($_);
  393. } @cmodule_phases;
  394.  
  395. my $cmodule_template_2 = <<"EOF";
  396. static void name ## _register_hooks(apr_pool_t *p)
  397. {
  398. @cmodule_hooks
  399. }
  400.  
  401. module AP_MODULE_DECLARE_DATA name ## _module = {
  402.     STANDARD20_MODULE_STUFF,
  403.     $cmodule_config_hooks,
  404.     name ## _register_hooks, /* register hooks */
  405. }
  406. EOF
  407.  
  408. my %cmodule_templates = (1 => $cmodule_template_1, 2 => $cmodule_template_2);
  409.  
  410. sub cmodules_module_template {
  411.     my $self = shift;
  412.     my $template = $self->server->version_of(\%cmodule_templates);
  413.     chomp $template;
  414.  
  415.     $template =~ s,$, \\,mg;
  416.     $template =~ s, \\$,,s;
  417.  
  418.     local $" = ', ';
  419.  
  420.     return <<EOF;
  421. #define APACHE_HTTPD_TEST_MODULE(name) \\
  422.     $template
  423. EOF
  424. }
  425.  
  426. sub cmodules_generate_include {
  427.     my $self = shift;
  428.  
  429.     my $file = "$self->{cmodules_dir}/apache_httpd_test.h";
  430.     my $fh = $self->genfile($file);
  431.  
  432.     while (read Apache::TestConfigC::DATA, my $buf, 1024) {
  433.         print $fh $buf;
  434.     }
  435.  
  436.     print $fh @cmodule_hook_defines, @cmodule_config_defines;
  437.  
  438.     print $fh $self->cmodules_module_template;
  439.  
  440.     close $fh;
  441. }
  442.  
  443. package Apache::TestConfigC; #Apache/TestConfig.pm also has __DATA__
  444. 1;
  445. __DATA__
  446. #ifndef APACHE_HTTPD_TEST_H
  447. #define APACHE_HTTPD_TEST_H
  448.  
  449. /* headers present in both 1.x and 2.x */
  450. #include "httpd.h"
  451. #include "http_config.h"
  452. #include "http_protocol.h"
  453. #include "http_request.h"
  454. #include "http_log.h"
  455. #include "http_main.h"
  456. #include "http_core.h"
  457. #include "ap_config.h"
  458.  
  459. #ifdef APACHE1
  460. #define AP_METHOD_BIT  1
  461. typedef size_t apr_size_t;
  462. typedef array_header apr_array_header_t;
  463. #endif /* APACHE1 */
  464.  
  465. #ifdef APACHE2
  466. #ifndef APACHE_HTTPD_TEST_HOOK_ORDER
  467. #define APACHE_HTTPD_TEST_HOOK_ORDER APR_HOOK_MIDDLE
  468. #endif
  469. #include "ap_compat.h"
  470. #endif /* APACHE2 */
  471.  
  472. #endif /* APACHE_HTTPD_TEST_H */
  473.  
  474.