#! /usr/bin/env bash
upload='false'
for arg in "$@"; do
case "${arg}" in
--help)
echo "Usage: build-precompiled [--upload]"
exit 0
;;
--upload)
upload='true'
;;
*)
echo "Invalid argument \"${arg}\"" >&2
exit 1
;;
esac
done
# Ensure we are in the correct working directory
cd "$(dirname "$(which "$0")")/.." || exit 1
# Determine the version of Filed
version=''
eval "$(( grep '^# *define *FILED_VERSION' filed.c; echo '|version=FILED_VERSION' ) | cpp -E | grep '^|' | sed 's@^|@@')"
if [ -z "${version}" ]; then
echo "Unable to determine which version of Filed we are compiling. Aborting." >&2
exit 1
fi
# Cleanup
rm -rf workdir-buildPrecompiled-*
# Compile everything, all at once
idx=-1
for tryCompilerDir in "$(readlink -f ~/root/cross-compilers)" "$(readlink -f ~/devel/build-cc/TMP)"; do
setup_cc="${tryCompilerDir}/setup-cc"
platforms=(
$("${setup_cc}" | tail -n +2)
)
for platform in "${platforms[@]}"; do
idx=$[$idx + 1]
(
workdir="workdir-buildPrecompiled-${idx}-$(openssl rand 20 -hex)-platform-${platform}" || exit 1
mkdir "${workdir}" || exit 1
cd "${workdir}" || exit 1
eval $("${setup_cc}" "${platform}")
make_extra=(
-f ../Makefile
srcdir=..
CC="${CC}"
)
case "${platform}" in
*-musl-*|*-musl)
make_extra=("${make_extra[@]}" FILED_EXTRA_LDFLAGS="-static")
;;
esac
make "${make_extra[@]}"
) &
done
done
# Wait for that to get done
wait
# Rename the files into place
mkdir -p compiled
binaries=()
for binary in workdir-buildPrecompiled-*/filed; do
platform="$(echo "${binary}" | sed 's@^.*-platform-@@;s@/.*$@@')"
binary_dest="compiled/filed-${version}-${platform}"
mv "${binary}" "${binary_dest}"
binaries+=("${binary_dest}")
done
if [ "${upload}" = 'true' ]; then
for binary in "${binaries[@]}"; do
fossil uv add "${binary}" --as "releases/${version}/$(basename "${binary}")"
done
fi
# Cleanup
rm -rf workdir-buildPrecompiled-*
exit 0