| 1 |
#!/usr/bin/env bash |
| 2 |
# |
| 3 |
# crossclang |
| 4 |
# A helper script to simplify cross-compilation |
| 5 |
# |
| 6 |
# ldshim can fix some issues at link-time. |
| 7 |
# |
| 8 |
# Copyright 2019 Christian Kohlschütter |
| 9 |
# |
| 10 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 |
# you may not use this file except in compliance with the License. |
| 12 |
# You may obtain a copy of the License at |
| 13 |
# |
| 14 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
# |
| 16 |
# Unless required by applicable law or agreed to in writing, software |
| 17 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 18 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 |
# See the License for the specific language governing permissions and |
| 20 |
# limitations under the License. |
| 21 |
# |
| 22 |
|
| 23 |
printHelp=0 |
| 24 |
|
| 25 |
linker="" |
| 26 |
linkerArgs=() |
| 27 |
searchPaths=() |
| 28 |
replaceWithAbsolutePaths=() |
| 29 |
sysroot= |
| 30 |
|
| 31 |
while [ $# -gt 0 ]; do |
| 32 |
v="$1" |
| 33 |
shift |
| 34 |
|
| 35 |
case "$v" in |
| 36 |
-L* ) |
| 37 |
searchPaths+=("${v#-L}") |
| 38 |
;; |
| 39 |
dllcrt2.o|crt2.o|crtbegin.o|crtend.o|crtbeginS.o|crti.o|crtendS.o|crtn.o ) |
| 40 |
if [ ! -f "$v" ]; then |
| 41 |
replaceWithAbsolutePaths+=(${#linkerArgs[@]}) |
| 42 |
fi |
| 43 |
;; |
| 44 |
--Xldshim-ld=* ) |
| 45 |
linker="${v#--Xldshim-ld=}" |
| 46 |
continue |
| 47 |
;; |
| 48 |
esac |
| 49 |
|
| 50 |
linkerArgs+=("$v") |
| 51 |
done |
| 52 |
|
| 53 |
if [ -z "$linker" ]; then |
| 54 |
echo "No linker specified. Add -Wl,--Xldshim-ld=path/to/ld to your clang options" >&2 |
| 55 |
exit 1 |
| 56 |
fi |
| 57 |
|
| 58 |
linker=("$linker") |
| 59 |
linkerPath="$(PATH=/usr/bin:$PATH which ${linker[@]} 2>/dev/null | head -n 1)" |
| 60 |
if [ ! -f "$linkerPath" ]; then |
| 61 |
echo "Cannot find linker: ${linker[@]}" >&2 |
| 62 |
exit 1 |
| 63 |
fi |
| 64 |
|
| 65 |
if [ $printHelp -eq 1 ]; then |
| 66 |
cat <<EOT |
| 67 |
Syntax: $0 [--help|-h] |
| 68 |
-- <linker opts>... |
| 69 |
EOT |
| 70 |
exit 1 |
| 71 |
fi |
| 72 |
|
| 73 |
for i in ${replaceWithAbsolutePaths[@]}; do |
| 74 |
filename="${linkerArgs[$i]}" |
| 75 |
for path in ${searchPaths[@]}; do |
| 76 |
if [[ -f "$path/$filename" ]]; then |
| 77 |
linkerArgs[$i]="$path/$filename" |
| 78 |
break |
| 79 |
fi |
| 80 |
done |
| 81 |
done |
| 82 |
|
| 83 |
# set -x |
| 84 |
exec "$linkerPath" ${linkerArgs[@]} |