From: Matt Evans <matt@ozlabs.org>
To: KVM list <kvm@vger.kernel.org>
Cc: Pekka Enberg <penberg@kernel.org>, Sasha Levin <levinsasha928@gmail.com>
Subject: [PATCH V3] kvm tools: Add build target for statically-linked binary
Date: Thu, 02 Feb 2012 13:47:34 +1100 [thread overview]
Message-ID: <4F29F946.7050504@ozlabs.org> (raw)
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 <matt@ozlabs.org>
---
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
next reply other threads:[~2012-02-02 2:47 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-02-02 2:47 Matt Evans [this message]
2012-02-02 3:03 ` [PATCH V3] kvm tools: Add build target for statically-linked binary Asias He
2012-02-02 6:56 ` Pekka Enberg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4F29F946.7050504@ozlabs.org \
--to=matt@ozlabs.org \
--cc=kvm@vger.kernel.org \
--cc=levinsasha928@gmail.com \
--cc=penberg@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.