public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* A question of KVM selftests' makefile
@ 2023-01-25  8:53 Yu Zhang
  2023-01-25 17:26 ` Sean Christopherson
  2023-01-25 18:46 ` Paolo Bonzini
  0 siblings, 2 replies; 4+ messages in thread
From: Yu Zhang @ 2023-01-25  8:53 UTC (permalink / raw)
  To: kvm; +Cc: pbonzini, seanjc

Hi all,

  Currently, unlike the build system of Linux kernel, KVM selftests will
have to run "make clean && make" to rebuild the entire test suite, once
a header file is modified.

  Is it designed like this on purpose, or does anyone wanna change it?

  I hacked the makefile by using "-MD" as EXTRA_CFLAGS, so that dependency
rules can be generated for each target object, whose prerequisites contains
the source file and the included header files as well.

  However, this change has its own costs. E.g., new ".o" and ".d" files will
occupy more storage. And performance-wise, the benifit could be limited,
because for now, most header files are needed by almost every ".c" files.
But with the evolution of KVM selftests, more ".h" files may be added. Some
of which may be of special usage. E.g., file "include/x86_64/mce.h" is only
used by "x86_64/ucna_injection_test". Having to rebuild the whole test suite
just because one specific header is touched would be annoying.

  I am not sure if this change is worthy, or if there's a better solution.
So does anyone have any suggestion? 

  Below are my current hack in KVM selftest's makefile in case someone is
interested. It can not be optimal and any comment is welcome. Thanks!

B.R.
Yu

diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
index 1750f91dd936..b329e0d1a460 100644
--- a/tools/testing/selftests/kvm/Makefile
+++ b/tools/testing/selftests/kvm/Makefile
@@ -180,6 +180,8 @@ TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(ARCH_DIR))
 TEST_GEN_PROGS_EXTENDED += $(TEST_GEN_PROGS_EXTENDED_$(ARCH_DIR))
 LIBKVM += $(LIBKVM_$(ARCH_DIR))

+OVERRIDE_TARGETS = 1
+
 # lib.mak defines $(OUTPUT), prepends $(OUTPUT)/ to $(TEST_GEN_PROGS), and most
 # importantly defines, i.e. overwrites, $(CC) (unless `make -e` or `make CC=`,
 # which causes the environment variable to override the makefile).
@@ -198,9 +200,11 @@ CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
        -fno-builtin-memcmp -fno-builtin-memcpy -fno-builtin-memset \
        -fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \
        -I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \
-       -I$(<D) -Iinclude/$(ARCH_DIR) -I ../rseq -I.. $(EXTRA_CFLAGS) \
+       -I$(<D) -Iinclude/$(ARCH_DIR) -I ../rseq -I.. \
        $(KHDR_INCLUDES)

+EXTRA_CFLAGS += -MD
+
 no-pie-option := $(call try-run, echo 'int main(void) { return 0; }' | \
         $(CC) -Werror $(CFLAGS) -no-pie -x c - -o "$$TMP", -no-pie)

@@ -218,11 +222,22 @@ LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S))
 LIBKVM_STRING_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING))
 LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ)

-EXTRA_CLEAN += $(LIBKVM_OBJS) cscope.*
+TEST_GEN_OBJ = $(patsubst %, %.o, $(TEST_GEN_PROGS))
+TEST_GEN_OBJ += $(patsubst %, %.o, $(TEST_GEN_PROGS_EXTENDED))
+TEST_DEP_FILES = $(patsubst %.o, %.d, $(TEST_GEN_OBJ))
+TEST_DEP_FILES += $(patsubst %.o, %.d, $(LIBKVM_OBJS))
+-include $(TEST_DEP_FILES)
+
+$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): %: %.o
+       $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBKVM_OBJS) $(LDLIBS) -o $@
+$(TEST_GEN_OBJ): %.o: %.c
+       $(CC) $(CFLAGS) $(CPPFLAGS) $(EXTRA_CFLAGS) $(TARGET_ARCH) -c $< -o $@
+
+EXTRA_CLEAN += $(LIBKVM_OBJS) $(TEST_DEP_FILES) $(TEST_GEN_OBJ) cscope.*

 x := $(shell mkdir -p $(sort $(dir $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ))))
 $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c
-       $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
+       $(CC) $(CFLAGS) $(CPPFLAGS) $(EXTRA_CFLAGS) $(TARGET_ARCH) -c $< -o $@

 $(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S
        $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: A question of KVM selftests' makefile
  2023-01-25  8:53 A question of KVM selftests' makefile Yu Zhang
@ 2023-01-25 17:26 ` Sean Christopherson
  2023-01-25 18:46 ` Paolo Bonzini
  1 sibling, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2023-01-25 17:26 UTC (permalink / raw)
  To: Yu Zhang; +Cc: kvm, pbonzini

On Wed, Jan 25, 2023, Yu Zhang wrote:
> Hi all,
> 
>   Currently, unlike the build system of Linux kernel, KVM selftests will
> have to run "make clean && make" to rebuild the entire test suite, once
> a header file is modified.
> 
>   Is it designed like this on purpose,

My #1 rule: never assume anything weird in KVM was an explicit design choice :-)

>   or does anyone wanna change it?

Yes!

>   I hacked the makefile by using "-MD" as EXTRA_CFLAGS, so that dependency
> rules can be generated for each target object, whose prerequisites contains
> the source file and the included header files as well.
> 
>   However, this change has its own costs. E.g., new ".o" and ".d" files will
> occupy more storage. And performance-wise, the benifit could be limited,
> because for now, most header files are needed by almost every ".c" files.
> But with the evolution of KVM selftests, more ".h" files may be added. Some
> of which may be of special usage. E.g., file "include/x86_64/mce.h" is only
> used by "x86_64/ucna_injection_test". Having to rebuild the whole test suite
> just because one specific header is touched would be annoying.
> 
>   I am not sure if this change is worthy,

Absolutely worthy, I've run afoul of the lack of dependency tracking far too many
times.

> or if there's a better solution.

This part I don't know.  I would have cobbled together something long ago if I
wasn't so clueless about Makefiles.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: A question of KVM selftests' makefile
  2023-01-25  8:53 A question of KVM selftests' makefile Yu Zhang
  2023-01-25 17:26 ` Sean Christopherson
@ 2023-01-25 18:46 ` Paolo Bonzini
  2023-01-27 14:29   ` Yu Zhang
  1 sibling, 1 reply; 4+ messages in thread
From: Paolo Bonzini @ 2023-01-25 18:46 UTC (permalink / raw)
  To: Yu Zhang, kvm; +Cc: seanjc

On 1/25/23 09:53, Yu Zhang wrote:
>   x := $(shell mkdir -p $(sort $(dir $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ))))
>   $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c
> -       $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
> +       $(CC) $(CFLAGS) $(CPPFLAGS) $(EXTRA_CFLAGS) $(TARGET_ARCH) -c $< -o $@

Yes, this all looks very good.  Feel free to post it as a patch!

Paolo


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: A question of KVM selftests' makefile
  2023-01-25 18:46 ` Paolo Bonzini
@ 2023-01-27 14:29   ` Yu Zhang
  0 siblings, 0 replies; 4+ messages in thread
From: Yu Zhang @ 2023-01-27 14:29 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: kvm, seanjc

On Wed, Jan 25, 2023 at 07:46:47PM +0100, Paolo Bonzini wrote:
> On 1/25/23 09:53, Yu Zhang wrote:
> >   x := $(shell mkdir -p $(sort $(dir $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ))))
> >   $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c
> > -       $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
> > +       $(CC) $(CFLAGS) $(CPPFLAGS) $(EXTRA_CFLAGS) $(TARGET_ARCH) -c $< -o $@
> 
> Yes, this all looks very good.  Feel free to post it as a patch!
> 
> Paolo
> 

Thank you so much, Paolo & Sean! Just posted the patch!

B.R.
Yu

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-01-27 14:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-25  8:53 A question of KVM selftests' makefile Yu Zhang
2023-01-25 17:26 ` Sean Christopherson
2023-01-25 18:46 ` Paolo Bonzini
2023-01-27 14:29   ` Yu Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox