Skip to content

Commit 9ac870c

Browse files
committed
Make sure "make clean" removes everything "make" has created under $(O)
"make clean" would leave behind some files and many directories. Fix this by correctly tracking the files and directories created under $(O) during the build process: - Fix incorrect file names in $(cleanfiles) and add a few missing ones. - Introduce a makefile macro: $(cleandirs-for-rmdir), defined in a new file: mk/cleandirs.mk. It returns the list of directories that should be removed, given a list of files. The clean target removes the files, then all the directories in depth- first order. $(O) is also removed, if found to be empty. Note that a more straightforward approach was discussed in [1]: use "rm -rf $(O)/some_dir" and get rid of the whole file and directory tracking via $(cleanfiles) and $(cleandirs). Although it was agreed it would be safe, doing so would necessarily break the backward compatibility for build scripts relying on "make O=<some path>", due to the additional level ($(O)/some_dir). Finally, mk/cleandirs.mk is exported to the TA dev kit and the clean rule for the TAs is updated. [1] OP-TEE#1270 Signed-off-by: Jerome Forissier <[email protected]> Reviewed-by: Etienne Carriere <[email protected]> Reviewed-by: Jens Wiklander <[email protected]> Acked-by: Andy Green <[email protected]>
1 parent 608bd28 commit 9ac870c

File tree

7 files changed

+52
-14
lines changed

7 files changed

+52
-14
lines changed

Makefile

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ endif
3838
ARCH ?= arm
3939
PLATFORM ?= vexpress
4040
# Default value for PLATFORM_FLAVOR is set in plat-$(PLATFORM)/conf.mk
41-
O ?= out/$(ARCH)-plat-$(PLATFORM)
41+
ifeq ($O,)
42+
O := out
43+
out-dir := $(O)/$(ARCH)-plat-$(PLATFORM)
44+
else
45+
out-dir := $(O)
46+
endif
4247

4348
arch_$(ARCH) := y
4449

45-
ifneq ($O,)
46-
out-dir := $O
47-
endif
48-
4950
ifneq ($V,1)
5051
q := @
5152
cmd-echo := true
@@ -80,10 +81,15 @@ endef
8081
$(foreach t, $(ta-targets), $(eval $(call build-ta-target, $(t))))
8182
endif
8283

84+
include mk/cleandirs.mk
85+
8386
.PHONY: clean
8487
clean:
85-
@$(cmd-echo-silent) ' CLEAN .'
88+
@$(cmd-echo-silent) ' CLEAN $(out-dir)'
8689
${q}rm -f $(cleanfiles)
90+
${q}dirs="$(call cleandirs-for-rmdir)"; if [ "$$dirs" ]; then rmdir $$dirs; fi
91+
@if [ "$(out-dir)" != "$(O)" ]; then $(cmd-echo-silent) ' CLEAN $(O)'; fi
92+
${q}if [ -d "$(O)" ]; then rmdir --ignore-fail-on-non-empty $(O); fi
8793

8894
.PHONY: cscope
8995
cscope:

core/arch/arm/kernel/link.mk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ $(link-out-dir)/unpaged.o: $(link-out-dir)/unpaged_entries.txt
5656
`cat $(link-out-dir)/unpaged_entries.txt` \
5757
$(ldargs-unpaged-objs) -o $@
5858

59-
cleanfiles += $(link-out-dir)/text_unpaged.ld.S:
59+
cleanfiles += $(link-out-dir)/text_unpaged.ld.S
6060
$(link-out-dir)/text_unpaged.ld.S: $(link-out-dir)/unpaged.o
6161
@$(cmd-echo-silent) ' GEN $@'
6262
$(q)$(READELFcore) -a -W $< | ${AWK} -f ./scripts/gen_ld_text_sects.awk > $@
6363

64-
cleanfiles += $(link-out-dir)/rodata_unpaged.ld.S:
64+
cleanfiles += $(link-out-dir)/rodata_unpaged.ld.S
6565
$(link-out-dir)/rodata_unpaged.ld.S: $(link-out-dir)/unpaged.o
6666
@$(cmd-echo-silent) ' GEN $@'
6767
$(q)$(READELFcore) -a -W $< | \
@@ -93,12 +93,12 @@ $(link-out-dir)/init.o: $(link-out-dir)/init_entries.txt
9393
`cat $(link-out-dir)/init_entries.txt` \
9494
$(ldargs-init-objs) -o $@
9595

96-
cleanfiles += $(link-out-dir)/text_init.ld.S:
96+
cleanfiles += $(link-out-dir)/text_init.ld.S
9797
$(link-out-dir)/text_init.ld.S: $(link-out-dir)/init.o
9898
@$(cmd-echo-silent) ' GEN $@'
9999
$(q)$(READELFcore) -a -W $< | ${AWK} -f ./scripts/gen_ld_text_sects.awk > $@
100100

101-
cleanfiles += $(link-out-dir)/rodata_init.ld.S:
101+
cleanfiles += $(link-out-dir)/rodata_init.ld.S
102102
$(link-out-dir)/rodata_init.ld.S: $(link-out-dir)/init.o
103103
@$(cmd-echo-silent) ' GEN $@'
104104
$(q)$(READELFcore) -a -W $< | \

core/sub.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ produce-ta_pub_key = ta_pub_key.c
77
depends-ta_pub_key = $(TA_SIGN_KEY)
88
recipe-ta_pub_key = scripts/pem_to_pub_c.py --prefix ta_pub_key \
99
--key $(TA_SIGN_KEY) --out $(sub-dir-out)/ta_pub_key.c
10+
cleanfiles += $(sub-dir-out)/ta_pub_key.c

lib/libutee/tui/sub.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ recipe-default_bold := scripts/render_font.py \
1515
--font_file $(sub-dir)/fonts/amble/Amble-Bold.ttf \
1616
--font_size 20 --font_name default_bold \
1717
--out_dir $(sub-dir-out)
18+
cleanfiles += $(sub-dir-out)/default_bold.c $(sub-dir-out)/default_bold.h
1819

1920
gensrcs-y += default_regular
2021
produce-additional-default_regular = default_regular.h
@@ -25,5 +26,5 @@ recipe-default_regular := scripts/render_font.py \
2526
--font_file $(sub-dir)/fonts/amble/Amble-Regular.ttf \
2627
--font_size 20 --font_name default_regular \
2728
--out_dir $(sub-dir-out)
28-
29+
cleanfiles += $(sub-dir-out)/default_regular.c $(sub-dir-out)/default_regular.h
2930

mk/cleandirs.mk

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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

ta/mk/ta_dev_kit.mk

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,15 @@ libdeps += $(ta-dev-kit-dir)/lib/libutee.a
6868
libdeps += $(ta-dev-kit-dir)/lib/libzlib.a
6969
libdeps += $(ta-dev-kit-dir)/lib/libpng.a
7070

71+
include $(ta-dev-kit-dir)/mk/cleandirs.mk
72+
7173
.PHONY: clean
7274
clean:
73-
@$(cmd-echo-silent) ' CLEAN .'
75+
@$(cmd-echo-silent) ' CLEAN $(out-dir)'
7476
${q}rm -f $(cleanfiles)
75-
77+
${q}dirs="$(call cleandirs-for-rmdir)"; if [ "$$dirs" ]; then rmdir $$dirs; fi
78+
@$(cmd-echo-silent) ' CLEAN $(O)'
79+
${q}if [ -d "$(O)" ]; then rmdir --ignore-fail-on-non-empty $(O); fi
7680

7781
subdirs = .
7882
include $(ta-dev-kit-dir)/mk/subdir.mk

ta/ta.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ $(foreach f, $(libfiles), \
7575
$(eval $(call copy-file, $(f), $(out-dir)/export-$(sm)/lib)))
7676

7777
# Copy .mk files
78-
ta-mkfiles = mk/compile.mk mk/subdir.mk mk/gcc.mk \
78+
ta-mkfiles = mk/compile.mk mk/subdir.mk mk/gcc.mk mk/cleandirs.mk \
7979
$(wildcard ta/arch/$(ARCH)/link.mk) \
8080
ta/mk/ta_dev_kit.mk
8181

0 commit comments

Comments
 (0)