#-----------------------------------------------------------------------------#
# Copyright (C) 1995 University of Melbourne.
# This file may only be copied under the terms of the GNU General
# Public License - see the file COPYING in the Mercury distribution.
#-----------------------------------------------------------------------------#

# library/Mmake - this is the main part of the Makefile
# for building the Mercury libraries.

MERCURY_DIR=..
include ../Mmake.common

MAIN_TARGET=mercury

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

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

MNC	=	MERCURY_NC_BUILTIN="`vpath_find nc_builtin.nl`" mnc
MNL	=	MERCURY_LIB_DIR=. \
	MERCURY_LIB_NOS="`vpath_find $(library.nos) $(NU_LIBRARY_NOS)`" \
		mnl

MSC	=	MERCURY_SICSTUS_COMPILER=./sicstus_compile msc
MSL	=	MERCURY_SP_LIB_DIR=. MERCURY_SICSTUS_PROLOG=sp \
MERCURY_SP_LIB_OBJS="`vpath_find sp_builtin.ql $(library.qls) $(NU_LIBRARY_QLS)`" \
		msl

MCD	=	MERCURY_INT_DIR=. \
		MERCURY_C_INCL_DIR=$(RUNTIME_DIR) $(MC) --generate-dependencies
MCI	=	MERCURY_INT_DIR=. \
		MERCURY_C_INCL_DIR=$(RUNTIME_DIR) $(MC) --make-interface
MCG	=	MERCURY_INT_DIR=. \
		MERCURY_C_INCL_DIR=$(RUNTIME_DIR) $(MC) --compile-to-c
MGNUC	=	MERCURY_C_INCL_DIR=$(RUNTIME_DIR) mgnuc
MGNUCFLAGS =	-I $(BOEHM_GC_DIR) $(PROFFLAGS)
C2INIT =	MERCURY_MOD_LIB_DIR=$(RUNTIME_DIR) \
		MERCURY_MOD_LIB_MODS="$(SYS_CS)" \
		MERCURY_MKINIT=../util/mkinit c2init

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

NU_LIBRARY_NOS = \
	io.nu.no require.nu.no std_util.nu.no string.nu.no term_io.nu.no \
	int.nu.no mercury_builtin.nu.no
NU_LIBRARY_QLS = \
	io.nu.ql require.nu.ql std_util.nu.ql string.nu.ql term_io.nu.ql \
	int.nu.ql mercury_builtin.nu.ql sp_lib.ql

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

# targets

.PHONY: all
all : ints mercury nuprolog sicstus

.PHONY: mercury
mercury : ints libmercury

.PHONY: nuprolog
nuprolog : ints library.nu $(library.nos) $(NU_LIBRARY_NOS) \
			nc_builtin.nl np_builtin.nl

.PHONY: sicstus
sicstus : ints sicstus_compile library.sicstus.debug

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

.PHONY: depend
depend		: library.depend

.PHONY: check
check		: library.check

.PHONY: ints 
ints		: library.ints

.PHONY: mods
mods		: library.mods
library.mods	: $(library.mods)

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

tags		: $(library.ms)
	mtags $(library.ms)

library.stats : ../compiler/source_stats.awk $(library.ms)
	awk -f ../compiler/source_stats.awk `vpath_find $(library.ms)` > $@
	
#-----------------------------------------------------------------------------#

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

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

.PHONY: os cs
os: $(library.os)
cs: $(library.cs)

library.pic_os=$(library.os:.o=.$(PIC_O))

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

.PHONY: libmercury
libmercury : libmercury.a libmercury.$(SO) library.init

libmercury.a : $(library.os)
	ar cr libmercury.a $(library.os)
	$(RANLIB) libmercury.a

libmercury.so : $(library.pic_os)
	$(LINK_SHARED_OBJ) -o libmercury.so $(library.pic_os)

library.init: $(library.cs)
	cat `vpath_find $(library.cs)` | grep '^INIT ' > library.init

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

# we need to add some manual dependencies, since the
# automatic dependencies assume that the library
# isn't part of the application, but in this case it is.

library.nu library.nu.debug: $(NU_LIBRARY_NOS)
library.sicstus library.sicstus.debug: sp_builtin.ql $(NU_LIBRARY_QLS)

library.nu.debug: error.no debug.no portray.no
library.sicstus.debug: error.ql

sp_builtin.ql $(library.qls) $(NU_LIBRARY_QLS) error.ql: sicstus_compile

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

sicstus_compile: sp_builtin.pl
	{ echo "on_exception(Error, (				"; \
	  echo "  prolog_flag(compiling, _, fastcode),		"; \
	  echo "  compile(sp_builtin),				"; \
	  echo "  garbage_collect,				"; \
	  echo "  save('$@', 1),				"; \
	  echo "  unix(argv(Args)),				"; \
	  echo "  ( Args = [CompileMode|Files] ->		"; \
	  echo "      prolog_flag(compiling, _, CompileMode),	"; \
	  echo "      fcompile(Files)				"; \
	  echo "  ; true )					"; \
	  echo "), print_message(error, Error)), halt ; halt.	"; \
	} | $(SP)

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

CHGRP=mercury
CHMOD=go+w

# Installation targets

.PHONY: install
install: install_mercury $(INSTALL_SICSTUS) $(INSTALL_NUPROLOG)

.PHONY: install_all
install_all: install_mercury install_nuprolog install_sicstus

.PHONY: install_mercury
install_mercury: install_ints install_init install_library

.PHONY: install_ints
install_ints: $(library.ints)
	[ -d $(INSTALL_INT_DIR) ] || mkdir -p $(INSTALL_INT_DIR)
	for file in `vpath_find $(library.ints)`; do \
		target=$(INSTALL_INT_DIR)/`basename $$file`; \
		if cmp -s $$file $$target; then \
			echo "$$target unchanged"; \
		else \
			echo "installing $$target"; \
			cp $$file $$target; \
		fi; \
	done

.PHONY: install_init
install_init: library.init
	[ -d $(INSTALL_MODULE_DIR) ] || mkdir -p $(INSTALL_MODULE_DIR)
	cp `vpath_find library.init` $(INSTALL_MODULE_DIR)

.PHONY: install_library
install_library: libmercury.a libmercury.$(SO)
	[ -d $(INSTALL_MERC_LIB_DIR) ] || mkdir -p $(INSTALL_MERC_LIB_DIR)
	cp `vpath_find libmercury.a libmercury.$(SO)` $(INSTALL_MERC_LIB_DIR)

.PHONY: install_nuprolog
install_nuprolog: install_library_nu install_interpreter_nu

.PHONY: install_library_nu
install_library_nu: $(library.nos) $(NU_LIBRARY_NOS) debug.no portray.no \
			nc_builtin.nl np_builtin.nl
	[ -d $(INSTALL_NU_DIR) ] || mkdir -p $(INSTALL_NU_DIR)
	cp `vpath_find nc_builtin.nl np_builtin.nl` $(INSTALL_NU_DIR)
	[ -d $(INSTALL_NU_ARCH_DIR) ] || mkdir -p $(INSTALL_NU_ARCH_DIR)
	cp `vpath_find $(library.nos) $(NU_LIBRARY_NOS) debug.no portray.no` \
		$(INSTALL_NU_ARCH_DIR)

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

.PHONY: install_sicstus
install_sicstus: install_interpreter_sicstus install_sicstus_compile

.PHONY: install_interpreter_sicstus
install_interpreter_sicstus: library.sicstus library.sicstus.debug
	[ -d $(INSTALL_SP_ARCH_DIR) ] || mkdir -p $(INSTALL_SP_ARCH_DIR)
	cp `vpath_find library.sicstus library.sicstus.debug` \
		$(INSTALL_SP_ARCH_DIR)

.PHONY: install_sicstus_compile
install_sicstus_compile: sicstus_compile
	[ -d $(INSTALL_SP_ARCH_DIR) ] || mkdir -p $(INSTALL_SP_ARCH_DIR)
	cp `vpath_find sicstus_compile` $(INSTALL_SP_ARCH_DIR)

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