Jerome Forissier | 9ac870c | 2017-01-06 09:33:29 +0100 | [diff] [blame^] | 1 | # Cleaning directories generated during a previous build, |
| 2 | # a failed previous build or even no previous build. |
| 3 | # Track build directories through 'cleanfiles'. |
| 4 | |
| 5 | define _enum-parent-dirs |
| 6 | $(if $(1),$(1) $(if $(filter / ./,$(dir $(1))),,$(call enum-parent-dirs,$(dir $(1)))),) |
| 7 | endef |
| 8 | |
| 9 | define enum-parent-dirs |
| 10 | $(call _enum-parent-dirs,$(patsubst %/,%,$(1))) |
| 11 | endef |
| 12 | |
| 13 | define _reverse |
| 14 | $(if $(1),$(call _reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1)) |
| 15 | endef |
| 16 | |
| 17 | # Returns the list of all existing output directories up to $(O) including all |
| 18 | # intermediate levels, in depth first order so that rmdir can process them in |
| 19 | # order. May return an empty string. |
| 20 | # Example: if cleanfiles is "foo/a/file1 foo/b/c/d/file2" and O=foo, this will |
| 21 | # return "foo/b/c/d foo/b/c foo/b foo/a" (assuming all exist). |
| 22 | define cleandirs-for-rmdir |
| 23 | $(wildcard $(addprefix $(O)/,$(call _reverse,$(sort |
| 24 | $(foreach d,$(patsubst $(O)/%,%,$(dir $(cleanfiles))), |
| 25 | $(call enum-parent-dirs,$(d))))))) |
| 26 | endef |