modules-y += coreboot

#coreboot_version := git
#coreboot_repo := https://github.com/osresearch/coreboot
coreboot_version := 4.5
coreboot_dir := coreboot-$(coreboot_version)
coreboot_tar := coreboot-$(coreboot_version).tar.xz
coreboot_url := https://www.coreboot.org/releases/$(coreboot_tar)
coreboot_hash := 0ffdcb0d18f506c483f8fe99df54fe7d5769f834eeffdc23160b035fee2a6027


# Coreboot builds are specialized on a per-target basis.
# The builds are done in a per-target subdirectory
#coreboot_config := coreboot-$(TARGET).config

EXTRA_FLAGS := -fdebug-prefix-map=$(pwd)=heads -gno-record-gcc-switches

coreboot_configure := \
	$(MAKE) \
		oldconfig \
		obj=./$(BOARD) \
		DOTCONFIG=../../config/coreboot-$(BOARD).config \
		BUILD_TIMELESS=1 \
		CFLAGS_x86_32="$(EXTRA_FLAGS)" \
		CFLAGS_x86_64="$(EXTRA_FLAGS)" \

coreboot_target := \
	obj=./$(BOARD) \
	DOTCONFIG=../../config/coreboot-$(BOARD).config \
	BUILD_TIMELESS=1 \
	CFLAGS_x86_32="$(EXTRA_FLAGS)" \
	CFLAGS_x86_64="$(EXTRA_FLAGS)" \
	$(MAKE_JOBS)

coreboot_output := $(BOARD)/coreboot.rom


# hack to force a build dependency on the cross compiler
$(build)/$(coreboot_dir)/.configured: $(build)/$(coreboot_dir)/util/crossgcc/xgcc/bin/i386-elf-gcc
$(build)/$(coreboot_dir)/util/crossgcc/xgcc/bin/i386-elf-gcc: $(build)/$(coreboot_dir)/.canary
	echo '******* Building crossgcc-i386 (this might take a while) ******'
	$(MAKE) -C "$(build)/$(coreboot_dir)" CPUS=`nproc` crossgcc-i386
	#echo '******* Building crossgcc-arm (this might take a while) ******'
	#$(MAKE) -C "$(build)/$(coreboot_dir)" crossgcc-arm

# The coreboot-blobs must be unpacked before we can build coreboot
# if we are using a tar file; git checkout will clone the submodule.
coreboot_depends := linux initrd

ifneq "$(coreboot_version)" "git"

# if we are not building from a git checkout,
# we must also download the coreboot-blobs tree
coreboot_depends += coreboot-blobs
modules-y += coreboot-blobs

coreboot-blobs_version := $(coreboot_version)
coreboot-blobs_tar := coreboot-blobs-$(coreboot-blobs_version).tar.xz
coreboot-blobs_dir := coreboot-$(coreboot-blobs_version)/3rdparty/blobs
coreboot-blobs_url := https://www.coreboot.org/releases/$(coreboot-blobs_tar)
coreboot-blobs_hash := 86dc3939f546fa9c3907434f9e8ee9e2362f9572b492fc92ea89ae313cf214e4

## there is nothing to build for the blobs, this should be
## made easier to make happen
coreboot-blobs_output := .built
coreboot-blobs_configure := echo -e 'all:\n\ttouch .built' > Makefile

endif