xref: /openbsd/usr.bin/libtool/LT/Program.pm (revision 4cfece93)
1# $OpenBSD: Program.pm,v 1.19 2012/07/13 11:56:12 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::Program;
23use File::Basename;
24use LT::Archive;
25use LT::Util;
26use LT::Trace;
27
28sub new
29{
30	my $class = shift;
31	bless {}, $class;
32}
33
34# write a wrapper script for an executable so it can be executed within
35# the build directory
36sub write_wrapper
37{
38	my $self = shift;
39
40	my $program = $self->{outfilepath};
41	my $pfile = basename($program);
42	my $realprogram = $ltdir . '/' . $pfile;
43	open(my $pw, '>', $program) or die "Cannot write $program: $!\n";
44	print $pw <<EOF
45#!/bin/sh
46
47# $program - wrapper for $realprogram
48# Generated by libtool $version
49
50argdir=`dirname \$0`
51if test -f "\$argdir/$realprogram"; then
52    # Add our own library path to LD_LIBRARY_PATH
53    LD_LIBRARY_PATH=\$argdir/$ltdir
54    export LD_LIBRARY_PATH
55
56    # Run the actual program with our arguments.
57    exec "\$argdir/$realprogram" \${1+"\$\@"}
58
59    echo "\$0: cannot exec $program \${1+"\$\@"}"
60    exit 1
61else
62    echo "\$0: error: \\\`\$argdir/$realprogram' does not exist." 1>&2
63    exit 1
64fi
65EOF
66;
67	close($pw);
68	chmod 0755, $program;
69}
70
71sub install
72{
73	my ($class, $src, $dst, $instprog, $instopts) = @_;
74
75	my $srcdir = dirname $src;
76	my $srcfile = basename $src;
77	my $realpath = "$srcdir/$ltdir/$srcfile";
78	LT::Exec->install(@$instprog, @$instopts, $realpath, $dst);
79}
80
811;
82