Makefile.clean 2.9 KB

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