xref: /freebsd/contrib/ncurses/misc/shlib (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1#!/bin/sh
2##############################################################################
3# Copyright 2020 Thomas E. Dickey                                            #
4# Copyright 1998-2005,2007 Free Software Foundation, Inc.                    #
5#                                                                            #
6# Permission is hereby granted, free of charge, to any person obtaining a    #
7# copy of this software and associated documentation files (the "Software"), #
8# to deal in the Software without restriction, including without limitation  #
9# the rights to use, copy, modify, merge, publish, distribute, distribute    #
10# with modifications, sublicense, and/or sell copies of the Software, and to #
11# permit persons to whom the Software is furnished to do so, subject to the  #
12# following conditions:                                                      #
13#                                                                            #
14# The above copyright notice and this permission notice shall be included in #
15# all copies or substantial portions of the Software.                        #
16#                                                                            #
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
20# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
23# DEALINGS IN THE SOFTWARE.                                                  #
24#                                                                            #
25# Except as contained in this notice, the name(s) of the above copyright     #
26# holders shall not be used in advertising or otherwise to promote the sale, #
27# use or other dealings in this Software without prior written               #
28# authorization.                                                             #
29##############################################################################
30#
31# Author: Thomas E. Dickey <dickey@clark.net> 1996
32#
33# $Id: shlib,v 1.13 2020/02/02 23:34:34 tom Exp $
34# Use this script as a wrapper when running executables linked to shared
35# libraries on systems that use the $LD_LIBRARY_PATH variable and don't embed
36# the soname's path within the linked executable (such as IRIX), e.g,
37#
38#	shlib knight
39#
40# Setting LD_LIBRARY_PATH, overrides/supplements the loader's normal search
41# path, and works on most systems.  The drawback is that then the environment
42# variable has to be set to run the programs within this directory tree.
43#
44# For Linux (and other systems using the GNU loader), we can use the rpath
45# directive, which embeds the pathname of the library within the executable.
46# Using the Linux loader's rpath directive introduces a constraint, since
47# it's embedded into the binary, and means that the binary cannot be moved
48# around (though it'll work if the $exec_prefix convention that puts the bin
49# and lib directories under the same parent is followed).
50#
51# Using the actual soname (e.g., ../lib/libncurses.so) alone, is a more
52# flexible solution; you can link without having to set the environment
53# variable, and on some systems (IRIX) you can even run the resulting binaries
54# without setting LD_LIBRARY_PATH.
55#
56# Using a conventional link, with -L and -l options on Linux results in a
57# statically linked executable, which we don't want at all.
58#
59# Special cases:
60#
61#	BeOS R4.5 uses $LIBRARY_PATH rather than $LD_LIBRARY_PATH.
62#	Cygwin uses $PATH
63#	Mac OS X uses $DYLD_LIBRARY_PATH
64#
65# Other cases not handled by this script:
66#
67#	AIX uses $LIBPATH
68#	IRIX64 may use $LD_LIBRARY64_PATH or $LD_LIBRARYN32_PATH
69#	Solaris may use $LD_LIBRARY_PATH_64
70#
71CDPATH=
72#
73# Make sure that we use the PATH that was set in run_tic.sh
74if test -n "$SHLIB_PATH" ; then
75	PATH=$SHLIB_PATH
76	export PATH
77fi
78
79# Find the lib-directory for this build tree
80q=""
81for p in lib ../lib ../../lib ../../../lib
82do
83	if test -d $p; then
84		q=`cd $p; pwd`
85		break
86	elif test -f configure && test ! -d ../$p ; then
87		break
88	fi
89done
90
91# Set the environment variable.
92if test -n "$q" ; then
93	system=
94	if test -n "$SHLIB_HOST" ; then
95		system="$SHLIB_HOST"
96	elif test -n "$PATHEXT" ; then
97		system=cygwin
98	elif test -n "$LIBRARY_PATH" ; then
99		system=beos
100	elif test -n "$DYLD_LIBRARY_PATH" ; then
101		system=darwin
102	elif test -n "$LD_LIBRARY_PATH"; then
103		system=unix
104	else
105		for r in $q/*.*
106		do
107			if test -f "$r"
108			then
109				case $r in
110				*.dll)
111					system=cygwin
112					;;
113				*.dylib)
114					system=darwin
115					;;
116				esac
117			fi
118			test -n "$system" && break
119		done
120	fi
121
122	case .$system in
123	.cygwin*)
124		variable=PATH
125		;;
126	.beos*)
127		variable=LIBRARY_PATH
128		;;
129	.darwin*)
130		variable=DYLD_LIBRARY_PATH
131		;;
132	*)
133		variable=LD_LIBRARY_PATH
134		;;
135	esac
136
137	eval 'test -z "$'$variable'" && '$variable'=":"'
138	eval $variable'="$q:$'$variable'"'
139	eval 'export '$variable
140fi
141
142eval "$*"
143