1*f41ccc36Sespie# $OpenBSD: LaFile.pm,v 1.25 2023/07/06 08:29:26 espie Exp $ 2dd9b5fdeSespie 3dd9b5fdeSespie# Copyright (c) 2007-2010 Steven Mestdagh <steven@openbsd.org> 4b8664c47Sespie# Copyright (c) 2012 Marc Espie <espie@openbsd.org> 5dd9b5fdeSespie# 6dd9b5fdeSespie# Permission to use, copy, modify, and distribute this software for any 7dd9b5fdeSespie# purpose with or without fee is hereby granted, provided that the above 8dd9b5fdeSespie# copyright notice and this permission notice appear in all copies. 9dd9b5fdeSespie# 10dd9b5fdeSespie# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11dd9b5fdeSespie# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12dd9b5fdeSespie# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13dd9b5fdeSespie# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14dd9b5fdeSespie# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15dd9b5fdeSespie# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16dd9b5fdeSespie# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17dd9b5fdeSespie 18*f41ccc36Sespieuse v5.36; 19dd9b5fdeSespie 20dd9b5fdeSespiepackage LT::LaFile; 21dd9b5fdeSespieuse parent qw(LT::LaLoFile); 22dd9b5fdeSespieuse File::Basename; 23dd9b5fdeSespieuse LT::Archive; 24dd9b5fdeSespieuse LT::Util; 25f98ddbc5Sespieuse LT::Trace; 26dd9b5fdeSespie 27dd9b5fdeSespie# allows special treatment for some keywords 28*f41ccc36Sespiesub set($self, $k, $v) 29dd9b5fdeSespie{ 30dd9b5fdeSespie $self->SUPER::set($k, $v); 31dd9b5fdeSespie if ($k eq 'dependency_libs') { 32dd9b5fdeSespie my @l = split /\s+/, $v; 33dd9b5fdeSespie $self->{deplib_list} = \@l; 34dd9b5fdeSespie } 35dd9b5fdeSespie} 36dd9b5fdeSespie 37*f41ccc36Sespiesub deplib_list($self) 38dd9b5fdeSespie{ 39dd9b5fdeSespie return $self->{deplib_list} 40dd9b5fdeSespie} 41dd9b5fdeSespie 42dd9b5fdeSespie# XXX not sure how much of this cruft we need 43*f41ccc36Sespiesub write($lainfo, $filename, $name) 44dd9b5fdeSespie{ 45dd9b5fdeSespie my $libname = $lainfo->stringize('libname'); 46dd9b5fdeSespie my $sharedlibname = $lainfo->stringize('dlname'); 47dd9b5fdeSespie my $staticlibname = $lainfo->stringize('old_library'); 48dd9b5fdeSespie my $librarynames = $lainfo->stringize('library_names'); 49dd9b5fdeSespie my $deplibs = $lainfo->stringize('dependency_libs'); 50dd9b5fdeSespie my $current = $lainfo->stringize('current'); 51dd9b5fdeSespie my $revision = $lainfo->stringize('revision'); 52dd9b5fdeSespie my $age = $lainfo->stringize('age'); 53dd9b5fdeSespie my $installed = $lainfo->stringize('installed'); 54dd9b5fdeSespie my $shouldnotlink = $lainfo->stringize('shouldnotlink'); 55dd9b5fdeSespie my $libdir = $lainfo->stringize('libdir'); 56dd9b5fdeSespie 57dd9b5fdeSespie open(my $la, '>', $filename) or die "Cannot write $filename: $!\n"; 58b5f8d823Sjasper say "creating $filename" if $main::verbose; 59dd9b5fdeSespie print $la <<EOF 60dd9b5fdeSespie# $name - libtool library file 61dd9b5fdeSespie# Generated by libtool $version 62dd9b5fdeSespie# 63dd9b5fdeSespie# Please DO NOT delete this file! 64dd9b5fdeSespie# It is necessary for linking the library. 65dd9b5fdeSespie 66dd9b5fdeSespie# The name that we can dlopen(3). 67dd9b5fdeSespiedlname='$sharedlibname' 68dd9b5fdeSespie 69dd9b5fdeSespie# Names of this library. 70dd9b5fdeSespielibrary_names='$librarynames' 71dd9b5fdeSespie 72dd9b5fdeSespie# The name of the static archive. 73dd9b5fdeSespieold_library='$staticlibname' 74dd9b5fdeSespie 75dd9b5fdeSespie# Libraries that this one depends upon. 76dd9b5fdeSespiedependency_libs='$deplibs' 77dd9b5fdeSespie 78dd9b5fdeSespie# Version information for $libname. 79dd9b5fdeSespiecurrent=$current 80dd9b5fdeSespieage=$age 81dd9b5fdeSespierevision=$revision 82dd9b5fdeSespie 83dd9b5fdeSespie# Is this an already installed library? 84dd9b5fdeSespieinstalled=$installed 85dd9b5fdeSespie 86dd9b5fdeSespie# Should we warn about portability when linking against -modules? 87dd9b5fdeSespieshouldnotlink=$shouldnotlink 88dd9b5fdeSespie 89dd9b5fdeSespie# Files to dlopen/dlpreopen 90dd9b5fdeSespiedlopen='' 91dd9b5fdeSespiedlpreopen='' 92dd9b5fdeSespie 93dd9b5fdeSespie# Directory that this library needs to be installed in: 94dd9b5fdeSespielibdir='$libdir' 95dd9b5fdeSespieEOF 96dd9b5fdeSespie; 97dd9b5fdeSespie} 98dd9b5fdeSespie 99*f41ccc36Sespiesub write_shared_libs_log($self, $origv) 100dd9b5fdeSespie{ 1012fa24231Sjasper if (!defined $ENV{SHARED_LIBS_LOG}) { 102dd9b5fdeSespie return; 103dd9b5fdeSespie } 1042fa24231Sjasper my $logfile = $ENV{SHARED_LIBS_LOG}; 10576505abaSespie my $wantheader = ! -f $logfile; 10676505abaSespie open (my $fh, '>>', $logfile) or return; 10776505abaSespie my $v = join('.', $self->stringize('current'), 10876505abaSespie $self->stringize('revision')); 10976505abaSespie 110dd9b5fdeSespie # Remove first leading 'lib', we don't want that in SHARED_LIBS_LOG. 11176505abaSespie my $libname = $self->stringize('libname'); 112dd9b5fdeSespie $libname =~ s/^lib//; 11376505abaSespie print $fh "# SHARED_LIBS+= <libname> <obsd version> # <orig version>\n" if $wantheader; 114dd9b5fdeSespie printf $fh "SHARED_LIBS +=\t%-20s %-8s # %s\n", $libname, $v, $origv; 115dd9b5fdeSespie} 116dd9b5fdeSespie 117dd9b5fdeSespie# find .la file associated with a -llib flag 118dd9b5fdeSespie# XXX pick the right one if multiple are found! 119*f41ccc36Sespiesub find($self, $l, $sd) 120dd9b5fdeSespie{ 1211fee14ebSzhuk tsay {"searching .la for $l in $sd"}; 1221fee14ebSzhuk foreach my $la_candidate ("$sd/lib$l.la", "$sd/$l.la") { 123dd9b5fdeSespie if (-f $la_candidate) { 124f98ddbc5Sespie tsay {"found $la_candidate"}; 125dd9b5fdeSespie return $la_candidate; 126dd9b5fdeSespie } 127dd9b5fdeSespie } 128f98ddbc5Sespie tsay {".la for $l not found!"}; 1291fee14ebSzhuk return undef; 130dd9b5fdeSespie} 131dd9b5fdeSespie 132*f41ccc36Sespiesub install($class, $src, $dstdir, $instprog, $instopts, $strip) 133fc08f4fdSespie{ 134fc08f4fdSespie my $srcdir = dirname($src); 135fc08f4fdSespie my $srcfile = basename($src); 136fc08f4fdSespie my $dstfile = $srcfile; 137fc08f4fdSespie 138fc08f4fdSespie my @opts = @$instopts; 139fc08f4fdSespie my @stripopts = ('--strip-debug'); 140f418c875Sespie if ($$instprog[-1] =~ m/install([.-](sh|check|wrapper))?$/) { 141fc08f4fdSespie push @opts, '-m', '644'; 142fc08f4fdSespie } 143fc08f4fdSespie 144fc08f4fdSespie my $lainfo = $class->parse($src); 145fc08f4fdSespie my $sharedlib = $lainfo->{'dlname'}; 146fc08f4fdSespie my $staticlib = $lainfo->{'old_library'}; 147fc08f4fdSespie my $laipath = "$srcdir/$ltdir/$srcfile".'i'; 148fc08f4fdSespie if ($staticlib) { 149fc08f4fdSespie # do not strip static libraries, this is done below 150fc08f4fdSespie my @realinstopts = @opts; 151fc08f4fdSespie @realinstopts = grep { $_ ne '-s' } @realinstopts; 152fc08f4fdSespie my $s = "$srcdir/$ltdir/$staticlib"; 153fc08f4fdSespie my $d = "$dstdir/$staticlib"; 154fc08f4fdSespie LT::Exec->install(@$instprog, @realinstopts, $s, $d); 155fc08f4fdSespie LT::Exec->install('strip', @stripopts, $d) if $strip; 156fc08f4fdSespie } 157fc08f4fdSespie if ($sharedlib) { 158fc08f4fdSespie my $s = "$srcdir/$ltdir/$sharedlib"; 159fc08f4fdSespie my $d = "$dstdir/$sharedlib"; 160fc08f4fdSespie LT::Exec->install(@$instprog, @opts, $s, $d); 161fc08f4fdSespie } 162fc08f4fdSespie if ($laipath) { 163fc08f4fdSespie # do not try to strip .la files 164fc08f4fdSespie my @realinstopts = @opts; 165fc08f4fdSespie @realinstopts = grep { $_ ne '-s' } @realinstopts; 166fc08f4fdSespie my $s = $laipath; 167fc08f4fdSespie my $d = "$dstdir/$dstfile"; 168fc08f4fdSespie LT::Exec->install(@$instprog, @realinstopts, $s, $d); 169fc08f4fdSespie } 170fc08f4fdSespie # for libraries with a -release in their name 171c97be2cbSespie my @libnames = split /\s+/, $lainfo->{library_names}; 172fc08f4fdSespie foreach my $n (@libnames) { 173fc08f4fdSespie next if $n eq $sharedlib; 174fc08f4fdSespie unlink("$dstdir/$n"); 175fc08f4fdSespie symlink($sharedlib, "$dstdir/$n"); 176fc08f4fdSespie } 177fc08f4fdSespie} 178fc08f4fdSespie 179*f41ccc36Sespiesub parse($class, $filename) 1806ffc057aSsemarie{ 1816ffc057aSsemarie my $info = $class->SUPER::parse($filename); 1826ffc057aSsemarie 1836ffc057aSsemarie $info->{deplib_list} //= LT::UList->new; 1846ffc057aSsemarie $info->{libdir} //= ''; 1856ffc057aSsemarie 1866ffc057aSsemarie return $info; 1876ffc057aSsemarie} 1886ffc057aSsemarie 189dd9b5fdeSespie1; 190