cross-rs / cross-toolchains

Additional Dockerfiles and crosstool-ng config files to build additional toolchains.
Apache License 2.0
88 stars 16 forks source link

aarch64-apple-ios image build failed due to libdispatch/libblocksruntime #49

Closed cavivie closed 7 months ago

cavivie commented 7 months ago

The error log:

...
271.5 checking for __cxa_demangle in -lc++abi... no
271.6 checking for __cxa_demangle in -lc++... no
271.6 checking for __cxa_demangle in -lsupc++... yes
271.7 checking for __gcc_personality_v0 in -lgcc... yes
271.8 checking whether clang supports -Wno-shift-negative-value... yes
271.8 checking whether clang supports -Wno-misleading-indentation... yes
271.8 checking whether clang supports -Wno-gnu-folding-constant... yes
271.9 checking whether clang supports -Wno-unused-but-set-variable... no
271.9 checking whether clang supports -Wno-deprecated-non-prototype... no
271.9 checking whether ld supports -rpath,<path>,--enable-new-dtags... yes
272.0 checking for llvm-config... /usr/bin/llvm-config
272.0 checking for lto_get_version in -lLTO... yes
272.3 checking for _Block_release in -lBlocksRuntime... no
272.4 checking for _Block_release in -lc... no
272.4 Required library libblocksruntime not installed (install libdispatch!)
------
Dockerfile.aarch64-apple-ios-cross:26
--------------------
  24 |     COPY cross-toolchains/docker/ios.sh /
  25 |     COPY cross-toolchains/docker/ios-wrapper.c /
  26 | >>> RUN /ios.sh $CROSS_TARGET $TARGET_CPU
  27 |
  28 |     COPY cross-toolchains/docker/ios-symlink.sh /
--------------------
ERROR: failed to solve: process "/bin/sh -c /ios.sh $CROSS_TARGET $TARGET_CPU" did not complete successfully: exit code: 1
Error:
   0: `docker buildx build --platform linux/amd64 --build-arg 'CROSS_TARGET_TRIPLE=AARCH64_APPLE_IOS_CROSS' --load --pull --cache-from 'type=registry,ref=.../cross-rs/aarch64-apple-ios-cross:main' --tag .../cross-rs/aarch64-apple-ios-cross:latest --label 'org.cross-rs.for-cross-target=aarch64-apple-ios-cross' --label 'org.cross-rs.runs-with=x86_64-unknown-linux-gnu' --file .../cross-rs/docker/cross-toolchains/docker/Dockerfile.aarch64-apple-ios-cross --progress auto --build-arg 'IOS_SDK_DIR=/ios-sdks' --build-arg 'IOS_SDK_FILE=iPhoneOS16.2.sdk.tar.xz' .` failed with exit status: 1

Location:
   /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/convert/mod.rs:716

Warning: call to docker failed
Suggestion: is `buildx` available for the container engine?
Note: disable the `buildkit` dependency optionally with `CROSS_CONTAINER_ENGINE_NO_BUILDKIT=1`

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.

Next, try to replace libdispatch0 with libdispatch-dev, but still fails, and the error log will be:

264.2 libtool: compile:  clang++ -DPACKAGE_NAME=\"cctools\" -DPACKAGE_TARNAME=\"cctools\" -DPACKAGE_VERSION=\"1009.2\" "-DPACKAGE_STRING=\"cctools 1009.2\"" -DPACKAGE_BUGREPORT=\"t.poechtrager@gmail.com\" -DPACKAGE_URL=\"\" -DHAVE_STDIO_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_STRINGS_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_UNISTD_H=1 -DSTDC_HEADERS=1 -DHAVE_DLFCN_H=1 -DLT_OBJDIR=\".libs/\" -DEMULATED_HOST_CPU_TYPE=16777228 -DEMULATED_HOST_CPU_SUBTYPE=0 -D__STDC_LIMIT_MACROS=1 -D__STDC_CONSTANT_MACROS=1 -DHAVE_EXECINFO_H=1 -DHAVE_UUID_UUID_H=1 -DHAVE_UTIMENSAT=1 -DHAVE_BCOPY=1 -DHAVE_BCMP=1 -DHAVE_BZERO=1 -DHAVE_INDEX=1 -DHAVE_RINDEX=1 -DHAVE_STAT_ST_MTIM=1 -DHAVE_CLOCK_GETTIME=1 -DHAVE_DISPATCH_DISPATCH_H=1 -I. -isystem /opt/cctools/include -D__DARWIN_UNIX03 -Wall -Wno-long-long -Wno-import -Wno-format -Wno-deprecated -Wno-unused-variable -Wno-unused-private-field -Wno-unused-function -Wno-invalid-offsetof -Wno-int-conversion -Wno-char-subscripts -Wno-shift-negative-value -Wno-misleading-indentation -Wno-gnu-folding-constant -DLTO_SUPPORT -DTAPI_SUPPORT -D__LITTLE_ENDIAN__=1 -I../../../include -I../../../include/foreign -I../../../ld64/src -I../../../ld64/src/abstraction -I../../../ld64/src/3rd -I../../../ld64/src/3rd/include -I../../../ld64/src/ld -I../../../ld64/src/ld/parsers -I../../../ld64/src/ld/passes -I../../../ld64/src/mach_o -DPROGRAM_PREFIX=\"aarch64-apple-ios-\" -O3 -std=c++2a -isystem /usr/local/include -isystem /usr/pkg/include -DLD64_VERSION_NUM=907 -fblocks -isystem /opt/cctools/include -c Error.cpp  -fPIC -DPIC -o .libs/libmacho_la-Error.o
264.2 libtool: compile:  clang++ -DPACKAGE_NAME=\"cctools\" -DPACKAGE_TARNAME=\"cctools\" -DPACKAGE_VERSION=\"1009.2\" "-DPACKAGE_STRING=\"cctools 1009.2\"" -DPACKAGE_BUGREPORT=\"t.poechtrager@gmail.com\" -DPACKAGE_URL=\"\" -DHAVE_STDIO_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_STRINGS_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_UNISTD_H=1 -DSTDC_HEADERS=1 -DHAVE_DLFCN_H=1 -DLT_OBJDIR=\".libs/\" -DEMULATED_HOST_CPU_TYPE=16777228 -DEMULATED_HOST_CPU_SUBTYPE=0 -D__STDC_LIMIT_MACROS=1 -D__STDC_CONSTANT_MACROS=1 -DHAVE_EXECINFO_H=1 -DHAVE_UUID_UUID_H=1 -DHAVE_UTIMENSAT=1 -DHAVE_BCOPY=1 -DHAVE_BCMP=1 -DHAVE_BZERO=1 -DHAVE_INDEX=1 -DHAVE_RINDEX=1 -DHAVE_STAT_ST_MTIM=1 -DHAVE_CLOCK_GETTIME=1 -DHAVE_DISPATCH_DISPATCH_H=1 -I. -isystem /opt/cctools/include -D__DARWIN_UNIX03 -Wall -Wno-long-long -Wno-import -Wno-format -Wno-deprecated -Wno-unused-variable -Wno-unused-private-field -Wno-unused-function -Wno-invalid-offsetof -Wno-int-conversion -Wno-char-subscripts -Wno-shift-negative-value -Wno-misleading-indentation -Wno-gnu-folding-constant -DLTO_SUPPORT -DTAPI_SUPPORT -D__LITTLE_ENDIAN__=1 -I../../../include -I../../../include/foreign -I../../../ld64/src -I../../../ld64/src/abstraction -I../../../ld64/src/3rd -I../../../ld64/src/3rd/include -I../../../ld64/src/ld -I../../../ld64/src/ld/parsers -I../../../ld64/src/ld/passes -I../../../ld64/src/mach_o -DPROGRAM_PREFIX=\"aarch64-apple-ios-\" -O3 -std=c++2a -isystem /usr/local/include -isystem /usr/pkg/include -DLD64_VERSION_NUM=907 -fblocks -isystem /opt/cctools/include -c ExportsTrie.cpp  -fPIC -DPIC -o .libs/libmacho_la-ExportsTrie.o
264.3 In file included from ExportsTrie.cpp:31:
264.3 ./ExportsTrie.h:29:10: fatal error: 'span' file not found
264.3 #include <span>
264.3          ^~~~~~
264.4 1 error generated.
264.4 make[4]: *** [Makefile:464: libmacho_la-ExportsTrie.lo] Error 1
264.4 make[4]: Leaving directory '/tmp/tmp.geoD45gBZz/cctools-port/cctools/ld64/src/mach_o'
264.4 make[3]: *** [Makefile:479: all-recursive] Error 1
264.4 make[3]: Leaving directory '/tmp/tmp.geoD45gBZz/cctools-port/cctools/ld64/src/mach_o'
264.4 make[2]: *** [Makefile:384: all-recursive] Error 1
264.4 make[2]: Leaving directory '/tmp/tmp.geoD45gBZz/cctools-port/cctools/ld64/src'
264.4 make[1]: Leaving directory '/tmp/tmp.geoD45gBZz/cctools-port/cctools/ld64'
264.4 make[1]: *** [Makefile:385: all-recursive] Error 1
264.4 make: *** [Makefile:417: all-recursive] Error 1
------
Dockerfile.aarch64-apple-ios-cross:26
--------------------
  24 |     COPY cross-toolchains/docker/ios.sh /
  25 |     COPY cross-toolchains/docker/ios-wrapper.c /
  26 | >>> RUN /ios.sh $CROSS_TARGET $TARGET_CPU
  27 |
  28 |     COPY cross-toolchains/docker/ios-symlink.sh /
--------------------
ERROR: failed to solve: process "/bin/sh -c /ios.sh $CROSS_TARGET $TARGET_CPU" did not complete successfully: exit code: 2

Next, try to install libstdc++-10-dev, but still fails, and the error log will be:

277.7 libtool: compile:  clang++ -DPACKAGE_NAME=\"cctools\" -DPACKAGE_TARNAME=\"cctools\" -DPACKAGE_VERSION=\"1009.2\" "-DPACKAGE_STRING=\"cctools 1009.2\"" -DPACKAGE_BUGREPORT=\"t.poechtrager@gmail.com\" -DPACKAGE_URL=\"\" -DHAVE_STDIO_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_STRINGS_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_UNISTD_H=1 -DSTDC_HEADERS=1 -DHAVE_DLFCN_H=1 -DLT_OBJDIR=\".libs/\" -DEMULATED_HOST_CPU_TYPE=16777228 -DEMULATED_HOST_CPU_SUBTYPE=0 -D__STDC_LIMIT_MACROS=1 -D__STDC_CONSTANT_MACROS=1 -DHAVE_EXECINFO_H=1 -DHAVE_UUID_UUID_H=1 -DHAVE_UTIMENSAT=1 -DHAVE_BCOPY=1 -DHAVE_BCMP=1 -DHAVE_BZERO=1 -DHAVE_INDEX=1 -DHAVE_RINDEX=1 -DHAVE_STAT_ST_MTIM=1 -DHAVE_CLOCK_GETTIME=1 -DHAVE_DISPATCH_DISPATCH_H=1 -I. -isystem /opt/cctools/include -D__DARWIN_UNIX03 -Wall -Wno-long-long -Wno-import -Wno-format -Wno-deprecated -Wno-unused-variable -Wno-unused-private-field -Wno-unused-function -Wno-invalid-offsetof -Wno-int-conversion -Wno-char-subscripts -Wno-shift-negative-value -Wno-misleading-indentation -Wno-gnu-folding-constant -DLTO_SUPPORT -D__LITTLE_ENDIAN__=1 -I../../../../include -I../../../../include/foreign -I../../../../ld64/src/abstraction -I../../../../ld64/src/ld -I../../../../ld64/src/ld/parsers -I../../../../ld64/src/3rd -I../../../../ld64/src -O3 -std=c++2a -isystem /usr/local/include -isystem /usr/pkg/include -DLD64_VERSION_NUM=907 -fblocks -isystem /opt/cctools/include -c huge.cpp  -fPIC -DPIC -o .libs/libPasses_la-huge.o
279.4 order.cpp:658:41: error: use of undeclared identifier 'DISPATCH_APPLY_AUTO'
279.4         dispatch_apply(_state.sections.size(), DISPATCH_APPLY_AUTO, ^(size_t index) {
279.4                                                ^
279.5 code_dedup.cpp:349:43: error: use of undeclared identifier 'DISPATCH_APPLY_AUTO'
279.5     dispatch_apply(state.sections.size(), DISPATCH_APPLY_AUTO, ^(size_t index) {
279.5                                           ^
279.7 1 error generated.
279.7 make[4]: *** [Makefile:479: libPasses_la-order.lo] Error 1
279.7 make[4]: *** Waiting for unfinished jobs....
279.8 1 error generated.
279.8 make[4]: *** [Makefile:491: libPasses_la-code_dedup.lo] Error 1
293.9 make[4]: Leaving directory '/tmp/tmp.anxFM5V5pn/cctools-port/cctools/ld64/src/ld/passes'
293.9 make[3]: *** [Makefile:682: all-recursive] Error 1
293.9 make[3]: Leaving directory '/tmp/tmp.anxFM5V5pn/cctools-port/cctools/ld64/src/ld'
293.9 make[2]: *** [Makefile:384: all-recursive] Error 1
293.9 make[2]: Leaving directory '/tmp/tmp.anxFM5V5pn/cctools-port/cctools/ld64/src'
293.9 make[1]: *** [Makefile:385: all-recursive] Error 1
293.9 make[1]: Leaving directory '/tmp/tmp.anxFM5V5pn/cctools-port/cctools/ld64'
293.9 make: *** [Makefile:417: all-recursive] Error 1
------
Dockerfile.aarch64-apple-ios-cross:26
--------------------
  24 |     COPY cross-toolchains/docker/ios.sh /
  25 |     COPY cross-toolchains/docker/ios-wrapper.c /
  26 | >>> RUN /ios.sh $CROSS_TARGET $TARGET_CPU
  27 |
  28 |     COPY cross-toolchains/docker/ios-symlink.sh /
--------------------
ERROR: failed to solve: process "/bin/sh -c /ios.sh $CROSS_TARGET $TARGET_CPU" did not complete successfully: exit code: 2

Next, try to use latest cctools-port(1009.2-ld64-907 - a98286d8), but still fails, and the error log will be:

286.9 libtool: compile:  clang++ -DPACKAGE_NAME=\"cctools\" -DPACKAGE_TARNAME=\"cctools\" -DPACKAGE_VERSION=\"1009.2\" "-DPACKAGE_STRING=\"cctools 1009.2\"" -DPACKAGE_BUGREPORT=\"t.poechtrager@gmail.com\" -DPACKAGE_URL=\"\" -DHAVE_STDIO_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_STRINGS_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_UNISTD_H=1 -DSTDC_HEADERS=1 -DHAVE_DLFCN_H=1 -DLT_OBJDIR=\".libs/\" -DEMULATED_HOST_CPU_TYPE=16777228 -DEMULATED_HOST_CPU_SUBTYPE=0 -D__STDC_LIMIT_MACROS=1 -D__STDC_CONSTANT_MACROS=1 -DHAVE_EXECINFO_H=1 -DHAVE_UUID_UUID_H=1 -DHAVE_UTIMENSAT=1 -DHAVE_BCOPY=1 -DHAVE_BCMP=1 -DHAVE_BZERO=1 -DHAVE_INDEX=1 -DHAVE_RINDEX=1 -DHAVE_STAT_ST_MTIM=1 -DHAVE_CLOCK_GETTIME=1 -DHAVE_DISPATCH_DISPATCH_H=1 -I. -isystem /opt/cctools/include -D__DARWIN_UNIX03 -Wall -Wno-long-long -Wno-import -Wno-format -Wno-deprecated -Wno-unused-variable -Wno-unused-private-field -Wno-unused-function -Wno-invalid-offsetof -Wno-int-conversion -Wno-char-subscripts -Wno-shift-negative-value -Wno-misleading-indentation -Wno-gnu-folding-constant -DLTO_SUPPORT -D__LITTLE_ENDIAN__=1 -I../../../../include -I../../../../include/foreign -I../../../../ld64/src/abstraction -I../../../../ld64/src/ld -I../../../../ld64/src/ld/parsers -I../../../../ld64/src/3rd -I../../../../ld64/src -O3 -std=c++2a -isystem /usr/local/include -isystem /usr/pkg/include -DLD64_VERSION_NUM=907 -fblocks -isystem /opt/cctools/include -c objc.cpp  -fPIC -DPIC -o .libs/libPasses_la-objc.o
288.4 order.cpp:658:41: error: use of undeclared identifier 'DISPATCH_APPLY_AUTO'
288.4         dispatch_apply(_state.sections.size(), DISPATCH_APPLY_AUTO, ^(size_t index) {
288.4                                                ^
288.5 code_dedup.cpp:349:43: error: use of undeclared identifier 'DISPATCH_APPLY_AUTO'
288.5     dispatch_apply(state.sections.size(), DISPATCH_APPLY_AUTO, ^(size_t index) {
288.5                                           ^
288.7 1 error generated.
288.7 make[4]: *** [Makefile:479: libPasses_la-order.lo] Error 1
288.7 make[4]: *** Waiting for unfinished jobs....
288.8 1 error generated.
288.8 make[4]: *** [Makefile:491: libPasses_la-code_dedup.lo] Error 1
302.5 make[4]: Leaving directory '/tmp/tmp.6eXNtxF8lL/cctools-port/cctools/ld64/src/ld/passes'
302.5 make[3]: Leaving directory '/tmp/tmp.6eXNtxF8lL/cctools-port/cctools/ld64/src/ld'
302.5 make[3]: *** [Makefile:682: all-recursive] Error 1
302.5 make[2]: *** [Makefile:384: all-recursive] Error 1
302.5 make[2]: Leaving directory '/tmp/tmp.6eXNtxF8lL/cctools-port/cctools/ld64/src'
302.5 make[1]: *** [Makefile:385: all-recursive] Error 1
302.5 make[1]: Leaving directory '/tmp/tmp.6eXNtxF8lL/cctools-port/cctools/ld64'
302.5 make: *** [Makefile:417: all-recursive] Error 1
------
Dockerfile.aarch64-apple-ios-cross:26
--------------------
  24 |     COPY cross-toolchains/docker/ios.sh /
  25 |     COPY cross-toolchains/docker/ios-wrapper.c /
  26 | >>> RUN /ios.sh $CROSS_TARGET $TARGET_CPU
  27 |
  28 |     COPY cross-toolchains/docker/ios-symlink.sh /
--------------------
ERROR: failed to solve: process "/bin/sh -c /ios.sh $CROSS_TARGET $TARGET_CPU" did not complete successfully: exit code: 2
cavivie commented 7 months ago

Fixed by:

ios.sh

#!/usr/bin/env bash
# Adapted from cctools-port
#   https://github.com/tpoechtrager/cctools-port/tree/master/usage_examples/ios_toolchain
# This code is public domain
#   https://github.com/tpoechtrager/cctools-port/issues/21#issuecomment-223382676

set -x
set -eo pipefail

# shellcheck disable=SC1091
. lib.sh

if [[ "${IOS_SDK_FILE}" == "nonexistent" ]] && [[ -z "${IOS_SDK_URL}" ]]; then
    echo 'Must set the environment variable `IOS_SDK_FILE` or `IOS_SDK_URL`.' 1>&2
    exit 1
fi

get_sdk_version() {
    local version
    version=$(echo "${1}" | grep -P -o "[0-9][0-9].[0-9]+" | head -1)
    if [ -z "${version}" ]; then
        version=$(echo "${1}" | grep -P -o "[0-9].[0-9]+" | head -1)
    fi
    if [ -z "${version}" ]; then
        echo "iPhoneOS Version must be in the SDK filename!" 1>&2
        exit 1
    fi

    echo "${version}"
}

extract() {
    local tarball="${1}"
    local outdir="${2}"
    mkdir -p "${outdir}"

    case $1 in
        *.tar.xz|*.txz)
            tar -xJf "${tarball}" --directory "${2}"
            ;;
        *.tar.gz|*.tgz)
            tar -xzf "${tarball}" --directory "${2}"
            ;;
        *.tar.bz2|*.tbz2)
            tar -xjf "${tarball}" --directory "${2}"
            ;;
        *)
            echo "unhandled archive type" 1>&2
            exit 1
            ;;
    esac
}

main() {
    local target="${1}"
    local target_cpu="${2}"
    local ldid_commit="4bf8f4d60384a0693dbbe2084ce62a35bfeb87ab"
    local libdispatch_commit="ee39300b12a77efd3f2f020e009e42d557adbb29"
    local libtapi_commit="b8c5ac40267aa5f6004dd38cc2b2cd84f2d9d555"
    local cctools_commit="a98286d858210b209395624477533c0bde05556a"
    local install_dir="/opt/cctools"
    local sdk_dir="${install_dir}"/SDKs

    install_packages curl \
        gcc \
        g++ \
        make

    apt-get update
    apt-get install --assume-yes --no-install-recommends clang \
    libstdc++-10-dev \
        libbz2-dev \
        libssl-dev \
        libxml2-dev \
        zlib1g-dev \
        xz-utils \
        llvm-dev \
        uuid-dev

    local td
    td="$(mktemp -d)"
    pushd "${td}"

    # first, extract the SDK and get the version.
    mkdir sdk
    pushd sdk
    if [[ -f /"${IOS_SDK_FILE}" ]]; then
        cp /"${IOS_SDK_FILE}" .
    else
        curl --retry 3 -sSfL "${IOS_SDK_URL}" -O
    fi
    filename=$(ls -t -1 . | head -1)
    sdk_version=$(get_sdk_version "${filename}")
    extract "${filename}" SDKs

    # now, need to get our metadata, and move the SDK to the output dir
    mkdir -p "${install_dir}"
    mv SDKs "${sdk_dir}"
    local syslib
    syslib=$(find "${sdk_dir}" -name libSystem.dylib -o -name libSystem.tbd | head -n1)
    local wrapper_sdkdir
    pushd "${sdk_dir}"
    wrapper_sdkdir=$(echo iPhoneOS*sdk | head -n1)
    popd
    popd

    mkdir -p ios-wrapper
    pushd ios-wrapper
    cp /ios-wrapper.c .
    # want targets like armv7s, arm64
    mkdir -p "${install_dir}/bin"
    local clang="${install_dir}/bin/${target}-clang"
    gcc -O2 -Wall -Wextra -pedantic ios-wrapper.c \
        -DSDK_DIR="\"${wrapper_sdkdir}\"" \
        -DTARGET_CPU="\"${target_cpu}"\" \
        -DOS_VER_MIN="\"${sdk_version}"\" \
        -o "${clang}"
    popd

    # build our apple dependencies
    git clone https://github.com/tpoechtrager/ldid.git --depth 1
    pushd ldid
    git fetch --depth=1 origin "${ldid_commit}"
    make INSTALLPREFIX="${install_dir}" -j install
    popd

    git clone https://github.com/tpoechtrager/apple-libdispatch.git --depth 1
    pushd apple-libdispatch
    git fetch --depth=1 origin "${libdispatch_commit}"
    CC=clang CXX=clang++ cmake \
    -DCMAKE_BUILD_TYPE=RELEASE \
    -DCMAKE_INSTALL_PREFIX=${install_dir}
    make install
    popd

    git clone https://github.com/tpoechtrager/apple-libtapi.git --depth 1
    pushd apple-libtapi
    git fetch --depth=1 origin "${libtapi_commit}"
    INSTALLPREFIX="${install_dir}" ./build.sh
    ./install.sh
    popd

    # valid targets include `aarch64-apple-ios`
    git clone https://github.com/tpoechtrager/cctools-port.git --depth 1
    pushd cctools-port/cctools
    git fetch --depth=1 origin "${cctools_commit}"
    ./configure \
        --prefix="${install_dir}" \
        --with-libtapi="${install_dir}" \
    --with-libdispatch="${install_dir}" \
    --with-libblocksruntime="${install_dir}" \
        --target="${target}"
    make -j
    make install
    popd

    # make a symlink since clang is both a c and c++ compiler
    ln -sf "${clang}" "${clang}"++

    # need a fake wrapper for xcrun, which is used by `cc`.
    echo '#!/usr/bin/env sh
echo "${SDKROOT}"
' > "${install_dir}/bin/xcrun"
chmod +x "${install_dir}/bin/xcrun"

    purge_packages

    popd

    rm -rf "${td}"
    rm ios-wrapper.c
    rm "${0}"
}

main "${@}"

ios-wrapper.c

// Adapted from cctools-port
//  https://github.com/tpoechtrager/cctools-port/tree/master/usage_examples/ios_toolchain
// This code is public domain
//  https://github.com/tpoechtrager/cctools-port/issues/21#issuecomment-223382676

#ifndef OS_VER_MIN
#define OS_VER_MIN "4.2"
#endif

#ifndef SDK_DIR
#define SDK_DIR ""
#endif

#define _GNU_SOURCE

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <limits.h>

#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif

#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#include <sys/sysctl.h>
#endif

#ifdef __OpenBSD__
#include <sys/types.h>
#include <sys/user.h>
#include <sys/stat.h>
#endif

char *get_executable_path(char *epath, size_t buflen)
{
    char *p;
#ifdef __APPLE__
    unsigned int l = buflen;
    if (_NSGetExecutablePath(epath, &l) != 0) return NULL;
#elif defined(__FreeBSD__) || defined(__DragonFly__)
    int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
    size_t l = buflen;
    if (sysctl(mib, 4, epath, &l, NULL, 0) != 0) return NULL;
#elif defined(__OpenBSD__)
    int mib[4];
    char **argv;
    size_t len;
    size_t l;
    const char *comm;
    int ok = 0;
    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC_ARGS;
    mib[2] = getpid();
    mib[3] = KERN_PROC_ARGV;
    if (sysctl(mib, 4, NULL, &len, NULL, 0) < 0)
        abort();
    if (!(argv = malloc(len)))
        abort();
    if (sysctl(mib, 4, argv, &len, NULL, 0) < 0)
        abort();
    comm = argv[0];
    if (*comm == '/' || *comm == '.')
    {
        char *rpath;
        if ((rpath = realpath(comm, NULL)))
        {
            strlcpy(epath, rpath, buflen);
            free(rpath);
            ok = 1;
        }
    }
    else
    {
        char *sp;
        char *xpath = strdup(getenv("PATH"));
        char *path = strtok_r(xpath, ":", &sp);
        struct stat st;
        if (!xpath)
            abort();
        while (path)
        {
            snprintf(epath, buflen, "%s/%s", path, comm);
            if (!stat(epath, &st) && (st.st_mode & S_IXUSR))
        {
                ok = 1;
                break;
            }
            path = strtok_r(NULL, ":", &sp);
        }
        free(xpath);
    }
    free(argv);
    if (!ok) return NULL;
    l = strlen(epath);
#else
    ssize_t l = readlink("/proc/self/exe", epath, buflen - 1);
    if (l > 0) epath[l] = '\0';
#endif
    if (l <= 0) return NULL;
    epath[buflen - 1] = '\0';
    p = strrchr(epath, '/');
    if (p) *p = '\0';
    return epath;
}

char *get_filename(char *str)
{
    char *p = strrchr(str, '/');
    return p ? &p[1] : str;
}

void target_info(char *argv[], char **triple, char **compiler)
{
    char *p = get_filename(argv[0]);
    char *x = strrchr(p, '-');
    if (!x) abort();
    *compiler = &x[1];
    *x = '\0';
    *triple = p;
}

void env(char **p, const char *name, char *fallback)
{
    char *ev = getenv(name);
    if (ev) { *p = ev; return; }
    *p = fallback;
}

int main(int argc, char *argv[])
{
    char **args = alloca(sizeof(char*) * (argc+50));
    int i, j;

    char execpath[PATH_MAX+1];
    char sdkpath[PATH_MAX+1];
    char osvermin[64];

    char *compiler;
    char *target;

    char *sdk;
    char *cpu;
    char *osmin;

    target_info(argv, &target, &compiler);
    if (!get_executable_path(execpath, sizeof(execpath))) abort();
    snprintf(sdkpath, sizeof(sdkpath) - 1, "%s/../SDKs/" SDK_DIR, execpath);

    env(&sdk, "IOS_SDK_SYSROOT", sdkpath);
    env(&cpu, "IOS_TARGET_CPU", TARGET_CPU);

    env(&osmin, "IPHONEOS_DEPLOYMENT_TARGET", OS_VER_MIN);
    unsetenv("IPHONEOS_DEPLOYMENT_TARGET");

    snprintf(osvermin, sizeof(osvermin), "-miphoneos-version-min=%s", osmin);

    for (i = 1; i < argc; ++i)
    {
        if (!strcmp(argv[i], "-arch"))
        {
            cpu = NULL;
            break;
        }
    }

    i = 0;

    args[i++] = compiler;
    args[i++] = "-target";
    args[i++] = target;
    args[i++] = "-isysroot";
    args[i++] = sdk;

    if (cpu)
    {
        args[i++] = "-arch";
        args[i++] = cpu;
    }

    args[i++] = osvermin;
    args[i++] = "-mlinker-version=907";
    args[i++] = "-Wl,-adhoc_codesign";
    args[i++] = "-Wno-unused-command-line-argument";

    for (j = 1; j < argc; ++i, ++j)
        args[i] = argv[j];

    args[i] = NULL;

    setenv("COMPILER_PATH", execpath, 1);
    execvp(compiler, args);

    fprintf(stderr, "cannot invoke compiler!\n");
    return 1;
}