make: fix "make clean" against directories generated at build
This change removes the directory created from xtest build and
xtest test TAs build.
Prevent trailing "/." in path string, i.e "$O"=="some/relative/path/.".
These fail are rejected by the rmdir command.
Allow O=. or O=./ by preventing execution of rmdir.
From TA root Makefile and as $(O) can be a relative path or absolute
path, change to makefile directory to 'rmdir' target from make command
execution path.
Let ta/Makefile handle the "ta/" sub-directory info in build/clean.
Let host/xtest/Makefile handle the "xtest/" sub-directory info in
build/clean.
Build output subdirectory should be nicely empty and ready to be
remove if exists. Root output directory is removed only if empty
as it might contain other stuff than our build artifacts.
As 'clean' rules can be ran from partially built environment, only
remove existing directories.
Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org>
Tested-by: Jerome Forissier <jerome.forissier@linaro.org> (HiKey)
Tested-by: Etienne Carriere <etienne.carriere@linaro.org> (b2260, qemu)
diff --git a/Makefile b/Makefile
index d689a86..86177ae 100644
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,11 @@
-ifneq ($O,)
- out-dir := $O
+ifeq ($O,)
+out-dir := $(CURDIR)/out
else
- # If no build folder has been specified, then create all build files in
- # the current directory under a folder named out.
- out-dir := $(CURDIR)/out
+include scripts/common.mk
+out-dir := $(call strip-trailing-slashes-and-dots,$(O))
+ifeq ($(out-dir),)
+$(error invalid output directory (O=$(O)))
+endif
endif
-include $(TA_DEV_KIT_DIR)/host_include/conf.mk
@@ -15,6 +17,8 @@
q :=
echo := @:
endif
+# export 'q', used by sub-makefiles.
+export q
# If _HOST or _TA specific compilers are not specified, then use CROSS_COMPILE
CROSS_COMPILE_HOST ?= $(CROSS_COMPILE)
@@ -32,22 +36,20 @@
xtest:
$(q)$(MAKE) -C host/xtest CROSS_COMPILE="$(CROSS_COMPILE_HOST)" \
--no-builtin-variables \
- q=$(q) \
- O=$(out-dir)/xtest \
+ O=$(out-dir) \
$@
.PHONY: ta
ta:
$(q)$(MAKE) -C ta CROSS_COMPILE="$(CROSS_COMPILE_TA)" \
- q=$(q) \
- O=$(out-dir)/ta \
+ O=$(out-dir) \
$@
.PHONY: clean
ifneq ($(wildcard $(TA_DEV_KIT_DIR)/host_include/conf.mk),)
clean:
- $(q)$(MAKE) -C host/xtest O=$(out-dir)/xtest q=$(q) $@
- $(q)$(MAKE) -C ta O=$(out-dir)/ta q=$(q) $@
+ $(q)$(MAKE) -C host/xtest O=$(out-dir) $@
+ $(q)$(MAKE) -C ta O=$(out-dir) $@
else
clean:
$(q)echo "TA_DEV_KIT_DIR is not correctly defined"
diff --git a/host/xtest/Makefile b/host/xtest/Makefile
index 8342e3c..ff497cc 100644
--- a/host/xtest/Makefile
+++ b/host/xtest/Makefile
@@ -1,8 +1,11 @@
# Normally this makefile shouldn't be called directly and we expect the output
# path to be on a certain location to fit together with the other OP-TEE
# gits and helper scripts.
-ifeq ($O,)
-$(error output path should be specified when calling this makefile)
+
+include ../../scripts/common.mk
+out-dir := $(call strip-trailing-slashes-and-dots,$(O))
+ifeq ($(out-dir),)
+$(error invalid output directory (O=$(O)))
endif
include $(TA_DEV_KIT_DIR)/host_include/conf.mk
@@ -62,7 +65,7 @@
xtest_9000.c
endif
-objs := $(patsubst %.c,$(O)/%.o, $(srcs))
+objs := $(patsubst %.c,$(out-dir)/xtest/%.o, $(srcs))
CFLAGS += -I./
CFLAGS += -I./adbg/include
@@ -142,16 +145,24 @@
all: xtest
xtest: $(objs)
- @echo " LD $(O)/$@"
- $(q)@$(CC) -o $(O)/$@ $+ $(LDFLAGS)
+ @echo " LD $(out-dir)/xtest/$@"
+ $(q)@$(CC) -o $(out-dir)/xtest/$@ $+ $(LDFLAGS)
-$(O)/%.o: $(CURDIR)/%.c
- $(q)mkdir -p $(O)/adbg/src
+$(out-dir)/xtest/%.o: $(CURDIR)/%.c
+ $(q)mkdir -p $(out-dir)/xtest/adbg/src
@echo ' CC $<'
$(q)$(CC) $(CFLAGS) -c $< -o $@
+RMDIR := rmdir --ignore-fail-on-non-empty
+define rm-build-dirs
+ $(q)for d in $1; do $(RMDIR) $(out-dir)/xtest/$$d 2> /dev/null; true; done
+ $(q)$(RMDIR) $(out-dir)/xtest 2> /dev/null; true
+ $(q)$(RMDIR) $(out-dir) 2> /dev/null; true
+endef
+
.PHONY: clean
clean:
- @echo ' CLEAN $(O)'
- $(q)rm -f $(O)/xtest
+ @echo ' CLEAN $(out-dir)'
+ $(q)rm -f $(out-dir)/xtest/xtest
$(q)$(foreach obj,$(objs), rm -f $(obj))
+ $(call rm-build-dirs,adbg/src adbg)
diff --git a/scripts/common.mk b/scripts/common.mk
new file mode 100644
index 0000000..103a415
--- /dev/null
+++ b/scripts/common.mk
@@ -0,0 +1,9 @@
+# Makefile script used in various OP-TEE test components
+
+# Since 'clean' make rules rely on 'rmdir', removed directories shall
+# not end with 'current directory' (./ or .) subpath information.
+# This macro remove trailing './' info from input path.
+define strip-trailing-slashes-and-dots
+$(eval _o := $(patsubst %/,%,$(patsubst %/.,%,$(1))))$(if \
+ $(filter-out $(1),$(_o)),$(call strip-trailing-slashes-and-dots,$(_o)),$(_o))
+endef
diff --git a/ta/Makefile b/ta/Makefile
index bcc7723..e3917a7 100644
--- a/ta/Makefile
+++ b/ta/Makefile
@@ -1,8 +1,11 @@
# Normally this makefile shouldn't be called directly and we expect the output
# path to be on a certain location to fit together with the other OP-TEE
# gits and helper scripts.
-ifeq ($O,)
-$(error output path should be specified when calling this makefile)
+
+include ../scripts/common.mk
+out-dir := $(call strip-trailing-slashes-and-dots,$(O))
+ifeq ($(out-dir),)
+$(error invalid output directory (O=$(O)))
endif
# Prevent use of LDFLAGS from the environment. For example, yocto exports
@@ -45,8 +48,16 @@
.PHONY: ta
ta:
- $(q)$(foreach dir,$(TA_DIRS), $(MAKE) -C $(dir) O=$(O)/$(dir) &&) true
+ $(q)$(foreach dir,$(TA_DIRS), $(MAKE) -C $(dir) O=$(out-dir)/ta/$(dir) &&) true
+
+# remove build directories including ta/<ta-name>/ directories.
+# Note: $(out-dir) may be a relative path.
+RMDIR := rmdir --ignore-fail-on-non-empty
+define rm-build-subdir
+echo `[ -d $1 ] && { cd $1; $(RMDIR) $(out-dir)/ta $(out-dir) 2> /dev/null; true; }` > /dev/null
+endef
.PHONY: clean
clean:
- $(q)$(foreach dir,$(TA_DIRS), $(MAKE) -C $(dir) O=$(O)/$(dir) $@ &&) true
+ $(q)$(foreach dir,$(TA_DIRS), $(MAKE) -C $(dir) O=$(out-dir)/ta/$(dir) $@ &&) true
+ $(q)$(foreach dir,$(TA_DIRS), $(call rm-build-subdir,$(dir));)