
# Copyright (C) 2020, 2025, 2026 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/.


GEN_INTF := ../../../scripts/generate-intr-interface.scm
GEN_BODY := ../../../scripts/generate-intr-body.scm
GEN_TM := ../../../scripts/generate-intr-target-module.scm

NORMAL_MODULES := support utils
ALL_INTR_MODULES := gobject variant
INTR_MODULES := variant
ALL_MODULES := $(NORMAL_MODULES) $(ALL_INTR_MODULES)

gintrlibdir = $(LIB_DIR)/theme-d-intr/base

nodist_gintrlib_DATA = \
  $(patsubst %,%.tci,$(ALL_MODULES)) \
  $(patsubst %,%.tcb,$(ALL_MODULES)) \
  $(patsubst %,__intf_%.aux,$(ALL_MODULES)) \
  $(patsubst %,__impl_%.aux,$(ALL_MODULES))

linkedlibdir = $(THEME_D_TARGET_LIB_DIR)/theme-d-intr/base

nodist_linkedlib_DATA = \
  $(patsubst %,__intf_%.go,$(ALL_MODULES)) \
  $(patsubst %,__impl_%.go,$(ALL_MODULES)) \
  $(patsubst %,__scm_%.go,$(ALL_INTR_MODULES))

EXTRA_COMP_OPTIONS ?=
EXTRA_LINK_OPTIONS ?=

%.tci : %.thi
	theme-d-compile $(EXTRA_COMP_OPTIONS) -m ../..: -o $@ $<

%.tcb : %.thb %.tci
	theme-d-compile $(EXTRA_COMP_OPTIONS) -m ../..: -o $@ $<

__scm_variant.go : __scm_variant.scm
	guild compile -o $@ $<

__scm_gobject.go : __scm_gobject.scm
	guild compile -o $@ $<

utils.tci : variant.tci

__intf_support.go : support.tci
	GUILE_LOAD_PATH=../../guile-code:$$GUILE_LOAD_PATH \
	theme-d-link --module -m ../..: $(EXTRA_LINK_OPTIONS) \
	-x "(g-golf)" -x "(guile-theme-d-intr support)" \
	--guile-target-path=..:../../guile-code: \
	$<

__intf_support.aux : __intf_support.go

__impl_support.go : support.tcb __intf_support.aux
	GUILE_LOAD_PATH=../../guile-code:$$GUILE_LOAD_PATH \
	theme-d-link --module -m ../..: $(EXTRA_LINK_OPTIONS) \
	-x "(g-golf)" -x "(guile-theme-d-intr support)" \
	--guile-target-path=..:../../guile-code: \
	$<

__impl_support.aux : __impl_support.go

__intf_utils.go : utils.tci
	GUILE_LOAD_PATH=../../guile-code:$$GUILE_LOAD_PATH \
	theme-d-link --module -m ../..: $(EXTRA_LINK_OPTIONS) \
	-x "(g-golf)" \
	-x "(guile-theme-d-intr support)" \
	-x "(guile-theme-d-intr support3)" \
	--guile-target-path=..:../../guile-code: \
	$<

__intf_utils.aux : __intf_utils.go

__impl_utils.go : utils.tcb __intf_utils.aux
	GUILE_LOAD_PATH=../../guile-code:$$GUILE_LOAD_PATH \
	theme-d-link --module -m ../..: $(EXTRA_LINK_OPTIONS) \
	-x "(g-golf)" \
	-x "(guile-theme-d-intr support)" \
	-x "(guile-theme-d-intr support3)" \
	--guile-target-path=..:../../guile-code: \
	$<

__impl_utils.aux : __impl_utils.go

$(patsubst %,__intf_%.go,$(ALL_INTR_MODULES)) : __intf_%.go : %.tci __scm_%.go
	GUILE_LOAD_PATH=../../guile-code:$$GUILE_LOAD_PATH \
	theme-d-link --module -m ../..: $(EXTRA_LINK_OPTIONS) \
	-x "(theme-d-intr base $(patsubst __intf_%.go,__scm_%,$@))" \
	--guile-target-path=..:../../guile-code: \
	$<

$(patsubst %,__intf_%.aux,$(ALL_INTR_MODULES)) : __intf_%.aux : __intf_%.go

$(patsubst %,__impl_%.go,$(ALL_INTR_MODULES)) : __impl_%.go : %.tcb %.tci __scm_%.go \
  __intf_%.aux
	GUILE_LOAD_PATH=../../guile-code:$$GUILE_LOAD_PATH \
	theme-d-link --module -m ../..: $(EXTRA_LINK_OPTIONS) \
	-x "(theme-d-intr base $(patsubst __impl_%.go,__scm_%,$@))" \
	--guile-target-path=..:../../guile-code: \
	$<

$(patsubst %,__impl_%.aux,$(ALL_INTR_MODULES)) : __impl_%.aux : __impl_%.go

$(patsubst %,%.thi,$(INTR_MODULES)) : %.thi : intr-imports-%.scm
	GUILE_LOAD_PATH=../../../guile-code:$$GUILE_LOAD_PATH \
	  $(GEN_INTF) -e -i $< -o $@ -m "(theme-d-intr base $(patsubst %.thi,%,$@))"

$(patsubst %,%.thb,$(INTR_MODULES)) : %.thb : intr-imports-%.scm %.thi
	GUILE_LOAD_PATH=../../../guile-code:$$GUILE_LOAD_PATH \
	  $(GEN_BODY) -e -i $< -o $@ -m "(theme-d-intr base $(patsubst %.thb,%,$@))"

$(patsubst %,__scm_%.scm,$(INTR_MODULES)) : __scm_%.scm : intr-imports-%.scm
	GUILE_LOAD_PATH=../../../guile-code:$$GUILE_LOAD_PATH \
	  $(GEN_TM) -e -i $< -o $@ -m "(theme-d-intr base $(patsubst %.scm,%,$@))"

# We check the entities recursively for the gobject module (no -e).

gobject.thi : intr-imports-gobject.scm
	GUILE_LOAD_PATH=../../../guile-code:$$GUILE_LOAD_PATH \
	  $(GEN_INTF) -i $< -o $@ -m "(theme-d-intr base $(patsubst %.thi,%,$@))"

gobject.thb : intr-imports-gobject.scm gobject.thi
	GUILE_LOAD_PATH=../../../guile-code:$$GUILE_LOAD_PATH \
	  $(GEN_BODY) -i $< -o $@ -m "(theme-d-intr base $(patsubst %.thb,%,$@))"

__scm_gobject.scm : intr-imports-gobject.scm
	GUILE_LOAD_PATH=../../../guile-code:$$GUILE_LOAD_PATH \
	  $(GEN_TM) -i $< -o $@ -m "(theme-d-intr base $(patsubst %.scm,%,$@))"

__impl_utils.go : __intf_variant.aux variant.tcb

mostlyclean-local: mostlyclean-local-check

.PHONY: mostlyclean-local-check

mostlyclean-local-check:
	-rm $(patsubst %,%.tci,$(ALL_MODULES))
	-rm $(patsubst %,%.tcb,$(ALL_MODULES))
	-rm $(patsubst %,__intf_%.go,$(ALL_MODULES))
	-rm $(patsubst %,__intf_%.aux,$(ALL_MODULES))
	-rm $(patsubst %,__impl_%.go,$(ALL_MODULES))
	-rm $(patsubst %,__impl_%.aux,$(ALL_MODULES))
	-rm $(patsubst %,%.thi,$(ALL_INTR_MODULES))
	-rm $(patsubst %,%.thb,$(ALL_INTR_MODULES))
	-rm $(patsubst %,__scm_%.scm,$(ALL_INTR_MODULES))
	-rm *.tree-il

