developer | e5732cc | 2022-03-24 10:55:08 +0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright (c) 2022, MediaTek Inc. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | |
| 7 | # Get local directory path |
| 8 | define GET_LOCAL_DIR |
| 9 | $(patsubst %/,%,$(dir $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))) |
| 10 | endef |
| 11 | |
| 12 | # Clear module source variable |
| 13 | define CLEAR_LOCAL_SRCS |
| 14 | $(eval $(1) :=) |
| 15 | endef |
| 16 | |
| 17 | define EXPAND_SUB_MAKEFILE |
| 18 | include $(S) |
| 19 | endef |
| 20 | |
| 21 | # Expand sub rules.mk |
| 22 | define INCLUDE_MAKEFILE |
| 23 | $(eval MODULES_SUB_MAKEFILE := $(patsubst %,%/rules.mk,$(1))) |
| 24 | $(foreach S,$(MODULES_SUB_MAKEFILE),$(eval $(EXPAND_SUB_MAKEFILE))) |
| 25 | endef |
| 26 | |
| 27 | # Determine option variable is defined or not then define it |
| 28 | define add_defined_option |
| 29 | ifdef $(1) |
| 30 | ifeq ($(findstring $(value $(1)), $(uppercase_table)),) |
| 31 | DEFINES += -D$(1)$(if $(value $(1)),=$(value $(1)),) |
| 32 | else |
| 33 | ifeq ($(strip $(value $(1))),y) |
| 34 | DEFINES += -D$(1)$(if $(value $(1)),=1,) |
| 35 | endif |
| 36 | endif |
| 37 | endif |
| 38 | endef |
| 39 | |
| 40 | define EXPAND_RULES_MAKEFILE |
| 41 | LOCAL_SRCS-y := |
| 42 | MODULE := |
| 43 | SUB_RULES-y := |
| 44 | include $(S) |
| 45 | endef |
| 46 | |
| 47 | # INCLUDE_MODULES macro expand included modules rules.mk |
| 48 | # Arguments: |
| 49 | # $(1) = MODULES variables |
| 50 | define 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))) |
| 54 | endef |
| 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) |
| 60 | define MAKE_LOCALS |
| 61 | $(eval $(call uppercase,$(2))_SOURCES += $(1)) |
| 62 | endef |
| 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 |
| 68 | define MAKE_LINKERFILE |
| 69 | $(eval EXTRA_GENERATED_LINKER_SCRIPT += $(BUILD_PLAT)/$(2)/$(patsubst %.ld.S,%.ld,$(notdir $(1)))) |
| 70 | endef |
| 71 | |
| 72 | # MAKE_LINKERFILE_ITER call MAKE_LINKERFILE iteratively |
| 73 | # $(1) = linker script source file |
| 74 | # $(2) = BL stage |
| 75 | define MAKE_LINKERFILE_ITER |
| 76 | $(eval $(foreach link_src,$(1),$(call MAKE_LINKERFILE,$(link_src),$(2)))) |
| 77 | endef |
| 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) |
| 83 | define 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)))) |
| 87 | endef |
| 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 |
| 95 | define 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 | |
| 113 | libraries: $(OBJS_TEMP) |
| 114 | endef |
| 115 | |
| 116 | # Include MTK configuration files |
| 117 | |
| 118 | # MTK makefile variables |
developer | d7e5948 | 2022-08-09 19:37:25 +0800 | [diff] [blame] | 119 | ifeq (${COREBOOT},1) |
| 120 | MTK_COMMON_CFG := $(MTK_PLAT)/common/coreboot_config.mk |
| 121 | else |
| 122 | MTK_COMMON_CFG := $(MTK_PLAT)/common/common_config.mk |
| 123 | endif |
developer | e5732cc | 2022-03-24 10:55:08 +0800 | [diff] [blame] | 124 | MTK_PLAT := plat/mediatek |
| 125 | MTK_PLAT_SOC := ${MTK_PLAT}/${MTK_SOC} |
developer | e5732cc | 2022-03-24 10:55:08 +0800 | [diff] [blame] | 126 | MTK_PLAT_CFG := $(MTK_PLAT_SOC)/plat_config.mk |
| 127 | MTK_PROJECT_CFG := $(MTK_PLAT)/project/$(PLAT)/project_config.mk |
| 128 | MTK_OPTIONS := $(MTK_PLAT)/build_helpers/options.mk |
| 129 | MTK_COND_EVAL := $(MTK_PLAT)/build_helpers/conditional_eval_options.mk |
| 130 | |
| 131 | # Indicate which BL should be built in command line |
developer | e5732cc | 2022-03-24 10:55:08 +0800 | [diff] [blame] | 132 | ifeq (${NEED_BL32},yes) |
| 133 | MTK_BL := bl32 |
developer | 6d8e8f8 | 2022-06-20 10:25:35 +0800 | [diff] [blame] | 134 | else |
| 135 | MTK_BL := bl31 |
developer | e5732cc | 2022-03-24 10:55:08 +0800 | [diff] [blame] | 136 | endif |
| 137 | # Include common, platform, board level config |
| 138 | include $(MTK_COMMON_CFG) |
| 139 | include $(MTK_PLAT_CFG) |
| 140 | -include $(MTK_PROJECT_CFG) |
| 141 | include $(MTK_COND_EVAL) |
| 142 | include $(MTK_OPTIONS) |