1# SPDX-License-Identifier: GPL-2.0
2# ==========================================================================
3# Cleaning up
4# ==========================================================================
5
6src := $(obj)
7
8PHONY := __clean
9__clean:
10
11include scripts/Kbuild.include
12
13# The filename Kbuild has precedence over Makefile
14kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
15include $(if $(wildcard $(kbuild-dir)/Kbuild), $(kbuild-dir)/Kbuild, $(kbuild-dir)/Makefile)
16
17# Figure out what we need to build from the various variables
18# ==========================================================================
19
20__subdir-y	:= $(patsubst %/,%,$(filter %/, $(obj-y)))
21subdir-y	+= $(__subdir-y)
22__subdir-m	:= $(patsubst %/,%,$(filter %/, $(obj-m)))
23subdir-m	+= $(__subdir-m)
24__subdir-	:= $(patsubst %/,%,$(filter %/, $(obj-)))
25subdir-		+= $(__subdir-)
26
27# Subdirectories we need to descend into
28
29subdir-ym	:= $(sort $(subdir-y) $(subdir-m))
30subdir-ymn      := $(sort $(subdir-ym) $(subdir-))
31
32# Add subdir path
33
34subdir-ymn	:= $(addprefix $(obj)/,$(subdir-ymn))
35
36# Temporal work-around for U-Boot
37
38subdir-ymn	:= $(foreach f, $(subdir-ymn), \
39				$(if $(wildcard $(srctree)/$f/Makefile),$f))
40
41# build a list of files to remove, usually relative to the current
42# directory
43
44__clean-files	:= $(extra-y) $(extra-m) $(extra-)       \
45		   $(always) $(targets) $(clean-files)   \
46		   $(hostprogs-y) $(hostprogs-m) $(hostprogs-) \
47		   $(hostlibs-y) $(hostlibs-m) $(hostlibs-) \
48		   $(hostcxxlibs-y) $(hostcxxlibs-m)
49
50__clean-files   := $(filter-out $(no-clean-files), $(__clean-files))
51
52# clean-files is given relative to the current directory, unless it
53# starts with $(objtree)/ (which means "./", so do not add "./" unless
54# you want to delete a file from the toplevel object directory).
55
56__clean-files   := $(wildcard                                               \
57		   $(addprefix $(obj)/, $(filter-out $(objtree)/%, $(__clean-files))) \
58		   $(filter $(objtree)/%, $(__clean-files)))
59
60# same as clean-files
61
62__clean-dirs    := $(wildcard                                               \
63		   $(addprefix $(obj)/, $(filter-out $(objtree)/%, $(clean-dirs)))    \
64		   $(filter $(objtree)/%, $(clean-dirs)))
65
66# ==========================================================================
67
68quiet_cmd_clean    = CLEAN   $(obj)
69      cmd_clean    = rm -f $(__clean-files)
70quiet_cmd_cleandir = CLEAN   $(__clean-dirs)
71      cmd_cleandir = rm -rf $(__clean-dirs)
72
73
74__clean: $(subdir-ymn)
75ifneq ($(strip $(__clean-files)),)
76	+$(call cmd,clean)
77endif
78ifneq ($(strip $(__clean-dirs)),)
79	+$(call cmd,cleandir)
80endif
81	@:
82
83
84# ===========================================================================
85# Generic stuff
86# ===========================================================================
87
88# Descending
89# ---------------------------------------------------------------------------
90
91PHONY += $(subdir-ymn)
92$(subdir-ymn):
93	$(Q)$(MAKE) $(clean)=$@
94
95.PHONY: $(PHONY)
96