Blame support/apxs.in

Packit 90a5c9
#!@perlbin@ -w
Packit 90a5c9
#
Packit 90a5c9
# Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
# contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
# this work for additional information regarding copyright ownership.
Packit 90a5c9
# The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
# (the "License"); you may not use this file except in compliance with
Packit 90a5c9
# the License.  You may obtain a copy of the License at
Packit 90a5c9
#
Packit 90a5c9
#     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
#
Packit 90a5c9
# Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
# distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
# See the License for the specific language governing permissions and
Packit 90a5c9
# limitations under the License.
Packit 90a5c9
Packit 90a5c9
require 5.004;
Packit 90a5c9
use strict;
Packit 90a5c9
package apxs;
Packit 90a5c9
Packit 90a5c9
##
Packit 90a5c9
##  Configuration
Packit 90a5c9
##
Packit 90a5c9
Packit 90a5c9
my %config_vars = ();
Packit 90a5c9
Packit dfa46a
# Awful hack to make apxs libdir-agnostic:
Packit dfa46a
my $pkg_config = "/usr/bin/pkg-config";
Packit dfa46a
if (! -x "$pkg_config") {
Packit dfa46a
    error("$pkg_config not found!");
Packit dfa46a
    exit(1);
Packit dfa46a
}
Packit dfa46a
Packit dfa46a
my $libdir = `pkg-config --variable=libdir apr-1`;
Packit dfa46a
chomp $libdir;
Packit dfa46a
Packit dfa46a
my $installbuilddir = $libdir . "/httpd/build";
Packit dfa46a
Packit 90a5c9
get_config_vars("$installbuilddir/config_vars.mk",\%config_vars);
Packit 90a5c9
Packit 90a5c9
# read the configuration variables once
Packit 90a5c9
Packit 90a5c9
my $prefix         = get_vars("prefix");
Packit 90a5c9
my $CFG_PREFIX     = $prefix;
Packit 90a5c9
my $exec_prefix    = get_vars("exec_prefix");
Packit 90a5c9
my $datadir        = get_vars("datadir");
Packit 90a5c9
my $localstatedir  = get_vars("localstatedir");
Packit 90a5c9
my $CFG_TARGET     = get_vars("progname");
Packit 90a5c9
my $CFG_SYSCONFDIR = get_vars("sysconfdir");
Packit 90a5c9
my $CFG_CFLAGS     = join ' ', map { get_vars($_) }
Packit 90a5c9
  qw(SHLTCFLAGS CFLAGS NOTEST_CPPFLAGS EXTRA_CPPFLAGS EXTRA_CFLAGS);
Packit 90a5c9
my $CFG_LDFLAGS    = join ' ', map { get_vars($_) }
Packit 90a5c9
  qw(LDFLAGS NOTEST_LDFLAGS SH_LDFLAGS);
Packit 90a5c9
my $includedir     = get_vars("includedir");
Packit 90a5c9
my $CFG_INCLUDEDIR = eval qq("$includedir");
Packit 90a5c9
my $CFG_CC         = get_vars("CC");
Packit 90a5c9
my $libexecdir     = get_vars("libexecdir");
Packit 90a5c9
my $CFG_LIBEXECDIR = eval qq("$libexecdir");
Packit 90a5c9
my $sbindir        = get_vars("sbindir");
Packit 90a5c9
my $CFG_SBINDIR    = eval qq("$sbindir");
Packit 90a5c9
my $ltflags        = $ENV{'LTFLAGS'};
Packit 90a5c9
$ltflags or $ltflags = "--silent";
Packit 90a5c9
Packit 90a5c9
my %internal_vars = map {$_ => 1}
Packit 90a5c9
    qw(TARGET CC CFLAGS CFLAGS_SHLIB LD_SHLIB LDFLAGS_SHLIB LIBS_SHLIB
Packit 90a5c9
       PREFIX SBINDIR INCLUDEDIR LIBEXECDIR SYSCONFDIR);
Packit 90a5c9
Packit 90a5c9
##
Packit 90a5c9
##  parse argument line
Packit 90a5c9
##
Packit 90a5c9
Packit 90a5c9
#   defaults for parameters
Packit 90a5c9
my $opt_n = '';
Packit 90a5c9
my $opt_g = '';
Packit 90a5c9
my $opt_c = 0;
Packit 90a5c9
my $opt_o = '';
Packit 90a5c9
my @opt_D = ();
Packit 90a5c9
my @opt_I = ();
Packit 90a5c9
my @opt_L = ();
Packit 90a5c9
my @opt_l = ();
Packit 90a5c9
my @opt_W = ();
Packit 90a5c9
my @opt_S = ();
Packit 90a5c9
my $opt_e = 0;
Packit 90a5c9
my $opt_i = 0;
Packit 90a5c9
my $opt_a = 0;
Packit 90a5c9
my $opt_A = 0;
Packit 90a5c9
my $opt_q = 0;
Packit 90a5c9
my $opt_h = 0;
Packit 90a5c9
my $opt_p = 0;
Packit 90a5c9
my $opt_v = 0;
Packit 90a5c9
Packit 90a5c9
#   this subroutine is derived from Perl's getopts.pl with the enhancement of
Packit 90a5c9
#   the "+" metacharacter at the format string to allow a list to be built by
Packit 90a5c9
#   subsequent occurrences of the same option.
Packit 90a5c9
sub Getopts {
Packit 90a5c9
    my ($argumentative, @ARGV) = @_;
Packit 90a5c9
    my $errs = 0;
Packit 90a5c9
    local $_;
Packit 90a5c9
Packit 90a5c9
    my @args = split / */, $argumentative;
Packit 90a5c9
    while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
Packit 90a5c9
        my ($first, $rest) = ($1,$2);
Packit 90a5c9
        if ($_ =~ m|^--$|) {
Packit 90a5c9
            shift @ARGV;
Packit 90a5c9
            last;
Packit 90a5c9
        }
Packit 90a5c9
        my $pos = index($argumentative,$first);
Packit 90a5c9
        if ($pos >= 0) {
Packit 90a5c9
            if ($pos < $#args && $args[$pos+1] eq ':') {
Packit 90a5c9
                shift @ARGV;
Packit 90a5c9
                if ($rest eq '') {
Packit 90a5c9
                    unless (@ARGV) {
Packit 90a5c9
                        error("Incomplete option: $first (needs an argument)");
Packit 90a5c9
                        $errs++;
Packit 90a5c9
                    }
Packit 90a5c9
                    $rest = shift(@ARGV);
Packit 90a5c9
                }
Packit 90a5c9
                eval "\$opt_$first = \$rest;";
Packit 90a5c9
            }
Packit 90a5c9
            elsif ($pos < $#args && $args[$pos+1] eq '+') {
Packit 90a5c9
                shift @ARGV;
Packit 90a5c9
                if ($rest eq '') {
Packit 90a5c9
                    unless (@ARGV) {
Packit 90a5c9
                        error("Incomplete option: $first (needs an argument)");
Packit 90a5c9
                        $errs++;
Packit 90a5c9
                    }
Packit 90a5c9
                    $rest = shift(@ARGV);
Packit 90a5c9
                }
Packit 90a5c9
                eval "push(\@opt_$first, \$rest);";
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                eval "\$opt_$first = 1";
Packit 90a5c9
                if ($rest eq '') {
Packit 90a5c9
                    shift(@ARGV);
Packit 90a5c9
                }
Packit 90a5c9
                else {
Packit 90a5c9
                    $ARGV[0] = "-$rest";
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            error("Unknown option: $first");
Packit 90a5c9
            $errs++;
Packit 90a5c9
            if ($rest ne '') {
Packit 90a5c9
                $ARGV[0] = "-$rest";
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                shift(@ARGV);
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    return ($errs == 0, @ARGV);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub usage {
Packit 90a5c9
    print STDERR "Usage: apxs -g [-S =<val>] -n <modname>\n";
Packit 90a5c9
    print STDERR "       apxs -q [-v] [-S =<val>] [<query> ...]\n";
Packit 90a5c9
    print STDERR "       apxs -c [-S =<val>] [-o <dsofile>] [-D <name>[=<value>]]\n";
Packit 90a5c9
    print STDERR "               [-I <incdir>] [-L <libdir>] [-l <libname>] [-Wc,<flags>]\n";
Packit 90a5c9
    print STDERR "               [-Wl,<flags>] [-p] <files> ...\n";
Packit 90a5c9
    print STDERR "       apxs -i [-S =<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
Packit 90a5c9
    print STDERR "       apxs -e [-S =<val>] [-a] [-A] [-n <modname>] <dsofile> ...\n";
Packit 90a5c9
    exit(1);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
#   option handling
Packit 90a5c9
my $rc;
Packit 90a5c9
($rc, @ARGV) = &Getopts("qn:gco:I+D+L+l+W+S+eiaApv", @ARGV);
Packit 90a5c9
&usage if ($rc == 0);
Packit 90a5c9
&usage if ($#ARGV == -1 and not $opt_g and not $opt_q);
Packit 90a5c9
&usage if (not $opt_q and not ($opt_g and $opt_n) and not $opt_i and not $opt_c and not $opt_e);
Packit 90a5c9
Packit 90a5c9
#   argument handling
Packit 90a5c9
my @args = @ARGV;
Packit 90a5c9
my $name = 'unknown';
Packit 90a5c9
$name = $opt_n if ($opt_n ne '');
Packit 90a5c9
Packit 90a5c9
if (@opt_S) {
Packit 90a5c9
    my ($opt_S);
Packit 90a5c9
    foreach $opt_S (@opt_S) {
Packit 90a5c9
	if ($opt_S =~ m/^([^=]+)=(.*)$/) {
Packit 90a5c9
	    my ($var) = $1;
Packit 90a5c9
	    my ($val) = $2;
Packit 90a5c9
	    my $oldval = eval "\$CFG_$var";
Packit 90a5c9
Packit 90a5c9
	    unless ($var and $oldval) {
Packit 90a5c9
		print STDERR "apxs:Error: no config variable $var\n";
Packit 90a5c9
		&usage;
Packit 90a5c9
	    }
Packit 90a5c9
Packit 90a5c9
	    eval "\$CFG_${var}=\"${val}\"";
Packit 90a5c9
	} else {
Packit 90a5c9
	    print STDERR "apxs:Error: malformatted -S option\n";
Packit 90a5c9
	    &usage;
Packit 90a5c9
	}	
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
##
Packit 90a5c9
##  Initial shared object support check
Packit 90a5c9
##
Packit 90a5c9
unless ("@MOD_SO_ENABLED@" eq "yes") {
Packit 90a5c9
    error("Sorry, no shared object support for Apache");
Packit 90a5c9
    error("available under your platform. Make sure");
Packit 90a5c9
    error("the Apache module mod_so is compiled into");
Packit 90a5c9
    error("the server binary.");
Packit 90a5c9
    exit 1;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub get_config_vars{
Packit 90a5c9
    my ($file, $rh_config) = @_;
Packit 90a5c9
Packit 90a5c9
    open IN, $file or die "cannot open $file: $!";
Packit 90a5c9
    while (<IN>){
Packit 90a5c9
        if (/^\s*(.*?)\s*=\s*(.*)$/){
Packit 90a5c9
            $rh_config->{$1} = $2;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    close IN;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub get_vars {
Packit 90a5c9
    my $result = '';
Packit 90a5c9
    my $ok = 0;
Packit 90a5c9
    my $arg;
Packit 90a5c9
    foreach $arg (@_) {
Packit 90a5c9
        if (exists $config_vars{$arg} or exists $config_vars{lc $arg}) {
Packit 90a5c9
            my $val = exists $config_vars{$arg}
Packit 90a5c9
                ? $config_vars{$arg}
Packit 90a5c9
                : $config_vars{lc $arg};
Packit 90a5c9
            $val =~ s/[()]//g;
Packit 90a5c9
            $result .= eval "qq($val)" if defined $val;
Packit 90a5c9
            $result .= ";;";
Packit 90a5c9
            $ok = 1;
Packit 90a5c9
        }
Packit 90a5c9
        if (not $ok) {
Packit 90a5c9
            if (exists $internal_vars{$arg} or exists $internal_vars{lc $arg}) {
Packit 90a5c9
                my $val = exists $internal_vars{$arg} ? $arg : lc $arg;
Packit 90a5c9
                $val = eval "\$CFG_$val";
Packit 90a5c9
                $result .= eval "qq($val)" if defined $val;
Packit 90a5c9
                $result .= ";;";
Packit 90a5c9
                $ok = 1;
Packit 90a5c9
            }
Packit 90a5c9
            if (not $ok) {
Packit 90a5c9
                error("Invalid query string `$arg'");
Packit 90a5c9
                exit(1);
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    $result =~ s|;;$||;
Packit 90a5c9
    return $result;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
##
Packit 90a5c9
##  Operation
Packit 90a5c9
##
Packit 90a5c9
Packit 90a5c9
#   helper function for executing a list of
Packit 90a5c9
#   system command with return code checks
Packit 90a5c9
sub execute_cmds {
Packit 90a5c9
    my (@cmds) = @_;
Packit 90a5c9
    my ($cmd, $rc);
Packit 90a5c9
Packit 90a5c9
    foreach $cmd (@cmds) {
Packit 90a5c9
        notice($cmd);
Packit 90a5c9
        $rc = system $cmd;
Packit 90a5c9
        if ($rc) {
Packit 90a5c9
            error(sprintf "Command failed with rc=%d\n", $rc << 8);
Packit 90a5c9
            exit 1 ;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
if ($opt_g) {
Packit 90a5c9
    ##
Packit 90a5c9
    ##  SAMPLE MODULE SOURCE GENERATION
Packit 90a5c9
    ##
Packit 90a5c9
Packit 90a5c9
    if (-d $name) {
Packit 90a5c9
        error("Directory `$name' already exists. Remove first");
Packit 90a5c9
        exit(1);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    my $data = join('', <DATA>);
Packit 90a5c9
    $data =~ s|%NAME%|$name|sg;
Packit 90a5c9
    $data =~ s|%TARGET%|$CFG_TARGET|sg;
Packit 90a5c9
    $data =~ s|%PREFIX%|$prefix|sg;
Packit dfa46a
    $data =~ s|%LIBDIR%|$libdir|sg;
Packit 90a5c9
Packit 90a5c9
    my ($mkf, $mods, $src) = ($data =~ m|^(.+)-=#=-\n(.+)-=#=-\n(.+)|s);
Packit 90a5c9
Packit 90a5c9
    notice("Creating [DIR]  $name");
Packit 90a5c9
    system("mkdir $name");
Packit 90a5c9
    notice("Creating [FILE] $name/Makefile");
Packit 90a5c9
    open(FP, ">${name}/Makefile") || die;
Packit 90a5c9
    print FP $mkf;
Packit 90a5c9
    close(FP);
Packit 90a5c9
    notice("Creating [FILE] $name/modules.mk");
Packit 90a5c9
    open(FP, ">${name}/modules.mk") || die;
Packit 90a5c9
    print FP $mods;
Packit 90a5c9
    close(FP);
Packit 90a5c9
    notice("Creating [FILE] $name/mod_$name.c");
Packit 90a5c9
    open(FP, ">${name}/mod_${name}.c") || die;
Packit 90a5c9
    print FP $src;
Packit 90a5c9
    close(FP);
Packit 90a5c9
    notice("Creating [FILE] $name/.deps");
Packit 90a5c9
    system("touch ${name}/.deps");
Packit 90a5c9
Packit 90a5c9
    exit(0);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
if ($opt_q) {
Packit 90a5c9
    ##
Packit 90a5c9
    ##  QUERY INFORMATION 
Packit 90a5c9
    ##
Packit 90a5c9
    my $result;
Packit 90a5c9
    if ($#args >= 0) { 
Packit 90a5c9
        $result = get_vars(@args);
Packit 90a5c9
        print "$result\n";
Packit 90a5c9
    } else {
Packit 90a5c9
        # -q without var name prints all variables and their values
Packit 90a5c9
        
Packit 90a5c9
        # Additional -v pretty-prints output
Packit 90a5c9
        if ($opt_v) {
Packit 90a5c9
            # Variable names in alphabetic order
Packit 90a5c9
            my @vars = sort {uc($a) cmp uc($b)} keys %config_vars;
Packit 90a5c9
            
Packit 90a5c9
            # Make the left column as wide as the longest variable name
Packit 90a5c9
            my $width = 0;
Packit 90a5c9
            foreach (@vars) {
Packit 90a5c9
                my $l = length $_; 
Packit 90a5c9
                $width = $l unless ($l <= $width);
Packit 90a5c9
            }
Packit 90a5c9
    
Packit 90a5c9
            foreach (@vars) {
Packit 90a5c9
                printf "%-${width}s = %s\n", $_, $config_vars{$_};
Packit 90a5c9
            }
Packit 90a5c9
        } else {
Packit 90a5c9
            # Unprettified name=value list
Packit 90a5c9
            foreach (keys %config_vars) {
Packit 90a5c9
                print "$_=$config_vars{$_}\n";
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
my $apr_config = get_vars("APR_CONFIG");
Packit 90a5c9
Packit 90a5c9
if (! -x "$apr_config") {
Packit 90a5c9
    error("$apr_config not found!");
Packit 90a5c9
    exit(1);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
my $apr_major_version = (split /\./, `$apr_config --version`)[0];
Packit 90a5c9
Packit 90a5c9
my $apu_config = "";
Packit 90a5c9
if ($apr_major_version < 2) {
Packit 90a5c9
    $apu_config = get_vars("APU_CONFIG");
Packit 90a5c9
Packit 90a5c9
    if (! -x "$apu_config") {
Packit 90a5c9
        error("$apu_config not found!");
Packit 90a5c9
        exit(1);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
my $libtool = `$apr_config --apr-libtool`;
Packit 90a5c9
chomp($libtool);
Packit 90a5c9
Packit 90a5c9
my $apr_includedir = `$apr_config --includes`;
Packit 90a5c9
chomp($apr_includedir);
Packit 90a5c9
my $apu_includedir = "";
Packit 90a5c9
if ($apr_major_version < 2) {
Packit 90a5c9
    $apu_includedir = `$apu_config --includes`;
Packit 90a5c9
    chomp($apu_includedir);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
if ($opt_c) {
Packit 90a5c9
    ##
Packit 90a5c9
    ##  SHARED OBJECT COMPILATION
Packit 90a5c9
    ##
Packit 90a5c9
Packit 90a5c9
    #   split files into sources and objects
Packit 90a5c9
    my @srcs = ();
Packit 90a5c9
    my @objs = ();
Packit 90a5c9
    my $f;
Packit 90a5c9
    foreach $f (@args) {
Packit 90a5c9
        if ($f =~ m|\.c$|) {
Packit 90a5c9
            push(@srcs, $f);
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            push(@objs, $f);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    #   determine output file
Packit 90a5c9
    my $dso_file;
Packit 90a5c9
    if ($opt_o eq '') {
Packit 90a5c9
        if ($#srcs > -1) {
Packit 90a5c9
            $dso_file = $srcs[0];
Packit 90a5c9
            $dso_file =~ s|\.[^.]+$|.la|;
Packit 90a5c9
        }
Packit 90a5c9
        elsif ($#objs > -1) {
Packit 90a5c9
            $dso_file = $objs[0];
Packit 90a5c9
            $dso_file =~ s|\.[^.]+$|.la|;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            $dso_file = "mod_unknown.la";
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        $dso_file = $opt_o;
Packit 90a5c9
        $dso_file =~ s|\.[^.]+$|.la|;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    #   create compilation commands
Packit 90a5c9
    my @cmds = ();
Packit 90a5c9
    my $opt = '';
Packit 90a5c9
    my ($opt_Wc, $opt_I, $opt_D);
Packit 90a5c9
    foreach $opt_Wc (@opt_W) {
Packit 90a5c9
        $opt .= "$1 " if ($opt_Wc =~ m|^\s*c,(.*)$|);
Packit 90a5c9
    }
Packit 90a5c9
    foreach $opt_I (@opt_I) {
Packit 90a5c9
        $opt .= "-I$opt_I ";
Packit 90a5c9
    }
Packit 90a5c9
    foreach $opt_D (@opt_D) {
Packit 90a5c9
        $opt .= "-D$opt_D ";
Packit 90a5c9
    }
Packit 90a5c9
    my $cflags = "$CFG_CFLAGS";
Packit 90a5c9
    my $s;
Packit 90a5c9
    my $mod;
Packit 90a5c9
    foreach $s (@srcs) {
Packit 90a5c9
        my $slo = $s;
Packit 90a5c9
        $slo =~ s|\.c$|.slo|;
Packit 90a5c9
        my $lo = $s;
Packit 90a5c9
        $lo =~ s|\.c$|.lo|;
Packit 90a5c9
        my $la = $s;
Packit 90a5c9
        $la =~ s|\.c$|.la|;
Packit 90a5c9
        my $o = $s;
Packit 90a5c9
        $o =~ s|\.c$|.o|;
Packit 90a5c9
        push(@cmds, "$libtool $ltflags --mode=compile $CFG_CC $cflags -I$CFG_INCLUDEDIR $apr_includedir $apu_includedir $opt -c -o $lo $s && touch $slo");
Packit 90a5c9
        unshift(@objs, $lo);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    #   create link command
Packit 90a5c9
    my $o;
Packit 90a5c9
    my $lo;	
Packit 90a5c9
    foreach $o (@objs) {
Packit 90a5c9
        $lo .= " $o";
Packit 90a5c9
    }
Packit 90a5c9
    my ($opt_Wl, $opt_L, $opt_l);
Packit 90a5c9
    $opt = '';
Packit 90a5c9
    foreach $opt_Wl (@opt_W) {
Packit 90a5c9
        $opt .= "$1 " if ($opt_Wl =~ m|^\s*l,(.*)$|);
Packit 90a5c9
    }
Packit 90a5c9
    foreach $opt_L (@opt_L) {
Packit 90a5c9
        $opt .= " -L$opt_L";
Packit 90a5c9
    }
Packit 90a5c9
    foreach $opt_l (@opt_l) {
Packit 90a5c9
        $opt .= " -l$opt_l";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    my $ldflags = "$CFG_LDFLAGS";
Packit 90a5c9
    if ($opt_p == 1) {
Packit 90a5c9
        
Packit dfa46a
        my $apr_libs=`$apr_config --cflags --ldflags --link-libtool`;
Packit 90a5c9
        chomp($apr_libs);
Packit 90a5c9
        my $apu_libs="";
Packit 90a5c9
        if ($apr_major_version < 2) {
Packit dfa46a
            $apu_libs=`$apu_config --ldflags --link-libtool`;
Packit 90a5c9
            chomp($apu_libs);
Packit 90a5c9
        }
Packit 90a5c9
        
Packit 90a5c9
        $opt .= " ".$apu_libs." ".$apr_libs;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        my $apr_ldflags=`$apr_config --ldflags`;
Packit 90a5c9
        chomp($apr_ldflags);
Packit 90a5c9
        $opt .= " -rpath $CFG_LIBEXECDIR -module -avoid-version $apr_ldflags";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    push(@cmds, "$libtool $ltflags --mode=link $CFG_CC $ldflags -o $dso_file $opt $lo");
Packit 90a5c9
Packit 90a5c9
    #   execute the commands
Packit 90a5c9
    &execute_cmds(@cmds);
Packit 90a5c9
Packit 90a5c9
    #   allow one-step compilation and installation
Packit 90a5c9
    if ($opt_i or $opt_e) {
Packit 90a5c9
        @args = ( $dso_file );
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
if ($opt_i or $opt_e) {
Packit 90a5c9
    ##
Packit 90a5c9
    ##  SHARED OBJECT INSTALLATION
Packit 90a5c9
    ##
Packit 90a5c9
Packit 90a5c9
    #   determine installation commands
Packit 90a5c9
    #   and corresponding LoadModule directive
Packit 90a5c9
    my @lmd = ();
Packit 90a5c9
    my @cmds = ();
Packit 90a5c9
    my $f;
Packit 90a5c9
    foreach $f (@args) {
Packit 90a5c9
        #  ack all potential gcc, hp/ux, win32+os2+aix and os/x extensions
Packit 90a5c9
        if ($f !~ m#(\.so$|\.la$|\.sl$|\.dll$|\.dylib$|)#) {
Packit 90a5c9
            error("file $f is not a shared object");
Packit 90a5c9
            exit(1);
Packit 90a5c9
        }
Packit 90a5c9
        my $t = $f;
Packit 90a5c9
        $t =~ s|^.+/([^/]+)$|$1|;
Packit 90a5c9
        #  use .so unambigiously for installed shared library modules
Packit 90a5c9
        $t =~ s|\.[^./\\]+$|\.so|;
Packit 90a5c9
        if ($opt_i) {
Packit 90a5c9
	    push(@cmds, "$installbuilddir/instdso.sh SH_LIBTOOL='" .
Packit 90a5c9
                 "$libtool' $f $CFG_LIBEXECDIR");
Packit 90a5c9
	    push(@cmds, "chmod 755 $CFG_LIBEXECDIR/$t");
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        #   determine module symbolname and filename
Packit 90a5c9
        my $filename = '';
Packit 90a5c9
        if ($name eq 'unknown') {
Packit 90a5c9
            $name = '';
Packit 90a5c9
            my $base = $f;
Packit 90a5c9
            $base =~ s|\.[^.]+$||;
Packit 90a5c9
            if (-f "$base.c") {
Packit 90a5c9
                open(FP, "<$base.c");
Packit 90a5c9
                my $content = join('', <FP>);
Packit 90a5c9
                close(FP);
Packit 90a5c9
                if ($content =~ m|.*AP_DECLARE_MODULE\s*\(\s*([a-zA-Z0-9_]+)\s*\)\s*=.*|s || $content =~ m|.*module\s+(?:AP_MODULE_DECLARE_DATA\s+)?([a-zA-Z0-9_]+)_module\s*=\s*.*|s) {
Packit 90a5c9
                    $name = "$1";
Packit 90a5c9
                    $filename = "$base.c";
Packit 90a5c9
                    $filename =~ s|^[^/]+/||;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            if ($name eq '') {
Packit 90a5c9
                if ($base =~ m|.*mod_([a-zA-Z0-9_]+)\..+|) {
Packit 90a5c9
                    $name = "$1";
Packit 90a5c9
                    $filename = $base;
Packit 90a5c9
                    $filename =~ s|^[^/]+/||;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            if ($name eq '') {
Packit 90a5c9
                error("Sorry, cannot determine bootstrap symbol name");
Packit 90a5c9
                error("Please specify one with option `-n'");
Packit 90a5c9
                exit(1);
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        if ($filename eq '') {
Packit 90a5c9
            $filename = "mod_${name}.c";
Packit 90a5c9
        }
Packit 90a5c9
        my $dir = $CFG_LIBEXECDIR;
Packit 90a5c9
        $dir =~ s|^$CFG_PREFIX/?||;
Packit 90a5c9
        $dir =~ s|(.)$|$1/|;
Packit 90a5c9
	$t =~ s|\.la$|.so|;
Packit 90a5c9
        push(@lmd, sprintf("LoadModule %-18s %s", "${name}_module", "$dir$t"));
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    #   execute the commands
Packit 90a5c9
    &execute_cmds(@cmds);
Packit 90a5c9
Packit 90a5c9
    #   activate module via LoadModule/AddModule directive
Packit 90a5c9
    if ($opt_a or $opt_A) {
Packit 90a5c9
        if (not -f "$CFG_SYSCONFDIR/$CFG_TARGET.conf") {
Packit 90a5c9
            error("Config file $CFG_SYSCONFDIR/$CFG_TARGET.conf not found");
Packit 90a5c9
            exit(1);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        open(FP, "<$CFG_SYSCONFDIR/$CFG_TARGET.conf") || die;
Packit 90a5c9
        my $content = join('', <FP>);
Packit 90a5c9
        close(FP);
Packit 90a5c9
Packit 90a5c9
        if ($content !~ m|\n#?\s*LoadModule\s+|) {
Packit 90a5c9
            error("Activation failed for custom $CFG_SYSCONFDIR/$CFG_TARGET.conf file.");
Packit 90a5c9
            error("At least one `LoadModule' directive already has to exist.");
Packit 90a5c9
            exit(1);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        my $lmd;
Packit 90a5c9
        my $c = '';
Packit 90a5c9
        $c = '#' if ($opt_A);
Packit 90a5c9
        foreach $lmd (@lmd) {
Packit 90a5c9
            my $what = $opt_A ? "preparing" : "activating";
Packit 90a5c9
            my $lmd_re = $lmd;
Packit 90a5c9
            $lmd_re =~ s/\s+/\\s+/g;
Packit 90a5c9
Packit 90a5c9
            if ($content !~ m|\n#?\s*$lmd_re|) {
Packit 90a5c9
                # check for open <containers>, so that the new LoadModule
Packit 90a5c9
                # directive always appears *outside* of an <container>.
Packit 90a5c9
Packit 90a5c9
                my $before = ($content =~ m|^(.*\n)#?\s*LoadModule\s+[^\n]+\n|s)[0];
Packit 90a5c9
Packit 90a5c9
                # the '()=' trick forces list context and the scalar
Packit 90a5c9
                # assignment counts the number of list members (aka number
Packit 90a5c9
                # of matches) then
Packit 90a5c9
                my $cntopen = () = ($before =~ m|^\s*<[^/].*$|mg);
Packit 90a5c9
                my $cntclose = () = ($before =~ m|^\s*</.*$|mg);
Packit 90a5c9
Packit 90a5c9
                if ($cntopen == $cntclose) {
Packit 90a5c9
                    # fine. Last LoadModule is contextless.
Packit 90a5c9
                    $content =~ s|^(.*\n#?\s*LoadModule\s+[^\n]+\n)|$1$c$lmd\n|s;
Packit 90a5c9
                }
Packit 90a5c9
                elsif ($cntopen < $cntclose) {
Packit 90a5c9
                    error('Configuration file is not valid. There are sections'
Packit 90a5c9
                          . ' closed before opened.');
Packit 90a5c9
                    exit(1);
Packit 90a5c9
                }
Packit 90a5c9
                else {
Packit 90a5c9
                    # put our cmd after the section containing the last
Packit 90a5c9
                    # LoadModule.
Packit 90a5c9
                    my $found =
Packit 90a5c9
                    $content =~ s!\A (               # string and capture start
Packit 90a5c9
                                  (?:(?:
Packit 90a5c9
                                    ^\s*             # start of conf line with a
Packit 90a5c9
                                    (?:[^<]|<[^/])   # directive which does not
Packit 90a5c9
                                                     # start with '</'
Packit 90a5c9
Packit 90a5c9
                                    .*(?:$)\n        # rest of the line.
Packit 90a5c9
                                                     # the '$' is in parentheses
Packit 90a5c9
                                                     # to avoid misinterpreting
Packit 90a5c9
                                                     # the string "$\" as
Packit 90a5c9
                                                     # perl variable.
Packit 90a5c9
Packit 90a5c9
                                    )*               # catch as much as possible
Packit 90a5c9
                                                     # of such lines. (including
Packit 90a5c9
                                                     # zero)
Packit 90a5c9
Packit 90a5c9
                                    ^\s*</.*(?:$)\n? # after the above, we
Packit 90a5c9
                                                     # expect a config line with
Packit 90a5c9
                                                     # a closing container (</)
Packit 90a5c9
Packit 90a5c9
                                  ) {$cntopen}       # the whole pattern (bunch
Packit 90a5c9
                                                     # of lines that end up with
Packit 90a5c9
                                                     # a closing directive) must
Packit 90a5c9
                                                     # be repeated $cntopen
Packit 90a5c9
                                                     # times. That's it.
Packit 90a5c9
                                                     # Simple, eh? ;-)
Packit 90a5c9
Packit 90a5c9
                                  )                  # capture end
Packit 90a5c9
                                 !$1$c$lmd\n!mx;
Packit 90a5c9
Packit 90a5c9
                    unless ($found) {
Packit 90a5c9
                        error('Configuration file is not valid. There are '
Packit 90a5c9
                              . 'sections opened and not closed.');
Packit 90a5c9
                        exit(1);
Packit 90a5c9
                    }
Packit 90a5c9
                }
Packit 90a5c9
            } else {
Packit 90a5c9
                # replace already existing LoadModule line
Packit 90a5c9
                $content =~ s|^(.*\n)#?\s*$lmd_re[^\n]*\n|$1$c$lmd\n|s;
Packit 90a5c9
            }
Packit 90a5c9
            $lmd =~ m|LoadModule\s+(.+?)_module.*|;
Packit 90a5c9
            notice("[$what module `$1' in $CFG_SYSCONFDIR/$CFG_TARGET.conf]");
Packit 90a5c9
        }
Packit 90a5c9
        if (@lmd) {
Packit 90a5c9
            if (open(FP, ">$CFG_SYSCONFDIR/$CFG_TARGET.conf.new")) {
Packit 90a5c9
                print FP $content;
Packit 90a5c9
                close(FP);
Packit 90a5c9
                system("cp $CFG_SYSCONFDIR/$CFG_TARGET.conf $CFG_SYSCONFDIR/$CFG_TARGET.conf.bak && " .
Packit 90a5c9
                       "cp $CFG_SYSCONFDIR/$CFG_TARGET.conf.new $CFG_SYSCONFDIR/$CFG_TARGET.conf && " .
Packit 90a5c9
                       "rm $CFG_SYSCONFDIR/$CFG_TARGET.conf.new");
Packit 90a5c9
            } else {
Packit 90a5c9
                notice("unable to open configuration file");
Packit 90a5c9
            }
Packit 90a5c9
	}
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub error{
Packit 90a5c9
    print STDERR "apxs:Error: $_[0].\n";
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub notice{
Packit 90a5c9
    print STDERR "$_[0]\n";
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
##EOF##
Packit 90a5c9
__DATA__
Packit 90a5c9
##
Packit 90a5c9
##  Makefile -- Build procedure for sample %NAME% Apache module
Packit 90a5c9
##  Autogenerated via ``apxs -n %NAME% -g''.
Packit 90a5c9
##
Packit 90a5c9
Packit 90a5c9
builddir=.
Packit 90a5c9
top_srcdir=%PREFIX%
Packit dfa46a
top_builddir=%LIBDIR%/httpd
Packit dfa46a
include %LIBDIR%/httpd/build/special.mk
Packit 90a5c9
Packit 90a5c9
#   the used tools
Packit 90a5c9
APACHECTL=apachectl
Packit 90a5c9
Packit 90a5c9
#   additional defines, includes and libraries
Packit 90a5c9
#DEFS=-Dmy_define=my_value
Packit 90a5c9
#INCLUDES=-Imy/include/dir
Packit 90a5c9
#LIBS=-Lmy/lib/dir -lmylib
Packit 90a5c9
Packit 90a5c9
#   the default target
Packit 90a5c9
all: local-shared-build
Packit 90a5c9
Packit 90a5c9
#   install the shared object file into Apache 
Packit 90a5c9
install: install-modules-yes
Packit 90a5c9
Packit 90a5c9
#   cleanup
Packit 90a5c9
clean:
Packit 90a5c9
	-rm -f mod_%NAME%.o mod_%NAME%.lo mod_%NAME%.slo mod_%NAME%.la 
Packit 90a5c9
Packit 90a5c9
#   simple test
Packit 90a5c9
test: reload
Packit 90a5c9
	lynx -mime_header http://localhost/%NAME%
Packit 90a5c9
Packit 90a5c9
#   install and activate shared object by reloading Apache to
Packit 90a5c9
#   force a reload of the shared object file
Packit 90a5c9
reload: install restart
Packit 90a5c9
Packit 90a5c9
#   the general Apache start/restart/stop
Packit 90a5c9
#   procedures
Packit 90a5c9
start:
Packit 90a5c9
	$(APACHECTL) start
Packit 90a5c9
restart:
Packit 90a5c9
	$(APACHECTL) restart
Packit 90a5c9
stop:
Packit 90a5c9
	$(APACHECTL) stop
Packit 90a5c9
Packit 90a5c9
-=#=-
Packit 90a5c9
mod_%NAME%.la: mod_%NAME%.slo
Packit 90a5c9
	$(SH_LINK) -rpath $(libexecdir) -module -avoid-version  mod_%NAME%.lo
Packit 90a5c9
DISTCLEAN_TARGETS = modules.mk
Packit 90a5c9
shared =  mod_%NAME%.la
Packit 90a5c9
-=#=-
Packit 90a5c9
/* 
Packit 90a5c9
**  mod_%NAME%.c -- Apache sample %NAME% module
Packit 90a5c9
**  [Autogenerated via ``apxs -n %NAME% -g'']
Packit 90a5c9
**
Packit 90a5c9
**  To play with this sample module first compile it into a
Packit 90a5c9
**  DSO file and install it into Apache's modules directory 
Packit 90a5c9
**  by running:
Packit 90a5c9
**
Packit 90a5c9
**    $ apxs -c -i mod_%NAME%.c
Packit 90a5c9
**
Packit 90a5c9
**  Then activate it in Apache's %TARGET%.conf file for instance
Packit 90a5c9
**  for the URL /%NAME% in as follows:
Packit 90a5c9
**
Packit 90a5c9
**    #   %TARGET%.conf
Packit 90a5c9
**    LoadModule %NAME%_module modules/mod_%NAME%.so
Packit 90a5c9
**    <Location /%NAME%>
Packit 90a5c9
**    SetHandler %NAME%
Packit 90a5c9
**    </Location>
Packit 90a5c9
**
Packit 90a5c9
**  Then after restarting Apache via
Packit 90a5c9
**
Packit 90a5c9
**    $ apachectl restart
Packit 90a5c9
**
Packit 90a5c9
**  you immediately can request the URL /%NAME% and watch for the
Packit 90a5c9
**  output of this module. This can be achieved for instance via:
Packit 90a5c9
**
Packit 90a5c9
**    $ lynx -mime_header http://localhost/%NAME% 
Packit 90a5c9
**
Packit 90a5c9
**  The output should be similar to the following one:
Packit 90a5c9
**
Packit 90a5c9
**    HTTP/1.1 200 OK
Packit 90a5c9
**    Date: Tue, 31 Mar 1998 14:42:22 GMT
Packit 90a5c9
**    Server: Apache/1.3.4 (Unix)
Packit 90a5c9
**    Connection: close
Packit 90a5c9
**    Content-Type: text/html
Packit 90a5c9
**  
Packit 90a5c9
**    The sample page from mod_%NAME%.c
Packit 90a5c9
*/ 
Packit 90a5c9
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_protocol.h"
Packit 90a5c9
#include "ap_config.h"
Packit 90a5c9
Packit 90a5c9
/* The sample content handler */
Packit 90a5c9
static int %NAME%_handler(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    if (strcmp(r->handler, "%NAME%")) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
    r->content_type = "text/html";      
Packit 90a5c9
Packit 90a5c9
    if (!r->header_only)
Packit 90a5c9
        ap_rputs("The sample page from mod_%NAME%.c\n", r);
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void %NAME%_register_hooks(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    ap_hook_handler(%NAME%_handler, NULL, NULL, APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* Dispatch list for API hooks */
Packit 90a5c9
module AP_MODULE_DECLARE_DATA %NAME%_module = {
Packit 90a5c9
    STANDARD20_MODULE_STUFF, 
Packit 90a5c9
    NULL,                  /* create per-dir    config structures */
Packit 90a5c9
    NULL,                  /* merge  per-dir    config structures */
Packit 90a5c9
    NULL,                  /* create per-server config structures */
Packit 90a5c9
    NULL,                  /* merge  per-server config structures */
Packit 90a5c9
    NULL,                  /* table of config file commands       */
Packit 90a5c9
    %NAME%_register_hooks  /* register hooks                      */
Packit 90a5c9
};
Packit 90a5c9