CONFIG_MUSL ?= y

ifeq "$(MUSL_CROSS_ONCE)" ""
MUSL_CROSS_ONCE := 1
modules-$(CONFIG_MUSL) += musl-cross-make

musl-cross-make_version := 227df8b99103f9c59f6570babf892978e293082f
musl-cross-make_dir := musl-cross-make-$(musl-cross-make_version)
musl-cross-make_url := https://github.com/richfelker/musl-cross-make/archive/$(musl-cross-make_version).tar.gz
musl-cross-make_tar := musl-cross-make-$(musl-cross-make_version).tar.gz
musl-cross-make_hash := bb3fc7851088e1e5e1274ee56a0ab6ae176043d160fdf0b71027934b091f208a

# Auto-detect crossgcc from cache restore before checking CROSS env var
CROSS_PATH := $(pwd)/crossgcc/$(CONFIG_TARGET_ARCH)
CROSS_CHECK := $(CROSS_PATH)/bin/$(subst -,_,$(MUSL_ARCH))-linux-musl-gcc

ifneq "$(wildcard $(CROSS_CHECK))" ""
CROSS := $(CROSS_PATH)/bin/$(subst -,_,$(MUSL_ARCH))-linux-musl-
endif

ifneq "$(CROSS)" ""

# check that $(CROSS)gcc exists or else things just won't work
ifneq "y" "$(shell [ -x '$(CROSS)gcc' ] && echo y)"
$(error $(CROSS)gcc does not exist - can not build)
else
$(info Using $(CROSS)gcc)
endif

# The cross compiler has already been built, so the musl-cross-make target
# is a NOP.  We really don't need to check out this code tree, but it is easier
# if we have a target for it.
musl-cross-make_target := --version

# Ask the compiler where to find its own libc.so
musl-cross-make_libraries := \
	$(shell $(CROSS)gcc --print-file-name=libc.so) \

else

# Force a full build of the cross compiler for target platform
# No need to build i386 for x86 since coreboot uses its own compiler
#
# For reproducibility, config.mak is written with:
#   BUILD = x86_64-pc-linux-gnu -- prevents config.guess from probing
#     the Docker host kernel (different CI runners produce different triplets)
#   -Wa,--no-pad-sections -- prevents gas from padding section ends,
#     producing non-deterministic alignment between runs
#   --with-debug-prefix-map=$(pwd)=. -- normalizes build paths in debug info
#   --enable-compressed-debug-sections=no -- disables non-deterministic zlib
#     debug-section compression
# SOURCE_DATE_EPOCH from the pinned commit epoch prevents __DATE__/__TIME__
# embedding during the GCC build.
musl-cross-make_configure := \
	echo -e >> Makefile 'musl-target:' ; \
	echo -e >> Makefile '\t$$(MAKE) TARGET="$(MUSL_ARCH)-linux-musl" install' ; \
	echo -e 'BUILD = x86_64-pc-linux-gnu\nCOMMON_CONFIG += CFLAGS="-Wa,--no-pad-sections"\nCOMMON_CONFIG += --with-debug-prefix-map=$(pwd)=.\nBINUTILS_CONFIG += --enable-compressed-debug-sections=no\nSOURCES = $(packages)' >> config.mak

CROSS_PATH ?= $(pwd)/crossgcc/$(CONFIG_TARGET_ARCH)

musl-cross-make_target = \
	SOURCE_DATE_EPOCH=$(shell cd $(build)/$(musl-cross-make_dir) && git log -1 --format=%ct 2>/dev/null || echo 0) \
	OUTPUT="$(CROSS_PATH)" \
	MAKE="$(MAKE)" \
	$(MAKE_JOBS) \
	"musl-target"

CROSS := $(CROSS_PATH)/bin/$(subst -,_,$(MUSL_ARCH))-linux-musl-
musl-cross-make_libraries := $(CROSS_PATH)/$(subst -,_,$(MUSL_ARCH))-linux-musl/lib/libc.so

endif


musl-cross-make_output := $(CROSS)gcc

# Pre-seed component tarballs into $(packages) via fetch_source_archive.sh
# (primary -> Purism mirror fallback), following the same pattern as
# coreboot's crossgcc packages.  Tarballs are cached alongside other module
# tarballs and picked up by Purism's package mirror sync.
$(build)/$(musl-cross-make_dir)/.sources: $(build)/$(musl-cross-make_dir)/.canary
	WGET="$(WGET)" bin/fetch_musl_cross_make_archive.sh \
		"$(build)/$(musl-cross-make_dir)" "$(packages)"
	touch "$@"

# 'make packages' seeds mirrors with these tarballs too
packages: $(build)/$(musl-cross-make_dir)/.sources

# Run the fetch script before configure so tarballs land in packages/
# before the build starts
$(build)/$(musl-cross-make_dir)/.configured: $(build)/$(musl-cross-make_dir)/.sources

## Fake a target so that musl will force a header install by the
## Linux kernel sources.
$(build)/$(musl-cross-make_dir)/.build: $(INSTALL)/include/linux/limits.h


endif
