# Configure the C toolchain
# It's tempting to use ${system} instead of linux-x86_64 here but Android NDK
# only includes pre-built tools for linux-x86_64 so there's no point.
prebuilt="$ANDROIDNDK/toolchains/llvm/prebuilt/linux-x86_64"

sysroot_include_dir="${prebuilt}/sysroot/usr/include"
arch_include_dir="${prebuilt}/sysroot/usr/include/aarch64-linux-android"
# python_includes="${prebuilt}/python3/include"
arch_libs_dir="${prebuilt}/sysroot/usr/lib/aarch64-linux-android/${ANDROID_PLATFORM_VERSION}"

export PATH="${prebuilt}/bin/:${PATH}"
# We just put it on PATH
export CC="aarch64-linux-android${ANDROID_PLATFORM_VERSION}-clang"
export ARCH="arm64-v8a"

# export AS="aarch64-linux-android-as"

# https://discourse.llvm.org/t/clang-failed-to-invoke-lld-correctly/62427 says
# to use this longer -target identifier, not just "aarch64".  It says
# "aarch64" is an unknown target and clang falls back to doing something dumb
# with gcc.  We can profile a triple instead of a quad and clang will
# automatically fill in unknown for the missing field ("vendor").
export CFLAGS="-target aarch64-linux-android -nostdlib -fPIC -march=armv8a -fomit-frame-pointer -DANDROID -I${sysroot_include_dir} -I${arch_include_dir}" #  -I${python_includes}
export CXXFLAGS="$CFLAGS"

export LDFLAGS="-L${arch_libs_dir}"

# Must have ld.lld on $PATH for this -fuse-ld to work
export LDSHARED="$CC -fuse-ld=lld -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions"

export LDLIBS="-lm"

# Need these???  None have been run so far.
AR="exit 1;"
RANLIB="exit 2;"
STRIP="exit 3;"
READELF="exit 4;"
OBJCOPY="exit 5;"

# Configure Python/distutils I think
export HOSTARCH=$ARCH


python3.10 -m build

# Some commands like this do the same-ish build as python -m build
#
# export LDSHARED=/nix/store/n7mv52lj2alq2gkys0skwwgs6c7s1cyl-androidsdk/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android31-clang
# /nix/store/n7mv52lj2alq2gkys0skwwgs6c7s1cyl-androidsdk/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android31-clang -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -target aarch64 -fPIC -march=armv8a -fomit-frame-pointer -DANDROID -I/nix/store/n7mv52lj2alq2gkys0skwwgs6c7s1cyl-androidsdk/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include -I/nix/store/n7mv52lj2alq2gkys0skwwgs6c7s1cyl-androidsdk/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/aarch64-linux-android -fPIC -Izfec/ -I/tmp/nix-shell.8SWxlo/build-env-a4lrm0_7/include -I/nix/store/dlhmz1f440qvxdxcbpnx3xqwdz4m1c7x-python3-3.10.4/include/python3.10 -c zfec/fec.c -o build/temp.linux-x86_64-cpython-310/zfec/fec.o -std=c99
# /nix/store/n7mv52lj2alq2gkys0skwwgs6c7s1cyl-androidsdk/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android31-clang -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -target aarch64 -fPIC -march=armv8a -fomit-frame-pointer -DANDROID -I/nix/store/n7mv52lj2alq2gkys0skwwgs6c7s1cyl-androidsdk/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include -I/nix/store/n7mv52lj2alq2gkys0skwwgs6c7s1cyl-androidsdk/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/aarch64-linux-android -fPIC -Izfec/ -I/tmp/nix-shell.8SWxlo/build-env-a4lrm0_7/include -I/nix/store/dlhmz1f440qvxdxcbpnx3xqwdz4m1c7x-python3-3.10.4/include/python3.10 -c zfec/_fecmodule.c -o build/temp.linux-x86_64-cpython-310/zfec/_fecmodule.o -std=c99
# PATH=$PATH:$(dirname $(find $ANDROIDNDK/ -name ld.lld)) /nix/store/n7mv52lj2alq2gkys0skwwgs6c7s1cyl-androidsdk/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android31-clang -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -L/nix/store/n7mv52lj2alq2gkys0skwwgs6c7s1cyl-androidsdk/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/31 -target aarch64 -fPIC -march=armv8a -fomit-frame-pointer -DANDROID -I/nix/store/n7mv52lj2alq2gkys0skwwgs6c7s1cyl-androidsdk/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include -I/nix/store/n7mv52lj2alq2gkys0skwwgs6c7s1cyl-androidsdk/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/aarch64-linux-android build/temp.linux-x86_64-cpython-310/zfec/_fecmodule.o build/temp.linux-x86_64-cpython-310/zfec/fec.o -L$PY38LIB -o _fec.cpython-310-x86_64-linux-gnu.so -fuse-ld=lld

# https://discourse.llvm.org/t/clang-sparc-generated-o-file-incompatible-with-elf64-x86-64/55238 says the target ld and as must be on $PATH
