# Generic makefile, for crosscompiling use make -f Makefile.<target>

#Authors: Bernd Paysan, Anton Ertl
#Copyright (C) 2014,2015,2016,2017,2018,2019,2020,2021,2022,2023 Free Software Foundation, Inc.

#This file is part of Gforth.

#Gforth is free software; you can redistribute it and/or
#modify it 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 program 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 http://www.gnu.org/licenses/.

ifndef VERBOSE
.SILENT:
endif

INCLUDES   = @SWIGINCLUDES@ -I@srcdir@/../freetype-gl -I../wayland
SWIG       = @SWIG@
TARGETS    = @SWIGMODULES@
WRAPPERS   = $(patsubst %.fs, %.fsx, $(TARGETS))
GENWRAPPERS   = $(patsubst %.fs, %-fsi.c, $(TARGETS))
GENWRAPPERS_PLUS   = $(patsubst %.fs, %-fsi.c+, $(TARGETS))
LIBRARY    = @SWIGLIBS@
HOST_OS    = $(subst -,_,@host_os@)
OPTIONS    = -forth -cpperraswarn -Dhost_os_$(HOST_OS) -no-sectioncomments -use-structs -stackcomments $(subst -F,-I, $(INCLUDES))
INSTALL	= /usr/bin/install -c
ARCH=
VERSION	= "`gforth --version 2>&1 | cut -f2 -d' '`"
SHELL	= /bin/sh
RMTREE	= rm -rf
prefix = 
exec_prefix = ${prefix}
libexecdir = $(package)${exec_prefix}/lib
libccdir = $(subst $(DESTDIR),,$(libexecdir)/gforth$(ARCH)/$(VERSION)/libcc-named/)
VPATH = @srcdir@:.
srcdir = @srcdir@
top_srcdir = @top_srcdir@
top_builddir = @top_builddir@
DESTDIR = $(PWD)
LIBTOOL = @GNU_LIBTOOL@ --silent
GCC	= @CC@
HOSTCC	= @HOSTCC@
CC	= $(HOSTCC) $(INCLUDES) -I.
HELPERS = stat.fs pthread-types.fs

all: $(GENWRAPPERS) $(TARGETS) $(HELPERS)

wrappers: $(WRAPPERS)

build-libcc-named: $(LIBRARY) $(TARGETS)
		$(RMTREE) lib/gforth$(ARCH)/$(VERSION)/libcc-named/
		-for i in $(LIBRARY); do echo $$i:; (cd ..; ./libforth -e "also c-lib s\" `pwd`/lib/gforth$(ARCH)/$(VERSION)/libcc-named/\" >libcc-named-dir libcc-path clear-path libcc-named-dir libcc-path also-path :noname 2drop s\" $(DESTDIR)$(libccdir)\" ; is replace-rpath previous" $(srcdir)/$$i -e bye); done

libs: build-libcc-named $(LIBRARY)
	for i in $(LIBRARY); do \
	    $(LIBTOOL) --mode=install $(INSTALL) lib/gforth$(ARCH)/$(VERSION)/libcc-named/libgf`basename $$i .fs`.la $(DESTDIR)$(libccdir)libgf`basename $$i .fs`.la; \
	done

# execute compiled file
%.fs: %-fsi.c
	@echo SWIG-GEN $@
	$(srcdir)/trygcc "$(CC)" $@x $<
	./$@x -gforth | $(srcdir)/doexec $@ $(srcdir) > $@

stat.fs: stat-fsi.c
	$(CC) -o $@x $<
	./$@x -gforth > $@

pthread-types.fs: pthread-types-fsi.c
	$(CC) -o $@x $<
	./$@x -gforth > $@

# compile fsi-file
#%.fsx: %-fsi.c
#	$(CC) -o $@ $<

# use swig to create the fsi file
# if swig -forth is available, do this even when there's a fsi file older
# than the configure run
%-fsi.c: $(srcdir)/%.i @HAS_SWIG_FORTH@
	@echo SWIG $(patsubst %-fsi.c,%.i,$@)
	$(SWIG) $(OPTIONS) -o $@+ $(srcdir)/$(patsubst %-fsi.c,%.i,$@)
	$(srcdir)/doprep $@+ $(srcdir) > $@

run: $(DEMO)
	gforth $(DEMO)

.PHONY: clean

clean:
	rm -f $(TARGETS) $(HELPERS)
	rm -f $(GENWRAPPERS) $(GENWRAPPERS_PLUS)
	rm -f $(WRAPPERS) $(patsub %,%x,$(HELPERS))
	rm -f example.o example example-fast example.fs

# Example for C program that wraps Gforth

example: example.o example.fs
	$(CC) -o $@ -rdynamic -lgforth $(CFLAGS) $(LDFLAGS) example.o

example-fast: example.o example.fs
	$(CC) -o $@ -rdynamic -lgforth-fast $(CFLAGS) $(LDFLAGS) example.o
