@SET_MAKE@
BUILDTOP	= @BUILDTOP@
SRCTOP		= @SRCTOP@
SRCDIR		= @srcdir@
VPATH		= @srcdir@
PREFIX		= @prefix@
PLATFORM	= @PLATFORM@
LIB_DIR		= $(PREFIX)/cache/x-oz/contrib
HOMEURL         = @HOMEURL@

OZC		= @OZC@
OZL		= @OZL@
COMPILE		= $(OZC) -c
OZTOOL		= @OZTOOL@

INCS            = -I$(SRCTOP)/platform/emulator \
		  -I$(BUILDTOP)/platform/emulator \
		  -I$(PREFIX)/include \
		  -I. \
		  -I/usr/local/oz/include

CPPFLAGS	= @CPPFLAGS@ $(INCS)
CXXFLAGS	= @CXXFLAGS@
LDFLAGS		= @LDFLAGS@
LIBS		= @LIBS@

INSTALL		= @INSTALL@
INSTALL_DIR	= @INSTALL_DIR@
INSTALL_LIB	= $(INSTALL) -m 444
INSTALL_DLL	= $(INSTALL) -m 555

LIBS     	= -lc -lm

CFLAGS   	= -O3 
CFLAGS   	+= $(INCS)

LDFLAGS  	= -L$(VPATH)

CXX     	= $(OZTOOL) c++
CC     		= $(OZTOOL) cc
LD 		= $(OZTOOL) ld
DEPEND   	= $(CXX) $(CFLAGS) -MM
PERL		= @PERL@

OZDEPEND 	= $(OZC) -c -M

FCPDECL         = $(SRCDIR)/../../platform/emulator/bidecl.perl

#----------------------------------------------------------------------
# Sources and targets
#----------------------------------------------------------------------

CPPSRCS  	= \
		misc.cc \
		ri.cc \
		builtins.cc \
		propagators.cc

MODULE_SPEC     = \
		modRI.spec

ifeq ($(PLATFORM), linux-i486)
CSRCS    	= fpgetset.c sigfpe.c
CFLAGS   	+= -DLINUX_IEEE
endif

OZSRCS  	= RI.oz

OZGSRCS  	= 

TARGETS		= RI.ozf ri.so-$(PLATFORM)
LIB_TARGETS	= $(TARGETS:%=$(LIB_DIR)/%)

GENERATED_FILES = $(MODULE_SPEC:.spec=-table.cc)

.PHONY: all install clean veryclean distclean bootstrap depend

all:		first $(TARGETS)
install:	$(LIB_DIR) $(LIB_TARGETS)

$(LIB_DIR):
	$(INSTALL_DIR) $@

$(LIB_DIR)/%.ozf: %.ozf
	$(INSTALL_LIB) $< $@

$(LIB_DIR)/%.so-$(PLATFORM): %.so-$(PLATFORM)
	$(INSTALL_DLL) $< $@


first: $(GENERATED_FILES)

#----------------------------------------------------------------------
# Oz part
#----------------------------------------------------------------------

%.ozf: %.oz 
	$(OZC) -c $< -o $@ 

%.ozf: %.ozg 
	$(OZC) -c --gump $< -o $@ 

#----------------------------------------------------------------------
# C part
#----------------------------------------------------------------------

%.so-$(PLATFORM): $(CPPSRCS:.cc=.o) $(CSRCS:.c=.o)
	$(LD) -o $@ $^ $(LDFLAGS) $(LIBS)

%.o: %.cc 
	$(CXX) $(OPTIMIZE) $(CFLAGS) -c $< -o $@ 

%.o: %.c 
	$(CC) $(OPTIMIZE) $(CFLAGS) -c $< -o $@ 

%-table.cc: %.spec
	$(PERL) $(FCPDECL) -file $< -interface > $@ || { rm -f $@; exit 1; }

#----------------------------------------------------------------------
# Maintenance
#----------------------------------------------------------------------

depend: Makefile $(CPPSRCS) $(OZSRCS) $(OZGSRCS) first
	$(RM) Makefile.deps
	$(DEPEND) $(CPPSRCS:%=$(SRCDIR)/%) >> Makefile.deps
	$(DEPEND) $(CSRCS:%=$(SRCDIR)/%) >> Makefile.deps
	$(OZDEPEND) $(OZSRCS:%=$(SRCDIR)/%) $(OZGSRCS:%=$(SRCDIR)/%) \
	>> Makefile.deps

clean: 
	-$(RM) *.ozf
	-$(RM) *.o
	-$(RM) *.so-$(PLATFORM)
	-$(RM) *.so
	-$(RM) *.l
	-$(RM) *.C
	-$(RM) *~
	-$(RM) $(GENERATED_FILES)

distclean: clean
	-$(RM) config.* Makefile*

include $(BUILDTOP)/share/Makefile.boot

bootstrap: boot-all

#----------------------------------------------------------------------
# Automatic Makefile update
#----------------------------------------------------------------------

Makefile: Makefile.in config.status
	./config.status

config.status: configure
	./config.status --recheck

#----------------------------------------------------------------------
# Dependencies
#----------------------------------------------------------------------
-include Makefile.deps
