xref: /openbsd/usr.bin/libtool/LT/Mode/Link/Program.pm (revision 9761804b)
1# $OpenBSD: Program.pm,v 1.6 2014/04/20 17:34:26 zhuk 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);
21
22use LT::Program;
23
24package LT::Program;
25
26sub link
27{
28	return LT::Linker::Program->new->link(@_);
29}
30
31package LT::Linker::Program;
32our @ISA = qw(LT::Linker);
33
34use LT::Trace;
35use LT::Util;
36use File::Basename;
37
38sub link
39{
40	my ($linker, $self, $ltprog, $ltconfig, $dirs, $libs, $deplibs,
41	    $libdirs, $parser, $gp) = @_;
42
43	tsay {"linking program (", ($gp->static ? "not " : ""),
44	    "dynamically linking not-installed libtool libraries)"};
45
46	my $fpath  = $self->{outfilepath};
47	my $RPdirs = $self->{RPdirs};
48
49	my $odir  = dirname($fpath);
50	my $fname = basename($fpath);
51
52	my @libflags;
53	my @cmd;
54	my $dst;
55
56	my ($staticlibs, $finalorderedlibs, $args) =
57	    $linker->common1($parser, $gp, $deplibs, $libdirs, $dirs, $libs);
58
59	my $symlinkdir = $ltdir;
60	if ($odir ne '.') {
61		$symlinkdir = "$odir/$ltdir";
62	}
63	mkdir $symlinkdir if ! -d $symlinkdir;
64	if ($parser->{seen_la_shared}) {
65		$dst = ($odir eq '.') ? "$ltdir/$fname" : "$odir/$ltdir/$fname";
66		$self->write_wrapper;
67	} else {
68		$dst = ($odir eq '.') ? $fname : "$odir/$fname";
69	}
70
71	my $rpath_link = LT::UList->new;
72	# add libdirs to rpath if they are not in standard lib path
73	for my $l (@$libdirs) {
74		if (LT::OSConfig->is_search_dir($l)) {
75			push @$rpath_link, $l;
76		} else {
77			push @$RPdirs, $l;
78		}
79	}
80	foreach my $k (keys %$libs) {
81		tprint {"key = $k - "};
82		my $r = ref($libs->{$k});
83		tsay {"ref = $r"};
84		$libs->create($k)->resolve_library($dirs, 1, $gp->static,
85		    ref($self));
86	}
87
88	my @libobjects = values %$libs;
89	tsay {"libs:\n", join("\n", keys %$libs)};
90	tsay {"libfiles:\n", join("\n", map { $_->{fullpath} } @libobjects)};
91
92	$linker->create_symlinks($symlinkdir, $libs);
93	foreach my $k (@$finalorderedlibs) {
94		my $a = $libs->{$k}->{fullpath} || die "Link error: $k not found in \$libs\n";
95		if ($a =~ m/\.a$/) {
96			# don't make a -lfoo out of a static library
97			push @libflags, $a;
98		} else {
99			push @libflags, $linker->infer_libparameter($a, $k);
100		}
101	}
102
103	my @linkeropts = ();
104	if (!$ltconfig->noshared) {
105		for my $d (@$RPdirs) {
106			push(@linkeropts, '-rpath', $d);
107		}
108		for my $d (@$rpath_link) {
109			push(@linkeropts, '-rpath-link', $d);
110		}
111	}
112
113	push(@linkeropts, $linker->export_symbols($ltconfig,
114	    "$odir/$ltdir/$fname", $gp, @{$self->{objlist}}, @$staticlibs));
115
116	@cmd = @$ltprog;
117	push @cmd, '-o', $dst;
118	push @cmd, '-pthread' if $parser->{pthread};
119	push @cmd, @$args if $args;
120	push @cmd, @{$self->{objlist}} if @{$self->{objlist}};
121	push @cmd, @$staticlibs if @$staticlibs;
122	push @cmd, "-L$symlinkdir", @libflags if @libflags;
123	push @cmd, join(',', '-Wl', @linkeropts) if @linkeropts;
124	LT::Exec->link(@cmd);
125}
1261;
127