Exec class to run things.

LaLoFile class to read/write la and lo files.
This commit is contained in:
espie 2008-11-01 13:06:16 +00:00
parent c20c1f3963
commit c17c566a6b

View File

@ -1,5 +1,5 @@
#!/usr/bin/perl
# $OpenBSD: libtool,v 1.50 2008/10/31 10:28:05 espie Exp $
# $OpenBSD: libtool,v 1.51 2008/11/01 13:06:16 espie Exp $
# Copyright (c) 2007-2008 Steven Mestdagh <steven@openbsd.org>
#
@ -24,6 +24,8 @@ use File::Path;
use Getopt::Long;
use Getopt::Std;
package main;
use subs qw(
create_symlinks
debug
@ -37,25 +39,242 @@ use subs qw(
is_prog_wrapper
linkcmd
notyet
parse_file
parse_linkargs
parse_version_info
perform
process_deplibs
resolve_la
reverse_zap_duplicates
write_la_file
write_lo_file
write_prog_wrapper
);
{
package Exec;
my $dry = 0;
my $verbose = 0;
my $performed = 0;
sub performed
{
return $performed;
}
sub dry_run
{
$dry = 1;
}
sub verbose_run
{
$verbose = 1;
}
sub new
{
my $class = shift;
bless {}, $class;
}
sub chdir
{
my ($self, $dir) = @_;
my $class = ref($self) || $self;
bless {dir => $dir}, $class;
}
sub command_run
{
my ($self, @l) = @_;
my $pid = fork();
if ($pid == -1) {
die "Couldn't fork while running @l\n";
}
if ($pid == 0) {
if ($self->{dir}) {
CORE::chdir($self->{dir}) or die "Can't chdir to $self->{dir}\n";
}
exec(@l);
die "Exec failed @l\n";
} else {
my $kid = waitpid($pid, 0);
if ($? != 0) {
die "Error while executing ", join(' ', @l), "\n";
}
}
}
sub shell
{
my ($self, @cmds) = @_;
# create an object "on the run"
if (!ref($self)) {
$self = $self->new;
}
for my $c (@cmds) {
print "$c\n" if $verbose || $dry;
if (!$dry) {
$self->command_run($c);
}
}
$performed++;
}
sub command
{
my ($self, @l) = @_;
# create an object "on the run"
if (!ref($self)) {
$self = $self->new;
}
print "@l\n" if $verbose || $dry;
if (!$dry) {
$self->command_run(@l);
}
$performed++;
}
}
package LaLoFile;
use File::Basename;
my %file_cache; # which files have been parsed
sub parse
{
my ($class, $filename) = @_;
my $key = basename($filename);
main::debug 1, "parsing $filename";
if (defined $file_cache{$key}) {
main::debug 1, " (cached)\n";
return $file_cache{$key};
} else {
main::debug 1, "\n";
open(my $fh, '<', $filename) or die "cannot read $filename: $!\n";
my $info = $class->new;
while (<$fh>) {
chomp;
next if /^\#/;
next if /^\s*$/;
if (m/^(\S+)\=\'(.*)\'$/) {
$info->{$1} = $2;
} elsif (m/^(\S+)\=(\S+)$/) {
$info->{$1} = $2;
}
}
$file_cache{$key} = $info;
return $info;
}
}
sub new
{
my $class = shift;
bless {}, $class;
}
package LaFile;
our @ISA=(qw(LaLoFile));
use File::Basename;
# XXX not sure how much of this cruft we need
sub write
{
my ($lainfo, $filename, $name) = @_;
my $libname = $lainfo->{'libname'} || '';
my $sharedlibname = $lainfo->{'dlname'} || '';
my $staticlibname = $lainfo->{'old_library'} || '';
my $librarynames = $lainfo->{'library_names'} || '';
my $deplibs = $lainfo->{'dependency_libs'};
my ($current, $revision, $age) = ('', '', '');
$current = $lainfo->{'current'} if (defined $lainfo->{'current'});
$revision = $lainfo->{'revision'} if (defined $lainfo->{'revision'});
$age = $lainfo->{'age'} if (defined $lainfo->{'age'});
my $installed = $lainfo->{'installed'};
my $shouldnotlink = $lainfo->{'shouldnotlink'};
my $libdir = $lainfo->{'libdir'} || '';
open(my $la, '>', $filename) or die "cannot write $filename: $!\n";
print "creating $filename\n" if $main::verbose || $main::D;
print $la <<EOF
# $name - libtool library file
# Generated by libtool $main::version
#
# Please DO NOT delete this file!
# It is necessary for linking the library.
# The name that we can dlopen(3).
dlname='$sharedlibname'
# Names of this library.
library_names='$librarynames'
# The name of the static archive.
old_library='$staticlibname'
# Libraries that this one depends upon.
dependency_libs='$deplibs'
# Version information for $libname.
current=$current
age=$age
revision=$revision
# Is this an already installed library?
installed=$installed
# Should we warn about portability when linking against -modules?
shouldnotlink=$shouldnotlink
# Files to dlopen/dlpreopen
dlopen=''
dlpreopen=''
# Directory that this library needs to be installed in:
libdir='$libdir'
EOF
;
}
package LoFile;
our @ISA=(qw(LaLoFile));
use File::Basename;
# write a libtool object file
sub write
{
my ($self, $filename) = @_;
my $picobj = $self->{picobj} || '';
my $nonpicobj = $self->{nonpicobj} || '';
my $name = basename $filename;
open(my $lo, '>', $filename) or die "cannot write $filename: $!\n";
print "creating $filename\n" if $main::verbose || $main::D;
print $lo <<EOF
# $name - a libtool object file
# Generated by libtool $main::version
#
pic_object='$picobj'
non_pic_object='$nonpicobj'
EOF
;
}
package main;
use constant {
OBJECT => 0,
LIBRARY => 1,
PROGRAM => 2,
};
my $version = '1.5.26'; # pretend to be this version of libtool
our $version = '1.5.26'; # pretend to be this version of libtool
my @no_shared_archs = qw(m88k vax sh);
# XXX my $machine_arch = `machine -a`;
my $machine_arch = 'amd64';
@ -73,7 +292,6 @@ $instlibdir = $ENV{'LIBDIR'} if defined $ENV{'LIBDIR'};
my $mode;
my $D = 0; # debug flag
my $verbose = 1;
my $dry = 0; # dry-run
my %opts; # options passed to libtool
my @Ropts; # -R options on the command line
@ -85,7 +303,6 @@ my %libs; # libraries
my %libstofind;
my @orderedlibs; # ordered library keys (may contain duplicates)
my %dirs; # paths to find libraries
my %file_cache; # which files have been parsed
my $res_level = 0; # resolve level
my $parse_level = 0; # parse recursion level
my $performed = 0; # number of commands executed via system()
@ -121,7 +338,7 @@ $gp->configure( 'no_ignore_case',
);
$gp->getoptions('config' => \&config,
'debug' => \$D,
'dry-run|n' => sub { $dry = 1; },
'dry-run|n' => sub { Exec->dry_run },
'features' => \&features,
'finish' => sub { $mode = 'finish'; },
'help' => \&help, # does not return
@ -132,6 +349,9 @@ $gp->getoptions('config' => \&config,
'version' => sub { print "$version\n" ; exit(0); },
);
if ($verbose || $D) {
Exec->verbose_run;
}
# what are we going to run (cc, c++, ...)
my $ltprog = shift @ARGV or die "no libtool command\n";
@ -214,7 +434,7 @@ if ($mode eq 'compile') {
$cmd .= " -o ";
$cmd .= ($odir eq '.') ? '' : $odir.'/';
$cmd .= $picobj;
perform($cmd);
Exec->shell($cmd);
}
if ($nonpic) {
$cmd = $ltprog;
@ -222,10 +442,12 @@ if ($mode eq 'compile') {
$cmd .= " -o ";
$cmd .= ($odir eq '.') ? '' : $odir.'/';
$cmd .= $nonpicobj;
perform($cmd);
Exec->shell($cmd);
}
write_lo_file($outfile, ($pic) ? $picobj : '',
($nonpic) ? $nonpicobj : '');
my $lofile = LoFile->new;
$lofile->{picobj} = $picobj if $pic;
$lofile->{nonpicobj} = $nonpicobj if $nonpic;
$lofile->write($outfile);
} elsif ($mode eq 'install') {
# deal with multi-arg ltprog (e.g. /bin/sh install-sh ...)
while (@ARGV && $ltprog !~ m/(install([.-]sh)?|cp)$/) {
@ -281,7 +503,7 @@ if ($mode eq 'compile') {
if ($ltprog =~ m/install([.-]sh)?$/) {
push @instopts, '-m 644';
}
my $lainfo = parse_file($s);
my $lainfo = LaFile->parse($s);
# replace info where needed when installing the .la file
my $sharedlib = $lainfo->{'dlname'};
my $staticlib = $lainfo->{'old_library'};
@ -308,13 +530,14 @@ if ($mode eq 'compile') {
if ($s =~ m/\.la$/ || $d =~ m /\.la$/) {
map { $_ = '' if $_ eq '-s' } @realinstopts;
}
perform("$ltprog @realinstopts $s $d");
Exec->shell("$ltprog @realinstopts $s $d");
}
while (my ($d, $s) = each %tosymlink) {
perform("cd $dstdir && rm -f $d && ln -s $s $d");
unlink("$dstdir/$d");
symlink($s, "$dstdir/$d");
}
if (defined $install_opts{'d'}) {
perform("$ltprog @instopts @ARGV");
Exec->shell("$ltprog @instopts @ARGV");
}
} elsif ($mode eq 'link') {
my $cmd;
@ -416,7 +639,7 @@ if ($mode eq 'compile') {
}
my @tmpcmd = linkcmds($ofile, $ofile, $odir, PROGRAM, 1, $objlist);
$cmd = $tmpcmd[0];
perform($cmd);
Exec->shell($cmd);
write_prog_wrapper($outfile);
chmod 0755, $outfile;
} elsif ($linkmode == LIBRARY) {
@ -465,26 +688,26 @@ if ($mode eq 'compile') {
debug 1, "SHARED: $shared\nSTATIC: $static\n";
my %lainfo;
$lainfo{'libname'} = $libname;
my $lainfo = LaFile->new;
$lainfo->{'libname'} = $libname;
if ($shared) {
$lainfo{'dlname'} = $sharedlib;
$lainfo{'library_names'} = $sharedlib;
$lainfo{'library_names'} .= " $sharedlib_symlink"
$lainfo->{'dlname'} = $sharedlib;
$lainfo->{'library_names'} = $sharedlib;
$lainfo->{'library_names'} .= " $sharedlib_symlink"
if ($opts{'release'});
perform(linkcmds($ofile, $sharedlib, $odir, LIBRARY, 1, \@sobjs));
Exec->shell(linkcmds($ofile, $sharedlib, $odir, LIBRARY, 1, \@sobjs));
debug 1, "sharedlib: $sharedlib\n";
$lainfo{'current'} = $current;
$lainfo{'revision'} = $revision;
$lainfo{'age'} = $age;
$lainfo->{'current'} = $current;
$lainfo->{'revision'} = $revision;
$lainfo->{'age'} = $age;
}
if ($static) {
$lainfo{'old_library'} = $staticlib;
perform(linkcmds($ofile, $staticlib, $odir, LIBRARY, 0, ($allpicobj) ? \@sobjs : \@objs));
$lainfo->{'old_library'} = $staticlib;
Exec->shell(linkcmds($ofile, $staticlib, $odir, LIBRARY, 0, ($allpicobj) ? \@sobjs : \@objs));
debug 1, print "staticlib: $staticlib\n";
}
$lainfo{'installed'} = 'no';
$lainfo{'shouldnotlink'} = $opts{'module'} ? 'yes' : 'no';
$lainfo->{'installed'} = 'no';
$lainfo->{'shouldnotlink'} = $opts{'module'} ? 'yes' : 'no';
map { $_ = "-R$_" } @Ropts;
my $deplibstring = join ' ', @deplibs;
$deplibstring = "@Ropts $deplibstring" if (@Ropts);
@ -493,21 +716,22 @@ if ($mode eq 'compile') {
my @finaldeplibs = reverse_zap_duplicates(@deplibs);
debug 1, "finaldeplibs = @finaldeplibs\n";
$deplibstring = join ' ', @finaldeplibs;
$lainfo{'dependency_libs'} = $deplibstring;
$lainfo->{'dependency_libs'} = $deplibstring;
if (@RPopts) {
if (@RPopts > 1) {
debug 1, "more than 1 -rpath option given, taking the first: ", $RPopts[0], "\n";
}
$lainfo{'libdir'} = $RPopts[0];
$lainfo->{'libdir'} = $RPopts[0];
}
write_la_file($outfile, $ofile, \%lainfo);
perform("cd $odir/$ltdir && rm -f $ofile && ln -s ../$ofile $ofile");
$lainfo->write($outfile, $ofile);
unlink("$odir/$ltdir/$ofile");
symlink("../$ofile", "$odir/$ltdir/$ofile");
if ($shared) {
my $lai = "$odir/$ltdir/$ofile".'i';
$lainfo{'dependency_libs'} = process_deplibs($deplibstring);
$lainfo{'installed'} = 'yes';
$lainfo->{'dependency_libs'} = process_deplibs($deplibstring);
$lainfo->{'installed'} = 'yes';
# write .lai file (.la file that will be installed)
write_la_file($lai, $ofile, \%lainfo);
$lainfo->write($lai, $ofile);
}
}
} elsif ($mode eq 'finish' || $mode eq 'clean' || $mode eq 'uninstall') {
@ -515,12 +739,12 @@ if ($mode eq 'compile') {
exit 0;
} elsif ($mode eq 'execute') {
# XXX check whether this is right
perform("$ltprog @ARGV");
Exec->shell("$ltprog @ARGV");
} else {
die "MODE=$mode not implemented yet.\n";
}
if ($performed == 0) {
if (Exec->performed == 0) {
die "no commands to execute.\n"
}
@ -560,20 +784,6 @@ sub debug
}
}
# display and execute a list of commands via system(), and die on error.
sub perform
{
my @cmd = @_;
foreach my $c (@cmd) {
print "$c\n" if ($verbose || $dry || $D);
if (!$dry) {
system($c) == 0 or die "Error while executing:\n$c\n";
}
}
$performed++;
}
# convert 4:5:8 into a list of numbers
sub parse_version_info
{
@ -604,89 +814,6 @@ sub create_symlinks
}
}
# write a libtool object file
sub write_lo_file
{
my $filename = shift;
my $picobj = shift;
my $nonpicobj = shift;
my $name = basename $filename;
open(my $lo, '>', $filename) or die "cannot write $filename: $!\n";
print "creating $filename\n" if ($verbose || $D);
print $lo <<EOF
# $name - a libtool object file
# Generated by libtool $version
#
pic_object='$picobj'
non_pic_object='$nonpicobj'
EOF
;
}
# XXX not sure how much of this cruft we need
sub write_la_file
{
my $filename = shift;
my $name = shift;
my $lainfo = shift;
my $libname = $lainfo->{'libname'} || '';
my $sharedlibname = $lainfo->{'dlname'} || '';
my $staticlibname = $lainfo->{'old_library'} || '';
my $librarynames = $lainfo->{'library_names'} || '';
my $deplibs = $lainfo->{'dependency_libs'};
my ($current, $revision, $age) = ('', '', '');
$current = $lainfo->{'current'} if (defined $lainfo->{'current'});
$revision = $lainfo->{'revision'} if (defined $lainfo->{'revision'});
$age = $lainfo->{'age'} if (defined $lainfo->{'age'});
my $installed = $lainfo->{'installed'};
my $shouldnotlink = $lainfo->{'shouldnotlink'};
my $libdir = $lainfo->{'libdir'} || '';
open(my $la, '>', $filename) or die "cannot write $filename: $!\n";
print "creating $filename\n" if ($verbose || $D);
print $la <<EOF
# $name - libtool library file
# Generated by libtool $version
#
# Please DO NOT delete this file!
# It is necessary for linking the library.
# The name that we can dlopen(3).
dlname='$sharedlibname'
# Names of this library.
library_names='$librarynames'
# The name of the static archive.
old_library='$staticlibname'
# Libraries that this one depends upon.
dependency_libs='$deplibs'
# Version information for $libname.
current=$current
age=$age
revision=$revision
# Is this an already installed library?
installed=$installed
# Should we warn about portability when linking against -modules?
shouldnotlink=$shouldnotlink
# Files to dlopen/dlpreopen
dlopen=''
dlpreopen=''
# Directory that this library needs to be installed in:
libdir='$libdir'
EOF
;
}
# write a wrapper script for an executable so it can be executed within
# the build directory
sub write_prog_wrapper
@ -727,36 +854,6 @@ sub is_prog_wrapper
return eval(grep { m/wrapper\sfor/ } <$pw>);
}
sub parse_file
{
my $filename = shift;
my $key = basename($filename);
debug 1, "parsing $filename";
if (defined $file_cache{$key}) {
debug 1, " (cached)\n";
return $file_cache{$key};
} else {
debug 1, "\n";
open(my $fh, '<', $filename) or die "cannot read $filename: $!\n";
my $info = {};
while (<$fh>) {
chomp;
next if /^\#/;
next if /^\s*$/;
if (m/^(\S+)\=\'(.*)\'$/) {
$info->{$1} = $2;
} elsif (m/^(\S+)\=(\S+)$/) {
$info->{$1} = $2;
}
}
$file_cache{$key} = $info;
return $info;
}
}
# resolve .la files until a level with empty dependency_libs is reached.
sub resolve_la
{
@ -767,7 +864,7 @@ sub resolve_la
foreach my $a (@args) {
next if ($a !~ m/(.*)\.la$/);
my $lainfo = parse_file($a);
my $lainfo = LaFile->parse($a);
if (exists $lainfo->{'dependency_libs'}) {
$res_level++;
$a = $a . ' ' . resolve_la($lainfo->{'dependency_libs'});
@ -876,7 +973,7 @@ sub parse_linkargs
my $d = abs_path(dirname($a));
$dirs{$d} = 1;
my $fulla = abs_path($a);
my $lainfo = parse_file($fulla);
my $lainfo = LaFile->parse($fulla);
my $dlname = $lainfo->{'dlname'};
my $oldlib = $lainfo->{'old_library'};
my $libdir = $lainfo->{'libdir'};
@ -1005,7 +1102,7 @@ sub process_deplibs
$lf = '';
} elsif ($lf =~ m/\/\S+\/(\S+\.la)/) {
my $lafile = $1;
$lf = parse_file($lf)->{'libdir'}.'/'.$lafile;
$lf = LaFile->parse($lf)->{'libdir'}.'/'.$lafile;
}
}
return join ' ', @linkflags;
@ -1172,7 +1269,7 @@ sub generate_objlist
if ($a =~ m/\S+\.lo$/) {
my $ofile = basename $a;
my $odir = dirname $a;
my $loinfo = parse_file($a);
my $loinfo = LoFile->parse($a);
if ($loinfo->{'non_pic_object'}) {
my $o;
$o .= "$odir/" if ($odir ne '.');
@ -1223,7 +1320,7 @@ sub extract_archive
debug 1, "mkdir -p $dir\n";
File::Path::mkpath($dir);
}
perform("cd $dir && ar x $archive");
Exec->chdir($dir)->command('ar', 'x', $archive);
}
sub get_objlist_from_archive