#!/bin/sh

set -e
set -u

libexec="$(cd "$(dirname "$0")"; pwd)"
top="${libexec}/.."
src="${2:-"${top}/src"}"
version="${1:-$("${libexec}/metadata" node_version)}"

NJOBS="${NJOBS:-$(getconf _NPROCESSORS_ONLN 2>/dev/null || getconf NPROCESSORS_ONLN 2>/dev/null || true)}"
NJOBS="${NJOBS:-1}"

echo "parallel job count: ${NJOBS}"

BUILDTYPE="${BUILDTYPE:-Release}"
GITHUB_ACTIONS="${GITHUB_ACTIONS:-}"

cd "${src}/node-v${version}"

# Maglev is disabled because of a suspected x64 snapshot regression in
# DoComputeOutputFrames
configure_flags='--openssl-no-asm --without-npm --shared --with-intl=full-icu'
eval "$("${libexec}/platform")"

echo "configure: ${configure_flags}"
echo "compilers: CC='${CC}' CXX='${CXX}' CC_host='${CC_host:-}' CXX_host='${CXX_host:-}'"

${CC} -v
${CXX} -v

# shellcheck disable=SC2086
./configure ${configure_flags}

make BUILDTYPE="${BUILDTYPE}" config.gypi
make BUILDTYPE="${BUILDTYPE}" "out/Makefile"

# workaround for node specifying `-msign-return-address=all` in ALL `CFLAGS` for aarch64 builds
# (if the host isn't also aarch64, this flag causes a compiler error)

# shellcheck disable=SC2154 # these variables are defined by `eval`ing the output of the platform script above
if [ "$host_platform" != "$target_platform" ] && [ "${target_platform%%-*}" = "aarch64"  ]; then
	find . -iname "*.host.mk" -exec sed -i '/-msign-return-address/d' {} ';'
fi

if [ "${BUILDTYPE}" = "Release" ] && [ "${GITHUB_ACTIONS}" != "" ]; then
	case "${target_platform}" in
		arm64*-darwin*)
			# dwarf debug info for v8 is so massive the macos
			# builder runs out of disk space when it's enabled
			find out -name '*.mk' -exec sed -i '' -e 's/gdwarf-2/g0/g' {} ';'
			;;
	esac
fi

export PATH="${PWD}/out/tools/bin:${PATH}"
make -j"${NJOBS}" -C out BUILDTYPE="${BUILDTYPE}" V=
