
# 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 := main

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

GEN_NAME := "(intr-dbus-examples introspect2 _intr-imports)"
INTR_MODULE := "(intr-dbus-examples introspect2 _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_freedesktop_DBus_Introspectable.*
	-rm _intr-imports.tci _intr-imports.tcb \
	  _intr-imports.thi _intr-imports.thb _intr-imports-target.scm \
	  _intr-imports-target.go

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

_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 %.tci
	$(TDC) $(MODULE_OPTION) $(EXTRA_COMP_OPTIONS) $<

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

org_freedesktop_DBus_Introspectable.thi : introspectable.xml
	set -o pipefail ; \
	gen-dbus-theme-d-interface $< "(intr-dbus-examples introspect2)" | \
		pretty-print-scheme > $@

org_freedesktop_DBus_Introspectable.thb : introspectable.xml
	set -o pipefail ; \
	gen-dbus-theme-d-body $< "(intr-dbus-examples introspect2)" | \
		pretty-print-scheme > $@

main.tcp : org_freedesktop_DBus_Introspectable.tci

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