Skip to contentSkip to conversation

selftests: more general make nesting support

PATCH In review Greg Thelen 2 participants 4 messages Jul 28, 2020 – Aug 5, 2020 Kselftest

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests: more general make nesting support

selftests can be built from the toplevel kernel makefile (e.g. make kselftest-all) or directly (make -C tools/testing/selftests all).

The toplevel kernel makefile explicitly disables implicit rules with "MAKEFLAGS += -rR", which is passed to tools/testing/selftests. Some selftest makefiles require implicit make rules, which is why commit 67d8712dcc70 ("selftests: Fix build failures when invoked from kselftest target") reenables implicit rules by clearing MAKEFLAGS if MAKELEVEL=1.

So far so good. However, if the toplevel makefile is called from an outer makefile then MAKELEVEL will be elevated, which breaks the MAKELEVEL equality test.
Example wrapped makefile error:

  $ cat ~/Makefile
  all:
  	$(MAKE) defconfig
  	$(MAKE) kselftest-all
  $ make -sf ~/Makefile
    futex_wait_timeout.c /src/tools/testing/selftests/kselftest_harness.h   /src/tools/testing/selftests/kselftest.h ../include/futextest.h ../include/atomic.h ../include/logging.h -lpthread -lrt -o /src/tools/testing/selftests/futex/functional/futex_wait_timeout
  make[4]: futex_wait_timeout.c: Command not found

Rather than checking $(MAKELEVEL), check for $(LINK.c), which is a more direct side effect of "make -R". This enables arbitrary makefile nesting.

---
 tools/testing/selftests/Makefile | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/Makefile +4 -4
index 1195bd85af38..289a2e4b3f6f 100644
@@ -84,10 +84,10 @@ endif
 # of the targets gets built.
 FORCE_TARGETS ?=
 
-# Clear LDFLAGS and MAKEFLAGS if called from main
-# Makefile to avoid test build failures when test
-# Makefile doesn't have explicit build rules.
-ifeq (1,$(MAKELEVEL))
+# Clear LDFLAGS and MAKEFLAGS when implicit rules are missing.  This provides
+# implicit rules to sub-test Makefiles which avoids build failures in test
+# Makefile that don't have explicit build rules.
+ifeq (,$(LINK.c))
 override LDFLAGS =
 override MAKEFLAGS =
 endif
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
#1 lore raw
Quoted 52 lines · Greg Thelen

selftests can be built from the toplevel kernel makefile (e.g. make kselftest-all) or directly (make -C tools/testing/selftests all).

The toplevel kernel makefile explicitly disables implicit rules with "MAKEFLAGS += -rR", which is passed to tools/testing/selftests. Some selftest makefiles require implicit make rules, which is why commit 67d8712dcc70 ("selftests: Fix build failures when invoked from kselftest target") reenables implicit rules by clearing MAKEFLAGS if MAKELEVEL=1.

So far so good. However, if the toplevel makefile is called from an outer makefile then MAKELEVEL will be elevated, which breaks the MAKELEVEL equality test.
Example wrapped makefile error:

  $ cat ~/Makefile
  all:
        $(MAKE) defconfig
        $(MAKE) kselftest-all
  $ make -sf ~/Makefile
    futex_wait_timeout.c /src/tools/testing/selftests/kselftest_harness.h   /src/tools/testing/selftests/kselftest.h ../include/futextest.h ../include/atomic.h ../include/logging.h -lpthread -lrt -o /src/tools/testing/selftests/futex/functional/futex_wait_timeout
  make[4]: futex_wait_timeout.c: Command not found

Rather than checking $(MAKELEVEL), check for $(LINK.c), which is a more direct side effect of "make -R". This enables arbitrary makefile nesting.

---
 tools/testing/selftests/Makefile | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/Makefile +4 -5
index 1195bd85af38..289a2e4b3f6f 100644
@@ -84,10 +84,10 @@ endif
 # of the targets gets built.
 FORCE_TARGETS ?=
 
-# Clear LDFLAGS and MAKEFLAGS if called from main
-# Makefile to avoid test build failures when test
-# Makefile doesn't have explicit build rules.
-ifeq (1,$(MAKELEVEL))
+# Clear LDFLAGS and MAKEFLAGS when implicit rules are missing.  This provides
+# implicit rules to sub-test Makefiles which avoids build failures in test
+# Makefile that don't have explicit build rules.
+ifeq (,$(LINK.c))
 override LDFLAGS =
 override MAKEFLAGS =
 endif
--
Quoted 1 line

2.28.0.rc0.142.g3c755180ce-goog

Is there any feedback on this patch? It's not high priority but something that will help me make more use of selftests.

maintainer
#2 lore raw
Quoted 41 lines · Greg Thelen
Quoted 40 lines · Greg Thelen

selftests can be built from the toplevel kernel makefile (e.g. make kselftest-all) or directly (make -C tools/testing/selftests all).

The toplevel kernel makefile explicitly disables implicit rules with "MAKEFLAGS += -rR", which is passed to tools/testing/selftests. Some selftest makefiles require implicit make rules, which is why commit 67d8712dcc70 ("selftests: Fix build failures when invoked from kselftest target") reenables implicit rules by clearing MAKEFLAGS if MAKELEVEL=1.

So far so good. However, if the toplevel makefile is called from an outer makefile then MAKELEVEL will be elevated, which breaks the MAKELEVEL equality test.
Example wrapped makefile error:

   $ cat ~/Makefile
   all:
         $(MAKE) defconfig
         $(MAKE) kselftest-all
   $ make -sf ~/Makefile
     futex_wait_timeout.c /src/tools/testing/selftests/kselftest_harness.h   /src/tools/testing/selftests/kselftest.h ../include/futextest.h ../include/atomic.h ../include/logging.h -lpthread -lrt -o /src/tools/testing/selftests/futex/functional/futex_wait_timeout
   make[4]: futex_wait_timeout.c: Command not found

Rather than checking $(MAKELEVEL), check for $(LINK.c), which is a more direct side effect of "make -R". This enables arbitrary makefile nesting.

---
  tools/testing/selftests/Makefile | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/Makefile +0 -0
index 1195bd85af38..289a2e4b3f6f 100644
@@ -84,10 +84,10 @@ endif
  # of the targets gets built.
  FORCE_TARGETS ?=
 
Quoted 17 lines
Quoted 12 lines
tools/testing/selftests/Makefile +4 -5
-# Clear LDFLAGS and MAKEFLAGS if called from main
-# Makefile to avoid test build failures when test
-# Makefile doesn't have explicit build rules.
-ifeq (1,$(MAKELEVEL))
+# Clear LDFLAGS and MAKEFLAGS when implicit rules are missing.  This provides
+# implicit rules to sub-test Makefiles which avoids build failures in test
+# Makefile that don't have explicit build rules.
+ifeq (,$(LINK.c))
  override LDFLAGS =
  override MAKEFLAGS =
  endif
--
Quoted 1 line

2.28.0.rc0.142.g3c755180ce-goog

Is there any feedback on this patch? It's not high priority but something that will help me make more use of selftests.

No issues with the patch. I will apply it once the merge window ends.

thanks,
-- Shuah

#3 lore raw
Quoted 60 lines · Shuah Khan
Quoted 41 lines · Greg Thelen
Quoted 40 lines · Greg Thelen

selftests can be built from the toplevel kernel makefile (e.g. make kselftest-all) or directly (make -C tools/testing/selftests all).

The toplevel kernel makefile explicitly disables implicit rules with "MAKEFLAGS += -rR", which is passed to tools/testing/selftests. Some selftest makefiles require implicit make rules, which is why commit 67d8712dcc70 ("selftests: Fix build failures when invoked from kselftest target") reenables implicit rules by clearing MAKEFLAGS if MAKELEVEL=1.

So far so good. However, if the toplevel makefile is called from an outer makefile then MAKELEVEL will be elevated, which breaks the MAKELEVEL equality test.
Example wrapped makefile error:

   $ cat ~/Makefile
   all:
         $(MAKE) defconfig
         $(MAKE) kselftest-all
   $ make -sf ~/Makefile
     futex_wait_timeout.c /src/tools/testing/selftests/kselftest_harness.h   /src/tools/testing/selftests/kselftest.h ../include/futextest.h ../include/atomic.h ../include/logging.h -lpthread -lrt -o /src/tools/testing/selftests/futex/functional/futex_wait_timeout
   make[4]: futex_wait_timeout.c: Command not found

Rather than checking $(MAKELEVEL), check for $(LINK.c), which is a more direct side effect of "make -R". This enables arbitrary makefile nesting.

---
  tools/testing/selftests/Makefile | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/Makefile +0 -0
index 1195bd85af38..289a2e4b3f6f 100644
@@ -84,10 +84,10 @@ endif
  # of the targets gets built.
  FORCE_TARGETS ?=
 
Quoted 17 lines
Quoted 12 lines
tools/testing/selftests/Makefile +4 -5
-# Clear LDFLAGS and MAKEFLAGS if called from main
-# Makefile to avoid test build failures when test
-# Makefile doesn't have explicit build rules.
-ifeq (1,$(MAKELEVEL))
+# Clear LDFLAGS and MAKEFLAGS when implicit rules are missing.  This provides
+# implicit rules to sub-test Makefiles which avoids build failures in test
+# Makefile that don't have explicit build rules.
+ifeq (,$(LINK.c))
  override LDFLAGS =
  override MAKEFLAGS =
  endif
--
Quoted 1 line

2.28.0.rc0.142.g3c755180ce-goog

Is there any feedback on this patch? It's not high priority but something that will help me make more use of selftests.

Quoted 4 lines

No issues with the patch. I will apply it once the merge window ends.

thanks,
-- Shuah

Great. Thank you.