]> granicus.if.org Git - apache/blob - support/apxs.in
Update export file for AIX with recent symbol 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 -I$CFG_INCLUDEDIR/apr $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|\.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+(?:MODULE_VAR_EXPORT\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         push(@lmd, sprintf("LoadModule %-18s %s", "${name}_module", "$dir$t"));
455         push(@amd, sprintf("AddModule %s", $filename));
456     }
457
458     #   execute the commands
459     &execute_cmds(@cmds);
460
461     #   activate module via LoadModule/AddModule directive
462     if ($opt_a or $opt_A) {
463         if (not -f "$CFG_SYSCONFDIR/$CFG_TARGET.conf") {
464             print "apxs:Error: Config file $CFG_SYSCONFDIR/$CFG_TARGET.conf not found\n";
465             exit(1);
466         }
467
468         open(FP, "<$CFG_SYSCONFDIR/$CFG_TARGET.conf") || die;
469         my $content = join('', <FP>);
470         close(FP);
471
472         if ($content !~ m|\n#?\s*LoadModule\s+|) {
473             print STDERR "apxs:Error: Activation failed for custom $CFG_SYSCONFDIR/$CFG_TARGET.conf file.\n";
474             print STDERR "apxs:Error: At least one `LoadModule' directive already has to exist.\n";
475             exit(1);
476         }
477
478         my $lmd;
479         my $c = '';
480         $c = '#' if ($opt_A);
481         foreach $lmd (@lmd) {
482             my $what = $opt_A ? "preparing" : "activating";
483             if ($content !~ m|\n#?\s*$lmd|) {
484                  $content =~ s|^(.*\n#?\s*LoadModule\s+[^\n]+\n)|$1$c$lmd\n|sg;
485             } else {
486                  $content =~ s|^(.*\n)#?\s*$lmd[^\n]*\n|$1$c$lmd\n|sg;
487             }
488             $lmd =~ m|LoadModule\s+(.+?)_module.*|;
489             print STDERR "[$what module `$1' in $CFG_SYSCONFDIR/$CFG_TARGET.conf]\n";
490         }
491         my $amd;
492         foreach $amd (@amd) {
493             if ($content !~ m|\n#?\s*$amd|) {
494                  $content =~ s|^(.*\n#?\s*AddModule\s+[^\n]+\n)|$1$c$amd\n|sg;
495             } else {
496                  $content =~ s|^(.*\n)#?\s*$amd[^\n]*\n|$1$c$amd\n|sg;
497             }
498         }
499         if (@lmd or @amd) {
500             if (open(FP, ">$CFG_SYSCONFDIR/$CFG_TARGET.conf.new")) {
501                 print FP $content;
502                 close(FP);
503                 system("cp $CFG_SYSCONFDIR/$CFG_TARGET.conf $CFG_SYSCONFDIR/$CFG_TARGET.conf.bak && " .
504                        "cp $CFG_SYSCONFDIR/$CFG_TARGET.conf.new $CFG_SYSCONFDIR/$CFG_TARGET.conf && " .
505                        "rm $CFG_SYSCONFDIR/$CFG_TARGET.conf.new");
506             } else {
507                 print STDERR "unable to open configuration file\n";
508             }
509         }
510     }
511 }
512
513 ##EOF##
514 __DATA__
515 ##
516 ##  Makefile -- Build procedure for sample %NAME% Apache module
517 ##  Autogenerated via ``apxs -n %NAME% -g''.
518 ##
519
520 #   the used tools
521 APXS=apxs
522 APACHECTL=apachectl
523
524 #   additional defines, includes and libraries
525 #DEF=-Dmy_define=my_value
526 #INC=-Imy/include/dir
527 #LIB=-Lmy/lib/dir -lmylib
528
529 #   the default target
530 all: mod_%NAME%.la
531
532 #   compile the shared object file
533 mod_%NAME%.la: mod_%NAME%.c
534         $(APXS) -c $(DEF) $(INC) $(LIB) mod_%NAME%.c
535
536 #   install the shared object file into Apache 
537 install: all
538         $(APXS) -i -a -n '%NAME%' mod_%NAME%.la
539
540 #   cleanup
541 clean:
542         -rm -f mod_%NAME%.o mod_%NAME%.lo mod_%NAME%.slo mod_%NAME%.la 
543
544 #   simple test
545 test: reload
546         lynx -mime_header http://localhost/%NAME%
547
548 #   install and activate shared object by reloading Apache to
549 #   force a reload of the shared object file
550 reload: install restart
551
552 #   the general Apache start/restart/stop
553 #   procedures
554 start:
555         $(APACHECTL) start
556 restart:
557         $(APACHECTL) restart
558 stop:
559         $(APACHECTL) stop
560
561 -=#=-
562 /* 
563 **  mod_%NAME%.c -- Apache sample %NAME% module
564 **  [Autogenerated via ``apxs -n %NAME% -g'']
565 **
566 **  To play with this sample module first compile it into a
567 **  DSO file and install it into Apache's libexec directory 
568 **  by running:
569 **
570 **    $ apxs -c -i mod_%NAME%.c
571 **
572 **  Then activate it in Apache's %TARGET%.conf file for instance
573 **  for the URL /%NAME% in as follows:
574 **
575 **    #   %TARGET%.conf
576 **    LoadModule %NAME%_module libexec/mod_%NAME%.so
577 **    <Location /%NAME%>
578 **    SetHandler %NAME%
579 **    </Location>
580 **
581 **  Then after restarting Apache via
582 **
583 **    $ apachectl restart
584 **
585 **  you immediately can request the URL /%NAME and watch for the
586 **  output of this module. This can be achieved for instance via:
587 **
588 **    $ lynx -mime_header http://localhost/%NAME% 
589 **
590 **  The output should be similar to the following one:
591 **
592 **    HTTP/1.1 200 OK
593 **    Date: Tue, 31 Mar 1998 14:42:22 GMT
594 **    Server: Apache/1.3.4 (Unix)
595 **    Connection: close
596 **    Content-Type: text/html
597 **  
598 **    The sample page from mod_%NAME%.c
599 */ 
600
601 #include "httpd.h"
602 #include "http_config.h"
603 #include "http_protocol.h"
604 #include "ap_config.h"
605
606 /* The sample content handler */
607 static int %NAME%_handler(request_rec *r)
608 {
609     r->content_type = "text/html";      
610     ap_send_http_header(r);
611     if (!r->header_only)
612         ap_rputs("The sample page from mod_%NAME%.c\n", r);
613     return OK;
614 }
615
616 /* Dispatch list of content handlers */
617 static const handler_rec %NAME%_handlers[] = { 
618     { "%NAME%", %NAME%_handler }, 
619     { NULL, NULL }
620 };
621
622 /* Dispatch list for API hooks */
623 module MODULE_VAR_EXPORT %NAME%_module = {
624     STANDARD20_MODULE_STUFF, 
625     NULL,                  /* create per-dir    config structures */
626     NULL,                  /* merge  per-dir    config structures */
627     NULL,                  /* create per-server config structures */
628     NULL,                  /* merge  per-server config structures */
629     NULL,                  /* table of config file commands       */
630     %NAME%_handlers,       /* [#8] MIME-typed-dispatched handlers */
631     NULL                   /* register hooks                      */
632 };
633