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))
15ifneq ("$(wildcard $(kbuild-dir))", "")
16include $(if $(wildcard $(kbuild-dir)/Kbuild), $(kbuild-dir)/Kbuild, $(kbuild-dir)/Makefile)
17endif
18
19# Figure out what we need to build from the various variables
20# ==========================================================================
21
22subdir-ymn := $(sort $(subdir-y) $(subdir-m) $(subdir-) \
23		$(patsubst %/,%, $(filter %/, $(obj-y) $(obj-m) $(obj-))))
24
25# Add subdir path
26
27subdir-ymn	:= $(addprefix $(obj)/,$(subdir-ymn))
28
29# build a list of files to remove, usually relative to the current
30# directory
31
32__clean-files	:= \
33	$(clean-files) $(targets) $(hostprogs) $(userprogs) \
34	$(extra-y) $(extra-m) $(extra-) \
35	$(always-y) $(always-m) $(always-) \
36	$(hostprogs-always-y) $(hostprogs-always-m) $(hostprogs-always-) \
37	$(userprogs-always-y) $(userprogs-always-m) $(userprogs-always-)
38
39# deprecated
40__clean-files	+= $(always) $(hostprogs-y) $(hostprogs-m) $(hostprogs-)
41
42__clean-files   := $(filter-out $(no-clean-files), $(__clean-files))
43
44# clean-files is given relative to the current directory, unless it
45# starts with $(objtree)/ (which means "./", so do not add "./" unless
46# you want to delete a file from the toplevel object directory).
47
48__clean-files   := $(wildcard                                               \
49		   $(addprefix $(obj)/, $(filter-out $(objtree)/%, $(__clean-files))) \
50		   $(filter $(objtree)/%, $(__clean-files)))
51
52# ==========================================================================
53
54quiet_cmd_clean = CLEAN   $(obj)
55      cmd_clean = rm -rf $(__clean-files)
56
57__clean: $(subdir-ymn)
58ifneq ($(strip $(__clean-files)),)
59	$(call cmd,clean)
60endif
61	@:
62
63
64# ===========================================================================
65# Generic stuff
66# ===========================================================================
67
68# Descending
69# ---------------------------------------------------------------------------
70
71PHONY += $(subdir-ymn)
72$(subdir-ymn):
73	$(Q)$(MAKE) $(clean)=$@
74
75.PHONY: $(PHONY)
76