xref: /openbsd/usr.bin/libtool/LT/LaFile.pm (revision 09467b48)
1# $OpenBSD: LaFile.pm,v 1.24 2019/09/28 06:25:57 semarie 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 strict;
19use warnings;
20use feature qw(say switch state);
21
22package LT::LaFile;
23use parent qw(LT::LaLoFile);
24use File::Basename;
25use LT::Archive;
26use LT::Util;
27use LT::Trace;
28
29# allows special treatment for some keywords
30sub set
31{
32	my ($self, $k, $v) = @_;
33
34	$self->SUPER::set($k, $v);
35	if ($k eq 'dependency_libs') {
36		my @l = split /\s+/, $v;
37		$self->{deplib_list} = \@l;
38	}
39}
40
41sub deplib_list
42{
43	my $self = shift;
44	return $self->{deplib_list}
45}
46
47# XXX not sure how much of this cruft we need
48sub write
49{
50	my ($lainfo, $filename, $name) = @_;
51
52	my $libname = $lainfo->stringize('libname');
53	my $sharedlibname = $lainfo->stringize('dlname');
54	my $staticlibname = $lainfo->stringize('old_library');
55	my $librarynames = $lainfo->stringize('library_names');
56	my $deplibs = $lainfo->stringize('dependency_libs');
57	my $current = $lainfo->stringize('current');
58	my $revision = $lainfo->stringize('revision');
59	my $age = $lainfo->stringize('age');
60	my $installed = $lainfo->stringize('installed');
61	my $shouldnotlink = $lainfo->stringize('shouldnotlink');
62	my $libdir = $lainfo->stringize('libdir');
63
64	open(my $la, '>', $filename) or die "Cannot write $filename: $!\n";
65	say "creating $filename" if $main::verbose;
66	print $la <<EOF
67# $name - libtool library file
68# Generated by libtool $version
69#
70# Please DO NOT delete this file!
71# It is necessary for linking the library.
72
73# The name that we can dlopen(3).
74dlname='$sharedlibname'
75
76# Names of this library.
77library_names='$librarynames'
78
79# The name of the static archive.
80old_library='$staticlibname'
81
82# Libraries that this one depends upon.
83dependency_libs='$deplibs'
84
85# Version information for $libname.
86current=$current
87age=$age
88revision=$revision
89
90# Is this an already installed library?
91installed=$installed
92
93# Should we warn about portability when linking against -modules?
94shouldnotlink=$shouldnotlink
95
96# Files to dlopen/dlpreopen
97dlopen=''
98dlpreopen=''
99
100# Directory that this library needs to be installed in:
101libdir='$libdir'
102EOF
103;
104}
105
106sub write_shared_libs_log
107{
108	my ($self, $origv) = @_;
109	if (!defined $ENV{SHARED_LIBS_LOG}) {
110	       return;
111	}
112	my $logfile = $ENV{SHARED_LIBS_LOG};
113	my $wantheader = ! -f $logfile;
114	open (my $fh, '>>', $logfile) or return;
115	my $v = join('.', $self->stringize('current'),
116	    $self->stringize('revision'));
117
118	# Remove first leading 'lib', we don't want that in SHARED_LIBS_LOG.
119	my $libname = $self->stringize('libname');
120	$libname =~ s/^lib//;
121	print $fh "# SHARED_LIBS+= <libname>      <obsd version> # <orig version>\n" if $wantheader;
122	printf $fh "SHARED_LIBS +=\t%-20s %-8s # %s\n", $libname, $v, $origv;
123}
124
125# find .la file associated with a -llib flag
126# XXX pick the right one if multiple are found!
127sub find
128{
129	my ($self, $l, $sd) = @_;
130
131	tsay {"searching .la for $l in $sd"};
132	foreach my $la_candidate ("$sd/lib$l.la", "$sd/$l.la") {
133		if (-f $la_candidate) {
134			tsay {"found $la_candidate"};
135			return $la_candidate;
136		}
137	}
138	tsay {".la for $l not found!"};
139	return undef;
140}
141
142sub install
143{
144	my ($class, $src, $dstdir, $instprog, $instopts, $strip) = @_;
145
146	my $srcdir = dirname($src);
147	my $srcfile = basename($src);
148	my $dstfile = $srcfile;
149
150	my @opts = @$instopts;
151	my @stripopts = ('--strip-debug');
152	if ($$instprog[-1] =~ m/install([.-](sh|check|wrapper))?$/) {
153		push @opts, '-m', '644';
154	}
155
156	my $lainfo = $class->parse($src);
157	my $sharedlib = $lainfo->{'dlname'};
158	my $staticlib = $lainfo->{'old_library'};
159	my $laipath = "$srcdir/$ltdir/$srcfile".'i';
160	if ($staticlib) {
161		# do not strip static libraries, this is done below
162		my @realinstopts = @opts;
163		@realinstopts = grep { $_ ne '-s' } @realinstopts;
164		my $s = "$srcdir/$ltdir/$staticlib";
165		my $d = "$dstdir/$staticlib";
166		LT::Exec->install(@$instprog, @realinstopts, $s, $d);
167		LT::Exec->install('strip', @stripopts, $d) if $strip;
168	}
169	if ($sharedlib) {
170		my $s = "$srcdir/$ltdir/$sharedlib";
171		my $d = "$dstdir/$sharedlib";
172		LT::Exec->install(@$instprog, @opts, $s, $d);
173	}
174	if ($laipath) {
175		# do not try to strip .la files
176		my @realinstopts = @opts;
177		@realinstopts = grep { $_ ne '-s' } @realinstopts;
178		my $s = $laipath;
179		my $d = "$dstdir/$dstfile";
180		LT::Exec->install(@$instprog, @realinstopts, $s, $d);
181	}
182	# for libraries with a -release in their name
183	my @libnames = split /\s+/, $lainfo->{library_names};
184	foreach my $n (@libnames) {
185		next if $n eq $sharedlib;
186		unlink("$dstdir/$n");
187		symlink($sharedlib, "$dstdir/$n");
188	}
189}
190
191sub parse
192{
193	my ($class, $filename) = @_;
194
195	my $info = $class->SUPER::parse($filename);
196
197	$info->{deplib_list} //= LT::UList->new;
198	$info->{libdir} //= '';
199
200	return $info;
201}
202
2031;
204