Makefile.clean 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # ==========================================================================
  2. # Cleaning up
  3. # ==========================================================================
  4. src := $(obj)
  5. PHONY := __clean
  6. __clean:
  7. include scripts/Kbuild.include
  8. # The filename Kbuild has precedence over Makefile
  9. kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
  10. include $(if $(wildcard $(kbuild-dir)/Kbuild), $(kbuild-dir)/Kbuild, $(kbuild-dir)/Makefile)
  11. # Figure out what we need to build from the various variables
  12. # ==========================================================================
  13. __subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
  14. subdir-y += $(__subdir-y)
  15. __subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m)))
  16. subdir-m += $(__subdir-m)
  17. __subdir- := $(patsubst %/,%,$(filter %/, $(obj-)))
  18. subdir- += $(__subdir-)
  19. # Subdirectories we need to descend into
  20. subdir-ym := $(sort $(subdir-y) $(subdir-m))
  21. subdir-ymn := $(sort $(subdir-ym) $(subdir-))
  22. # Add subdir path
  23. subdir-ymn := $(addprefix $(obj)/,$(subdir-ymn))
  24. # build a list of files to remove, usually relative to the current
  25. # directory
  26. __clean-files := $(extra-y) $(extra-m) $(extra-) \
  27. $(always) $(targets) $(clean-files) \
  28. $(host-progs) \
  29. $(hostprogs-y) $(hostprogs-m) $(hostprogs-)
  30. __clean-files := $(filter-out $(no-clean-files), $(__clean-files))
  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. # 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)