delibtoolize.pl revision 1054
5322N/A#! /usr/perl5/bin/perl
5322N/A#
5322N/A# Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5322N/A#
5322N/A# Permission is hereby granted, free of charge, to any person obtaining a
5322N/A# copy of this software and associated documentation files (the "Software"),
5322N/A# to deal in the Software without restriction, including without limitation
5322N/A# the rights to use, copy, modify, merge, publish, distribute, sublicense,
5322N/A# and/or sell copies of the Software, and to permit persons to whom the
5322N/A# Software is furnished to do so, subject to the following conditions:
5322N/A#
5322N/A# The above copyright notice and this permission notice (including the next
5322N/A# paragraph) shall be included in all copies or substantial portions of the
5322N/A# Software.
5322N/A#
5322N/A# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5322N/A# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5322N/A# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
5322N/A# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
5322N/A# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
5322N/A# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
5322N/A# DEALINGS IN THE SOFTWARE.
5322N/A#
5322N/A#
5322N/A
5322N/A#
5322N/A# Undo libtool damage to makefiles to allow us to control the linker
5322N/A# settings that libtool tries to force on us.
5322N/A#
5322N/A# Usage: delibtoolize.pl [-P] <path>
5322N/A# -P - Use large pic flags (-KPIC/-fPIC) instead of default/small (-Kpic/-fpic)
5322N/A# -s - Only track libraries from a single file at a time, instead of across all
5322N/A# files in a project
5322N/A
5322N/Ause strict;
5322N/Ause warnings;
5322N/Ause integer;
5322N/Ause Getopt::Std;
5322N/A
5322N/Ause File::Find;
5322N/A
5322N/Amy %opts;
5322N/Agetopts('Ps', \%opts);
5322N/A
5322N/Amy $pic_size = "pic";
5322N/Aif (exists($opts{'P'})) {
5322N/A $pic_size = "PIC";
5322N/A}
5322N/A
5322N/Amy $single_file;
5322N/Aif (exists($opts{'s'})) {
5322N/A $single_file = 1;
5322N/A}
5322N/A
5322N/Amy %compiler_pic_flags = ( 'cc' => "-K$pic_size -DPIC",
5322N/A 'gcc' => "-f$pic_size -DPIC" );
5322N/Amy %compiler_sharedobj_flags = ( 'cc' => '-G -z allextract',
5322N/A 'gcc' => '-shared -Wl,-z,allextract' );
5322N/A
5322N/Amy %so_versions = ();
5322N/Amy %ltlib_names = ();
5322N/Amy @Makefiles;
5322N/A
5322N/Asub rulename_to_filename {
5322N/A my $rulename = $_[0];
5322N/A if (exists($ltlib_names{$rulename})) {
5322N/A return $ltlib_names{$rulename};
5322N/A } else {
5322N/A return $rulename;
5322N/A }
5322N/A}
5322N/A
5322N/A# Expands make macros in a string
5322N/Asub expand_macros {
5322N/A my ($in, $macro_ref) = @_;
5322N/A
5322N/A $in =~ s{\$\(([^\)]+)\)}{
5322N/A exists($macro_ref->{$1}) ? $macro_ref->{$1} : "" }msgex;
5322N/A
5322N/A return $in;
5322N/A}
5322N/A
5322N/Asub scan_file {
5322N/A if ($_ eq 'Makefile' && -f $_) {
5322N/A my $old_file = $_;
5322N/A
5322N/A open my $OLD, '<', $old_file
5322N/A or die "Can't open $old_file for reading: $!\n";
5322N/A
5322N/A # Read in original file and preprocess for data we'll need later
5322N/A my $l = "";
5322N/A my %makefile_macros = ();
5322N/A my %makefile_ldflags = ();
5322N/A my @makefile_ltlibs = ();
5322N/A
5322N/A while (my $n = <$OLD>) {
5322N/A $l .= $n;
5322N/A # handle line continuation
5322N/A next if ($n =~ m/\\$/);
5322N/A
5322N/A # Save macros for later expansion if needed
5322N/A if ($l =~ m/^([^\#\s]*)\s*=\s*(.*?)\s*$/ms) {
5322N/A $makefile_macros{$1} = $2;
5322N/A }
5322N/A
5322N/A if ($l =~ m/^([^\#\s]*)_la_LDFLAGS\s*=(.*)/ms) {
5322N/A my $libname = $1;
5322N/A my $flags = $2;
5322N/A
5322N/A $makefile_ldflags{$libname} = $flags;
5322N/A }
5322N/A elsif ($l =~ m/^[^\#\s]*_LTLIBRARIES\s*=(.*)$/ms) {
5322N/A push @makefile_ltlibs, $1;
5322N/A }
5322N/A $l = "";
5322N/A }
5322N/A close($OLD) or die;
5322N/A
5322N/A foreach my $ltline ( @makefile_ltlibs ) {
5322N/A my $ltline_exp = expand_macros($ltline, \%makefile_macros);
5322N/A foreach my $ltl (split /\s+/, $ltline_exp) {
5322N/A $ltl =~ s{\.la$}{}ms;
5322N/A my $transformed = $ltl;
5322N/A $transformed =~ s{[^\w\@]}{_}msg;
5322N/A $ltlib_names{$transformed} = $ltl;
5322N/A }
5322N/A }
5322N/A
5322N/A foreach my $librulename (keys %makefile_ldflags) {
5322N/A my $libname = rulename_to_filename($librulename);
5322N/A my $flags = expand_macros($makefile_ldflags{$librulename},
5322N/A \%makefile_macros);
5322N/A my $vers;
5322N/A
5322N/A if ($flags =~ m/[\b\s]-version-(number|info)\s+(\S+)/ms) {
5322N/A my $vtype = $1;
5322N/A my $v = $2;
5322N/A
5322N/A if (($vtype eq "info") && ($v =~ m/^(\d+):\d+:(\d+)$/ms)) {
5322N/A $vers = $1 - $2;
5322N/A } elsif ($v =~ m/^(\d+)[:\d]*$/ms) {
5322N/A $vers = $1;
5322N/A } else {
5322N/A $vers = $v;
5322N/A }
5322N/A }
5322N/A elsif ($flags =~ m/-avoid-version\b/ms) {
5322N/A $vers = 'none';
5322N/A }
5322N/A
5322N/A my $ln = $libname;
5322N/A if ($single_file) {
5322N/A $ln = $File::Find::name . "::" . $libname;
5322N/A }
5322N/A if (defined($vers) && !defined($so_versions{$ln})) {
5322N/A $so_versions{$ln} = $vers;
5322N/A# print "Set version to $so_versions{$ln} for $ln.\n";
5322N/A }
5322N/A }
5322N/A
5322N/A push @Makefiles, $File::Find::name;
5322N/A }
5322N/A}
5322N/A
5322N/Asub modify_file {
5322N/A my ($filename) = @_;
5322N/A
5322N/A print "delibtoolizing $filename...\n";
5322N/A
5322N/A my $old_file = $filename . '~';
5322N/A my $new_file = $filename;
5322N/A rename($new_file, $old_file) or
5322N/A die "Can't rename $new_file to $old_file: $!\n";
5322N/A
5322N/A open my $OLD, '<', $old_file
5322N/A or die "Can't open $old_file for reading: $!\n";
5322N/A open my $NEW, '>', $new_file
5322N/A or die "Can't open $new_file for writing: $!\n";
5322N/A
5322N/A my $compiler;
5322N/A my @inlines = ();
5322N/A
5322N/A # Read in original file and preprocess for data we'll need later
5322N/A my $l = "";
5322N/A while (my $n = <$OLD>) {
5322N/A $l .= $n;
5322N/A # handle line continuation
5322N/A next if ($n =~ m/\\$/);
5322N/A
5322N/A if ($l =~ m/^\s*CC\s*=(?:.*\s+)?(\S*cc)/) {
5322N/A $compiler = $1;
5322N/A }
5322N/A
5322N/A push @inlines, $l;
5322N/A $l = "";
5322N/A }
5322N/A close($OLD) or die;
5322N/A
5322N/A my $compiler_type = 'cc'; # default to Sun Studio
5322N/A if (defined($compiler) && ($compiler =~ m/gcc/)) {
5322N/A $compiler_type = 'gcc';
5322N/A }
5322N/A
5322N/A my $picflags = $compiler_pic_flags{$compiler_type};
5322N/A my $sharedobjflags = $compiler_sharedobj_flags{$compiler_type};
5322N/A
5322N/A my $curtarget = "";
5322N/A
5322N/A foreach $l (@inlines) {
5322N/A chomp $l;
5322N/A
5322N/A # Remove libtool script from compile steps &
5322N/A # add PIC flags that libtool normally provides
5322N/A $l =~ s{\$\(LIBTOOL\)
5322N/A (?:[\\\s]+ \$\(LT_QUIET\))?
5322N/A (?:[\\\s]+ \$\(AM_V_lt\))?
5322N/A (?:[\\\s]+ --tag=(?:CC|CXX))?
5322N/A (?:[\\\s]+ \$\(AM_LIBTOOLFLAGS\) [\\\s]+ \$\(LIBTOOLFLAGS\))?
5322N/A [\\\s]+ --mode=compile
5322N/A [\\\s]+ (\$\(CC\)|\$\(CCAS\)|\$\(CXX\)|\$\(COMPILE\))
5322N/A }{$1 $picflags}xs;
5322N/A
5322N/A # Remove libtool script from link step
5322N/A $l =~ s{\$\(LIBTOOL\)
5322N/A (?:[\\\s]+ \$\(LT_QUIET\))?
5322N/A (?:[\\\s]+ \$\(AM_V_lt\))?
5322N/A (?:[\\\s]+ --tag=(?:CC|CXX))?
5322N/A (?:[\\\s]+ \$\(AM_LIBTOOLFLAGS\) [\\\s]+ \$\(LIBTOOLFLAGS\))?
5322N/A [\\\s]+ --mode=link
5322N/A }{}xs;
5322N/A
5322N/A # Change -rpath to -R in link arguments
5322N/A $l =~ s{(\s*)-rpath(\s*)}{$1-R$2}msg;
5322N/A
5322N/A # Change flags for building shared object from arguments to libtool
5322N/A # script into arguments to linker
5322N/A if ($l =~ m/_la_LDFLAGS\s*=/) {
5322N/A $l =~ s{(\s*$sharedobjflags)+\b}{}msg;
5322N/A $l =~ s{(_la_LDFLAGS\s*=\s*)}{$1 $sharedobjflags }ms;
5322N/A $l =~ s{(\s+)-avoid-version\b}{$1}ms;
5322N/A $l =~ s{(\s+)-module\b}{$1}ms;
5322N/A $l =~ s{(\s+)-version-(?:number|info)\s+\S+}{$1-h \$\@}ms;
5322N/A $l =~ s{(\s+)-no-undefined\b}{$1-z defs}ms;
5322N/A }
5322N/A
5322N/A # Change file names
5322N/A my @so_list = keys %so_versions;
5322N/A if ($single_file) {
5322N/A my $pat = $filename . "::";
5322N/A @so_list = grep(/^$pat/, @so_list);
5322N/A }
5322N/A foreach my $so (@so_list) {
5322N/A my $v = $so_versions{$so};
5322N/A if ($v eq 'none') {
5322N/A $l =~ s{$so\.la\b}{$so.so}msg;
5322N/A } else {
5322N/A $l =~ s{$so\.la\b}{$so.so.$v}msg;
5322N/A }
5322N/A }
5322N/A $l =~ s{\.la\b}{.a}msg;
5322N/A $l =~ s{\.libs/([\*%])\.o\b}{$1.lo}msg;
5322N/A $l =~ s{\.lo\b}{.o}msg;
5322N/A
5322N/A my $newtarget = $curtarget;
5322N/A if ($l =~ m/^(\S+):/) {
5322N/A $newtarget = $1;
5322N/A } elsif ($l =~ m/^\s*$/) {
5322N/A $newtarget = "";
5322N/A }
5322N/A
5322N/A if ($curtarget ne $newtarget) { # end of rules for a target
5322N/A # Need to add in .so links that libtool makes for .la installs
5322N/A if ($curtarget =~ m/^install-(.*)LTLIBRARIES$/ms) {
5322N/A my $dirname = $1;
5322N/A my $installrule = <<'END_RULE';
5322N/A list='$(<DIRNAME>_LTLIBRARIES)'; for p in $$list; do \
5322N/A so=$${p%+(.+(\d))} ; \
5322N/A if [[ "$$p" != "$$so" ]] ; then \
5322N/A echo "rm -f $(DESTDIR)$(<DIRNAME>dir)/$$so" ; \
5322N/A rm -f $(DESTDIR)$(<DIRNAME>dir)/$$so ; \
5322N/A echo "ln -s $$p $(DESTDIR)$(<DIRNAME>dir)/$$so" ; \
5322N/A ln -s $$p $(DESTDIR)$(<DIRNAME>dir)/$$so ; \
5322N/A fi; \
5322N/A done
5322N/AEND_RULE
5322N/A $installrule =~ s/\<DIRNAME\>/$dirname/msg;
5322N/A $l .= $installrule;
5322N/A }
5322N/A
5322N/A $curtarget = $newtarget;
5322N/A }
5322N/A
5322N/A # Static libraries
5322N/A if ($curtarget =~ m/^.*\.a$/) {
5322N/A $l =~ s{\$\(\w*LINK\)}{\$(AR) cru $curtarget}ms;
5322N/A $l =~ s{\$\(\w*(?:LIBS|LIBADD)\)}{}msg;
5322N/A $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);
}