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
80 # this subroutine is derived from Perl's getopts.pl with the enhancement of
81 # the "+" metacharacter at the format string to allow a list to be built by
82 # subsequent occurrences of the same option.
84 my ($argumentative, @ARGV) = @_;
89 my @args = split / */, $argumentative;
90 while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
91 my ($first, $rest) = ($1,$2);
96 my $pos = index($argumentative,$first);
98 if ($pos < $#args && $args[$pos+1] eq ':') {
102 error("Incomplete option: $first (needs an argument)");
105 $rest = shift(@ARGV);
107 eval "\$opt_$first = \$rest;";
109 elsif ($pos < $#args && $args[$pos+1] eq '+') {
113 error("Incomplete option: $first (needs an argument)");
116 $rest = shift(@ARGV);
118 eval "push(\@opt_$first, \$rest);";
121 eval "\$opt_$first = 1";
131 error("Unknown option: $first");
141 return ($errs == 0, @ARGV);
145 print STDERR "Usage: apxs -g [-S <var>=<val>] -n <modname>\n";
146 print STDERR " apxs -q [-v] [-S <var>=<val>] [<query> ...]\n";
147 print STDERR " apxs -c [-S <var>=<val>] [-o <dsofile>] [-D <name>[=<value>]]\n";
148 print STDERR " [-I <incdir>] [-L <libdir>] [-l <libname>] [-Wc,<flags>]\n";
149 print STDERR " [-Wl,<flags>] [-p] <files> ...\n";
150 print STDERR " apxs -i [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
151 print STDERR " apxs -e [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
157 ($rc, @ARGV) = &Getopts("qn:gco:I+D+L+l+W+S+eiaApv", @ARGV);
158 &usage if ($rc == 0);
159 &usage if ($#ARGV == -1 and not $opt_g and not $opt_q);
160 &usage if (not $opt_q and not ($opt_g and $opt_n) and not $opt_i and not $opt_c and not $opt_e);
164 my $name = 'unknown';
165 $name = $opt_n if ($opt_n ne '');
169 foreach $opt_S (@opt_S) {
170 if ($opt_S =~ m/^([^=]+)=(.*)$/) {
173 my $oldval = eval "\$CFG_$var";
175 unless ($var and $oldval) {
176 print STDERR "apxs:Error: no config variable $var\n";
180 eval "\$CFG_${var}=\"${val}\"";
182 print STDERR "apxs:Error: malformatted -S option\n";
189 ## Initial shared object support check
191 unless ("@MOD_SO_ENABLED@" eq "yes") {
192 error("Sorry, no shared object support for Apache");
193 error("available under your platform. Make sure");
194 error("the Apache module mod_so is compiled into");
195 error("the server binary.");
200 my ($file, $rh_config) = @_;
202 open IN, $file or die "cannot open $file: $!";
204 if (/^\s*(.*?)\s*=\s*(.*)$/){
205 $rh_config->{$1} = $2;
216 if (exists $config_vars{$arg} or exists $config_vars{lc $arg}) {
217 my $val = exists $config_vars{$arg}
219 : $config_vars{lc $arg};
221 $result .= eval "qq($val)" if defined $val;
226 if (exists $internal_vars{$arg} or exists $internal_vars{lc $arg}) {
227 my $val = exists $internal_vars{$arg} ? $arg : lc $arg;
228 $val = eval "\$CFG_$val";
229 $result .= eval "qq($val)" if defined $val;
234 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) {
455 my $apr_libs=`$apr_config --cflags --ldflags --link-libtool --libs`;
458 if ($apr_major_version < 2) {
459 $apu_libs=`$apu_config --ldflags --link-libtool --libs`;
463 $opt .= " ".$apu_libs." ".$apr_libs;
466 my $apr_ldflags=`$apr_config --ldflags`;
468 $opt .= " -rpath $CFG_LIBEXECDIR -module -avoid-version $apr_ldflags";
471 push(@cmds, "$libtool $ltflags --mode=link $CFG_CC -o $dso_file $opt $lo");
473 # execute the commands
474 &execute_cmds(@cmds);
476 # allow one-step compilation and installation
477 if ($opt_i or $opt_e) {
478 @args = ( $dso_file );
482 if ($opt_i or $opt_e) {
484 ## SHARED OBJECT INSTALLATION
487 # determine installation commands
488 # and corresponding LoadModule directive
493 # ack all potential gcc, hp/ux, win32+os2+aix and os/x extensions
494 if ($f !~ m#(\.so$|\.la$|\.sl$|\.dll$|\.dylib$|)#) {
495 error("file $f is not a shared object");
499 $t =~ s|^.+/([^/]+)$|$1|;
500 # use .so unambigiously for installed shared library modules
501 $t =~ s|\.[^./\\]+$|\.so|;
503 push(@cmds, "$installbuilddir/instdso.sh SH_LIBTOOL='" .
504 "$libtool' $f $CFG_LIBEXECDIR");
505 push(@cmds, "chmod 755 $CFG_LIBEXECDIR/$t");
508 # determine module symbolname and filename
510 if ($name eq 'unknown') {
513 $base =~ s|\.[^.]+$||;
515 open(FP, "<$base.c");
516 my $content = join('', <FP>);
518 if ($content =~ m|.*module\s+(?:AP_MODULE_DECLARE_DATA\s+)?([a-zA-Z0-9_]+)_module\s*=\s*.*|s) {
520 $filename = "$base.c";
521 $filename =~ s|^[^/]+/||;
525 if ($base =~ m|.*mod_([a-zA-Z0-9_]+)\..+|) {
528 $filename =~ s|^[^/]+/||;
532 error("Sorry, cannot determine bootstrap symbol name");
533 error("Please specify one with option `-n'");
537 if ($filename eq '') {
538 $filename = "mod_${name}.c";
540 my $dir = $CFG_LIBEXECDIR;
541 $dir =~ s|^$CFG_PREFIX/?||;
544 push(@lmd, sprintf("LoadModule %-18s %s", "${name}_module", "$dir$t"));
547 # execute the commands
548 &execute_cmds(@cmds);
550 # activate module via LoadModule/AddModule directive
551 if ($opt_a or $opt_A) {
552 if (not -f "$CFG_SYSCONFDIR/$CFG_TARGET.conf") {
553 error("Config file $CFG_SYSCONFDIR/$CFG_TARGET.conf not found");
557 open(FP, "<$CFG_SYSCONFDIR/$CFG_TARGET.conf") || die;
558 my $content = join('', <FP>);
561 if ($content !~ m|\n#?\s*LoadModule\s+|) {
562 error("Activation failed for custom $CFG_SYSCONFDIR/$CFG_TARGET.conf file.");
563 error("At least one `LoadModule' directive already has to exist.");
569 $c = '#' if ($opt_A);
570 foreach $lmd (@lmd) {
571 my $what = $opt_A ? "preparing" : "activating";
572 if ($content !~ m|\n#?\s*$lmd|) {
573 # check for open <containers>, so that the new LoadModule
574 # directive always appears *outside* of an <container>.
576 my $before = ($content =~ m|^(.*\n)#?\s*LoadModule\s+[^\n]+\n|s)[0];
578 # the '()=' trick forces list context and the scalar
579 # assignment counts the number of list members (aka number
581 my $cntopen = () = ($before =~ m|^\s*<[^/].*$|mg);
582 my $cntclose = () = ($before =~ m|^\s*</.*$|mg);
584 if ($cntopen == $cntclose) {
585 # fine. Last LoadModule is contextless.
586 $content =~ s|^(.*\n#?\s*LoadModule\s+[^\n]+\n)|$1$c$lmd\n|s;
588 elsif ($cntopen < $cntclose) {
589 error('Configuration file is not valid. There are sections'
590 . ' closed before opened.');
594 # put our cmd after the section containing the last
597 $content =~ s!\A ( # string and capture start
599 ^\s* # start of conf line with a
600 (?:[^<]|<[^/]) # directive which does not
603 .*(?:$)\n # rest of the line.
604 # the '$' is in parentheses
605 # to avoid misinterpreting
609 )* # catch as much as possible
610 # of such lines. (including
613 ^\s*</.*(?:$)\n? # after the above, we
614 # expect a config line with
615 # a closing container (</)
617 ) {$cntopen} # the whole pattern (bunch
618 # of lines that end up with
619 # a closing directive) must
620 # be repeated $cntopen
628 error('Configuration file is not valid. There are '
629 . 'sections opened and not closed.');
634 # replace already existing LoadModule line
635 $content =~ s|^(.*\n)#?\s*$lmd[^\n]*\n|$1$c$lmd\n|s;
637 $lmd =~ m|LoadModule\s+(.+?)_module.*|;
638 notice("[$what module `$1' in $CFG_SYSCONFDIR/$CFG_TARGET.conf]");
641 if (open(FP, ">$CFG_SYSCONFDIR/$CFG_TARGET.conf.new")) {
644 system("cp $CFG_SYSCONFDIR/$CFG_TARGET.conf $CFG_SYSCONFDIR/$CFG_TARGET.conf.bak && " .
645 "cp $CFG_SYSCONFDIR/$CFG_TARGET.conf.new $CFG_SYSCONFDIR/$CFG_TARGET.conf && " .
646 "rm $CFG_SYSCONFDIR/$CFG_TARGET.conf.new");
648 notice("unable to open configuration file");
655 print STDERR "apxs:Error: $_[0].\n";
659 print STDERR "$_[0]\n";
665 ## Makefile -- Build procedure for sample %NAME% Apache module
666 ## Autogenerated via ``apxs -n %NAME% -g''.
671 top_builddir=%PREFIX%
672 include %INSTALLBUILDDIR%/special.mk
678 # additional defines, includes and libraries
679 #DEFS=-Dmy_define=my_value
680 #INCLUDES=-Imy/include/dir
681 #LIBS=-Lmy/lib/dir -lmylib
684 all: local-shared-build
686 # install the shared object file into Apache
687 install: install-modules-yes
691 -rm -f mod_%NAME%.o mod_%NAME%.lo mod_%NAME%.slo mod_%NAME%.la
695 lynx -mime_header http://localhost/%NAME%
697 # install and activate shared object by reloading Apache to
698 # force a reload of the shared object file
699 reload: install restart
701 # the general Apache start/restart/stop
711 mod_%NAME%.la: mod_%NAME%.slo
712 $(SH_LINK) -rpath $(libexecdir) -module -avoid-version mod_%NAME%.lo
713 DISTCLEAN_TARGETS = modules.mk
714 shared = mod_%NAME%.la
717 ** mod_%NAME%.c -- Apache sample %NAME% module
718 ** [Autogenerated via ``apxs -n %NAME% -g'']
720 ** To play with this sample module first compile it into a
721 ** DSO file and install it into Apache's modules directory
724 ** $ apxs -c -i mod_%NAME%.c
726 ** Then activate it in Apache's %TARGET%.conf file for instance
727 ** for the URL /%NAME% in as follows:
730 ** LoadModule %NAME%_module modules/mod_%NAME%.so
731 ** <Location /%NAME%>
735 ** Then after restarting Apache via
737 ** $ apachectl restart
739 ** you immediately can request the URL /%NAME% and watch for the
740 ** output of this module. This can be achieved for instance via:
742 ** $ lynx -mime_header http://localhost/%NAME%
744 ** The output should be similar to the following one:
747 ** Date: Tue, 31 Mar 1998 14:42:22 GMT
748 ** Server: Apache/1.3.4 (Unix)
750 ** Content-Type: text/html
752 ** The sample page from mod_%NAME%.c
756 #include "http_config.h"
757 #include "http_protocol.h"
758 #include "ap_config.h"
760 /* The sample content handler */
761 static int %NAME%_handler(request_rec *r)
763 if (strcmp(r->handler, "%NAME%")) {
766 r->content_type = "text/html";
769 ap_rputs("The sample page from mod_%NAME%.c\n", r);
773 static void %NAME%_register_hooks(apr_pool_t *p)
775 ap_hook_handler(%NAME%_handler, NULL, NULL, APR_HOOK_MIDDLE);
778 /* Dispatch list for API hooks */
779 module AP_MODULE_DECLARE_DATA %NAME%_module = {
780 STANDARD20_MODULE_STUFF,
781 NULL, /* create per-dir config structures */
782 NULL, /* merge per-dir config structures */
783 NULL, /* create per-server config structures */
784 NULL, /* merge per-server config structures */
785 NULL, /* table of config file commands */
786 %NAME%_register_hooks /* register hooks */