-
Notifications
You must be signed in to change notification settings - Fork 252
/
ld-wrapper.sh
83 lines (75 loc) · 2.19 KB
/
ld-wrapper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!@shell@
#
# # Usage
#
# ```
# $ @program@ [ARG ..]
# ```
#
# # Synopsis
#
# This program takes the place of
# @program@
# so that additional options can be added before invoking it. There are 2
# primary responsibilities for this program:
#
# 1. Ensure that correct dynamic linker is used, by setting a `-dynamic-linker`
# option
# 1. Add an `-rpath` option for each entry in the `$LD_RUN_PATH` environment
# variable, if it is set
#
# The idea and implementation which this is based upon is thanks to the nixpkgs
# project, specifically in the `cc-wrapper` package. For more details, see:
# https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/cc-wrapper/ld-wrapper.sh
#
# # Environment Variables
#
# There are several environment variables that are consumed by this program:
#
# * `$LD_RUN_PATH` (*Optional*): Each path entry in this variable will get a
# corresponding `-rpath $path` option added to the real program invocation
# * `$DEBUG` (*Optional*): If set, this program will output the original and
# extra flags added to standard error
#
#
# # Main program
# Fail whenever a command returns a non-zero exit code.
set -e
# Populate a `$params` variable with all arguments passed to this program.
params=("$@")
# Create an empty array for extra arguments.
before=()
after=()
# Add the dynamic linker.
before+=("-dynamic-linker @dynamic_linker@")
# Add `-rpath` switches.
#
# Sidenote: why the process substitution strangeness? Learn more:
# http://mywiki.wooledge.org/BashFAQ/024
# shellcheck disable=SC2086
while read -r path; do
if [ -n "$path" ]; then
after+=("-rpath $path")
fi
done < <(echo $LD_RUN_PATH | tr : '\n')
# Optionally print debug info.
if [ -n "$DEBUG" ]; then
echo "original flags to @program@:" >&2
for i in "${params[@]}"; do
echo " $i" >&2
done
echo "before flags to @program@:" >&2
# shellcheck disable=SC2068
for i in ${before[@]}; do
echo " $i" >&2
done
echo "after flags to @program@:" >&2
# shellcheck disable=SC2068
for i in ${after[@]}; do
echo " $i" >&2
done
fi
# Become the underlying real program
# Note: $before and $after must *not* be quoted
# shellcheck disable=SC2068
exec @program@ ${before[@]} "${params[@]}" ${after[@]}