[LTP] [PATCH v4 2/3] build: Build shared libraries once when make runs in testcases/
Li Wang (Hieco)
li.wang@linux.dev
Tue Nov 24 10:06:32 CET 2026
From: Andrea Cervesato <andrea.cervesato@suse.com>
Running make directly under testcases/ (for example
"make -C testcases/kernel/syscalls -j8") can start many sub-makes in
parallel. Each leaf can then reach the $(LIBLTP) rule in
testcases.mk and call "$(MAKE) -C lib" at the same time. This builds
the same library more than once, and parallel builds can race on
libltp.a. libkerntest.a and the libs/ libraries have the same
problem.
Add include/mk/generic_lib_deps.inc and include it from both the
generic leaf and trunk targets. When make is started inside
testcases/ (MAKELEVEL == 0), the file adds libltp.a, the libs/
libraries and, for testcases/ and testcases/kernel/*, libkerntest.a
to MAKE_DEPS. Leaf targets and trunk subdirectories depend on
MAKE_DEPS as order-only prerequisites. As a result, the libraries
are built once, in the top-level make process, before any
subdirectory is entered.
The library targets are .PHONY, so lib/, libs/ and
testcases/kernel/lib/ are always entered and their own Makefiles
decide what to rebuild. A change to the library sources is therefore
picked up. Because the prerequisites are order-only, tests that are
already linked are not relinked.
Remove the old $(LIBLTP) rule and "MAKE_DEPS := $(LIBLTP)" from
include/mk/testcases.mk, which the new file replaces. Builds started
from the top-level directory are not affected.
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
Signed-off-by: Li Wang (Hieco) <li.wang@linux.dev>
---
include/mk/generic_leaf_target.inc | 2 ++
include/mk/generic_lib_deps.inc | 34 +++++++++++++++++++++++++++++
include/mk/generic_trunk_target.inc | 8 +++++++
include/mk/testcases.mk | 5 +----
testcases/kernel/include/lib.mk | 2 ++
5 files changed, 47 insertions(+), 4 deletions(-)
create mode 100644 include/mk/generic_lib_deps.inc
diff --git a/include/mk/generic_leaf_target.inc b/include/mk/generic_leaf_target.inc
index 7c685fea7..533381b9f 100644
--- a/include/mk/generic_leaf_target.inc
+++ b/include/mk/generic_leaf_target.inc
@@ -76,6 +76,8 @@
# INSTALL_DIR := $(libdir)
#
+include $(top_srcdir)/include/mk/generic_lib_deps.inc
+
.PHONY: all clean install check
ifneq ($(strip $(MAKE_TARGETS)),)
diff --git a/include/mk/generic_lib_deps.inc b/include/mk/generic_lib_deps.inc
new file mode 100644
index 000000000..666b3f9ef
--- /dev/null
+++ b/include/mk/generic_lib_deps.inc
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) Linux Test Project, 2026
+
+ifeq ($(MAKELEVEL),0)
+
+ifneq ($(filter $(abs_top_builddir)/testcases%,$(CURDIR)),)
+
+MAKE_DEPS += $(abs_top_builddir)/lib/libltp.a
+MAKE_DEPS += $(abs_top_builddir)/libs-all
+
+.PHONY: $(abs_top_builddir)/lib/libltp.a
+$(abs_top_builddir)/lib/libltp.a:
+ mkdir -m 00755 -p "$(@D)"
+ $(MAKE) -C "$(@D)" -f "$(abs_top_srcdir)/lib/Makefile" all
+
+.PHONY: $(abs_top_builddir)/libs-all
+$(abs_top_builddir)/libs-all: $(LTPLIBS_FILES) | $(abs_top_builddir)/lib/libltp.a
+ mkdir -m 00755 -p "$(abs_top_builddir)/libs"
+ $(MAKE) -C "$(abs_top_builddir)/libs" -f "$(abs_top_srcdir)/libs/Makefile" all
+
+endif
+
+ifneq ($(filter $(abs_top_builddir)/testcases $(abs_top_builddir)/testcases/kernel%,$(filter-out $(abs_top_builddir)/testcases/kernel/lib,$(CURDIR))),)
+
+MAKE_DEPS += $(abs_top_builddir)/testcases/kernel/lib/libkerntest.a
+
+.PHONY: $(abs_top_builddir)/testcases/kernel/lib/libkerntest.a
+$(abs_top_builddir)/testcases/kernel/lib/libkerntest.a: | $(abs_top_builddir)/lib/libltp.a
+ mkdir -m 00755 -p "$(@D)"
+ $(MAKE) -C "$(@D)" -f "$(abs_top_srcdir)/testcases/kernel/lib/Makefile" all
+
+endif
+
+endif
diff --git a/include/mk/generic_trunk_target.inc b/include/mk/generic_trunk_target.inc
index f9db7896a..4186ee9d9 100644
--- a/include/mk/generic_trunk_target.inc
+++ b/include/mk/generic_trunk_target.inc
@@ -36,6 +36,8 @@ RECURSIVE_TARGETS ?= all install check
$(eval $(get_make_dirs))
+include $(top_srcdir)/include/mk/generic_lib_deps.inc
+
.PHONY: $(RECURSIVE_TARGETS) $(addprefix trunk-,$(RECURSIVE_TARGETS))
$(SUBDIRS): %:
@@ -78,6 +80,12 @@ endif
install: trunk-install
+ifneq ($(strip $(MAKE_DEPS)),)
+ifneq ($(strip $(filter all install,$(RECURSIVE_TARGETS))),)
+$(filter all install,$(RECURSIVE_TARGETS)): | $(MAKE_DEPS)
+endif
+endif
+
# Print out CURDIR to check for a recursion issue.
ifeq ($(strip $(SUBDIRS)),)
$(warning CURDIR is: $(CURDIR))
diff --git a/include/mk/testcases.mk b/include/mk/testcases.mk
index 5960d7485..f9ff93928 100644
--- a/include/mk/testcases.mk
+++ b/include/mk/testcases.mk
@@ -15,10 +15,7 @@ LIBLTP := $(LIBLTP_DIR)/libltp.a
$(APICMDS_DIR)/tst_kvercmp: | $(APICMDS_DIR)
$(MAKE) -C "$(APICMDS_DIR)" -f "$(abs_top_srcdir)/tools/apicmds/Makefile" all
-$(LIBLTP): | $(LIBLTP_DIR)
- $(MAKE) -C "$(LIBLTP_DIR)" -f "$(abs_top_srcdir)/lib/Makefile" all
-
-MAKE_DEPS := $(LIBLTP)
+MAKE_DEPS :=
INSTALL_DIR := testcases/bin
diff --git a/testcases/kernel/include/lib.mk b/testcases/kernel/include/lib.mk
index c1a0c6d52..8f1ec5313 100644
--- a/testcases/kernel/include/lib.mk
+++ b/testcases/kernel/include/lib.mk
@@ -29,8 +29,10 @@ LDFLAGS += -L$(LIBKERNTEST_DIR)
$(LIBKERNTEST_DIR):
mkdir -p "$@"
+ifneq ($(MAKELEVEL),0)
$(LIBKERNTEST): | $(LIBKERNTEST_DIR)
$(MAKE) -C "$(LIBKERNTEST_DIR)" -f "$(LIBKERNTEST_SRCDIR)/Makefile" all
+endif
MAKE_DEPS += $(LIBKERNTEST)
--
2.56.1
More information about the ltp
mailing list