
# 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
TDC := theme-d-compile
TDL := theme-d-link

NORMAL_MODULES := interfaces types wrappers
INTR_MODULES := connection utils node-info
ALL_MODULES := $(NORMAL_MODULES) $(INTR_MODULES)
PROGS := dbus-introspect gen-dbus-theme-d-body gen-dbus-theme-d-interface

MODULE_OPTION := -m ../..:

GEN_NAME := "(theme-d-intr dbus _intr-imports)"
INTR_MODULE := "(theme-d-intr dbus _intr-imports-target)"

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

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/dbus

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

GUILEBASEDIR = $(shell pkg-config --variable=siteccachedir guile-$(MY_GUILE_VERSION))
guilemoddir = $(GUILEBASEDIR)/theme-d-intr/dbus

nodist_guilemod_DATA = \
  _intr-imports-target.go

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 $@ $<

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

$(patsubst %,__scm_%.go,$(INTR_MODULES)) : __scm_%.go : __scm_%.scm
	guild compile -o $@ $<

$(patsubst %,__intf_%.go,$(NORMAL_MODULES)) : __intf_%.go : %.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: \
	$<

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

$(patsubst %,__impl_%.go,$(NORMAL_MODULES)) : __impl_%.go : %.tcb __intf_%.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: \
	$<

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

$(patsubst %,__intf_%.go,$(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 dbus $(patsubst __intf_%.go,__scm_%,$@))" \
	--guile-target-path=..:../../guile-code: \
	$<

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

$(patsubst %,__impl_%.go,$(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 dbus $(patsubst __impl_%.go,__scm_%,$@))" \
	--guile-target-path=..:../../guile-code: \
	$<

$(patsubst %,__impl_%.aux,$(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 dbus $(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 dbus $(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 dbus $(patsubst %.scm,%,$@))"

__impl_connection.go : __intf_utils.aux utils.tcb

__impl_node-info.go : __intf_utils.aux utils.tcb

__impl_interfaces.go : __intf_types.aux __intf_wrappers.aux \
  types.tcb wrappers.tcb

__impl_wrappers.go : __intf_types.aux __intf_connection.aux \
  types.tcb connection.tcb

dbus-introspect.tcp : dbus-introspect.thp connection.tci

interfaces.tci : types.tci wrappers.tci

node-info.tci : utils.tci

connection.tci : utils.tci

wrappers.tci : types.tci

wrappers.tcb : connection.tci

gen-dbus-theme-d-interface.tcp : interfaces.tci

gen-dbus-theme-d-body.tcp : interfaces.tci

dbus-introspect.go : dbus-introspect.tcp connection.tcb utils.tcb \
  _intr-imports-target.go
	$(TDL) $(MODULE_OPTION) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x "(theme-d-intr dbus _intr-imports-target)" \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="-O1" \
	  $(EXTRA_LINK_OPTIONS) $<

gen-dbus-theme-d-interface.go : gen-dbus-theme-d-interface.tcp interfaces.tcb types.tcb wrappers.tcb \
  connection.tcb utils.tcb
	$(TDL) $(MODULE_OPTION) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x "(guile-theme-d-intr support3)" \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="-O1" \
	  $(EXTRA_LINK_OPTIONS) $<

gen-dbus-theme-d-body.go : gen-dbus-theme-d-body.tcp interfaces.tcb types.tcb wrappers.tcb
	$(TDL) $(MODULE_OPTION) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x "(guile-theme-d-intr support3)" \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="-O1" \
	  $(EXTRA_LINK_OPTIONS) $<

_intr-imports-target.scm: intr-imports.scm
	GUILE_LOAD_PATH=../../../guile-code:$$GUILE_LOAD_PATH \
	  $(GEN_TM) -m $(INTR_MODULE)

_intr-imports-target.go : _intr-imports-target.scm
	guild compile -O1 -o $@ $<

mostlyclean-local: mostlyclean-local-check

.PHONY: mostlyclean-local-check

mostlyclean-local-check:
	-rm $(patsubst %,%.tci,$(ALL_MODULES))
	-rm $(patsubst %,%.tcb,$(ALL_MODULES))
	-rm $(patsubst %,%.tcp,$(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,$(INTR_MODULES))
	-rm $(patsubst %,%.thb,$(INTR_MODULES))
	-rm $(patsubst %,__scm_%.scm,$(INTR_MODULES))
	-rm $(patsubst %,%.go,$(PROGS))
	-rm *.tree-il
