apxs.in revision 8350a6ee6ab2fafa65dc0216827d380deeedbb0a
402N/A#!@perlbin@ -w
402N/A# ====================================================================
402N/A# The Apache Software License, Version 1.1
402N/A#
402N/A# Copyright (c) 2000-2001 The Apache Software Foundation. All rights
402N/A# reserved.
402N/A#
402N/A# Redistribution and use in source and binary forms, with or without
402N/A# modification, are permitted provided that the following conditions
402N/A# are met:
402N/A#
402N/A# 1. Redistributions of source code must retain the above copyright
402N/A# notice, this list of conditions and the following disclaimer.
402N/A#
402N/A# 2. Redistributions in binary form must reproduce the above copyright
402N/A# notice, this list of conditions and the following disclaimer in
402N/A# the documentation and/or other materials provided with the
402N/A# distribution.
402N/A#
402N/A# 3. The end-user documentation included with the redistribution,
3817N/A# if any, must include the following acknowledgment:
402N/A# "This product includes software developed by the
814N/A# Apache Software Foundation (http://www.apache.org/)."
814N/A# Alternately, this acknowledgment may appear in the software itself,
814N/A# if and wherever such third-party acknowledgments normally appear.
1780N/A#
814N/A# 4. The names "Apache" and "Apache Software Foundation" must
402N/A# not be used to endorse or promote products derived from this
402N/A# software without prior written permission. For written
402N/A# permission, please contact apache@apache.org.
402N/A#
402N/A# 5. Products derived from this software may not be called "Apache",
402N/A# nor may "Apache" appear in their name, without prior written
618N/A# permission of the Apache Software Foundation.
402N/A#
402N/A# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
402N/A# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
844N/A# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
844N/A# DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
402N/A# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1407N/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
402N/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2899N/A# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2899N/A# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
4250N/A# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
814N/A# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3817N/A# SUCH DAMAGE.
3817N/A# ====================================================================
3817N/A#
402N/A# This software consists of voluntary contributions made by many
3955N/A# individuals on behalf of the Apache Software Foundation. For more
3955N/A# information on the Apache Software Foundation, please see
3955N/A# <http://www.apache.org/>.
3955N/A#
3955N/A
3955N/Arequire 5.003;
3955N/Ause strict;
3955N/Apackage apxs;
3955N/A
3955N/A##
3955N/A## Configuration
3955N/A##
3955N/A
3955N/Amy $prefix = "@prefix@";
402N/Amy $CFG_PREFIX = $prefix;
402N/A
402N/A# read the configuration variables once
402N/Amy %config_vars = ();
402N/Aget_config_vars("$prefix/build/config_vars.mk",\%config_vars);
402N/A
402N/Amy $exec_prefix = get_vars("exec_prefix");
402N/Amy $CFG_TARGET = get_vars("progname");
402N/Amy $CFG_SYSCONFDIR = get_vars("sysconfdir");
402N/Amy $CFG_CFLAGS = join ' ', map { get_vars($_) }
402N/A qw(SHLTCFLAGS CFLAGS NOTEST_CPPFLAGS EXTRA_CPPFLAGS EXTRA_CFLAGS);
402N/Amy $includedir = get_vars("includedir");
402N/Amy $CFG_INCLUDEDIR = eval qq("$includedir");
402N/Amy $CFG_CC = get_vars("CC");
402N/Amy $libexecdir = get_vars("libexecdir");
402N/Amy $CFG_LIBEXECDIR = eval qq("$libexecdir");
402N/Amy $sbindir = get_vars("sbindir");
402N/Amy $CFG_SBINDIR = eval qq("$sbindir");
402N/A
402N/Amy %internal_vars = map {$_ => 1}
402N/A qw(TARGET CC CFLAGS CFLAGS_SHLIB LD_SHLIB LDFLAGS_SHLIB LIBS_SHLIB
402N/A PREFIX SBINDIR INCLUDEDIR LIBEXECDIR SYSCONFDIR);
402N/A
402N/A##
402N/A## parse argument line
402N/A##
402N/A
402N/A# defaults for parameters
402N/Amy $opt_n = '';
402N/Amy $opt_g = '';
402N/Amy $opt_c = 0;
402N/Amy $opt_o = '';
402N/Amy @opt_D = ();
402N/Amy @opt_I = ();
402N/Amy @opt_L = ();
402N/Amy @opt_l = ();
402N/Amy @opt_W = ();
402N/Amy @opt_S = ();
402N/Amy $opt_e = 0;
402N/Amy $opt_i = 0;
402N/Amy $opt_a = 0;
402N/Amy $opt_A = 0;
459N/Amy $opt_q = 0;
402N/Amy $opt_h = 0;
402N/A
402N/A# this subroutine is derived from Perl's getopts.pl with the enhancement of
402N/A# the "+" metacharacter at the format string to allow a list to be built by
402N/A# subsequent occurrences of the same option.
402N/Asub Getopts {
402N/A my ($argumentative, @ARGV) = @_;
402N/A my $errs = 0;
402N/A local $_;
402N/A local $[ = 0;
402N/A
402N/A my @args = split / */, $argumentative;
402N/A while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
402N/A my ($first, $rest) = ($1,$2);
402N/A if ($_ =~ m|^--$|) {
402N/A shift @ARGV;
402N/A last;
402N/A }
402N/A my $pos = index($argumentative,$first);
402N/A if ($pos >= $[) {
402N/A if ($args[$pos+1] eq ':') {
402N/A shift @ARGV;
402N/A if ($rest eq '') {
402N/A unless (@ARGV) {
402N/A error("Incomplete option: $first (needs an argument)");
3955N/A $errs++;
402N/A }
402N/A $rest = shift(@ARGV);
402N/A }
402N/A eval "\$opt_$first = \$rest;";
402N/A }
402N/A elsif ($args[$pos+1] eq '+') {
402N/A shift @ARGV;
402N/A if ($rest eq '') {
402N/A unless (@ARGV) {
402N/A error("Incomplete option: $first (needs an argument)");
402N/A $errs++;
402N/A }
402N/A $rest = shift(@ARGV);
402N/A }
402N/A eval "push(\@opt_$first, \$rest);";
402N/A }
402N/A else {
402N/A eval "\$opt_$first = 1";
402N/A if ($rest eq '') {
402N/A shift(@ARGV);
402N/A }
402N/A else {
402N/A $ARGV[0] = "-$rest";
402N/A }
402N/A }
402N/A }
402N/A else {
402N/A error("Unknown option: $first");
402N/A $errs++;
402N/A if ($rest ne '') {
402N/A $ARGV[0] = "-$rest";
402N/A }
402N/A else {
402N/A shift(@ARGV);
402N/A }
402N/A }
402N/A }
402N/A return ($errs == 0, @ARGV);
402N/A}
402N/A
402N/Asub usage {
402N/A print STDERR "Usage: apxs -g [-S <var>=<val>] -n <modname>\n";
402N/A print STDERR " apxs -q [-S <var>=<val>] <query> ...\n";
402N/A print STDERR " apxs -c [-S <var>=<val>] [-o <dsofile>] [-D <name>[=<value>]]\n";
402N/A print STDERR " [-I <incdir>] [-L <libdir>] [-l <libname>] [-Wc,<flags>]\n";
402N/A print STDERR " [-Wl,<flags>] <files> ...\n";
402N/A print STDERR " apxs -i [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
402N/A print STDERR " apxs -e [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
402N/A exit(1);
402N/A}
402N/A
402N/A# option handling
402N/Amy $rc;
402N/A($rc, @ARGV) = &Getopts("qn:gco:I+D+L+l+W+S+eiaA", @ARGV);
402N/A&usage if ($rc == 0);
402N/A&usage if ($#ARGV == -1 and not $opt_g);
402N/A&usage if (not $opt_q and not ($opt_g and $opt_n) and not $opt_i and not $opt_c and not $opt_e);
402N/A
402N/A# argument handling
402N/Amy @args = @ARGV;
402N/Amy $name = 'unknown';
402N/A$name = $opt_n if ($opt_n ne '');
402N/A
402N/Aif (@opt_S) {
402N/A my ($opt_S);
402N/A foreach $opt_S (@opt_S) {
402N/A if ($opt_S =~ m/^([^=]+)=(.*)$/) {
402N/A my ($var) = $1;
402N/A my ($val) = $2;
402N/A my $oldval = eval "\$CFG_$var";
402N/A
402N/A unless ($var and $oldval) {
402N/A print STDERR "apxs:Error: no config variable $var\n";
402N/A &usage;
402N/A }
402N/A
402N/A eval "\$CFG_${var}=\"${val}\"";
402N/A } else {
402N/A print STDERR "apxs:Error: malformatted -S option\n";
402N/A &usage;
402N/A }
402N/A }
402N/A}
402N/A
402N/A##
402N/A## Initial shared object support check
402N/A##
402N/Amy $httpd = get_vars("sbindir") . "/" . get_vars("progname");
402N/A$httpd = eval qq("$httpd");
402N/A$httpd = eval qq("$httpd");
402N/A
402N/A#allow apxs to be run from the source tree, before installation
402N/Aif ($0 =~ m:support/apxs$:) {
402N/A ($httpd = $0) =~ s:support/apxs$::;
402N/A}
402N/A
402N/Aunless (-x "$httpd") {
402N/A error("$httpd not found or not executable");
402N/A exit 1;
402N/A}
402N/A
402N/Aunless (grep /mod_so/, `$httpd -l`) {
402N/A error("Sorry, no shared object support for Apache");
402N/A error("available under your platform. Make sure");
402N/A error("the Apache module mod_so is compiled into");
402N/A error("your server binary `$httpd'.");
402N/A exit 1;
402N/A}
402N/A
402N/Asub get_config_vars{
402N/A my ($file, $rh_config) = @_;
402N/A
3353N/A open IN, $file or die "cannot open $file: $!";
3353N/A while (<IN>){
3353N/A if (/^\s*(.*?)\s*=\s*(.*)$/){
3353N/A $rh_config->{$1} = $2;
3353N/A }
3353N/A }
3353N/A close IN;
3353N/A}
3353N/A
3353N/Asub get_vars {
3353N/A my $result = '';
3955N/A my $ok = 0;
3955N/A my $arg;
3353N/A foreach $arg (@_) {
3353N/A if (exists $config_vars{$arg} or exists $config_vars{lc $arg}) {
1938N/A my $val = exists $config_vars{$arg}
1938N/A ? $config_vars{$arg}
3477N/A : $config_vars{lc $arg};
3477N/A $val =~ s/[()]//g;
3477N/A $result .= eval "qq($val)";
402N/A $result .= ";;";
402N/A $ok = 1;
402N/A }
402N/A if (not $ok) {
402N/A if (exists $internal_vars{$arg} or exists $internal_vars{lc $arg}) {
402N/A my $val = exists $internal_vars{$arg} ? $arg : lc $arg;
402N/A $val = eval "\$CFG_$val";
402N/A $result .= eval "qq($val)";
402N/A $result .= ";;";
402N/A $ok = 1;
402N/A }
402N/A if (not $ok) {
402N/A error("Invalid query string `$arg'");
402N/A exit(1);
402N/A }
402N/A }
402N/A }
402N/A $result =~ s|;;$||;
402N/A $result =~ s|:| |;
402N/A return $result;
3353N/A}
402N/A
4337N/A##
4337N/A## Operation
402N/A##
402N/A
402N/A# helper function for executing a list of
402N/A# system command with return code checks
402N/Asub execute_cmds {
3817N/A my (@cmds) = @_;
3817N/A my ($cmd, $rc);
3817N/A
foreach $cmd (@cmds) {
notice($cmd);
$rc = system $cmd;
if ($rc) {
error(sprintf "Command failed with rc=%d\n", $rc << 8);
exit 1 ;
}
}
}
if ($opt_g) {
##
## SAMPLE MODULE SOURCE GENERATION
##
if (-d $name) {
error("Directory `$name' already exists. Remove first");
exit(1);
}
my $data = join('', <DATA>);
$data =~ s|%NAME%|$name|sg;
$data =~ s|%TARGET%|$CFG_TARGET|sg;
my ($mkf, $mods, $src) = ($data =~ m|^(.+)-=#=-\n(.+)-=#=-\n(.+)|s);
notice("Creating [DIR] $name");
system("mkdir $name");
notice("Creating [FILE] $name/Makefile");
open(FP, ">${name}/Makefile") || die;
print FP $mkf;
close(FP);
notice("Creating [FILE] $name/modules.mk");
open(FP, ">${name}/modules.mk") || die;
print FP $mods;
close(FP);
notice("Creating [FILE] $name/mod_$name.c");
open(FP, ">${name}/mod_${name}.c") || die;
print FP $src;
close(FP);
notice("Creating [FILE] $name/.deps");
system("touch ${name}/.deps");
exit(0);
}
if ($opt_q) {
##
## QUERY INFORMATION
##
my $result = get_vars(@args);
print "$result";
}
if ($opt_c) {
##
## SHARED OBJECT COMPILATION
##
# split files into sources and objects
my @srcs = ();
my @objs = ();
my $f;
foreach $f (@args) {
if ($f =~ m|\.c$|) {
push(@srcs, $f);
}
else {
push(@objs, $f);
}
}
# determine output file
my $dso_file;
if ($opt_o eq '') {
if ($#srcs > -1) {
$dso_file = $srcs[0];
$dso_file =~ s|\.[^.]+$|.la|;
}
elsif ($#objs > -1) {
$dso_file = $objs[0];
$dso_file =~ s|\.[^.]+$|.la|;
}
else {
$dso_file = "mod_unknown.so";
}
}
else {
$dso_file = $opt_o;
}
# create compilation commands
my @cmds = ();
my $opt = '';
my ($opt_Wc, $opt_I, $opt_D);
foreach $opt_Wc (@opt_W) {
$opt .= "$1 " if ($opt_Wc =~ m|^\s*c,(.*)$|);
}
foreach $opt_I (@opt_I) {
$opt .= "-I$opt_I ";
}
foreach $opt_D (@opt_D) {
$opt .= "-D$opt_D ";
}
my $cflags = "$CFG_CFLAGS";
my $s;
my $mod;
foreach $s (@srcs) {
my $slo = $s;
$slo =~ s|\.c$|.slo|;
my $lo = $s;
$lo =~ s|\.c$|.lo|;
my $la = $s;
$la =~ s|\.c$|.la|;
my $o = $s;
$o =~ s|\.c$|.o|;
push(@cmds, "$prefix/build/libtool --silent --mode=compile $CFG_CC $cflags -I$CFG_INCLUDEDIR $opt -c -o $lo $s && touch $slo");
unshift(@objs, $lo);
}
# create link command
my $o;
my $lo;
foreach $o (@objs) {
$lo .= " $o";
}
my ($opt_Wl, $opt_L, $opt_l);
foreach $opt_Wl (@opt_W) {
if ($CFG_CC !~ m/gcc$/) {
$opt .= " $1" if ($opt_Wl =~ m|^\s*l,(.*)$|);
} else {
$opt .= " -W$opt_Wl";
}
}
foreach $opt_L (@opt_L) {
$opt .= " -L$opt_L";
}
foreach $opt_l (@opt_l) {
$opt .= " -l$opt_l";
}
push(@cmds, "$prefix/build/libtool --silent --mode=link $CFG_CC -o $dso_file -rpath $CFG_LIBEXECDIR -module -avoid-version $opt $lo");
# execute the commands
&execute_cmds(@cmds);
# allow one-step compilation and installation
if ($opt_i or $opt_e) {
@args = ( $dso_file );
}
}
if ($opt_i or $opt_e) {
##
## SHARED OBJECT INSTALLATION
##
# determine installation commands
# and corresponding LoadModule/AddModule directives
my @lmd = ();
my @amd = ();
my @cmds = ();
my $f;
foreach $f (@args) {
if ($f !~ m#(\.so$|\.la$)#) {
error("file $f is not a shared object");
exit(1);
}
my $t = $f;
$t =~ s|^.+/([^/]+)$|$1|;
$t =~ s|\.la$|\.so|;
if ($opt_i) {
push(@cmds, "$prefix/build/instdso.sh SH_LIBTOOL='" .
"$prefix/build/libtool' $f $CFG_LIBEXECDIR");
push(@cmds, "chmod 755 $CFG_LIBEXECDIR/$t");
}
# determine module symbolname and filename
my $filename = '';
if ($name eq 'unknown') {
$name = '';
my $base = $f;
$base =~ s|\.[^.]+$||;
if (-f "$base.c") {
open(FP, "<$base.c");
my $content = join('', <FP>);
close(FP);
if ($content =~ m|.*module\s+(?:AP_MODULE_DECLARE_DATA\s+)?([a-zA-Z0-9_]+)_module\s*=\s*.*|s) {
$name = "$1";
$filename = "$base.c";
$filename =~ s|^[^/]+/||;
}
}
if ($name eq '') {
if ($base =~ m|.*mod_([a-zA-Z0-9_]+)\..+|) {
$name = "$1";
$filename = $base;
$filename =~ s|^[^/]+/||;
}
}
if ($name eq '') {
error("Sorry, cannot determine bootstrap symbol name");
error("Please specify one with option `-n'");
exit(1);
}
}
if ($filename eq '') {
$filename = "mod_${name}.c";
}
my $dir = $CFG_LIBEXECDIR;
$dir =~ s|^$CFG_PREFIX/?||;
$dir =~ s|(.)$|$1/|;
$t =~ s|\.la$|.so|;
push(@lmd, sprintf("LoadModule %-18s %s", "${name}_module", "$dir$t"));
push(@amd, sprintf("AddModule %s", $filename));
}
# execute the commands
&execute_cmds(@cmds);
# activate module via LoadModule/AddModule directive
if ($opt_a or $opt_A) {
if (not -f "$CFG_SYSCONFDIR/$CFG_TARGET.conf") {
error("Config file $CFG_SYSCONFDIR/$CFG_TARGET.conf not found");
exit(1);
}
open(FP, "<$CFG_SYSCONFDIR/$CFG_TARGET.conf") || die;
my $content = join('', <FP>);
close(FP);
if ($content !~ m|\n#?\s*LoadModule\s+|) {
error("Activation failed for custom $CFG_SYSCONFDIR/$CFG_TARGET.conf file.");
error("At least one `LoadModule' directive already has to exist.");
exit(1);
}
my $lmd;
my $c = '';
$c = '#' if ($opt_A);
foreach $lmd (@lmd) {
my $what = $opt_A ? "preparing" : "activating";
if ($content !~ m|\n#?\s*$lmd|) {
$content =~ s|^(.*\n#?\s*LoadModule\s+[^\n]+\n)|$1$c$lmd\n|sg;
} else {
$content =~ s|^(.*\n)#?\s*$lmd[^\n]*\n|$1$c$lmd\n|sg;
}
$lmd =~ m|LoadModule\s+(.+?)_module.*|;
notice("[$what module `$1' in $CFG_SYSCONFDIR/$CFG_TARGET.conf]");
}
my $amd;
foreach $amd (@amd) {
if ($content !~ m|\n#?\s*$amd|) {
$content =~ s|^(.*\n#?\s*AddModule\s+[^\n]+\n)|$1$c$amd\n|sg;
} else {
$content =~ s|^(.*\n)#?\s*$amd[^\n]*\n|$1$c$amd\n|sg;
}
}
if (@lmd or @amd) {
if (open(FP, ">$CFG_SYSCONFDIR/$CFG_TARGET.conf.new")) {
print FP $content;
close(FP);
system("cp $CFG_SYSCONFDIR/$CFG_TARGET.conf $CFG_SYSCONFDIR/$CFG_TARGET.conf.bak && " .
"cp $CFG_SYSCONFDIR/$CFG_TARGET.conf.new $CFG_SYSCONFDIR/$CFG_TARGET.conf && " .
"rm $CFG_SYSCONFDIR/$CFG_TARGET.conf.new");
} else {
notice("unable to open configuration file");
}
}
}
}
sub error{
print STDERR "apxs:Error: $_[0].\n";
}
sub notice{
print STDERR "$_[0]\n";
}
##EOF##
__DATA__
##
## Makefile -- Build procedure for sample %NAME% Apache module
## Autogenerated via ``apxs -n %NAME% -g''.
##
builddir=$(shell pwd)
top_srcdir=/home/rbb/apachebin4
top_builddir=/home/rbb/apachebin4/build
include /home/rbb/apachebin4/build/special.mk
# the used tools
APXS=apxs
APACHECTL=apachectl
# additional defines, includes and libraries
#DEF=-Dmy_define=my_value
#INC=-Imy/include/dir
#LIB=-Lmy/lib/dir -lmylib
# the default target
all: local-shared-build
# install the shared object file into Apache
install: install-modules
# cleanup
clean:
-rm -f mod_%NAME%.o mod_%NAME%.lo mod_%NAME%.slo mod_%NAME%.la
# simple test
test: reload
lynx -mime_header http://localhost/%NAME%
# install and activate shared object by reloading Apache to
# force a reload of the shared object file
reload: install restart
# the general Apache start/restart/stop
# procedures
start:
$(APACHECTL) start
restart:
$(APACHECTL) restart
stop:
$(APACHECTL) stop
-=#=-
mod_%NAME%.la: mod_%NAME%.slo
$(SH_LINK) -rpath $(libexecdir) -module -avoid-version mod_%NAME%.lo
DISTCLEAN_TARGETS = modules.mk
shared = mod_%NAME%.la
-=#=-
/*
** mod_%NAME%.c -- Apache sample %NAME% module
** [Autogenerated via ``apxs -n %NAME% -g'']
**
** To play with this sample module first compile it into a
** DSO file and install it into Apache's modules directory
** by running:
**
** $ apxs -c -i mod_%NAME%.c
**
** Then activate it in Apache's %TARGET%.conf file for instance
** for the URL /%NAME% in as follows:
**
** # %TARGET%.conf
** LoadModule %NAME%_module modules/mod_%NAME%.so
** <Location /%NAME%>
** SetHandler %NAME%
** </Location>
**
** Then after restarting Apache via
**
** $ apachectl restart
**
** you immediately can request the URL /%NAME% and watch for the
** output of this module. This can be achieved for instance via:
**
** $ lynx -mime_header http://localhost/%NAME%
**
** The output should be similar to the following one:
**
** HTTP/1.1 200 OK
** Date: Tue, 31 Mar 1998 14:42:22 GMT
** Server: Apache/1.3.4 (Unix)
** Connection: close
** Content-Type: text/html
**
** The sample page from mod_%NAME%.c
*/
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
/* The sample content handler */
static int %NAME%_handler(request_rec *r)
{
if (strcmp(r->handler, "%NAME%")) {
return DECLINED;
}
r->content_type = "text/html";
if (!r->header_only)
ap_rputs("The sample page from mod_%NAME%.c\n", r);
return OK;
}
static void %NAME%_register_hooks(apr_pool_t *p)
{
ap_hook_handler(%NAME%_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA %NAME%_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
NULL, /* table of config file commands */
%NAME%_register_hooks /* register hooks */
};