develooper Front page | perl.perl5.porters | Postings from January 2001

Re: have_library() and family

Thread Previous | Thread Next
From:
Matt Sergeant
Date:
January 17, 2001 10:53
Subject:
Re: have_library() and family
Message ID:
Pine.LNX.4.30.0101171841170.1406-100000@ted.sergeant.org
On Wed, 17 Jan 2001, Jarkko Hietaniemi wrote:

> On Wed, Jan 17, 2001 at 12:26:22PM +0000, Matt Sergeant wrote:
> > Would anyone be interested in seeing MakeMaker add a have_library(),
> > have_func() and so on be added? These names are taken from Ruby, where in
>
> Oh, yes.  OH, YES.

OK, all I've done so far is have_library(), but there's scope for more
here. And this isn't integrated with MakeMaker itself, because I'm only on
Perl 5.5 here, and this stuff really requires some sort of globals, which
MakeMaker doesn't use... But feel free to play with it. I don't have time
to work on integrating it into MakeMaker yet (or expect to have time
soon). The ruby version of the code can be found in
/prefix/lib/ruby/1.6/mkmf.rb in your ruby installation.

use ExtUtils::MakeMaker;

# %config used by have_library()
my %config;

while($_ = shift) {
    my ($key, $val) = split(/=/, $_, 2);
    $config{$key} = $val;
}

my $DEBUG = delete $config{DEBUG};

# two examples of usage

# 1. check if we can link to libghttp
have_library("ghttp") || die <<DEATH;
You don't have libghttp in your LIBS. Get it from:
  ftp://..../
DEATH

# 2. check if func exists, and set define
have_library("ghttp", "ghttp_get_header_names");

WriteMakefile(
    ... params here
    %config,
);

###############################
# Actual functions of relevance
###############################

use Config;
use Symbol;

my $LINK = "$Config{ld} -o conftest $Config{ccflags} -I$Config{incpath} %s
$Config{ldflags} conftest.c %s %s";

sub rm_f {
    my @files = @_;
    my @realfiles;
    foreach (@files) {
        push @realfiles, glob($_);
    }
    if (@realfiles) {
        chmod(0777, @realfiles);
        unlink(@realfiles);
    }
}

sub xsystem {
    my $command = shift;
    if ($DEBUG) {
        print $command, "\n";
        if (system($command) != 0) {
            die "system call to '$command' failed";
        }
        return 1;
    }
    open(OLDOUT, ">&STDOUT");
    open(OLDERR, ">&STDERR");
    open(STDOUT, ">/dev/null");
    open(STDERR, ">/dev/null");
    my $retval = system($command);
    open(STDOUT, ">&OLDOUT");
    open(STDERR, ">&OLDERR");
    if ($retval != 0) {
        die "system call to '$command' failed";
    }
    return 1;
}

sub backtick {
    my $command = shift;
    if ($DEBUG) {
        print $command, "\n";
        my $results = `$command`;
        chomp $results;
        if ($? != 0) {
            die "backticks call to '$command' failed";
        }
        return $results;
    }
    open(OLDOUT, ">&STDOUT");
    open(OLDERR, ">&STDERR");
    open(STDOUT, ">/dev/null");
    open(STDERR, ">/dev/null");
    my $results = `$command`;
    my $retval = $?;
    open(STDOUT, ">&OLDOUT");
    open(STDERR, ">&OLDERR");
    if ($retval != 0) {
        die "backticks call to '$command' failed";
    }
    chomp $results;
    return $results;
}

sub try_link0 {
    my ($src, $opt) = @_;
    my $cfile = gensym();
    open($cfile, ">conftest.c") || die "Cannot write to file conftest.c";
    print $cfile $src;
    close($cfile);
    xsystem(sprintf($LINK, $config{INC}, $config{LIBS}, $opt));
}

sub try_link {
    my $result = eval {
        try_link0(@_);
    };
    my $err = $@;
    rm_f("conftest*");
    if ($err) {
        die $err;
    }
    return $result;
}

sub have_library {
    my ($lib, $func) = (@_, "main");
    printf("checking for %s() in -l%s... ", $func, $lib);

    my $result;
    if ($func) {
        my $libs = "-l$lib";
        eval {
        $result = try_link(<<"SRC", $libs);
int main() { return 0; }
int t() { ${func}(); return 0; }
SRC
        };
        if ($@) {
            warn $@ if $DEBUG;
        }
    }

    unless ($result) {
        print "no\n";
        return 0;
    }

    if ($func ne "main") {
        $config{DEFINE} .= uc(" -Dhave_$func");
    }

    print "yes\n";
    return 1;
}


-- 
<Matt/>

    /||    ** Director and CTO **
   //||    **  AxKit.com Ltd   **  ** XML Application Serving **
  // ||    ** http://axkit.org **  ** XSLT, XPathScript, XSP  **
 // \\| // **     Personal Web Site: http://sergeant.org/     **
     \\//
     //\\
    //  \\


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About