#
# Auto generated makefile
#

export `args.sdkName`?=$(abspath `args.relPath`)
include $(`args.sdkName`)/imports.mak
% if ((args.project.device == "am64x") || (args.project.device == "am243x")) {
include $(`args.sdkName`)/devconfig/devconfig.mak
% }

% if ((args.project.device == "am64x") || (args.project.device == "am243x")) {
ifeq ($(OS), Windows_NT)
	PYTHON=python
else
	PYTHON=python3
endif
% }

PROFILE?=release

MULTI_CORE_BOOTIMAGE_PATH=$(abspath .)
MULTI_CORE_BOOTIMAGE_NAME:=`args.project.name`_system.$(PROFILE).appimage
MULTI_CORE_BOOTIMAGE_NAME_SIGNED:=$(MULTI_CORE_BOOTIMAGE_NAME).signed
MULTI_CORE_BOOTIMAGE_NAME_XIP:=`args.project.name`_system.$(PROFILE).appimage_xip
% if (args.project.device == "awr294x") {
MULTI_CORE_BOOTIMAGE_NAME_QSPI:=`args.project.name`_system.$(PROFILE).appimage_qspi
% }
% if ((args.project.device == "am64x") || (args.project.device == "am243x")) {
MULTI_CORE_BOOTIMAGE_NAME_HS:=$(MULTI_CORE_BOOTIMAGE_NAME).hs
% }

% let numProjects = 0;
% for (let project of args.project.projects) {
CORE_`numProjects`=--script ../`project.cpu`_`project.os`/example.syscfg --context `args.device.getSysCfgCpu(project.cpu)` --output ../`project.cpu`_`project.os`/`project.cgt`/generated
    % numProjects = numProjects + 1;
% }

CORES = \
% for (let i = numProjects-1; i>=0; i--) {
    $(CORE_`i`) \
% }

all: syscfg
% for (let project of args.project.projects) {
	$(MAKE) -C ../`project.cpu`_`project.os`/`project.cgt`/ all
% }
	$(MAKE) $(MULTI_CORE_BOOTIMAGE_NAME)
% if ((args.project.device == "am64x") || (args.project.device == "am243x")) {
ifeq ($(DEVICE_TYPE), HS)
	$(MAKE) $(MULTI_CORE_BOOTIMAGE_NAME_HS)
endif
% }
% for (let project of args.project.projects) {
% if (project.isLinuxFwGen) {
	$(COPY) ../`project.cpu`_`project.os`/`project.cgt`/`args.device.getLinuxFwName(project.cpu)` .
% }
% }

clean:
% for (let project of args.project.projects) {
	$(MAKE) -C ../`project.cpu`_`project.os`/`project.cgt`/ clean
% }
	$(RM) $(MULTI_CORE_BOOTIMAGE_NAME)
	$(RM) $(MULTI_CORE_BOOTIMAGE_NAME_SIGNED)
	$(RM) $(MULTI_CORE_BOOTIMAGE_NAME_XIP)
% if (args.project.device == "awr294x") {
	$(RM) $(MULTI_CORE_BOOTIMAGE_NAME_QSPI)
% }
% for (let project of args.project.projects) {
% if (project.isLinuxFwGen) {
	$(RM) `args.device.getLinuxFwName(project.cpu)`
% }
% }

scrub:
% for (let project of args.project.projects) {
	$(MAKE) -C ../`project.cpu`_`project.os`/`project.cgt`/ scrub
% }
% for (let project of args.project.projects) {
% if (project.isLinuxFwGen) {
	$(RM) `args.device.getLinuxFwName(project.cpu)`
% }
% }
ifeq ($(OS),Windows_NT)
	$(RM) \*.appimage
	$(RM) \*.appimage.signed
% if ((args.project.device == "am64x") || (args.project.device == "am243x")) {
	$(RM) \*.appimage.hs
% }
	$(RM) \*.appimage_xip
else
	$(RM) *.appimage
	$(RM) *.appimage.signed
% if ((args.project.device == "am64x") || (args.project.device == "am243x")) {
	$(RM) \*.appimage.hs
% }
	$(RM) *.appimage_xip
endif

syscfg:
	@echo Generating SysConfig files ...
	$(SYSCFG_NODE) $(SYSCFG_CLI_PATH)/dist/cli.js --product $(SYSCFG_SDKPRODUCT) $(CORES)

syscfg-gui:
	$(SYSCFG_NWJS) $(SYSCFG_PATH) --product $(SYSCFG_SDKPRODUCT) --device `args.device.getSysCfgDevice(args.project.board)` --part `args.device.getSysCfgPart(args.project.board)` --package `args.device.getSysCfgPkg(args.project.board)` $(CORES)

#
# Generation of multi-core boot image which can be loaded by Secondary Boot Loader (SBL)
#
ifeq ($(OS),Windows_NT)
EXE_EXT=.exe
endif
BOOTIMAGE_TEMP_OUT_FILE=temp_stdout_$(PROFILE).txt

% if (args.project.device == "am64x") {
BOOTIMAGE_CORE_ID_a53ss0-0 = 0
BOOTIMAGE_CORE_ID_r5fss0-0 = 4
BOOTIMAGE_CORE_ID_r5fss0-1 = 5
BOOTIMAGE_CORE_ID_r5fss1-0 = 6
BOOTIMAGE_CORE_ID_r5fss1-1 = 7
BOOTIMAGE_CORE_ID_m4fss0-0 = 14
SBL_RUN_ADDRESS=0x70000000
SBL_DEV_ID=55
% }
% if (args.project.device == "am243x") {
BOOTIMAGE_CORE_ID_r5fss0-0 = 4
BOOTIMAGE_CORE_ID_r5fss0-1 = 5
BOOTIMAGE_CORE_ID_r5fss1-0 = 6
BOOTIMAGE_CORE_ID_r5fss1-1 = 7
BOOTIMAGE_CORE_ID_m4fss0-0 = 14
SBL_RUN_ADDRESS=0x70000000
SBL_DEV_ID=55
% }
% if (args.project.device == "am263x") {
BOOTIMAGE_CORE_ID_r5fss0-0 = 0
BOOTIMAGE_CORE_ID_r5fss0-1 = 1
BOOTIMAGE_CORE_ID_r5fss1-0 = 2
BOOTIMAGE_CORE_ID_r5fss1-1 = 3
SBL_RUN_ADDRESS=0x70000000
SBL_DEV_ID=55
% }
% if ((args.project.device == "am273x") || (args.project.device == "awr294x")) {
BOOTIMAGE_CORE_ID_r5fss0-0 = 0
BOOTIMAGE_CORE_ID_r5fss0-1 = 1
BOOTIMAGE_CORE_ID_c66ss0   = 2
% if (args.project.device == "awr294x") {
BOOTIMAGE_CORE_ID_r4       = 3
R4_RPRC_PATH = $(MCU_PLUS_SDK_PATH)/source/drivers/bootloader/soc/awr294x/xwr2xxx_radarss_metarprc.bin
% }
SBL_RUN_ADDRESS=0x10200000
SBL_DEV_ID=55
% }
% if (args.project.device == "am62x") {
BOOTIMAGE_CORE_ID_a53ss0-0 = 0
BOOTIMAGE_CORE_ID_a53ss0-1 = 1
BOOTIMAGE_CORE_ID_a53ss1-0 = 2
BOOTIMAGE_CORE_ID_a53ss1-1 = 3
BOOTIMAGE_CORE_ID_r5fss0-0 = 4
BOOTIMAGE_CORE_ID_m4fss0-0 = 5
SBL_RUN_ADDRESS=0x70000000
SBL_DEV_ID=55
% }

MULTI_CORE_IMAGE_GEN = $(SYSCFG_NODE) $(MCU_PLUS_SDK_PATH)/tools/boot/multicoreImageGen/multicoreImageGen.js
OUTRPRC_CMD = $(SYSCFG_NODE) $(MCU_PLUS_SDK_PATH)/tools/boot/out2rprc/elf2rprc.js
% if ((args.project.device == "am243x") || (args.project.device == "am64x")) {
APP_IMAGE_SIGN_CMD = $(MCU_PLUS_SDK_PATH)/tools/boot/signing/appimage_x509_cert_gen.py
% }

MULTI_CORE_APP_PARAMS = \
% for (let project of args.project.projects) {
    ../`project.cpu`_`project.os`/`project.cgt`/`args.project.name`.$(PROFILE).rprc@$(BOOTIMAGE_CORE_ID_`project.cpu`) \
% }

MULTI_CORE_APP_PARAMS_XIP = \
% for (let project of args.project.projects) {
    ../`project.cpu`_`project.os`/`project.cgt`/`args.project.name`.$(PROFILE).rprc_xip@$(BOOTIMAGE_CORE_ID_`project.cpu`) \
% }

% if (args.project.device == "awr294x") {
MULTI_CORE_APP_PARAMS_QSPI = $(MULTI_CORE_APP_PARAMS) $(R4_RPRC_PATH)@$(BOOTIMAGE_CORE_ID_r4)

% }
MULTI_CORE_BOOTIMAGE_DEPENDENCY = \
% for (let project of args.project.projects) {
    ../`project.cpu`_`project.os`/`project.cgt`/`args.project.name`.$(PROFILE).rprc \
% }

$(MULTI_CORE_BOOTIMAGE_DEPENDENCY):

$(MULTI_CORE_BOOTIMAGE_NAME): $(MULTI_CORE_BOOTIMAGE_DEPENDENCY)
	@echo  Boot multi-core image: $@ ...
	$(MULTI_CORE_IMAGE_GEN) --devID $(SBL_DEV_ID) --out $(MULTI_CORE_BOOTIMAGE_NAME) $(MULTI_CORE_APP_PARAMS) >> $(BOOTIMAGE_TEMP_OUT_FILE)
	$(MULTI_CORE_IMAGE_GEN) --devID $(SBL_DEV_ID) --out $(MULTI_CORE_BOOTIMAGE_NAME_XIP) $(MULTI_CORE_APP_PARAMS_XIP) >> $(BOOTIMAGE_TEMP_OUT_FILE)
% if (args.project.device == "awr294x") {
	$(MULTI_CORE_IMAGE_GEN) --devID $(SBL_DEV_ID) --out $(MULTI_CORE_BOOTIMAGE_NAME_QSPI) $(MULTI_CORE_APP_PARAMS_QSPI) >> $(BOOTIMAGE_TEMP_OUT_FILE)
% }
	$(RM) $(BOOTIMAGE_TEMP_OUT_FILE)
	@echo  Boot multi-core image: $(MULTI_CORE_BOOTIMAGE_PATH)/$@ Done !!!
	@echo  .
% if ((args.project.device == "am243x") || (args.project.device == "am64x")) {
	$(PYTHON) $(APP_IMAGE_SIGN_CMD) --bin $(MULTI_CORE_BOOTIMAGE_NAME) --authtype 1 --key $(APP_SIGNING_KEY) --output $(MULTI_CORE_BOOTIMAGE_NAME).hs_fs
	@echo  Boot multi-core image: $(MULTI_CORE_BOOTIMAGE_PATH)/$(MULTI_CORE_BOOTIMAGE_NAME).hs_fs Done !!!
	@echo  .

$(MULTI_CORE_BOOTIMAGE_NAME_HS): $(MULTI_CORE_BOOTIMAGE_NAME)
ifeq ($(DEVICE_TYPE),HS)
# Sign the appimage using appimage signing script
ifeq ($(ENC_ENABLED),no)
	@echo Boot image signing: Encryption is disabled.
	$(PYTHON) $(APP_IMAGE_SIGN_CMD) --bin $(MULTI_CORE_BOOTIMAGE_NAME) --authtype 1 --key $(APP_SIGNING_KEY) --output $(MULTI_CORE_BOOTIMAGE_NAME_HS)
else
	@echo Boot image signing: Encryption is enabled.
	$(PYTHON) $(APP_IMAGE_SIGN_CMD) --bin $(MULTI_CORE_BOOTIMAGE_NAME) --authtype 1 --key $(APP_SIGNING_KEY) --enc y --enckey $(APP_ENCRYPTION_KEY) --output $(MULTI_CORE_BOOTIMAGE_NAME_HS)
	$(RM) $(MULTI_CORE_BOOTIMAGE_NAME)-enc
endif
	@echo  Boot multi-core image: $(MULTI_CORE_BOOTIMAGE_PATH)/$@ Done !!!
	@echo  Boot multi-core image: $(MULTI_CORE_BOOTIMAGE_PATH)/$(MULTI_CORE_BOOTIMAGE_NAME_HS) Done !!!
	@echo  .
endif
% }
