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# ========================================================================== 16# Cleaning up 17# ========================================================================== 18 19src := $(obj) 20 21PHONY := __clean 22__clean: 23 24$(cur_makefile): ; 25 26# Shorthand for $(Q)$(MAKE) scripts/Makefile.clean obj=dir 27# Usage: 28# $(Q)$(MAKE) $(clean)=dir 29clean := -f $(srctree)/scripts/clean.mk obj 30 31kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src)) 32include $(kbuild-dir)/Makefile 33 34# Figure out what we need to build from the various variables 35# ========================================================================== 36 37__subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y))) 38subdir-y += $(__subdir-y) 39__subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m))) 40subdir-m += $(__subdir-m) 41__subdir- := $(patsubst %/,%,$(filter %/, $(obj-))) 42subdir- += $(__subdir-) 43 44# Subdirectories we need to descend into 45 46subdir-ym := $(sort $(subdir-y) $(subdir-m)) 47subdir-ymn := $(sort $(subdir-ym) $(subdir-)) 48 49obj-ymn := $(obj-y) $(obj-m) $(obj-) 50lib-ymn := $(lib-y) $(lib-m) $(lib-) 51 52archive-ymn := $(filter %.a, $(obj-ymn)) 53archive-src-ymn := $(foreach m, $(archive-ymn), $(if $(wildcard $(addprefix $(srctree)/$(src)/,$($(m:.a=-y):.o=.*))), $(m))) 54archive-obj-ymn := $(foreach m, $(archive-src-ymn), $($(m:.a=-y))) 55 56obj-ymn += $(filter %.o %.a, $(archive-obj-ymn)) 57subdir-ymn += $(filter %/, $(archive-obj-ymn)) 58 59# if $(foo-objs) exists, foo.o is a composite object 60multi-used-ymn := $(sort $(foreach m,$(obj-ymn), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m)))) 61multi-objs-ymn := $(foreach m, $(multi-used-ymn), $($(m:.o=-objs)) $($(m:.o=-y))) 62 63multi-objs-subdir-ymn := $(patsubst %/,%,$(filter %/, $(multi-objs-ymn))) 64subdir-ymn += $(multi-objs-subdir-ymn) 65multi-objs-ymn := $(filter-out %/, $(multi-objs-ymn)) 66 67obj-ymn := $(filter-out %/, $(obj-ymn)) 68obj-ymn += $(addsuffix /built-in.o, $(subdir-ymn)) 69obj-ymn += $(addsuffix /built-in.a, $(subdir-ymn)) 70 71extra-ymn := $(extra-y) $(extra-m) $(extra-) 72 73# build a list of files to remove, usually relative to the current 74# directory 75 76__clean-files := $(obj-ymn) $(lib-ymn) $(multi-used-ymn) \ 77 $(multi-objs-ymn) $(extra-ymn) \ 78 built-in.o built-in.a lib.a \ 79 $(always) $(targets) $(clean-files) 80 81__clean-files := $(filter-out $(no-clean-files), $(__clean-files)) 82 83# clean-files is given relative to the current directory, unless it 84# starts with $(objtree)/ (which means "./", so do not add "./" unless 85# you want to delete a file from the toplevel object directory). 86 87__clean-files := $(addprefix $(obj)/, $(filter-out $(objtree)/%, $(__clean-files))) \ 88 $(filter $(objtree)/%, $(__clean-files)) 89 90# same as clean-files 91 92__clean-dirs := $(addprefix $(obj)/, $(filter-out $(objtree)/%, $(clean-dirs))) \ 93 $(filter $(objtree)/%, $(clean-dirs)) 94 95# Add subdir path 96subdir-ymn := $(addprefix $(obj)/,$(subdir-ymn)) 97obj-ymn := $(addprefix $(obj)/,$(obj-ymn)) 98lib-ymn := $(addprefix $(obj)/,$(lib-ymn)) 99multi-used-ymn := $(addprefix $(obj)/,$(multi-used-ymn)) 100multi-objs-ymn := $(addprefix $(obj)/,$(multi-objs-ymn)) 101 102SUBMODS := $(filter $(obj)/%,$(SUBMODS)) 103 104ifneq ($(SUBMODS),) 105include scripts/submods.mk 106 107buildsubdir-ymn := $(call get_subdirs,$(subdir-ymn),$(SUBMODS)) 108__full-clean-files := $(__clean-files) 109__clean-files := $(filter $(SUBMODS),$(__full-clean-files)) 110__clean-files += $(obj)/built-in.o $(obj)/built-in.a $(obj)/lib.a 111__clean-files += $(foreach m,$(multi-used-ymn),$(if $(filter $(SUBMODS),$(call get_multi_objs,$m))),$m,) 112 113# Check for invalid targets 114errobj-ymn := $(SUBMODS) 115errobj-ymn := $(filter-out $(buildsubdir-ymn) $(addsuffix /%,$(buildsubdir-ymn)),$(errobj-ymn)) 116errobj-ymn := $(filter-out $(__clean-files),$(errobj-ymn)) 117errobj-ymn := $(filter-out $(foreach m,$(multi-used-ymn),$(call get_multi_objs,$m)),$(errobj-ymn)) 118ifneq ($(errobj-ymn),) 119$(warning Unknown targets in [$(obj)]: $(errobj-ymn)) 120endif 121 122else # SUBMODS 123 124ifeq ($(ALLCLEAN),1) 125RM_ALL := 1 126ifneq ($(KBUILD_OUTPUT),) 127RM_DIR := 1 128endif 129endif 130 131ifeq ($(RM_DIR),1) 132buildsubdir-ymn := 133else 134buildsubdir-ymn := $(subdir-ymn) 135endif 136 137endif # SUBMODS 138 139# Include intermediate files 140include scripts/include.mk 141__clean-files += $(foreach f,$(__clean-files),$(call get_depfile_name,$f)) 142 143#$(info RM_DIR=$(RM_DIR), RM_ALL=$(RM_ALL)) 144#$(info buildsubdir-ymn=$(buildsubdir-ymn)) 145#$(info __clean-files=$(__clean-files)) 146 147__clean-files := $(wildcard $(__clean-files)) 148__clean-dirs := $(wildcard $(__clean-dirs)) 149 150# ========================================================================== 151 152ifeq ($(RM_DIR),1) 153 154quiet_cmd_clean = RMDIR $(obj) 155 cmd_clean = $(call CMDRMDIR,$(obj)) 156 157else # RM_DIR 158 159quiet_cmd_clean = CLEAN $(obj) 160ifeq ($(RM_ALL),1) 161 cmd_clean = $(call CMDRMFILER,$(obj),*.o *.a *.s *.d) 162else 163 cmd_clean = $(call CMDRMFILE,$(__clean-files)) 164endif 165quiet_cmd_cleandir = CLEAN $(__clean-dirs) 166 cmd_cleandir = $(call CMDRMDIR,$(__clean-dirs)) 167 168endif # RM_DIR 169 170 171__clean: $(buildsubdir-ymn) 172 @: 173ifeq ($(RM_DIR),1) 174 +$(call cmd,clean) 175else # RM_DIR 176ifeq ($(RM_ALL),1) 177 +$(call cmd,clean) 178else 179ifneq ($(strip $(__clean-files)),) 180 +$(call cmd,clean) 181endif 182endif 183ifneq ($(strip $(__clean-dirs)),) 184 +$(call cmd,cleandir) 185endif 186endif # RM_DIR 187 188 189# =========================================================================== 190# Generic stuff 191# =========================================================================== 192 193# Descending 194# --------------------------------------------------------------------------- 195 196PHONY += $(subdir-ymn) 197$(subdir-ymn): 198 $(Q)$(MAKE) $(clean)=$@ 199 200 201# Declare the contents of the .PHONY variable as phony. We keep that 202# information in a variable se we can use it in if_changed and friends. 203 204.PHONY: $(PHONY) 205