Переглянути джерело

build: Added support for building QEMU as a shared library

For building QEMU as a shared lib, it must be loaded as a dylib. This breaks
compatibility with OSX < 10.4.
osy 6 роки тому
батько
коміт
bd3929e6b3
5 змінених файлів з 79 додано та 5 видалено
  1. 21 0
      Makefile
  2. 23 1
      Makefile.target
  3. 24 4
      configure
  4. 5 0
      qemu-img.c
  5. 6 0
      rules.mak

+ 21 - 0
Makefile

@@ -592,6 +592,16 @@ Makefile: $(version-obj-y)
 libqemuutil.a: $(util-obj-y) $(trace-obj-y) $(stub-obj-y)
 libqemuutil.a: $(util-obj-y) $(trace-obj-y) $(stub-obj-y)
 libvhost-user.a: $(libvhost-user-obj-y) $(util-obj-y) $(stub-obj-y)
 libvhost-user.a: $(libvhost-user-obj-y) $(util-obj-y) $(stub-obj-y)
 
 
+ifdef CONFIG_DARWIN
+QEMU_IMG_SHARED_LIBRARY=libqemu-img.dylib
+else
+QEMU_IMG_SHARED_LIBRARY=libqemu-img.so
+endif
+SOBJS=$(QEMU_IMG_SHARED_LIBRARY)
+ifdef CONFIG_SHARED_LIB
+all: $(SOBJS)
+endif
+
 ######################################################################
 ######################################################################
 
 
 COMMON_LDADDS = libqemuutil.a
 COMMON_LDADDS = libqemuutil.a
@@ -599,6 +609,11 @@ COMMON_LDADDS = libqemuutil.a
 qemu-img.o: qemu-img-cmds.h
 qemu-img.o: qemu-img-cmds.h
 
 
 qemu-img$(EXESUF): qemu-img.o $(authz-obj-y) $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
 qemu-img$(EXESUF): qemu-img.o $(authz-obj-y) $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
+
+$(QEMU_IMG_SHARED_LIBRARY): QEMU_LDFLAGS += $(LDFLAGS_SHARED)
+$(QEMU_IMG_SHARED_LIBRARY): qemu-img.o $(authz-obj-y) $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
+	$(call LINK, $(filter-out %.mak, $^))
+
 qemu-nbd$(EXESUF): qemu-nbd.o $(authz-obj-y) $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
 qemu-nbd$(EXESUF): qemu-nbd.o $(authz-obj-y) $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
 qemu-io$(EXESUF): qemu-io.o $(authz-obj-y) $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
 qemu-io$(EXESUF): qemu-io.o $(authz-obj-y) $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
 qemu-storage-daemon$(EXESUF): qemu-storage-daemon.o $(authz-obj-y) $(block-obj-y) $(crypto-obj-y) $(chardev-obj-y) $(io-obj-y) $(qom-obj-y) $(storage-daemon-obj-y) $(COMMON_LDADDS)
 qemu-storage-daemon$(EXESUF): qemu-storage-daemon.o $(authz-obj-y) $(block-obj-y) $(crypto-obj-y) $(chardev-obj-y) $(io-obj-y) $(qom-obj-y) $(storage-daemon-obj-y) $(COMMON_LDADDS)
@@ -764,6 +779,7 @@ clean: recurse-clean
 		-exec rm {} +
 		-exec rm {} +
 	rm -f $(edk2-decompressed)
 	rm -f $(edk2-decompressed)
 	rm -f $(filter-out %.tlb,$(TOOLS)) $(HELPERS-y) TAGS cscope.* *.pod *~ */*~
 	rm -f $(filter-out %.tlb,$(TOOLS)) $(HELPERS-y) TAGS cscope.* *.pod *~ */*~
+	rm -f $(SOBJS)
 	rm -f fsdev/*.pod scsi/*.pod
 	rm -f fsdev/*.pod scsi/*.pod
 	rm -f qemu-img-cmds.h
 	rm -f qemu-img-cmds.h
 	rm -f ui/shader/*-vert.h ui/shader/*-frag.h
 	rm -f ui/shader/*-vert.h ui/shader/*-frag.h
@@ -930,6 +946,11 @@ install: all $(if $(BUILD_DOCS),install-doc) \
 ifneq ($(TOOLS),)
 ifneq ($(TOOLS),)
 	$(call install-prog,$(TOOLS),$(DESTDIR)$(bindir))
 	$(call install-prog,$(TOOLS),$(DESTDIR)$(bindir))
 endif
 endif
+ifdef CONFIG_SHARED_LIB
+ifneq ($(SOBJS),)
+	$(call install-so,$(SOBJS),$(DESTDIR)$(libdir))
+endif
+endif
 ifneq ($(CONFIG_MODULES),)
 ifneq ($(CONFIG_MODULES),)
 	$(INSTALL_DIR) "$(DESTDIR)$(qemu_moddir)"
 	$(INSTALL_DIR) "$(DESTDIR)$(qemu_moddir)"
 	for s in $(modules-m:.mo=$(DSOSUF)); do \
 	for s in $(modules-m:.mo=$(DSOSUF)); do \

+ 23 - 1
Makefile.target

@@ -37,6 +37,14 @@ endif
 endif
 endif
 
 
 PROGS=$(QEMU_PROG) $(QEMU_PROGW)
 PROGS=$(QEMU_PROG) $(QEMU_PROGW)
+
+ifdef CONFIG_DARWIN
+QEMU_SHARED_LIBRARY=lib$(QEMU_PROG).dylib
+else
+QEMU_SHARED_LIBRARY=lib$(QEMU_PROG).so
+endif
+SOBJS=$(QEMU_SHARED_LIBRARY)
+
 STPFILES=
 STPFILES=
 
 
 config-target.h: config-target.h-timestamp
 config-target.h: config-target.h-timestamp
@@ -98,7 +106,11 @@ stap:
 endif
 endif
 .PHONY: stap
 .PHONY: stap
 
 
+ifdef CONFIG_SHARED_LIB
+all: $(SOBJS) stap
+else
 all: $(PROGS) stap
 all: $(PROGS) stap
+endif
 
 
 # Dummy command so that make thinks it has done something
 # Dummy command so that make thinks it has done something
 	@true
 	@true
@@ -222,6 +234,10 @@ ifdef CONFIG_DARWIN
 	$(call quiet-command,SetFile -a C $@,"SETFILE","$(TARGET_DIR)$@")
 	$(call quiet-command,SetFile -a C $@,"SETFILE","$(TARGET_DIR)$@")
 endif
 endif
 
 
+$(QEMU_SHARED_LIBRARY): QEMU_LDFLAGS += $(LDFLAGS_SHARED)
+$(QEMU_SHARED_LIBRARY): $(all-obj-y) $(COMMON_LDADDS)
+	$(call LINK, $(filter-out %.mak, $^))
+
 gdbstub-xml.c: $(TARGET_XML_FILES) $(SRC_PATH)/scripts/feature_to_c.sh
 gdbstub-xml.c: $(TARGET_XML_FILES) $(SRC_PATH)/scripts/feature_to_c.sh
 	$(call quiet-command,rm -f $@ && $(SHELL) $(SRC_PATH)/scripts/feature_to_c.sh $@ $(TARGET_XML_FILES),"GEN","$(TARGET_DIR)$@")
 	$(call quiet-command,rm -f $@ && $(SHELL) $(SRC_PATH)/scripts/feature_to_c.sh $@ $(TARGET_XML_FILES),"GEN","$(TARGET_DIR)$@")
 
 
@@ -232,7 +248,7 @@ hmp-commands-info.h: $(SRC_PATH)/hmp-commands-info.hx $(SRC_PATH)/scripts/hxtool
 	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"GEN","$(TARGET_DIR)$@")
 	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"GEN","$(TARGET_DIR)$@")
 
 
 clean: clean-target
 clean: clean-target
-	rm -f *.a *~ $(PROGS)
+	rm -f *.a *~ $(PROGS) $(SOBJS)
 	rm -f $(shell find . -name '*.[od]')
 	rm -f $(shell find . -name '*.[od]')
 	rm -f hmp-commands.h gdbstub-xml.c
 	rm -f hmp-commands.h gdbstub-xml.c
 	rm -f trace/generated-helpers.c trace/generated-helpers.c-timestamp
 	rm -f trace/generated-helpers.c trace/generated-helpers.c-timestamp
@@ -257,9 +273,15 @@ $(QEMU_PROG_FUZZ): config-devices.mak $(all-obj-y) $(COMMON_LDADDS) $(fuzz-obj-y
 endif
 endif
 
 
 install: all
 install: all
+ifdef CONFIG_SHARED_LIB
+ifneq ($(SOBJS),)
+	$(call install-so,$(SOBJS),$(DESTDIR)$(libdir))
+endif
+else
 ifneq ($(PROGS),)
 ifneq ($(PROGS),)
 	$(call install-prog,$(PROGS),$(DESTDIR)$(bindir))
 	$(call install-prog,$(PROGS),$(DESTDIR)$(bindir))
 endif
 endif
+endif
 ifdef CONFIG_TRACE_SYSTEMTAP
 ifdef CONFIG_TRACE_SYSTEMTAP
 	$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset"
 	$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset"
 	$(INSTALL_DATA) $(QEMU_PROG).stp-installed "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset/$(QEMU_PROG).stp"
 	$(INSTALL_DATA) $(QEMU_PROG).stp-installed "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset/$(QEMU_PROG).stp"

+ 24 - 4
configure

@@ -520,6 +520,7 @@ rng_none="no"
 secret_keyring=""
 secret_keyring=""
 libdaxctl=""
 libdaxctl=""
 host_device_support="yes"
 host_device_support="yes"
+shared_lib="no"
 
 
 supported_cpu="no"
 supported_cpu="no"
 supported_os="no"
 supported_os="no"
@@ -885,7 +886,8 @@ Darwin)
   darwin="yes"
   darwin="yes"
   hax="yes"
   hax="yes"
   hvf="yes"
   hvf="yes"
-  LDFLAGS_SHARED="-bundle -undefined dynamic_lookup"
+  DSOSUF=".dylib"
+  LDFLAGS_SHARED="-dynamiclib -undefined dynamic_lookup"
   if [ "$cpu" = "x86_64" ] ; then
   if [ "$cpu" = "x86_64" ] ; then
     QEMU_CFLAGS="-arch x86_64 $QEMU_CFLAGS"
     QEMU_CFLAGS="-arch x86_64 $QEMU_CFLAGS"
     QEMU_LDFLAGS="-arch x86_64 $QEMU_LDFLAGS"
     QEMU_LDFLAGS="-arch x86_64 $QEMU_LDFLAGS"
@@ -893,8 +895,8 @@ Darwin)
   cocoa="yes"
   cocoa="yes"
   audio_drv_list="coreaudio try-sdl"
   audio_drv_list="coreaudio try-sdl"
   audio_possible_drivers="coreaudio sdl"
   audio_possible_drivers="coreaudio sdl"
-  QEMU_LDFLAGS="-framework CoreFoundation -framework IOKit $QEMU_LDFLAGS"
-  libs_softmmu="-F/System/Library/Frameworks -framework Cocoa -framework IOKit $libs_softmmu"
+  QEMU_LDFLAGS="-framework CoreFoundation -framework IOKit -lresolv $QEMU_LDFLAGS"
+  libs_softmmu="-F/System/Library/Frameworks -framework IOKit $libs_softmmu"
   # Disable attempts to use ObjectiveC features in os/object.h since they
   # Disable attempts to use ObjectiveC features in os/object.h since they
   # won't work when we're compiling with gcc as a C compiler.
   # won't work when we're compiling with gcc as a C compiler.
   QEMU_CFLAGS="-DOS_OBJECT_USE_OBJC=0 $QEMU_CFLAGS"
   QEMU_CFLAGS="-DOS_OBJECT_USE_OBJC=0 $QEMU_CFLAGS"
@@ -907,7 +909,8 @@ iOS)
   ios="yes"
   ios="yes"
   hax="yes"
   hax="yes"
   hvf="yes"
   hvf="yes"
-  LDFLAGS_SHARED="-bundle -undefined dynamic_lookup"
+  DSOSUF=".dylib"
+  LDFLAGS_SHARED="-dynamiclib"
   if [ "$cpu" = "x86_64" ] ; then
   if [ "$cpu" = "x86_64" ] ; then
     QEMU_CFLAGS="-arch x86_64 $QEMU_CFLAGS"
     QEMU_CFLAGS="-arch x86_64 $QEMU_CFLAGS"
     QEMU_LDFLAGS="-arch x86_64 $QEMU_LDFLAGS"
     QEMU_LDFLAGS="-arch x86_64 $QEMU_LDFLAGS"
@@ -1679,6 +1682,17 @@ for opt do
   ;;
   ;;
   --disable-libdaxctl) libdaxctl=no
   --disable-libdaxctl) libdaxctl=no
   ;;
   ;;
+  --enable-shared-lib)
+      shared_lib=yes
+      want_tools=no
+      cocoa=no
+      gtk=no
+      sdl=no
+      QEMU_CFLAGS="$QEMU_CFLAGS -fPIC"
+      DSOSUF="-module$DSOSUF" # fix for make 2.81 not building target order correctly
+  ;;
+  --disable-shared-lib) shared_lib=no
+  ;;
   *)
   *)
       echo "ERROR: unknown option $opt"
       echo "ERROR: unknown option $opt"
       echo "Try '$0 --help' for more information"
       echo "Try '$0 --help' for more information"
@@ -1871,6 +1885,7 @@ Advanced options (experts only):
                            enable plugins via shared library loading
                            enable plugins via shared library loading
   --disable-containers     don't use containers for cross-building
   --disable-containers     don't use containers for cross-building
   --gdb=GDB-path           gdb to use for gdbstub tests [$gdb_bin]
   --gdb=GDB-path           gdb to use for gdbstub tests [$gdb_bin]
+  --enable-shared-lib      build QEMU as a shared library instead of an application
 
 
 Optional features, enabled with --enable-FEATURE and
 Optional features, enabled with --enable-FEATURE and
 disabled with --disable-FEATURE, default is enabled if available:
 disabled with --disable-FEATURE, default is enabled if available:
@@ -2404,6 +2419,7 @@ if test "$cocoa" = "yes"; then
     if test "$gtk" = "yes"; then
     if test "$gtk" = "yes"; then
         error_exit "Cocoa and GTK UIs cannot both be enabled at once"
         error_exit "Cocoa and GTK UIs cannot both be enabled at once"
     fi
     fi
+    libs_softmmu="$libs_softmmu -framework Cocoa"
     gtk=no
     gtk=no
     sdl=no
     sdl=no
 fi
 fi
@@ -7059,6 +7075,7 @@ echo "gdb               $gdb_bin"
 echo "rng-none          $rng_none"
 echo "rng-none          $rng_none"
 echo "Linux keyring     $secret_keyring"
 echo "Linux keyring     $secret_keyring"
 echo "host device support  $host_device_support"
 echo "host device support  $host_device_support"
+echo "build shared lib  $shared_lib"
 
 
 if test "$supported_cpu" = "no"; then
 if test "$supported_cpu" = "no"; then
     echo
     echo
@@ -7132,6 +7149,9 @@ fi
 if test "$host_device_support" = "yes" ; then
 if test "$host_device_support" = "yes" ; then
   echo "CONFIG_HOST_DEVICE=y" >> $config_host_mak
   echo "CONFIG_HOST_DEVICE=y" >> $config_host_mak
 fi
 fi
+if test "$shared_lib" = "yes" ; then
+  echo "CONFIG_SHARED_LIB=y" >> $config_host_mak
+fi
 if test "$debug_tcg" = "yes" ; then
 if test "$debug_tcg" = "yes" ; then
   echo "CONFIG_DEBUG_TCG=y" >> $config_host_mak
   echo "CONFIG_DEBUG_TCG=y" >> $config_host_mak
 fi
 fi

+ 5 - 0
qemu-img.c

@@ -50,6 +50,11 @@
 #include "crypto/init.h"
 #include "crypto/init.h"
 #include "trace/control.h"
 #include "trace/control.h"
 
 
+#if defined(CONFIG_SHARED_LIB)
+#undef main
+#define main qemu_img_main
+#endif
+
 #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
 #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
                           "\n" QEMU_COPYRIGHT "\n"
                           "\n" QEMU_COPYRIGHT "\n"
 
 

+ 6 - 0
rules.mak

@@ -154,6 +154,12 @@ define install-prog
 	$(if $(STRIP),$(STRIP) $(foreach T,$1,"$2/$(notdir $T)"),)
 	$(if $(STRIP),$(STRIP) $(foreach T,$1,"$2/$(notdir $T)"),)
 endef
 endef
 
 
+# install-so list, dir
+define install-so
+    $(INSTALL_DIR) "$2"
+    $(INSTALL_LIB) $1 "$2"
+endef
+
 # find-in-path
 # find-in-path
 # Usage: $(call find-in-path, prog)
 # Usage: $(call find-in-path, prog)
 # Looks in the PATH if the argument contains no slash, else only considers one
 # Looks in the PATH if the argument contains no slash, else only considers one