2 # ====================================================================
3 # The Apache Software License, Version 1.1
5 # Copyright (c) 2000 The Apache Software Foundation. All rights
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
12 # 1. Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in
17 # the documentation and/or other materials provided with the
20 # 3. The end-user documentation included with the redistribution,
21 # if any, must include the following acknowledgment:
22 # "This product includes software developed by the
23 # Apache Software Foundation (http://www.apache.org/)."
24 # Alternately, this acknowledgment may appear in the software itself,
25 # if and wherever such third-party acknowledgments normally appear.
27 # 4. The names "Apache" and "Apache Software Foundation" must
28 # not be used to endorse or promote products derived from this
29 # software without prior written permission. For written
30 # permission, please contact apache@apache.org.
32 # 5. Products derived from this software may not be called "Apache",
33 # nor may "Apache" appear in their name, without prior written
34 # permission of the Apache Software Foundation.
36 # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 # DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40 # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 # ====================================================================
50 # This software consists of voluntary contributions made by many
51 # individuals on behalf of the Apache Software Foundation. For more
52 # information on the Apache Software Foundation, please see
53 # <http://www.apache.org/>.
56 ## apxs -- APache eXtenSion tool
57 ## Written by Ralf S. Engelschall <rse@apache.org>
68 my $CFG_TARGET = '@progname@';
70 my $CFG_DEFS = '@DEFS@';
71 my $CFG_CFLAGS = '@CFLAGS@ @EXTRA_CFLAGS@';
72 my $CFG_PREFIX = "@prefix@";
73 my $prefix = "$CFG_PREFIX";
74 my $CFG_EXEC_PREFIX = "@exec_prefix@";
75 my $exec_prefix = "$CFG_EXEC_PREFIX";
76 my $CFG_SBINDIR = "@bindir@";
77 my $CFG_INCLUDEDIR = "@includedir@";
78 my $CFG_LIBEXECDIR = "@libexecdir@";
79 my $CFG_SYSCONFDIR = "@sysconfdir@";
83 ## Cleanup the above stuff
85 $CFG_CFLAGS =~ s|^\s+||;
86 $CFG_CFLAGS =~ s|\s+$||;
87 $CFG_CFLAGS =~ s|\s+`.+apaci`||;
90 ## parse argument line
93 # defaults for parameters
110 # this subroutine is derived from Perl's getopts.pl with the enhancement of
111 # the "+" metacharater at the format string to allow a list to be build by
112 # subsequent occurance of the same option.
114 my ($argumentative, @ARGV) = @_;
115 my (@args, $first, $rest, $pos);
120 @args = split( / */, $argumentative);
121 while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
122 ($first, $rest) = ($1,$2);
127 $pos = index($argumentative,$first);
129 if($args[$pos+1] eq ':') {
133 print STDERR "apxs:Error: Incomplete option: $first (needs an argument)\n";
136 $rest = shift(@ARGV);
138 eval "\$opt_$first = \$rest;";
140 elsif ($args[$pos+1] eq '+') {
144 print STDERR "apxs:Error: Incomplete option: $first (needs an argument)\n";
147 $rest = shift(@ARGV);
149 eval "push(\@opt_$first, \$rest);";
152 eval "\$opt_$first = 1";
162 print STDERR "apxs:Error: Unknown option: $first\n";
172 return ($errs == 0, @ARGV);
176 print STDERR "Usage: apxs -g [-S <var>=<val>] -n <modname>\n";
177 print STDERR " apxs -q [-S <var>=<val>] <query> ...\n";
178 print STDERR " apxs -c [-S <var>=<val>] [-o <dsofile>] [-D <name>[=<value>]]\n";
179 print STDERR " [-I <incdir>] [-L <libdir>] [-l <libname>] [-Wc,<flags>]\n";
180 print STDERR " [-Wl,<flags>] <files> ...\n";
181 print STDERR " apxs -i [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
182 print STDERR " apxs -e [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
188 ($rc, @ARGV) = &Getopts("qn:gco:I+D+L+l+W+S+eiaA", @ARGV);
189 &usage if ($rc == 0);
190 &usage if ($#ARGV == -1 and not $opt_g);
191 &usage if (not $opt_q and not ($opt_g and $opt_n) and not $opt_i and not $opt_c and not $opt_e);
195 my $name = 'unknown';
196 $name = $opt_n if ($opt_n ne '');
200 foreach $opt_S (@opt_S) {
201 if ($opt_S =~ m/^([^=]+)=(.*)$/) {
204 my $oldval = eval "\$CFG_$var";
206 unless ($var and $oldval) {
207 print STDERR "apxs:Error: no config variable $var\n";
211 eval "\$CFG_${var}=\"${val}\"";
213 print STDERR "apxs:Error: malformatted -S option\n";
220 ## Initial shared object support check
222 my $httpd = "$CFG_SBINDIR/$CFG_TARGET";
224 #allow apxs to be run from the source tree, before installation
225 if ($0 =~ m:support/apxs$:) {
226 ($httpd = $0) =~ s:support/apxs$::;
227 $httpd .= $CFG_TARGET;
231 print STDERR "apxs:Error: $httpd not found or not executable\n";
234 if (not grep(/mod_so/, `$httpd -l`)) {
235 print STDERR "apxs:Error: Sorry, no shared object support for Apache\n";
236 print STDERR "apxs:Error: available under your platform. Make sure\n";
237 print STDERR "apxs:Error: the Apache module mod_so is compiled into\n";
238 print STDERR "apxs:Error: your server binary `$httpd'.\n";
246 # helper function for executing a list of
247 # system command with return code checks
252 foreach $cmd (@cmds) {
253 print STDERR "$cmd\n";
254 $rc = system("$cmd");
256 printf(STDERR "apxs:Break: Command failed with rc=%d\n", $rc << 8);
264 ## SAMPLE MODULE SOURCE GENERATION
268 print STDERR "apxs:Error: Directory `$name' already exists. Remove first\n";
272 my $data = join('', <DATA>);
273 $data =~ s|%NAME%|$name|sg;
274 $data =~ s|%TARGET%|$CFG_TARGET|sg;
276 my ($mkf, $src) = ($data =~ m|^(.+)-=#=-\n(.+)|s);
278 print STDERR "Creating [DIR] $name\n";
279 system("mkdir $name");
280 print STDERR "Creating [FILE] $name/Makefile\n";
281 open(FP, ">${name}/Makefile") || die;
284 print STDERR "Creating [FILE] $name/mod_$name.c\n";
285 open(FP, ">${name}/mod_${name}.c") || die;
300 foreach $arg (@args) {
304 TARGET CC CFLAGS CFLAGS_SHLIB LD_SHLIB LDFLAGS_SHLIB LIBS_SHLIB
305 PREFIX SBINDIR INCLUDEDIR LIBEXECDIR SYSCONFDIR
307 if ($arg eq $name or $arg eq lc($name)) {
308 my $val = eval "\$CFG_$name";
309 $result .= "${val}::";
314 printf(STDERR "apxs:Error: Invalid query string `%s'\n", $arg);
325 ## SHARED OBJECT COMPILATION
328 # split files into sources and objects
341 # determine output file
345 $dso_file = $srcs[0];
346 $dso_file =~ s|\.[^.]+$|.la|;
348 elsif ($#objs > -1) {
349 $dso_file = $objs[0];
350 $dso_file =~ s|\.[^.]+$|.la|;
353 $dso_file = "mod_unknown.so";
360 # create compilation commands
363 my ($opt_Wc, $opt_I, $opt_D);
364 foreach $opt_Wc (@opt_W) {
365 $opt .= "$1 " if ($opt_Wc =~ m|^\s*c,(.*)$|);
367 foreach $opt_I (@opt_I) {
370 foreach $opt_D (@opt_D) {
373 my $cflags = "$CFG_CFLAGS";
377 $slo =~ s|\.c$|.slo|;
382 push(@cmds, "libtool --silent --mode=compile $CFG_CC $cflags -I$CFG_INCLUDEDIR $opt -c $s && touch $slo");
383 push(@cmds, "libtool --silent --mode=link $CFG_CC $cflags -o $la -rpath $CFG_LIBEXECDIR -module -avoid-version $lo");
387 # execute the commands
388 &execute_cmds(@cmds);
390 # allow one-step compilation and installation
391 if ($opt_i or $opt_e) {
392 @args = ( $dso_file );
396 if ($opt_i or $opt_e) {
398 ## SHARED OBJECT INSTALLATION
401 # determine installation commands
402 # and corresponding LoadModule/AddModule directives
408 if ($f !~ m#(\.so$|\.la$)#) {
409 print STDERR "apxs:Error: file $f is not a shared object\n";
413 $t =~ s|^.+/([^/]+)$|$1|;
415 push(@cmds, "libtool --mode=install cp $f $CFG_LIBEXECDIR/$t");
416 push(@cmds, "chmod 755 $CFG_LIBEXECDIR/$t");
419 # determine module symbolname and filename
421 if ($name eq 'unknown') {
424 $base =~ s|\.[^.]+$||;
426 open(FP, "<$base.c");
427 my $content = join('', <FP>);
429 if ($content =~ m|.*module\s+(?:AP_MODULE_DECLARE_DATA\s+)?([a-zA-Z0-9_]+)_module\s*=\s*.*|s) {
431 $filename = "$base.c";
432 $filename =~ s|^[^/]+/||;
436 if ($base =~ m|.*mod_([a-zA-Z0-9_]+)\..+|) {
439 $filename =~ s|^[^/]+/||;
443 print "apxs:Error: Sorry, cannot determine bootstrap symbol name\n";
444 print "apxs:Error: Please specify one with option `-n'\n";
448 if ($filename eq '') {
449 $filename = "mod_${name}.c";
451 my $dir = $CFG_LIBEXECDIR;
452 $dir =~ s|^$CFG_PREFIX/?||;
455 push(@lmd, sprintf("LoadModule %-18s %s", "${name}_module", "$dir$t"));
456 push(@amd, sprintf("AddModule %s", $filename));
459 # execute the commands
460 &execute_cmds(@cmds);
462 # activate module via LoadModule/AddModule directive
463 if ($opt_a or $opt_A) {
464 if (not -f "$CFG_SYSCONFDIR/$CFG_TARGET.conf") {
465 print "apxs:Error: Config file $CFG_SYSCONFDIR/$CFG_TARGET.conf not found\n";
469 open(FP, "<$CFG_SYSCONFDIR/$CFG_TARGET.conf") || die;
470 my $content = join('', <FP>);
473 if ($content !~ m|\n#?\s*LoadModule\s+|) {
474 print STDERR "apxs:Error: Activation failed for custom $CFG_SYSCONFDIR/$CFG_TARGET.conf file.\n";
475 print STDERR "apxs:Error: At least one `LoadModule' directive already has to exist.\n";
481 $c = '#' if ($opt_A);
482 foreach $lmd (@lmd) {
483 my $what = $opt_A ? "preparing" : "activating";
484 if ($content !~ m|\n#?\s*$lmd|) {
485 $content =~ s|^(.*\n#?\s*LoadModule\s+[^\n]+\n)|$1$c$lmd\n|sg;
487 $content =~ s|^(.*\n)#?\s*$lmd[^\n]*\n|$1$c$lmd\n|sg;
489 $lmd =~ m|LoadModule\s+(.+?)_module.*|;
490 print STDERR "[$what module `$1' in $CFG_SYSCONFDIR/$CFG_TARGET.conf]\n";
493 foreach $amd (@amd) {
494 if ($content !~ m|\n#?\s*$amd|) {
495 $content =~ s|^(.*\n#?\s*AddModule\s+[^\n]+\n)|$1$c$amd\n|sg;
497 $content =~ s|^(.*\n)#?\s*$amd[^\n]*\n|$1$c$amd\n|sg;
501 if (open(FP, ">$CFG_SYSCONFDIR/$CFG_TARGET.conf.new")) {
504 system("cp $CFG_SYSCONFDIR/$CFG_TARGET.conf $CFG_SYSCONFDIR/$CFG_TARGET.conf.bak && " .
505 "cp $CFG_SYSCONFDIR/$CFG_TARGET.conf.new $CFG_SYSCONFDIR/$CFG_TARGET.conf && " .
506 "rm $CFG_SYSCONFDIR/$CFG_TARGET.conf.new");
508 print STDERR "unable to open configuration file\n";
517 ## Makefile -- Build procedure for sample %NAME% Apache module
518 ## Autogenerated via ``apxs -n %NAME% -g''.
525 # additional defines, includes and libraries
526 #DEF=-Dmy_define=my_value
527 #INC=-Imy/include/dir
528 #LIB=-Lmy/lib/dir -lmylib
533 # compile the shared object file
534 mod_%NAME%.la: mod_%NAME%.c
535 $(APXS) -c $(DEF) $(INC) $(LIB) mod_%NAME%.c
537 # install the shared object file into Apache
539 $(APXS) -i -a -n '%NAME%' mod_%NAME%.la
543 -rm -f mod_%NAME%.o mod_%NAME%.lo mod_%NAME%.slo mod_%NAME%.la
547 lynx -mime_header http://localhost/%NAME%
549 # install and activate shared object by reloading Apache to
550 # force a reload of the shared object file
551 reload: install restart
553 # the general Apache start/restart/stop
564 ** mod_%NAME%.c -- Apache sample %NAME% module
565 ** [Autogenerated via ``apxs -n %NAME% -g'']
567 ** To play with this sample module first compile it into a
568 ** DSO file and install it into Apache's modules directory
571 ** $ apxs -c -i mod_%NAME%.c
573 ** Then activate it in Apache's %TARGET%.conf file for instance
574 ** for the URL /%NAME% in as follows:
577 ** LoadModule %NAME%_module modules/mod_%NAME%.so
578 ** <Location /%NAME%>
582 ** Then after restarting Apache via
584 ** $ apachectl restart
586 ** you immediately can request the URL /%NAME% and watch for the
587 ** output of this module. This can be achieved for instance via:
589 ** $ lynx -mime_header http://localhost/%NAME%
591 ** The output should be similar to the following one:
594 ** Date: Tue, 31 Mar 1998 14:42:22 GMT
595 ** Server: Apache/1.3.4 (Unix)
597 ** Content-Type: text/html
599 ** The sample page from mod_%NAME%.c
603 #include "http_config.h"
604 #include "http_protocol.h"
605 #include "ap_config.h"
607 /* The sample content handler */
608 static int %NAME%_handler(request_rec *r)
610 if (strcmp(r->handler, "%NAME%")) {
613 r->content_type = "text/html";
614 ap_send_http_header(r);
616 ap_rputs("The sample page from mod_%NAME%.c\n", r);
620 static void %NAME%_register_hooks(apr_pool_t *p)
622 ap_hook_handler(%NAME%_handler, NULL, NULL, APR_HOOK_LAST);
625 /* Dispatch list for API hooks */
626 module AP_MODULE_DECLARE_DATA %NAME%_module = {
627 STANDARD20_MODULE_STUFF,
628 NULL, /* create per-dir config structures */
629 NULL, /* merge per-dir config structures */
630 NULL, /* create per-server config structures */
631 NULL, /* merge per-server config structures */
632 NULL, /* table of config file commands */
633 %NAME%_register_hooks /* register hooks */