1##########################################################
2## Common mk file used for Android to compile and       ##
3## integrate OP-TEE related components                  ##
4## Following flags need to be defined in optee*.mk      ##
5##    OPTEE_OS_DIR                                      ##
6##    OPTEE_TA_TARGETS                                  ##
7##    OPTEE_CFG_ARM64_CORE                              ##
8##    OPTEE_PLATFORM                                    ##
9##    OPTEE_PLATFORM_FLAVOR                             ##
10##    OPTEE_EXTRA_FLAGS (optional)                      ##
11##    OPTEE_EXTRA_TA_FLAGS (optional)                   ##
12## And BUILD_OPTEE_MK needs to be defined in optee*.mk  ##
13## to point to this file                                ##
14##                                                      ##
15## local_module needs to be defined before including    ##
16## this file to build TAs                               ##
17##                                                      ##
18##########################################################
19
20##########################################################
21## define common variables, like TA_DEV_KIT_DIR         ##
22##########################################################
23TOP_ROOT_ABS := $(realpath $(TOP))
24
25PREBUILT_MAKE ?= prebuilts/build-tools/linux-x86/bin/make
26# we need this check because the Pie build does not have
27# this prebuilt make tool
28ifneq (,$(wildcard $(PREBUILT_MAKE)))
29# for master and versions which has prebuilt make
30HOST_MAKE := $(PREBUILT_MAKE)
31
32# The AOSP build tool is not the regular make,
33# that it adds -jN to $(MAKE), and that we should preserve
34# the flag or we would lose parallel build
35# The MAKE is redefined here in AOSP ckati:
36#   https://android.googlesource.com/platform/build/kati/+/master/main.cc#100
37ifneq (,$(filter -j%, $(MAKE)))
38HOST_MAKE += $(filter -j%, $(MAKE))
39endif
40
41else
42# For P and old versions which does not have prebuilt make,
43# let's use MAKE as what we did before
44HOST_MAKE := $(MAKE)
45endif
46
47
48# OPTEE_OUT_DIR could be exported explicitly
49# if PRODUCT_OUT is not the default out directory in aosp workspace
50OPTEE_OUT_DIR ?= $(PRODUCT_OUT)/optee
51ABS_OPTEE_OUT_DIR ?= $(realpath $(PRODUCT_OUT))/optee
52OPTEE_TA_OUT_DIR ?= $(OPTEE_OUT_DIR)/ta
53ABS_OPTEE_TA_OUT_DIR ?= $(ABS_OPTEE_OUT_DIR)/ta
54# Set so that OP-TEE clients can find the installed dev-kit, which
55# depends on platform and its OP-TEE word-size.
56OPTEE_OS_OUT_DIR ?= $(OPTEE_OUT_DIR)/arm-plat-$(OPTEE_PLATFORM)
57ABS_OPTEE_OS_OUT_DIR := $(ABS_OPTEE_OUT_DIR)/arm-plat-$(OPTEE_PLATFORM)
58TA_DEV_KIT_DIR := $(ABS_OPTEE_OS_OUT_DIR)/export-${OPTEE_TA_TARGETS}
59
60CROSS_COMPILE64 ?= $(TOP_ROOT_ABS)/$(TARGET_TOOLS_PREFIX)
61CROSS_COMPILE_LINE := CROSS_COMPILE64="$(CROSS_COMPILE64)"
62ifneq ($(strip $($(combo_2nd_arch_prefix)TARGET_TOOLS_PREFIX)),)
63CROSS_COMPILE32 ?= $(TOP_ROOT_ABS)/$($(combo_2nd_arch_prefix)TARGET_TOOLS_PREFIX)
64CROSS_COMPILE_LINE += CROSS_COMPILE32="$(CROSS_COMPILE32)"
65endif
66
67OPTEE_BIN := $(OPTEE_OS_OUT_DIR)/core/tee.bin
68
69$(OPTEE_BIN) : $(sort $(shell find -L $(OPTEE_OS_DIR)))
70
71###########################################################
72## define making rules for $(OPTEE_BIN) target, and add  ##
73## condition check to make it only be defined once       ##
74## even though this mk file might be included multiple   ##
75## times. The process to generate $(OPTEE_BIN) file will ##
76## generate the header files under                       ##
77## $(TA_DEV_KIT_DIR)/host_include too.                   ##
78## And the $(OPTEE_BIN) will be used as dependency for   ##
79## other projects                                        ##
80###########################################################
81ifneq (true,$(BUILD_OPTEE_OS_DEFINED))
82BUILD_OPTEE_OS_DEFINED := true
83$(OPTEE_BIN):
84	@echo "Start building optee_os..."
85	+$(HOST_MAKE) -C $(TOP_ROOT_ABS)/$(OPTEE_OS_DIR) \
86		O=$(ABS_OPTEE_OS_OUT_DIR) \
87		CFG_USER_TA_TARGETS=$(OPTEE_TA_TARGETS) \
88		CFG_ARM64_core=$(OPTEE_CFG_ARM64_CORE) \
89		PLATFORM=$(OPTEE_PLATFORM) \
90		PLATFORM_FLAVOR=$(OPTEE_PLATFORM_FLAVOR) \
91		$(CROSS_COMPILE_LINE) \
92		$(OPTEE_EXTRA_FLAGS)
93	@echo "Finished building optee_os..."
94
95endif
96
97##########################################################
98## Lines for building TAs automatically                 ##
99## will only be included in Android.mk for TAs          ##
100## local_module:                                        ##
101##     need to be defined before include for this       ##
102##########################################################
103ifneq (false,$(INCLUDE_FOR_BUILD_TA))
104include $(CLEAR_VARS)
105
106LOCAL_MODULE := $(local_module)
107LOCAL_PREBUILT_MODULE_FILE := $(OPTEE_TA_OUT_DIR)/$(LOCAL_MODULE)
108LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR)/lib/optee_armtz
109LOCAL_MODULE_CLASS := EXECUTABLES
110LOCAL_MODULE_TAGS := optional
111
112TA_TMP_DIR := $(subst /,_,$(LOCAL_PATH))
113TA_TMP_FILE := $(OPTEE_TA_OUT_DIR)/$(TA_TMP_DIR)/$(LOCAL_MODULE)
114$(LOCAL_PREBUILT_MODULE_FILE): $(TA_TMP_FILE)
115	@mkdir -p $(dir $@)
116	cp -vf $< $@
117
118TA_TMP_FILE_DEPS :=
119ifneq ($(local_module_deps), )
120$(foreach dep,$(local_module_deps), $(eval TA_TMP_FILE_DEPS += $(TARGET_OUT_VENDOR)/lib/optee_armtz/$(dep)))
121endif
122$(TA_TMP_FILE): $(TA_TMP_FILE_DEPS)
123$(TA_TMP_FILE): PRIVATE_TA_SRC_DIR := $(LOCAL_PATH)
124$(TA_TMP_FILE): PRIVATE_TA_TMP_FILE := $(TA_TMP_FILE)
125$(TA_TMP_FILE): PRIVATE_TA_TMP_DIR := $(TA_TMP_DIR)
126$(TA_TMP_FILE): $(OPTEE_BIN)
127	@echo "Start building TA for $(PRIVATE_TA_SRC_DIR) $(PRIVATE_TA_TMP_FILE)..."
128	+$(HOST_MAKE) -C $(TOP_ROOT_ABS)/$(PRIVATE_TA_SRC_DIR) O=$(ABS_OPTEE_TA_OUT_DIR)/$(PRIVATE_TA_TMP_DIR) \
129		TA_DEV_KIT_DIR=$(TA_DEV_KIT_DIR) \
130		$(CROSS_COMPILE_LINE) \
131		$(OPTEE_EXTRA_TA_FLAGS)
132	@echo "Finished building TA for $(PRIVATE_TA_SRC_DIR) $(PRIVATE_TA_TMP_FILE)..."
133
134include $(BUILD_PREBUILT)
135local_module_deps :=
136endif
137