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

MERCURY_DIR=..
include $(MERCURY_DIR)/Mmake.common

MAIN_TARGET=library

VPATH=$(LIBRARY_DIR)

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

# If we're going to generate both `.o' files and `.pic_o' files, then
# don't remove the intermediate `.c' files.
RM_C	=	$(LIBRARY_RM_C)

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

# 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.

M_ENV	=	MERCURY_INT_DIR=$(LIBRARY_DIR) \
		MERCURY_ALL_C_INCL_DIRS="\
			-I$(TRACE_DIR) \
			-I$(LIBRARY_DIR) \
			-I$(RUNTIME_DIR) \
			-I$(BOEHM_GC_DIR) \
			-I$(BOEHM_GC_DIR)/include \
		"

MCD	=	$(M_ENV) $(MC) --generate-dependencies
MCI	=	$(M_ENV) $(MC) --make-interface
MCPI	=	$(M_ENV) $(MC) --make-private-interface
MCSI	=	$(M_ENV) $(MC) --make-short-interface
MCOI	=	$(M_ENV) $(MC) --make-opt-int
MCTOI	=	$(M_ENV) $(MC) --make-trans-opt
MCG	=	$(M_ENV) $(MC) --compile-to-c
MCS	=	$(M_ENV) $(MC) --split-c-files -c --cflags "$(ALL_CFLAGS)"
MGNUC	=	$(M_ENV) $(SCRIPTS_DIR)/mgnuc
MGNUCFLAGS =	$(DLL_CFLAGS)
LDFLAGS	=	-L$(LIBRARY_DIR) -L$(RUNTIME_DIR) -L$(BOEHM_GC_DIR)
LDLIBS	=	-l$(STD_LIB_NAME) -l$(RT_LIB_NAME) \
		` case "$(GRADE)" in 					\
		    *.par*.gc*.prof*)	echo "-lpar_gc_prof" ;;		\
		    *.par*.gc*)		echo "-lpar_gc" ;;		\
		    *.gc*.prof*)	echo "-lgc_prof" ;;		\
		    *.gc*)		echo "-lgc" ;;			\
		  esac							\
		`

MTAGS	=	$(SCRIPTS_DIR)/mtags

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

# Stuff for Windows DLLS using gnu-win32

ifeq ($(USE_DLLS),yes)

DLL_CFLAGS = -Dlib$(BROWSER_LIB_NAME)_DEFINE_DLL

include $(MERCURY_DIR)/Makefile.DLLs

else

DLL_CFLAGS =
DLL_DEF_LIB =

endif

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

# targets

.PHONY: all
all : library browse_test declarative_test

.PHONY: depend
depend		: mdb.depend browse_test.depend declarative_test.depend

.PHONY: check
check		: mdb.check

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

.PHONY: ints 
ints		: mdb.ints browse_test.ints declarative_test.ints

.PHONY: int3s 
int3s		: mdb.int3s

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

tags		: $(MTAGS) $(mdb.ms)
	$(MTAGS) $(mdb.ms) ../library/*.m

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

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

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

.PHONY: os cs ss
os: $(mdb.os)
cs: $(mdb.cs)
ss: $(mdb.ss)

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

.PHONY: library
library: lib$(BROWSER_LIB_NAME).$A lib$(BROWSER_LIB_NAME).$(EXT_FOR_SHARED_LIB)
library: $(BROWSER_LIB_NAME).init

lib$(BROWSER_LIB_NAME)$(DLL_DEF_LIB).$A : $(mdb.os)
	rm -f lib$(BROWSER_LIB_NAME)$(DLL_DEF_LIB).$A
	$(AR) $(ALL_ARFLAGS) \
		$(AR_LIBFILE_OPT)lib$(BROWSER_LIB_NAME)$(DLL_DEF_LIB).$A \
		$(mdb.os)
	$(RANLIB) lib$(BROWSER_LIB_NAME)$(DLL_DEF_LIB).$A

RPATH_1=$(SHLIB_RPATH_OPT)$(FINAL_INSTALL_MERC_LIB_DIR)
RPATH_2=$(SHLIB_RPATH_SEP)$(FINAL_INSTALL_MERC_GC_LIB_DIR)

lib$(BROWSER_LIB_NAME).so : $(mdb.pic_os)
	$(LINK_SHARED_OBJ) $(ERROR_UNDEFINED)				\
		-o lib$(BROWSER_LIB_NAME).so				\
		$(mdb.pic_os)						\
		$(RPATH_1)$(RPATH_2)					\
		$(LDFLAGS) $(LDLIBS)					\
		$(SOCKET_LIBRARY) $(NSL_LIBRARY) $(DL_LIBRARY)		\
		$(READLINE_LIBRARIES)					\
		$(SHARED_LIBS)

$(BROWSER_LIB_NAME).init: $(deps_subdir)mdb.dep mdb.init
	cp mdb.init $(BROWSER_LIB_NAME).init

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

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

realclean_local:
	rm -f lib$(BROWSER_LIB_NAME).$A lib$(BROWSER_LIB_NAME).so \
		$(BROWSER_LIB_NAME).init mdb.init
	rm -f 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)

ifeq ($(findstring il,$(GRADE)),il)                                     

# there is no browser in the .NET backend

.PHONY: install_init
install_init: 

.PHONY: install_library
install_library:

else 

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


.PHONY: install_library
install_library: lib$(BROWSER_LIB_NAME).$A \
		lib$(BROWSER_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)

endif


