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