blob: 6c42b53c2c4f10f9cadb051b0630ea08aad93b90 [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
Jerome Forissier6331a052017-01-10 14:06:19 +010023$(eval _O:=$(if $(O),$(O),.))$(wildcard $(addprefix $(_O)/,$(call _reverse,
24 $(sort $(foreach d,$(patsubst $(_O)/%,%,$(dir $(cleanfiles))),
25 $(call enum-parent-dirs,$(d)))))))
Jerome Forissier9ac870c2017-01-06 09:33:29 +010026endef
Jerome Forissier30a44332017-02-07 14:30:32 +010027
28RMDIR := rmdir --ignore-fail-on-non-empty
Jerome Forissierbc0d2712017-10-25 11:31:25 +020029
30# Remove files with "rm -f".
31# Split (possibly huge) file list into more manageable lines
32# (200 files at a time), to minimize the odds of having:
33# "/bin/bash: Argument list too long"
34define do-rm-f
Jens Wiklander7ea0c702019-05-24 09:20:06 +020035 $(call _do-rm-f, $(wordlist 1, 200, $(1))) \
Jerome Forissierbc0d2712017-10-25 11:31:25 +020036 $(eval _tail := $(wordlist 201, $(words $(1)), $(1)))
37 $(if $(_tail), $(call do-rm-f, $(_tail)))
38endef
39
40define _do-rm-f
41 ${q}rm -f $1
42endef