Makefile.clean 3.0 KB

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