#!/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 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[@]}