Makefile.clean 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # ==========================================================================
  2. # Cleaning up
  3. # ==========================================================================
  4. src := $(obj)
  5. .PHONY: __clean
  6. __clean:
  7. # The filename Kbuild has precedence over Makefile
  8. include $(if $(wildcard $(srctree)/$(src)/Kbuild), \
  9. $(srctree)/$(src)/Kbuild, $(srctree)/$(src)/Makefile)
  10. # Figure out what we need to build from the various variables
  11. # ==========================================================================
  12. __subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
  13. subdir-y += $(__subdir-y)
  14. __subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m)))
  15. subdir-m += $(__subdir-m)
  16. __subdir-n := $(patsubst %/,%,$(filter %/, $(obj-n)))
  17. subdir-n += $(__subdir-n)
  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-n) $(subdir-))
  23. # Add subdir path
  24. subdir-ymn := $(addprefix $(obj)/,$(subdir-ymn))
  25. # build a list of files to remove, usually releative to the current
  26. # directory
  27. __clean-files := $(extra-y) $(EXTRA_TARGETS) $(always) \
  28. $(targets) $(clean-files) \
  29. $(host-progs) \
  30. $(hostprogs-y) $(hostprogs-m) $(hostprogs-)
  31. # as clean-files is given relative to the current directory, this adds
  32. # a $(obj) prefix, except for absolute paths
  33. __clean-files := $(wildcard \
  34. $(addprefix $(obj)/, $(filter-out /%, $(__clean-files))) \
  35. $(filter /%, $(__clean-files)))
  36. # as clean-dirs is given relative to the current directory, this adds
  37. # a $(obj) prefix, except for absolute paths
  38. __clean-dirs := $(wildcard \
  39. $(addprefix $(obj)/, $(filter-out /%, $(clean-dirs))) \
  40. $(filter /%, $(clean-dirs)))
  41. # ==========================================================================
  42. quiet_cmd_clean = CLEAN $(obj)
  43. cmd_clean = rm -f $(__clean-files)
  44. quiet_cmd_cleandir = CLEAN $(__clean-dirs)
  45. cmd_cleandir = rm -rf $(__clean-dirs)
  46. __clean: $(subdir-ymn)
  47. ifneq ($(strip $(__clean-files)),)
  48. +$(call cmd,clean)
  49. endif
  50. ifneq ($(strip $(__clean-dirs)),)
  51. +$(call cmd,cleandir)
  52. endif
  53. ifneq ($(strip $(clean-rule)),)
  54. +$(clean-rule)
  55. endif
  56. @:
  57. # ===========================================================================
  58. # Generic stuff
  59. # ===========================================================================
  60. # Descending
  61. # ---------------------------------------------------------------------------
  62. .PHONY: $(subdir-ymn)
  63. $(subdir-ymn):
  64. $(Q)$(MAKE) $(clean)=$@
  65. # If quiet is set, only print short version of command
  66. cmd = @$(if $($(quiet)cmd_$(1)),echo ' $($(quiet)cmd_$(1))' &&) $(cmd_$(1))
  67. # Shorthand for $(Q)$(MAKE) scripts/Makefile.clean obj=dir
  68. # Usage:
  69. # $(Q)$(MAKE) $(clean)=dir
  70. clean := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.clean obj