1# ex:ts=8 sw=4: 2# $OpenBSD: Install.pm,v 1.8 2014/09/05 10:36:39 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 strict; 19use warnings; 20 21package LT::Mode::Install; 22our @ISA = qw(LT::Mode); 23 24use LT::Util; 25use LT::Trace; 26use Getopt::Std; 27use File::Basename; 28 29sub help 30{ 31 print <<"EOH"; 32 33Usage: $0 --mode=install [cp|install] [parameters] source dest 34Install executables/libraries. 35EOH 36} 37 38sub run 39{ 40 my ($class, $ltprog) = @_; 41 # we just parse the options in order to find the actual arguments 42 my @argvcopy = @ARGV; 43 my %install_opts; 44 tsay {"ltprog[-1] = $$ltprog[-1]"}; 45 if ($$ltprog[-1] =~ m/install([.-](sh|check|wrapper))?$/) { 46 getopts('BbCcdf:g:m:o:pSs', \%install_opts); 47 if (@ARGV < 2 && (!defined $install_opts{'d'} && @ARGV == 1)) { 48 die "Wrong number of arguments for install\n"; 49 } 50 } elsif ($$ltprog[-1] =~ m/cp$/) { 51 getopts('HLPRfipr', \%install_opts); 52 if (@ARGV < 2) { 53 die "Wrong number of arguments for install\n"; 54 } 55 } else { 56 die "Unsupported install program $$ltprog[-1]\n"; 57 } 58 my @instopts = @argvcopy[0 .. (@argvcopy - @ARGV - 1)]; 59 my $dst = pop @ARGV; 60 my @src = @ARGV; 61 my $dstdir; 62 if (-d $dst) { 63 $dstdir = $dst; 64 } else { 65 # dst is not a directory, i.e. a file 66 if (@src > 1) { 67 # XXX not really libtool's task to check this 68 die "Multiple source files combined with file destination.\n"; 69 } else { 70 $dstdir = dirname($dst); 71 } 72 } 73 foreach my $s (@src) { 74 my $dstfile = basename($s); 75 # resolve symbolic links, so we don't try to test later 76 # whether the symlink is a program wrapper etc. 77 if (-l $s) { 78 $s = readlink($s) or die "Cannot readlink $s: $!\n"; 79 } 80 my $srcdir = dirname($s); 81 my $srcfile = basename($s); 82 tsay {"srcdir = $srcdir\nsrcfile = $srcfile"}; 83 tsay {"dstdir = $dstdir\ndstfile = $dstfile"}; 84 if ($srcfile =~ m/^\S+\.la$/) { 85 require LT::LaFile; 86 LT::LaFile->install($s, $dstdir, $ltprog, \@instopts, $install_opts{'s'}); 87 } elsif (-f "$srcdir/$ltdir/$srcfile" && is_wrapper($s)) { 88 require LT::Program; 89 LT::Program->install($s, $dst, $ltprog, \@instopts); 90 } else { 91 LT::Exec->install(@$ltprog, @instopts, $s, $dst); 92 } 93 } 94 if (defined $install_opts{d}) { 95 LT::Exec->install(@$ltprog, @instopts, @ARGV); 96 } 97} 98 99sub is_wrapper 100{ 101 my $program = shift; 102 103 open(my $pw, '<', $program) or die "Cannot open $program: $!\n"; 104 my $line = <$pw>; 105 # if the first line isn't a shell, don't even bother 106 return 0 unless $line =~ m/^\#\!/; 107 my $i = 0; 108 while (my $line = <$pw>) { 109 return 1 if $line =~ m/wrapper\sfor/; 110 last if $i++ > 10; 111 } 112 return 0; 113} 114 1151; 116