blob: 656ccf676b33ab77ac3e29f2c1072f4d417008d9 [file] [log] [blame]
Jerome Forissier9ac870c2017-01-06 09:33:29 +01001# Cleaning directories generated during a previous build,
2# a failed previous build or even no previous build.
3# Track build directories through 'cleanfiles'.
4
5define _enum-parent-dirs
6$(if $(1),$(1) $(if $(filter / ./,$(dir $(1))),,$(call enum-parent-dirs,$(dir $(1)))),)
7endef
8
9define enum-parent-dirs
10$(call _enum-parent-dirs,$(patsubst %/,%,$(1)))
11endef
12
13define _reverse
14$(if $(1),$(call _reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1))
15endef
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).
22define cleandirs-for-rmdir
23$(wildcard $(addprefix $(O)/,$(call _reverse,$(sort
24 $(foreach d,$(patsubst $(O)/%,%,$(dir $(cleanfiles))),
25 $(call enum-parent-dirs,$(d)))))))
26endef