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