# Normally this makefile shouldn't be called directly and we expect the output
# path to be on a certain location to fit together with the other OP-TEE
# gits and helper scripts.

include ../scripts/common.mk
out-dir := $(call strip-trailing-slashes-and-dots,$(O))
ifeq ($(out-dir),)
$(error invalid output directory (O=$(O)))
endif

include $(TA_DEV_KIT_DIR)/mk/conf.mk

# Prevent use of LDFLAGS from the environment. For example, yocto exports
# LDFLAGS that are suitable for the client applications, not for TAs
LDFLAGS=

TA_DIRS := create_fail_test \
	   crypt \
	   os_test_lib \
	   os_test_lib_dl \
	   os_test \
	   rpc_test \
	   sims \
	   miss \
	   sims_keepalive \
	   storage \
	   storage2 \
	   concurrent \
	   concurrent_large \
	   storage_benchmark \
	   crypto_perf \
	   socket \
	   supp_plugin \
	   large \
	   subkey1 \
	   subkey2

ifeq ($(CFG_SECURE_DATA_PATH),y)
TA_DIRS += sdp_basic
endif

ifeq ($(CFG_TA_BTI),y)
TA_DIRS += bti_test
endif

.PHONY: all
all: ta

.PHONY: ta
ta:

define add-ta-target
.PHONY: ta-$(1)
ta-$(1):
	$(MAKE) -C $(1) O=$(out-dir)/ta/$(1)

ta: ta-$(1)

endef

$(eval $(foreach dir,$(TA_DIRS),$(call add-ta-target,$(dir))))

ta-os_test: ta-os_test_lib ta-os_test_lib_dl

ifneq (,$(GP_PACKAGE))
define add-gp-ta-target
.PHONY: gp-ta-$(1)
gp-ta-$(1):
	$(MAKE) -C $(1)

ta: gp-ta-$(1)

endef

$(eval $(foreach f,$(dir $(wildcard ../host/xtest/gp-suite/TTAs_Internal_API_1_1_1/*/*/code_files/Makefile \
	../host/xtest/gp-suite/TTAs_Internal_API_1_1_1/*/*/*/code_files/Makefile)),$(call add-gp-ta-target,$(f))))
endif

# remove build directories including ta/<ta-name>/ directories.
# Note: $(out-dir) may be a relative path.
RMDIR := rmdir --ignore-fail-on-non-empty
define rm-build-subdir
echo `[ -d $1 ] && { cd $1; $(RMDIR) $(out-dir)/ta $(out-dir) 2> /dev/null; true; }` > /dev/null
endef

.PHONY: clean
clean:
	$(q)$(foreach dir,$(TA_DIRS), [ ! -d $(dir) ] || $(MAKE) -C $(dir) O=$(out-dir)/ta/$(dir) $@ &&) true
	$(q)$(foreach dir,$(TA_DIRS), $(call rm-build-subdir,$(dir));)
