delibtoolize.pl revision 235
98N/A#! /usr/perl5/bin/perl
98N/A#
98N/A# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
98N/A#
235N/A# Permission is hereby granted, free of charge, to any person obtaining a
235N/A# copy of this software and associated documentation files (the
235N/A# "Software"), to deal in the Software without restriction, including
235N/A# without limitation the rights to use, copy, modify, merge, publish,
235N/A# distribute, and/or sell copies of the Software, and to permit persons
235N/A# to whom the Software is furnished to do so, provided that the above
235N/A# copyright notice(s) and this permission notice appear in all copies of
235N/A# the Software and that both the above copyright notice(s) and this
235N/A# permission notice appear in supporting documentation.
235N/A#
235N/A# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
235N/A# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
235N/A# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
235N/A# OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
235N/A# HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
235N/A# INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
235N/A# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
235N/A# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
235N/A# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
235N/A#
235N/A# Except as contained in this notice, the name of a copyright holder
235N/A# shall not be used in advertising or otherwise to promote the sale, use
235N/A# or other dealings in this Software without prior written authorization
235N/A# of the copyright holder.
235N/A#
235N/A# ident "@(#)delibtoolize.pl 1.7 07/10/10 SMI"
98N/A#
98N/A
98N/A#
98N/A# Undo libtool damage to makefiles to allow us to control the linker
98N/A# settings that libtool tries to force on us.
98N/A#
98N/A
98N/Ause strict;
98N/Ause warnings;
98N/Ause integer;
98N/A
98N/Ause File::Find;
98N/A
98N/Asub process_file {
235N/A if ($_ eq 'Makefile' && -f $_) {
98N/A print "delibtoolizing $File::Find::name...\n";
235N/A my $old_file = $_ . '~';
98N/A my $new_file = $_;
98N/A rename($new_file, $old_file) or
98N/A die "Can't rename $new_file to $old_file: $!\n";
98N/A
235N/A open my $OLD, '<', $old_file
235N/A or die "Can't open $old_file for reading: $!\n";
235N/A open my $NEW, '>', $new_file
235N/A or die "Can't open $new_file for writing: $!\n";
98N/A
98N/A my $compiler;
98N/A my @inlines = ();
98N/A my %so_versions = ();
98N/A
98N/A # Read in original file and preprocess for data we'll need later
235N/A while (my $l = <$OLD>) {
235N/A if ($l =~ m/^\s*CC\s*=\s*(\S*)/) {
98N/A $compiler = $1;
98N/A }
98N/A
98N/A # TODO: handle line continuation
235N/A if ($l =~ m/^(\S*)_la_LDFLAGS\s=.*-version-number (\d+)[:\d]+/ms) {
98N/A $so_versions{$1} = $2;
98N/A }
235N/A if ($l =~ m/^(\S*)_la_LDFLAGS\s=.*\s*-avoid-version\b/ms) {
235N/A $so_versions{$1} = 'none';
235N/A $l =~ s{-avoid-version\b}{}ms;
98N/A }
98N/A
98N/A push @inlines, $l;
98N/A }
235N/A close($OLD) or die;
98N/A
235N/A my $picflags = '-Kpic -DPIC';
235N/A my $sharedobjflags = '-G';
98N/A
235N/A if (defined($compiler) && ($compiler =~ m/gcc/)) {
235N/A $picflags = '-fpic -DPIC';
235N/A $sharedobjflags = '-shared';
98N/A }
98N/A
98N/A my $curtarget = "";
98N/A
191N/A my $l = "";
191N/A
191N/A foreach my $curline (@inlines) {
191N/A $l .= $curline;
191N/A next if ($curline =~ m/\\$/);
235N/A chomp $l;
191N/A
100N/A # Remove libtool script from compile steps &
100N/A # add PIC flags that libtool normally provides
191N/A $l =~ s{\$\(LIBTOOL\)
235N/A (?:[\\\s]+ \$\(LT_QUIET\))?
235N/A (?:[\\\s]+ --tag=(?:CC|CXX))?
191N/A (?:[\\\s]+ \$\(AM_LIBTOOLFLAGS\) [\\\s]+ \$\(LIBTOOLFLAGS\))?
191N/A [\\\s]+ --mode=compile
235N/A [\\\s]+ (\$\(CC\)|\$\(CCAS\)|\$\(CXX\))
191N/A }{$1 $picflags}xs;
98N/A
100N/A # Remove libtool script from link step
191N/A $l =~ s{\$\(LIBTOOL\)
235N/A (?:[\\\s]+ \$\(LT_QUIET\))?
235N/A (?:[\\\s]+ --tag=(?:CC|CXX))?
191N/A (?:[\\\s]+ \$\(AM_LIBTOOLFLAGS\) [\\\s]+ \$\(LIBTOOLFLAGS\))?
191N/A [\\\s]+ --mode=link
191N/A }{}xs;
98N/A
98N/A # Change -rpath to -R in link arguments
235N/A $l =~ s{(\s*)-rpath(\s*)}{$1-R$2}msg;
98N/A
98N/A # Change flags for building shared object from arguments to libtool
98N/A # script into arguments to linker
235N/A if ($l =~ m/_la_LDFLAGS\s*=/) {
235N/A $l =~ s{(\s*$sharedobjflags)+\b}{}msg;
235N/A $l =~ s{(_la_LDFLAGS\s*=\s*)}{$1 $sharedobjflags }ms;
235N/A $l =~ s{\s+-module\b}{}ms;
235N/A $l =~ s{\s+-version-number\s+\d+[:\d]+}{}ms;
235N/A $l =~ s{\s+-no-undefined\b}{ -z defs}ms;
235N/A }
98N/A
98N/A # Change file names
235N/A foreach my $so (keys %so_versions) {
235N/A if ($so_versions{$so} eq 'none') {
235N/A $l =~ s{$so\.la\b}{$so.so}msg;
98N/A } else {
235N/A $l =~ s{$so\.la\b}{$so.so.$so_versions{$so}}msg;
98N/A }
98N/A }
235N/A $l =~ s{\.la\b}{.a}msg;
235N/A $l =~ s{\.libs/\*\.o\b}{*.lo}msg;
235N/A $l =~ s{\.lo\b}{.o}msg;
98N/A
235N/A if ($l =~ m/^(\S+):/) {
98N/A $curtarget = $1;
235N/A } elsif ($l =~ m/^\s*$/) {
98N/A $curtarget = "";
98N/A }
98N/A
235N/A # Static libraries
235N/A if ($curtarget =~ m/^.*\.a$/) {
235N/A $l =~ s{\$\(\w*LINK\)}{\$(AR) cru $curtarget}ms;
235N/A $l =~ s{\$\(\w*(?:LIBS|LIBADD)\)}{}msg;
235N/A $l =~ s{(\$\((?:\w*_)?AR\).*\s+)-R\s*\$\(libdir\)}{$1}msg;
98N/A }
98N/A
235N/A print $NEW $l, "\n";
191N/A $l = "";
98N/A }
235N/A close($NEW) or die;
98N/A }
98N/A}
98N/A
98N/Afind(\&process_file, @ARGV);