1# Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13cur_makefile := $(lastword $(MAKEFILE_LIST)) 14 15$(cur_makefile): ; 16 17# add by jianyang@bestechnic.com for different target build $(LIB_NAME)-$(T).a 18ifeq ($(FORCE_TO_USE_TARGET_LIB),1) 19 20ifneq ($(SDK),1) 21define LIB_REPLACE 22$(1)-$(T)-y := $($(1)-y) 23endef 24$(foreach lib,$(filter %.a,$(obj-y)),$(eval $(call LIB_REPLACE,$(subst .a,,$(lib))))) 25endif 26 27obj-dir-new-y := $(filter-out %.a,$(obj-y)) 28obj-lib-new-y := $(foreach lib, $(filter %.a,$(obj-y)), $(subst .a,-$(T).a,$(lib))) 29obj-y := $(obj-lib-new-y) $(obj-dir-new-y) 30endif 31 32 33# Backward compatibility 34asflags-y += $(EXTRA_AFLAGS) 35ccflags-y += $(EXTRA_CFLAGS) 36cppflags-y += $(EXTRA_CPPFLAGS) 37ldflags-y += $(EXTRA_LDFLAGS) 38 39# 40# flags that take effect in sub directories 41export KBUILD_SUBDIR_ASFLAGS := $(KBUILD_SUBDIR_ASFLAGS) $(subdir-asflags-y) 42export KBUILD_SUBDIR_CCFLAGS := $(KBUILD_SUBDIR_CCFLAGS) $(subdir-ccflags-y) 43 44# Figure out what we need to build from the various variables 45# =========================================================================== 46 47# Handle archives 48# --------------------------------------------------------------------------- 49 50# Extract archives from $(obj-y) 51archive-y := $(filter %.a, $(obj-y)) 52archive-custom-valid := $(filter $(archive-custom), $(archive-y)) 53 54ifeq ($(archive-y),) 55archive-src-y := 56archive-bin-y := 57else # archive-y 58ifeq ($(FORCE_TO_USE_LIB),1) 59archive-bin-y := $(archive-y) 60else 61# Filter out archive-custom-valid 62archive-y := $(filter-out $(archive-custom-valid), $(archive-y)) 63 64# Extract archives that have source files available 65get-archive-src = $(wildcard $(addprefix $(srctree)/$(src)/,$($(1:.a=-y):.o=.*))) 66archive-src-y := $(foreach m, $(archive-y), $(if $(call get-archive-src,$(m)), $(m))) 67 68# Extract archives that do NOT have source files 69archive-bin-y := $(filter-out $(archive-src-y), $(archive-y)) 70endif 71 72# If the source files of the archive (including archive-custom-valid) exist, build the source files directly 73obj-y := $(filter-out $(archive-src-y) $(archive-custom-valid), $(obj-y)) $(foreach m, $(archive-src-y) $(archive-custom-valid), $($(m:.a=-y))) 74endif # archive-y 75 76# Handle composite objects 77# --------------------------------------------------------------------------- 78 79# if $(foo-objs) exists, foo.o is a composite object 80multi-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m)))) 81 82# Build list of the parts of our composite objects, our composite 83# objects depend on those (obviously) 84multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y))) 85multi-objs-subdir := $(patsubst %/,%,$(filter %/, $(multi-objs-y))) 86multi-objs-archive := $(filter %.a, $(multi-objs-y)) 87multi-objs-y := $(patsubst %/, %/built-in$(built_in_suffix), $(multi-objs-y)) 88multi-objs := $(multi-objs-y) 89 90define multi_subdir_built_in 91$(foreach m, $(1), $(if $($(m:.o=$(strip $2))), $(eval $(m:.o=$(strip $2)) := $(patsubst %/, %/built-in$(built_in_suffix), $($(m:.o=$(strip $2))))))) 92endef 93 94ifneq ($(multi-objs-subdir),) 95$(call multi_subdir_built_in,$(multi-used-y),-objs) 96$(call multi_subdir_built_in,$(multi-used-y),-y) 97endif 98 99ifneq ($(multi-objs-archive),) 100ifneq ($(filter $(archive-custom), $(multi-objs-archive)),) 101$(error Composite objects cannot be built from customized archives) 102endif 103mobjs-archive-src-y := 104mobjs-archive-bin-y := 105mobjs-archive-y := $(multi-objs-archive) 106 107ifeq ($(FORCE_TO_USE_LIB),1) 108mobjs-archive-bin-y += $(mobjs-archive-y) 109else 110# Extract archives that have source files available 111mobjs-archive-src-y += $(foreach m, $(mobjs-archive-y), $(if $(wildcard $(addprefix $(srctree)/$(src)/,$($(m:.a=-y):.o=.*))), $(m))) 112 113# Extract archives that do NOT have source files 114mobjs-archive-bin-y := $(filter-out $(mobjs-archive-src-y), $(mobjs-archive-y)) 115endif 116 117# If the source files of the archive (including archive-custom-valid) exist, build the source files directly 118mobjs-obj-y := $(foreach m, $(mobjs-archive-src-y), $($(m:.a=-y))) 119 120archive-y += $(mobjs-archive-y) 121archive-bin-y += $(mobjs-archive-bin-y) 122archive-src-y += $(mobjs-archive-src-y) 123obj-y += $(mobjs-obj-y) 124endif 125 126# Handle objects in subdirs 127# --------------------------------------------------------------------------- 128# o if we encounter foo/ in $(obj-y), replace it by foo/built-in.o or .a 129# and add the directory to the list of dirs to descend into: $(subdir-y) 130 131__subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y))) 132subdir-y += $(__subdir-y) 133obj-y := $(patsubst %/, %/built-in$(built_in_suffix), $(obj-y)) 134 135# Subdirectories we need to descend into 136subdir-y := $(sort $(subdir-y) $(multi-objs-subdir)) 137 138# $(subdir-obj-y) is the list of objects in $(obj-y) which uses dir/ to 139# tell kbuild to descend 140subdir-obj-y := $(filter %/built-in$(built_in_suffix), $(obj-y) $(multi-objs-y)) 141 142# $(obj-dirs) is a list of directories that contain object files 143obj-dirs := $(dir $(multi-objs) $(obj-y)) 144 145# Replace multi-part objects by their individual parts, look at local dir only 146real-objs-y := $(foreach m, $(filter-out $(subdir-obj-y), $(obj-y)), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m))) $(extra-y) 147 148# Add subdir path 149 150extra-y := $(addprefix $(obj)/,$(extra-y)) 151always := $(addprefix $(obj)/,$(always)) 152targets := $(addprefix $(obj)/,$(targets)) 153obj-y := $(addprefix $(obj)/,$(obj-y)) 154lib-y := $(addprefix $(obj)/,$(lib-y)) 155subdir-obj-y := $(addprefix $(obj)/,$(subdir-obj-y)) 156real-objs-y := $(addprefix $(obj)/,$(real-objs-y)) 157multi-used-y := $(addprefix $(obj)/,$(multi-used-y)) 158multi-objs-y := $(addprefix $(obj)/,$(multi-objs-y)) 159subdir-y := $(addprefix $(obj)/,$(subdir-y)) 160obj-dirs := $(addprefix $(obj)/,$(obj-dirs)) 161archive-y := $(addprefix $(obj)/,$(archive-y)) 162archive-src-y := $(addprefix $(obj)/,$(archive-src-y)) 163archive-bin-y := $(addprefix $(obj)/,$(archive-bin-y)) 164archive-custom-valid := $(addprefix $(obj)/,$(archive-custom-valid)) 165 166 167# filter-out modules 168ifeq ($(FORCE_TO_FILTER_MODULE),1) 169extra-y := $(if $(filter-module-pattern),$(filter-out $(filter-module-pattern),$(extra-y)),$(extra-y)) 170always := $(if $(filter-module-pattern),$(filter-out $(filter-module-pattern),$(always)),$(always)) 171targets := $(if $(filter-module-pattern),$(filter-out $(filter-module-pattern),$(targets)),$(targets)) 172obj-y := $(if $(filter-module-pattern) $(filter-obj),$(filter-out $(filter-module-pattern) $(filter-obj),$(obj-y)),$(obj-y)) 173lib-y := $(if $(filter-module-pattern),$(filter-out $(filter-module-pattern),$(lib-y)),$(lib-y)) 174subdir-obj-y := $(if $(filter-module-pattern),$(filter-out $(filter-module-pattern),$(subdir-obj-y)),$(subdir-obj-y)) 175real-objs-y := $(if $(filter-module-pattern),$(filter-out $(filter-module-pattern),$(real-objs-y)),$(real-objs-y)) 176multi-used-y := $(if $(filter-module-pattern),$(filter-out $(filter-module-pattern),$(multi-used-y)),$(multi-used-y)) 177multi-objs-y := $(if $(filter-module-pattern),$(filter-out $(filter-module-pattern),$(multi-objs-y)),$(multi-objs-y)) 178subdir-y := $(if $(filter-module-pattern),$(filter-out $(filter-module-pattern),$(subdir-y)),$(subdir-y)) 179obj-dirs := $(if $(filter-module-pattern),$(filter-out $(filter-module-pattern),$(obj-dirs)),$(obj-dirs)) 180archive-y := $(if $(filter-module-pattern),$(filter-out $(filter-module-pattern),$(archive-y)),$(archive-y)) 181archive-src-y := $(if $(filter-module-pattern),$(filter-out $(filter-module-pattern),$(archive-src-y)),$(archive-src-y)) 182archive-bin-y := $(if $(filter-module-pattern),$(filter-out $(filter-module-pattern),$(archive-bin-y)),$(archive-bin-y)) 183archive-custom-valid := $(if $(filter-module-pattern),$(filter-out $(filter-module-pattern),$(archive-custom-y)),$(archive-custom-y)) 184endif 185 186orig_c_flags = $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(KBUILD_SUBDIR_CCFLAGS) \ 187 $(ccflags-y) $(CFLAGS_$(basetarget).o) 188_c_flags = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(orig_c_flags) $(C_ONLY_FLAGS)) 189_c++_flags = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(orig_c_flags) $(C++_ONLY_FLAGS)) 190_a_flags = $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(KBUILD_SUBDIR_ASFLAGS) \ 191 $(asflags-y) $(AFLAGS_$(basetarget).o) 192_cpp_flags = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(@F)) 193 194# If building the kernel in a separate objtree expand all occurrences 195# of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/'). 196 197ifeq ($(KBUILD_SRC),) 198__c_flags = $(_c_flags) 199__c++_flags = $(_c++_flags) 200__a_flags = $(_a_flags) 201__cpp_flags = $(_cpp_flags) 202else 203 204# -I$(obj) locates generated .h files 205# $(call addtree,-I$(obj)) locates .h files in srctree, from generated .c files 206# and locates generated .h files 207# FIXME: Replace both with specific CFLAGS* statements in the makefiles 208__c_flags = $(call addtree,-I$(obj)) $(call flags,_c_flags) 209__c++_flags = $(call addtree,-I$(obj)) $(call flags,_c++_flags) 210__a_flags = $(call flags,_a_flags) 211__cpp_flags = $(call flags,_cpp_flags) 212endif 213 214ifeq ($(WIN_PLAT),y) 215# Not to include system header files in dependency files, 216# for there are space characters in system path by default. 217depfile_flags = -MMD -MP -MF $(depfile) -MT $@ 218else 219depfile_flags = -MD -MP -MF $(depfile) -MT $@ 220endif 221 222c_flags = $(depfile_flags) $(NOSTDINC_FLAGS) $(__c_flags) 223 224c++_flags = $(depfile_flags) $(NOSTDINC_FLAGS) $(__c++_flags) 225 226a_flags = $(depfile_flags) $(NOSTDINC_FLAGS) $(__a_flags) 227 228a_cpp_flags = $(a_flags) 229 230cpp_flags = $(depfile_flags) $(NOSTDINC_FLAGS) $(__cpp_flags) 231 232ld_flags = $(LDFLAGS) $(ldflags-y) 233 234# Useful for describing the dependency of composite objects 235# Usage: 236# $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add) 237define multi_depend 238$(foreach m, $(notdir $1), \ 239 $(eval $(obj)/$m: \ 240 $(addprefix $(obj)/, $(foreach s, $3, $($(m:%$(strip $2)=%$(s))))))) 241endef 242 243# Commands useful for building a boot image 244# =========================================================================== 245# 246# Use as following: 247# 248# target: source(s) FORCE 249# $(if_changed,ld/objcopy/gzip) 250# 251# and add target to extra-y so that we know we have to 252# read in the saved command line 253 254# Linking 255# --------------------------------------------------------------------------- 256 257quiet_cmd_ld = LD $@ 258cmd_ld = $(LD) $(LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F)) \ 259 $(filter-out FORCE,$^) -o $@ 260 261# Objcopy 262# --------------------------------------------------------------------------- 263 264quiet_cmd_objcopy = OBJCOPY $@ 265cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@ 266 267