#-----------------------------------------------------------------------------#
# Copyright (C) 1998-2004 The 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.
#-----------------------------------------------------------------------------#

# browser/Mmakefile - this is the Mmakefile for building the Mercury
# browser library, which also includes other functionality needed
# by Mercury debuggers.

# Since the code in this directory is intended to be invoked only from
# the trace library, which turns off tracing in the Mercury code it calls,
# compiling the modules in this directory with tracing on only makes
# the generated code much bigger. However, since all Mercury code
# in an executable must be of the same grade, we need to be able to
# compile the modules in this directory in debug grades as well.

# Specify which files to check for namespace cleanliness, and which name
# prefixes are allowed.

CHECK_HDRS  =
CHECK_MHDRS = $(mer_browser.mhs)
CHECK_OBJS  = $(mer_browser.os)
ALLOW_LIB_PREFIX=yes
ALLOW_BROWSER_PREFIX=yes

MERCURY_DIR=..
LINK_STDLIB_ONLY=yes
include $(MERCURY_DIR)/Mmake.common
-include Mmake.browser.params

# Module-specific options should go in Mercury.options so they
# can be found by `mmc --make'.
include Mercury.options

MAIN_TARGET=library

MERCURY_MAIN_LIB_MODULES=$(BROWSER_LIB_NAME) $(MDBCOMP_LIB_NAME)
MERCURY_MAIN_MODULES=$(MERCURY_MAIN_LIB_MODULES) browse_test declarative_test

DEPENDS =	$(patsubst %,%.depend,$(MERCURY_MAIN_MODULES))
INTS_TARGETS =	$(patsubst %,%.ints,$(MERCURY_MAIN_MODULES))
INT3S_TARGETS =	$(patsubst %,%.int3s,$(MERCURY_MAIN_MODULES))
LIBS =		$(patsubst %,lib%,$(MERCURY_MAIN_LIB_MODULES))
CHECKS =	$(patsubst %,%.check,$(MERCURY_MAIN_LIB_MODULES))

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

# 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, or create Mmake.browser.params.

CFLAGS	+=	$(DLL_CFLAGS) -I$(TRACE_DIR)
MLFLAGS +=	-R$(FINAL_INSTALL_MERC_LIB_DIR)	\
		-R$(FINAL_INSTALL_MERC_GC_LIB_DIR)
MCFLAGS +=	-R$(FINAL_INSTALL_MERC_LIB_DIR)	\
		-R$(FINAL_INSTALL_MERC_GC_LIB_DIR)
MLLIBS +=	$(SOCKET_LIBRARY) $(NSL_LIBRARY) $(DL_LIBRARY)	\
		$(READLINE_LIBRARIES)

MTAGS	=	$(SCRIPTS_DIR)/mtags

LN	=	ln

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

# Stuff for Windows DLLS using gnu-win32

ifeq ($(USE_DLLS),yes)

# XXX what do we need here for MDBCOMP_LIB_NAME?
DLL_CFLAGS = -Dlib$(BROWSER_LIB_NAME)_DEFINE_DLL

include $(MERCURY_DIR)/Makefile.DLLs

else

DLL_CFLAGS =
DLL_DEF_LIB =

endif

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

# targets

.PHONY: library
library: $(LIBS)

.PHONY: all
all: library browse_test declarative_test

.PHONY: depend
depend:	$(DEPENDS)
$(DEPENDS): Mercury.modules

# This directory contains two libraries, mer_browser and mer_mdbcomp,
# and mer_browser depends on mer_mdbcomp.  But Mmake will by default
# assume that each library contains any file referenced from the
# top-level module which occurs in the current directory, which leads
# it to incorrectly assume that the mer_browser library should include
# the mdbcomp.m and program_representation.m files.
# To avoid this problem, we temporarily rename those files away
# when building the dependencies for mer_browser.
#
# We use the phony .NOTPARALLEL target to disable parallel make,
# to ensure that we don't try to execute any other rule while
# the files are renamed.
# We also need to be careful to ensure that the files get renamed
# back properly even if making the dependencies fails or a signal occurs.
# XXX This is all a bit hacky.

.NOTPARALLEL:

$(BROWSER_LIB_NAME).dep $(BROWSER_LIB_NAME).depend:
	trap 'mv RENAMED_mdbcomp.m mdbcomp.m; \
		mv RENAMED_program_representation.m program_representation.m; \
		exit 1' 1 2 3 13 15; \
	mv mdbcomp.m RENAMED_mdbcomp.m; \
	mv program_representation.m RENAMED_program_representation.m ;\
	$(MCD) $(ALL_GRADEFLAGS) $(ALL_MCDFLAGS) $*; \
	status=$$?; \
	mv RENAMED_mdbcomp.m mdbcomp.m; \
	mv RENAMED_program_representation.m program_representation.m; \
	exit $$status

lib$(BROWSER_LIB_NAME).so: lib$(MDBCOMP_LIB_NAME).so
lib$(BROWSER_LIB_NAME): lib$(MDBCOMP_LIB_NAME)
lib$(BROWSER_LIB_NAME).int3s: lib$(MDBCOMP_LIB_NAME).int3s
lib$(BROWSER_LIB_NAME).ints: lib$(MDBCOMP_LIB_NAME).ints
lib$(BROWSER_LIB_NAME).cs: lib$(MDBCOMP_LIB_NAME).cs
lib$(BROWSER_LIB_NAME).ss: lib$(MDBCOMP_LIB_NAME).ss
lib$(BROWSER_LIB_NAME).ils: lib$(MDBCOMP_LIB_NAME).ils
lib$(BROWSER_LIB_NAME).javas: lib$(MDBCOMP_LIB_NAME).javas
lib$(BROWSER_LIB_NAME).check: lib$(MDBCOMP_LIB_NAME).check
$($(BROWSER_LIB_NAME).ints): $($(MDBCOMP_LIB_NAME).ints)
$($(BROWSER_LIB_NAME).cs): $($(MDBCOMP_LIB_NAME).cs)
$($(BROWSER_LIB_NAME).ss): $($(MDBCOMP_LIB_NAME).ss)
$($(BROWSER_LIB_NAME).ils): $($(MDBCOMP_LIB_NAME).ils)
$($(BROWSER_LIB_NAME).javas): $($(MDBCOMP_LIB_NAME).java_dates)
$($(BROWSER_LIB_NAME).c_dates): $($(MDBCOMP_LIB_NAME).c_dates)
$($(BROWSER_LIB_NAME).s_dates): $($(MDBCOMP_LIB_NAME).s_dates)
$($(BROWSER_LIB_NAME).il_dates): $($(MDBCOMP_LIB_NAME).il_dates)
$($(BROWSER_LIB_NAME).java_dates): $($(MDBCOMP_LIB_NAME).java_dates)

# This directory contains source files for which the module
# name doesn't match the file name, so smart recompilation
# won't work without the Mercury.modules file.
.PHONY: Mercury.modules
Mercury.modules:
	$(MC) $(ALL_MCFLAGS) -f *.m

.PHONY: check
check:	$(CHECKS)

.PHONY: all-ints 
all-ints: ints int3s

.PHONY: ints 
ints:	$(INTS_TARGETS)

.PHONY: int3s 
int3s:	$(INT3S_TARGETS)

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

tags:	$(MTAGS) $($(BROWSER_LIB_NAME).ms) $($(MDBCOMP_LIB_NAME).ms)
	$(MTAGS) $($(BROWSER_LIB_NAME).ms) $($(MDBCOMP_LIB_NAME).ms) \
		../library/*.m

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

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

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

.PHONY: dates
dates:
	touch $($(BROWSER_LIB_NAME).dates) $($(MDBCOMP_LIB_NAME).dates)

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

.PHONY: os cs ss ils
ifeq ($(MMAKE_USE_MMC_MAKE),no)
os: $($(BROWSER_LIB_NAME).os) $($(MDBCOMP_LIB_NAME).os)
cs: $($(BROWSER_LIB_NAME).cs) $($(MDBCOMP_LIB_NAME).cs)
ss: $($(BROWSER_LIB_NAME).ss) $($(MDBCOMP_LIB_NAME).ss)
ils: $($(BROWSER_LIB_NAME).ils) $($(MDBCOMP_LIB_NAME).ils)
else
os: $(BROWSER_LIB_NAME).os $(MDBCOMP_LIB_NAME).os
cs: $(BROWSER_LIB_NAME).cs $(MDBCOMP_LIB_NAME).cs
ss: $(BROWSER_LIB_NAME).ss $(MDBCOMP_LIB_NAME).ss
ils: $(BROWSER_LIB_NAME).ils $(MDBCOMP_LIB_NAME).ils
endif

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

# Ensure we recompile mdb__version if VERSION is changed.
$(os_subdir)mdb.o \
$(os_subdir)mdb.pic_o \
$(os_subdir)mdbcomp.o \
$(os_subdir)mdbcomp.pic_o \
        : $(RUNTIME_DIR)/mercury_conf.h

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

realclean_local:
	rm -f Mercury.modules tags

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

# Installation targets

.PHONY: install
install: install_init install_library

.PHONY: install_dirs
install_dirs:
	[ -d $(INSTALL_MODULE_DIR) ] || mkdir -p $(INSTALL_MODULE_DIR)
	[ -d $(INSTALL_MERC_LIB_DIR) ] || mkdir -p $(INSTALL_MERC_LIB_DIR)

ifneq ("$(filter il% java%,$(GRADE))","")

# there is no browser in the .NET and Java backends

.PHONY: install_init
install_init: 

.PHONY: install_library
install_library:

else 

.PHONY: install_init
install_init: $(BROWSER_LIB_NAME).init $(MDBCOMP_LIB_NAME).init install_dirs
	cp `vpath_find $(BROWSER_LIB_NAME).init` $(INSTALL_MODULE_DIR)
	cp `vpath_find $(MDBCOMP_LIB_NAME).init` $(INSTALL_MODULE_DIR)

.PHONY: install_library
install_library: \
		lib$(BROWSER_LIB_NAME).$A \
		lib$(BROWSER_LIB_NAME).$(EXT_FOR_SHARED_LIB) \
		lib$(MDBCOMP_LIB_NAME).$A \
		lib$(MDBCOMP_LIB_NAME).$(EXT_FOR_SHARED_LIB) \
		install_dirs
	cp `vpath_find lib$(BROWSER_LIB_NAME).$A \
		lib$(BROWSER_LIB_NAME).$(EXT_FOR_SHARED_LIB)` \
		$(INSTALL_MERC_LIB_DIR)
	cp `vpath_find lib$(MDBCOMP_LIB_NAME).$A \
		lib$(MDBCOMP_LIB_NAME).$(EXT_FOR_SHARED_LIB)` \
		$(INSTALL_MERC_LIB_DIR)
	$(RANLIB) $(INSTALL_MERC_LIB_DIR)/lib$(BROWSER_LIB_NAME).$A
	$(RANLIB) $(INSTALL_MERC_LIB_DIR)/lib$(MDBCOMP_LIB_NAME).$A

endif
