###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
#          text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2025 by The BRLTTY Developers.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU Lesser General Public License, as published by the Free Software
# Foundation; either version 2.1 of the License, or (at your option) any
# later version. Please see the file LICENSE-LGPL for details.
#
# Web Page: http://brltty.app/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

include $(SRC_TOP)bindings.mk

OCAML_OK = @OCAML_OK@
OCAMLC = @OCAMLC@
OCAMLOPT = @OCAMLOPT@
OCAMLFLAGS = -I +unix
OCAMLMKLIB = @OCAMLMKLIB@
OCAMLMKLIB_FLAGS = -dllib dll$(API_NAME)_stubs.so -cclib -l$(API_NAME)_stubs -cclib -l$(API_NAME)
OCAMLMKLIBOPT_FLAGS = -cclib -l$(API_NAME)_stubs -cclib -l$(API_NAME)
OCAMLBEST = @OCAMLBEST@
OCAMLVERSION = @OCAMLVERSION@
OCAMLLIB = @OCAMLLIB@
OCAMLWIN32 = @OCAMLWIN32@
OCAMLFIND = @OCAMLFIND@
OCAML_INSTALL_TARGET = @OCAML_INSTALL_TARGET@
OCAML_UNINSTALL_TARGET = @OCAML_UNINSTALL_TARGET@

OCAML_LIB = $(API_NAME)
OCAML_BCLIB = $(OCAML_LIB).cma
OCAML_NCLIB = @OCAML_NCLIB@

OCAML_MISC = $(SRC_DIR)/META brlapi.mli brlapi.cmi

OCAML_CLIBS = @OCAML_CLIBS@

OCAML_DESTDIR = $(INSTALL_ROOT)$(OCAMLLIB)
OCAML_PKGDIR = $(OCAML_DESTDIR)/$(OCAML_LIB)
OCAML_STUBDIR = $(OCAML_DESTDIR)/stublibs
OCAML_STUB = dll$(OCAML_LIB)_stubs.so
OCAML_OWNER = $(OCAML_STUB).owner

OCAML_FILES = $(OCAML_MISC) $(OCAML_CLIBS) $(OCAML_BCLIB) $(OCAML_NCLIB)

.PHONY: all clean
.PHONY: install install-with-findlib install-without-findlib
.PHONY: uninstall uninstall-with-findlib uninstall-without-findlib

all: $(OCAML_FILES)

$(OCAML_CLIBS): brlapi_stubs.$O | brlapi
	$(OCAMLMKLIB) -ldopt "$(LDFLAGS)" $(API_LDFLAGS) -oc $(OCAML_LIB)_stubs brlapi_stubs.$O

$(OCAML_BCLIB): $(OCAML_CLIBS) brlapi.cmo
	$(OCAMLC) -a $(OCAMLMKLIB_FLAGS) -o $(OCAML_LIB).cma brlapi.cmo

$(OCAML_NCLIB): $(OCAML_CLIBS) brlapi.cmx
	$(OCAMLMKLIB) -ldopt "$(LDFLAGS)" $(OCAMLMKLIBOPT_FLAGS) -o $(OCAML_LIB) brlapi.cmx

brlapi.cmi: brlapi.mli
	$(OCAMLC) $(OCAMLFLAGS) -cc $(CC) -ccopt "$(LIBCFLAGS)" -o $@ -c brlapi.mli

brlapi.cmo: brlapi.ml brlapi.cmi
	$(OCAMLC) $(OCAMLFLAGS) -cc $(CC) -ccopt "$(LIBCFLAGS)" -o $@ -c brlapi.ml

brlapi.cmx: brlapi.ml brlapi.cmi
	$(OCAMLOPT) $(OCAMLFLAGS) -cc $(CC) -ccopt "$(LIBCFLAGS)" -o $@ -c brlapi.cmx brlapi.ml

brlapi_stubs.$O: $(SRC_DIR)/brlapi_stubs.c
	$(OCAMLC) -cc $(CC) -ccopt "$(LIBCFLAGS)" -I $(BLD_TOP)$(PGM_DIR) -I $(SRC_TOP)$(PGM_DIR) -c $(SRC_DIR)/brlapi_stubs.c

brlapi.mli: $(SRC_DIR)/main.mli constants.mli
	cat $(SRC_DIR)/main.mli constants.mli > $@

brlapi.ml: $(SRC_DIR)/main.ml constants.ml
	cat $(SRC_DIR)/main.ml constants.ml > $@

constants.mli: constants.ml
	$(OCAMLC) $(OCAMLFLAGS) -o /dev/null -c -i constants.ml > $@

constants.ml: $(CONSTANTS_DEPENDENCIES)
	$(AWK) $(CONSTANTS_ARGUMENTS) >$@

clean::
	-rm -f -- *.cm* *.a *.so constants.ml* brlapi.ml*

install: all $(OCAML_INSTALL_TARGET)

install-without-findlib:
	$(INSTALL_DIRECTORY) $(OCAML_PKGDIR)
	for file in $(OCAML_FILES); do $(INSTALL) $$file $(OCAML_PKGDIR); done
	$(INSTALL_DIRECTORY) $(OCAML_STUBDIR)
	[ ! -f "$(OCAML_PKGDIR)/$(OCAML_STUB)" ] || mv $(OCAML_PKGDIR)/$(OCAML_STUB) $(OCAML_STUBDIR)
	echo >$(OCAML_STUBDIR)/$(OCAML_OWNER) $(OCAML_LIB)

OCAML_LDCONF = $(OCAML_DESTDIR)/ld.conf
install-with-findlib:
	$(INSTALL_DIRECTORY) $(OCAML_DESTDIR)
	[ ! -d "$(OCAML_DESTDIR)/$(OCAML_LIB)" ] || \
	$(OCAMLFIND) remove -destdir "$(OCAML_DESTDIR)" $(OCAML_LIB)
	set -- -destdir "$(OCAML_DESTDIR)"; \
	[ -f "$(OCAML_LDCONF)" ] && set -- "$${@}" -ldconf "$(OCAML_LDCONF)" || set -- "$${@}" -ldconf ignore; \
	$(OCAMLFIND) install "$${@}" $(OCAML_LIB) $(OCAML_FILES) 

uninstall: $(OCAML_UNINSTALL_TARGET)

uninstall-without-findlib:
	-rm -f -r -- $(OCAML_PKGDIR)
	-rm -f -- $(OCAML_STUBDIR)/$(OCAML_STUB)
	-rm -f -- $(OCAML_STUBDIR)/$(OCAML_OWNER)

uninstall-with-findlib:
	-$(OCAMLFIND) remove -destdir "$(OCAML_DESTDIR)" $(OCAML_LIB)

