#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-only

set -eu

destdir=${1}

is_enabled() {
	grep -q "^$1=y" include/config/auto.conf
}

find_in_scripts() {
	find scripts \
		\( -name atomic -o -name dtc -o -name kconfig -o -name package \) -prune -o \
		! -name unifdef -a ! -name mk_elfconfig -a \( -type f -o -type l \) -print
}

mkdir -p "${destdir}"

(
	cd "${srctree}"
	echo Makefile
	find "arch/${SRCARCH}" -maxdepth 1 -name 'Makefile*'
	find "arch/${SRCARCH}" -name generated -prune -o -name include -type d -print
	find "arch/${SRCARCH}" -name Kbuild.platforms -o -name Platform
	find include \( -name config -o -name generated \) -prune -o \( -type f -o -type l \) -print
	find_in_scripts
) | tar -c -f - -C "${srctree}" -T - | tar -xf - -C "${destdir}"

{
	if is_enabled CONFIG_OBJTOOL; then
		echo tools/objtool/objtool
	fi

	if is_enabled CONFIG_DEBUG_INFO_BTF_MODULES; then
		echo tools/bpf/resolve_btfids/resolve_btfids
	fi

	echo Module.symvers
	echo "arch/${SRCARCH}/include/generated"
	echo include/config/auto.conf
	echo include/config/kernel.release
	echo include/generated
	find_in_scripts

	if is_enabled CONFIG_GCC_PLUGINS; then
		find scripts/gcc-plugins -name '*.so'
	fi
} | tar -c -f - -T - | tar -xf - -C "${destdir}"

# When ${CC} and ${HOSTCC} differ, rebuild host programs using ${CC}.
#
# This caters to host programs that participate in Kbuild. objtool and
# resolve_btfids are out of scope.
if [ "${CC}" != "${HOSTCC}" ]; then
	cat "${destdir}/scripts/Makefile" - <<-'EOF' > "${destdir}/scripts/Kbuild"
	subdir-y += basic
	hostprogs-always-y += mod/modpost
	mod/modpost-objs := $(addprefix mod/, modpost.o file2alias.o sumversion.o symsearch.o)
	EOF

	# HOSTCXX is not overridden. The C++ compiler is used to build:
	# - scripts/kconfig/qconf, which is unneeded for external module builds
	# - GCC plugins, which will not work on the installed system even after
	#   being rebuilt.
	#
	# Clear VPATH and srcroot because the source files reside in the output
	# directory.
	# shellcheck disable=SC2016 # $(MAKE) and $(build) will be expanded by Make
	"${MAKE}" run-command KBUILD_RUN_COMMAND='+$(MAKE) HOSTCC="'"${CC}"'" VPATH= srcroot=. $(build)='"$(realpath --relative-to=. "${destdir}")"/scripts

	rm -f "${destdir}/scripts/Kbuild"
fi

find "${destdir}" \( -name '.*.cmd' -o -name '*.o' \) -delete
