Makefile.clean 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # SPDX-License-Identifier: GPL-2.0
  2. # ==========================================================================
  3. # Cleaning up
  4. # ==========================================================================
  5. src := $(obj)
  6. PHONY := __clean
  7. __clean:
  8. include scripts/Kbuild.include
  9. # The filename Kbuild has precedence over Makefile
  10. kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
  11. include $(if $(wildcard $(kbuild-dir)/Kbuild), $(kbuild-dir)/Kbuild, $(kbuild-dir)/Makefile)
  12. # Figure out what we need to build from the various variables
  13. # ==========================================================================
  14. __subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
  15. subdir-y += $(__subdir-y)
  16. __subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m)))
  17. subdir-m += $(__subdir-m)
  18. __subdir- := $(patsubst %/,%,$(filter %/, $(obj-)))
  19. subdir- += $(__subdir-)
  20. # Subdirectories we need to descend into
  21. subdir-ym := $(sort $(subdir-y) $(subdir-m))
  22. subdir-ymn := $(sort $(subdir-ym) $(subdir-))
  23. # Add subdir path
  24. subdir-ymn := $(addprefix $(obj)/,$(subdir-ymn))
  25. # build a list of files to remove, usually relative to the current
  26. # directory
  27. __clean-files := $(extra-y) $(extra-m) $(extra-) \
  28. $(always) $(targets) $(clean-files) \
  29. $(host-progs) \
  30. $(hostprogs-y) $(hostprogs-m) $(hostprogs-) \
  31. $(hostlibs-y) $(hostlibs-m) $(hostlibs-) \
  32. $(hostcxxlibs-y) $(hostcxxlibs-m)
  33. __clean-files := $(filter-out $(no-clean-files), $(__clean-files))
  34. # clean-files is given relative to the current directory, unless it
  35. # starts with $(objtree)/ (which means "./", so do not add "./" unless
  36. # you want to delete a file from the toplevel object directory).
  37. __clean-files := $(wildcard \
  38. $(addprefix $(obj)/, $(filter-out $(objtree)/%, $(__clean-files))) \
  39. $(filter $(objtree)/%, $(__clean-files)))
  40. # same as clean-files
  41. __clean-dirs := $(wildcard \
  42. $(addprefix $(obj)/, $(filter-out $(objtree)/%, $(clean-dirs))) \
  43. $(filter $(objtree)/%, $(clean-dirs)))
  44. # ==========================================================================
  45. quiet_cmd_clean = CLEAN $(obj)
  46. cmd_clean = rm -f $(__clean-files)
  47. quiet_cmd_cleandir = CLEAN $(__clean-dirs)
  48. cmd_cleandir = rm -rf $(__clean-dirs)
  49. __clean: $(subdir-ymn)
  50. ifneq ($(strip $(__clean-files)),)
  51. +$(call cmd,clean)
  52. endif
  53. ifneq ($(strip $(__clean-dirs)),)
  54. +$(call cmd,cleandir)
  55. endif
  56. @:
  57. # ===========================================================================
  58. # Generic stuff
  59. # ===========================================================================
  60. # Descending
  61. # ---------------------------------------------------------------------------
  62. PHONY += $(subdir-ymn)
  63. $(subdir-ymn):
  64. $(Q)$(MAKE) $(clean)=$@
  65. # Declare the contents of the .PHONY variable as phony. We keep that
  66. # information in a variable se we can use it in if_changed and friends.
  67. .PHONY: $(PHONY)