#Makefile for the C part of Gforth

#Authors: Anton Ertl, Bernd Paysan, Jens Wilke
#Copyright (C) 1995,1996,1997,1998,2000,2003,2006,2007,2008,2009,2011,2012,2013,2014,2015,2016,2019,2020,2021,2022 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

# ------------- gforth version

VERSION =@PACKAGE_VERSION@
LIBVERSION=@LIB_VERSION@
DOSVERSION=`echo $VERSION|sed 's/\.//g'`
EXE=@EXEEXT@

# ------------- System specific variables

machine=@machine@
kernel_fi = @kernel_fi@
include_fi = @include_fi@
# this is the type of machine
# used to extend the include path with ./arch/$machine
# so we could include a machine specific
# machine.h file

PATHSEP = @PATHSEP@

EC = @EC@

# ------------- Utility programs

SHELL	= /bin/sh
RM	= rm
RMTREE	= rm -rf
CP	= cp -p
TAR	= tar cf -
GCC	= @CC@
BOOTFORTH	= ../preforth
CC	= $(GCC)
LD	= $(GCC:gcc=ld)
AS	= $(GCC:gcc=as)
STRIP	= strip
DOSTRIP = true
GCCLD	= @GCC_LD@
LIBTOOL = @GNU_LIBTOOL@ --silent --tag=CC
LTLD = $(LIBTOOL) --mode=link $(GCCLD) -version-info $(LIBVERSION) -rpath $(libdir)
EXTRAPATH = $(PATHSEP)/usr/share/gforth/site-forth$(PATHSEP)/usr/local/share/gforth/site-forth

FORTHPATH = .$(PATHSEP)$(libdir)/gforth/site-forth$(PATHSEP)$(libdir)/gforth/$(VERSION)$(PATHSEP)$(datadir)/gforth/$(VERSION)$(EXTRAPATH)

# ------------  Source and Build directories

top_srcdir = @top_srcdir@
top_builddir = @top_builddir@

# ------------	Install directories

VPATH = @srcdir@:.
prefix = @prefix@
exec_prefix = @exec_prefix@
srcdir = @srcdir@
bindir = @bindir@
#read-only architecture-independent files
datadir = @datadir@
#read-only architecture-dependent non-ascii files
libdir = @libdir@
infodir = @infodir@
mandir = @mandir@
man1dir= $(mandir)/man1
man1ext= .1

# ------------- ltdl variables

LTDLDEPS = @LTDLDEPS@
LTDLINCL = @LTDLINCL@
LIBLTDL  = @LIBLTDL@

# ------------- Compiler Flags

XCFLAGS	= @CFLAGS@
XDEFINES = @DEFS@
SWITCHES = $(XCFLAGS) $(XDEFINES) $(OPTDEFINES)
#use -fno-inline to avoid register problems with asinh, atanh on gcc-3.3 on 386
ENGINE_FLAGS = @ENGINE_FLAGS@
DEBUGFLAG = @DEBUGFLAG@
CFLAGS	= $(DEBUGFLAG) -I$(srcdir)/../arch/$(machine) -I. -Wall $(SWITCHES) -DDEFAULTPATH='"$(FORTHPATH)"' $(LTDLINCL)
CPPFLAGS=@CPPFLAGS@
FORTHKFLAGS= --die-on-signal -p "..$(PATHSEP)$(srcdir)" -i ../$(kernel_fi)
FORTHK	= ../gforth $(FORTHKFLAGS)
FORTH	= $(BOOTFORTH) --die-on-signal

#John Wavrik should use -Xlinker -N to get a writable text (executable)
XLDFLAGS = @LDFLAGS@
LDFLAGS =  $(DEBUGFLAG) $(XLDFLAGS)
LDLIBS  = @LIBS@ @LTDL_LDLIBS@

AOBJECTS = @signals_o@ support$(OPT).o @LIBOBJS@ $(OPTOBJECTS)

@MAKEINC@

OBJECTS = engine$(OPT).o @engine2@ main$(OPT).o
OBJECTS_FAST = engine-fast$(OPT).o @engine_fast2@ main-fast$(OPT).o
OBJECTS_ITC = engine-itc$(OPT).o main-itc$(OPT).o
OBJECTS_DITC = engine-ditc$(OPT).o main-ditc$(OPT).o
OBJECTS_PROF = engine-prof$(OPT).o main-prof$(OPT).o
OBJECTS_FI =  engine$(OPT).o main-fi$(OPT).o

# In engine subdirectory there are (or should be) only files that belong to
# our engine, so we can make life easy
DEPS =  config.h forth.h io.h symver.h $(srcdir)/../arch/$(machine)/*.[h]
ENGINE_DEPS = engine.c $(DEPS) prim_lab.i prim.i @image_i@ fnmatch.h threaded.h
MAIN_DEPS = main.c $(DEPS) prim_superend.i prim_num.i prim_grp.i costs.i super2.i
ENGINE_FAST_DEPS = engine.c $(DEPS) prim_lab-fast.i prim-fast.i fnmatch.h threaded.h
MAIN_FAST_DEPS = main.c $(DEPS) prim_superend-fast.i prim_num-fast.i prim_grp-fast.i costs-fast.i super2-fast.i

COMPILE.c = $(GCC) $(CFLAGS) $(CPPFLAGS) -c # simulate BSD sys.mk
LIBTOOL.c = $(LIBTOOL) --mode=compile $(COMPILE.c)
COMPILER = $(COMPILE.c)

lib%.lo:	COMPILER = $(LIBTOOL.c)

#some makes don't do the -o $@ correctly, so we help them
.c.o:
	@echo CC -o $@ $<
	$(COMPILER) -o $@ $<

lib%.lo:	%.c
	@echo CC -o $@ $<
	$(COMPILER) -o $@ $<

#Since make -j compiles several dblsub in parallel,
#different .o files are generated
.s.o:
	@echo CC -o $@ $<
	$(COMPILER) -o $@ $<

support-noll.o libsupport-noll.lo: support.c config.h forth.h longlong.h
	@echo CC -o $@ $<
	$(COMPILER) -o $@ $<

support-ll.o libsupport-ll.lo: support.c config.h forth.h longlong.h
	@echo CC -DFORCE_LL -o $@ $<
	$(COMPILER) -DFORCE_LL -o $@ $<

support-noll-reg.o libsupport-noll-reg.lo: support.c config.h forth.h longlong.h
	@echo CC -DFORCE_REG -o $@ $<
	$(COMPILER) -DFORCE_REG -o $@ $<

support-ll-reg.o libsupport-ll-reg.lo: support.c config.h forth.h longlong.h
	@echo CC -DFORCE_LL -DFORCE_REG -o $@ $<
	$(COMPILER) -DFORCE_LL -DFORCE_REG -o $@ $<

strtol.o libstrtol.lo: strtol.c ansidecl.h

fnmatch.o libfnmatch.lo: fnmatch.c fnmatch.h

getopt1.o libgetopt1.lo: getopt1.c getopt.h

getopt.o libgetopt.lo: getopt.c getopt.h

androidmain.o libandroidmain.lo: androidmain.c config.h forth.h

GFORTHDEPS = $(AOBJECTS) $(LTDLDEPS) libmain.o
GFORTHOBJS = $(AOBJECTS) $(LDLIBS) libmain.o

LINKER=$(GCCLD) $(LDFLAGS) 
%.la:	LINKER=$(LTLD) $(LDFLAGS) 

gforth$(OPT)$(EC)$(EXE):	$(OBJECTS) $(GFORTHDEPS)
	@echo LD -o $@
	$(LINKER) $(OBJECTS) $(GFORTHOBJS) -o $@
	$(DOSTRIP) $@

gforth-fast$(OPT)$(EC)$(EXE):	$(OBJECTS_FAST) $(GFORTHDEPS)
	@echo LD -o $@
	$(LINKER) $(OBJECTS_FAST) $(GFORTHOBJS) -o $@
	$(DOSTRIP) $@

gforth-itc$(OPT)$(EC)$(EXE):	$(OBJECTS_ITC) $(GFORTHDEPS)
	@echo LD -o $@
	$(LINKER) $(OBJECTS_ITC) $(GFORTHOBJS) -o $@
	$(DOSTRIP) $@

gforth-ditc$(OPT)$(EC)$(EXE): $(OBJECTS_DITC) $(GFORTHDEPS)
	@echo LD -o $@
	$(LINKER) $(OBJECTS_DITC) $(GFORTHOBJS) -o $@
	$(DOSTRIP) $@

gforth-prof$(OPT)$(EC)$(EXE): $(OBJECTS_PROF) profile$(OPT).o $(GFORTHDEPS)
	@echo LD -o $@
	$(LINKER) $(OBJECTS_PROF) $(GFORTHOBJS) profile$(OPT).o $(LDLIBS) -o $@
	$(DOSTRIP) $@

gforth-fi$(OPT)$(EC)$(EXE):	$(OBJECTS_FI) $(GFORTHDEPS)
	@echo LD -o $@
	$(LINKER) $(OBJECTS_FI) $(GFORTHOBJS) -o $@
	$(DOSTRIP) $@

LIBGFORTHDEPS = $(AOBJECTS:%.o=lib%.lo) $(LTDLDEPS)
LIBGFORTHOBJS = $(AOBJECTS:%.o=lib%.lo) $(LDLIBS)

libgforth$(EC).la libgforth$(OPT)$(EC).la:	$(OBJECTS:%.o=lib%.lo) $(LIBGFORTHDEPS)
	@echo LD -o $@
	$(LINKER) $(OBJECTS:%.o=lib%.lo) $(LIBGFORTHOBJS) -o $@

libgforth-fast$(EC).la libgforth-fast$(OPT)$(EC).la:	$(OBJECTS_FAST:%.o=lib%.lo) $(LIBGFORTHDEPS)
	@echo LD -o $@
	$(LINKER) $(OBJECTS_FAST:%.o=lib%.lo) $(LIBGFORTHOBJS) -o $@

libgforth-itc$(EC).la libgforth-itc$(OPT)$(EC).la:	$(OBJECTS_ITC:%.o=lib%.lo) $(LIBGFORTHDEPS)
	@echo LD -o $@
	$(LINKER) $(OBJECTS_ITC:%.o=lib%.lo) $(LIBGFORTHOBJS) -o $@

libgforth-ditc$(EC).la libgforth-ditc$(OPT)$(EC).la: $(OBJECTS_DITC:%.o=lib%.lo) $(LIBGFORTHDEPS)
	@echo LD -o $@
	$(LINKER) $(OBJECTS_DITC:%.o=lib%.lo) $(LIBGFORTHOBJS) -o $@

libgforth-prof$(EC).la libgforth-prof$(OPT)$(EC).la: $(OBJECTS_PROF:%.o=lib%.lo) profile$(OPT).o $(LIBGFORTHDEPS)
	@echo LD -o $@
	$(LINKER) $(OBJECTS_PROF:%.o=lib%.lo) $(LIBGFORTHOBJS) profile$(OPT).lo $(LDLIBS:%o=lib%.lo) -o $@

libgforth-fi$(EC).la libgforth-fi$(OPT)$(EC).la:	$(OBJECTS_FI:%.o=lib%.lo) $(LIBGFORTHDEPS)
	@echo LD -o $@ $<
	$(LINKER) $(OBJECTS_FI:%.o=lib%.lo) $(LIBGFORTHOBJS) -o $@

# executables generated by linking to library

libgforth%$(EXE):	libgforth%.la liblibmain.lo
	@echo LD -o $@
	$(LTLD) $(LDFLAGS) $(patsubst lib%$(EXE),-l%,$@) liblibmain.lo -o $@

libgforth$(EXE):	libgforth.la liblibmain.lo
	@echo LD -o $@
	$(LTLD) $(LDFLAGS) $(patsubst lib%$(EXE),-l%,$@) liblibmain.lo -o $@

engine.s:	$(ENGINE_FAST_DEPS)
		@echo CC-ENGINE-S -o $@ $<
		$(COMPILER) -DFORCE_LL -DFORCE_REG -DGFORTH_DEBUGGING -DASMNAME $(ENGINE_FLAGS) -S $(srcdir)/engine.c

engine$(OPT).o libengine$(OPT).lo:	$(ENGINE_DEPS)
		@echo CC-ENGINE -o $@ $<
		$(COMPILER) $(ENGINE_FLAGS) -DGFORTH_DEBUGGING -o $@ $(srcdir)/engine.c

engine2$(OPT).o libengine2$(OPT).lo:	$(ENGINE_DEPS)
		@echo CC-ENGINE2 -o $@ $<
		$(COMPILER) $(ENGINE_FLAGS) -DGFORTH_DEBUGGING -DENGINE=2 -o $@ $(srcdir)/engine.c

engine-fast$(OPT).o libengine-fast$(OPT).lo:	$(ENGINE_FAST_DEPS)
		@echo CC-ENGINE-FAST -o $@ $<
		$(COMPILER) $(ENGINE_FLAGS) -o $@ $(srcdir)/engine.c

engine-fast2$(OPT).o libengine-fast2$(OPT).lo:	$(ENGINE_FAST_DEPS)
		@echo CC-ENGINE-FAST2 -o $@ $<
		$(COMPILER) $(ENGINE_FLAGS) -DENGINE=2 -o $@ $(srcdir)/engine.c

engine-itc$(OPT).o libengine-itc$(OPT).lo:	$(ENGINE_DEPS)
		@echo CC-ENGINE-ITC -o $@ $<
		$(COMPILER) $(ENGINE_FLAGS) -DINDIRECT_THREADED -DGFORTH_DEBUGGING -o $@ $(srcdir)/engine.c

engine-ditc$(OPT).o libengine-ditc$(OPT).lo:	$(ENGINE_DEPS)
		@echo CC-ENGINE-DITC -o $@ $<
		$(COMPILER) $(ENGINE_FLAGS) -DDOUBLY_INDIRECT -DGFORTH_DEBUGGING -o $@ $(srcdir)/engine.c

engine-prof$(OPT).o libengine-prof$(OPT).lo:	$(ENGINE_DEPS)
		@echo CC-ENGINE-PROF -o $@ $<
		$(COMPILER) $(ENGINE_FLAGS) -DVM_PROFILING -o $@ $(srcdir)/engine.c

main$(OPT).o libmain$(OPT).lo:		$(MAIN_DEPS) 
		@echo CC-MAIN -o $@ $<
		$(COMPILER) -DGFORTH_DEBUGGING @no_dynamic@ -o $@ $(srcdir)/main.c

main-fast$(OPT).o libmain-fast$(OPT).lo:	$(MAIN_FAST_DEPS) 
		@echo CC-MAIN -o $@ $<
		$(COMPILER) -o $@ $(srcdir)/main.c

main-itc$(OPT).o libmain-itc$(OPT).lo:	$(MAIN_DEPS) 
		@echo CC-MAIN -o $@ $<
		$(COMPILER) -DINDIRECT_THREADED -DGFORTH_DEBUGGING -o $@ $(srcdir)/main.c

main-ditc$(OPT).o libmain-ditc$(OPT).lo:	$(MAIN_DEPS)
		@echo CC-MAIN -o $@ $<
		$(COMPILER) -DDOUBLY_INDIRECT -DGFORTH_DEBUGGING -o $@ $(srcdir)/main.c

main-prof$(OPT).o libmain-prof$(OPT).lo:	$(MAIN_DEPS)
		@echo CC-MAIN -o $@ $<
		$(COMPILER) -DVM_PROFILING -o $@ $(srcdir)/main.c

main-fi$(OPT).o libmain-fi$(OPT).lo:	$(MAIN_DEPS)
		@echo CC-MAIN -o $@ $<
		$(COMPILER) -DINCLUDE_IMAGE -o $@ $(srcdir)/main.c

peephole$(OPT).o libpeephole$(OPT).lo:	peephole.c peephole.i $(DEPS)
		@echo CC-MAIN -o $@ $<
		$(COMPILER) -o $@ $(srcdir)/peephole.c

profile$(OPT).o libprofile$(OPT).lo:	profile.c profile.i $(DEPS)
		@echo CC-MAIN -o $@ $<
		$(COMPILER) -o $@ $(srcdir)/profile.c

#The next two rules seem to be superfluous:

# engine:		$(OBJECTS) $(OBJECTS0) ;

# engine_ditc:	$(OBJECTS_DITC) $(OBJECTS0) ;

#NeXTstep hack, can't be done that way...

#termios.o:	/usr/lib/libposix.a
#	ar x /usr/lib/libposix.a termios.o

image.i:	../fi2c.fs ../$(include_fi)
		$(BOOTFORTH) ../fi2c.fs -e "s\" ../$(include_fi)\" fi2c bye" >$@

../$(include_fi):	FORCE
		cd .. && $(MAKE) $(include_fi)

#phony targets depend on FORCE; this is more portable than .PHONY
FORCE:

