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