RM := rm -rf

CFLAGS = -g -DSWD -DUSE_BLOWFISH
CFLAGS += -fstack-protector -fstack-protector-all
OUTPUT_DIR = $(BF_SWD_ROOT)/../../../$(P4_BIN_DIR)
TRUSTLET_DIR = $(BF_SWD_ROOT)/../../common
TRUSTLET_ELF = mldap_TA.elf
TRUSTLET_UUID=$(MLDAP_UUID)

ifneq ($(filter V4.0, $(TG_SDK_VER)),)
    CFLAGS += -DSDK_VER_V4
    INC_32 = -I$(BLOWFISH_BASE)/platforms/TEEGRIS-4.1/swd/arch-arm/usr/include
    INC_64 = -I$(BLOWFISH_BASE)/platforms/TEEGRIS-4.1/swd/arch-arm64/usr/include
endif
 
ifneq ($(filter V3.0, $(TG_SDK_VER)),)
    CFLAGS += -DSDK_VER_V3
    INC_32 = -I$(BLOWFISH_BASE)/platforms/TEEGRIS-3.0/swd/arch-arm/usr/include
    INC_64 = -I$(BLOWFISH_BASE)/platforms/TEEGRIS-3.0/swd/arch-arm64/usr/include
endif
 
ifneq ($(filter V2.0, $(TG_SDK_VER)),)
    INC_32 = -I$(BLOWFISH_BASE)/platforms/BF-2.0/swd/arch-arm/usr/include
    INC_64 = -I$(BLOWFISH_BASE)/platforms/BF-2.0/swd/arch-arm64/usr/include
endif

ifeq ($(MODE), Debug)
    CFLAGS += -DSWD_DEBUG
endif

CC = ${TOOLCHAIN_PREFIX}gcc
STRIP = ${TOOLCHAIN_PREFIX}strip

INCLUDE_DIRS += \
    -I$(TRUSTLET_DIR) \
    -I$(TRUSTLET_DIR)/crypto/include \
    -I$(TRUSTLET_DIR)/x509 \
    -I$(TRUSTLET_DIR)/../teegris/src \
    -I$(TRUSTLET_DIR)/../../nwd/common/include \
    -I$(TRUSTLET_DIR)/base64 

ifeq ($(USE_NEW_STACK), TRUE)
    CFLAGS += -DUSE_NEW_STACK
endif

ifeq ($(USE_DRKV2), TRUE)
    CFLAGS += -DUSE_DRKV2
endif

SRC += \
    $(TRUSTLET_DIR)/../teegris/src/blowfish_agent_main.c \
    $(TRUSTLET_DIR)/../teegris/src/blowfish_utils.c \
    $(TRUSTLET_DIR)/../teegris/src/CryptoPlatformBF.c \
    $(TRUSTLET_DIR)/CryptoPlatform.c \
    $(TRUSTLET_DIR)/CommandHandler.c \
    $(TRUSTLET_DIR)/x509/asn1.c \
    $(TRUSTLET_DIR)/x509/asn1gen.c \
    $(TRUSTLET_DIR)/x509/asn1rsa.c \
    $(TRUSTLET_DIR)/x509/asn1ec.c \
    $(TRUSTLET_DIR)/x509/da_cert_gencer.c \
    $(TRUSTLET_DIR)/x509/da_cert_parcer.c \
    $(TRUSTLET_DIR)/x509/x509v3.c \
    $(TRUSTLET_DIR)/crypto/sec_alloc.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_wrapper.c \
    $(TRUSTLET_DIR)/crypto/rsa/rsa_wrapper.c \
    $(TRUSTLET_DIR)/crypto/mem_clr.c \
    $(TRUSTLET_DIR)/../../nwd/common/ServiceName.c \
    $(TRUSTLET_DIR)/../../nwd/common/DebugPrint.c \
    $(TRUSTLET_DIR)/../../nwd/common/TLV.c \
    $(TRUSTLET_DIR)/base64/base64.c\
    $(TRUSTLET_DIR)/crypto/rsa/rsa_gen.c \
    $(TRUSTLET_DIR)/crypto/rsa/rsa.c \
    $(TRUSTLET_DIR)/crypto/ec/ec_key.c \
    $(TRUSTLET_DIR)/crypto/ec/ec_lib.c \
    $(TRUSTLET_DIR)/crypto/ec/ec_curve.c \
    $(TRUSTLET_DIR)/crypto/ec/ec_cvt.c \
    $(TRUSTLET_DIR)/crypto/ec/ec_mult.c \
    $(TRUSTLET_DIR)/crypto/ec/ecp_mont.c \
    $(TRUSTLET_DIR)/crypto/ec/ecp_smpl.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_ctx.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_prime.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_lib.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_add.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_mul.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_gcd.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_word.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_asm.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_shift.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_mod.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_div.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_rand.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_exp.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_mont.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_sqr.c \
    $(TRUSTLET_DIR)/crypto/bn/bn_recp.c \
    $(TRUSTLET_DIR)/crypto/rand/rand_lib.c \
    $(TRUSTLET_DIR)/crypto/hash/sha512.c \

# All Target
all: pre-build main-build

# Main-build Target
main-build: $(TRUSTLET_ELF) secondary-outputs
# Tool invocations
$(TRUSTLET_ELF): $(SRC)
	@echo 'Building target: $@'
	@echo 'Invoking: GCC C Linker'
	$(CC) $(CFLAGS) $(INC_32) $(INC_64) -o $(TRUSTLET_ELF) $(SRC) $(INCLUDE_DIRS)
	@echo 'Finished building target: $@'
	@echo ' '
	$(MAKE) --no-print-directory post-build
# Other Targets
clean:
	-$(RM) $(TRUSTLET_ELF) $(TRUSTLET_UUID)
	-@echo ' '

pre-build:
#	-rm mldap_TA.elf.signed
	-@echo ' '

post-build:
#	echo "Signing with samsung_drv"
#	$(TA_AUTH_SCRIPTS_PATH)/sign_file.sh -c $(TA_SIGN_CERT) -k $(TA_SIGN_KEY) -a $(DIST) $(DIST_UUID)
	$(STRIP) $(TRUSTLET_ELF)

ifneq ($(filter V4.0, $(TG_SDK_VER)),)
	-java -jar ${BLOWFISH_BASE}/tools/teegris_authority_scripts/signclient.jar -runtype $(MLDAP_RUNTYPE) -model $(MLDAP_MODEL) -input $(TRUSTLET_ELF) -output $(OUTPUT_DIR)/$(TRUSTLET_UUID)
endif 
ifneq ($(filter V3.0, $(TG_SDK_VER)),)
	-java -jar ${BLOWFISH_BASE}/tools/teegris_authority_scripts/signclient.jar -runtype $(MLDAP_RUNTYPE) -model $(MLDAP_MODEL) -input $(TRUSTLET_ELF) -output $(OUTPUT_DIR)/$(TRUSTLET_UUID)
endif 
ifneq ($(filter V2.0, $(TG_SDK_VER)),)
	-java -jar ${BLOWFISH_BASE}/tools/bf_authority_scripts/signclient.jar -runtype $(MLDAP_RUNTYPE) -model $(MLDAP_MODEL) -input $(TRUSTLET_ELF) -output $(OUTPUT_DIR)/$(TRUSTLET_UUID)
endif
	-@echo ' '

secondary-outputs:

.PHONY: all clean dependents
.SECONDARY: main-build pre-build post-build

-include ../makefile.targets
