xref: /openbsd-src/usr.bin/libtool/LT/LaFile.pm (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1# $OpenBSD: LaFile.pm,v 1.19 2012/07/13 13:45:34 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 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, $dirs) = @_;
130
131	# sort dir search order by priority
132	# XXX not fully correct yet
133	my @sdirs = sort { $dirs->{$b} <=> $dirs->{$a} } keys %$dirs;
134	# search in cwd as well
135	unshift @sdirs, '.';
136	tsay {"searching .la for $l"};
137	tsay {"search path= ", join(':', @sdirs)};
138	foreach my $d (@sdirs) {
139		foreach my $la_candidate ("$d/lib$l.la", "$d/$l.la") {
140			if (-f $la_candidate) {
141				tsay {"found $la_candidate"};
142				return $la_candidate;
143			}
144		}
145	}
146	tsay {".la for $l not found!"};
147	return 0;
148}
149
150sub install
151{
152	my ($class, $src, $dstdir, $instprog, $instopts, $strip) = @_;
153
154	my $srcdir = dirname($src);
155	my $srcfile = basename($src);
156	my $dstfile = $srcfile;
157
158	my @opts = @$instopts;
159	my @stripopts = ('--strip-debug');
160	if ($$instprog[-1] =~ m/install([.-]sh)?$/) {
161		push @opts, '-m', '644';
162	}
163
164	my $lainfo = $class->parse($src);
165	my $sharedlib = $lainfo->{'dlname'};
166	my $staticlib = $lainfo->{'old_library'};
167	my $laipath = "$srcdir/$ltdir/$srcfile".'i';
168	if ($staticlib) {
169		# do not strip static libraries, this is done below
170		my @realinstopts = @opts;
171		@realinstopts = grep { $_ ne '-s' } @realinstopts;
172		my $s = "$srcdir/$ltdir/$staticlib";
173		my $d = "$dstdir/$staticlib";
174		LT::Exec->install(@$instprog, @realinstopts, $s, $d);
175		LT::Exec->install('strip', @stripopts, $d) if $strip;
176	}
177	if ($sharedlib) {
178		my $s = "$srcdir/$ltdir/$sharedlib";
179		my $d = "$dstdir/$sharedlib";
180		LT::Exec->install(@$instprog, @opts, $s, $d);
181	}
182	if ($laipath) {
183		# do not try to strip .la files
184		my @realinstopts = @opts;
185		@realinstopts = grep { $_ ne '-s' } @realinstopts;
186		my $s = $laipath;
187		my $d = "$dstdir/$dstfile";
188		LT::Exec->install(@$instprog, @realinstopts, $s, $d);
189	}
190	# for libraries with a -release in their name
191	my @libnames = split /\s+/, $lainfo->{library_names};
192	foreach my $n (@libnames) {
193		next if $n eq $sharedlib;
194		unlink("$dstdir/$n");
195		symlink($sharedlib, "$dstdir/$n");
196	}
197}
198
1991;
200