summaryrefslogtreecommitdiff
path: root/lang/rust/files/gcc-wrap
blob: f0faed53bf40d79a03e0c2f3c595b1412ceb298e (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#! /bin/sh

# Root of targets tools + dest directories
# or unset to default to a native build.

# This script assumes target tools in $root/tools 
# and target's destdir in $root/dest, the result of a NetBSD build.sh.
# ...or the native root, when we don't cross-compile
root=${CROSS_ROOT:-/}

native=false
if [ $root = "/" ]; then
	native=true
else
	# What's the tools/bin prefix (if we're cross-building)?
	gnuarch=${GNU_CROSS_TARGET:?}
fi

# Who are we a wrapper for? (Typically either gcc or c++)
who=$(basename $0 | sed -e 's/-wrap$//')

args=""

# May need to add $linkadd before first -l or fist -L
linkadd="-Wl,--sysroot=${root}/dest"
# (perhaps this is overly cautious, other adjustments we do
# below may be sufficient...)
# Lib directories to ensure we search and have in run-path
libs="/lib /usr/lib /usr/pkg/lib"

for d in $libs; do
	if ! $native; then
		linkadd="$linkadd -L=$d"
		linkadd="$linkadd -Wl,-rpath-link=${root}/dest/$d"
	fi
	# Run-path is for when we execute on the target,
	# so no $root prefix
	linkadd="$linkadd -Wl,-rpath,$d"
done

# ...and add a placeholder so we can tweak RPATH with chrpath,
# since chrpath can't extend the length of the run path
# (This may also not be needed, we use LD_LIBRARY_PATH instead)
placeholder="placeholder-$(date | openssl dgst -sha1 | \
	awk '{ print $2 }')"
linkadd="$linkadd -Wl,-rpath,/$placeholder"
# the / is a sneaky attempt to let it past cwrapper...

# More debugging
linkadd="$linkadd -Wl,--verbose"

linktweaked=false

# Step through args, tweak where required
set -- "$@"
while [ $# -gt 0 ]; do
	case "$1" in
# Insert = at the front of -isystem args.
# This is to get --sysroot prepended, so that
# we pick up the correct set of header files.
# (I thought this wasn't reqired, but apparently it is...)
		-isystem)
			shift
			args="$args -isystem =$1"
			;;
# Also doctor -I directives of known paths and
# redirect them to the --sysroot.
		-I/usr/include)
			args="$args -I=/usr/include"
			;;
		-I/usr/include/krb5)
			args="$args -I=/usr/include/krb5"
			;;
		-I/usr/pkg/include)
			args="$args -I=/usr/pkg/include"
			;;
		-I)
			if [ $2 = "/usr/include" ]; then
				args="$args -I=/usr/include"
				shift
			elif [ $2 = "/usr/include/krb5" ]; then
				args="$args -I=/usr/include/krb5"
				shift
			elif [ $2 = "/usr/pkg/include" ]; then
				args="$args -I=/usr/pkg/include"
				shift
			else
				args="$args -I"
			fi
			;;
		-l*)
			if ! $linktweaked; then
				args="$args $linkadd"
				linktweaked=true
			fi
			args="$args $1"
			;;
		-L)
			if ! $linktweaked; then
				args="$args $linkadd"
				linktweaked=true
			fi
			shift
			tweaked=false
			# redirect these to -Wl,--sysroot
			for d in /lib /usr/lib /usr/pkg/lib; do
				if [ $1 = $d ]; then
					args="$args -L =$d"
					tweaked=true
				fi
			done
			# Not redirected?  If so we need to add
			if ! $tweaked; then
				args="$args -L $1"
			fi
			;;
			
		-L/lib)
			if ! $linktweaked; then
				args="$args $linkadd"
				linktweaked=true
			fi
			args="$args -L=/lib"
			;;
		-L/usr/lib)
			if ! $linktweaked; then
				args="$args $linkadd"
				linktweaked=true
			fi
			args="$args -L=/usr/lib"
			;;
		-L/usr/pkg/lib)
			if ! $linktweaked; then
				args="$args $linkadd"
				linktweaked=true
			fi
			args="$args -L=/usr/pkg/lib"
			;;
		*)
			args="$args $1"
			;;
	esac
	shift
done

if $native; then
	# Try to avoid cwrappers, which does "undocumented magic"
	# by invoking the compiler "directly".
	cmd="/usr/bin/${who} $args"
else
	cmd="${root}/tools/bin/${gnuarch}-${who} \
		--sysroot=${root}/dest \
		$args"
fi

# Cannot echo to stdout, messes up e.g. "gcc -print-prog-name=ld" output...
#echo $cmd >&2
exec $cmd