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 = "@exp_installbuilddir@";
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_config = get_vars("APR_CONFIG");
329 if (! -x "$apr_config") {
330 error("$apr_config not found!");
334 my $apu_config = get_vars("APU_CONFIG");
336 if (! -x "$apu_config") {
337 error("$apu_config not found!");
341 my $libtool = `$apr_config --apr-libtool`;
344 my $apr_includedir = `$apr_config --includes`;
345 chomp($apr_includedir);
346 my $apu_includedir = `$apu_config --includes`;
347 chomp($apu_includedir);
351 ## SHARED OBJECT COMPILATION
354 # split files into sources and objects
367 # determine output file
371 $dso_file = $srcs[0];
372 $dso_file =~ s|\.[^.]+$|.la|;
374 elsif ($#objs > -1) {
375 $dso_file = $objs[0];
376 $dso_file =~ s|\.[^.]+$|.la|;
379 $dso_file = "mod_unknown.la";
384 $dso_file =~ s|\.[^.]+$|.la|;
387 # create compilation commands
390 my ($opt_Wc, $opt_I, $opt_D);
391 foreach $opt_Wc (@opt_W) {
392 $opt .= "$1 " if ($opt_Wc =~ m|^\s*c,(.*)$|);
394 foreach $opt_I (@opt_I) {
397 foreach $opt_D (@opt_D) {
400 my $cflags = "$CFG_CFLAGS";
405 $slo =~ s|\.c$|.slo|;
412 push(@cmds, "$libtool $ltflags --mode=compile $CFG_CC $cflags -I$CFG_INCLUDEDIR $apr_includedir $apu_includedir $opt -c -o $lo $s && touch $slo");
416 # create link command
422 my ($opt_Wl, $opt_L, $opt_l);
424 foreach $opt_Wl (@opt_W) {
425 $opt .= "$1 " if ($opt_Wl =~ m|^\s*l,(.*)$|);
427 foreach $opt_L (@opt_L) {
430 foreach $opt_l (@opt_l) {
436 my $apr_libs=`$apr_config --cflags --ldflags --link-libtool --libs`;
438 my $apu_libs=`$apu_config --ldflags --link-libtool --libs`;
441 $opt .= " ".$apu_libs." ".$apr_libs;
444 my $apr_ldflags=`$apr_config --ldflags`;
446 $opt .= " -rpath $CFG_LIBEXECDIR -module -avoid-version $apr_ldflags";
449 push(@cmds, "$libtool $ltflags --mode=link $CFG_CC -o $dso_file $opt $lo");
451 # execute the commands
452 &execute_cmds(@cmds);
454 # allow one-step compilation and installation
455 if ($opt_i or $opt_e) {
456 @args = ( $dso_file );
460 if ($opt_i or $opt_e) {
462 ## SHARED OBJECT INSTALLATION
465 # determine installation commands
466 # and corresponding LoadModule directive
471 # ack all potential gcc, hp/ux, win32+os2+aix and os/x extensions
472 if ($f !~ m#(\.so$|\.la$|\.sl$|\.dll$|\.dylib$|)#) {
473 error("file $f is not a shared object");
477 $t =~ s|^.+/([^/]+)$|$1|;
478 # use .so unambigiously for installed shared library modules
479 $t =~ s|\.[^./\\]+$|\.so|;
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"));
525 # execute the commands
526 &execute_cmds(@cmds);
528 # activate module via LoadModule/AddModule directive
529 if ($opt_a or $opt_A) {
530 if (not -f "$CFG_SYSCONFDIR/$CFG_TARGET.conf") {
531 error("Config file $CFG_SYSCONFDIR/$CFG_TARGET.conf not found");
535 open(FP, "<$CFG_SYSCONFDIR/$CFG_TARGET.conf") || die;
536 my $content = join('', <FP>);
539 if ($content !~ m|\n#?\s*LoadModule\s+|) {
540 error("Activation failed for custom $CFG_SYSCONFDIR/$CFG_TARGET.conf file.");
541 error("At least one `LoadModule' directive already has to exist.");
547 $c = '#' if ($opt_A);
548 foreach $lmd (@lmd) {
549 my $what = $opt_A ? "preparing" : "activating";
550 if ($content !~ m|\n#?\s*$lmd|) {
551 # check for open <containers>, so that the new LoadModule
552 # directive always appears *outside* of an <container>.
554 my $before = ($content =~ m|^(.*\n)#?\s*LoadModule\s+[^\n]+\n|s)[0];
556 # the '()=' trick forces list context and the scalar
557 # assignment counts the number of list members (aka number
559 my $cntopen = () = ($before =~ m|^\s*<[^/].*$|mg);
560 my $cntclose = () = ($before =~ m|^\s*</.*$|mg);
562 if ($cntopen == $cntclose) {
563 # fine. Last LoadModule is contextless.
564 $content =~ s|^(.*\n#?\s*LoadModule\s+[^\n]+\n)|$1$c$lmd\n|s;
566 elsif ($cntopen < $cntclose) {
567 error('Configuration file is not valid. There are sections'
568 . ' closed before opened.');
572 # put our cmd after the section containing the last
575 $content =~ s!\A ( # string and capture start
577 ^\s* # start of conf line with a
578 (?:[^<]|<[^/]) # directive which does not
581 .*(?:$)\n # rest of the line.
582 # the '$' is in parentheses
583 # to avoid misinterpreting
587 )* # catch as much as possible
588 # of such lines. (including
591 ^\s*</.*(?:$)\n? # after the above, we
592 # expect a config line with
593 # a closing container (</)
595 ) {$cntopen} # the whole pattern (bunch
596 # of lines that end up with
597 # a closing directive) must
598 # be repeated $cntopen
606 error('Configuration file is not valid. There are '
607 . 'sections opened and not closed.');
612 # replace already existing LoadModule line
613 $content =~ s|^(.*\n)#?\s*$lmd[^\n]*\n|$1$c$lmd\n|s;
615 $lmd =~ m|LoadModule\s+(.+?)_module.*|;
616 notice("[$what module `$1' in $CFG_SYSCONFDIR/$CFG_TARGET.conf]");
619 if (open(FP, ">$CFG_SYSCONFDIR/$CFG_TARGET.conf.new")) {
622 system("cp $CFG_SYSCONFDIR/$CFG_TARGET.conf $CFG_SYSCONFDIR/$CFG_TARGET.conf.bak && " .
623 "cp $CFG_SYSCONFDIR/$CFG_TARGET.conf.new $CFG_SYSCONFDIR/$CFG_TARGET.conf && " .
624 "rm $CFG_SYSCONFDIR/$CFG_TARGET.conf.new");
626 notice("unable to open configuration file");
633 print STDERR "apxs:Error: $_[0].\n";
637 print STDERR "$_[0]\n";
643 ## Makefile -- Build procedure for sample %NAME% Apache module
644 ## Autogenerated via ``apxs -n %NAME% -g''.
649 top_builddir=%PREFIX%
650 include %INSTALLBUILDDIR%/special.mk
656 # additional defines, includes and libraries
657 #DEFS=-Dmy_define=my_value
658 #INCLUDES=-Imy/include/dir
659 #LIBS=-Lmy/lib/dir -lmylib
662 all: local-shared-build
664 # install the shared object file into Apache
665 install: install-modules-yes
669 -rm -f mod_%NAME%.o mod_%NAME%.lo mod_%NAME%.slo mod_%NAME%.la
673 lynx -mime_header http://localhost/%NAME%
675 # install and activate shared object by reloading Apache to
676 # force a reload of the shared object file
677 reload: install restart
679 # the general Apache start/restart/stop
689 mod_%NAME%.la: mod_%NAME%.slo
690 $(SH_LINK) -rpath $(libexecdir) -module -avoid-version mod_%NAME%.lo
691 DISTCLEAN_TARGETS = modules.mk
692 shared = mod_%NAME%.la
695 ** mod_%NAME%.c -- Apache sample %NAME% module
696 ** [Autogenerated via ``apxs -n %NAME% -g'']
698 ** To play with this sample module first compile it into a
699 ** DSO file and install it into Apache's modules directory
702 ** $ apxs -c -i mod_%NAME%.c
704 ** Then activate it in Apache's %TARGET%.conf file for instance
705 ** for the URL /%NAME% in as follows:
708 ** LoadModule %NAME%_module modules/mod_%NAME%.so
709 ** <Location /%NAME%>
713 ** Then after restarting Apache via
715 ** $ apachectl restart
717 ** you immediately can request the URL /%NAME% and watch for the
718 ** output of this module. This can be achieved for instance via:
720 ** $ lynx -mime_header http://localhost/%NAME%
722 ** The output should be similar to the following one:
725 ** Date: Tue, 31 Mar 1998 14:42:22 GMT
726 ** Server: Apache/1.3.4 (Unix)
728 ** Content-Type: text/html
730 ** The sample page from mod_%NAME%.c
734 #include "http_config.h"
735 #include "http_protocol.h"
736 #include "ap_config.h"
738 /* The sample content handler */
739 static int %NAME%_handler(request_rec *r)
741 if (strcmp(r->handler, "%NAME%")) {
744 r->content_type = "text/html";
747 ap_rputs("The sample page from mod_%NAME%.c\n", r);
751 static void %NAME%_register_hooks(apr_pool_t *p)
753 ap_hook_handler(%NAME%_handler, NULL, NULL, APR_HOOK_MIDDLE);
756 /* Dispatch list for API hooks */
757 module AP_MODULE_DECLARE_DATA %NAME%_module = {
758 STANDARD20_MODULE_STUFF,
759 NULL, /* create per-dir config structures */
760 NULL, /* merge per-dir config structures */
761 NULL, /* create per-server config structures */
762 NULL, /* merge per-server config structures */
763 NULL, /* table of config file commands */
764 %NAME%_register_hooks /* register hooks */