OCAMLPACKS += xml-light2 stunnel

# -----------------------------------------------------------------------
# autogen stuff
# -----------------------------------------------------------------------

# should move this into global constants file in root and stop using
# relative paths...
AUTOGEN_DIR = ../../autogen
AUTOGEN_HELPER_DIR = ../../database

OCAMLINCLUDES += .. ../.. ../../database $(AUTOGEN_DIR) ../../xapi ../../client_records ../../util

OCAMLPACKS += sexpr http-svr rpc-light

# only gen_api requires datamodel library:
GEN_API_FILES = gen_api genOCaml ref ocaml_syntax gen_db_actions gen_db_check gen_empty_custom gen_client gen_server gen_common gen_rbac ../../database/escaping locking ../api_lowlevel gen_api_main ../datamodel ../constants ../api_errors ../api_messages ../datamodel_utils ../datamodel_values ocaml_utils ../datamodel_types ../dm_api $(AUTOGEN_HELPER_DIR)/string_marshall_helper
OCamlProgram(gen_api_main, $(GEN_API_FILES))
OCamlDocProgram(gen_api_main, $(GEN_API_FILES))

FILTER = closed # or opensource or closed or debug or implementation

$(AUTOGEN_DIR)/client.ml: gen_api_main $(AUTOGEN_DIR)/aPI.ml
	./gen_api_main -filterinternal true -filter $(FILTER) -mode client   > $(AUTOGEN_DIR)/client.ml

$(AUTOGEN_DIR)/aPI.ml: gen_api_main
	./gen_api_main -filterinternal true -filter $(FILTER) -mode api   > $(AUTOGEN_DIR)/aPI.ml

$(AUTOGEN_DIR)/server.ml: gen_api_main $(AUTOGEN_DIR)/aPI.ml
	./gen_api_main -filterinternal true -gendebug -filter $(FILTER) -mode server   > $(AUTOGEN_DIR)/server.ml

$(AUTOGEN_DIR)/db_actions.ml: gen_api_main $(AUTOGEN_DIR)/aPI.ml
	./gen_api_main -filterinternal false -filter nothing -mode db       > $(AUTOGEN_DIR)/db_actions.ml   # note: we do not filter internal fields/messages here

$(AUTOGEN_DIR)/custom_actions.ml: gen_api_main $(AUTOGEN_DIR)/aPI.ml
	./gen_api_main -filterinternal true -filter $(FILTER) -mode actions  > $(AUTOGEN_DIR)/custom_actions.ml

$(AUTOGEN_DIR)/rbac_static.ml: gen_api_main $(AUTOGEN_DIR)/aPI.ml
	./gen_api_main -filterinternal true -filter $(FILTER) -mode rbac   > $(AUTOGEN_DIR)/rbac_static.ml

$(AUTOGEN_DIR)/rbac_static.csv: gen_api_main $(AUTOGEN_DIR)/aPI.ml
	./gen_api_main -filterinternal true -gendebug -filter $(FILTER) -mode rbac   > $(AUTOGEN_DIR)/rbac_static.csv

.PHONY:autogen_idl
autogen_idl: $(AUTOGEN_DIR)/client.ml $(AUTOGEN_DIR)/aPI.ml $(AUTOGEN_DIR)/server.ml \
	$(AUTOGEN_DIR)/db_actions.ml $(AUTOGEN_DIR)/custom_actions.ml \
	$(AUTOGEN_DIR)/rbac_static.ml $(AUTOGEN_DIR)/rbac_static.csv

# -----------------------------------------------------------------------
# programs that depend on autogen files
# -----------------------------------------------------------------------

COMMON_OBJS = ref \
	../datamodel_types event_types \
	../api_errors ../api_messages ../../xapi/config_constants ../../xapi/xapi_globs \
	../constants
CLIENT_OBJS = $(AUTOGEN_DIR)/aPI $(AUTOGEN_DIR)/client event_helper
SERVER_OBJS = ../../database/escaping locking_helpers \
	$(AUTOGEN_DIR)/server \
	$(AUTOGEN_DIR)/db_actions $(AUTOGEN_DIR)/custom_actions \
	$(AUTOGEN_HELPER_DIR)/string_unmarshall_helper \
	$(AUTOGEN_HELPER_DIR)/string_marshall_helper \
	$(AUTOGEN_HELPER_DIR)/xml_spaces \
	$(AUTOGEN_HELPER_DIR)/db_action_helper \
	$(AUTOGEN_HELPER_DIR)/db_xml \
	$(AUTOGEN_HELPER_DIR)/db_remote_cache_access_v1 \
	$(AUTOGEN_HELPER_DIR)/db_rpc_common_v1 \
	$(AUTOGEN_HELPER_DIR)/db_rpc_client_v1 \
	$(AUTOGEN_HELPER_DIR)/db_remote_cache_access_v2 \
	$(AUTOGEN_HELPER_DIR)/db_rpc_common_v2 \
	$(AUTOGEN_HELPER_DIR)/db_rpc_client_v2 \
	$(AUTOGEN_HELPER_DIR)/db_cache_types \
	$(AUTOGEN_HELPER_DIR)/schema \
	$(AUTOGEN_HELPER_DIR)/db_filter \
	$(AUTOGEN_HELPER_DIR)/db_filter_types \
	$(AUTOGEN_HELPER_DIR)/db_filter_parse \
	$(AUTOGEN_HELPER_DIR)/db_filter_lex \
	$(AUTOGEN_HELPER_DIR)/db_cache \
	$(AUTOGEN_HELPER_DIR)/db_cache_impl \
	$(AUTOGEN_HELPER_DIR)/db_names \
	$(AUTOGEN_HELPER_DIR)/db_upgrade \
	$(AUTOGEN_HELPER_DIR)/db_exn \
	$(AUTOGEN_HELPER_DIR)/ref_index \
	$(AUTOGEN_HELPER_DIR)/db_backend \
	$(AUTOGEN_HELPER_DIR)/db_ref \
	$(AUTOGEN_HELPER_DIR)/backend_xml \
	$(AUTOGEN_HELPER_DIR)/generation \
	$(AUTOGEN_HELPER_DIR)/db_connections \
	$(AUTOGEN_HELPER_DIR)/db_conn_store \
	$(AUTOGEN_HELPER_DIR)/eventgen \
	$(AUTOGEN_HELPER_DIR)/master_connection \
	$(AUTOGEN_HELPER_DIR)/db_lock \
	$(AUTOGEN_HELPER_DIR)/block_device_io_errors \
	$(AUTOGEN_HELPER_DIR)/redo_log \
	../../xapi/static_vdis_list \
	../../xapi/parse_db_conf \
	../../client_records/records \
	../../client_records/record_util \
	../../util/vm_memory_constraints \
	locking ../dm_api \
	../datamodel ../datamodel_utils ../datamodel_values \
	gen_client ocaml_syntax ocaml_utils taskHelper exnHelper \
	server_helpers session_check context \
	../../xapi/pool_role \
	../../xapi/xapi_inventory \
	../../xapi/hashtbl_xml \
	../../xapi/localdb \
	../../xapi/xapi_role \
	../../xapi/db \
	rbac \
	rbac_audit \
	../../autogen/rbac_static \
	../../xapi/helper_hostname \
	../../xapi/helper_process \
	../../xapi/xmlrpc_sexpr

XAPI_CLIENT_OBJS = \
	../../util/util_globs_inventory \
	../../util/util_inventory \
	../../util/version \
	../../xapi/xapi_inventory \
	../../xapi/xapi_fist \
	../../xapi/features \
	../../license/v6rpc \
	../../license/v6daemon \
	../../license/v6errors \
	$(COMMON_OBJS) \
	$(CLIENT_OBJS)

_OCAMLPACKS = $(OCAMLPACKS)
OCAMLPACKS = 

OCamlLibrary(common, $(COMMON_OBJS))
OCamlLibrary(client, $(CLIENT_OBJS))
OCamlLibrary(server, $(SERVER_OBJS))
OCamlLibrary(xapi_client, $(XAPI_CLIENT_OBJS))

OCamlLibrary(htmldoc, htmldoc)

OCAMLPACKS=$(_OCAMLPACKS)

OCamlDocLibrary(common, $(COMMON_OBJS))
OCamlDocLibrary(client, $(CLIENT_OBJS))
OCamlDocLibrary(server, $(SERVER_OBJS))
OCamlDocPackage(xapi-client, $(XAPI_CLIENT_OBJS))

.PHONY: clean
clean:
	rm -f *~ *.opt *.run *.a *.cma *.cmxa *.cmo *.cmi *.o *.cmx *.omc *.annot doc/* xmlrpc_doc/* aPI.ml client.ml server.ml rbac_static.ml rbac_static.csv gen_api gen_api.run gen_api.opt write_out_api write_out_client write_out_db_actions write_out_server gen_api_main

.PHONY: lib-install
META: META.in
	sed 's/@VERSION@/$(PRODUCT_VERSION)/g' < $< > $@

if $(defined DESTDIR)
	INSTALL_PATH = $(DESTDIR)/$(shell ocamlfind printconf destdir)
	export
else
	INSTALL_PATH = $(shell ocamlfind printconf destdir)
	export

DATAMODEL_PACK_DIR=/tmp/xapi-datamodel

lib-install: META
	mkdir -p $(INSTALL_PATH)
	ocamlfind install -destdir $(INSTALL_PATH) -ldconf ignore xapi-client META $(addsuffixes .cmi, $(XAPI_CLIENT_OBJS)) $(if $(BYTE_ENABLED), xapi_client.cma) $(if $(NATIVE_ENABLED), xapi_client.cmxa xapi_client.a $(addsuffixes .cmx, $(XAPI_CLIENT_OBJS)))

.PHONY: lib-uninstall
lib-uninstall:
	ocamlfind remove -destdir $(INSTALL_PATH) xapi-client

