xref: /openbsd-src/usr.bin/libtool/LT/Mode/Install.pm (revision 0a6aab58a9ecdd9d26650fde0b817a724ac8dbc6)
1# ex:ts=8 sw=4:
2# $OpenBSD: Install.pm,v 1.9 2023/07/08 08:15:32 espie Exp $
3#
4# Copyright (c) 2007-2010 Steven Mestdagh <steven@openbsd.org>
5# Copyright (c) 2012 Marc Espie <espie@openbsd.org>
6#
7# Permission to use, copy, modify, and distribute this software for any
8# purpose with or without fee is hereby granted, provided that the above
9# copyright notice and this permission notice appear in all copies.
10#
11# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18use v5.36;
19
20package LT::Mode::Install;
21our @ISA = qw(LT::Mode);
22
23use LT::Util;
24use LT::Trace;
25use Getopt::Std;
26use File::Basename;
27
28sub help($)
29{
30	print <<"EOH";
31
32Usage: $0 --mode=install [cp|install] [parameters] source dest
33Install executables/libraries.
34EOH
35}
36
37# don't care about gp or ltconfig for install !
38sub run($class, $ltprog, $, $)
39{
40	# we just parse the options in order to find the actual arguments
41	my @argvcopy = @ARGV;
42	my %install_opts;
43	tsay {"ltprog[-1]  = $$ltprog[-1]"};
44	if ($$ltprog[-1] =~ m/install([.-](sh|check|wrapper))?$/) {
45		getopts('BbCcdf:g:m:o:pSs', \%install_opts);
46		if (@ARGV < 2 && (!defined $install_opts{'d'} && @ARGV == 1)) {
47			die "Wrong number of arguments for install\n";
48		}
49	} elsif ($$ltprog[-1] =~ m/cp$/) {
50		getopts('HLPRfipr', \%install_opts);
51		if (@ARGV < 2) {
52			die "Wrong number of arguments for install\n";
53		}
54	} else {
55		die "Unsupported install program $$ltprog[-1]\n";
56	}
57	my @instopts = @argvcopy[0 .. (@argvcopy - @ARGV - 1)];
58	my $dst = pop @ARGV;
59	my @src = @ARGV;
60	my $dstdir;
61	if (-d $dst) {
62		$dstdir = $dst;
63	} else {
64		# dst is not a directory, i.e. a file
65		if (@src > 1) {
66			# XXX not really libtool's task to check this
67			die "Multiple source files combined with file destination.\n";
68		} else {
69			$dstdir = dirname($dst);
70		}
71	}
72	foreach my $s (@src) {
73		my $dstfile = basename($s);
74		# resolve symbolic links, so we don't try to test later
75		# whether the symlink is a program wrapper etc.
76		if (-l $s) {
77			$s = readlink($s) or die "Cannot readlink $s: $!\n";
78		}
79		my $srcdir = dirname($s);
80		my $srcfile = basename($s);
81		tsay {"srcdir = $srcdir\nsrcfile = $srcfile"};
82		tsay {"dstdir = $dstdir\ndstfile = $dstfile"};
83		if ($srcfile =~ m/^\S+\.la$/) {
84			require LT::LaFile;
85			LT::LaFile->install($s, $dstdir, $ltprog, \@instopts, $install_opts{'s'});
86		} elsif (-f "$srcdir/$ltdir/$srcfile" && is_wrapper($s)) {
87			require LT::Program;
88			LT::Program->install($s, $dst, $ltprog, \@instopts);
89		} else {
90			LT::Exec->install(@$ltprog, @instopts, $s, $dst);
91		}
92	}
93	if (defined $install_opts{d}) {
94		LT::Exec->install(@$ltprog, @instopts, @ARGV);
95	}
96}
97
98sub is_wrapper($program)
99{
100	open(my $pw, '<', $program) or die "Cannot open $program: $!\n";
101	my $line = <$pw>;
102	# if the first line isn't a shell, don't even bother
103	return 0 unless $line =~ m/^\#\!/;
104	my $i = 0;
105	while (my $line = <$pw>) {
106		return 1 if $line =~ m/wrapper\sfor/;
107		last if $i++ > 10;
108	}
109	return 0;
110}
111
1121;
113