blob: 9c978b7bbdf189b7cdb45ae95464b3f99bb3c899 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001# ==========================================================================
2# Cleaning up
3# ==========================================================================
4
5src := $(obj)
6
7.PHONY: __clean
8__clean:
9
Sam Ravnborg2a691472005-07-25 20:26:04 +000010# The filename Kbuild has precedence over Makefile
11include $(if $(wildcard $(srctree)/$(src)/Kbuild), \
12 $(srctree)/$(src)/Kbuild, $(srctree)/$(src)/Makefile)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14# Figure out what we need to build from the various variables
15# ==========================================================================
16
17__subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
18subdir-y += $(__subdir-y)
19__subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m)))
20subdir-m += $(__subdir-m)
21__subdir-n := $(patsubst %/,%,$(filter %/, $(obj-n)))
22subdir-n += $(__subdir-n)
23__subdir- := $(patsubst %/,%,$(filter %/, $(obj-)))
24subdir- += $(__subdir-)
25
26# Subdirectories we need to descend into
27
28subdir-ym := $(sort $(subdir-y) $(subdir-m))
29subdir-ymn := $(sort $(subdir-ym) $(subdir-n) $(subdir-))
30
31# Add subdir path
32
33subdir-ymn := $(addprefix $(obj)/,$(subdir-ymn))
34
35# build a list of files to remove, usually releative to the current
36# directory
37
38__clean-files := $(extra-y) $(EXTRA_TARGETS) $(always) \
39 $(targets) $(clean-files) \
40 $(host-progs) \
41 $(hostprogs-y) $(hostprogs-m) $(hostprogs-)
42
43# as clean-files is given relative to the current directory, this adds
44# a $(obj) prefix, except for absolute paths
45
46__clean-files := $(wildcard \
47 $(addprefix $(obj)/, $(filter-out /%, $(__clean-files))) \
48 $(filter /%, $(__clean-files)))
49
50# as clean-dirs is given relative to the current directory, this adds
51# a $(obj) prefix, except for absolute paths
52
53__clean-dirs := $(wildcard \
54 $(addprefix $(obj)/, $(filter-out /%, $(clean-dirs))) \
55 $(filter /%, $(clean-dirs)))
56
57# ==========================================================================
58
59quiet_cmd_clean = CLEAN $(obj)
60 cmd_clean = rm -f $(__clean-files)
61quiet_cmd_cleandir = CLEAN $(__clean-dirs)
62 cmd_cleandir = rm -rf $(__clean-dirs)
63
64
65__clean: $(subdir-ymn)
66ifneq ($(strip $(__clean-files)),)
67 +$(call cmd,clean)
68endif
69ifneq ($(strip $(__clean-dirs)),)
70 +$(call cmd,cleandir)
71endif
72ifneq ($(strip $(clean-rule)),)
73 +$(clean-rule)
74endif
75 @:
76
77
78# ===========================================================================
79# Generic stuff
80# ===========================================================================
81
82# Descending
83# ---------------------------------------------------------------------------
84
85.PHONY: $(subdir-ymn)
86$(subdir-ymn):
87 $(Q)$(MAKE) $(clean)=$@
88
89# If quiet is set, only print short version of command
90
91cmd = @$(if $($(quiet)cmd_$(1)),echo ' $($(quiet)cmd_$(1))' &&) $(cmd_$(1))
92
93# Shorthand for $(Q)$(MAKE) scripts/Makefile.clean obj=dir
94# Usage:
95# $(Q)$(MAKE) $(clean)=dir
96clean := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.clean obj