]> granicus.if.org Git - apache/blob - support/apxs.in
Make apxs mostly work for 2.0. It doesn't add the module to the config
[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        = "/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@";
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 if (not -x "$CFG_SBINDIR/$CFG_TARGET") {
223         print STDERR "apxs:Error: $CFG_SBINDIR/$CFG_TARGET not found or not executable\n";
224         exit(1);
225 }
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";
231     exit(1);
232 }
233
234 ##
235 ##  Operation
236 ##
237
238 #   helper function for executing a list of
239 #   system command with return code checks
240 sub execute_cmds {
241     my (@cmds) = @_;
242     my ($cmd, $rc);
243
244     foreach $cmd (@cmds) {
245         print STDERR "$cmd\n";
246         $rc = system("$cmd");
247         if ($rc != 0) {
248             printf(STDERR "apxs:Break: Command failed with rc=%d\n", $rc << 8);
249             exit(1);
250         }
251     }
252 }
253
254 if ($opt_g) {
255     ##
256     ##  SAMPLE MODULE SOURCE GENERATION
257     ##
258
259     if (-d $name) {
260         print STDERR "apxs:Error: Directory `$name' already exists. Remove first\n";
261         exit(1);
262     }
263
264     my $data = join('', <DATA>);
265     $data =~ s|%NAME%|$name|sg;
266     $data =~ s|%TARGET%|$CFG_TARGET|sg;
267
268     my ($mkf, $src) = ($data =~ m|^(.+)-=#=-\n(.+)|s);
269
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;
274     print FP $mkf;
275     close(FP);
276     print STDERR "Creating [FILE] $name/mod_$name.c\n";
277     open(FP, ">${name}/mod_${name}.c") || die;
278     print FP $src;
279     close(FP);
280
281     exit(0);
282 }
283
284
285 if ($opt_q) {
286     ##
287     ##  QUERY INFORMATION 
288     ##
289
290     my $result = '';
291     my $arg;
292     foreach $arg (@args) {
293         my $ok = 0;
294         my $name;
295         foreach $name (qw(
296             TARGET CC CFLAGS CFLAGS_SHLIB LD_SHLIB LDFLAGS_SHLIB LIBS_SHLIB
297             PREFIX SBINDIR INCLUDEDIR LIBEXECDIR SYSCONFDIR
298         )) {
299             if ($arg eq $name or $arg eq lc($name)) {
300                 my $val = eval "\$CFG_$name";
301                 $result .= "${val}::";
302                 $ok = 1;
303             }
304         }
305         if (not $ok) {
306             printf(STDERR "apxs:Error: Invalid query string `%s'\n", $arg);
307             exit(1);
308         }
309     }
310     $result =~ s|::$||;
311     $result =~ s|::| |;
312     print $result;
313 }
314
315 if ($opt_c) {
316     ##
317     ##  SHARED OBJECT COMPILATION
318     ##
319
320     #   split files into sources and objects
321     my @srcs = ();
322     my @objs = ();
323     my $f;
324     foreach $f (@args) {
325         if ($f =~ m|\.c$|) {
326             push(@srcs, $f);
327         }
328         else {
329             push(@objs, $f);
330         }
331     }
332
333     #   determine output file
334     my $dso_file;
335     if ($opt_o eq '') {
336         if ($#srcs > -1) {
337             $dso_file = $srcs[0];
338             $dso_file =~ s|\.[^.]+$|.la|;
339         }
340         elsif ($#objs > -1) {
341             $dso_file = $objs[0];
342             $dso_file =~ s|\.[^.]+$|.la|;
343         }
344         else {
345             $dso_file = "mod_unknown.so";
346         }
347     }
348     else {
349         $dso_file = $opt_o;
350     }
351
352     #   create compilation commands
353     my @cmds = ();
354     my $opt = '';
355     my ($opt_Wc, $opt_I, $opt_D);
356     foreach $opt_Wc (@opt_W) {
357         $opt .= "$1 " if ($opt_Wc =~ m|^\s*c,(.*)$|);
358     }
359     foreach $opt_I (@opt_I) {
360         $opt .= "-I$opt_I ";
361     }
362     foreach $opt_D (@opt_D) {
363         $opt .= "-D$opt_D ";
364     }
365     my $cflags = "$CFG_CFLAGS";
366     my $s;
367     foreach $s (@srcs) {
368         my $slo = $s;
369         $slo =~ s|\.c$|.slo|;
370         my $lo = $s;
371         $lo =~ s|\.c$|.lo|;
372         my $la = $s;
373         $la =~ s|\.c$|.la|;
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");
376
377     }
378
379     #   execute the commands
380     &execute_cmds(@cmds);
381
382     #   allow one-step compilation and installation
383     if ($opt_i or $opt_e) {
384         @args = ( $dso_file );
385     }
386 }
387
388 if ($opt_i or $opt_e) {
389     ##
390     ##  SHARED OBJECT INSTALLATION
391     ##
392
393     #   determine installation commands
394     #   and corresponding LoadModule/AddModule directives
395     my @lmd = ();
396     my @amd = ();
397     my @cmds = ();
398     my $f;
399     foreach $f (@args) {
400         if ($f !~ m|\.la$|) {
401             print STDERR "apxs:Error: file $f is not a shared object\n";
402             exit(1);
403         }
404         my $t = $f;
405         $t =~ s|^.+/([^/]+)$|$1|;
406         if ($opt_i) {
407             push(@cmds, "libtool --mode=install cp $f $CFG_LIBEXECDIR/$t");
408             push(@cmds, "chmod 755 $CFG_LIBEXECDIR/$t");
409         }
410
411         #   determine module symbolname and filename
412         my $filename = '';
413         if ($name eq 'unknown') {
414             $name = '';
415             my $base = $f;
416             $base =~ s|\.[^.]+$||;
417             if (-f "$base.c") {
418                 open(FP, "<$base.c");
419                 my $content = join('', <FP>);
420                 close(FP);
421                 if ($content =~ m|.*module\s+(?:MODULE_VAR_EXPORT\s+)?([a-zA-Z0-9_]+)_module\s*=\s*.*|s) {
422                     $name = "$1";
423                     $filename = "$base.c";
424                     $filename =~ s|^[^/]+/||;
425                 }
426             }
427             if ($name eq '') {
428                 if ($base =~ m|.*mod_([a-zA-Z0-9_]+)\..+|) {
429                     $name = "$1";
430                     $filename = $base;
431                     $filename =~ s|^[^/]+/||;
432                 }
433             }
434             if ($name eq '') {
435                 print "apxs:Error: Sorry, cannot determine bootstrap symbol name\n";
436                 print "apxs:Error: Please specify one with option `-n'\n";
437                 exit(1);
438             }
439         }
440         if ($filename eq '') {
441             $filename = "mod_${name}.c";
442         }
443         my $dir = $CFG_LIBEXECDIR;
444         $dir =~ s|^$CFG_PREFIX/?||;
445         $dir =~ s|(.)$|$1/|;
446         push(@lmd, sprintf("LoadModule %-18s %s", "${name}_module", "$dir$t"));
447         push(@amd, sprintf("AddModule %s", $filename));
448     }
449
450     #   execute the commands
451     &execute_cmds(@cmds);
452
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";
457             exit(1);
458         }
459
460         open(FP, "<$CFG_SYSCONFDIR/$CFG_TARGET.conf") || die;
461         my $content = join('', <FP>);
462         close(FP);
463
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";
467             exit(1);
468         }
469
470         my $lmd;
471         my $c = '';
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;
477             } else {
478                  $content =~ s|^(.*\n)#?\s*$lmd[^\n]*\n|$1$c$lmd\n|sg;
479             }
480             $lmd =~ m|LoadModule\s+(.+?)_module.*|;
481             print STDERR "[$what module `$1' in $CFG_SYSCONFDIR/$CFG_TARGET.conf]\n";
482         }
483         my $amd;
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;
487             } else {
488                  $content =~ s|^(.*\n)#?\s*$amd[^\n]*\n|$1$c$amd\n|sg;
489             }
490         }
491         if (@lmd or @amd) {
492             if (open(FP, ">$CFG_SYSCONFDIR/$CFG_TARGET.conf.new")) {
493                 print FP $content;
494                 close(FP);
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");
498             } else {
499                 print STDERR "unable to open configuration file\n";
500             }
501         }
502     }
503 }
504
505 ##EOF##
506 __DATA__
507 ##
508 ##  Makefile -- Build procedure for sample %NAME% Apache module
509 ##  Autogenerated via ``apxs -n %NAME% -g''.
510 ##
511
512 #   the used tools
513 APXS=apxs
514 APACHECTL=apachectl
515
516 #   additional defines, includes and libraries
517 #DEF=-Dmy_define=my_value
518 #INC=-Imy/include/dir
519 #LIB=-Lmy/lib/dir -lmylib
520
521 #   the default target
522 all: mod_%NAME%.la
523
524 #   compile the shared object file
525 mod_%NAME%.la: mod_%NAME%.c
526         $(APXS) -c $(DEF) $(INC) $(LIB) mod_%NAME%.c
527
528 #   install the shared object file into Apache 
529 install: all
530         $(APXS) -i -a -n '%NAME%' mod_%NAME%.la
531
532 #   cleanup
533 clean:
534         -rm -f mod_%NAME%.o mod_%NAME%.lo mod_%NAME%.slo mod_%NAME%.la 
535
536 #   simple test
537 test: reload
538         lynx -mime_header http://localhost/%NAME%
539
540 #   install and activate shared object by reloading Apache to
541 #   force a reload of the shared object file
542 reload: install restart
543
544 #   the general Apache start/restart/stop
545 #   procedures
546 start:
547         $(APACHECTL) start
548 restart:
549         $(APACHECTL) restart
550 stop:
551         $(APACHECTL) stop
552
553 -=#=-
554 /* 
555 **  mod_%NAME%.c -- Apache sample %NAME% module
556 **  [Autogenerated via ``apxs -n %NAME% -g'']
557 **
558 **  To play with this sample module first compile it into a
559 **  DSO file and install it into Apache's libexec directory 
560 **  by running:
561 **
562 **    $ apxs -c -i mod_%NAME%.c
563 **
564 **  Then activate it in Apache's %TARGET%.conf file for instance
565 **  for the URL /%NAME% in as follows:
566 **
567 **    #   %TARGET%.conf
568 **    LoadModule %NAME%_module libexec/mod_%NAME%.so
569 **    <Location /%NAME%>
570 **    SetHandler %NAME%
571 **    </Location>
572 **
573 **  Then after restarting Apache via
574 **
575 **    $ apachectl restart
576 **
577 **  you immediately can request the URL /%NAME and watch for the
578 **  output of this module. This can be achieved for instance via:
579 **
580 **    $ lynx -mime_header http://localhost/%NAME% 
581 **
582 **  The output should be similar to the following one:
583 **
584 **    HTTP/1.1 200 OK
585 **    Date: Tue, 31 Mar 1998 14:42:22 GMT
586 **    Server: Apache/1.3.4 (Unix)
587 **    Connection: close
588 **    Content-Type: text/html
589 **  
590 **    The sample page from mod_%NAME%.c
591 */ 
592
593 #include "httpd.h"
594 #include "http_config.h"
595 #include "http_protocol.h"
596 #include "ap_config.h"
597
598 /* The sample content handler */
599 static int %NAME%_handler(request_rec *r)
600 {
601     r->content_type = "text/html";      
602     ap_send_http_header(r);
603     if (!r->header_only)
604         ap_rputs("The sample page from mod_%NAME%.c\n", r);
605     return OK;
606 }
607
608 /* Dispatch list of content handlers */
609 static const handler_rec %NAME%_handlers[] = { 
610     { "%NAME%", %NAME%_handler }, 
611     { NULL, NULL }
612 };
613
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                      */
624 };
625