| 1 |
#!/usr/bin/env bash |
| 2 |
# |
| 3 |
# crossclang |
| 4 |
# A helper script to simplify cross-compilation |
| 5 |
# |
| 6 |
# Copyright 2019 Christian Kohlschütter |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
|
| 21 |
# if you don't specify a -target, or specify -target current, we infer the name of the current target, |
| 22 |
# and look up for our configuration |
| 23 |
# if you specify -target default, the default clang compiler will be used |
| 24 |
# if you specify -target and some other value, we check the available targets in target-sdks |
| 25 |
# and add custom options to clang to simplify cross-compilation |
| 26 |
# if you specify an invalid value to -target, the set of available targets is shown as an error message |
| 27 |
|
| 28 |
## debugging |
| 29 |
# echo PWD $(pwd) |
| 30 |
# echo CROSSCLANG $0 $@ |
| 31 |
# set -x |
| 32 |
|
| 33 |
# Make sure to include all paths that may contain custom linkers etc. |
| 34 |
PATH="$PATH":/usr/bin:/opt/crossclang/local/bin:/opt/homebrew/bin:/usr/local/bin |
| 35 |
export PATH |
| 36 |
|
| 37 |
currentDir=$(cd "$(dirname $0)"; pwd) |
| 38 |
|
| 39 |
gccMode=0 |
| 40 |
compiler=clang |
| 41 |
basename=$(basename $0) |
| 42 |
if [ $basename == "gcc" ]; then |
| 43 |
gccMode=2 |
| 44 |
compiler="gcc" |
| 45 |
fi |
| 46 |
|
| 47 |
clangArgs=() |
| 48 |
gccArgs=() |
| 49 |
libsArgs=() |
| 50 |
target="" |
| 51 |
sdkroot= |
| 52 |
sdkroot_base="" |
| 53 |
|
| 54 |
needLinker=1 |
| 55 |
isShared=0 |
| 56 |
nostdlib=0 |
| 57 |
|
| 58 |
hasShared=0 |
| 59 |
defaultFallback=0 |
| 60 |
looksLikeCPP=0 |
| 61 |
|
| 62 |
# parse arguments |
| 63 |
previous="" |
| 64 |
|
| 65 |
hasArgs=$# |
| 66 |
hasFiles=0 |
| 67 |
withAndWithoutLC=0 |
| 68 |
hasO=0 |
| 69 |
outputFile= |
| 70 |
useLdShim=0 |
| 71 |
outputStripLibPrefix=0 |
| 72 |
serializeDiagnosticsOut= |
| 73 |
|
| 74 |
llvmName=llvm |
| 75 |
fileList= |
| 76 |
# ld64_sdk_version=10.9 |
| 77 |
ld64_sdk_version= |
| 78 |
hideUnknownWarningWarnings=0 |
| 79 |
|
| 80 |
hasExportDynamic=0 |
| 81 |
ignoreExportDynamic=0 |
| 82 |
|
| 83 |
skipGccArg=0 |
| 84 |
analyze=0 |
| 85 |
|
| 86 |
extraVersionArgs=() |
| 87 |
|
| 88 |
while [ $# -gt 0 ]; do |
| 89 |
v="$1" |
| 90 |
shift |
| 91 |
|
| 92 |
skipArg=0 |
| 93 |
|
| 94 |
case "$previous" in |
| 95 |
-isdkroot ) sdkroot="$v" ;; |
| 96 |
-o ) outputFile="$v" ; skipArg=1 ;; |
| 97 |
--serialize-diagnostics ) serializeDiagnosticsOut="$v" ;; |
| 98 |
esac |
| 99 |
|
| 100 |
case "$v" in |
| 101 |
-E ) needLinker=0 ;; |
| 102 |
-fsyntax-only ) needLinker=0 ;; |
| 103 |
-c ) needLinker=0 ;; |
| 104 |
-S ) needLinker=0 ;; |
| 105 |
-bundle ) isShared=1 ; skipArg=1 ;; |
| 106 |
-dynamic ) isShared=1 ;; |
| 107 |
-dynamiclib ) isShared=1 ;; |
| 108 |
-shared ) isShared=1; hasShared=1 ;; |
| 109 |
-nostdlib ) nostdlib=1 ;; |
| 110 |
-Xcrossclang-with-and-without-lc ) withAndWithoutLC=1; skipArg=1 ;; |
| 111 |
-Xcrossclang-llvm-name ) llvmName="$1"; shift ; skipArg=1 ;; |
| 112 |
-Xcrossclang-use-ldshim ) useLdShim=1 ; skipArg=1 ;; |
| 113 |
-Xcrossclang-use-gcc=* ) gccMode=1 ; compiler="${v#-Xcrossclang-use-gcc=}" ; skipArg=1 ;; |
| 114 |
-Xcrossclang-output-strip-lib-prefix ) outputStripLibPrefix=1 ; skipArg=1 ;; |
| 115 |
-Xcrossclang-ld64-sdk-version ) ld64_sdk_version="$1"; shift ; skipArg=1 ;; |
| 116 |
-Xcrossclang-hide-unknown-warning-warnings ) hideUnknownWarningWarnings=1 ; skipArg=1 ;; |
| 117 |
-Xcrossclang-ignore-export-dynamic) ignoreExportDynamic=1 ; skipArg=1 ;; |
| 118 |
# -index-store-path ) shift ; skipArg=1 ;; # if you're hitting this, set "Enable Index-While-Building Functionality=No" in Xcode build settings instead |
| 119 |
-filelist ) fileList="$1" ; shift; skipArg=1 ;; |
| 120 |
-current_version ) shift ; skipArg=1 ;; |
| 121 |
-compatibility_version ) shift ; skipArg=1 ;; |
| 122 |
-o ) hasO=1; skipArg=1 ;; |
| 123 |
-Weverything ) skipGccArg=1 ;; |
| 124 |
-Wcomma ) skipGccArg=1 ;; |
| 125 |
-Wblock-capture-autoreleasing ) skipGccArg=1 ;; |
| 126 |
-Winfinite-recursion ) skipGccArg=1 ;; |
| 127 |
-Wshorten-64-to-32 ) skipGccArg=1 ;; |
| 128 |
-Wobjc-literal-conversion ) skipGccArg=1 ;; |
| 129 |
-Wnon-literal-null-conversion ) skipGccArg=1 ;; |
| 130 |
-Wno-c++11-extensions ) skipGccArg=1 ;; |
| 131 |
-Wno-declaration-after-statement ) skipGccArg=1 ;; |
| 132 |
-Wbool-conversion ) skipGccArg=1 ;; |
| 133 |
-Wconstant-conversion ) skipGccArg=1 ;; |
| 134 |
-Wquoted-include-in-framework-header ) skipGccArg=1 ;; |
| 135 |
-fno-c++-static-destructors ) skipGccArg=1 ;; |
| 136 |
-fmacro-backtrace-limit=0 ) skipGccArg=1 ;; |
| 137 |
-fdiagnostics-show-note-include-stack ) skipGccArg=1 ;; |
| 138 |
-Xclang ) skipGccArg=2 ;; # has an argument |
| 139 |
--analyze ) analyze=1 ;; |
| 140 |
-install_name ) skipGccArg=2 ;; # has an argument |
| 141 |
-iquote ) skipGccArg=2 ;; # has an argument |
| 142 |
-iframework ) skipGccArg=2 ;; # has an argument |
| 143 |
--serialize-diagnostics ) skipGccArg=2 ;; # has an argument |
| 144 |
*.cc ) looksLikeCPP=1 ;; |
| 145 |
*.cpp ) looksLikeCPP=1 ;; |
| 146 |
*.cxx ) looksLikeCPP=1 ;; |
| 147 |
*.c++ ) looksLikeCPP=1 ;; |
| 148 |
*.hh ) looksLikeCPP=1 ;; |
| 149 |
*.hpp ) looksLikeCPP=1 ;; |
| 150 |
*.mm ) looksLikeCPP=1 ;; |
| 151 |
-l* ) libsArgs+=("$v") ; skipArg=1 ;; # for compatibility, libs will be added last |
| 152 |
-target ) v="$1"; target="$v"; shift; skipArg=1 ;; |
| 153 |
c++ ) if [ "$previous" == "-x" ]; then looksLikeCPP=1; fi ;; |
| 154 |
-m64 ) extraVersionArgs+=("$v") ;; |
| 155 |
-Xlinker ) |
| 156 |
if [[ "$1" == "-export_dynamic" ]]; then |
| 157 |
hasExportDynamic=1 |
| 158 |
elif [[ -n "$1" ]]; then |
| 159 |
clangArgs+=("-Xlinker" "$1") |
| 160 |
gccArgs+=("-Xlinker" "$1") |
| 161 |
fi |
| 162 |
shift |
| 163 |
skipArg=1 |
| 164 |
;; |
| 165 |
- ) hasFiles=1 ;; |
| 166 |
esac |
| 167 |
|
| 168 |
if [[ $hasFiles -eq 0 && -n "$v" && "${v:0:1}" != "-" && -e "$v" ]]; then |
| 169 |
hasFiles=1 |
| 170 |
fi |
| 171 |
|
| 172 |
if [[ "$skipArg" -eq 0 ]]; then |
| 173 |
clangArgs+=("$v") |
| 174 |
if [[ "$skipGccArg" -le 0 ]]; then |
| 175 |
gccArgs+=("$v") |
| 176 |
else |
| 177 |
skipGccArg=$(( $skipGccArg - 1 )) |
| 178 |
fi |
| 179 |
fi |
| 180 |
|
| 181 |
previous="$v" |
| 182 |
done |
| 183 |
|
| 184 |
if [[ $hasExportDynamic -eq 1 && $ignoreExportDynamic -eq 0 ]]; then |
| 185 |
clangArgs+=("-Xlinker" "-export_dynamic") |
| 186 |
gccArgs+=("-Xlinker" "-export_dynamic") |
| 187 |
fi |
| 188 |
|
| 189 |
if [[ $analyze -gt 0 && $gccMode -gt 0 ]]; then |
| 190 |
# When using the static code analyzer, switch back to clang |
| 191 |
compiler=clang |
| 192 |
gccMode=0 |
| 193 |
fi |
| 194 |
|
| 195 |
if [[ $gccMode -eq 2 ]]; then |
| 196 |
case "$target" in |
| 197 |
""|default|unspecified) |
| 198 |
;; |
| 199 |
*) |
| 200 |
compiler=clang |
| 201 |
gccMode=0 |
| 202 |
esac |
| 203 |
fi |
| 204 |
|
| 205 |
if [[ $gccMode -gt 0 ]]; then |
| 206 |
if [[ -n "$serializeDiagnosticsOut" ]]; then |
| 207 |
# Create fake diagnostics file |
| 208 |
xxd -r -ps -- >"$serializeDiagnosticsOut" <<EOT |
| 209 |
4449414701080000300000000701b240b44239d043383c20812d94833ccc |
| 210 |
433abc833b1c04886280407110240b0429a443389cc3432290423a84c339 |
| 211 |
a4823b98c33b3c24c32cc8c338c84238b8c33994c303528c4238d0832b84 |
| 212 |
433b94c3434290423a84c33998023b84c3393c248629a4033b94832b8443 |
| 213 |
3b94c3837198423ae0432ad0c34190a80ac810255008140285285104834a |
| 214 |
16080c82d47440944021500814a2040a8142a09024102530a8a681288142 |
| 215 |
a01018d4f540944021500814a2040a8142a0101814000000210c00000200 |
| 216 |
00001400000000000000 |
| 217 |
EOT |
| 218 |
fi |
| 219 |
fi |
| 220 |
|
| 221 |
clangDir= |
| 222 |
whichComp=$(which "$compiler" 2>/dev/null) |
| 223 |
[[ -n "$whichComp" ]] && clangDir=$(dirname "$whichComp") |
| 224 |
[[ -n "$clangDir" ]] && clangDir=$(cd "$clangDir"; pwd) |
| 225 |
[[ "$currentDir" == "$clangDir" ]] && clangDir= |
| 226 |
|
| 227 |
if [[ -z "$clangDir" && $gccMode -eq 0 ]]; then |
| 228 |
if [[ "$target" == "default" || -z "$target" ]]; then |
| 229 |
clangDir= |
| 230 |
comp=$(which gcc) |
| 231 |
[[ -n "$comp" ]] && clangDir=$(dirname "$comp") && clangDir=$(cd "$clangDir" && pwd) |
| 232 |
if [ -n "$clangDir" ]; then |
| 233 |
echo "Warning: No clang found, trying native gcc: $comp" >&2 |
| 234 |
gccMode=1 |
| 235 |
compiler="$comp" |
| 236 |
fi |
| 237 |
fi |
| 238 |
fi |
| 239 |
|
| 240 |
findOtherClang=0 |
| 241 |
if [[ "$currentDir" == "$clangDir" || -z "$clangDir" ]]; then |
| 242 |
# looks like we're in the PATH |
| 243 |
findOtherClang=1 |
| 244 |
fi |
| 245 |
|
| 246 |
llvmPath= |
| 247 |
if [[ -z "$(which llvm-ar 2>/dev/null)" || $findOtherClang -gt 0 ]]; then |
| 248 |
# let's add the latest LLVM to the path |
| 249 |
latestLLVM=$(shopt -u failglob ; for f in {/usr/local,/opt/homebrew}/Cellar/"$llvmName"/*/bin; do [[ -d "$f" ]] && echo $f; done | sort -r -n | head -n 1) |
| 250 |
if [[ -n "$latestLLVM" && -d "$latestLLVM" ]]; then |
| 251 |
llvmPath="$latestLLVM" |
| 252 |
fi |
| 253 |
fi |
| 254 |
|
| 255 |
if [ -n "$llvmPath" ]; then |
| 256 |
export PATH="$llvmPath:/usr/bin:/usr/local/bin:$PATH" |
| 257 |
else |
| 258 |
export PATH="/usr/bin:/usr/local/bin:$PATH" |
| 259 |
fi |
| 260 |
|
| 261 |
if [[ $gccMode -gt 0 ]]; then |
| 262 |
clangArgs=( ${gccArgs[@]} ) |
| 263 |
fi |
| 264 |
|
| 265 |
whichComp=$(which $compiler) |
| 266 |
if [[ -z "$whichComp" ]]; then |
| 267 |
echo "crossclang could not find compiler $compiler in path $PATH, giving up" >&2 |
| 268 |
exit 1 |
| 269 |
fi |
| 270 |
|
| 271 |
clangDir=$(cd $(dirname "$whichComp"); pwd) |
| 272 |
if [ "$currentDir" == "$clangDir" ]; then |
| 273 |
echo "crossclang detected in PATH. Can't find real clang, giving up" >&2 |
| 274 |
exit 1 |
| 275 |
fi |
| 276 |
|
| 277 |
output_args=() |
| 278 |
clangLinkerArgs=() |
| 279 |
isystem_decls=() |
| 280 |
|
| 281 |
isCompile=0 |
| 282 |
[[ "$outputFile" == *.o ]] && isCompile=1 |
| 283 |
|
| 284 |
if [ $hasO -gt 0 ]; then |
| 285 |
if [[ $outputStripLibPrefix -eq 1 ]]; then |
| 286 |
outputFile="${outputFile#lib}" |
| 287 |
fi |
| 288 |
output_args+=("-o" "$outputFile") |
| 289 |
fi |
| 290 |
|
| 291 |
if [ $hasFiles -eq 0 ]; then |
| 292 |
needLinker=0 |
| 293 |
fi |
| 294 |
|
| 295 |
|
| 296 |
if [ "$target" == "default" ]; then |
| 297 |
defaultFallback=1 |
| 298 |
if [[ $gccMode -gt 0 ]]; then |
| 299 |
target=unspecified |
| 300 |
else |
| 301 |
target="" |
| 302 |
fi |
| 303 |
elif [ "$target" == "current" ]; then |
| 304 |
defaultFallback=2 |
| 305 |
target= |
| 306 |
elif [[ -z "$target" && $gccMode -gt 0 ]]; then |
| 307 |
target=unspecified |
| 308 |
fi |
| 309 |
|
| 310 |
if [[ "$target" != "unspecified" ]]; then |
| 311 |
currentTarget= |
| 312 |
if [[ -z "$target" ]]; then |
| 313 |
if [ $defaultFallback -eq 0 ]; then |
| 314 |
PATH=/usr/bin:$PATH |
| 315 |
fi |
| 316 |
|
| 317 |
target=$($(which $compiler) --version ${extraVersionArgs[@]} | grep "^Target: " | head -n 1) |
| 318 |
target=${target##* } |
| 319 |
if [ -z "$target" ]; then |
| 320 |
echo "Could not determine target" >&2 |
| 321 |
exit 1 |
| 322 |
fi |
| 323 |
currentTarget="$target" |
| 324 |
|
| 325 |
if [ $defaultFallback -eq 1 ]; then |
| 326 |
if [[ -n "$fileList" ]]; then |
| 327 |
clangArgs+=("-filelist" "$fileList") |
| 328 |
fi |
| 329 |
|
| 330 |
exec clang ${output_args[@]} ${clangArgs[@]} |
| 331 |
fi |
| 332 |
fi |
| 333 |
|
| 334 |
sdkroot_bases=() |
| 335 |
if [ -n "$sdkroot_base" ]; then |
| 336 |
sdkroot_bases+=("$sdkroot_base") |
| 337 |
fi |
| 338 |
sdkroot_bases+=( |
| 339 |
"$(dirname $0)/../target-sdks" |
| 340 |
"$HOME/.crossclang/target-sdks" |
| 341 |
"/opt/crossclang/target-sdks" |
| 342 |
) |
| 343 |
|
| 344 |
if [[ "$target" == "/"* ]]; then |
| 345 |
sdkroot="$target" |
| 346 |
else |
| 347 |
for base in ${sdkroot_bases[@]}; do |
| 348 |
if [[ -n "$base" && -f "$base/$target/target.conf" ]]; then |
| 349 |
sdkroot_base="$base" |
| 350 |
break |
| 351 |
fi |
| 352 |
done |
| 353 |
fi |
| 354 |
|
| 355 |
if [ -z "$sdkroot" ]; then |
| 356 |
if [[ -n "$sdkroot_base" && -n "$target" ]]; then |
| 357 |
sdkroot="$sdkroot_base/$target" |
| 358 |
|
| 359 |
sdkrootFiles=$(ls "$sdkroot" | grep -v target) |
| 360 |
if [[ -z "$sdkrootFiles" ]]; then |
| 361 |
echo "Warning: crossclang has an incomplete configuration for target \"$target\" -- you need to run sync-target-sdk first!" >&2 |
| 362 |
fi |
| 363 |
elif [[ "$defaultFallback" -eq 0 ]]; then |
| 364 |
platformTarget=$($(which $compiler) --version ${extraVersionArgs[@]} | grep "^Target: " | head -n 1) |
| 365 |
platformTarget=${platformTarget##* } |
| 366 |
|
| 367 |
tryPlatformClang=0 |
| 368 |
case "$target" in |
| 369 |
arm64-apple-macos11) |
| 370 |
case "$platformTarget" in |
| 371 |
arm64-apple-darwin*) |
| 372 |
tryPlatformClang=1 |
| 373 |
;; |
| 374 |
esac |
| 375 |
;; |
| 376 |
x86_64-apple-macos11) |
| 377 |
case "$platformTarget" in |
| 378 |
arm64-apple-darwin*|x86_64-apple-darwin2*) |
| 379 |
tryPlatformClang=1 |
| 380 |
;; |
| 381 |
esac |
| 382 |
;; |
| 383 |
x86_64-apple-macos) |
| 384 |
case "$platformTarget" in |
| 385 |
arm64-apple-darwin*|x86_64-apple-darwin*) |
| 386 |
tryPlatformClang=1 |
| 387 |
;; |
| 388 |
esac |
| 389 |
;; |
| 390 |
esac |
| 391 |
|
| 392 |
if [[ $tryPlatformClang -eq 1 ]]; then |
| 393 |
export PATH=/usr/bin:$PATH |
| 394 |
if [[ -n "$fileList" ]]; then |
| 395 |
clangArgs+=("-filelist" "$fileList") |
| 396 |
fi |
| 397 |
if ! (clang -v 2>&1 | grep -q "^Target: $target\$"); then |
| 398 |
echo "Warning: crossclang not configured for target \"$target\" -- you need to run prepare-target-sdk first. Trying native clang now." >&2 |
| 399 |
fi |
| 400 |
exec clang ${output_args[@]} ${clangArgs[@]} -target "$target" |
| 401 |
fi |
| 402 |
|
| 403 |
echo "Error: crossclang not configured for target \"$target\" -- you need to run prepare-target-sdk first!" >&2 |
| 404 |
elif [[ "$defaultFallback" -eq 2 ]]; then |
| 405 |
export PATH=/usr/bin:$PATH |
| 406 |
if [[ -n "$fileList" ]]; then |
| 407 |
clangArgs+=("-filelist" "$fileList") |
| 408 |
fi |
| 409 |
if ! (clang -v 2>&1 | grep -q "^Target: $target\$"); then |
| 410 |
echo "Warning: crossclang not configured for target \"$target\" -- you need to run prepare-target-sdk first. Trying native clang now." >&2 |
| 411 |
fi |
| 412 |
|
| 413 |
exec clang ${output_args[@]} ${clangArgs[@]} -target "$target" |
| 414 |
fi |
| 415 |
fi |
| 416 |
|
| 417 |
sdkrootOrig="$sdkroot" |
| 418 |
sdkroot=$(cd "$sdkroot" 2>/dev/null && pwd) |
| 419 |
targetConf="$sdkroot/target.conf" |
| 420 |
|
| 421 |
target_include_path=() |
| 422 |
target_library_path=() |
| 423 |
target_framework_path=() |
| 424 |
|
| 425 |
if [[ $? -ne 0 || -z "$sdkrootOrig" || ! -f "$targetConf" ]]; then |
| 426 |
if [[ "$target" != "$currentTarget" ]]; then |
| 427 |
echo "Could not find SDK root for target: $target" >&2 |
| 428 |
echo "Available targets:$(for f in $(find "${sdkroot_bases[@]}" -mindepth 1 -maxdepth 1 2>/dev/null); do if [ -f "$f/target.conf" ]; then echo -n $'\n'"- $(basename $f)"; fi done)" >&2 |
| 429 |
echo "- current (use current target)" >&2 |
| 430 |
echo "- default (use default clang)" >&2 |
| 431 |
exit 1 |
| 432 |
fi |
| 433 |
|
| 434 |
# build for current architecture |
| 435 |
sdkroot= |
| 436 |
target_triple="$target" |
| 437 |
target_linker=ld |
| 438 |
if [ -n "$(ld -help 2>/dev/null| grep ld64)" ]; then |
| 439 |
target_linker=ld64 |
| 440 |
fi |
| 441 |
else |
| 442 |
target=$(cd "$sdkroot"; basename "$(pwd)" ) |
| 443 |
sdkroot=$(cd "$sdkroot"; pwd ) |
| 444 |
source "$targetConf" |
| 445 |
fi |
| 446 |
|
| 447 |
triple="$target" |
| 448 |
if [ -n "$target_triple" ]; then |
| 449 |
triple="$target_triple" |
| 450 |
if [[ $gccMode -gt 0 ]]; then |
| 451 |
# do not add -target |
| 452 |
true |
| 453 |
else |
| 454 |
clangArgs+=("-target" "$target_triple") |
| 455 |
fi |
| 456 |
fi |
| 457 |
|
| 458 |
if [ -n "$sdkroot" ]; then |
| 459 |
isystem_decls+=("--sysroot" "$sdkroot") |
| 460 |
isystem_decls+=("-isysroot" "$sdkroot") |
| 461 |
fi |
| 462 |
|
| 463 |
for include in ${target_include_cpp_path[@]}; do |
| 464 |
if [[ $gccMode -gt 0 ]]; then |
| 465 |
isystem_decls+=("-isystem" "$sdkroot/$include") |
| 466 |
else |
| 467 |
isystem_decls+=("-cxx-isystem" "$sdkroot/$include") |
| 468 |
if [ $looksLikeCPP -gt 0 ]; then |
| 469 |
# NOTE: working around an issue where -cxx-isystem is ignored |
| 470 |
isystem_decls+=("-isystem" "$sdkroot/$include") |
| 471 |
fi |
| 472 |
fi |
| 473 |
done |
| 474 |
for include in ${target_include_path[@]}; do |
| 475 |
isystem_decls+=("-isystem" "$sdkroot/$include") |
| 476 |
done |
| 477 |
|
| 478 |
for path in ${target_framework_path[@]}; do |
| 479 |
isystem_decls+=("-iframeworkwithsysroot" "$path") |
| 480 |
done |
| 481 |
|
| 482 |
if [ -f "$sdkroot/target.h" ]; then |
| 483 |
isystem_decls+=("-imacros" "$sdkroot/target.h") |
| 484 |
fi |
| 485 |
|
| 486 |
if [ $hasFiles -eq 0 ]; then |
| 487 |
isystem_decls=() |
| 488 |
fi |
| 489 |
|
| 490 |
fi |
| 491 |
|
| 492 |
if [ $hasArgs -eq 0 ]; then |
| 493 |
needLinker=0 |
| 494 |
fi |
| 495 |
|
| 496 |
if [ $needLinker -gt 0 ]; then |
| 497 |
ldPath="" |
| 498 |
|
| 499 |
if [ -z "$target_linker" ]; then |
| 500 |
target_linker=ld |
| 501 |
fi |
| 502 |
|
| 503 |
case "$target_linker" in |
| 504 |
ld) |
| 505 |
ldPath=$(which ld.lld) |
| 506 |
|
| 507 |
if [ $nostdlib -gt 0 ]; then |
| 508 |
clangLinkerArgs+=("-fPIC") |
| 509 |
fi |
| 510 |
|
| 511 |
if [ $isShared -gt 0 ]; then |
| 512 |
if [[ $hasShared -eq 0 ]]; then |
| 513 |
clangLinkerArgs+=("-shared") |
| 514 |
fi |
| 515 |
fi |
| 516 |
|
| 517 |
if [ -n "$sdkroot" ]; then |
| 518 |
clangLinkerArgs+=("-Wl,--sysroot=$sdkroot") |
| 519 |
for path in ${target_library_path[@]}; do |
| 520 |
if [ -d "$sdkroot/$path" ]; then |
| 521 |
clangLinkerArgs+=("-Wl,-L$sdkroot/$path") |
| 522 |
fi |
| 523 |
done |
| 524 |
fi |
| 525 |
|
| 526 |
;; |
| 527 |
ld64) |
| 528 |
ldPath=$currentDir/ld64shim |
| 529 |
if [[ -n "$ld64_sdk_version" ]]; then |
| 530 |
clangLinkerArgs+=("-Wl,-sdk_version,$ld64_sdk_version") |
| 531 |
fi |
| 532 |
|
| 533 |
#if [ $nostdlib -gt 0 ]; then |
| 534 |
# clangLinkerArgs+=("-Wl,-undefined,dynamic_lookup") |
| 535 |
#fi |
| 536 |
|
| 537 |
if [ -n "$sdkroot" ]; then |
| 538 |
clangLinkerArgs+=("-Wl,-Z,-syslibroot,$sdkroot") |
| 539 |
|
| 540 |
for path in ${target_library_path[@]}; do |
| 541 |
if [ -d "$sdkroot/$path" ]; then |
| 542 |
clangLinkerArgs+=("-Wl,-L$path") |
| 543 |
fi |
| 544 |
done |
| 545 |
for path in ${target_framework_path[@]}; do |
| 546 |
if [ -d "$sdkroot/$path" ]; then |
| 547 |
clangLinkerArgs+=("-Wl,-F$path") |
| 548 |
fi |
| 549 |
done |
| 550 |
fi |
| 551 |
;; |
| 552 |
"") |
| 553 |
# No linker specified |
| 554 |
if [ $gccMode -gt 0 ]; then |
| 555 |
# OK |
| 556 |
true |
| 557 |
else |
| 558 |
echo "Warning: No target linker specified, trying default" >&2 |
| 559 |
fi |
| 560 |
|
| 561 |
if [ $isShared -gt 0 ]; then |
| 562 |
if [[ $hasShared -eq 0 ]]; then |
| 563 |
clangLinkerArgs+=("-shared") |
| 564 |
fi |
| 565 |
fi |
| 566 |
|
| 567 |
;; |
| 568 |
*) |
| 569 |
echo "Warning: Unknown target linker: $target_linker" >&2 |
| 570 |
;; |
| 571 |
esac |
| 572 |
|
| 573 |
if [[ -n "$target_linker_binary" ]]; then |
| 574 |
ldPath=$(which "$target_linker_binary") |
| 575 |
if [[ -z "$ldPath" ]]; then |
| 576 |
echo "Error: Could not find custom target linker in path: $target_linker_binary" >&2 |
| 577 |
exit 1 |
| 578 |
fi |
| 579 |
fi |
| 580 |
|
| 581 |
if [[ $gccMode -gt 0 ]]; then |
| 582 |
# ignore -fuse-ld |
| 583 |
true |
| 584 |
else |
| 585 |
if [[ -n "$ldPath" && -f "$ldPath" ]]; then |
| 586 |
if [ "$useLdShim" -eq 1 ]; then |
| 587 |
ldPath="$currentDir/ldshim" |
| 588 |
fi |
| 589 |
clangLinkerArgs+=("-fuse-ld=$ldPath") |
| 590 |
fi |
| 591 |
fi |
| 592 |
fi |
| 593 |
|
| 594 |
if [[ -n "$fileList" ]]; then |
| 595 |
if [[ "$target_linker" == "ld64" ]]; then |
| 596 |
clangLinkerArgs+=("-filelist" "$fileList") |
| 597 |
else |
| 598 |
clangLinkerArgs+=($(cat "$fileList")) |
| 599 |
fi |
| 600 |
fi |
| 601 |
|
| 602 |
if [[ "$triple" == *"-w64-"* ]]; then |
| 603 |
clangArgsNew=() |
| 604 |
for arg in ${clangArgs[@]}; do |
| 605 |
case "$arg" in |
| 606 |
-fpic|-fPIC|-fpie|-fPIE) |
| 607 |
# just ignore these |
| 608 |
continue |
| 609 |
;; |
| 610 |
esac |
| 611 |
clangArgsNew+=("$arg") |
| 612 |
done |
| 613 |
clangArgs=(${clangArgsNew[@]}) |
| 614 |
fi |
| 615 |
if [[ $gccMode -gt 0 ]]; then |
| 616 |
clangArgsNew=() |
| 617 |
for arg in ${clangArgs[@]}; do |
| 618 |
case "$arg" in |
| 619 |
-bundle) |
| 620 |
# just ignore these |
| 621 |
continue |
| 622 |
;; |
| 623 |
esac |
| 624 |
clangArgsNew+=("$arg") |
| 625 |
done |
| 626 |
clangArgs=(${clangArgsNew[@]}) |
| 627 |
fi |
| 628 |
|
| 629 |
|
| 630 |
compilerBinary="$(which $compiler)" |
| 631 |
if [ -z "$compilerBinary" ]; then |
| 632 |
echo "Cannot compile -- compiler not found: $compiler" >&2 |
| 633 |
exit 1 |
| 634 |
fi |
| 635 |
|
| 636 |
# set -x |
| 637 |
if [[ $withAndWithoutLC -eq 1 ]]; then |
| 638 |
nodepsOut="${outputFile%.*}.nodeps.${outputFile##*.}" |
| 639 |
|
| 640 |
wwoArgs=(${output_args[@]}) |
| 641 |
wwoArgsNoDeps=(-D__CROSSCLANG_NODEPS__=1 -o "$nodepsOut") |
| 642 |
if [ $isCompile -eq 0 ]; then |
| 643 |
# linking phase |
| 644 |
wwoArgs+=(-lc) |
| 645 |
wwoArgsNoDeps+=(-nostdlib) |
| 646 |
fi |
| 647 |
clangArgsNoDeps=() |
| 648 |
for arg in ${clangArgs[@]}; do |
| 649 |
if [[ $arg != -* && $arg == *.o ]]; then |
| 650 |
arg="${arg%.*}.nodeps.${arg##*.}" |
| 651 |
fi |
| 652 |
clangArgsNoDeps+=($arg) |
| 653 |
done |
| 654 |
|
| 655 |
"$compilerBinary" ${isystem_decls[@]} ${clangLinkerArgs[@]} ${wwoArgs[@]} ${clangArgs[@]} ${libsArgs[@]} && \ |
| 656 |
"$compilerBinary" ${isystem_decls[@]} ${clangLinkerArgs[@]} ${wwoArgsNoDeps[@]} ${clangArgsNoDeps[@]} ${libsArgs[@]} |
| 657 |
elif [[ $hideUnknownWarningWarnings -eq 1 ]]; then |
| 658 |
exec "$compilerBinary" ${isystem_decls[@]} ${clangLinkerArgs[@]} ${output_args[@]} ${clangArgs[@]} ${libsArgs[@]} 2> >(grep -v -E "unrecognized command-line option .* may have been intended to silence") |
| 659 |
else |
| 660 |
exec "$compilerBinary" ${isystem_decls[@]} ${clangLinkerArgs[@]} ${output_args[@]} ${clangArgs[@]} ${libsArgs[@]} |
| 661 |
fi |