#-----------------------------------------------------------------------------#

# Mmake - this is Mmake file for building the Mercury compiler 

MERCURY_DIR=..
include ../Mmake.common

MAIN_TARGET=mercury

VPATH=../library

#-----------------------------------------------------------------------------#

# Specify which compilers to use to compile the compiler.
# Don't change these without good reason - if you want to
# do a temporary change, change ../Mmake.params

MCD	=	MERCURY_INT_DIR=. $(MC) --generate-dependencies
MCI	=	MERCURY_INT_DIR=$(LIBRARY_DIR) $(MC) --make-interface
MCG	=	MERCURY_C_INCL_DIR=$(RUNTIME_DIR) \
		MERCURY_INT_DIR=$(LIBRARY_DIR) $(MC) --compile-to-c
MGNUC	=	MERCURY_C_INCL_DIR=$(RUNTIME_DIR) mgnuc
MGNUCFLAGS =	-I $(BOEHM_GC_DIR) $(PROFFLAGS)
C2INIT =	\
MERCURY_MOD_LIB_MODS="../runtime/runtime.init ../library/library.init" \
		MERCURY_MKINIT=../util/mkinit c2init
ML	=	MERCURY_C_LIB_DIR=. ml
MLFLAGS =	-- -static -L$(RUNTIME_DIR) -L$(LIBRARY_DIR) -L$(BOEHM_GC_DIR)
MSL	=	MERCURY_SP_LIB_DIR=../library msl
MNLFLAGS =	-u 6000

#-----------------------------------------------------------------------------#

# targets
#
# mercury_compile
# mercury_compile.nu
# mercury_compile.nu.debug
# mercury_compile.sicstus
# mercury_compile.sicstus.debug

.PHONY: depend
depend : mercury_compile.depend

# we need to make sure the .pp and .in files get converted to .m before
# we do the make depend
mercury_compile.depend: mercury_compile.m optimize.m code_gen.m conf.m

.PHONY: all
all : mercury nuprolog sicstus

.PHONY: mercury
mercury: mercury_compile

.PHONY: nuprolog
nuprolog: mercury_compile.nu

.PHONY: sicstus
sicstus: mercury_compile.sicstus

#-----------------------------------------------------------------------------#

# Add some additional dependencies, so that Mmake knows to remake the
# compiler if one of the libraries changes.

mercury_compile: $(RUNTIME_DIR)/libmer.a $(LIBRARY_DIR)/libmercury.a \
	$(BOEHM_GC_DIR)/libgc.a

#-----------------------------------------------------------------------------#

conf.m: conf.m.in # XXX ../config.status - problems with bootstrapping
	cd ..; CONFIG_FILES=compiler/conf.m CONFIG_HEADERS= ./config.status

#-----------------------------------------------------------------------------#

.PHONY: check
check		: mercury_compile.check

.PHONY: ints 
ints		: mercury_compile.ints

#-----------------------------------------------------------------------------#

tags		: $(mercury_compile.ms)
	mtags *.pp *.in $(mercury_compile.ms) ../library/*.m

mercury_compile.stats : source_stats.awk $(mercury_compile.ms)
	awk -f `vpath_find source_stats.awk` \
		`vpath_find $(mercury_compile.ms)` > $@

#-----------------------------------------------------------------------------#

.PHONY: dates
dates		:
	touch $(mercury_compile.dates)

#-----------------------------------------------------------------------------#

.PHONY: os cs
os: $(mercury_compile.os) mercury_compile_init.o
cs: $(mercury_compile.cs) mercury_compile_init.c

#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#

# Installation targets

.PHONY: install
install: install_mercury

.PHONY: install_all
install_all: install_mercury install_nuprolog install_sicstus

.PHONY: install_mercury
install_mercury: install_compiler
	
.PHONY: install_nuprolog
install_nuprolog: install_compiler_nu

.PHONY: install_sicstus
install_sicstus: install_compiler_sicstus

.PHONY: install_compiler
install_compiler: mercury_compile
	-[ -d $(INSTALL_MERC_BIN_DIR) ] || mkdir -p $(INSTALL_MERC_BIN_DIR)
	cp `vpath_find mercury_compile` $(INSTALL_MERC_BIN_DIR)

.PHONY: install_compiler_nu
install_compiler_nu: mercury_compile.nu
	-[ -d $(INSTALL_NU_ARCH_DIR) ] || mkdir -p $(INSTALL_NU_ARCH_DIR) 
	sed "s^`pwd`^$(INSTALL_NU_ARCH_DIR)^g" `vpath_find mercury_compile.nu` \
		> $(INSTALL_NU_ARCH_DIR)/mercury_compile.nu
	-chmod +x $(INSTALL_NU_ARCH_DIR)/mercury_compile.nu
	cp `vpath_find mercury_compile.nu.save` $(INSTALL_NU_ARCH_DIR)

.PHONY: install_compiler_sicstus
install_compiler_sicstus: mercury_compile.sicstus
	-[ -d $(INSTALL_SP_ARCH_DIR) ] || mkdir -p $(INSTALL_SP_ARCH_DIR) 
	cp `vpath_find mercury_compile.sicstus` $(INSTALL_SP_ARCH_DIR)

#-----------------------------------------------------------------------------#
