3 # Licensed to the Apache Software Foundation (ASF) under one or more
4 # contributor license agreements. See the NOTICE file distributed with
5 # this work for additional information regarding copyright ownership.
6 # The ASF licenses this file to You under the Apache License, Version 2.0
7 # (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
28 my $installbuilddir = "/bottlenecks/rubbos/app/apache2/build";
29 get_config_vars("$installbuilddir/config_vars.mk",\%config_vars);
31 # read the configuration variables once
33 my $prefix = get_vars("prefix");
34 my $CFG_PREFIX = $prefix;
35 my $exec_prefix = get_vars("exec_prefix");
36 my $datadir = get_vars("datadir");
37 my $localstatedir = get_vars("localstatedir");
38 my $CFG_TARGET = get_vars("progname");
39 my $CFG_SYSCONFDIR = get_vars("sysconfdir");
40 my $CFG_CFLAGS = join ' ', map { get_vars($_) }
41 qw(SHLTCFLAGS CFLAGS NOTEST_CPPFLAGS EXTRA_CPPFLAGS EXTRA_CFLAGS);
42 my $includedir = get_vars("includedir");
43 my $CFG_INCLUDEDIR = eval qq("$includedir");
44 my $CFG_CC = get_vars("CC");
45 my $libexecdir = get_vars("libexecdir");
46 my $CFG_LIBEXECDIR = eval qq("$libexecdir");
47 my $sbindir = get_vars("sbindir");
48 my $CFG_SBINDIR = eval qq("$sbindir");
49 my $ltflags = $ENV{'LTFLAGS'};
50 $ltflags or $ltflags = "--silent";
52 my %internal_vars = map {$_ => 1}
53 qw(TARGET CC CFLAGS CFLAGS_SHLIB LD_SHLIB LDFLAGS_SHLIB LIBS_SHLIB
54 PREFIX SBINDIR INCLUDEDIR LIBEXECDIR SYSCONFDIR);
57 ## parse argument line
60 # defaults for parameters
79 # this subroutine is derived from Perl's getopts.pl with the enhancement of
80 # the "+" metacharacter at the format string to allow a list to be built by
81 # subsequent occurrences of the same option.
83 my ($argumentative, @ARGV) = @_;
88 my @args = split / */, $argumentative;
89 while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
90 my ($first, $rest) = ($1,$2);
95 my $pos = index($argumentative,$first);
97 if ($pos < $#args && $args[$pos+1] eq ':') {
101 error("Incomplete option: $first (needs an argument)");
104 $rest = shift(@ARGV);
106 eval "\$opt_$first = \$rest;";
108 elsif ($pos < $#args && $args[$pos+1] eq '+') {
112 error("Incomplete option: $first (needs an argument)");
115 $rest = shift(@ARGV);
117 eval "push(\@opt_$first, \$rest);";
120 eval "\$opt_$first = 1";
130 error("Unknown option: $first");
140 return ($errs == 0, @ARGV);
144 print STDERR "Usage: apxs -g [-S <var>=<val>] -n <modname>\n";
145 print STDERR " apxs -q [-S <var>=<val>] <query> ...\n";
146 print STDERR " apxs -c [-S <var>=<val>] [-o <dsofile>] [-D <name>[=<value>]]\n";
147 print STDERR " [-I <incdir>] [-L <libdir>] [-l <libname>] [-Wc,<flags>]\n";
148 print STDERR " [-Wl,<flags>] [-p] <files> ...\n";
149 print STDERR " apxs -i [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
150 print STDERR " apxs -e [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
156 ($rc, @ARGV) = &Getopts("qn:gco:I+D+L+l+W+S+eiaAp", @ARGV);
157 &usage if ($rc == 0);
158 &usage if ($#ARGV == -1 and not $opt_g);
159 &usage if (not $opt_q and not ($opt_g and $opt_n) and not $opt_i and not $opt_c and not $opt_e);
163 my $name = 'unknown';
164 $name = $opt_n if ($opt_n ne '');
168 foreach $opt_S (@opt_S) {
169 if ($opt_S =~ m/^([^=]+)=(.*)$/) {
172 my $oldval = eval "\$CFG_$var";
174 unless ($var and $oldval) {
175 print STDERR "apxs:Error: no config variable $var\n";
179 eval "\$CFG_${var}=\"${val}\"";
181 print STDERR "apxs:Error: malformatted -S option\n";
188 ## Initial shared object support check
190 my $httpd = get_vars("sbindir") . "/" . get_vars("progname");
191 $httpd = eval qq("$httpd");
192 $httpd = eval qq("$httpd");
193 my $envvars = get_vars("sbindir") . "/envvars";
194 $envvars = eval qq("$envvars");
195 $envvars = eval qq("$envvars");
197 #allow apxs to be run from the source tree, before installation
198 if ($0 =~ m:support/apxs$:) {
199 ($httpd = $0) =~ s:support/apxs$::;
202 unless (-x "$httpd") {
203 error("$httpd not found or not executable");
207 unless (grep /mod_so/, `. $envvars && $httpd -l`) {
208 error("Sorry, no shared object support for Apache");
209 error("available under your platform. Make sure");
210 error("the Apache module mod_so is compiled into");
211 error("your server binary `$httpd'.");
216 my ($file, $rh_config) = @_;
218 open IN, $file or die "cannot open $file: $!";
220 if (/^\s*(.*?)\s*=\s*(.*)$/){
221 $rh_config->{$1} = $2;
232 if (exists $config_vars{$arg} or exists $config_vars{lc $arg}) {
233 my $val = exists $config_vars{$arg}
235 : $config_vars{lc $arg};
237 $result .= eval "qq($val)" if defined $val;
242 if (exists $internal_vars{$arg} or exists $internal_vars{lc $arg}) {
243 my $val = exists $internal_vars{$arg} ? $arg : lc $arg;
244 $val = eval "\$CFG_$val";
245 $result .= eval "qq($val)" if defined $val;
250 error("Invalid query string `$arg'");
264 # helper function for executing a list of
265 # system command with return code checks
270 foreach $cmd (@cmds) {
274 error(sprintf "Command failed with rc=%d\n", $rc << 8);
282 ## SAMPLE MODULE SOURCE GENERATION
286 error("Directory `$name' already exists. Remove first");
290 my $data = join('', <DATA>);
291 $data =~ s|%NAME%|$name|sg;
292 $data =~ s|%TARGET%|$CFG_TARGET|sg;
293 $data =~ s|%PREFIX%|$prefix|sg;
294 $data =~ s|%INSTALLBUILDDIR%|$installbuilddir|sg;
296 my ($mkf, $mods, $src) = ($data =~ m|^(.+)-=#=-\n(.+)-=#=-\n(.+)|s);
298 notice("Creating [DIR] $name");
299 system("mkdir $name");
300 notice("Creating [FILE] $name/Makefile");
301 open(FP, ">${name}/Makefile") || die;
304 notice("Creating [FILE] $name/modules.mk");
305 open(FP, ">${name}/modules.mk") || die;
308 notice("Creating [FILE] $name/mod_$name.c");
309 open(FP, ">${name}/mod_${name}.c") || die;
312 notice("Creating [FILE] $name/.deps");
313 system("touch ${name}/.deps");
323 my $result = get_vars(@args);
327 my $apr_bindir = get_vars("APR_BINDIR");
329 if (! -x "$apr_bindir/apr-config") {
330 error("$apr_bindir/apr-config not found!");
334 my $apu_bindir = get_vars("APU_BINDIR");
336 if (! -x "$apu_bindir/apu-config") {
337 error("$apu_bindir/apu-config not found!");
341 my $libtool = `$apr_bindir/apr-config --installbuilddir`;
343 $libtool = "$libtool/libtool";
345 my $apr_includedir = `$apr_bindir/apr-config --includes`;
346 chomp($apr_includedir);
347 my $apu_includedir = `$apu_bindir/apu-config --includes`;
348 chomp($apu_includedir);
352 ## SHARED OBJECT COMPILATION
355 # split files into sources and objects
368 # determine output file
372 $dso_file = $srcs[0];
373 $dso_file =~ s|\.[^.]+$|.la|;
375 elsif ($#objs > -1) {
376 $dso_file = $objs[0];
377 $dso_file =~ s|\.[^.]+$|.la|;
380 $dso_file = "mod_unknown.la";
385 $dso_file =~ s|\.[^.]+$|.la|;
388 # create compilation commands
391 my ($opt_Wc, $opt_I, $opt_D);
392 foreach $opt_Wc (@opt_W) {
393 $opt .= "$1 " if ($opt_Wc =~ m|^\s*c,(.*)$|);
395 foreach $opt_I (@opt_I) {
398 foreach $opt_D (@opt_D) {
401 my $cflags = "$CFG_CFLAGS";
406 $slo =~ s|\.c$|.slo|;
413 push(@cmds, "$libtool $ltflags --mode=compile $CFG_CC $cflags -I$CFG_INCLUDEDIR $apr_includedir $apu_includedir $opt -c -o $lo $s && touch $slo");
417 # create link command
423 my ($opt_Wl, $opt_L, $opt_l);
425 foreach $opt_Wl (@opt_W) {
426 $opt .= "$1 " if ($opt_Wl =~ m|^\s*l,(.*)$|);
428 foreach $opt_L (@opt_L) {
431 foreach $opt_l (@opt_l) {
437 my $apr_libs=`$apr_bindir/apr-config --ldflags --link-libtool --libs`;
439 my $apu_libs=`$apu_bindir/apu-config --ldflags --link-libtool --libs`;
442 $opt .= " ".$apu_libs." ".$apr_libs;
445 my $apr_ldflags=`$apr_bindir/apr-config --ldflags`;
447 $opt .= " -rpath $CFG_LIBEXECDIR -module -avoid-version $apr_ldflags";
450 push(@cmds, "$libtool $ltflags --mode=link $CFG_CC -o $dso_file $opt $lo");
452 # execute the commands
453 &execute_cmds(@cmds);
455 # allow one-step compilation and installation
456 if ($opt_i or $opt_e) {
457 @args = ( $dso_file );
461 if ($opt_i or $opt_e) {
463 ## SHARED OBJECT INSTALLATION
466 # determine installation commands
467 # and corresponding LoadModule/AddModule directives
473 if ($f !~ m#(\.so$|\.la$)#) {
474 error("file $f is not a shared object");
478 $t =~ s|^.+/([^/]+)$|$1|;
481 push(@cmds, "$installbuilddir/instdso.sh SH_LIBTOOL='" .
482 "$libtool' $f $CFG_LIBEXECDIR");
483 push(@cmds, "chmod 755 $CFG_LIBEXECDIR/$t");
486 # determine module symbolname and filename
488 if ($name eq 'unknown') {
491 $base =~ s|\.[^.]+$||;
493 open(FP, "<$base.c");
494 my $content = join('', <FP>);
496 if ($content =~ m|.*module\s+(?:AP_MODULE_DECLARE_DATA\s+)?([a-zA-Z0-9_]+)_module\s*=\s*.*|s) {
498 $filename = "$base.c";
499 $filename =~ s|^[^/]+/||;
503 if ($base =~ m|.*mod_([a-zA-Z0-9_]+)\..+|) {
506 $filename =~ s|^[^/]+/||;
510 error("Sorry, cannot determine bootstrap symbol name");
511 error("Please specify one with option `-n'");
515 if ($filename eq '') {
516 $filename = "mod_${name}.c";
518 my $dir = $CFG_LIBEXECDIR;
519 $dir =~ s|^$CFG_PREFIX/?||;
522 push(@lmd, sprintf("LoadModule %-18s %s", "${name}_module", "$dir$t"));
523 push(@amd, sprintf("AddModule %s", $filename));
526 # execute the commands
527 &execute_cmds(@cmds);
529 # activate module via LoadModule/AddModule directive
530 if ($opt_a or $opt_A) {
531 if (not -f "$CFG_SYSCONFDIR/$CFG_TARGET.conf") {
532 error("Config file $CFG_SYSCONFDIR/$CFG_TARGET.conf not found");
536 open(FP, "<$CFG_SYSCONFDIR/$CFG_TARGET.conf") || die;
537 my $content = join('', <FP>);
540 if ($content !~ m|\n#?\s*LoadModule\s+|) {
541 error("Activation failed for custom $CFG_SYSCONFDIR/$CFG_TARGET.conf file.");
542 error("At least one `LoadModule' directive already has to exist.");
548 $c = '#' if ($opt_A);
549 foreach $lmd (@lmd) {
550 my $what = $opt_A ? "preparing" : "activating";
552 $lmd_re =~ s/\s+/\\s+/g;
554 if ($content !~ m|\n#?\s*$lmd_re|) {
555 # check for open <containers>, so that the new LoadModule
556 # directive always appears *outside* of an <container>.
558 my $before = ($content =~ m|^(.*\n)#?\s*LoadModule\s+[^\n]+\n|s)[0];
560 # the '()=' trick forces list context and the scalar
561 # assignment counts the number of list members (aka number
563 my $cntopen = () = ($before =~ m|^\s*<[^/].*$|mg);
564 my $cntclose = () = ($before =~ m|^\s*</.*$|mg);
566 if ($cntopen == $cntclose) {
567 # fine. Last LoadModule is contextless.
568 $content =~ s|^(.*\n#?\s*LoadModule\s+[^\n]+\n)|$1$c$lmd\n|s;
570 elsif ($cntopen < $cntclose) {
571 error('Configuration file is not valid. There are sections'
572 . ' closed before opened.');
576 # put our cmd after the section containing the last
579 $content =~ s!\A ( # string and capture start
581 ^\s* # start of conf line with a
582 (?:[^<]|<[^/]) # directive which does not
585 .*(?:$)\n # rest of the line.
586 # the '$' is in parentheses
587 # to avoid misinterpreting
591 )* # catch as much as possible
592 # of such lines. (including
595 ^\s*</.*(?:$)\n? # after the above, we
596 # expect a config line with
597 # a closing container (</)
599 ) {$cntopen} # the whole pattern (bunch
600 # of lines that end up with
601 # a closing directive) must
602 # be repeated $cntopen
610 error('Configuration file is not valid. There are '
611 . 'sections opened and not closed.');
616 # replace already existing LoadModule line
617 $content =~ s|^(.*\n)#?\s*$lmd_re[^\n]*\n|$1$c$lmd\n|s;
619 $lmd =~ m|LoadModule\s+(.+?)_module.*|;
620 notice("[$what module `$1' in $CFG_SYSCONFDIR/$CFG_TARGET.conf]");
623 foreach $amd (@amd) {
624 if ($content !~ m|\n#?\s*$amd|) {
625 $content =~ s|^(.*\n#?\s*AddModule\s+[^\n]+\n)|$1$c$amd\n|sg;
627 $content =~ s|^(.*\n)#?\s*$amd[^\n]*\n|$1$c$amd\n|sg;
631 if (open(FP, ">$CFG_SYSCONFDIR/$CFG_TARGET.conf.new")) {
634 system("cp $CFG_SYSCONFDIR/$CFG_TARGET.conf $CFG_SYSCONFDIR/$CFG_TARGET.conf.bak && " .
635 "cp $CFG_SYSCONFDIR/$CFG_TARGET.conf.new $CFG_SYSCONFDIR/$CFG_TARGET.conf && " .
636 "rm $CFG_SYSCONFDIR/$CFG_TARGET.conf.new");
638 notice("unable to open configuration file");
645 print STDERR "apxs:Error: $_[0].\n";
649 print STDERR "$_[0]\n";
655 ## Makefile -- Build procedure for sample %NAME% Apache module
656 ## Autogenerated via ``apxs -n %NAME% -g''.
661 top_builddir=%PREFIX%
662 include %INSTALLBUILDDIR%/special.mk
668 # additional defines, includes and libraries
669 #DEFS=-Dmy_define=my_value
670 #INCLUDES=-Imy/include/dir
671 #LIBS=-Lmy/lib/dir -lmylib
674 all: local-shared-build
676 # install the shared object file into Apache
677 install: install-modules
681 -rm -f mod_%NAME%.o mod_%NAME%.lo mod_%NAME%.slo mod_%NAME%.la
685 lynx -mime_header http://localhost/%NAME%
687 # install and activate shared object by reloading Apache to
688 # force a reload of the shared object file
689 reload: install restart
691 # the general Apache start/restart/stop
701 mod_%NAME%.la: mod_%NAME%.slo
702 $(SH_LINK) -rpath $(libexecdir) -module -avoid-version mod_%NAME%.lo
703 DISTCLEAN_TARGETS = modules.mk
704 shared = mod_%NAME%.la
707 ** mod_%NAME%.c -- Apache sample %NAME% module
708 ** [Autogenerated via ``apxs -n %NAME% -g'']
710 ** To play with this sample module first compile it into a
711 ** DSO file and install it into Apache's modules directory
714 ** $ apxs -c -i mod_%NAME%.c
716 ** Then activate it in Apache's %TARGET%.conf file for instance
717 ** for the URL /%NAME% in as follows:
720 ** LoadModule %NAME%_module modules/mod_%NAME%.so
721 ** <Location /%NAME%>
725 ** Then after restarting Apache via
727 ** $ apachectl restart
729 ** you immediately can request the URL /%NAME% and watch for the
730 ** output of this module. This can be achieved for instance via:
732 ** $ lynx -mime_header http://localhost/%NAME%
734 ** The output should be similar to the following one:
737 ** Date: Tue, 31 Mar 1998 14:42:22 GMT
738 ** Server: Apache/1.3.4 (Unix)
740 ** Content-Type: text/html
742 ** The sample page from mod_%NAME%.c
746 #include "http_config.h"
747 #include "http_protocol.h"
748 #include "ap_config.h"
750 /* The sample content handler */
751 static int %NAME%_handler(request_rec *r)
753 if (strcmp(r->handler, "%NAME%")) {
756 r->content_type = "text/html";
759 ap_rputs("The sample page from mod_%NAME%.c\n", r);
763 static void %NAME%_register_hooks(apr_pool_t *p)
765 ap_hook_handler(%NAME%_handler, NULL, NULL, APR_HOOK_MIDDLE);
768 /* Dispatch list for API hooks */
769 module AP_MODULE_DECLARE_DATA %NAME%_module = {
770 STANDARD20_MODULE_STUFF,
771 NULL, /* create per-dir config structures */
772 NULL, /* merge per-dir config structures */
773 NULL, /* create per-server config structures */
774 NULL, /* merge per-server config structures */
775 NULL, /* table of config file commands */
776 %NAME%_register_hooks /* register hooks */