blob: fc3876e71f984efe853a17fcbbbb44693684e158 [file] [log] [blame]
developere5732cc2022-03-24 10:55:08 +08001#
2# Copyright (c) 2022, MediaTek Inc. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6
7# Get local directory path
8define GET_LOCAL_DIR
9$(patsubst %/,%,$(dir $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))))
10endef
11
12# Clear module source variable
13define CLEAR_LOCAL_SRCS
14$(eval $(1) :=)
15endef
16
17define EXPAND_SUB_MAKEFILE
18include $(S)
19endef
20
21# Expand sub rules.mk
22define INCLUDE_MAKEFILE
23$(eval MODULES_SUB_MAKEFILE := $(patsubst %,%/rules.mk,$(1)))
24$(foreach S,$(MODULES_SUB_MAKEFILE),$(eval $(EXPAND_SUB_MAKEFILE)))
25endef
26
27# Determine option variable is defined or not then define it
28define add_defined_option
29ifdef $(1)
30ifeq ($(findstring $(value $(1)), $(uppercase_table)),)
31DEFINES += -D$(1)$(if $(value $(1)),=$(value $(1)),)
32else
33ifeq ($(strip $(value $(1))),y)
34DEFINES += -D$(1)$(if $(value $(1)),=1,)
35endif
36endif
37endif
38endef
39
40define EXPAND_RULES_MAKEFILE
41LOCAL_SRCS-y :=
42MODULE :=
43SUB_RULES-y :=
44include $(S)
45endef
46
47# INCLUDE_MODULES macro expand included modules rules.mk
48# Arguments:
49# $(1) = MODULES variables
50define INCLUDE_MODULES
51$(eval MODULES_TEMP := $(1))
52$(eval MODULES_MAKEFILE := $(patsubst %,%/rules.mk,$(MODULES_TEMP)))
53$(foreach S,$(MODULES_MAKEFILE),$(eval $(EXPAND_RULES_MAKEFILE)))
54endef
55
56# MAKE_LOCALS expand module source file variable to BL${BL}_SOURCES
57# Arguments:
58# $(1) = source file
59# $(2) = BL stage (1, 2, 2u, 31, 32)
60define MAKE_LOCALS
61$(eval $(call uppercase,$(2))_SOURCES += $(1))
62endef
63
64# MAKE_LINKERFILE change linker script source file name to
65# target linker script
66# $(1) = linker script source file
67# $(2) = BL stage
68define MAKE_LINKERFILE
69$(eval EXTRA_GENERATED_LINKER_SCRIPT += $(BUILD_PLAT)/$(2)/$(patsubst %.ld.S,%.ld,$(notdir $(1))))
70endef
71
72# MAKE_LINKERFILE_ITER call MAKE_LINKERFILE iteratively
73# $(1) = linker script source file
74# $(2) = BL stage
75define MAKE_LINKERFILE_ITER
76$(eval $(foreach link_src,$(1),$(call MAKE_LINKERFILE,$(link_src),$(2))))
77endef
78
79# MAKE_LD_ITER generate the linker scripts using the C preprocessor iteratively
80# $(1) = output linker script
81# $(2) = input template
82# $(3) = BL stage (1, 2, 2u, 31, 32)
83define MAKE_LD_ITER
84$(eval index_list=$(shell seq $(words $(1))))
85$(eval $(foreach i, $(index_list), \
86$(call MAKE_LD,$(word $(i), $(1)), $(word $(i), $(2)),$(3))))
87endef
88
89# MAKE_MODULE reference MAKE_OBJS.
90# Create module folder under out/bl$(BL)/$(module)
91# Arguments:
92# $(1) = module name
93# $(2) = source file
94# $(3) = BL stage
95define MAKE_MODULE
96 $(eval MODULE := $(strip $(1)))
97 $(eval BUILD_DIR := ${BUILD_PLAT}/${3})
98 $(eval SOURCES := $(2))
99 $(eval OBJS_TEMP := $(addprefix $(BUILD_DIR)/$(MODULE)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
100 $(eval MODULE_OBJS += $(OBJS_TEMP))
101 # We use sort only to get a list of unique object directory names.
102 # ordering is not relevant but sort removes duplicates.
103 $(eval TEMP_OBJ_DIRS := $(sort $(dir ${OBJS_TEMP} ${LINKERFILE})))
104 # The $(dir ) function leaves a trailing / on the directory names
105 # Rip off the / to match directory names with make rule targets.
106 $(eval OBJ_DIRS := $(patsubst %/,%,$(TEMP_OBJ_DIRS)))
107
108$(eval $(foreach objd,${OBJ_DIRS},$(call MAKE_PREREQ_DIR,${objd},${BUILD_DIR})))
109${3}_dirs: | ${OBJ_DIRS}
110
111$(eval $(call MAKE_OBJS,$(BUILD_DIR)/$(MODULE),$(SOURCES),${3}))
112
113libraries: $(OBJS_TEMP)
114endef
115
116# Include MTK configuration files
117
118# MTK makefile variables
developerd7e59482022-08-09 19:37:25 +0800119ifeq (${COREBOOT},1)
120MTK_COMMON_CFG := $(MTK_PLAT)/common/coreboot_config.mk
121else
122MTK_COMMON_CFG := $(MTK_PLAT)/common/common_config.mk
123endif
developere5732cc2022-03-24 10:55:08 +0800124MTK_PLAT := plat/mediatek
125MTK_PLAT_SOC := ${MTK_PLAT}/${MTK_SOC}
developere5732cc2022-03-24 10:55:08 +0800126MTK_PLAT_CFG := $(MTK_PLAT_SOC)/plat_config.mk
127MTK_PROJECT_CFG := $(MTK_PLAT)/project/$(PLAT)/project_config.mk
128MTK_OPTIONS := $(MTK_PLAT)/build_helpers/options.mk
129MTK_COND_EVAL := $(MTK_PLAT)/build_helpers/conditional_eval_options.mk
130
131# Indicate which BL should be built in command line
developere5732cc2022-03-24 10:55:08 +0800132ifeq (${NEED_BL32},yes)
133MTK_BL := bl32
developer6d8e8f82022-06-20 10:25:35 +0800134else
135MTK_BL := bl31
developere5732cc2022-03-24 10:55:08 +0800136endif
137# Include common, platform, board level config
138include $(MTK_COMMON_CFG)
139include $(MTK_PLAT_CFG)
140-include $(MTK_PROJECT_CFG)
141include $(MTK_COND_EVAL)
142include $(MTK_OPTIONS)