]> granicus.if.org Git - apache/blobdiff - support/apxs.in
Kill the hardcoded values
[apache] / support / apxs.in
index eb3e2d184a779d1a423ea907f0ebeff9d0aa8fbc..d2c213e342e514da631f68ab824e1cb46cbe9daa 100644 (file)
@@ -1,63 +1,21 @@
-#!/usr/local/bin/perl
-# ====================================================================
-# The Apache Software License, Version 1.1
+#!@perlbin@ -w
 #
-# Copyright (c) 2000 The Apache Software Foundation.  All rights
-# reserved.
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
 #
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
+#     http://www.apache.org/licenses/LICENSE-2.0
 #
-# 1. Redistributions of source code must retain the above copyright
-#    notice, this list of conditions and the following disclaimer.
-#
-# 2. Redistributions in binary form must reproduce the above copyright
-#    notice, this list of conditions and the following disclaimer in
-#    the documentation and/or other materials provided with the
-#    distribution.
-#
-# 3. The end-user documentation included with the redistribution,
-#    if any, must include the following acknowledgment:
-#       "This product includes software developed by the
-#        Apache Software Foundation (http://www.apache.org/)."
-#    Alternately, this acknowledgment may appear in the software itself,
-#    if and wherever such third-party acknowledgments normally appear.
-#
-# 4. The names "Apache" and "Apache Software Foundation" must
-#    not be used to endorse or promote products derived from this
-#    software without prior written permission. For written
-#    permission, please contact apache@apache.org.
-#
-# 5. Products derived from this software may not be called "Apache",
-#    nor may "Apache" appear in their name, without prior written
-#    permission of the Apache Software Foundation.
-#
-# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
-# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-# DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
-# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
-# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
-# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
-# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-# SUCH DAMAGE.
-# ====================================================================
-#
-# This software consists of voluntary contributions made by many
-# individuals on behalf of the Apache Software Foundation.  For more
-# information on the Apache Software Foundation, please see
-# <http://www.apache.org/>.
-#
-##
-##  apxs -- APache eXtenSion tool
-##  Written by Ralf S. Engelschall <rse@apache.org>
-##
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
 
-require 5.003;
+require 5.004;
 use strict;
 package apxs;
 
@@ -65,25 +23,35 @@ package apxs;
 ##  Configuration
 ##
 
-my $CFG_TARGET        = '@TARGET@';            # substituted via Makefile.tmpl 
-my $CFG_CC            = '@CC@';                # substituted via Makefile.tmpl
-my $CFG_CFLAGS        = '@CFLAGS@';            # substituted via Makefile.tmpl
-my $CFG_CFLAGS_SHLIB  = '@CFLAGS_SHLIB@';      # substituted via Makefile.tmpl
-my $CFG_LD_SHLIB      = '@LD_SHLIB@';          # substituted via Makefile.tmpl
-my $CFG_LDFLAGS_SHLIB = '@LDFLAGS_MOD_SHLIB@'; # substituted via Makefile.tmpl 
-my $CFG_LIBS_SHLIB    = '@LIBS_SHLIB@';        # substituted via Makefile.tmpl 
-my $CFG_PREFIX        = '@prefix@';            # substituted via APACI install
-my $CFG_SBINDIR       = '@sbindir@';           # substituted via APACI install
-my $CFG_INCLUDEDIR    = '@includedir@';        # substituted via APACI install
-my $CFG_LIBEXECDIR    = '@libexecdir@';        # substituted via APACI install
-my $CFG_SYSCONFDIR    = '@sysconfdir@';        # substituted via APACI install
-
-##
-##  Cleanup the above stuff
-##
-$CFG_CFLAGS =~ s|^\s+||;
-$CFG_CFLAGS =~ s|\s+$||;
-$CFG_CFLAGS =~ s|\s+`.+apaci`||;
+my %config_vars = ();
+
+my $installbuilddir = "@exp_installbuilddir@";
+get_config_vars("$installbuilddir/config_vars.mk",\%config_vars);
+
+# read the configuration variables once
+
+my $prefix         = get_vars("prefix");
+my $CFG_PREFIX     = $prefix;
+my $exec_prefix    = get_vars("exec_prefix");
+my $datadir        = get_vars("datadir");
+my $localstatedir  = get_vars("localstatedir");
+my $CFG_TARGET     = get_vars("progname");
+my $CFG_SYSCONFDIR = get_vars("sysconfdir");
+my $CFG_CFLAGS     = join ' ', map { get_vars($_) }
+  qw(SHLTCFLAGS CFLAGS NOTEST_CPPFLAGS EXTRA_CPPFLAGS EXTRA_CFLAGS);
+my $includedir     = get_vars("includedir");
+my $CFG_INCLUDEDIR = eval qq("$includedir");
+my $CFG_CC         = get_vars("CC");
+my $libexecdir     = get_vars("libexecdir");
+my $CFG_LIBEXECDIR = eval qq("$libexecdir");
+my $sbindir        = get_vars("sbindir");
+my $CFG_SBINDIR    = eval qq("$sbindir");
+my $ltflags        = $ENV{'LTFLAGS'};
+$ltflags or $ltflags = "--silent";
+
+my %internal_vars = map {$_ => 1}
+    qw(TARGET CC CFLAGS CFLAGS_SHLIB LD_SHLIB LDFLAGS_SHLIB LIBS_SHLIB
+       PREFIX SBINDIR INCLUDEDIR LIBEXECDIR SYSCONFDIR);
 
 ##
 ##  parse argument line
@@ -105,43 +73,45 @@ my $opt_i = 0;
 my $opt_a = 0;
 my $opt_A = 0;
 my $opt_q = 0;
+my $opt_h = 0;
+my $opt_p = 0;
+my $opt_v = 0;
 
 #   this subroutine is derived from Perl's getopts.pl with the enhancement of
-#   the "+" metacharater at the format string to allow a list to be build by
-#   subsequent occurance of the same option.
+#   the "+" metacharacter at the format string to allow a list to be built by
+#   subsequent occurrences of the same option.
 sub Getopts {
     my ($argumentative, @ARGV) = @_;
-    my (@args, $first, $rest, $pos);
-    my ($errs) = 0;
-    local ($_);
-    local ($[) = 0;
-
-    @args = split( / */, $argumentative);
-    while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
-        ($first, $rest) = ($1,$2);
+    my $errs = 0;
+    local $_;
+    local $[ = 0;
+
+    my @args = split / */, $argumentative;
+    while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
+        my ($first, $rest) = ($1,$2);
         if ($_ =~ m|^--$|) {
-            shift(@ARGV);
+            shift @ARGV;
             last;
         }
-        $pos = index($argumentative,$first);
-        if($pos >= $[) {
-            if($args[$pos+1] eq ':') {
-                shift(@ARGV);
-                if($rest eq '') {
+        my $pos = index($argumentative,$first);
+        if ($pos >= $[) {
+            if ($pos < $#args && $args[$pos+1] eq ':') {
+                shift @ARGV;
+                if ($rest eq '') {
                     unless (@ARGV) {
-                        print STDERR "apxs:Error: Incomplete option: $first (needs an argument)\n";
-                        ++$errs;
+                        error("Incomplete option: $first (needs an argument)");
+                        $errs++;
                     }
                     $rest = shift(@ARGV);
                 }
                 eval "\$opt_$first = \$rest;";
             }
-            elsif ($args[$pos+1] eq '+') {
-                shift(@ARGV);
-                if($rest eq '') {
+            elsif ($pos < $#args && $args[$pos+1] eq '+') {
+                shift @ARGV;
+                if ($rest eq '') {
                     unless (@ARGV) {
-                        print STDERR "apxs:Error: Incomplete option: $first (needs an argument)\n";
-                        ++$errs;
+                        error("Incomplete option: $first (needs an argument)");
+                        $errs++;
                     }
                     $rest = shift(@ARGV);
                 }
@@ -149,7 +119,7 @@ sub Getopts {
             }
             else {
                 eval "\$opt_$first = 1";
-                if($rest eq '') {
+                if ($rest eq '') {
                     shift(@ARGV);
                 }
                 else {
@@ -158,9 +128,9 @@ sub Getopts {
             }
         }
         else {
-            print STDERR "apxs:Error: Unknown option: $first\n";
-            ++$errs;
-            if($rest ne '') {
+            error("Unknown option: $first");
+            $errs++;
+            if ($rest ne '') {
                 $ARGV[0] = "-$rest";
             }
             else {
@@ -173,10 +143,10 @@ sub Getopts {
 
 sub usage {
     print STDERR "Usage: apxs -g [-S <var>=<val>] -n <modname>\n";
-    print STDERR "       apxs -q [-S <var>=<val>] <query> ...\n";
+    print STDERR "       apxs -q [-v] [-S <var>=<val>] [<query> ...]\n";
     print STDERR "       apxs -c [-S <var>=<val>] [-o <dsofile>] [-D <name>[=<value>]]\n";
     print STDERR "               [-I <incdir>] [-L <libdir>] [-l <libname>] [-Wc,<flags>]\n";
-    print STDERR "               [-Wl,<flags>] <files> ...\n";
+    print STDERR "               [-Wl,<flags>] [-p] <files> ...\n";
     print STDERR "       apxs -i [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
     print STDERR "       apxs -e [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
     exit(1);
@@ -184,9 +154,9 @@ sub usage {
 
 #   option handling
 my $rc;
-($rc, @ARGV) = &Getopts("qn:gco:I+D+L+l+W+S+eiaA", @ARGV);
+($rc, @ARGV) = &Getopts("qn:gco:I+D+L+l+W+S+eiaApv", @ARGV);
 &usage if ($rc == 0);
-&usage if ($#ARGV == -1 and not $opt_g);
+&usage if ($#ARGV == -1 and not $opt_g and not $opt_q);
 &usage if (not $opt_q and not ($opt_g and $opt_n) and not $opt_i and not $opt_c and not $opt_e);
 
 #   argument handling
@@ -218,16 +188,57 @@ if (@opt_S) {
 ##
 ##  Initial shared object support check
 ##
-if (not -x "$CFG_SBINDIR/$CFG_TARGET") {
-       print STDERR "apxs:Error: $CFG_SBINDIR/$CFG_TARGET not found or not executable\n";
-       exit(1);
+unless ("@MOD_SO_ENABLED@" eq "yes") {
+    error("Sorry, no shared object support for Apache");
+    error("available under your platform. Make sure");
+    error("the Apache module mod_so is compiled into");
+    error("the server binary.");
+    exit 1;
 }
-if (not grep(/mod_so/, `$CFG_SBINDIR/$CFG_TARGET -l`)) {
-    print STDERR "apxs:Error: Sorry, no shared object support for Apache\n";
-    print STDERR "apxs:Error: available under your platform. Make sure\n";
-    print STDERR "apxs:Error: the Apache module mod_so is compiled into\n";
-    print STDERR "apxs:Error: your server binary `$CFG_SBINDIR/$CFG_TARGET'.\n";
-    exit(1);
+
+sub get_config_vars{
+    my ($file, $rh_config) = @_;
+
+    open IN, $file or die "cannot open $file: $!";
+    while (<IN>){
+        if (/^\s*(.*?)\s*=\s*(.*)$/){
+            $rh_config->{$1} = $2;
+        }
+    }
+    close IN;
+}
+
+sub get_vars {
+    my $result = '';
+    my $ok = 0;
+    my $arg;
+    foreach $arg (@_) {
+        if (exists $config_vars{$arg} or exists $config_vars{lc $arg}) {
+            my $val = exists $config_vars{$arg}
+                ? $config_vars{$arg}
+                : $config_vars{lc $arg};
+            $val =~ s/[()]//g;
+            $result .= eval "qq($val)" if defined $val;
+            $result .= ";;";
+            $ok = 1;
+        }
+        if (not $ok) {
+            if (exists $internal_vars{$arg} or exists $internal_vars{lc $arg}) {
+                my $val = exists $internal_vars{$arg} ? $arg : lc $arg;
+                $val = eval "\$CFG_$val";
+                $result .= eval "qq($val)" if defined $val;
+                $result .= ";;";
+                $ok = 1;
+            }
+            if (not $ok) {
+                error("Invalid query string `$arg'");
+                exit(1);
+            }
+        }
+    }
+    $result =~ s|;;$||;
+    $result =~ s|:| |;
+    return $result;
 }
 
 ##
@@ -241,11 +252,11 @@ sub execute_cmds {
     my ($cmd, $rc);
 
     foreach $cmd (@cmds) {
-        print STDERR "$cmd\n";
-        $rc = system("$cmd");
-        if ($rc != 0) {
-            printf(STDERR "apxs:Break: Command failed with rc=%d\n", $rc << 8);
-            exit(1);
+        notice($cmd);
+        $rc = system $cmd;
+        if ($rc) {
+            error(sprintf "Command failed with rc=%d\n", $rc << 8);
+            exit 1 ;
         }
     }
 }
@@ -256,26 +267,34 @@ if ($opt_g) {
     ##
 
     if (-d $name) {
-        print STDERR "apxs:Error: Directory `$name' already exists. Remove first\n";
+        error("Directory `$name' already exists. Remove first");
         exit(1);
     }
 
     my $data = join('', <DATA>);
     $data =~ s|%NAME%|$name|sg;
     $data =~ s|%TARGET%|$CFG_TARGET|sg;
+    $data =~ s|%PREFIX%|$prefix|sg;
+    $data =~ s|%INSTALLBUILDDIR%|$installbuilddir|sg;
 
-    my ($mkf, $src) = ($data =~ m|^(.+)-=#=-\n(.+)|s);
+    my ($mkf, $mods, $src) = ($data =~ m|^(.+)-=#=-\n(.+)-=#=-\n(.+)|s);
 
-    print STDERR "Creating [DIR]  $name\n";
+    notice("Creating [DIR]  $name");
     system("mkdir $name");
-    print STDERR "Creating [FILE] $name/Makefile\n";
+    notice("Creating [FILE] $name/Makefile");
     open(FP, ">${name}/Makefile") || die;
     print FP $mkf;
     close(FP);
-    print STDERR "Creating [FILE] $name/mod_$name.c\n";
+    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);
 }
@@ -285,30 +304,65 @@ if ($opt_q) {
     ##
     ##  QUERY INFORMATION 
     ##
-
-    my $result = '';
-    my $arg;
-    foreach $arg (@args) {
-        my $ok = 0;
-        my $name;
-        foreach $name (qw(
-            TARGET CC CFLAGS CFLAGS_SHLIB LD_SHLIB LDFLAGS_SHLIB LIBS_SHLIB
-            PREFIX SBINDIR INCLUDEDIR LIBEXECDIR SYSCONFDIR
-        )) {
-            if ($arg eq $name or $arg eq lc($name)) {
-                my $val = eval "\$CFG_$name";
-                $result .= "${val}::";
-                $ok = 1;
+    my $result;
+    if ($#args >= 0) { 
+        $result = get_vars(@args);
+        print "$result\n";
+    } else {
+        # -q without var name prints all variables and their values
+        
+        # Additional -v pretty-prints output
+        if ($opt_v) {
+            # Variable names in alphabetic order
+            my @vars = sort {uc($a) cmp uc($b)} keys %config_vars;
+            
+            # Make the left column as wide as the longest variable name
+            my $width = 0;
+            foreach (@vars) {
+                my $l = length $_; 
+                $width = $l unless ($l <= $width);
+            }
+    
+            foreach (@vars) {
+                printf "%-${width}s = %s\n", $_, $config_vars{$_};
+            }
+        } else {
+            # Unprettified name=value list
+            foreach (keys %config_vars) {
+                print "$_=$config_vars{$_}\n";
             }
-        }
-        if (not $ok) {
-            printf(STDERR "apxs:Error: Invalid query string `%s'\n", $arg);
-            exit(1);
         }
     }
-    $result =~ s|::$||;
-    $result =~ s|::| |;
-    print $result;
+}
+
+my $apr_config = get_vars("APR_CONFIG");
+
+if (! -x "$apr_config") {
+    error("$apr_config not found!");
+    exit(1);
+}
+
+my $apr_major_version = (split /\./, `$apr_config --version`)[0];
+
+my $apu_config = "";
+if ($apr_major_version < 2) {
+    $apu_config = get_vars("APU_CONFIG");
+
+    if (! -x "$apu_config") {
+        error("$apu_config not found!");
+        exit(1);
+    }
+}
+
+my $libtool = `$apr_config --apr-libtool`;
+chomp($libtool);
+
+my $apr_includedir = `$apr_config --includes`;
+chomp($apr_includedir);
+my $apu_includedir = "";
+if ($apr_major_version < 2) {
+    $apu_includedir = `$apu_config --includes`;
+    chomp($apu_includedir);
 }
 
 if ($opt_c) {
@@ -334,18 +388,19 @@ if ($opt_c) {
     if ($opt_o eq '') {
         if ($#srcs > -1) {
             $dso_file = $srcs[0];
-            $dso_file =~ s|\.[^.]+$|.so|;
+            $dso_file =~ s|\.[^.]+$|.la|;
         }
         elsif ($#objs > -1) {
             $dso_file = $objs[0];
-            $dso_file =~ s|\.[^.]+$|.so|;
+            $dso_file =~ s|\.[^.]+$|.la|;
         }
         else {
-            $dso_file = "mod_unknown.so";
+            $dso_file = "mod_unknown.la";
         }
     }
     else {
         $dso_file = $opt_o;
+        $dso_file =~ s|\.[^.]+$|.la|;
     }
 
     #   create compilation commands
@@ -361,29 +416,32 @@ if ($opt_c) {
     foreach $opt_D (@opt_D) {
         $opt .= "-D$opt_D ";
     }
-    my $cflags = "$CFG_CFLAGS $CFG_CFLAGS_SHLIB";
+    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, "$CFG_CC $cflags -I$CFG_INCLUDEDIR $opt -c $s");
-        unshift(@objs, $o);
+        push(@cmds, "$libtool $ltflags --mode=compile $CFG_CC $cflags -I$CFG_INCLUDEDIR $apr_includedir $apu_includedir $opt -c -o $lo $s && touch $slo");
+        unshift(@objs, $lo);
     }
 
     #   create link command
-    my $cmd = "$CFG_LD_SHLIB $CFG_LDFLAGS_SHLIB -o $dso_file";
     my $o;
+    my $lo;    
     foreach $o (@objs) {
-        $cmd .= " $o";
+        $lo .= " $o";
     }
-    $opt = '';
     my ($opt_Wl, $opt_L, $opt_l);
+    $opt = '';
     foreach $opt_Wl (@opt_W) {
-               if($CFG_LD_SHLIB ne "gcc") {
-               $opt .= " $1" if ($opt_Wl =~ m|^\s*l,(.*)$|);
-               } else {
-               $opt .= " -W$opt_Wl";
-               }
+        $opt .= "$1 " if ($opt_Wl =~ m|^\s*l,(.*)$|);
     }
     foreach $opt_L (@opt_L) {
         $opt .= " -L$opt_L";
@@ -391,9 +449,26 @@ if ($opt_c) {
     foreach $opt_l (@opt_l) {
         $opt .= " -l$opt_l";
     }
-    $cmd .= $opt;
-    $cmd .= " $CFG_LIBS_SHLIB";
-    push(@cmds, $cmd);
+
+    if ($opt_p == 1) {
+        
+        my $apr_libs=`$apr_config --cflags --ldflags --link-libtool --libs`;
+        chomp($apr_libs);
+        my $apu_libs="";
+        if ($apr_major_version < 2) {
+            $apu_libs=`$apu_config --ldflags --link-libtool --libs`;
+            chomp($apu_libs);
+        }
+        
+        $opt .= " ".$apu_libs." ".$apr_libs;
+    }
+    else {
+        my $apr_ldflags=`$apr_config --ldflags`;
+        chomp($apr_ldflags);
+        $opt .= " -rpath $CFG_LIBEXECDIR -module -avoid-version $apr_ldflags";
+    }
+
+    push(@cmds, "$libtool $ltflags --mode=link $CFG_CC -o $dso_file $opt $lo");
 
     #   execute the commands
     &execute_cmds(@cmds);
@@ -410,20 +485,23 @@ if ($opt_i or $opt_e) {
     ##
 
     #   determine installation commands
-    #   and corresponding LoadModule/AddModule directives
+    #   and corresponding LoadModule directive
     my @lmd = ();
-    my @amd = ();
     my @cmds = ();
     my $f;
     foreach $f (@args) {
-        if ($f !~ m|\.so$|) {
-            print STDERR "apxs:Error: file $f is not a shared object\n";
+        #  ack all potential gcc, hp/ux, win32, aix and os/x extensions
+        if ($f !~ m#(\.so$|\.la$|\.sl$|\.dll$|\.dylib$|)#) {
+            error("file $f is not a shared object");
             exit(1);
         }
         my $t = $f;
         $t =~ s|^.+/([^/]+)$|$1|;
+        #  use .so unambigiously for installed shared library modules
+        $t =~ s|\.[^./\\]+$|\.so|;
         if ($opt_i) {
-           push(@cmds, "cp $f $CFG_LIBEXECDIR/$t");
+           push(@cmds, "$installbuilddir/instdso.sh SH_LIBTOOL='" .
+                 "$libtool' $f $CFG_LIBEXECDIR");
            push(@cmds, "chmod 755 $CFG_LIBEXECDIR/$t");
         }
 
@@ -437,7 +515,7 @@ if ($opt_i or $opt_e) {
                 open(FP, "<$base.c");
                 my $content = join('', <FP>);
                 close(FP);
-                if ($content =~ m|.*module\s+(?:MODULE_VAR_EXPORT\s+)?([a-zA-Z0-9_]+)_module\s*=\s*.*|s) {
+                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|^[^/]+/||;
@@ -451,8 +529,8 @@ if ($opt_i or $opt_e) {
                 }
             }
             if ($name eq '') {
-                print "apxs:Error: Sorry, cannot determine bootstrap symbol name\n";
-                print "apxs:Error: Please specify one with option `-n'\n";
+                error("Sorry, cannot determine bootstrap symbol name");
+                error("Please specify one with option `-n'");
                 exit(1);
             }
         }
@@ -462,8 +540,8 @@ if ($opt_i or $opt_e) {
         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
@@ -472,7 +550,7 @@ if ($opt_i or $opt_e) {
     #   activate module via LoadModule/AddModule directive
     if ($opt_a or $opt_A) {
         if (not -f "$CFG_SYSCONFDIR/$CFG_TARGET.conf") {
-            print "apxs:Error: Config file $CFG_SYSCONFDIR/$CFG_TARGET.conf not found\n";
+            error("Config file $CFG_SYSCONFDIR/$CFG_TARGET.conf not found");
             exit(1);
         }
 
@@ -481,8 +559,8 @@ if ($opt_i or $opt_e) {
         close(FP);
 
         if ($content !~ m|\n#?\s*LoadModule\s+|) {
-            print STDERR "apxs:Error: Activation failed for custom $CFG_SYSCONFDIR/$CFG_TARGET.conf file.\n";
-            print STDERR "apxs:Error: At least one `LoadModule' directive already has to exist.\n";
+            error("Activation failed for custom $CFG_SYSCONFDIR/$CFG_TARGET.conf file.");
+            error("At least one `LoadModule' directive already has to exist.");
             exit(1);
         }
 
@@ -492,22 +570,74 @@ if ($opt_i or $opt_e) {
         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;
+                # check for open <containers>, so that the new LoadModule
+                # directive always appears *outside* of an <container>.
+
+                my $before = ($content =~ m|^(.*\n)#?\s*LoadModule\s+[^\n]+\n|s)[0];
+
+                # the '()=' trick forces list context and the scalar
+                # assignment counts the number of list members (aka number
+                # of matches) then
+                my $cntopen = () = ($before =~ m|^\s*<[^/].*$|mg);
+                my $cntclose = () = ($before =~ m|^\s*</.*$|mg);
+
+                if ($cntopen == $cntclose) {
+                    # fine. Last LoadModule is contextless.
+                    $content =~ s|^(.*\n#?\s*LoadModule\s+[^\n]+\n)|$1$c$lmd\n|s;
+                }
+                elsif ($cntopen < $cntclose) {
+                    error('Configuration file is not valid. There are sections'
+                          . ' closed before opened.');
+                    exit(1);
+                }
+                else {
+                    # put our cmd after the section containing the last
+                    # LoadModule.
+                    my $found =
+                    $content =~ s!\A (               # string and capture start
+                                  (?:(?:
+                                    ^\s*             # start of conf line with a
+                                    (?:[^<]|<[^/])   # directive which does not
+                                                     # start with '</'
+
+                                    .*(?:$)\n        # rest of the line.
+                                                     # the '$' is in parentheses
+                                                     # to avoid misinterpreting
+                                                     # the string "$\" as
+                                                     # perl variable.
+
+                                    )*               # catch as much as possible
+                                                     # of such lines. (including
+                                                     # zero)
+
+                                    ^\s*</.*(?:$)\n? # after the above, we
+                                                     # expect a config line with
+                                                     # a closing container (</)
+
+                                  ) {$cntopen}       # the whole pattern (bunch
+                                                     # of lines that end up with
+                                                     # a closing directive) must
+                                                     # be repeated $cntopen
+                                                     # times. That's it.
+                                                     # Simple, eh? ;-)
+
+                                  )                  # capture end
+                                 !$1$c$lmd\n!mx;
+
+                    unless ($found) {
+                        error('Configuration file is not valid. There are '
+                              . 'sections opened and not closed.');
+                        exit(1);
+                    }
+                }
             } else {
-                 $content =~ s|^(.*\n)#?\s*$lmd[^\n]*\n|$1$c$lmd\n|sg;
+                # replace already existing LoadModule line
+                $content =~ s|^(.*\n)#?\s*$lmd[^\n]*\n|$1$c$lmd\n|s;
             }
             $lmd =~ m|LoadModule\s+(.+?)_module.*|;
-            print STDERR "[$what module `$1' in $CFG_SYSCONFDIR/$CFG_TARGET.conf]\n";
+            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 (@lmd) {
             if (open(FP, ">$CFG_SYSCONFDIR/$CFG_TARGET.conf.new")) {
                 print FP $content;
                 close(FP);
@@ -515,12 +645,20 @@ if ($opt_i or $opt_e) {
                        "cp $CFG_SYSCONFDIR/$CFG_TARGET.conf.new $CFG_SYSCONFDIR/$CFG_TARGET.conf && " .
                        "rm $CFG_SYSCONFDIR/$CFG_TARGET.conf.new");
             } else {
-                print STDERR "unable to open configuration file\n";
+                notice("unable to open configuration file");
             }
        }
     }
 }
 
+sub error{
+    print STDERR "apxs:Error: $_[0].\n";
+}
+
+sub notice{
+    print STDERR "$_[0]\n";
+}
+
 ##EOF##
 __DATA__
 ##
@@ -528,29 +666,29 @@ __DATA__
 ##  Autogenerated via ``apxs -n %NAME% -g''.
 ##
 
+builddir=.
+top_srcdir=%PREFIX%
+top_builddir=%PREFIX%
+include %INSTALLBUILDDIR%/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
+#DEFS=-Dmy_define=my_value
+#INCLUDES=-Imy/include/dir
+#LIBS=-Lmy/lib/dir -lmylib
 
 #   the default target
-all: mod_%NAME%.so
-
-#   compile the shared object file
-mod_%NAME%.so: mod_%NAME%.c
-       $(APXS) -c $(DEF) $(INC) $(LIB) mod_%NAME%.c
+all: local-shared-build
 
 #   install the shared object file into Apache 
-install: all
-       $(APXS) -i -a -n '%NAME%' mod_%NAME%.so
+install: install-modules-yes
 
 #   cleanup
 clean:
-       -rm -f mod_%NAME%.o mod_%NAME%.so
+       -rm -f mod_%NAME%.o mod_%NAME%.lo mod_%NAME%.slo mod_%NAME%.la 
 
 #   simple test
 test: reload
@@ -569,13 +707,18 @@ 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 libexec directory 
+**  DSO file and install it into Apache's modules directory 
 **  by running:
 **
 **    $ apxs -c -i mod_%NAME%.c
@@ -584,7 +727,7 @@ stop:
 **  for the URL /%NAME% in as follows:
 **
 **    #   %TARGET%.conf
-**    LoadModule %NAME%_module libexec/mod_%NAME%.so
+**    LoadModule %NAME%_module modules/mod_%NAME%.so
 **    <Location /%NAME%>
 **    SetHandler %NAME%
 **    </Location>
@@ -593,7 +736,7 @@ stop:
 **
 **    $ apachectl restart
 **
-**  you immediately can request the URL /%NAME and watch for the
+**  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% 
@@ -617,39 +760,29 @@ stop:
 /* The sample content handler */
 static int %NAME%_handler(request_rec *r)
 {
+    if (strcmp(r->handler, "%NAME%")) {
+        return DECLINED;
+    }
     r->content_type = "text/html";      
-    ap_send_http_header(r);
+
     if (!r->header_only)
         ap_rputs("The sample page from mod_%NAME%.c\n", r);
     return OK;
 }
 
-/* Dispatch list of content handlers */
-static const handler_rec %NAME%_handlers[] = { 
-    { "%NAME%", %NAME%_handler }, 
-    { NULL, NULL }
-};
+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 MODULE_VAR_EXPORT %NAME%_module = {
-    STANDARD_MODULE_STUFF, 
-    NULL,                  /* module initializer                  */
+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%_handlers,       /* [#8] MIME-typed-dispatched handlers */
-    NULL,                  /* [#1] URI to filename translation    */
-    NULL,                  /* [#4] validate user id from request  */
-    NULL,                  /* [#5] check if the user is ok _here_ */
-    NULL,                  /* [#3] check access by host address   */
-    NULL,                  /* [#6] determine MIME type            */
-    NULL,                  /* [#7] pre-run fixups                 */
-    NULL,                  /* [#9] log a transaction              */
-    NULL,                  /* [#2] header parser                  */
-    NULL,                  /* child_init                          */
-    NULL,                  /* child_exit                          */
-    NULL                   /* [#0] post read-request              */
+    %NAME%_register_hooks  /* register hooks                      */
 };