3 # Copyright 2000-2004 Apache Software Foundation
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
27 my $installbuilddir = "@exp_installbuilddir@";
28 get_config_vars("$installbuilddir/config_vars.mk",\%config_vars);
30 # read the configuration variables once
32 my $prefix = get_vars("prefix");
33 my $CFG_PREFIX = $prefix;
34 my $exec_prefix = get_vars("exec_prefix");
35 my $datadir = get_vars("datadir");
36 my $localstatedir = get_vars("localstatedir");
37 my $CFG_TARGET = get_vars("progname");
38 my $CFG_SYSCONFDIR = get_vars("sysconfdir");
39 my $CFG_CFLAGS = join ' ', map { get_vars($_) }
40 qw(SHLTCFLAGS CFLAGS NOTEST_CPPFLAGS EXTRA_CPPFLAGS EXTRA_CFLAGS);
41 my $includedir = get_vars("includedir");
42 my $CFG_INCLUDEDIR = eval qq("$includedir");
43 my $CFG_CC = get_vars("CC");
44 my $libexecdir = get_vars("libexecdir");
45 my $CFG_LIBEXECDIR = eval qq("$libexecdir");
46 my $sbindir = get_vars("sbindir");
47 my $CFG_SBINDIR = eval qq("$sbindir");
48 my $ltflags = $ENV{'LTFLAGS'};
49 $ltflags or $ltflags = "--silent";
51 my %internal_vars = map {$_ => 1}
52 qw(TARGET CC CFLAGS CFLAGS_SHLIB LD_SHLIB LDFLAGS_SHLIB LIBS_SHLIB
53 PREFIX SBINDIR INCLUDEDIR LIBEXECDIR SYSCONFDIR);
56 ## parse argument line
59 # defaults for parameters
78 # this subroutine is derived from Perl's getopts.pl with the enhancement of
79 # the "+" metacharacter at the format string to allow a list to be built by
80 # subsequent occurrences of the same option.
82 my ($argumentative, @ARGV) = @_;
87 my @args = split / */, $argumentative;
88 while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
89 my ($first, $rest) = ($1,$2);
94 my $pos = index($argumentative,$first);
96 if ($pos < $#args && $args[$pos+1] eq ':') {
100 error("Incomplete option: $first (needs an argument)");
103 $rest = shift(@ARGV);
105 eval "\$opt_$first = \$rest;";
107 elsif ($pos < $#args && $args[$pos+1] eq '+') {
111 error("Incomplete option: $first (needs an argument)");
114 $rest = shift(@ARGV);
116 eval "push(\@opt_$first, \$rest);";
119 eval "\$opt_$first = 1";
129 error("Unknown option: $first");
139 return ($errs == 0, @ARGV);
143 print STDERR "Usage: apxs -g [-S <var>=<val>] -n <modname>\n";
144 print STDERR " apxs -q [-S <var>=<val>] <query> ...\n";
145 print STDERR " apxs -c [-S <var>=<val>] [-o <dsofile>] [-D <name>[=<value>]]\n";
146 print STDERR " [-I <incdir>] [-L <libdir>] [-l <libname>] [-Wc,<flags>]\n";
147 print STDERR " [-Wl,<flags>] [-p] <files> ...\n";
148 print STDERR " apxs -i [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
149 print STDERR " apxs -e [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
155 ($rc, @ARGV) = &Getopts("qn:gco:I+D+L+l+W+S+eiaAp", @ARGV);
156 &usage if ($rc == 0);
157 &usage if ($#ARGV == -1 and not $opt_g);
158 &usage if (not $opt_q and not ($opt_g and $opt_n) and not $opt_i and not $opt_c and not $opt_e);
162 my $name = 'unknown';
163 $name = $opt_n if ($opt_n ne '');
167 foreach $opt_S (@opt_S) {
168 if ($opt_S =~ m/^([^=]+)=(.*)$/) {
171 my $oldval = eval "\$CFG_$var";
173 unless ($var and $oldval) {
174 print STDERR "apxs:Error: no config variable $var\n";
178 eval "\$CFG_${var}=\"${val}\"";
180 print STDERR "apxs:Error: malformatted -S option\n";
187 ## Initial shared object support check
189 my $httpd = get_vars("sbindir") . "/" . get_vars("progname");
190 $httpd = eval qq("$httpd");
191 $httpd = eval qq("$httpd");
192 my $envvars = get_vars("sbindir") . "/envvars";
193 $envvars = eval qq("$envvars");
194 $envvars = eval qq("$envvars");
196 #allow apxs to be run from the source tree, before installation
197 if ($0 =~ m:support/apxs$:) {
198 ($httpd = $0) =~ s:support/apxs$::;
201 unless (-x "$httpd") {
202 error("$httpd not found or not executable");
206 unless (grep /mod_so/, `. $envvars && $httpd -l`) {
207 error("Sorry, no shared object support for Apache");
208 error("available under your platform. Make sure");
209 error("the Apache module mod_so is compiled into");
210 error("your server binary `$httpd'.");
215 my ($file, $rh_config) = @_;
217 open IN, $file or die "cannot open $file: $!";
219 if (/^\s*(.*?)\s*=\s*(.*)$/){
220 $rh_config->{$1} = $2;
231 if (exists $config_vars{$arg} or exists $config_vars{lc $arg}) {
232 my $val = exists $config_vars{$arg}
234 : $config_vars{lc $arg};
236 $result .= eval "qq($val)" if defined $val;
241 if (exists $internal_vars{$arg} or exists $internal_vars{lc $arg}) {
242 my $val = exists $internal_vars{$arg} ? $arg : lc $arg;
243 $val = eval "\$CFG_$val";
244 $result .= eval "qq($val)" if defined $val;
249 error("Invalid query string `$arg'");
263 # helper function for executing a list of
264 # system command with return code checks
269 foreach $cmd (@cmds) {
273 error(sprintf "Command failed with rc=%d\n", $rc << 8);
281 ## SAMPLE MODULE SOURCE GENERATION
285 error("Directory `$name' already exists. Remove first");
289 my $data = join('', <DATA>);
290 $data =~ s|%NAME%|$name|sg;
291 $data =~ s|%TARGET%|$CFG_TARGET|sg;
292 $data =~ s|%PREFIX%|$prefix|sg;
293 $data =~ s|%INSTALLBUILDDIR%|$installbuilddir|sg;
295 my ($mkf, $mods, $src) = ($data =~ m|^(.+)-=#=-\n(.+)-=#=-\n(.+)|s);
297 notice("Creating [DIR] $name");
298 system("mkdir $name");
299 notice("Creating [FILE] $name/Makefile");
300 open(FP, ">${name}/Makefile") || die;
303 notice("Creating [FILE] $name/modules.mk");
304 open(FP, ">${name}/modules.mk") || die;
307 notice("Creating [FILE] $name/mod_$name.c");
308 open(FP, ">${name}/mod_${name}.c") || die;
311 notice("Creating [FILE] $name/.deps");
312 system("touch ${name}/.deps");
322 my $result = get_vars(@args);
326 my $apr_bindir = get_vars("APR_BINDIR");
328 if (! -x "$apr_bindir/apr-config") {
329 error("$apr_bindir/apr-config not found!");
333 my $apu_bindir = get_vars("APU_BINDIR");
335 if (! -x "$apu_bindir/apu-config") {
336 error("$apu_bindir/apu-config not found!");
340 my $libtool = `$apr_bindir/apr-config --installbuilddir`;
342 $libtool = "$libtool/libtool";
344 my $apr_includedir = `$apr_bindir/apr-config --includes`;
345 chomp($apr_includedir);
346 my $apu_includedir = `$apu_bindir/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.so";
386 # create compilation commands
389 my ($opt_Wc, $opt_I, $opt_D);
390 foreach $opt_Wc (@opt_W) {
391 $opt .= "$1 " if ($opt_Wc =~ m|^\s*c,(.*)$|);
393 foreach $opt_I (@opt_I) {
396 foreach $opt_D (@opt_D) {
399 my $cflags = "$CFG_CFLAGS";
404 $slo =~ s|\.c$|.slo|;
411 push(@cmds, "$libtool $ltflags --mode=compile $CFG_CC $cflags -I$CFG_INCLUDEDIR $apr_includedir $apu_includedir $opt -c -o $lo $s && touch $slo");
415 # create link command
421 my ($opt_Wl, $opt_L, $opt_l);
422 foreach $opt_Wl (@opt_W) {
423 if ($CFG_CC !~ m/gcc$/) {
424 $opt .= " $1" if ($opt_Wl =~ m|^\s*l,(.*)$|);
426 $opt .= " -W$opt_Wl";
429 foreach $opt_L (@opt_L) {
432 foreach $opt_l (@opt_l) {
438 my $apr_libs=`$apr_bindir/apr-config --cflags --ldflags --link-libtool --libs`;
440 my $apu_libs=`$apu_bindir/apu-config --ldflags --link-libtool --libs`;
443 $opt .= " ".$apu_libs." ".$apr_libs;
446 my $apr_ldflags=`$apr_bindir/apr-config --ldflags`;
448 $opt .= " -rpath $CFG_LIBEXECDIR -module -avoid-version $apr_ldflags";
451 push(@cmds, "$libtool $ltflags --mode=link $CFG_CC -o $dso_file $opt $lo");
453 # execute the commands
454 &execute_cmds(@cmds);
456 # allow one-step compilation and installation
457 if ($opt_i or $opt_e) {
458 @args = ( $dso_file );
462 if ($opt_i or $opt_e) {
464 ## SHARED OBJECT INSTALLATION
467 # determine installation commands
468 # and corresponding LoadModule directive
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"));
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
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 */