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