# libguestfs OCaml tools common code
# Copyright (C) 2011-2013 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

include $(top_srcdir)/subdir-rules.mk

EXTRA_DIST = \
	$(SOURCES)

CLEANFILES = *~ *.cmi *.cmo *.cmx *.cmxa *.o

# Alphabetical order.
SOURCES = \
	common_gettext.ml \
	common_utils.ml \
	common_utils_tests.ml \
	config.ml \
	crypt-c.c \
	crypt.ml \
	crypt.mli \
	firstboot.mli \
	firstboot.ml \
	fsync-c.c \
	fsync.mli \
	fsync.ml \
	hostname.mli \
	hostname.ml \
	password.mli \
	password.ml \
	progress-c.c \
	progress.mli \
	progress.ml \
	random_seed.mli \
	random_seed.ml \
	tty-c.c \
	tTY.mli \
	tTY.ml \
	urandom.mli \
	urandom.ml \
	uri-c.c \
	uRI.mli \
	uRI.ml

if HAVE_OCAML

# Notes:
# - We're not actually building a functioning program here, we're just
#   linking everything together to check all the modules build OK.
# - This list must be in dependency order.
OBJECTS = \
	$(top_builddir)/fish/guestfish-progress.o \
	$(top_builddir)/fish/guestfish-uri.o \
	tty-c.o \
	fsync-c.o \
	progress-c.o \
	uri-c.o \
	crypt-c.o \
	config.cmx \
	libdir.cmx \
	common_gettext.cmx \
	common_utils.cmx \
	urandom.cmx \
	random_seed.cmx \
	hostname.cmx \
	firstboot.cmx \
	tTY.cmx \
	fsync.cmx \
	progress.cmx \
	uRI.cmx \
	crypt.cmx \
	password.cmx

noinst_SCRIPTS = dummy

# -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.
OCAMLPACKAGES = -package str,unix -I $(top_builddir)/src/.libs -I ../ocaml
if HAVE_OCAML_PKG_GETTEXT
OCAMLPACKAGES += -package gettext-stub
endif

OCAMLCFLAGS = -g -warn-error CDEFLMPSUVYZX $(OCAMLPACKAGES)
OCAMLOPTFLAGS = $(OCAMLCFLAGS)

OCAMLCLIBS  = \
	$(LIBXML2_LIBS) -lncurses -lcrypt \
	-L../src/.libs -lutils \
	-L../gnulib/lib/.libs -lgnu

dummy: $(OBJECTS)
	$(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) \
	  mlguestfs.cmxa -linkpkg $^ \
	  -cclib '$(OCAMLCLIBS)' \
	  $(OCAML_GCOV_LDFLAGS) \
	  -o $@

.mli.cmi:
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) -c $< -o $@
.ml.cmo:
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) -c $< -o $@
.ml.cmx:
	$(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) -c $< -o $@

# This OCaml module has to be generated by make (configure will put
# unexpanded prefix macro in).

libdir.ml: Makefile
	echo 'let libdir = "$(libdir)"' > $@-t
	mv $@-t $@

# automake will decide we don't need C support in this file.  Really
# we do, so we have to provide it ourselves.

DEFAULT_INCLUDES = \
	-I. \
	-I$(top_builddir) \
	-I$(shell $(OCAMLC) -where) \
	-I$(top_srcdir)/src \
	-I$(top_srcdir)/fish

.c.o:
	$(CC) $(CFLAGS) $(PROF_CFLAGS) $(DEFAULT_INCLUDES) -c $< -o $@

# Tests.

check_SCRIPTS = common_utils_tests

common_utils_tests: common_gettext.cmx common_utils.cmx common_utils_tests.cmx
	$(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) \
	  mlguestfs.cmxa -linkpkg $^ -cclib -lncurses -o $@

TESTS_ENVIRONMENT = $(top_builddir)/run --test

TESTS = common_utils_tests

check-valgrind:
	$(MAKE) VG="$(top_builddir)/run @VG@" check

# Dependencies.
depend: .depend

.depend: $(wildcard $(abs_srcdir)/*.mli) $(wildcard $(abs_srcdir)/*.ml)
	rm -f $@ $@-t
	$(OCAMLFIND) ocamldep -I ../ocaml -I $(abs_srcdir) $^ | \
	  $(SED) 's/ *$$//' | \
	  $(SED) -e :a -e '/ *\\$$/N; s/ *\\\n */ /; ta' | \
	  $(SED) -e 's,$(abs_srcdir)/,$(builddir)/,g' | \
	  sort > $@-t
	mv $@-t $@

-include .depend

endif

DISTCLEANFILES = .depend

.PHONY: depend docs
