Makefile.clean 2.9 KB

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