This commit adds a target for 'lkvm-static' which is linked -static.

(This can be useful to construct kvmtool binaries for minimalist
auto-test host filesystems.)

Since extra features (SDL, AIO, VNC etc.) are auto-detected in the makefile
AND some features may only work when linked one way (but not both), the
feature detection now checks for both dynamic & static success.  Intermediate
objects build with correct flags (e.g. CONFIG_HAS_AIO) for the end target.

Signed-off-by: Matt Evans <[email protected]>
---

V3:  OK, I solved the problem of "probe for dynamic optional lib, fail to
link it statically" with a second set of probes, for static optional libs,
with appropriate dependencies to tie all this back to lkvm-static.


 tools/kvm/Makefile |   98 ++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 69 insertions(+), 29 deletions(-)

diff --git a/tools/kvm/Makefile b/tools/kvm/Makefile
index ccf10fb..513bb51 100644
--- a/tools/kvm/Makefile
+++ b/tools/kvm/Makefile
@@ -143,52 +143,76 @@ else
        UNSUPP_ERR =
 endif
 
+###
+
+# Detect optional features.
+# On a given system, some libs may link statically, some may not; so, check
+# both and only build those that link!
 
 FLAGS_BFD := $(CFLAGS) -lbfd
-has_bfd := $(call try-cc,$(SOURCE_BFD),$(FLAGS_BFD))
-ifeq ($(has_bfd),y)
-       CFLAGS  += -DCONFIG_HAS_BFD
-       OBJS    += symbol.o
-       LIBS    += -lbfd
+ifeq ($(call try-cc,$(SOURCE_BFD),$(FLAGS_BFD)),y)
+       CFLAGS_DYNOPT   += -DCONFIG_HAS_BFD
+       OBJS_DYNOPT     += symbol.o
+       LIBS_DYNOPT     += -lbfd
+endif
+ifeq ($(call try-cc,$(SOURCE_BFD),$(FLAGS_BFD) -static),y)
+       CFLAGS_STATOPT  += -DCONFIG_HAS_BFD
+       OBJS_STATOPT    += symbol.o
+       LIBS_STATOPT    += -lbfd
 endif
 
 FLAGS_VNCSERVER := $(CFLAGS) -lvncserver
-has_vncserver := $(call try-cc,$(SOURCE_VNCSERVER),$(FLAGS_VNCSERVER))
-ifeq ($(has_vncserver),y)
-       OBJS    += ui/vnc.o
-       CFLAGS  += -DCONFIG_HAS_VNCSERVER
-       LIBS    += -lvncserver
+ifeq ($(call try-cc,$(SOURCE_VNCSERVER),$(FLAGS_VNCSERVER)),y)
+       OBJS_DYNOPT     += ui/vnc.o
+       CFLAGS_DYNOPT   += -DCONFIG_HAS_VNCSERVER
+       LIBS_DYNOPT     += -lvncserver
+endif
+ifeq ($(call try-cc,$(SOURCE_VNCSERVER),$(FLAGS_VNCSERVER) -static),y)
+       OBJS_STATOPT    += ui/vnc.o
+       CFLAGS_STATOPT  += -DCONFIG_HAS_VNCSERVER
+       LIBS_STATOPT    += -lvncserver
 endif
 
 FLAGS_SDL := $(CFLAGS) -lSDL
-has_SDL := $(call try-cc,$(SOURCE_SDL),$(FLAGS_SDL))
-ifeq ($(has_SDL),y)
-       OBJS    += ui/sdl.o
-       CFLAGS  += -DCONFIG_HAS_SDL
-       LIBS    += -lSDL
+ifeq ($(call try-cc,$(SOURCE_SDL),$(FLAGS_SDL)),y)
+       OBJS_DYNOPT     += ui/sdl.o
+       CFLAGS_DYNOPT   += -DCONFIG_HAS_SDL
+       LIBS_DYNOPT     += -lSDL
+endif
+ifeq ($(call try-cc,$(SOURCE_SDL),$(FLAGS_SDL) -static), y)
+       OBJS_STATOPT    += ui/sdl.o
+       CFLAGS_STATOPT  += -DCONFIG_HAS_SDL
+       LIBS_STATOPT    += -lSDL
 endif
 
 FLAGS_ZLIB := $(CFLAGS) -lz
-has_ZLIB := $(call try-cc,$(SOURCE_ZLIB),$(FLAGS_ZLIB))
-ifeq ($(has_ZLIB),y)
-       CFLAGS  += -DCONFIG_HAS_ZLIB
-       LIBS    += -lz
+ifeq ($(call try-cc,$(SOURCE_ZLIB),$(FLAGS_ZLIB)),y)
+       CFLAGS_DYNOPT   += -DCONFIG_HAS_ZLIB
+       LIBS_DYNOPT     += -lz
+endif
+ifeq ($(call try-cc,$(SOURCE_ZLIB),$(FLAGS_ZLIB) -static),y)
+       CFLAGS_STATOPT  += -DCONFIG_HAS_ZLIB
+       LIBS_STATOPT    += -lz
 endif
 
 FLAGS_AIO := $(CFLAGS) -laio
-has_AIO := $(call try-cc,$(SOURCE_AIO),$(FLAGS_AIO))
-ifeq ($(has_AIO),y)
-       CFLAGS  += -DCONFIG_HAS_AIO
-       LIBS    += -laio
+ifeq ($(call try-cc,$(SOURCE_AIO),$(FLAGS_AIO)),y)
+       CFLAGS_DYNOPT   += -DCONFIG_HAS_AIO
+       LIBS_DYNOPT     += -laio
+endif
+ifeq ($(call try-cc,$(SOURCE_AIO),$(FLAGS_AIO) -static),y)
+       CFLAGS_STATOPT  += -DCONFIG_HAS_AIO
+       LIBS_STATOPT    += -laio
 endif
 
+###
+
 LIBS   += -lrt
 LIBS   += -lpthread
 LIBS   += -lutil
 
 
 DEPS   := $(patsubst %.o,%.d,$(OBJS))
-OBJS   += $(OTHEROBJS)
 
 DEFINES        += -D_FILE_OFFSET_BITS=64
 DEFINES        += -D_GNU_SOURCE
@@ -229,9 +253,21 @@ KVMTOOLS-VERSION-FILE:
        @$(SHELL_PATH) util/KVMTOOLS-VERSION-GEN $(OUTPUT)
 -include $(OUTPUT)KVMTOOLS-VERSION-FILE
 
-$(PROGRAM): $(DEPS) $(OBJS)
+# When building -static all objects are built with appropriate flags, which
+# may differ between static & dynamic .o.  The objects are separated into
+# .o and .static.o.  See the %.o: %.c rules below.
+#
+# $(OTHEROBJS) are things that do not get substituted like this.
+#
+STATIC_OBJS = $(patsubst %.o,%.static.o,$(OBJS) $(OBJS_STATOPT))
+
+$(PROGRAM)-static:  $(DEPS) $(STATIC_OBJS) $(OTHEROBJS)
+       $(E) "  LINK    " $@
+       $(Q) $(CC) -static $(CFLAGS) $(STATIC_OBJS) $(OTHEROBJS) $(LIBS) 
$(LIBS_STATOPT) -o $@
+
+$(PROGRAM): $(DEPS) $(OBJS) $(OBJS_DYNOPT) $(OTHEROBJS)
        $(E) "  LINK    " $@
-       $(Q) $(CC) $(CFLAGS) $(OBJS) $(LIBS) -o $@
+       $(Q) $(CC) $(CFLAGS) $(OBJS) $(OBJS_DYNOPT) $(OTHEROBJS) $(LIBS) 
$(LIBS_DYNOPT) -o $@
 
 $(PROGRAM_ALIAS): $(PROGRAM)
        $(E) "  LN      " $@
@@ -258,13 +294,17 @@ builtin-help.d: $(KVM_INCLUDE)/common-cmds.h
 
 $(OBJS):
 
-util/rbtree.o: ../../lib/rbtree.c
+util/rbtree.static.o util/rbtree.o: ../../lib/rbtree.c
        $(E) "  CC      " $@
        $(Q) $(CC) -c $(CFLAGS) $< -o $@
 
+%.static.o: %.c
+       $(E) "  CC      " $@
+       $(Q) $(CC) -c $(CFLAGS) $(CFLAGS_STATOPT)  $< -o $@
+
 %.o: %.c
        $(E) "  CC      " $@
-       $(Q) $(CC) -c $(CFLAGS) $< -o $@
+       $(Q) $(CC) -c $(CFLAGS) $(CFLAGS_DYNOPT) $< -o $@
 
 
 $(KVM_INCLUDE)/common-cmds.h: util/generate-cmdlist.sh command-list.txt
@@ -325,7 +365,7 @@ clean:
        $(Q) rm -f x86/bios/bios-rom.h
        $(Q) rm -f tests/boot/boot_test.iso
        $(Q) rm -rf tests/boot/rootfs/
-       $(Q) rm -f $(DEPS) $(OBJS) $(PROGRAM) $(PROGRAM_ALIAS) $(GUEST_INIT) 
$(GUEST_INIT_S2)
+       $(Q) rm -f $(DEPS) $(OBJS) $(OTHEROBJS) $(OBJS_DYNOPT) $(STATIC_OBJS) 
$(PROGRAM) $(PROGRAM_ALIAS) $(PROGRAM)-static $(GUEST_INIT) $(GUEST_INIT_S2)
        $(Q) rm -f cscope.*
        $(Q) rm -f tags
        $(Q) rm -f TAGS
-- 
1.7.0.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to