Makefile.clean 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # ==========================================================================
  2. # Cleaning up
  3. # ==========================================================================
  4. src := $(obj)
  5. PHONY := __clean
  6. __clean:
  7. # Shorthand for $(Q)$(MAKE) scripts/Makefile.clean obj=dir
  8. # Usage:
  9. # $(Q)$(MAKE) $(clean)=dir
  10. clean := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.clean obj
  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-n := $(patsubst %/,%,$(filter %/, $(obj-n)))
  21. subdir-n += $(__subdir-n)
  22. __subdir- := $(patsubst %/,%,$(filter %/, $(obj-)))
  23. subdir- += $(__subdir-)
  24. # Subdirectories we need to descend into
  25. subdir-ym := $(sort $(subdir-y) $(subdir-m))
  26. subdir-ymn := $(sort $(subdir-ym) $(subdir-n) $(subdir-))
  27. # Add subdir path
  28. subdir-ymn := $(addprefix $(obj)/,$(subdir-ymn))
  29. # Temporal work-around for U-Boot
  30. subdir-ymn := $(foreach f, $(subdir-ymn), \
  31. $(if $(wildcard $(srctree)/$f/Makefile),$f))
  32. # build a list of files to remove, usually relative to the current
  33. # directory
  34. __clean-files := $(extra-y) $(always) \
  35. $(targets) $(clean-files) \
  36. $(host-progs) \
  37. $(hostprogs-y) $(hostprogs-m) $(hostprogs-)
  38. __clean-files := $(filter-out $(no-clean-files), $(__clean-files))
  39. # as clean-files is given relative to the current directory, this adds
  40. # a $(obj) prefix, except for absolute paths
  41. __clean-files := $(wildcard \
  42. $(addprefix $(obj)/, $(filter-out /%, $(__clean-files))) \
  43. $(filter /%, $(__clean-files)))
  44. # as clean-dirs is given relative to the current directory, this adds
  45. # a $(obj) prefix, except for absolute paths
  46. __clean-dirs := $(wildcard \
  47. $(addprefix $(obj)/, $(filter-out /%, $(clean-dirs))) \
  48. $(filter /%, $(clean-dirs)))
  49. # ==========================================================================
  50. quiet_cmd_clean = CLEAN $(obj)
  51. cmd_clean = rm -f $(__clean-files)
  52. quiet_cmd_cleandir = CLEAN $(__clean-dirs)
  53. cmd_cleandir = rm -rf $(__clean-dirs)
  54. __clean: $(subdir-ymn)
  55. ifneq ($(strip $(__clean-files)),)
  56. +$(call cmd,clean)
  57. endif
  58. ifneq ($(strip $(__clean-dirs)),)
  59. +$(call cmd,cleandir)
  60. endif
  61. ifneq ($(strip $(clean-rule)),)
  62. +$(clean-rule)
  63. endif
  64. @:
  65. # ===========================================================================
  66. # Generic stuff
  67. # ===========================================================================
  68. # Descending
  69. # ---------------------------------------------------------------------------
  70. PHONY += $(subdir-ymn)
  71. $(subdir-ymn):
  72. $(Q)$(MAKE) $(clean)=$@
  73. # If quiet is set, only print short version of command
  74. cmd = @$(if $($(quiet)cmd_$(1)),echo ' $($(quiet)cmd_$(1))' &&) $(cmd_$(1))
  75. # Declare the contents of the .PHONY variable as phony. We keep that
  76. # information in a variable se we can use it in if_changed and friends.
  77. .PHONY: $(PHONY)