customize: Build a customize.cma/customize.cmxa library.

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.
This commit is contained in:
Richard W.M. Jones
2016-07-14 12:19:14 +01:00
parent 1e2877c6f4
commit 6a0fe553b9
5 changed files with 71 additions and 65 deletions

View File

@@ -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

View File

@@ -20,11 +20,12 @@ include $(top_srcdir)/subdir-rules.mk
EXTRA_DIST = \
$(generator_built) \
$(SOURCES_MLI) $(SOURCES_ML) $(SOURCES_C) \
customize_main.ml \
test-virt-customize.sh \
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 +65,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 +75,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 +121,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 +162,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

2
customize/dummy.c Normal file
View File

@@ -0,0 +1,2 @@
/* Dummy source, to be used for OCaml-based tools with no C sources. */
enum { foo = 1 };

View File

@@ -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

2
sysprep/dummy.c Normal file
View File

@@ -0,0 +1,2 @@
/* Dummy source, to be used for OCaml-based tools with no C sources. */
enum { foo = 1 };