ifneq ($(wildcard ../../build/teegris_config.mk),)
IS_RUNTIME_BUILD = true
else
IS_RUNTIME_BUILD = false
endif


SECURE_TOOLCHAIN = $(TEEGRIS_TA_TOOLCHAIN)
CONFIG_TA_ARCH = $(TAD_TA_ARCH)
CONFIG_DEBUG = ON

ifeq ($(IS_RUNTIME_BUILD),true)
$(info [TAD] runtime build)
include ../../build/teegris_config.mk
TRUSTLET_DIR := src
OUTPUT_DIR	:= Out/ta
SOC_NAME=$(TEEGRIS_BUILD_MODEL)
else
$(info [TAD] local build)
include ../../../script/teegris_config.mk
TRUSTLET_DIR := ../src
OUTPUT_DIR	:= ../../Out/ta
endif


TARGET_TA_NAME = 00000000-0000-0000-0000-446e6c6f6164

SRC_TA := \
  $(TRUSTLET_DIR)/dependency_teegris/teegdep_main.c \
  $(TRUSTLET_DIR)/dependency_teegris/teegdep_util.c \
  $(TRUSTLET_DIR)/dependency_teegris/teegdep_crypto.c \
  $(TRUSTLET_DIR)/cmd_manager.c \
  $(TRUSTLET_DIR)/tlv_parser.c \
  $(TRUSTLET_DIR)/parser_tlv.c \
  $(TRUSTLET_DIR)/util.c \
  $(TRUSTLET_DIR)/parser_certificate.c \
  $(TRUSTLET_DIR)/cmd_crypto.c \
  $(TRUSTLET_DIR)/cmd_skpm.c \
  $(TRUSTLET_DIR)/cmd_common.c

INCLUDE_DIRS += \
  $(TRUSTLET_DIR)/dependency_teegris/inc \
  $(TRUSTLET_DIR)/inc
INCLUDE_DIRS := $(INCLUDE_DIRS:%=-I %)

CONFIG_CFLAGS_SW += \
  $(INCLUDE_DIRS) \
  -DUSE_TEEGRIS

CONFIG_CFLAGS_SW += -DUSE_$(shell echo $(SOC_NAME) | tr a-z A-Z)
CONFIG_CFLAGS_SW += -DUSE_$(shell echo $(PLATFORM_NAME) | sed -e "s/-//g" | sed -e "s/\.//g")

CONFIG_CFLAGS_SW := $(shell echo $(CONFIG_CFLAGS_SW) | sed -e "s/-Werror //g")

UNSIGNED_TA	:= $(OUTPUT_DIR)/unsigned_TA
SIGNED_TA	:= $(OUTPUT_DIR)/signed/$(TARGET_TA_NAME)
OBJS 		:= $(patsubst $(TRUSTLET_DIR)/%.c,$(OUTPUT_DIR)/%.o,$(SRC_TA))

.PHONY: all
all: make_output_dir $(SIGNED_TA)

.PHONY: clean
clean:
	rm -rf $(OUTPUT_DIR)

.PHONY: make_out
make_output_dir:
	$(MKDIR) $(OUTPUT_DIR)/dependency_teegris
	$(MKDIR) $(OUTPUT_DIR)/signed

$(SIGNED_TA): $(UNSIGNED_TA)	#sign
	@echo "\e[0;33m##### SIGNING #####\e[0m"
	$(CONFIG_STRIP_SW) $<
	$(signing_command)
ifeq ($(IS_RUNTIME_BUILD),true)
	@echo "\e[0;33m##### Copy #####\e[0m"
	$(CP) $(SIGNED_TA) $(TEEGRIS_TA_DIST_PATH)
endif

$(UNSIGNED_TA): $(OBJS)		#link
	$(CONFIG_CC_SW) $(CONFIG_CFLAGS_SW) $(CONFIG_LDFLAGS_SW) $(OBJS) -o $@

$(OUTPUT_DIR)/%.o : $(TRUSTLET_DIR)/%.c		#build 
	$(CONFIG_CC_SW) $(CONFIG_CFLAGS_SW) -c $<  -o $@
