
# Copyright (C) 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/.


.PHONY: all clean

PROGS := dbus-test-server1 dbus-test-client1 dbus-test-client1-2

MODULES := org_gnu_test_DBus

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

GEN_NAME := "(intr-dbus-examples server-and-client-1 _intr-imports)"
INTR_MODULE := "(intr-dbus-examples server-and-client-1 _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

all: $(patsubst %,%.go,$(PROGS))

clean:
	-rm $(patsubst %,%.go,$(PROGS))
	-rm $(patsubst %,%.tcp,$(PROGS))
	-rm org_gnu_test_DBus.*
	-rm _intr-imports.tci _intr-imports.tcb \
	  _intr-imports.thi _intr-imports.thb _intr-imports-target.scm

ifdef LINK_MODULES

$(patsubst %,__intf_%.go,$(MODULES)) : __intf_%.go : %.tci
	$(TDL) --module $(MODULE_OPTION2) $(EXTRA_LINK_OPTIONS) \
	-x "(g-golf)" -x "(guile-theme-d-intr support)" \
	--duplicates="merge-generics replace warn-override-core warn last" \
	--extra-guild-options="@extra_guild_options@" \
	$<

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

$(patsubst %,__impl_%.go,$(MODULES)) : __impl_%.go : %.tcb __intf_%.aux
	$(TDL) --module $(MODULE_OPTION2) $(EXTRA_LINK_OPTIONS) \
	-x "(g-golf)" -x "(guile-theme-d-intr support)" \
	-x "(theme-d-intr base __scm_variant)" \
	-x "(theme-d-intr dbus __scm_connection)" \
	--duplicates="merge-generics replace warn-override-core warn last" \
	--extra-guild-options="@extra_guild_options@" \
	$<

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

dbus-test-server1.go : dbus-test-server1.tcp
	$(TDL) --module $(MODULE_OPTION2) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x "(theme-d-intr base __scm_variant)" \
	  -x "(theme-d-intr dbus __scm_utils)" \
	  -x "(theme-d-intr dbus __scm_connection)" \
	  -x "(theme-d-intr dbus __scm_node-info)" \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="@extra_guild_options@" \
	  $(EXTRA_LINK_OPTIONS) $<

dbus-test-client1.go : dbus-test-client1.tcp
	$(TDL) --module $(MODULE_OPTION2) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x "(ice-9 format)" \
	  -x "(theme-d-intr base __scm_variant)" \
	  -x "(theme-d-intr dbus __scm_utils)" \
	  -x "(theme-d-intr dbus __scm_connection)" \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="@extra_guild_options@" \
	  $(EXTRA_LINK_OPTIONS) $<

dbus-test-client1-2.go : dbus-test-client1-2.tcp org_gnu_test_DBus.tcb \
  __intf_org_gnu_test_DBus.aux __impl_org_gnu_test_DBus.aux
	$(TDL) --module $(MODULE_OPTION2) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x "(ice-9 format)" \
	  -x "(theme-d-intr base __scm_variant)" \
	  -x "(theme-d-intr dbus __scm_utils)" \
	  -x "(theme-d-intr dbus __scm_connection)" \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="@extra_guild_options@" \
	  $(EXTRA_LINK_OPTIONS) $<

else

dbus-test-server1.go : dbus-test-server1.tcp \
  _intr-imports-target.go
	$(TDL) $(MODULE_OPTION) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x "(intr-dbus-examples server-and-client-1 _intr-imports-target)" \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="@extra_guild_options@" \
	  $(EXTRA_LINK_OPTIONS) $<

dbus-test-client1.go : dbus-test-client1.tcp \
  _intr-imports-target.go
	$(TDL) $(MODULE_OPTION) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x "(ice-9 format)" \
	  -x "(intr-dbus-examples server-and-client-1 _intr-imports-target)" \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="@extra_guild_options@" \
	  $(EXTRA_LINK_OPTIONS) $<

dbus-test-client1-2.go : dbus-test-client1-2.tcp \
  org_gnu_test_DBus.tcb _intr-imports-target.go
	$(TDL) $(MODULE_OPTION) \
	  -x "(g-golf)" \
	  -x "(guile-theme-d-intr support)" \
	  -x "(ice-9 format)" \
	  -x "(intr-dbus-examples server-and-client-1 _intr-imports-target)" \
	  --duplicates="merge-generics replace warn-override-core warn last" \
	  --extra-guild-options="@extra_guild_options@" \
	  $(EXTRA_LINK_OPTIONS) $<

endif

_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

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

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

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

org_gnu_test_DBus.thi : server1.xml
	set -o pipefail ; \
		gen-dbus-theme-d-interface server1.xml "(intr-dbus-examples server-and-client-1)" | \
		pretty-print-scheme > $@

org_gnu_test_DBus.thb : server1.xml
	set -o pipefail ; \
		gen-dbus-theme-d-body server1.xml "(intr-dbus-examples server-and-client-1)" | \
		pretty-print-scheme > $@

dbus-test-client1-2.tcp : org_gnu_test_DBus.tci

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