# Busybox is always included, unless explicitly turned off
CONFIG_BUSYBOX ?= y
modules-$(CONFIG_BUSYBOX) += busybox

busybox_version := 1.36.1
busybox_dir := busybox-$(busybox_version)
busybox_tar := busybox-$(busybox_version).tar.bz2
busybox_url := https://busybox.net/downloads/$(busybox_tar)
busybox_hash := b8cc24c9574d809e7279c3be349795c5d5ceb6fdf19ca709f80cde50e47de314

busybox_configure := $(MAKE) CC="$(heads_cc)" oldconfig
busybox_config := config/busybox.config
busybox_output := busybox
busybox_target := \
	$(CROSS_TOOLS) \
	$(MAKE_JOBS) \
	SOURCE_DATE_EPOCH=0 \

# For reproducibility, busybox is linked without --gc-sections.
# SEE: patches/busybox-1.36.1/0004-trylink-reproducible.patch
# patches scripts/trylink to skip --gc-sections when SOURCE_DATE_EPOCH
# is set.  ld.bfd's --gc-sections uses internal hash tables influenced
# by ASLR (no upstream binutils fix through 2.47).
# SOURCE_DATE_EPOCH=0 in busybox_target triggers the patch.
# The install rule copies the already-built binary and runs
# applets/install.sh directly, avoiding 'make install' which could
# trigger a re-link via the FORCE dependency on busybox_unstripped.

# Install symlinks when the busybox program is installed

ifeq "$(CONFIG_BUSYBOX)" "y"
initrd_bins += $(initrd_bin_dir)/busybox
endif

$(initrd_bin_dir)/busybox: $(build)/$(busybox_dir)/.build
	# Regenerate busybox.links (may be missing after clean/cache restore)
	# mkll only runs the preprocessor (-E) on generated headers;
	# using the cross-compiler keeps the build chain
	# self-contained (no host tools assumed)
	$(call do,INSTALL,bin/busybox,\
		cp $(build)/$(busybox_dir)/busybox \
			$(initrd_bin_dir)/busybox && \
		cd $(build)/$(busybox_dir) && \
		HOSTCC="$(heads_cc)" \
		$(SHELL) applets/busybox.mkll include/autoconf.h include/applets.h > busybox.links && \
		test -s busybox.links && \
		$(SHELL) applets/install.sh $(initrd_bin_dir)/.. --symlinks \
	)
	# Remove busybox's reboot symlink that conflicts with our custom script
	$(call do,RM-SYMLINKS,bin/busybox,\
		rm -f $(initrd_bin_dir)/../sbin/reboot \
		$(VERBOSE_REDIRECT) \
	)
	@touch $< # ensure that our build file is still newer


busybox_depends := $(musl_dep)
