[Libguestfs] [PATCH 2/2] customize: Build a customize.cma/customize.cmxa library.

Richard W.M. Jones rjones at redhat.com
Thu Jul 14 13:30:13 UTC 2016


Instead of linking with individual objects, which is very tedious,
build a proper library and link virt-builder, virt-customize and
virt-sysprep to it.

This makes the binaries a tiny bit smaller because .cmxa/.a files
allow unused code to be removed by the linker, whereas explicitly
linking .cmx/.o files does not.
---
 builder/Makefile.am   | 25 ++--------------
 customize/Makefile.am | 81 +++++++++++++++++++++++++++++++++++++--------------
 customize/dummy.c     |  2 ++
 sysprep/Makefile.am   | 25 +++-------------
 sysprep/dummy.c       |  2 ++
 5 files changed, 70 insertions(+), 65 deletions(-)
 create mode 100644 customize/dummy.c
 create mode 100644 sysprep/dummy.c

diff --git a/builder/Makefile.am b/builder/Makefile.am
index d95ab29..4e77f52 100644
--- a/builder/Makefile.am
+++ b/builder/Makefile.am
@@ -89,10 +89,6 @@ SOURCES_ML = \
 	builder.ml
 
 SOURCES_C = \
-	../customize/perl_edit-c.c \
-	../customize/crypt-c.c \
-	../fish/file-edit.c \
-	../fish/file-edit.h \
 	index-scan.c \
 	index-struct.c \
 	index-parse.c \
@@ -127,22 +123,7 @@ virt_builder_CFLAGS = \
 	$(LIBXML2_CFLAGS) \
 	$(YAJL_CFLAGS)
 
-BOBJECTS = \
-	$(top_builddir)/customize/customize_utils.cmo \
-	$(top_builddir)/customize/urandom.cmo \
-	$(top_builddir)/customize/random_seed.cmo \
-	$(top_builddir)/customize/hostname.cmo \
-	$(top_builddir)/customize/timezone.cmo \
-	$(top_builddir)/customize/firstboot.cmo \
-	$(top_builddir)/customize/perl_edit.cmo \
-	$(top_builddir)/customize/crypt.cmo \
-	$(top_builddir)/customize/password.cmo \
-	$(top_builddir)/customize/SELinux_relabel.cmo \
-	$(top_builddir)/customize/ssh_key.cmo \
-	$(top_builddir)/customize/subscription_manager.cmo \
-	$(top_builddir)/customize/customize_cmdline.cmo \
-	$(top_builddir)/customize/customize_run.cmo \
-	$(SOURCES_ML:.ml=.cmo)
+BOBJECTS = $(SOURCES_ML:.ml=.cmo)
 XOBJECTS = $(BOBJECTS:.cmo=.cmx)
 
 # -I $(top_builddir)/src/.libs is a hack which forces corresponding -L
@@ -179,11 +160,11 @@ OCAMLFLAGS = $(OCAML_FLAGS) $(OCAML_WARN_ERROR)
 if !HAVE_OCAMLOPT
 OBJECTS = $(BOBJECTS)
 BEST    = c
-OCAMLLINKFLAGS = mlguestfs.cma mllib.cma -custom
+OCAMLLINKFLAGS = mlguestfs.cma mllib.cma customize.cma -custom
 else
 OBJECTS = $(XOBJECTS)
 BEST    = opt
-OCAMLLINKFLAGS = mlguestfs.cmxa mllib.cmxa
+OCAMLLINKFLAGS = mlguestfs.cmxa mllib.cmxa customize.cmxa
 endif
 
 virt_builder_DEPENDENCIES = $(OBJECTS) $(top_srcdir)/ocaml-link.sh
diff --git a/customize/Makefile.am b/customize/Makefile.am
index 39547e9..9c0d3c7 100644
--- a/customize/Makefile.am
+++ b/customize/Makefile.am
@@ -24,7 +24,7 @@ EXTRA_DIST = \
 	virt-customize.pod
 
 CLEANFILES = \
-	*~ *.annot *.cmi *.cmo *.cmx *.cmxa *.o \
+	*~ *.annot *.cmi *.cmo *.cmx *.cmxa *.o dll*.so \
 	stamp-virt-customize.pod \
 	virt-customize virt-customize.1
 
@@ -64,8 +64,7 @@ SOURCES_ML = \
 	subscription_manager.ml \
 	timezone.ml \
 	customize_cmdline.ml \
-	customize_run.ml \
-	customize_main.ml
+	customize_run.ml
 
 SOURCES_C = \
 	../fish/file-edit.c \
@@ -75,24 +74,44 @@ SOURCES_C = \
 
 if HAVE_OCAML
 
-bin_PROGRAMS = virt-customize
+# Build the virt-customize objects into a library, since these are
+# reused by virt-builder and virt-sysprep.  We pretend we are building
+# a C library.  automake handles the compilation of C sources for us.
+# At the end we take the C objects and OCaml objects and link them
+# into the OCaml library.  This C library is never used.
+noinst_LIBRARIES = libcustomize.a
 
-virt_customize_SOURCES = $(SOURCES_C)
-virt_customize_CPPFLAGS = \
+if !HAVE_OCAMLOPT
+CUSTOMIZE_CMA = customize.cma
+else
+CUSTOMIZE_CMA = customize.cmxa
+endif
+
+noinst_DATA = $(CUSTOMIZE_CMA)
+
+libcustomize_a_SOURCES = $(SOURCES_C)
+libcustomize_a_CPPFLAGS = \
 	-I. \
 	-I$(top_builddir) \
 	-I$(top_srcdir)/gnulib/lib -I$(top_builddir)/gnulib/lib \
 	-I$(shell $(OCAMLC) -where) \
 	-I$(top_srcdir)/src \
 	-I$(top_srcdir)/fish
-virt_customize_CFLAGS = \
+libcustomize_a_CFLAGS = \
 	$(WARN_CFLAGS) $(WERROR_CFLAGS) \
 	$(LIBVIRT_CFLAGS) \
-	$(LIBXML2_CFLAGS)
+	$(LIBXML2_CFLAGS) \
+	-fPIC
 
 BOBJECTS = $(SOURCES_ML:.ml=.cmo)
 XOBJECTS = $(BOBJECTS:.cmo=.cmx)
 
+if !HAVE_OCAMLOPT
+OBJECTS = $(BOBJECTS)
+else
+OBJECTS = $(XOBJECTS)
+endif
+
 # -I $(top_builddir)/src/.libs is a hack which forces corresponding -L
 # option to be passed to gcc, so we don't try linking against an
 # installed copy of libguestfs.
@@ -101,11 +120,36 @@ OCAMLPACKAGES = \
 	-I $(top_builddir)/src/.libs \
 	-I $(top_builddir)/gnulib/lib/.libs \
 	-I $(top_builddir)/ocaml \
-	-I $(top_builddir)/mllib
+	-I $(top_builddir)/mllib \
+	-I $(builddir)
 if HAVE_OCAML_PKG_GETTEXT
 OCAMLPACKAGES += -package gettext-stub
 endif
 
+libcustomize_a_DEPENDENCIES = $(OBJECTS)
+
+$(CUSTOMIZE_CMA): $(OBJECTS) libcustomize.a
+	$(OCAMLFIND) mklib -g $(OCAMLPACKAGES) \
+	    $(OBJECTS) $(libcustomize_a_OBJECTS) -o customize
+
+# Build the virt-customize program.
+bin_PROGRAMS = virt-customize
+
+virt_customize_SOURCES = dummy.c
+
+CUSTOMIZE_BOBJECTS = customize_main.cmo
+CUSTOMIZE_XOBJECTS = $(BOBJECTS:.cmo=.cmx)
+
+if !HAVE_OCAMLOPT
+CUSTOMIZE_THEOBJECTS = $(CUSTOMIZE_BOBJECTS)
+BEST    = c
+OCAMLLINKFLAGS = mlguestfs.cma mllib.cma customize.cma -custom
+else
+CUSTOMIZE_THEOBJECTS = $(CUSTOMIZE_XOBJECTS)
+BEST    = opt
+OCAMLLINKFLAGS = mlguestfs.cmxa mllib.cmxa customize.cmxa
+endif
+
 OCAMLCLIBS = \
 	-lutils \
 	$(LIBTINFO_LIBS) \
@@ -117,25 +161,18 @@ OCAMLCLIBS = \
 
 OCAMLFLAGS = $(OCAML_FLAGS) $(OCAML_WARN_ERROR)
 
-if !HAVE_OCAMLOPT
-OBJECTS = $(BOBJECTS)
-BEST    = c
-OCAMLLINKFLAGS = mlguestfs.cma mllib.cma -custom
-else
-OBJECTS = $(XOBJECTS)
-BEST    = opt
-OCAMLLINKFLAGS = mlguestfs.cmxa mllib.cmxa
-endif
-
-virt_customize_DEPENDENCIES = $(OBJECTS) $(top_srcdir)/ocaml-link.sh
+virt_customize_DEPENDENCIES = \
+	$(top_srcdir)/ocaml-link.sh \
+	$(CUSTOMIZE_THEOBJECTS) \
+	$(CUSTOMIZE_CMA)
 virt_customize_LINK = \
 	$(top_srcdir)/ocaml-link.sh -cclib '$(OCAMLCLIBS)' -- \
 	  $(OCAMLFIND) $(BEST) $(OCAMLFLAGS) $(OCAMLPACKAGES) $(OCAMLLINKFLAGS) \
-	  $(OBJECTS) -o $@
+	  $(CUSTOMIZE_THEOBJECTS) -o $@
 
 # Manual pages and HTML files for the website.
 man_MANS = virt-customize.1
-noinst_DATA = $(top_builddir)/website/virt-customize.1.html
+noinst_DATA += $(top_builddir)/website/virt-customize.1.html
 
 virt-customize.1 $(top_builddir)/website/virt-customize.1.html: stamp-virt-customize.pod
 
diff --git a/customize/dummy.c b/customize/dummy.c
new file mode 100644
index 0000000..ebab619
--- /dev/null
+++ b/customize/dummy.c
@@ -0,0 +1,2 @@
+/* Dummy source, to be used for OCaml-based tools with no C sources. */
+enum { foo = 1 };
diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am
index b4072f7..25193b2 100644
--- a/sysprep/Makefile.am
+++ b/sysprep/Makefile.am
@@ -80,9 +80,7 @@ SOURCES_ML = \
 	main.ml
 
 SOURCES_C = \
-	../customize/crypt-c.c \
-	../customize/perl_edit-c.c \
-	../fish/file-edit.c
+	dummy.c
 
 if HAVE_OCAML
 
@@ -100,22 +98,7 @@ virt_sysprep_CFLAGS = \
 	$(WARN_CFLAGS) $(WERROR_CFLAGS) \
 	$(LIBXML2_CFLAGS)
 
-BOBJECTS = \
-	$(top_builddir)/customize/customize_utils.cmo \
-	$(top_builddir)/customize/crypt.cmo \
-	$(top_builddir)/customize/urandom.cmo \
-	$(top_builddir)/customize/password.cmo \
-	$(top_builddir)/customize/random_seed.cmo \
-	$(top_builddir)/customize/hostname.cmo \
-	$(top_builddir)/customize/timezone.cmo \
-	$(top_builddir)/customize/firstboot.cmo \
-	$(top_builddir)/customize/perl_edit.cmo \
-	$(top_builddir)/customize/SELinux_relabel.cmo \
-	$(top_builddir)/customize/ssh_key.cmo \
-	$(top_builddir)/customize/subscription_manager.cmo \
-	$(top_builddir)/customize/customize_cmdline.cmo \
-	$(top_builddir)/customize/customize_run.cmo \
-	$(SOURCES_ML:.ml=.cmo)
+BOBJECTS = $(SOURCES_ML:.ml=.cmo)
 XOBJECTS = $(BOBJECTS:.cmo=.cmx)
 
 # -I $(top_builddir)/src/.libs is a hack which forces corresponding -L
@@ -145,11 +128,11 @@ OCAMLFLAGS = $(OCAML_FLAGS) $(OCAML_WARN_ERROR)
 if !HAVE_OCAMLOPT
 OBJECTS = $(BOBJECTS)
 BEST    = c
-OCAMLLINKFLAGS = mlguestfs.cma mllib.cma -custom
+OCAMLLINKFLAGS = mlguestfs.cma mllib.cma customize.cma -custom
 else
 OBJECTS = $(XOBJECTS)
 BEST    = opt
-OCAMLLINKFLAGS = mlguestfs.cmxa mllib.cmxa
+OCAMLLINKFLAGS = mlguestfs.cmxa mllib.cmxa customize.cmxa
 endif
 
 virt_sysprep_DEPENDENCIES = $(OBJECTS) $(top_srcdir)/ocaml-link.sh
diff --git a/sysprep/dummy.c b/sysprep/dummy.c
new file mode 100644
index 0000000..ebab619
--- /dev/null
+++ b/sysprep/dummy.c
@@ -0,0 +1,2 @@
+/* Dummy source, to be used for OCaml-based tools with no C sources. */
+enum { foo = 1 };
-- 
2.7.4




More information about the Libguestfs mailing list