[PATCH v2 2/4] tests: introduce common fragment for unit tests

dmukhin@xen.org posted 4 patches 4 weeks, 1 day ago
[PATCH v2 2/4] tests: introduce common fragment for unit tests
Posted by dmukhin@xen.org 4 weeks, 1 day ago
From: Denis Mukhin <dmukhin@ford.com> 

Move test harness generation into a new shared make fragment so that
it can be reused by other unit tests.

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
Changes from v1:
- moved fragment to tools/tests/
---
 tools/tests/Rules.mk       | 91 ++++++++++++++++++++++++++++++++++++++
 tools/tests/domid/Makefile | 85 +----------------------------------
 2 files changed, 92 insertions(+), 84 deletions(-)
 create mode 100644 tools/tests/Rules.mk

diff --git a/tools/tests/Rules.mk b/tools/tests/Rules.mk
new file mode 100644
index 000000000000..daa9e69301e4
--- /dev/null
+++ b/tools/tests/Rules.mk
@@ -0,0 +1,91 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Common unit test fragment.
+#
+# Copyright 2025 Ford Motor Company
+
+include $(XEN_ROOT)/tools/Rules.mk
+
+define list-c-headers
+$(shell sed -n \
+    's/^[ \t]*# *include[ \t]*[<"]\([^">]*\)[">].*/\1/p' $(1) 2>/dev/null)
+endef
+
+# Generate mock environment by replicating header file hierarchy;
+# each header file will point to a harness header.
+#
+# $1 target
+# $2 list of test harnesses
+define emit-harness-nested-rule
+$(1): $(2)
+	set -e; \
+	mkdir -p $$(@D); \
+	for i in $(2); do [ -e $$@ ] || ln -s $$$$i $$@; done
+
+endef
+
+# Helper function to emit mocked hypervisor code dependencies.
+#
+# $1 Harness file name.
+# $2 Mocked hypervisor file name.
+# $3 List of dependencies to mock.
+define emit-harness-rules
+$(foreach x,$(3),$(call emit-harness-nested-rule,\
+                        $(CURDIR)/generated/$(x),\
+                        $(addprefix $(CURDIR)/,$(1))))
+$(2:.c=.o): $(addprefix $(CURDIR)/generated/,$(3))
+endef
+
+define emit-harness-deps
+$(if $(strip $(3)),$(call emit-harness-rules,$1,$2,$3),)
+endef
+
+# Emit dependencies for mocked hypervisor code.
+#
+# $1 Hypervisor file name.
+# $2 Hypervisor source path.
+# $3 Harness header file name (optional).
+define vpath-with-harness-deps
+vpath $(1) $(2)
+$(call emit-harness-deps,$(or $(strip $(3)),harness.h),\
+                         $(1),\
+                         $(call list-c-headers,$(2)$(1)))
+endef
+
+.PHONY: all
+all: $(TESTS)
+
+.PHONY: run
+run: $(TESTS)
+ifeq ($(CC),$(HOSTCC))
+	set -e; $(foreach t,$(TESTS),./$(t);)
+else
+	$(warning HOSTCC != CC, will not run test)
+endif
+
+.PHONY: clean
+clean:
+	$(RM) -r generated
+	$(RM) -- *.o $(TESTS) $(DEPS_RM)
+
+.PHONY: distclean
+distclean: clean
+	$(RM) -- *~
+
+.PHONY: install
+install: all
+	$(INSTALL_DIR) $(DESTDIR)$(LIBEXEC)/tests
+	set -e; $(foreach t,$(TESTS),$(INSTALL_PROG) $t $(DESTDIR)$(LIBEXEC)/tests;)
+
+.PHONY: uninstall
+uninstall:
+	set -e; $(foreach t,$(TESTS),$(RM) -- $(DESTDIR)$(LIBEXEC)/tests/$t;)
+
+CFLAGS += -D__XEN_TOOLS__
+# Honor mocked hypervisor header over tools/include/xen symlinks
+CFLAGS += -I$(CURDIR)/generated/
+CFLAGS += $(CFLAGS_xeninclude)
+
+ifeq ($(filter clean distclean,$(MAKECMDGOALS)),)
+-include $(DEPS_INCLUDE)
+endif
diff --git a/tools/tests/domid/Makefile b/tools/tests/domid/Makefile
index dd22a25b038a..2f8cc5380462 100644
--- a/tools/tests/domid/Makefile
+++ b/tools/tests/domid/Makefile
@@ -7,84 +7,7 @@
 TESTS := test-domid
 
 XEN_ROOT = $(CURDIR)/../../..
-include $(XEN_ROOT)/tools/Rules.mk
-
-define list-c-headers
-$(shell sed -n \
-    's/^[ \t]*# *include[ \t]*[<"]\([^">]*\)[">].*/\1/p' $(1) 2>/dev/null)
-endef
-
-# Generate mock environment by replicating header file hierarchy;
-# each header file will point to a harness header.
-#
-# $1 target
-# $2 list of test harnesses
-define emit-harness-nested-rule
-$(1): $(2)
-	set -e; \
-	mkdir -p $$(@D); \
-	for i in $(2); do [ -e $$@ ] || ln -s $$$$i $$@; done
-
-endef
-
-# Helper function to emit mocked hypervisor code dependencies.
-#
-# $1 Harness file name.
-# $2 Mocked hypervisor file name.
-# $3 List of dependencies to mock.
-define emit-harness-rules
-$(foreach x,$(3),$(call emit-harness-nested-rule,\
-                        $(CURDIR)/generated/$(x),\
-                        $(addprefix $(CURDIR)/,$(1))))
-$(2:.c=.o): $(addprefix $(CURDIR)/generated/,$(3))
-endef
-
-define emit-harness-deps
-$(if $(strip $(3)),$(call emit-harness-rules,$1,$2,$3),)
-endef
-
-# Emit dependencies for mocked hypervisor code.
-#
-# $1 Hypervisor file name.
-# $2 Hypervisor source path.
-# $3 Harness header file name (optional).
-define vpath-with-harness-deps
-vpath $(1) $(2)
-$(call emit-harness-deps,$(or $(strip $(3)),harness.h),\
-                         $(1),\
-                         $(call list-c-headers,$(2)$(1)))
-endef
-
-.PHONY: all
-all: $(TESTS)
-
-.PHONY: run
-run: $(TESTS)
-ifeq ($(CC),$(HOSTCC))
-	set -e; $(foreach t,$(TESTS),./$(t);)
-else
-	$(warning HOSTCC != CC, will not run test)
-endif
-
-.PHONY: clean
-clean:
-	$(RM) -r generated
-	$(RM) -- *.o $(TESTS) $(DEPS_RM)
-
-.PHONY: distclean
-distclean: clean
-	$(RM) -- *~
-
-.PHONY: install
-install: all
-	$(INSTALL_DIR) $(DESTDIR)$(LIBEXEC)/tests
-	set -e; $(foreach t,$(TESTS),$(INSTALL_PROG) $t $(DESTDIR)$(LIBEXEC)/tests;)
-
-.PHONY: uninstall
-uninstall:
-	set -e; $(foreach t,$(TESTS),$(RM) -- $(DESTDIR)$(LIBEXEC)/tests/$t;)
-
-CFLAGS += -D__XEN_TOOLS__
+include $(XEN_ROOT)/tools/tests/Rules.mk
 
 # find-next-bit.c
 CFLAGS-find-next-bit.c += '-DEXPORT_SYMBOL(x)=' \
@@ -96,10 +19,6 @@ CFLAGS-find-next-bit.c += '-DEXPORT_SYMBOL(x)=' \
 
 find-next-bit.o: CFLAGS += $(CFLAGS-find-next-bit.c)
 
-# Honor mocked hypervisor header over tools/include/xen symlinks
-CFLAGS += -I$(CURDIR)/generated/
-CFLAGS += $(CFLAGS_xeninclude)
-
 vpath find-next-bit.c $(XEN_ROOT)/xen/lib/
 
 # Point to the hypervisor code and generate test harness dependencies
@@ -109,5 +28,3 @@ $(eval $(call vpath-with-harness-deps,domid.c,$(XEN_ROOT)/xen/common/))
 
 test-domid: domid.o find-next-bit.o test-domid.o
 	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
-
--include $(DEPS_INCLUDE)
-- 
2.52.0
Re: [PATCH v2 2/4] tests: introduce common fragment for unit tests
Posted by Stefano Stabellini 2 weeks, 2 days ago
On Sat, 10 Jan 2026, dmukhin@xen.org wrote:
> From: Denis Mukhin <dmukhin@ford.com> 
> 
> Move test harness generation into a new shared make fragment so that
> it can be reused by other unit tests.
> 
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>

Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>


> ---
> Changes from v1:
> - moved fragment to tools/tests/
> ---
>  tools/tests/Rules.mk       | 91 ++++++++++++++++++++++++++++++++++++++
>  tools/tests/domid/Makefile | 85 +----------------------------------
>  2 files changed, 92 insertions(+), 84 deletions(-)
>  create mode 100644 tools/tests/Rules.mk
> 
> diff --git a/tools/tests/Rules.mk b/tools/tests/Rules.mk
> new file mode 100644
> index 000000000000..daa9e69301e4
> --- /dev/null
> +++ b/tools/tests/Rules.mk
> @@ -0,0 +1,91 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +#
> +# Common unit test fragment.
> +#
> +# Copyright 2025 Ford Motor Company
> +
> +include $(XEN_ROOT)/tools/Rules.mk
> +
> +define list-c-headers
> +$(shell sed -n \
> +    's/^[ \t]*# *include[ \t]*[<"]\([^">]*\)[">].*/\1/p' $(1) 2>/dev/null)
> +endef
> +
> +# Generate mock environment by replicating header file hierarchy;
> +# each header file will point to a harness header.
> +#
> +# $1 target
> +# $2 list of test harnesses
> +define emit-harness-nested-rule
> +$(1): $(2)
> +	set -e; \
> +	mkdir -p $$(@D); \
> +	for i in $(2); do [ -e $$@ ] || ln -s $$$$i $$@; done
> +
> +endef
> +
> +# Helper function to emit mocked hypervisor code dependencies.
> +#
> +# $1 Harness file name.
> +# $2 Mocked hypervisor file name.
> +# $3 List of dependencies to mock.
> +define emit-harness-rules
> +$(foreach x,$(3),$(call emit-harness-nested-rule,\
> +                        $(CURDIR)/generated/$(x),\
> +                        $(addprefix $(CURDIR)/,$(1))))
> +$(2:.c=.o): $(addprefix $(CURDIR)/generated/,$(3))
> +endef
> +
> +define emit-harness-deps
> +$(if $(strip $(3)),$(call emit-harness-rules,$1,$2,$3),)
> +endef
> +
> +# Emit dependencies for mocked hypervisor code.
> +#
> +# $1 Hypervisor file name.
> +# $2 Hypervisor source path.
> +# $3 Harness header file name (optional).
> +define vpath-with-harness-deps
> +vpath $(1) $(2)
> +$(call emit-harness-deps,$(or $(strip $(3)),harness.h),\
> +                         $(1),\
> +                         $(call list-c-headers,$(2)$(1)))
> +endef
> +
> +.PHONY: all
> +all: $(TESTS)
> +
> +.PHONY: run
> +run: $(TESTS)
> +ifeq ($(CC),$(HOSTCC))
> +	set -e; $(foreach t,$(TESTS),./$(t);)
> +else
> +	$(warning HOSTCC != CC, will not run test)
> +endif
> +
> +.PHONY: clean
> +clean:
> +	$(RM) -r generated
> +	$(RM) -- *.o $(TESTS) $(DEPS_RM)
> +
> +.PHONY: distclean
> +distclean: clean
> +	$(RM) -- *~
> +
> +.PHONY: install
> +install: all
> +	$(INSTALL_DIR) $(DESTDIR)$(LIBEXEC)/tests
> +	set -e; $(foreach t,$(TESTS),$(INSTALL_PROG) $t $(DESTDIR)$(LIBEXEC)/tests;)
> +
> +.PHONY: uninstall
> +uninstall:
> +	set -e; $(foreach t,$(TESTS),$(RM) -- $(DESTDIR)$(LIBEXEC)/tests/$t;)
> +
> +CFLAGS += -D__XEN_TOOLS__
> +# Honor mocked hypervisor header over tools/include/xen symlinks
> +CFLAGS += -I$(CURDIR)/generated/
> +CFLAGS += $(CFLAGS_xeninclude)
> +
> +ifeq ($(filter clean distclean,$(MAKECMDGOALS)),)
> +-include $(DEPS_INCLUDE)
> +endif
> diff --git a/tools/tests/domid/Makefile b/tools/tests/domid/Makefile
> index dd22a25b038a..2f8cc5380462 100644
> --- a/tools/tests/domid/Makefile
> +++ b/tools/tests/domid/Makefile
> @@ -7,84 +7,7 @@
>  TESTS := test-domid
>  
>  XEN_ROOT = $(CURDIR)/../../..
> -include $(XEN_ROOT)/tools/Rules.mk
> -
> -define list-c-headers
> -$(shell sed -n \
> -    's/^[ \t]*# *include[ \t]*[<"]\([^">]*\)[">].*/\1/p' $(1) 2>/dev/null)
> -endef
> -
> -# Generate mock environment by replicating header file hierarchy;
> -# each header file will point to a harness header.
> -#
> -# $1 target
> -# $2 list of test harnesses
> -define emit-harness-nested-rule
> -$(1): $(2)
> -	set -e; \
> -	mkdir -p $$(@D); \
> -	for i in $(2); do [ -e $$@ ] || ln -s $$$$i $$@; done
> -
> -endef
> -
> -# Helper function to emit mocked hypervisor code dependencies.
> -#
> -# $1 Harness file name.
> -# $2 Mocked hypervisor file name.
> -# $3 List of dependencies to mock.
> -define emit-harness-rules
> -$(foreach x,$(3),$(call emit-harness-nested-rule,\
> -                        $(CURDIR)/generated/$(x),\
> -                        $(addprefix $(CURDIR)/,$(1))))
> -$(2:.c=.o): $(addprefix $(CURDIR)/generated/,$(3))
> -endef
> -
> -define emit-harness-deps
> -$(if $(strip $(3)),$(call emit-harness-rules,$1,$2,$3),)
> -endef
> -
> -# Emit dependencies for mocked hypervisor code.
> -#
> -# $1 Hypervisor file name.
> -# $2 Hypervisor source path.
> -# $3 Harness header file name (optional).
> -define vpath-with-harness-deps
> -vpath $(1) $(2)
> -$(call emit-harness-deps,$(or $(strip $(3)),harness.h),\
> -                         $(1),\
> -                         $(call list-c-headers,$(2)$(1)))
> -endef
> -
> -.PHONY: all
> -all: $(TESTS)
> -
> -.PHONY: run
> -run: $(TESTS)
> -ifeq ($(CC),$(HOSTCC))
> -	set -e; $(foreach t,$(TESTS),./$(t);)
> -else
> -	$(warning HOSTCC != CC, will not run test)
> -endif
> -
> -.PHONY: clean
> -clean:
> -	$(RM) -r generated
> -	$(RM) -- *.o $(TESTS) $(DEPS_RM)
> -
> -.PHONY: distclean
> -distclean: clean
> -	$(RM) -- *~
> -
> -.PHONY: install
> -install: all
> -	$(INSTALL_DIR) $(DESTDIR)$(LIBEXEC)/tests
> -	set -e; $(foreach t,$(TESTS),$(INSTALL_PROG) $t $(DESTDIR)$(LIBEXEC)/tests;)
> -
> -.PHONY: uninstall
> -uninstall:
> -	set -e; $(foreach t,$(TESTS),$(RM) -- $(DESTDIR)$(LIBEXEC)/tests/$t;)
> -
> -CFLAGS += -D__XEN_TOOLS__
> +include $(XEN_ROOT)/tools/tests/Rules.mk
>  
>  # find-next-bit.c
>  CFLAGS-find-next-bit.c += '-DEXPORT_SYMBOL(x)=' \
> @@ -96,10 +19,6 @@ CFLAGS-find-next-bit.c += '-DEXPORT_SYMBOL(x)=' \
>  
>  find-next-bit.o: CFLAGS += $(CFLAGS-find-next-bit.c)
>  
> -# Honor mocked hypervisor header over tools/include/xen symlinks
> -CFLAGS += -I$(CURDIR)/generated/
> -CFLAGS += $(CFLAGS_xeninclude)
> -
>  vpath find-next-bit.c $(XEN_ROOT)/xen/lib/
>  
>  # Point to the hypervisor code and generate test harness dependencies
> @@ -109,5 +28,3 @@ $(eval $(call vpath-with-harness-deps,domid.c,$(XEN_ROOT)/xen/common/))
>  
>  test-domid: domid.o find-next-bit.o test-domid.o
>  	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
> -
> --include $(DEPS_INCLUDE)
> -- 
> 2.52.0
>