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 = "/home/rbb/apachebin";
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 if (not -x "$CFG_SBINDIR/$CFG_TARGET") {
223 print STDERR "apxs:Error: $CFG_SBINDIR/$CFG_TARGET not found or not executable\n";
226 if (not grep(/mod_so/, `$CFG_SBINDIR/$CFG_TARGET -l`)) {
227 print STDERR "apxs:Error: Sorry, no shared object support for Apache\n";
228 print STDERR "apxs:Error: available under your platform. Make sure\n";
229 print STDERR "apxs:Error: the Apache module mod_so is compiled into\n";
230 print STDERR "apxs:Error: your server binary `$CFG_SBINDIR/$CFG_TARGET'.\n";
238 # helper function for executing a list of
239 # system command with return code checks
244 foreach $cmd (@cmds) {
245 print STDERR "$cmd\n";
246 $rc = system("$cmd");
248 printf(STDERR "apxs:Break: Command failed with rc=%d\n", $rc << 8);
256 ## SAMPLE MODULE SOURCE GENERATION
260 print STDERR "apxs:Error: Directory `$name' already exists. Remove first\n";
264 my $data = join('', <DATA>);
265 $data =~ s|%NAME%|$name|sg;
266 $data =~ s|%TARGET%|$CFG_TARGET|sg;
268 my ($mkf, $src) = ($data =~ m|^(.+)-=#=-\n(.+)|s);
270 print STDERR "Creating [DIR] $name\n";
271 system("mkdir $name");
272 print STDERR "Creating [FILE] $name/Makefile\n";
273 open(FP, ">${name}/Makefile") || die;
276 print STDERR "Creating [FILE] $name/mod_$name.c\n";
277 open(FP, ">${name}/mod_${name}.c") || die;
292 foreach $arg (@args) {
296 TARGET CC CFLAGS CFLAGS_SHLIB LD_SHLIB LDFLAGS_SHLIB LIBS_SHLIB
297 PREFIX SBINDIR INCLUDEDIR LIBEXECDIR SYSCONFDIR
299 if ($arg eq $name or $arg eq lc($name)) {
300 my $val = eval "\$CFG_$name";
301 $result .= "${val}::";
306 printf(STDERR "apxs:Error: Invalid query string `%s'\n", $arg);
317 ## SHARED OBJECT COMPILATION
320 # split files into sources and objects
333 # determine output file
337 $dso_file = $srcs[0];
338 $dso_file =~ s|\.[^.]+$|.la|;
340 elsif ($#objs > -1) {
341 $dso_file = $objs[0];
342 $dso_file =~ s|\.[^.]+$|.la|;
345 $dso_file = "mod_unknown.so";
352 # create compilation commands
355 my ($opt_Wc, $opt_I, $opt_D);
356 foreach $opt_Wc (@opt_W) {
357 $opt .= "$1 " if ($opt_Wc =~ m|^\s*c,(.*)$|);
359 foreach $opt_I (@opt_I) {
362 foreach $opt_D (@opt_D) {
365 my $cflags = "$CFG_CFLAGS";
369 $slo =~ s|\.c$|.slo|;
374 push(@cmds, "libtool --silent --mode=compile $CFG_CC $cflags -I$CFG_INCLUDEDIR -I$CFG_INCLUDEDIR/apr $opt -c $s && touch $slo");
375 push(@cmds, "libtool --silent --mode=link $CFG_CC $cflags -o $la -rpath $CFG_LIBEXECDIR -module -avoid-version $lo");
379 # execute the commands
380 &execute_cmds(@cmds);
382 # allow one-step compilation and installation
383 if ($opt_i or $opt_e) {
384 @args = ( $dso_file );
388 if ($opt_i or $opt_e) {
390 ## SHARED OBJECT INSTALLATION
393 # determine installation commands
394 # and corresponding LoadModule/AddModule directives
400 if ($f !~ m|\.la$|) {
401 print STDERR "apxs:Error: file $f is not a shared object\n";
405 $t =~ s|^.+/([^/]+)$|$1|;
407 push(@cmds, "libtool --mode=install cp $f $CFG_LIBEXECDIR/$t");
408 push(@cmds, "chmod 755 $CFG_LIBEXECDIR/$t");
411 # determine module symbolname and filename
413 if ($name eq 'unknown') {
416 $base =~ s|\.[^.]+$||;
418 open(FP, "<$base.c");
419 my $content = join('', <FP>);
421 if ($content =~ m|.*module\s+(?:MODULE_VAR_EXPORT\s+)?([a-zA-Z0-9_]+)_module\s*=\s*.*|s) {
423 $filename = "$base.c";
424 $filename =~ s|^[^/]+/||;
428 if ($base =~ m|.*mod_([a-zA-Z0-9_]+)\..+|) {
431 $filename =~ s|^[^/]+/||;
435 print "apxs:Error: Sorry, cannot determine bootstrap symbol name\n";
436 print "apxs:Error: Please specify one with option `-n'\n";
440 if ($filename eq '') {
441 $filename = "mod_${name}.c";
443 my $dir = $CFG_LIBEXECDIR;
444 $dir =~ s|^$CFG_PREFIX/?||;
446 push(@lmd, sprintf("LoadModule %-18s %s", "${name}_module", "$dir$t"));
447 push(@amd, sprintf("AddModule %s", $filename));
450 # execute the commands
451 &execute_cmds(@cmds);
453 # activate module via LoadModule/AddModule directive
454 if ($opt_a or $opt_A) {
455 if (not -f "$CFG_SYSCONFDIR/$CFG_TARGET.conf") {
456 print "apxs:Error: Config file $CFG_SYSCONFDIR/$CFG_TARGET.conf not found\n";
460 open(FP, "<$CFG_SYSCONFDIR/$CFG_TARGET.conf") || die;
461 my $content = join('', <FP>);
464 if ($content !~ m|\n#?\s*LoadModule\s+|) {
465 print STDERR "apxs:Error: Activation failed for custom $CFG_SYSCONFDIR/$CFG_TARGET.conf file.\n";
466 print STDERR "apxs:Error: At least one `LoadModule' directive already has to exist.\n";
472 $c = '#' if ($opt_A);
473 foreach $lmd (@lmd) {
474 my $what = $opt_A ? "preparing" : "activating";
475 if ($content !~ m|\n#?\s*$lmd|) {
476 $content =~ s|^(.*\n#?\s*LoadModule\s+[^\n]+\n)|$1$c$lmd\n|sg;
478 $content =~ s|^(.*\n)#?\s*$lmd[^\n]*\n|$1$c$lmd\n|sg;
480 $lmd =~ m|LoadModule\s+(.+?)_module.*|;
481 print STDERR "[$what module `$1' in $CFG_SYSCONFDIR/$CFG_TARGET.conf]\n";
484 foreach $amd (@amd) {
485 if ($content !~ m|\n#?\s*$amd|) {
486 $content =~ s|^(.*\n#?\s*AddModule\s+[^\n]+\n)|$1$c$amd\n|sg;
488 $content =~ s|^(.*\n)#?\s*$amd[^\n]*\n|$1$c$amd\n|sg;
492 if (open(FP, ">$CFG_SYSCONFDIR/$CFG_TARGET.conf.new")) {
495 system("cp $CFG_SYSCONFDIR/$CFG_TARGET.conf $CFG_SYSCONFDIR/$CFG_TARGET.conf.bak && " .
496 "cp $CFG_SYSCONFDIR/$CFG_TARGET.conf.new $CFG_SYSCONFDIR/$CFG_TARGET.conf && " .
497 "rm $CFG_SYSCONFDIR/$CFG_TARGET.conf.new");
499 print STDERR "unable to open configuration file\n";
508 ## Makefile -- Build procedure for sample %NAME% Apache module
509 ## Autogenerated via ``apxs -n %NAME% -g''.
516 # additional defines, includes and libraries
517 #DEF=-Dmy_define=my_value
518 #INC=-Imy/include/dir
519 #LIB=-Lmy/lib/dir -lmylib
524 # compile the shared object file
525 mod_%NAME%.la: mod_%NAME%.c
526 $(APXS) -c $(DEF) $(INC) $(LIB) mod_%NAME%.c
528 # install the shared object file into Apache
530 $(APXS) -i -a -n '%NAME%' mod_%NAME%.la
534 -rm -f mod_%NAME%.o mod_%NAME%.lo mod_%NAME%.slo mod_%NAME%.la
538 lynx -mime_header http://localhost/%NAME%
540 # install and activate shared object by reloading Apache to
541 # force a reload of the shared object file
542 reload: install restart
544 # the general Apache start/restart/stop
555 ** mod_%NAME%.c -- Apache sample %NAME% module
556 ** [Autogenerated via ``apxs -n %NAME% -g'']
558 ** To play with this sample module first compile it into a
559 ** DSO file and install it into Apache's libexec directory
562 ** $ apxs -c -i mod_%NAME%.c
564 ** Then activate it in Apache's %TARGET%.conf file for instance
565 ** for the URL /%NAME% in as follows:
568 ** LoadModule %NAME%_module libexec/mod_%NAME%.so
569 ** <Location /%NAME%>
573 ** Then after restarting Apache via
575 ** $ apachectl restart
577 ** you immediately can request the URL /%NAME and watch for the
578 ** output of this module. This can be achieved for instance via:
580 ** $ lynx -mime_header http://localhost/%NAME%
582 ** The output should be similar to the following one:
585 ** Date: Tue, 31 Mar 1998 14:42:22 GMT
586 ** Server: Apache/1.3.4 (Unix)
588 ** Content-Type: text/html
590 ** The sample page from mod_%NAME%.c
594 #include "http_config.h"
595 #include "http_protocol.h"
596 #include "ap_config.h"
598 /* The sample content handler */
599 static int %NAME%_handler(request_rec *r)
601 r->content_type = "text/html";
602 ap_send_http_header(r);
604 ap_rputs("The sample page from mod_%NAME%.c\n", r);
608 /* Dispatch list of content handlers */
609 static const handler_rec %NAME%_handlers[] = {
610 { "%NAME%", %NAME%_handler },
614 /* Dispatch list for API hooks */
615 module MODULE_VAR_EXPORT %NAME%_module = {
616 STANDARD20_MODULE_STUFF,
617 NULL, /* create per-dir config structures */
618 NULL, /* merge per-dir config structures */
619 NULL, /* create per-server config structures */
620 NULL, /* merge per-server config structures */
621 NULL, /* table of config file commands */
622 %NAME%_handlers, /* [#8] MIME-typed-dispatched handlers */
623 NULL /* register hooks */