home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / TestConfigC.pm < prev    next >
Encoding:
Perl POD Document  |  2003-11-12  |  9.4 KB  |  397 lines

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