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) = @_;
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 [-v] [-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+eiaApv", @ARGV);
157 &usage if ($rc == 0);
158 &usage if ($#ARGV == -1 and not $opt_g and not $opt_q);
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 unless ("@MOD_SO_ENABLED@" eq "yes") {
191 error("Sorry, no shared object support for Apache");
192 error("available under your platform. Make sure");
193 error("the Apache module mod_so is compiled into");
194 error("the server binary.");
199 my ($file, $rh_config) = @_;
201 open IN, $file or die "cannot open $file: $!";
203 if (/^\s*(.*?)\s*=\s*(.*)$/){
204 $rh_config->{$1} = $2;
215 if (exists $config_vars{$arg} or exists $config_vars{lc $arg}) {
216 my $val = exists $config_vars{$arg}
218 : $config_vars{lc $arg};
220 $result .= eval "qq($val)" if defined $val;
225 if (exists $internal_vars{$arg} or exists $internal_vars{lc $arg}) {
226 my $val = exists $internal_vars{$arg} ? $arg : lc $arg;
227 $val = eval "\$CFG_$val";
228 $result .= eval "qq($val)" if defined $val;
233 error("Invalid query string `$arg'");
247 # helper function for executing a list of
248 # system command with return code checks
253 foreach $cmd (@cmds) {
257 error(sprintf "Command failed with rc=%d\n", $rc << 8);
265 ## SAMPLE MODULE SOURCE GENERATION
269 error("Directory `$name' already exists. Remove first");
273 my $data = join('', <DATA>);
274 $data =~ s|%NAME%|$name|sg;
275 $data =~ s|%TARGET%|$CFG_TARGET|sg;
276 $data =~ s|%PREFIX%|$prefix|sg;
277 $data =~ s|%INSTALLBUILDDIR%|$installbuilddir|sg;
279 my ($mkf, $mods, $src) = ($data =~ m|^(.+)-=#=-\n(.+)-=#=-\n(.+)|s);
281 notice("Creating [DIR] $name");
282 system("mkdir $name");
283 notice("Creating [FILE] $name/Makefile");
284 open(FP, ">${name}/Makefile") || die;
287 notice("Creating [FILE] $name/modules.mk");
288 open(FP, ">${name}/modules.mk") || die;
291 notice("Creating [FILE] $name/mod_$name.c");
292 open(FP, ">${name}/mod_${name}.c") || die;
295 notice("Creating [FILE] $name/.deps");
296 system("touch ${name}/.deps");
308 $result = get_vars(@args);
311 # -q without var name prints all variables and their values
313 # Additional -v pretty-prints output
315 # Variable names in alphabetic order
316 my @vars = sort {uc($a) cmp uc($b)} keys %config_vars;
318 # Make the left column as wide as the longest variable name
322 $width = $l unless ($l <= $width);
326 printf "%-${width}s = %s\n", $_, $config_vars{$_};
329 # Unprettified name=value list
330 foreach (keys %config_vars) {
331 print "$_=$config_vars{$_}\n";
337 my $apr_config = get_vars("APR_CONFIG");
339 if (! -x "$apr_config") {
340 error("$apr_config not found!");
344 my $apr_major_version = (split /\./, `$apr_config --version`)[0];
347 if ($apr_major_version < 2) {
348 $apu_config = get_vars("APU_CONFIG");
350 if (! -x "$apu_config") {
351 error("$apu_config not found!");
356 my $libtool = `$apr_config --apr-libtool`;
359 my $apr_includedir = `$apr_config --includes`;
360 chomp($apr_includedir);
361 my $apu_includedir = "";
362 if ($apr_major_version < 2) {
363 $apu_includedir = `$apu_config --includes`;
364 chomp($apu_includedir);
369 ## SHARED OBJECT COMPILATION
372 # split files into sources and objects
385 # determine output file
389 $dso_file = $srcs[0];
390 $dso_file =~ s|\.[^.]+$|.la|;
392 elsif ($#objs > -1) {
393 $dso_file = $objs[0];
394 $dso_file =~ s|\.[^.]+$|.la|;
397 $dso_file = "mod_unknown.la";
402 $dso_file =~ s|\.[^.]+$|.la|;
405 # create compilation commands
408 my ($opt_Wc, $opt_I, $opt_D);
409 foreach $opt_Wc (@opt_W) {
410 $opt .= "$1 " if ($opt_Wc =~ m|^\s*c,(.*)$|);
412 foreach $opt_I (@opt_I) {
415 foreach $opt_D (@opt_D) {
418 my $cflags = "$CFG_CFLAGS";
423 $slo =~ s|\.c$|.slo|;
430 push(@cmds, "$libtool $ltflags --mode=compile $CFG_CC $cflags -I$CFG_INCLUDEDIR $apr_includedir $apu_includedir $opt -c -o $lo $s && touch $slo");
434 # create link command
440 my ($opt_Wl, $opt_L, $opt_l);
442 foreach $opt_Wl (@opt_W) {
443 $opt .= "$1 " if ($opt_Wl =~ m|^\s*l,(.*)$|);
445 foreach $opt_L (@opt_L) {
448 foreach $opt_l (@opt_l) {
454 my $apr_libs=`$apr_config --cflags --ldflags --link-libtool --libs`;
457 if ($apr_major_version < 2) {
458 $apu_libs=`$apu_config --ldflags --link-libtool --libs`;
462 $opt .= " ".$apu_libs." ".$apr_libs;
465 my $apr_ldflags=`$apr_config --ldflags`;
467 $opt .= " -rpath $CFG_LIBEXECDIR -module -avoid-version $apr_ldflags";
470 push(@cmds, "$libtool $ltflags --mode=link $CFG_CC -o $dso_file $opt $lo");
472 # execute the commands
473 &execute_cmds(@cmds);
475 # allow one-step compilation and installation
476 if ($opt_i or $opt_e) {
477 @args = ( $dso_file );
481 if ($opt_i or $opt_e) {
483 ## SHARED OBJECT INSTALLATION
486 # determine installation commands
487 # and corresponding LoadModule directive
492 # ack all potential gcc, hp/ux, win32+os2+aix and os/x extensions
493 if ($f !~ m#(\.so$|\.la$|\.sl$|\.dll$|\.dylib$|)#) {
494 error("file $f is not a shared object");
498 $t =~ s|^.+/([^/]+)$|$1|;
499 # use .so unambigiously for installed shared library modules
500 $t =~ s|\.[^./\\]+$|\.so|;
502 push(@cmds, "$installbuilddir/instdso.sh SH_LIBTOOL='" .
503 "$libtool' $f $CFG_LIBEXECDIR");
504 push(@cmds, "chmod 755 $CFG_LIBEXECDIR/$t");
507 # determine module symbolname and filename
509 if ($name eq 'unknown') {
512 $base =~ s|\.[^.]+$||;
514 open(FP, "<$base.c");
515 my $content = join('', <FP>);
517 if ($content =~ m|.*module\s+(?:AP_MODULE_DECLARE_DATA\s+)?([a-zA-Z0-9_]+)_module\s*=\s*.*|s) {
519 $filename = "$base.c";
520 $filename =~ s|^[^/]+/||;
524 if ($base =~ m|.*mod_([a-zA-Z0-9_]+)\..+|) {
527 $filename =~ s|^[^/]+/||;
531 error("Sorry, cannot determine bootstrap symbol name");
532 error("Please specify one with option `-n'");
536 if ($filename eq '') {
537 $filename = "mod_${name}.c";
539 my $dir = $CFG_LIBEXECDIR;
540 $dir =~ s|^$CFG_PREFIX/?||;
543 push(@lmd, sprintf("LoadModule %-18s %s", "${name}_module", "$dir$t"));
546 # execute the commands
547 &execute_cmds(@cmds);
549 # activate module via LoadModule/AddModule directive
550 if ($opt_a or $opt_A) {
551 if (not -f "$CFG_SYSCONFDIR/$CFG_TARGET.conf") {
552 error("Config file $CFG_SYSCONFDIR/$CFG_TARGET.conf not found");
556 open(FP, "<$CFG_SYSCONFDIR/$CFG_TARGET.conf") || die;
557 my $content = join('', <FP>);
560 if ($content !~ m|\n#?\s*LoadModule\s+|) {
561 error("Activation failed for custom $CFG_SYSCONFDIR/$CFG_TARGET.conf file.");
562 error("At least one `LoadModule' directive already has to exist.");
568 $c = '#' if ($opt_A);
569 foreach $lmd (@lmd) {
570 my $what = $opt_A ? "preparing" : "activating";
571 if ($content !~ m|\n#?\s*$lmd|) {
572 # check for open <containers>, so that the new LoadModule
573 # directive always appears *outside* of an <container>.
575 my $before = ($content =~ m|^(.*\n)#?\s*LoadModule\s+[^\n]+\n|s)[0];
577 # the '()=' trick forces list context and the scalar
578 # assignment counts the number of list members (aka number
580 my $cntopen = () = ($before =~ m|^\s*<[^/].*$|mg);
581 my $cntclose = () = ($before =~ m|^\s*</.*$|mg);
583 if ($cntopen == $cntclose) {
584 # fine. Last LoadModule is contextless.
585 $content =~ s|^(.*\n#?\s*LoadModule\s+[^\n]+\n)|$1$c$lmd\n|s;
587 elsif ($cntopen < $cntclose) {
588 error('Configuration file is not valid. There are sections'
589 . ' closed before opened.');
593 # put our cmd after the section containing the last
596 $content =~ s!\A ( # string and capture start
598 ^\s* # start of conf line with a
599 (?:[^<]|<[^/]) # directive which does not
602 .*(?:$)\n # rest of the line.
603 # the '$' is in parentheses
604 # to avoid misinterpreting
608 )* # catch as much as possible
609 # of such lines. (including
612 ^\s*</.*(?:$)\n? # after the above, we
613 # expect a config line with
614 # a closing container (</)
616 ) {$cntopen} # the whole pattern (bunch
617 # of lines that end up with
618 # a closing directive) must
619 # be repeated $cntopen
627 error('Configuration file is not valid. There are '
628 . 'sections opened and not closed.');
633 # replace already existing LoadModule line
634 $content =~ s|^(.*\n)#?\s*$lmd[^\n]*\n|$1$c$lmd\n|s;
636 $lmd =~ m|LoadModule\s+(.+?)_module.*|;
637 notice("[$what module `$1' in $CFG_SYSCONFDIR/$CFG_TARGET.conf]");
640 if (open(FP, ">$CFG_SYSCONFDIR/$CFG_TARGET.conf.new")) {
643 system("cp $CFG_SYSCONFDIR/$CFG_TARGET.conf $CFG_SYSCONFDIR/$CFG_TARGET.conf.bak && " .
644 "cp $CFG_SYSCONFDIR/$CFG_TARGET.conf.new $CFG_SYSCONFDIR/$CFG_TARGET.conf && " .
645 "rm $CFG_SYSCONFDIR/$CFG_TARGET.conf.new");
647 notice("unable to open configuration file");
654 print STDERR "apxs:Error: $_[0].\n";
658 print STDERR "$_[0]\n";
664 ## Makefile -- Build procedure for sample %NAME% Apache module
665 ## Autogenerated via ``apxs -n %NAME% -g''.
670 top_builddir=%PREFIX%
671 include %INSTALLBUILDDIR%/special.mk
677 # additional defines, includes and libraries
678 #DEFS=-Dmy_define=my_value
679 #INCLUDES=-Imy/include/dir
680 #LIBS=-Lmy/lib/dir -lmylib
683 all: local-shared-build
685 # install the shared object file into Apache
686 install: install-modules-yes
690 -rm -f mod_%NAME%.o mod_%NAME%.lo mod_%NAME%.slo mod_%NAME%.la
694 lynx -mime_header http://localhost/%NAME%
696 # install and activate shared object by reloading Apache to
697 # force a reload of the shared object file
698 reload: install restart
700 # the general Apache start/restart/stop
710 mod_%NAME%.la: mod_%NAME%.slo
711 $(SH_LINK) -rpath $(libexecdir) -module -avoid-version mod_%NAME%.lo
712 DISTCLEAN_TARGETS = modules.mk
713 shared = mod_%NAME%.la
716 ** mod_%NAME%.c -- Apache sample %NAME% module
717 ** [Autogenerated via ``apxs -n %NAME% -g'']
719 ** To play with this sample module first compile it into a
720 ** DSO file and install it into Apache's modules directory
723 ** $ apxs -c -i mod_%NAME%.c
725 ** Then activate it in Apache's %TARGET%.conf file for instance
726 ** for the URL /%NAME% in as follows:
729 ** LoadModule %NAME%_module modules/mod_%NAME%.so
730 ** <Location /%NAME%>
734 ** Then after restarting Apache via
736 ** $ apachectl restart
738 ** you immediately can request the URL /%NAME% and watch for the
739 ** output of this module. This can be achieved for instance via:
741 ** $ lynx -mime_header http://localhost/%NAME%
743 ** The output should be similar to the following one:
746 ** Date: Tue, 31 Mar 1998 14:42:22 GMT
747 ** Server: Apache/1.3.4 (Unix)
749 ** Content-Type: text/html
751 ** The sample page from mod_%NAME%.c
755 #include "http_config.h"
756 #include "http_protocol.h"
757 #include "ap_config.h"
759 /* The sample content handler */
760 static int %NAME%_handler(request_rec *r)
762 if (strcmp(r->handler, "%NAME%")) {
765 r->content_type = "text/html";
768 ap_rputs("The sample page from mod_%NAME%.c\n", r);
772 static void %NAME%_register_hooks(apr_pool_t *p)
774 ap_hook_handler(%NAME%_handler, NULL, NULL, APR_HOOK_MIDDLE);
777 /* Dispatch list for API hooks */
778 module AP_MODULE_DECLARE_DATA %NAME%_module = {
779 STANDARD20_MODULE_STUFF,
780 NULL, /* create per-dir config structures */
781 NULL, /* merge per-dir config structures */
782 NULL, /* create per-server config structures */
783 NULL, /* merge per-server config structures */
784 NULL, /* table of config file commands */
785 %NAME%_register_hooks /* register hooks */