]> granicus.if.org Git - apache/blob - support/apxs.in
adjust to ap_hook_handler changes
[apache] / support / apxs.in
1 #!@perlbin@
2 # ====================================================================
3 # The Apache Software License, Version 1.1
4 #
5 # Copyright (c) 2000 The Apache Software Foundation.  All rights
6 # reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # 1. Redistributions of source code must retain the above copyright
13 #    notice, this list of conditions and the following disclaimer.
14 #
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
18 #    distribution.
19 #
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.
26 #
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.
31 #
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.
35 #
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
47 # SUCH DAMAGE.
48 # ====================================================================
49 #
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/>.
54 #
55 ##
56 ##  apxs -- APache eXtenSion tool
57 ##  Written by Ralf S. Engelschall <rse@apache.org>
58 ##
59
60 require 5.003;
61 use strict;
62 package apxs;
63
64 ##
65 ##  Configuration
66 ##
67
68 my $CFG_TARGET        = '@progname@';
69 my $CFG_CC            = '@CC@';
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@";
80
81
82 ##
83 ##  Cleanup the above stuff
84 ##
85 $CFG_CFLAGS =~ s|^\s+||;
86 $CFG_CFLAGS =~ s|\s+$||;
87 $CFG_CFLAGS =~ s|\s+`.+apaci`||;
88
89 ##
90 ##  parse argument line
91 ##
92
93 #   defaults for parameters
94 my $opt_n = '';
95 my $opt_g = '';
96 my $opt_c = 0;
97 my $opt_o = '';
98 my @opt_D = ();
99 my @opt_I = ();
100 my @opt_L = ();
101 my @opt_l = ();
102 my @opt_W = ();
103 my @opt_S = ();
104 my $opt_e = 0;
105 my $opt_i = 0;
106 my $opt_a = 0;
107 my $opt_A = 0;
108 my $opt_q = 0;
109
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.
113 sub Getopts {
114     my ($argumentative, @ARGV) = @_;
115     my (@args, $first, $rest, $pos);
116     my ($errs) = 0;
117     local ($_);
118     local ($[) = 0;
119
120     @args = split( / */, $argumentative);
121     while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
122         ($first, $rest) = ($1,$2);
123         if ($_ =~ m|^--$|) {
124             shift(@ARGV);
125             last;
126         }
127         $pos = index($argumentative,$first);
128         if($pos >= $[) {
129             if($args[$pos+1] eq ':') {
130                 shift(@ARGV);
131                 if($rest eq '') {
132                     unless (@ARGV) {
133                         print STDERR "apxs:Error: Incomplete option: $first (needs an argument)\n";
134                         ++$errs;
135                     }
136                     $rest = shift(@ARGV);
137                 }
138                 eval "\$opt_$first = \$rest;";
139             }
140             elsif ($args[$pos+1] eq '+') {
141                 shift(@ARGV);
142                 if($rest eq '') {
143                     unless (@ARGV) {
144                         print STDERR "apxs:Error: Incomplete option: $first (needs an argument)\n";
145                         ++$errs;
146                     }
147                     $rest = shift(@ARGV);
148                 }
149                 eval "push(\@opt_$first, \$rest);";
150             }
151             else {
152                 eval "\$opt_$first = 1";
153                 if($rest eq '') {
154                     shift(@ARGV);
155                 }
156                 else {
157                     $ARGV[0] = "-$rest";
158                 }
159             }
160         }
161         else {
162             print STDERR "apxs:Error: Unknown option: $first\n";
163             ++$errs;
164             if($rest ne '') {
165                 $ARGV[0] = "-$rest";
166             }
167             else {
168                 shift(@ARGV);
169             }
170         }
171     }
172     return ($errs == 0, @ARGV);
173 }
174
175 sub usage {
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";
183     exit(1);
184 }
185
186 #   option handling
187 my $rc;
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);
192
193 #   argument handling
194 my @args = @ARGV;
195 my $name = 'unknown';
196 $name = $opt_n if ($opt_n ne '');
197
198 if (@opt_S) {
199     my ($opt_S);
200     foreach $opt_S (@opt_S) {
201         if ($opt_S =~ m/^([^=]+)=(.*)$/) {
202             my ($var) = $1;
203             my ($val) = $2;
204             my $oldval = eval "\$CFG_$var";
205
206             unless ($var and $oldval) {
207                 print STDERR "apxs:Error: no config variable $var\n";
208                 &usage;
209             }
210
211             eval "\$CFG_${var}=\"${val}\"";
212         } else {
213             print STDERR "apxs:Error: malformatted -S option\n";
214             &usage;
215         }       
216     }
217 }
218
219 ##
220 ##  Initial shared object support check
221 ##
222 my $httpd = "$CFG_SBINDIR/$CFG_TARGET";
223
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;
228 }
229
230 if (not -x $httpd) {
231         print STDERR "apxs:Error: $httpd not found or not executable\n";
232         exit(1);
233 }
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";
239     exit(1);
240 }
241
242 ##
243 ##  Operation
244 ##
245
246 #   helper function for executing a list of
247 #   system command with return code checks
248 sub execute_cmds {
249     my (@cmds) = @_;
250     my ($cmd, $rc);
251
252     foreach $cmd (@cmds) {
253         print STDERR "$cmd\n";
254         $rc = system("$cmd");
255         if ($rc != 0) {
256             printf(STDERR "apxs:Break: Command failed with rc=%d\n", $rc << 8);
257             exit(1);
258         }
259     }
260 }
261
262 if ($opt_g) {
263     ##
264     ##  SAMPLE MODULE SOURCE GENERATION
265     ##
266
267     if (-d $name) {
268         print STDERR "apxs:Error: Directory `$name' already exists. Remove first\n";
269         exit(1);
270     }
271
272     my $data = join('', <DATA>);
273     $data =~ s|%NAME%|$name|sg;
274     $data =~ s|%TARGET%|$CFG_TARGET|sg;
275
276     my ($mkf, $src) = ($data =~ m|^(.+)-=#=-\n(.+)|s);
277
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;
282     print FP $mkf;
283     close(FP);
284     print STDERR "Creating [FILE] $name/mod_$name.c\n";
285     open(FP, ">${name}/mod_${name}.c") || die;
286     print FP $src;
287     close(FP);
288
289     exit(0);
290 }
291
292
293 if ($opt_q) {
294     ##
295     ##  QUERY INFORMATION 
296     ##
297
298     my $result = '';
299     my $arg;
300     foreach $arg (@args) {
301         my $ok = 0;
302         my $name;
303         foreach $name (qw(
304             TARGET CC CFLAGS CFLAGS_SHLIB LD_SHLIB LDFLAGS_SHLIB LIBS_SHLIB
305             PREFIX SBINDIR INCLUDEDIR LIBEXECDIR SYSCONFDIR
306         )) {
307             if ($arg eq $name or $arg eq lc($name)) {
308                 my $val = eval "\$CFG_$name";
309                 $result .= "${val}::";
310                 $ok = 1;
311             }
312         }
313         if (not $ok) {
314             printf(STDERR "apxs:Error: Invalid query string `%s'\n", $arg);
315             exit(1);
316         }
317     }
318     $result =~ s|::$||;
319     $result =~ s|::| |;
320     print $result;
321 }
322
323 if ($opt_c) {
324     ##
325     ##  SHARED OBJECT COMPILATION
326     ##
327
328     #   split files into sources and objects
329     my @srcs = ();
330     my @objs = ();
331     my $f;
332     foreach $f (@args) {
333         if ($f =~ m|\.c$|) {
334             push(@srcs, $f);
335         }
336         else {
337             push(@objs, $f);
338         }
339     }
340
341     #   determine output file
342     my $dso_file;
343     if ($opt_o eq '') {
344         if ($#srcs > -1) {
345             $dso_file = $srcs[0];
346             $dso_file =~ s|\.[^.]+$|.la|;
347         }
348         elsif ($#objs > -1) {
349             $dso_file = $objs[0];
350             $dso_file =~ s|\.[^.]+$|.la|;
351         }
352         else {
353             $dso_file = "mod_unknown.so";
354         }
355     }
356     else {
357         $dso_file = $opt_o;
358     }
359
360     #   create compilation commands
361     my @cmds = ();
362     my $opt = '';
363     my ($opt_Wc, $opt_I, $opt_D);
364     foreach $opt_Wc (@opt_W) {
365         $opt .= "$1 " if ($opt_Wc =~ m|^\s*c,(.*)$|);
366     }
367     foreach $opt_I (@opt_I) {
368         $opt .= "-I$opt_I ";
369     }
370     foreach $opt_D (@opt_D) {
371         $opt .= "-D$opt_D ";
372     }
373     my $cflags = "$CFG_CFLAGS";
374     my $s;
375     foreach $s (@srcs) {
376         my $slo = $s;
377         $slo =~ s|\.c$|.slo|;
378         my $lo = $s;
379         $lo =~ s|\.c$|.lo|;
380         my $la = $s;
381         $la =~ s|\.c$|.la|;
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");
384
385     }
386
387     #   execute the commands
388     &execute_cmds(@cmds);
389
390     #   allow one-step compilation and installation
391     if ($opt_i or $opt_e) {
392         @args = ( $dso_file );
393     }
394 }
395
396 if ($opt_i or $opt_e) {
397     ##
398     ##  SHARED OBJECT INSTALLATION
399     ##
400
401     #   determine installation commands
402     #   and corresponding LoadModule/AddModule directives
403     my @lmd = ();
404     my @amd = ();
405     my @cmds = ();
406     my $f;
407     foreach $f (@args) {
408         if ($f !~ m#(\.so$|\.la$)#) {
409             print STDERR "apxs:Error: file $f is not a shared object\n";
410             exit(1);
411         }
412         my $t = $f;
413         $t =~ s|^.+/([^/]+)$|$1|;
414         if ($opt_i) {
415             push(@cmds, "libtool --mode=install cp $f $CFG_LIBEXECDIR/$t");
416             push(@cmds, "chmod 755 $CFG_LIBEXECDIR/$t");
417         }
418
419         #   determine module symbolname and filename
420         my $filename = '';
421         if ($name eq 'unknown') {
422             $name = '';
423             my $base = $f;
424             $base =~ s|\.[^.]+$||;
425             if (-f "$base.c") {
426                 open(FP, "<$base.c");
427                 my $content = join('', <FP>);
428                 close(FP);
429                 if ($content =~ m|.*module\s+(?:AP_MODULE_DECLARE_DATA\s+)?([a-zA-Z0-9_]+)_module\s*=\s*.*|s) {
430                     $name = "$1";
431                     $filename = "$base.c";
432                     $filename =~ s|^[^/]+/||;
433                 }
434             }
435             if ($name eq '') {
436                 if ($base =~ m|.*mod_([a-zA-Z0-9_]+)\..+|) {
437                     $name = "$1";
438                     $filename = $base;
439                     $filename =~ s|^[^/]+/||;
440                 }
441             }
442             if ($name eq '') {
443                 print "apxs:Error: Sorry, cannot determine bootstrap symbol name\n";
444                 print "apxs:Error: Please specify one with option `-n'\n";
445                 exit(1);
446             }
447         }
448         if ($filename eq '') {
449             $filename = "mod_${name}.c";
450         }
451         my $dir = $CFG_LIBEXECDIR;
452         $dir =~ s|^$CFG_PREFIX/?||;
453         $dir =~ s|(.)$|$1/|;
454         $t =~ s|\.la$|.so|;
455         push(@lmd, sprintf("LoadModule %-18s %s", "${name}_module", "$dir$t"));
456         push(@amd, sprintf("AddModule %s", $filename));
457     }
458
459     #   execute the commands
460     &execute_cmds(@cmds);
461
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";
466             exit(1);
467         }
468
469         open(FP, "<$CFG_SYSCONFDIR/$CFG_TARGET.conf") || die;
470         my $content = join('', <FP>);
471         close(FP);
472
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";
476             exit(1);
477         }
478
479         my $lmd;
480         my $c = '';
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;
486             } else {
487                  $content =~ s|^(.*\n)#?\s*$lmd[^\n]*\n|$1$c$lmd\n|sg;
488             }
489             $lmd =~ m|LoadModule\s+(.+?)_module.*|;
490             print STDERR "[$what module `$1' in $CFG_SYSCONFDIR/$CFG_TARGET.conf]\n";
491         }
492         my $amd;
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;
496             } else {
497                  $content =~ s|^(.*\n)#?\s*$amd[^\n]*\n|$1$c$amd\n|sg;
498             }
499         }
500         if (@lmd or @amd) {
501             if (open(FP, ">$CFG_SYSCONFDIR/$CFG_TARGET.conf.new")) {
502                 print FP $content;
503                 close(FP);
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");
507             } else {
508                 print STDERR "unable to open configuration file\n";
509             }
510         }
511     }
512 }
513
514 ##EOF##
515 __DATA__
516 ##
517 ##  Makefile -- Build procedure for sample %NAME% Apache module
518 ##  Autogenerated via ``apxs -n %NAME% -g''.
519 ##
520
521 #   the used tools
522 APXS=apxs
523 APACHECTL=apachectl
524
525 #   additional defines, includes and libraries
526 #DEF=-Dmy_define=my_value
527 #INC=-Imy/include/dir
528 #LIB=-Lmy/lib/dir -lmylib
529
530 #   the default target
531 all: mod_%NAME%.la
532
533 #   compile the shared object file
534 mod_%NAME%.la: mod_%NAME%.c
535         $(APXS) -c $(DEF) $(INC) $(LIB) mod_%NAME%.c
536
537 #   install the shared object file into Apache 
538 install: all
539         $(APXS) -i -a -n '%NAME%' mod_%NAME%.la
540
541 #   cleanup
542 clean:
543         -rm -f mod_%NAME%.o mod_%NAME%.lo mod_%NAME%.slo mod_%NAME%.la 
544
545 #   simple test
546 test: reload
547         lynx -mime_header http://localhost/%NAME%
548
549 #   install and activate shared object by reloading Apache to
550 #   force a reload of the shared object file
551 reload: install restart
552
553 #   the general Apache start/restart/stop
554 #   procedures
555 start:
556         $(APACHECTL) start
557 restart:
558         $(APACHECTL) restart
559 stop:
560         $(APACHECTL) stop
561
562 -=#=-
563 /* 
564 **  mod_%NAME%.c -- Apache sample %NAME% module
565 **  [Autogenerated via ``apxs -n %NAME% -g'']
566 **
567 **  To play with this sample module first compile it into a
568 **  DSO file and install it into Apache's modules directory 
569 **  by running:
570 **
571 **    $ apxs -c -i mod_%NAME%.c
572 **
573 **  Then activate it in Apache's %TARGET%.conf file for instance
574 **  for the URL /%NAME% in as follows:
575 **
576 **    #   %TARGET%.conf
577 **    LoadModule %NAME%_module modules/mod_%NAME%.so
578 **    <Location /%NAME%>
579 **    SetHandler %NAME%
580 **    </Location>
581 **
582 **  Then after restarting Apache via
583 **
584 **    $ apachectl restart
585 **
586 **  you immediately can request the URL /%NAME% and watch for the
587 **  output of this module. This can be achieved for instance via:
588 **
589 **    $ lynx -mime_header http://localhost/%NAME% 
590 **
591 **  The output should be similar to the following one:
592 **
593 **    HTTP/1.1 200 OK
594 **    Date: Tue, 31 Mar 1998 14:42:22 GMT
595 **    Server: Apache/1.3.4 (Unix)
596 **    Connection: close
597 **    Content-Type: text/html
598 **  
599 **    The sample page from mod_%NAME%.c
600 */ 
601
602 #include "httpd.h"
603 #include "http_config.h"
604 #include "http_protocol.h"
605 #include "ap_config.h"
606
607 /* The sample content handler */
608 static int %NAME%_handler(request_rec *r)
609 {
610     if (strcmp(r->handler, "%NAME%")) {
611         return DECLINED;
612     }
613     r->content_type = "text/html";      
614     ap_send_http_header(r);
615     if (!r->header_only)
616         ap_rputs("The sample page from mod_%NAME%.c\n", r);
617     return OK;
618 }
619
620 static void %NAME%_register_hooks(apr_pool_t *p)
621 {
622     ap_hook_handler(%NAME%_handler, NULL, NULL, APR_HOOK_LAST);
623 }
624
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                      */
634 };
635