delibtoolize.pl revision 851
4249c36b4ab4024464db91e2606f651f3b20c1e5nd#! /usr/perl5/bin/perl
4249c36b4ab4024464db91e2606f651f3b20c1e5nd#
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# Use is subject to license terms.
4249c36b4ab4024464db91e2606f651f3b20c1e5nd#
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# Permission is hereby granted, free of charge, to any person obtaining a
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# copy of this software and associated documentation files (the
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# "Software"), to deal in the Software without restriction, including
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# without limitation the rights to use, copy, modify, merge, publish,
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# distribute, and/or sell copies of the Software, and to permit persons
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# to whom the Software is furnished to do so, provided that the above
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# copyright notice(s) and this permission notice appear in all copies of
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# the Software and that both the above copyright notice(s) and this
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# permission notice appear in supporting documentation.
4249c36b4ab4024464db91e2606f651f3b20c1e5nd#
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4249c36b4ab4024464db91e2606f651f3b20c1e5nd#
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# Except as contained in this notice, the name of a copyright holder
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# shall not be used in advertising or otherwise to promote the sale, use
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# or other dealings in this Software without prior written authorization
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# of the copyright holder.
4249c36b4ab4024464db91e2606f651f3b20c1e5nd#
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# ident "@(#)delibtoolize.pl 1.15 09/12/05 SMI"
4249c36b4ab4024464db91e2606f651f3b20c1e5nd#
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd#
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# Undo libtool damage to makefiles to allow us to control the linker
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# settings that libtool tries to force on us.
4249c36b4ab4024464db91e2606f651f3b20c1e5nd#
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# Usage: delibtoolize.pl [-P] <path>
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# -P - Use large pic flags (-KPIC/-fPIC) instead of default/small (-Kpic/-fpic)
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# -s - Only track libraries from a single file at a time, instead of across all
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# files in a project
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nduse strict;
4249c36b4ab4024464db91e2606f651f3b20c1e5nduse warnings;
4249c36b4ab4024464db91e2606f651f3b20c1e5nduse integer;
4249c36b4ab4024464db91e2606f651f3b20c1e5nduse Getopt::Std;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nduse File::Find;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5ndmy %opts;
4249c36b4ab4024464db91e2606f651f3b20c1e5ndgetopts('Ps', \%opts);
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5ndmy $pic_size = "pic";
4249c36b4ab4024464db91e2606f651f3b20c1e5ndif (exists($opts{'P'})) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $pic_size = "PIC";
4249c36b4ab4024464db91e2606f651f3b20c1e5nd}
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5ndmy $single_file;
4249c36b4ab4024464db91e2606f651f3b20c1e5ndif (exists($opts{'s'})) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $single_file = 1;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd}
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5ndmy %compiler_pic_flags = ( 'cc' => "-K$pic_size -DPIC",
4249c36b4ab4024464db91e2606f651f3b20c1e5nd 'gcc' => "-f$pic_size -DPIC" );
4249c36b4ab4024464db91e2606f651f3b20c1e5ndmy %compiler_sharedobj_flags = ( 'cc' => '-G -z allextract',
4249c36b4ab4024464db91e2606f651f3b20c1e5nd 'gcc' => '-shared -Wl,-z,allextract' );
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5ndmy %so_versions = ();
4249c36b4ab4024464db91e2606f651f3b20c1e5ndmy %ltlib_names = ();
4249c36b4ab4024464db91e2606f651f3b20c1e5ndmy @Makefiles;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5ndsub rulename_to_filename {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $rulename = $_[0];
4249c36b4ab4024464db91e2606f651f3b20c1e5nd if (exists($ltlib_names{$rulename})) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd return $ltlib_names{$rulename};
4249c36b4ab4024464db91e2606f651f3b20c1e5nd } else {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd return $rulename;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd}
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5ndsub scan_file {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd if ($_ eq 'Makefile' && -f $_) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $old_file = $_;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd open my $OLD, '<', $old_file
4249c36b4ab4024464db91e2606f651f3b20c1e5nd or die "Can't open $old_file for reading: $!\n";
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd # Read in original file and preprocess for data we'll need later
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $l = "";
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my %makefile_macros = ();
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my %makefile_ldflags = ();
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd while (my $n = <$OLD>) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $l .= $n;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd # handle line continuation
4249c36b4ab4024464db91e2606f651f3b20c1e5nd next if ($n =~ m/\\$/);
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd # Save macros for later expansion if needed
4249c36b4ab4024464db91e2606f651f3b20c1e5nd if ($l =~ m/^([^\#\s]*)\s*=\s*(.*)\s*/ms) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $makefile_macros{$1} = $2;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd if ($l =~ m/^([^\#\s]*)_la_LDFLAGS\s*=(.*)/ms) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $libname = $1;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $flags = $2;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $makefile_ldflags{$libname} = $flags;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd elsif ($l =~ m/^[^\#\s]*_LTLIBRARIES\s*=(.*)$/ms) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd foreach my $ltl (split /\s+/, $1) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $ltl =~ s{\.la$}{}ms;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $transformed = $ltl;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $transformed =~ s{[^\w\@]}{_}msg;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $ltlib_names{$transformed} = $ltl;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $l = "";
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd close($OLD) or die;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd foreach my $librulename (keys %makefile_ldflags) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $libname = rulename_to_filename($librulename);
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $flags = $makefile_ldflags{$librulename};
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $vers;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $flags =~ s{\$\(([^\)]+)\)}{$makefile_macros{$1}}msg;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd if ($flags =~ m/[\b\s]-version-(number|info)\s+(\S+)/ms) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $vtype = $1;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $v = $2;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd if (($vtype eq "info") && ($v =~ m/^(\d+):\d+:(\d+)$/ms)) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $vers = $1 - $2;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd } elsif ($v =~ m/^(\d+)[:\d]*$/ms) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $vers = $1;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd } else {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $vers = $v;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd elsif ($flags =~ m/-avoid-version\b/ms) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $vers = 'none';
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $ln = $libname;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd if ($single_file) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $ln = $File::Find::name . "::" . $libname;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd if (defined($vers) && !defined($so_versions{$ln})) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $so_versions{$ln} = $vers;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd# print "Set version to $so_versions{$ln} for $ln.\n";
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd push @Makefiles, $File::Find::name;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd}
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5ndsub modify_file {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my ($filename) = @_;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd print "delibtoolizing $filename...\n";
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $old_file = $filename . '~';
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $new_file = $filename;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd rename($new_file, $old_file) or
4249c36b4ab4024464db91e2606f651f3b20c1e5nd die "Can't rename $new_file to $old_file: $!\n";
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd open my $OLD, '<', $old_file
4249c36b4ab4024464db91e2606f651f3b20c1e5nd or die "Can't open $old_file for reading: $!\n";
4249c36b4ab4024464db91e2606f651f3b20c1e5nd open my $NEW, '>', $new_file
4249c36b4ab4024464db91e2606f651f3b20c1e5nd or die "Can't open $new_file for writing: $!\n";
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $compiler;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my @inlines = ();
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd # Read in original file and preprocess for data we'll need later
4249c36b4ab4024464db91e2606f651f3b20c1e5nd my $l = "";
4249c36b4ab4024464db91e2606f651f3b20c1e5nd while (my $n = <$OLD>) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $l .= $n;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd # handle line continuation
4249c36b4ab4024464db91e2606f651f3b20c1e5nd next if ($n =~ m/\\$/);
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd if ($l =~ m/^\s*CC\s*=(?:.*\s+)?(\S+)/) {
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $compiler = $1;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd
4249c36b4ab4024464db91e2606f651f3b20c1e5nd push @inlines, $l;
4249c36b4ab4024464db91e2606f651f3b20c1e5nd $l = "";
4249c36b4ab4024464db91e2606f651f3b20c1e5nd }
4249c36b4ab4024464db91e2606f651f3b20c1e5nd close($OLD) or die;
my $compiler_type = 'cc'; # default to Sun Studio
if (defined($compiler) && ($compiler =~ m/gcc/)) {
$compiler_type = 'gcc';
}
my $picflags = $compiler_pic_flags{$compiler_type};
my $sharedobjflags = $compiler_sharedobj_flags{$compiler_type};
my $curtarget = "";
foreach $l (@inlines) {
chomp $l;
# Remove libtool script from compile steps &
# add PIC flags that libtool normally provides
$l =~ s{\$\(LIBTOOL\)
(?:[\\\s]+ \$\(LT_QUIET\))?
(?:[\\\s]+ \$\(AM_V_lt\))?
(?:[\\\s]+ --tag=(?:CC|CXX))?
(?:[\\\s]+ \$\(AM_LIBTOOLFLAGS\) [\\\s]+ \$\(LIBTOOLFLAGS\))?
[\\\s]+ --mode=compile
[\\\s]+ (\$\(CC\)|\$\(CCAS\)|\$\(CXX\)|\$\(COMPILE\))
}{$1 $picflags}xs;
# Remove libtool script from link step
$l =~ s{\$\(LIBTOOL\)
(?:[\\\s]+ \$\(LT_QUIET\))?
(?:[\\\s]+ \$\(AM_V_lt\))?
(?:[\\\s]+ --tag=(?:CC|CXX))?
(?:[\\\s]+ \$\(AM_LIBTOOLFLAGS\) [\\\s]+ \$\(LIBTOOLFLAGS\))?
[\\\s]+ --mode=link
}{}xs;
# Change -rpath to -R in link arguments
$l =~ s{(\s*)-rpath(\s*)}{$1-R$2}msg;
# Change flags for building shared object from arguments to libtool
# script into arguments to linker
if ($l =~ m/_la_LDFLAGS\s*=/) {
$l =~ s{(\s*$sharedobjflags)+\b}{}msg;
$l =~ s{(_la_LDFLAGS\s*=\s*)}{$1 $sharedobjflags }ms;
$l =~ s{(\s+)-avoid-version\b}{$1}ms;
$l =~ s{(\s+)-module\b}{$1}ms;
$l =~ s{(\s+)-version-(?:number|info)\s+\S+}{$1-h \$\@}ms;
$l =~ s{(\s+)-no-undefined\b}{$1-z defs}ms;
}
# Change file names
my @so_list = keys %so_versions;
if ($single_file) {
my $pat = $filename . "::";
@so_list = grep(/^$pat/, @so_list);
}
foreach my $so (@so_list) {
my $v = $so_versions{$so};
if ($v eq 'none') {
$l =~ s{$so\.la\b}{$so.so}msg;
} else {
$l =~ s{$so\.la\b}{$so.so.$v}msg;
}
}
$l =~ s{\.la\b}{.a}msg;
$l =~ s{\.libs/([\*%])\.o\b}{$1.lo}msg;
$l =~ s{\.lo\b}{.o}msg;
my $newtarget = $curtarget;
if ($l =~ m/^(\S+):/) {
$newtarget = $1;
} elsif ($l =~ m/^\s*$/) {
$newtarget = "";
}
if ($curtarget ne $newtarget) { # end of rules for a target
# Need to add in .so links that libtool makes for .la installs
if ($curtarget =~ m/^install-(.*)LTLIBRARIES$/ms) {
my $dirname = $1;
my $installrule = <<'END_RULE';
list='$(<DIRNAME>_LTLIBRARIES)'; for p in $$list; do \
so=$${p%+(.+(\d))} ; \
if [[ "$$p" != "$$so" ]] ; then \
echo "rm -f $(DESTDIR)$(<DIRNAME>dir)/$$so" ; \
rm -f $(DESTDIR)$(<DIRNAME>dir)/$$so ; \
echo "ln -s $$p $(DESTDIR)$(<DIRNAME>dir)/$$so" ; \
ln -s $$p $(DESTDIR)$(<DIRNAME>dir)/$$so ; \
fi; \
done
END_RULE
$installrule =~ s/\<DIRNAME\>/$dirname/msg;
$l .= $installrule;
}
$curtarget = $newtarget;
}
# Static libraries
if ($curtarget =~ m/^.*\.a$/) {
$l =~ s{\$\(\w*LINK\)}{\$(AR) cru $curtarget}ms;
$l =~ s{\$\(\w*(?:LIBS|LIBADD)\)}{}msg;
$l =~ s{(\$\((?:\w*_)?AR\).*\s+)-R\s*\$\(libdir\)}{$1}msg;
}
print $NEW $l, "\n";
$l = "";
}
close($NEW) or die;
}
find(\&scan_file, @ARGV);
foreach my $mf ( @Makefiles ) {
modify_file($mf);
}