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 $CFG_LDFLAGS = join ' ', map { get_vars($_) }
43 qw(LDFLAGS NOTEST_LDFLAGS SH_LDFLAGS);
44 my $includedir = get_vars("includedir");
45 my $CFG_INCLUDEDIR = eval qq("$includedir");
46 my $CFG_CC = get_vars("CC");
47 my $libexecdir = get_vars("libexecdir");
48 my $CFG_LIBEXECDIR = eval qq("$libexecdir");
49 my $sbindir = get_vars("sbindir");
50 my $CFG_SBINDIR = eval qq("$sbindir");
51 my $ltflags = $ENV{'LTFLAGS'};
52 $ltflags or $ltflags = "--silent";
54 my %internal_vars = map {$_ => 1}
55 qw(TARGET CC CFLAGS CFLAGS_SHLIB LD_SHLIB LDFLAGS_SHLIB LIBS_SHLIB
56 PREFIX SBINDIR INCLUDEDIR LIBEXECDIR SYSCONFDIR);
59 ## parse argument line
62 # defaults for parameters
82 # this subroutine is derived from Perl's getopts.pl with the enhancement of
83 # the "+" metacharacter at the format string to allow a list to be built by
84 # subsequent occurrences of the same option.
86 my ($argumentative, @ARGV) = @_;
90 my @args = split / */, $argumentative;
91 while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
92 my ($first, $rest) = ($1,$2);
97 my $pos = index($argumentative,$first);
99 if ($pos < $#args && $args[$pos+1] eq ':') {
103 error("Incomplete option: $first (needs an argument)");
106 $rest = shift(@ARGV);
108 eval "\$opt_$first = \$rest;";
110 elsif ($pos < $#args && $args[$pos+1] eq '+') {
114 error("Incomplete option: $first (needs an argument)");
117 $rest = shift(@ARGV);
119 eval "push(\@opt_$first, \$rest);";
122 eval "\$opt_$first = 1";
132 error("Unknown option: $first");
142 return ($errs == 0, @ARGV);
146 print STDERR "Usage: apxs -g [-S <var>=<val>] -n <modname>\n";
147 print STDERR " apxs -q [-v] [-S <var>=<val>] [<query> ...]\n";
148 print STDERR " apxs -c [-S <var>=<val>] [-o <dsofile>] [-D <name>[=<value>]]\n";
149 print STDERR " [-I <incdir>] [-L <libdir>] [-l <libname>] [-Wc,<flags>]\n";
150 print STDERR " [-Wl,<flags>] [-p] <files> ...\n";
151 print STDERR " apxs -i [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
152 print STDERR " apxs -e [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
158 ($rc, @ARGV) = &Getopts("qn:gco:I+D+L+l+W+S+eiaApv", @ARGV);
159 &usage if ($rc == 0);
160 &usage if ($#ARGV == -1 and not $opt_g and not $opt_q);
161 &usage if (not $opt_q and not ($opt_g and $opt_n) and not $opt_i and not $opt_c and not $opt_e);
165 my $name = 'unknown';
166 $name = $opt_n if ($opt_n ne '');
170 foreach $opt_S (@opt_S) {
171 if ($opt_S =~ m/^([^=]+)=(.*)$/) {
174 my $oldval = eval "\$CFG_$var";
176 unless ($var and $oldval) {
177 print STDERR "apxs:Error: no config variable $var\n";
181 eval "\$CFG_${var}=\"${val}\"";
183 print STDERR "apxs:Error: malformatted -S option\n";
190 ## Initial shared object support check
192 unless ("@MOD_SO_ENABLED@" eq "yes") {
193 error("Sorry, no shared object support for Apache");
194 error("available under your platform. Make sure");
195 error("the Apache module mod_so is compiled into");
196 error("the server binary.");
201 my ($file, $rh_config) = @_;
203 open IN, $file or die "cannot open $file: $!";
205 if (/^\s*(.*?)\s*=\s*(.*)$/){
206 $rh_config->{$1} = $2;
217 if (exists $config_vars{$arg} or exists $config_vars{lc $arg}) {
218 my $val = exists $config_vars{$arg}
220 : $config_vars{lc $arg};
222 $result .= eval "qq($val)" if defined $val;
227 if (exists $internal_vars{$arg} or exists $internal_vars{lc $arg}) {
228 my $val = exists $internal_vars{$arg} ? $arg : lc $arg;
229 $val = eval "\$CFG_$val";
230 $result .= eval "qq($val)" if defined $val;
235 error("Invalid query string `$arg'");
248 # helper function for executing a list of
249 # system command with return code checks
254 foreach $cmd (@cmds) {
258 error(sprintf "Command failed with rc=%d\n", $rc << 8);
266 ## SAMPLE MODULE SOURCE GENERATION
270 error("Directory `$name' already exists. Remove first");
274 my $data = join('', <DATA>);
275 $data =~ s|%NAME%|$name|sg;
276 $data =~ s|%TARGET%|$CFG_TARGET|sg;
277 $data =~ s|%PREFIX%|$prefix|sg;
278 $data =~ s|%INSTALLBUILDDIR%|$installbuilddir|sg;
280 my ($mkf, $mods, $src) = ($data =~ m|^(.+)-=#=-\n(.+)-=#=-\n(.+)|s);
282 notice("Creating [DIR] $name");
283 system("mkdir $name");
284 notice("Creating [FILE] $name/Makefile");
285 open(FP, ">${name}/Makefile") || die;
288 notice("Creating [FILE] $name/modules.mk");
289 open(FP, ">${name}/modules.mk") || die;
292 notice("Creating [FILE] $name/mod_$name.c");
293 open(FP, ">${name}/mod_${name}.c") || die;
296 notice("Creating [FILE] $name/.deps");
297 system("touch ${name}/.deps");
309 $result = get_vars(@args);
312 # -q without var name prints all variables and their values
314 # Additional -v pretty-prints output
316 # Variable names in alphabetic order
317 my @vars = sort {uc($a) cmp uc($b)} keys %config_vars;
319 # Make the left column as wide as the longest variable name
323 $width = $l unless ($l <= $width);
327 printf "%-${width}s = %s\n", $_, $config_vars{$_};
330 # Unprettified name=value list
331 foreach (keys %config_vars) {
332 print "$_=$config_vars{$_}\n";
338 my $apr_config = get_vars("APR_CONFIG");
340 if (! -x "$apr_config") {
341 error("$apr_config not found!");
345 my $apr_major_version = (split /\./, `$apr_config --version`)[0];
348 if ($apr_major_version < 2) {
349 $apu_config = get_vars("APU_CONFIG");
351 if (! -x "$apu_config") {
352 error("$apu_config not found!");
357 my $libtool = `$apr_config --apr-libtool`;
360 my $apr_includedir = `$apr_config --includes`;
361 chomp($apr_includedir);
362 my $apu_includedir = "";
363 if ($apr_major_version < 2) {
364 $apu_includedir = `$apu_config --includes`;
365 chomp($apu_includedir);
370 ## SHARED OBJECT COMPILATION
373 # split files into sources and objects
386 # determine output file
390 $dso_file = $srcs[0];
391 $dso_file =~ s|\.[^.]+$|.la|;
393 elsif ($#objs > -1) {
394 $dso_file = $objs[0];
395 $dso_file =~ s|\.[^.]+$|.la|;
398 $dso_file = "mod_unknown.la";
403 $dso_file =~ s|\.[^.]+$|.la|;
406 # create compilation commands
409 my ($opt_Wc, $opt_I, $opt_D);
410 foreach $opt_Wc (@opt_W) {
411 $opt .= "$1 " if ($opt_Wc =~ m|^\s*c,(.*)$|);
413 foreach $opt_I (@opt_I) {
416 foreach $opt_D (@opt_D) {
419 my $cflags = "$CFG_CFLAGS";
424 $slo =~ s|\.c$|.slo|;
431 push(@cmds, "$libtool $ltflags --mode=compile $CFG_CC $cflags -I$CFG_INCLUDEDIR $apr_includedir $apu_includedir $opt -c -o $lo $s && touch $slo");
435 # create link command
441 my ($opt_Wl, $opt_L, $opt_l);
443 foreach $opt_Wl (@opt_W) {
444 $opt .= "$1 " if ($opt_Wl =~ m|^\s*l,(.*)$|);
446 foreach $opt_L (@opt_L) {
449 foreach $opt_l (@opt_l) {
453 my $ldflags = "$CFG_LDFLAGS";
456 my $apr_libs=`$apr_config --cflags --ldflags --link-libtool --libs`;
459 if ($apr_major_version < 2) {
460 $apu_libs=`$apu_config --ldflags --link-libtool --libs`;
464 $opt .= " ".$apu_libs." ".$apr_libs;
467 my $apr_ldflags=`$apr_config --ldflags`;
469 $opt .= " -rpath $CFG_LIBEXECDIR -module -avoid-version $apr_ldflags";
472 push(@cmds, "$libtool $ltflags --mode=link $CFG_CC $ldflags -o $dso_file $opt $lo");
474 # execute the commands
475 &execute_cmds(@cmds);
477 # allow one-step compilation and installation
478 if ($opt_i or $opt_e) {
479 @args = ( $dso_file );
483 if ($opt_i or $opt_e) {
485 ## SHARED OBJECT INSTALLATION
488 # determine installation commands
489 # and corresponding LoadModule directive
494 # ack all potential gcc, hp/ux, win32+os2+aix and os/x extensions
495 if ($f !~ m#(\.so$|\.la$|\.sl$|\.dll$|\.dylib$|)#) {
496 error("file $f is not a shared object");
500 $t =~ s|^.+/([^/]+)$|$1|;
501 # use .so unambigiously for installed shared library modules
502 $t =~ s|\.[^./\\]+$|\.so|;
504 push(@cmds, "$installbuilddir/instdso.sh SH_LIBTOOL='" .
505 "$libtool' $f $CFG_LIBEXECDIR");
506 push(@cmds, "chmod 755 $CFG_LIBEXECDIR/$t");
509 # determine module symbolname and filename
511 if ($name eq 'unknown') {
514 $base =~ s|\.[^.]+$||;
516 open(FP, "<$base.c");
517 my $content = join('', <FP>);
519 if ($content =~ m|.*AP_DECLARE_MODULE\s*\(\s*([a-zA-Z0-9_]+)\s*\)\s*=.*|s || $content =~ m|.*module\s+(?:AP_MODULE_DECLARE_DATA\s+)?([a-zA-Z0-9_]+)_module\s*=\s*.*|s) {
521 $filename = "$base.c";
522 $filename =~ s|^[^/]+/||;
526 if ($base =~ m|.*mod_([a-zA-Z0-9_]+)\..+|) {
529 $filename =~ s|^[^/]+/||;
533 error("Sorry, cannot determine bootstrap symbol name");
534 error("Please specify one with option `-n'");
538 if ($filename eq '') {
539 $filename = "mod_${name}.c";
541 my $dir = $CFG_LIBEXECDIR;
542 $dir =~ s|^$CFG_PREFIX/?||;
545 push(@lmd, sprintf("LoadModule %-18s %s", "${name}_module", "$dir$t"));
548 # execute the commands
549 &execute_cmds(@cmds);
551 # activate module via LoadModule/AddModule directive
552 if ($opt_a or $opt_A) {
553 if (not -f "$CFG_SYSCONFDIR/$CFG_TARGET.conf") {
554 error("Config file $CFG_SYSCONFDIR/$CFG_TARGET.conf not found");
558 open(FP, "<$CFG_SYSCONFDIR/$CFG_TARGET.conf") || die;
559 my $content = join('', <FP>);
562 if ($content !~ m|\n#?\s*LoadModule\s+|) {
563 error("Activation failed for custom $CFG_SYSCONFDIR/$CFG_TARGET.conf file.");
564 error("At least one `LoadModule' directive already has to exist.");
570 $c = '#' if ($opt_A);
571 foreach $lmd (@lmd) {
572 my $what = $opt_A ? "preparing" : "activating";
574 $lmd_re =~ s/\s+/\\s+/g;
576 if ($content !~ m|\n#?\s*$lmd_re|) {
577 # check for open <containers>, so that the new LoadModule
578 # directive always appears *outside* of an <container>.
580 my $before = ($content =~ m|^(.*\n)#?\s*LoadModule\s+[^\n]+\n|s)[0];
582 # the '()=' trick forces list context and the scalar
583 # assignment counts the number of list members (aka number
585 my $cntopen = () = ($before =~ m|^\s*<[^/].*$|mg);
586 my $cntclose = () = ($before =~ m|^\s*</.*$|mg);
588 if ($cntopen == $cntclose) {
589 # fine. Last LoadModule is contextless.
590 $content =~ s|^(.*\n#?\s*LoadModule\s+[^\n]+\n)|$1$c$lmd\n|s;
592 elsif ($cntopen < $cntclose) {
593 error('Configuration file is not valid. There are sections'
594 . ' closed before opened.');
598 # put our cmd after the section containing the last
601 $content =~ s!\A ( # string and capture start
603 ^\s* # start of conf line with a
604 (?:[^<]|<[^/]) # directive which does not
607 .*(?:$)\n # rest of the line.
608 # the '$' is in parentheses
609 # to avoid misinterpreting
613 )* # catch as much as possible
614 # of such lines. (including
617 ^\s*</.*(?:$)\n? # after the above, we
618 # expect a config line with
619 # a closing container (</)
621 ) {$cntopen} # the whole pattern (bunch
622 # of lines that end up with
623 # a closing directive) must
624 # be repeated $cntopen
632 error('Configuration file is not valid. There are '
633 . 'sections opened and not closed.');
638 # replace already existing LoadModule line
639 $content =~ s|^(.*\n)#?\s*$lmd_re[^\n]*\n|$1$c$lmd\n|s;
641 $lmd =~ m|LoadModule\s+(.+?)_module.*|;
642 notice("[$what module `$1' in $CFG_SYSCONFDIR/$CFG_TARGET.conf]");
645 if (open(FP, ">$CFG_SYSCONFDIR/$CFG_TARGET.conf.new")) {
648 system("cp $CFG_SYSCONFDIR/$CFG_TARGET.conf $CFG_SYSCONFDIR/$CFG_TARGET.conf.bak && " .
649 "cp $CFG_SYSCONFDIR/$CFG_TARGET.conf.new $CFG_SYSCONFDIR/$CFG_TARGET.conf && " .
650 "rm $CFG_SYSCONFDIR/$CFG_TARGET.conf.new");
652 notice("unable to open configuration file");
659 print STDERR "apxs:Error: $_[0].\n";
663 print STDERR "$_[0]\n";
669 ## Makefile -- Build procedure for sample %NAME% Apache module
670 ## Autogenerated via ``apxs -n %NAME% -g''.
675 top_builddir=%PREFIX%
676 include %INSTALLBUILDDIR%/special.mk
681 # additional defines, includes and libraries
682 #DEFS=-Dmy_define=my_value
683 #INCLUDES=-Imy/include/dir
684 #LIBS=-Lmy/lib/dir -lmylib
687 all: local-shared-build
689 # install the shared object file into Apache
690 install: install-modules-yes
694 -rm -f mod_%NAME%.o mod_%NAME%.lo mod_%NAME%.slo mod_%NAME%.la
698 lynx -mime_header http://localhost/%NAME%
700 # install and activate shared object by reloading Apache to
701 # force a reload of the shared object file
702 reload: install restart
704 # the general Apache start/restart/stop
714 mod_%NAME%.la: mod_%NAME%.slo
715 $(SH_LINK) -rpath $(libexecdir) -module -avoid-version mod_%NAME%.lo
716 DISTCLEAN_TARGETS = modules.mk
717 shared = mod_%NAME%.la
720 ** mod_%NAME%.c -- Apache sample %NAME% module
721 ** [Autogenerated via ``apxs -n %NAME% -g'']
723 ** To play with this sample module first compile it into a
724 ** DSO file and install it into Apache's modules directory
727 ** $ apxs -c -i mod_%NAME%.c
729 ** Then activate it in Apache's %TARGET%.conf file for instance
730 ** for the URL /%NAME% in as follows:
733 ** LoadModule %NAME%_module modules/mod_%NAME%.so
734 ** <Location /%NAME%>
738 ** Then after restarting Apache via
740 ** $ apachectl restart
742 ** you immediately can request the URL /%NAME% and watch for the
743 ** output of this module. This can be achieved for instance via:
745 ** $ lynx -mime_header http://localhost/%NAME%
747 ** The output should be similar to the following one:
750 ** Date: Tue, 31 Mar 1998 14:42:22 GMT
751 ** Server: Apache/1.3.4 (Unix)
753 ** Content-Type: text/html
755 ** The sample page from mod_%NAME%.c
759 #include "http_config.h"
760 #include "http_protocol.h"
761 #include "ap_config.h"
763 /* The sample content handler */
764 static int %NAME%_handler(request_rec *r)
766 if (strcmp(r->handler, "%NAME%")) {
769 r->content_type = "text/html";
772 ap_rputs("The sample page from mod_%NAME%.c\n", r);
776 static void %NAME%_register_hooks(apr_pool_t *p)
778 ap_hook_handler(%NAME%_handler, NULL, NULL, APR_HOOK_MIDDLE);
781 /* Dispatch list for API hooks */
782 module AP_MODULE_DECLARE_DATA %NAME%_module = {
783 STANDARD20_MODULE_STUFF,
784 NULL, /* create per-dir config structures */
785 NULL, /* merge per-dir config structures */
786 NULL, /* create per-server config structures */
787 NULL, /* merge per-server config structures */
788 NULL, /* table of config file commands */
789 %NAME%_register_hooks /* register hooks */