
# Copyright (C) 2020-2022, 2025 Tommi Höynälänmaa

# This file is part of Theme-D-Intr.

# You can redistribute and/or modify this file under the terms of the
# GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any
# later version.

# This file 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, see https://www.gnu.org/licenses/.


.PHONY: all clean link-clean

ifdef THEME_D_LIB_DIR
  MODULE_OPTION = -m ../..:$(THEME_D_LIB_DIR):
else
  MODULE_OPTION = -m ../..:
endif

ifdef THEME_D_LIB_DIR
  FULL_MODULE_OPTION = -M ../..:$(THEME_D_LIB_DIR):
else
  FULL_MODULE_OPTION = -M ../..:
endif

GEN_NAME := "(intr-gtk4-examples calc2 _intr-imports)"
INTR_MODULE := "(intr-gtk4-examples calc2 _intr-imports-target)"

EXTRA_COMP_OPTIONS ?=
EXTRA_LINK_OPTIONS ?=

TDC ?= theme-d-compile

TDL ?= theme-d-link

GEN_INTF := generate-intr-interface
GEN_BODY := generate-intr-body
GEN_TM := generate-intr-target-module

MODULES := _intr-imports ui-utils plugin-ui utils plugin-model
INTERFACES := $(patsubst %,%.tci,$(MODULES))
BODIES := $(patsubst %,%.tcb,$(MODULES))

PLUGINS := error-functions gamma-functions rect-and-polar

.PRECIOUS: plugins/rect-and-polar.tci plugins/gamma-functions.tci plugins/error-functions.tci

all: main.go \
  $(patsubst %,plugins/%.go,$(PLUGINS))

clean:
	-rm main.go main.tcp main.thp _intr-imports.tci _intr-imports.tcb \
	  _intr-imports.thi _intr-imports.thb _intr-imports-target.scm
	-rm _intr-imports-target.go
	-rm ui-utils.tci ui-utils.tcb
	-rm utils.tci utils.tcb
	-rm plugin-dialog-impl.go plugin-dialog-ui.ui scheme-utils.go
	-rm plugin-root-dialog-impl.go plugin-root-dialog-ui.ui
	-rm plugin-model.tci plugin-model.tcb
	-rm plugin-ui.tci plugin-ui.tcb
	#-rm ui-interface.tci ui-interface.tcb
	-rm current-filename.go
	-rm __impl_*
	-rm __intf_*
	-rm plugins/*.tci plugins/*.tcb plugins/*.go

link-clean:
	-rm *.go
	-rm plugins/*.go
	-rm *.aux
	-rm plugins/*.aux
	-rm $(patsubst %,__intf_%.scm,$(MODULES))
	-rm $(patsubst %,__impl_%.scm,$(MODULES))
	-rm $(patsubst %,__intf_%.tree-il,$(MODULES))
	-rm $(patsubst %,__impl_%.tree-il,$(MODULES))

main.thp: main.thp.in
	sed -e "s|@programdir@|$$(pwd)|g" $< > $@

main.go: main.tcp _intr-imports-target.go
	$(TDL) --module $(FULL_MODULE_OPTION) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x $(INTR_MODULE) \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="@extra_guild_options@" \
	  $(EXTRA_LINK_OPTIONS) -o $@ $<

__intf__intr-imports.go __intf_ui-utils.go __intf_utils.go __intf_plugin-model.go : __intf_%.go : \
  %.tci _intr-imports-target.go
	$(TDL) --module $(FULL_MODULE_OPTION) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x $(INTR_MODULE) \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="@extra_guild_options@" \
	  $(EXTRA_LINK_OPTIONS) -o $@ $<

__intf_plugin-ui.go : plugin-ui.tci plugin-root-dialog-impl.go plugin-dialog-impl.go \
  plugin-root-dialog-impl.go plugin-dialog-impl.go \
  plugin-root-dialog-ui.ui plugin-dialog-ui.ui \
  _intr-imports-target.go
	$(TDL) --module $(FULL_MODULE_OPTION) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x "(intr-gtk4-examples calc2 plugin-root-dialog-impl)" \
	  -x "(intr-gtk4-examples calc2 plugin-dialog-impl)" \
	  -x $(INTR_MODULE) \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="@extra_guild_options@" \
	  $(EXTRA_LINK_OPTIONS) -o $@ $<

__impl__intr-imports.go : _intr-imports.tcb __intf__intr-imports.go \
  _intr-imports-target.go
	$(TDL) --module $(FULL_MODULE_OPTION) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x $(INTR_MODULE) \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="@extra_guild_options@" \
	  $(EXTRA_LINK_OPTIONS) -o $@ $<

__impl_ui-utils.go : ui-utils.tcb _intr-imports-target.go
	$(TDL) --module $(FULL_MODULE_OPTION) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x $(INTR_MODULE) \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="@extra_guild_options@" \
	  $(EXTRA_LINK_OPTIONS) -o $@ $<

__impl_plugin-ui.go : plugin-ui.tcb _intr-imports-target.go \
  plugin-root-dialog-impl.go plugin-dialog-impl.go \
  plugin-root-dialog-ui.ui plugin-dialog-ui.ui \
  scheme-utils.go
	$(TDL) --module $(FULL_MODULE_OPTION) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x $(INTR_MODULE) \
	  -x "(intr-gtk4-examples calc2 plugin-root-dialog-impl)" \
	  -x "(intr-gtk4-examples calc2 plugin-dialog-impl)" \
	  -x "(intr-gtk4-examples calc2 scheme-utils)" \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="@extra_guild_options@" \
	  $(EXTRA_LINK_OPTIONS) -o $@ $<

__impl_utils.go : utils.tcb scheme-utils.go current-filename.go _intr-imports-target.go
	$(TDL) --module $(FULL_MODULE_OPTION) \
	  -x "(intr-gtk4-examples calc2 scheme-utils)" \
	  -x "(intr-gtk4-examples calc2 current-filename)" \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="@extra_guild_options@" \
	  $(EXTRA_LINK_OPTIONS) -o $@ $<

__impl_plugin-model.go : plugin-model.tcb scheme-utils.go _intr-imports-target.go
	$(TDL) --module $(FULL_MODULE_OPTION) \
	  -x "(intr-gtk4-examples calc2 scheme-utils)" \
	  -x $(INTR_MODULE) \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="@extra_guild_options@" \
	  $(EXTRA_LINK_OPTIONS) -o $@ $<

_intr-imports-target.go plugin-dialog-impl.go plugin-root-dialog-impl.go scheme-utils.go current-filename.go : %.go : %.scm
	GUILE_LOAD_COMPILED_PATH=../..:$$GUILE_LOAD_COMPILED_PATH guild compile -o $@ $<

plugin-dialog-impl.go plugin-root-dialog-impl.go : current-filename.go

main.tcp: _intr-imports.tci plugin-model.tci plugin-ui.tci utils.tci ui-utils.tci

$(patsubst %,plugins/%.go,$(PLUGINS)) : %.go : %.tcb
	$(TDL) --plugin -M plugins: $(EXTRA_LINK_OPTIONS) -o $@ $<

$(patsubst %,plugins/%.tcb,$(PLUGINS)) : %.tcb : %.tci

plugins/%.tci : plugins/%.thi
	$(TDC) -m plugins: $(EXTRA_COMP_OPTIONS) -o $@ $<

plugins/%.tcb : plugins/%.thb plugins/%.tci
	$(TDC) -m plugins: $(EXTRA_COMP_OPTIONS) -o $@ $<

_intr-imports.thi: intr-imports.scm
	$(GEN_INTF) -m $(GEN_NAME)

_intr-imports.thb: intr-imports.scm
	$(GEN_BODY) -m $(GEN_NAME)

_intr-imports-target.scm: intr-imports.scm
	$(GEN_TM) -m $(INTR_MODULE)

_intr-imports.tcb: _intr-imports.tci

plugin-dialog-ui.ui : plugin-dialog-ui.scm
	guile -l plugin-dialog-ui.scm -c "(make-ui)" > $@

plugin-root-dialog-ui.ui : plugin-root-dialog-ui.scm
	guile -l plugin-root-dialog-ui.scm -c "(make-ui)" > $@

%.tci: %.thi
	$(TDC) $(MODULE_OPTION) $(EXTRA_COMP_OPTIONS) $<

%.tcb: %.thb
	$(TDC) $(MODULE_OPTION) $(EXTRA_COMP_OPTIONS) $<

%.tcp: %.thp
	$(TDC) $(MODULE_OPTION) $(EXTRA_COMP_OPTIONS) $<

include deps.mk
