summaryrefslogtreecommitdiff
path: root/usr.bin/libtool/LT/Library.pm
blob: eddab99cf54d67f9f03e40b9f2bbe38ecd448739 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# $OpenBSD: Library.pm,v 1.10 2014/04/16 10:31:27 zhuk Exp $

# Copyright (c) 2007-2010 Steven Mestdagh <steven@openbsd.org>
# Copyright (c) 2012 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use strict;
use warnings;
use feature qw(say switch state);

package LT::Library::Stash;

sub new
{
	my $class = shift;

	bless {}, $class;
}

sub create
{
	my ($self, $key) = @_;
	if (!exists $self->{$key}) {
		$self->{$key} = LT::Library->new($key);
	}
	return $self->{$key};
}

package LT::Library;

use LT::Util;
use LT::Trace;

# find actual library filename
# XXX pick the right one if multiple are found!
sub resolve_library
{
	my ($self, $dirs, $shared, $staticflag, $linkmode, $gp) = @_;

	my $libtofind = $self->{key};
	my $libfile = 0;
	my @globbedlib;

	my $pic = '';	# used when finding static libraries
	if ($linkmode eq 'LT::LaFile') {
		$pic = '_pic';
	}

	if (defined $self->{lafile}) {
		require LT::LaFile;
		# if there is a .la file, use the info from there
		tsay {"found .la file $self->{lafile} for library key: ",
		    $self->{key}};
		my $lainfo = LT::LaFile->parse($self->{lafile});
		my $dlname = $lainfo->{dlname};
		my $oldlib = $lainfo->{old_library};
		my $libdir = $lainfo->{libdir};
		my $installed = $lainfo->{installed};
 		my $d = abs_dir($self->{lafile});
		# get the name we need (this may include a -release)
		if (!$dlname && !$oldlib) {
			die "Link error: neither static nor shared library found in $self->{lafile}\n";
		}
		if ($d !~ m/\Q$ltdir\E$/ && $installed eq 'no') {
			$d .= "/$ltdir";
		}
		if ($shared) {
			if ($dlname) {
				$libfile = "$d/$dlname";
			} else {
				# fall back to static
				$libfile = "$d/$oldlib";
			}
			# if -static has been passed, don't link dynamically
			# against not-installed libraries
			if ($staticflag && $installed eq 'no') {
				$libfile = "$d/$oldlib";
			}
		} else {
			$libfile = "$d/$oldlib";
		}
		if (! -f $libfile) {
			tsay {".la file ", $self->{lafile}, 
			    "points to nonexistent file ", $libfile, " !"};
		}
	} else {
		# search in .libs when priority is high
		push @$dirs, $gp->libsearchdirs if $gp;
		tsay {"searching for $libtofind"};
		tsay {"search path= ", join(':', @$dirs)};
		tsay {"search type= ", $shared ? 'shared' : 'static'};
		foreach my $sd (@$dirs) {
		       if ($shared) {
				# select correct library by sorting by version number only
				my $bestlib = $self->findbest($sd, $libtofind);
				if ($bestlib) {
					tsay {"found $libtofind in $sd"};
					$libfile = $bestlib;
					last;
				} else {	
					# XXX find static library instead?
					my $spath = "$sd/lib$libtofind$pic.a";
					if (-f $spath) {
						tsay {"found static $libtofind in $sd"};
						$libfile = $spath;
						last;
					}
				}
		       } else {
				# look for a static library
				my $spath = "$sd/lib$libtofind.a";
				if (-f $spath) {
					tsay {"found static $libtofind in $sd"};
					$libfile = $spath;
					last;
				}
		       }
		}
	}
	if (!$libfile) {
		delete $self->{fullpath};
		if ($linkmode eq 'LT::LaFile') {
			say "warning: dependency on $libtofind dropped";
			$self->{dropped} = 1;
		} elsif ($linkmode eq 'LT::Program') {
			die "Link error: $libtofind not found!\n";
		}
	} else {
		$self->{fullpath} = $libfile;
		tsay {"\$libs->{$self->{key}}->{fullpath} = ", 
		    $self->{fullpath}};
	}
}

sub findbest
{
	my ($self, $sd, $name) = @_;
	my $best = undef;
	if (opendir(my $dir, $sd)) {
		my ($major, $minor) = (-1, -1);
		while (my $e = readdir($dir)) {
			next unless $e =~ m/^lib\Q$name\E\.so\.(\d+)\.(\d+)$/;
			if ($1 > $major || ($1 == $major && $2 > $minor)) {
				($major, $minor) = ($1, $2);
				$best = "$sd/$e";
			}
		}
		closedir($dir);
	}
	return $best;
}

# give a list of library dependencies found in the actual shared library
sub inspect
{
	my $self = shift;

	my $filename = $self->{fullpath};
	my @deps;

	if (!defined($filename)){
		say "warning: library was specified that could not be found: $self->{key}";
		return;
	}
	tsay {"inspecting $filename for library dependencies..."};
	open(my $fh, '-|', "objdump", "-p", "--", $filename);
	while (<$fh>) {
		if (m/\s+NEEDED\s+(\S+)\s*$/) {
			push @deps, $1;
		}
	}
	tsay {"found ", (@deps == 0) ? 'no ' : '',
		"deps for $filename\n@deps"};
	return @deps;
}

sub new
{
	my ($class, $key) = @_;
	bless { key => $key }, $class;
}

1;