#---------------------------------------------------------------------------
# File Name : Makefile
#---------------------------------------------------------------------------
# Description:
#  Makefile for the ISP20 Product Specific Buildroot
#
#  The build process clones the Honeywell Atmel Product Independent Buildroot
#  repository into a ./atmelbuildroot sub-directory. The Buildroot core in
#  ./atmelbuildroot/buildroot is accessed using the out-of-tree build mechanism
#  and the Product Independent Layer tree and the ISP20 Product Specific Layer
#  tree are accessed using the Buildroot External Tree mechanism.
#  The build process is performed in the ./output sub-directory to retain the
#  output file tree from the non-layered builds.
#  The ./dl subdirectory is used as the Buildroot download directory.
#---------------------------------------------------------------------------
# Copyright (c) 2020 Honeywell International Inc.
#---------------------------------------------------------------------------

# Local build definitions
BUILD_DIRECTORY := $(CURDIR)/output
TARGET_MAKEFILE := $(BUILD_DIRECTORY)/Makefile
DOWNLOAD_DIRECTORY := $(CURDIR)/dl

# The Honeywell Atmel Product Independent Buildroot repository
ATMEL_BUILDROOT_REPOSITORY := https://bitbucket.honeywell.com/scm/gcp/atmelbuildroot.git
ATMEL_BUILDROOT_REFERENCE := ATMEL_BUILDROOT_001_RELEASE_CANDIDATE_002

# Directory for the Honeywell Atmel Product Independent Buildroot repository clone
ATMEL_BUILDROOT_CLONE := $(CURDIR)/atmelbuildroot
BUILDROOT_CORE := $(ATMEL_BUILDROOT_CLONE)/buildroot
# The External Buildroot Tree access is relative to the Buildroot core directory
EXTERNAL_BUILDROOT_TREES := ../honeywell_buildroot_tree:../../isp20_buildroot_tree

.PHONY: all clean distclean sam9g25_isp20_development_defconfig sam9g25_isp20_release_defconfig sdk

# Export the Download Directory definition for use by all targets
export BR2_DL_DIR = $(DOWNLOAD_DIRECTORY)

all: $(ATMEL_BUILDROOT_CLONE) $(TARGET_MAKEFILE)
	make -C output all

sdk: $(ATMEL_BUILDROOT_CLONE) $(TARGET_MAKEFILE)
	make -C output sdk

clean: $(TARGET_MAKEFILE)
	make -C output clean

distclean:
	-make -C output distclean
	rm -rf output/*
	rm -rf $(ATMEL_BUILDROOT_CLONE)

sam9g25_isp20_development_defconfig: $(ATMEL_BUILDROOT_CLONE)
	make O=$(BUILD_DIRECTORY) -C $(BUILDROOT_CORE) BR2_EXTERNAL=$(EXTERNAL_BUILDROOT_TREES) sam9g25_isp20_development_defconfig

sam9g25_isp20_release_defconfig: $(ATMEL_BUILDROOT_CLONE)
	make O=$(BUILD_DIRECTORY) -C $(BUILDROOT_CORE) BR2_EXTERNAL=$(EXTERNAL_BUILDROOT_TREES) sam9g25_isp20_release_defconfig

$(ATMEL_BUILDROOT_CLONE):
	git clone -b $(ATMEL_BUILDROOT_REFERENCE) $(ATMEL_BUILDROOT_REPOSITORY) $(ATMEL_BUILDROOT_CLONE)

$(TARGET_MAKEFILE):
	$(error Please select Buildroot configuration first (e.g. "make sam9g25_isp20_release_defconfig"))
	@exit 1
