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

# Mmakefile for the Mercury trace library, which contains the runtime
# system components that are needed only if some procedures are traced.

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

# keep this list in alphabetical order, please
HDRS		=	\
			mercury_trace.h			\
			mercury_trace_alias.h		\
			mercury_trace_browse.h		\
			mercury_trace_completion.h	\
			mercury_trace_declarative.h	\
			mercury_trace_external.h 	\
			mercury_trace_help.h		\
			mercury_trace_internal.h	\
			mercury_trace_readline.h	\
			mercury_trace_source.h		\
			mercury_trace_spy.h		\
			mercury_trace_tables.h		\
			mercury_trace_util.h		\
			mercury_trace_vars.h

# keep this list in alphabetical order, please
CFILES		= 	\
			mercury_trace.c			\
			mercury_trace_alias.c		\
			mercury_trace_browse.c		\
			mercury_trace_completion.c	\
			mercury_trace_declarative.c	\
			mercury_trace_external.c 	\
			mercury_trace_help.c		\
			mercury_trace_internal.c	\
			mercury_trace_readline.c	\
			mercury_trace_source.c		\
			mercury_trace_spy.c		\
			mercury_trace_tables.c		\
			mercury_trace_util.c		\
			mercury_trace_vars.c

# The object files in this directory depend on many of the header files
# in the runtime. However, changes to many of these header files require
# a global make clean. Here we list only the header files from the runtime
# whose changes don't usually require a make clean but which nevertheless
# require the trace library to be recompiled.
RUNTIME_HDRS	=	\
			$(RUNTIME_DIR)/mercury_stack_layout.h

OBJS		= $(CFILES:.c=.$O)
PIC_OBJS	= $(CFILES:.c=.$(EXT_FOR_PIC_OBJECTS))

LDFLAGS		= -L$(BROWSER_DIR) -L$(LIBRARY_DIR) \
			-L$(RUNTIME_DIR) -L$(BOEHM_GC_DIR) -L/usr/local/lib
LDLIBS		= -l$(BROWSER_LIB_NAME) -l$(MDBCOMP_LIB_NAME) \
			$(MLLIBS) $(SOCKET_LIBRARY) \
			$(NSL_LIBRARY) $(DL_LIBRARY) $(READLINE_LIBRARIES)
THREADLIBS	= \
		` case "$(GRADE)" in					\
		    *.par*) echo "-lpthread" ;;				\
		  esac							\
		`
MAIN_TARGET=trace

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

CHECK_HDRS  = $(HDRS)
CHECK_MHDRS =
CHECK_OBJS  = $(OBJS)
ALLOW_LIB_PREFIX=no
ALLOW_BROWSER_PREFIX=no

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

# Avoid trying to make this file with `mmc --make' if it doesn't exist.
Mmake.trace.params: ;

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

CFLAGS		+= -I$(BROWSER_DIR) -g $(DLL_CFLAGS) \
			-DMR_NO_BACKWARDS_COMPAT -DMERCURY_CONF_BOOTSTRAP_H
MGNUCFLAGS	+= --no-ansi

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

# mercury_readline.c #includes the GNU readline headers, which
# lack prototypes and `const', so we need to disable warnings
# when compiling that file.
MGNUCFLAGS-mercury_trace_readline = --no-check

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

# Stuff for Windows DLLs

ifeq ($(USE_DLLS),yes)

DLL_CFLAGS	= -Dlib$(TRACE_LIB_NAME)_DEFINE_DLL

# the following header files are created automatically by Makefile.DLLs
LIB_DLL_H	 = lib$(TRACE_LIB_NAME)_dll.h
LIB_GLOBALS_H = lib$(TRACE_LIB_NAME)_globals.h

include $(MERCURY_DIR)/Makefile.DLLs

else

DLL_CFLAGS =
LIB_DLL_H =
LIB_GLOBALS_H =
DLL_DEF_LIB =

endif

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

$(OBJS) $(PIC_OBJS): $(HDRS) $(RUNTIME_HDRS)

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

.PHONY: trace
ifneq ("$(filter il% java%,$(GRADE))","")        
# there is no tracing in the .NET and Java backends
trace:
else
trace: lib$(TRACE_LIB_NAME).$A lib$(TRACE_LIB_NAME).$(EXT_FOR_SHARED_LIB)
trace: $(LIB_DLL_H) $(LIB_GLOBALS_H)
endif

lib$(TRACE_LIB_NAME)$(DLL_DEF_LIB).$A: $(OBJS)
	rm -f lib$(TRACE_LIB_NAME)$(DLL_DEF_LIB).$A
	$(AR) $(ALL_ARFLAGS) \
		$(AR_LIBFILE_OPT)lib$(TRACE_LIB_NAME)$(DLL_DEF_LIB).$A $(OBJS)
	$(RANLIB) lib$(TRACE_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$(TRACE_LIB_NAME).so: $(PIC_OBJS)
	$(LINK_SHARED_OBJ) $(ERROR_UNDEFINED)				\
		-o lib$(TRACE_LIB_NAME).so $(PIC_OBJS)			\
		$(RPATH_1)$(RPATH_2)					\
		$(LDFLAGS) $(LDLIBS) $(THREADLIBS)			\
		$(SHARED_LIBS)

.PHONY: cs
cs: $(CFILES)

tags: $(CFILES) $(HDRS)
	ctags $(CFILES) $(HDRS) $(RUNTIME_DIR)/*.c $(RUNTIME_DIR)/*.h

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

# installation rules

.PHONY: install
install: install_headers install_lib

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

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

.PHONY: install_headers
install_headers:

.PHONY: install_lib
install_lib:

else

.PHONY: install_headers
install_headers: $(HDRS) $(LIB_GLOBALS_H)
	cp `vpath_find $(HDRS) $(LIB_GLOBALS_H)` $(INSTALL_INC_DIR)

.PHONY: install_lib
install_lib: lib$(TRACE_LIB_NAME).$A lib$(TRACE_LIB_NAME).$(EXT_FOR_SHARED_LIB)
	-[ -d $(INSTALL_MERC_LIB_DIR) ] || mkdir -p $(INSTALL_MERC_LIB_DIR)
	cp `vpath_find lib$(TRACE_LIB_NAME).$A \
		lib$(TRACE_LIB_NAME).$(EXT_FOR_SHARED_LIB)` \
		$(INSTALL_MERC_LIB_DIR)
	$(RANLIB) $(INSTALL_MERC_LIB_DIR)/lib$(TRACE_LIB_NAME).$A

endif

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

clean_local: clean_o clean_check

.PHONY: clean_o
clean_o:
	rm -f $(OBJS) $(PIC_OBJS)

.PHONY: realclean_local
realclean_local:
	rm -f lib$(TRACE_LIB_NAME).$A lib$(TRACE_LIB_NAME).so

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