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
|
#!/usr/bin/env bash
#
# crossclang
# A helper script to simplify cross-compilation
#
# ldshim can fix some issues at link-time.
#
# Copyright 2019 Christian Kohlschütter
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
printHelp=0
linker=""
linkerArgs=()
searchPaths=()
replaceWithAbsolutePaths=()
sysroot=
while [ $# -gt 0 ]; do
v="$1"
shift
case "$v" in
-L* )
searchPaths+=("${v#-L}")
;;
dllcrt2.o|crt2.o|crtbegin.o|crtend.o|crtbeginS.o|crti.o|crtendS.o|crtn.o )
if [ ! -f "$v" ]; then
replaceWithAbsolutePaths+=(${#linkerArgs[@]})
fi
;;
--Xldshim-ld=* )
linker="${v#--Xldshim-ld=}"
continue
;;
esac
linkerArgs+=("$v")
done
if [ -z "$linker" ]; then
echo "No linker specified. Add -Wl,--Xldshim-ld=path/to/ld to your clang options" >&2
exit 1
fi
linker=("$linker")
linkerPath="$(PATH=/usr/bin:$PATH which ${linker[@]} 2>/dev/null | head -n 1)"
if [ ! -f "$linkerPath" ]; then
echo "Cannot find linker: ${linker[@]}" >&2
exit 1
fi
if [ $printHelp -eq 1 ]; then
cat <<EOT
Syntax: $0 [--help|-h]
-- <linker opts>...
EOT
exit 1
fi
for i in ${replaceWithAbsolutePaths[@]}; do
filename="${linkerArgs[$i]}"
for path in ${searchPaths[@]}; do
if [[ -f "$path/$filename" ]]; then
linkerArgs[$i]="$path/$filename"
break
fi
done
done
# set -x
exec "$linkerPath" ${linkerArgs[@]}
|