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

# This is the Mmakefile for the Mercury runtime library

MAIN_TARGET=runtime

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

# All the headers in $(HDRS) should be syntactically well-formed
# header files, unlike the headers in $(BODY_HDRS).
# All the headers in $(HDRS) must also be in C (not C++).

# Note that $(LIB_GLOBALS_H) cannot be part of $(HDRS), since it depends on
# lib$(RT_LIB_NAME)$(DLL_DEF_LIB).$A, and $(OBJ) : $(HDRS) would create a
# circular dependency.  For similar but different reasons,
# $(LIB_DLL_H) also cannot be part of $(HDRS).

#		  keep this list in alphabetical order, please
HDRS		=	\
			mercury.h		\
			mercury_accurate_gc.h	\
			mercury_agc_debug.h	\
			mercury_array_macros.h	\
			mercury_bootstrap.h	\
			mercury_builtin_types.h	\
			mercury_calls.h		\
			mercury_conf.h		\
			mercury_conf_bootstrap.h \
			mercury_conf_param.h	\
			mercury_construct.h	\
			mercury_context.h	\
			mercury_debug.h		\
			mercury_deconstruct.h	\
			mercury_deconstruct_macros.h \
			mercury_deep_copy.h	\
			mercury_deep_profiling.h \
			mercury_deep_profiling_hand.h \
			mercury_dlist.h		\
			mercury_dummy.h		\
			mercury_dword.h		\
			mercury_engine.h	\
			mercury_file.h		\
			mercury_float.h		\
			mercury_getopt.h	\
			mercury_goto.h		\
			mercury_grade.h		\
			mercury_hash_table.h	\
			mercury_heap.h		\
			mercury_heap_profile.h	\
			mercury_hlc_types.h	\
			mercury_ho_call.h	\
			mercury_imp.h		\
			mercury_init.h		\
			mercury_label.h		\
			mercury_layout_util.h	\
			mercury_library_types.h	\
			mercury_memory.h	\
			mercury_memory_handlers.h	\
			mercury_memory_zones.h	\
			mercury_minimal_model.h	\
			mercury_misc.h		\
			mercury_overflow.h	\
			mercury_proc_id.h	\
			mercury_prof.h		\
			mercury_prof_mem.h	\
			mercury_prof_time.h	\
			mercury_profiling_builtin.h	\
			mercury_reg_workarounds.h	\
			mercury_regorder.h	\
			mercury_regs.h		\
			mercury_runtime_util.h	\
			mercury_signal.h	\
			mercury_stack_layout.h	\
			mercury_stack_trace.h	\
			mercury_stacks.h	\
			mercury_std.h		\
			mercury_string.h	\
			mercury_tabling.h	\
			mercury_tabling_macros.h	\
			mercury_tags.h		\
			mercury_term_size.h	\
			mercury_thread.h	\
			mercury_timing.h	\
			mercury_trace_base.h	\
			mercury_trail.h		\
			mercury_type_desc.h	\
			mercury_type_info.h	\
			mercury_type_tables.h	\
			mercury_typeclass_info.h	\
			mercury_types.h		\
			mercury_univ.h		\
			mercury_wrapper.h

# The headers in $(BODY_HDRS) contain code schemes included multiple times
# in one or more source files. Their dependencies must be explicitly listed.
# They do not have to be syntactically well-formed.

BODY_HDRS	=	\
			mercury_deep_call_port_body.h	\
			mercury_deep_copy_body.h	\
			mercury_deep_leave_port_body.h	\
			mercury_deep_rec_depth_actions.h \
			mercury_deep_rec_depth_body.h	\
			mercury_deep_redo_port_body.h	\
			mercury_exception_catch_body.h	\
			mercury_hand_compare_body.h	\
			mercury_hand_unify_body.h	\
			mercury_make_type_info_body.h	\
			mercury_ml_arg_body.h		\
			mercury_ml_deconstruct_body.h	\
			mercury_ml_expand_body.h	\
			mercury_ml_functor_body.h	\
			mercury_unify_compare_body.h

MACHHDRS	= 	machdeps/no_regs.h	\
			machdeps/i386_regs.h	\
			machdeps/mips_regs.h	\
			machdeps/sparc_regs.h	\
			machdeps/alpha_regs.h	\
			machdeps/pa_regs.h	\
			machdeps/rs6000_regs.h

# MCPP_HDRS are header files in Microsoft's Managed extensions for C++

MCPP_HDRS	=	mercury_mcpp.h		

#		  keep this list in alphabetical order, please
CFILES		= 	\
			mercury.c		\
			mercury_accurate_gc.c	\
			mercury_agc_debug.c	\
			mercury_bootstrap.c	\
			mercury_builtin_types.c	\
			mercury_construct.c	\
			mercury_context.c	\
			mercury_debug.c		\
			mercury_deconstruct.c	\
			mercury_deep_copy.c	\
			mercury_deep_profiling.c \
			mercury_dlist.c		\
			mercury_dummy.c		\
			mercury_engine.c	\
			mercury_file.c		\
			mercury_float.c		\
			mercury_getopt.c	\
			mercury_getopt1.c	\
			mercury_grade.c		\
			mercury_hash_table.c	\
			mercury_heap_profile.c	\
			mercury_ho_call.c	\
			mercury_label.c		\
			mercury_layout_util.c	\
			mercury_memory.c	\
			mercury_memory_handlers.c	\
			mercury_memory_zones.c	\
			mercury_minimal_model.c	\
			mercury_misc.c		\
			mercury_prof.c		\
			mercury_prof_mem.c	\
			mercury_prof_time.c	\
			mercury_profiling_builtin.c	\
			mercury_reg_workarounds.c	\
			mercury_regs.c		\
			mercury_runtime_util.c	\
			mercury_signal.c	\
			mercury_stack_trace.c	\
			mercury_stacks.c	\
			mercury_string.c	\
			mercury_tabling.c	\
			mercury_term_size.c	\
			mercury_thread.c	\
			mercury_timing.c	\
			mercury_trace_base.c	\
			mercury_trail.c 	\
			mercury_type_desc.c	\
			mercury_type_info.c	\
			mercury_type_tables.c	\
			mercury_wrapper.c

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

CHECK_HDRS  = $(HDRS)
CHECK_MHDRS =
CHECK_OBJS  = $(CFILES:.c=.$O)
ALLOW_LIB_PREFIX=no
ALLOW_BROWSER_PREFIX=no

MERCURY_DIR=..
LINK_BOEHM_GC_ONLY=yes
include $(MERCURY_DIR)/Mmake.common
-include Mmake.runtime.params

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

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

CFLAGS	+= $(DLL_CFLAGS) -DMERCURY_BOOTSTRAP_H -DMERCURY_CONF_BOOTSTRAP_H
MGNUCFLAGS	+= --c-debug --no-ansi

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

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

LDFLAGS		= -L$(BOEHM_GC_DIR) -L/usr/local/lib
LDLIBS		= $(SHARED_GC_LIBS)

THREADLIBS	= \
		` case "$(GRADE)" in					\
		    *.par*|*.mps*) echo "-lpthread" ;;			\
		  esac							\
		`

$(HDR_CHECK_OBJS):	mercury_conf.h

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

# Stuff for Windows DLLs
# This needs to come after the include of Mmake.common, which defines USE_DLLS.

ifeq ($(USE_DLLS),yes)

DLL_CFLAGS	= -Dlib$(RT_LIB_NAME)_DEFINE_DLL

# the following header files are created automatically by Makefile.DLLs
LIB_DLL_H     = lib$(RT_LIB_NAME)_dll.h
LIB_GLOBALS_H = lib$(RT_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) $(MACHHDRS) $(LIB_DLL_H)

mercury_builtin_types.$(O):	mercury_hand_unify_compare_body.h
mercury_builtin_types.(O):	mercury_hand_unify_body.h mercury_hand_compare_body.h
mercury_deconstruct.$(O):	mercury_ml_expand_body.h
mercury_deep_copy.$(O):		mercury_deep_copy_body.h
mercury_type_info.$(O):		mercury_make_type_info_body.h
mercury_ho_call.$(O):		mercury_unify_compare_body.h

mercury_builtin_types.$(EXT_FOR_PIC_OBJECTS):	mercury_hand_unify_compare_body.h
mercury_builtin_types.(EXT_FOR_PIC_OBJECTS):	mercury_hand_unify_body.h mercury_hand_compare_body.h
mercury_deconstruct.$(EXT_FOR_PIC_OBJECTS):	mercury_ml_expand_body.h
mercury_deep_copy.$(EXT_FOR_PIC_OBJECTS):	mercury_deep_copy_body.h
mercury_type_info.$(EXT_FOR_PIC_OBJECTS):	mercury_make_type_info_body.h
mercury_ho_call.$(EXT_FOR_PIC_OBJECTS):		mercury_unify_compare_body.h

# ../tools/make_port_code makes both the .c and the .h file of the
# mercury_profiling_builtin module.
mercury_profiling_builtin.c:	mercury_profiling_builtin.h
mercury_profiling_builtin.h:	mercury_deep_call_port_body.h \
				mercury_deep_redo_port_body.h \
				mercury_deep_leave_port_body.h \
				../tools/make_port_code
	../tools/make_port_code

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

ifeq ($(findstring java,$(GRADE)),java)        
#
# For grade java, there's nothing to do

.PHONY: runtime
runtime:
	@echo "No Mercury runtime needed for grade=java"

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

# Managed C++ files compiled in this directory should not be put in an
# assembly of their own.  We put the runtime and library dlls into a single
# assembly called `mercury'
MS_CL_NOASM=:noAssembly
MS_CSCFLAGS=/t:module

# We need to build the following DLLs for the .NET runtime
DOTNET_DLLS=mercury_il.dll mercury_dotnet.dll

clean_local:
	rm -f $(DOTNET_DLLS)
	
runtime: $(DOTNET_DLLS)

mercury_dotnet.dll: mercury_il.dll $(MCPP_HDRS)

# Uncomment the following rule if you want to build Mercury in a form
# that will work with ROTOR.
# mercury_il.dll: mercury_il.il
#	sed '/REMOVE FOR ROTOR/s@^@//@' mercury_il.il > mercury_il.rotor.il
#	$(MS_ILASM) $(ALL_MS_ILASMFLAGS) /dll /quiet /OUT=mercury_il.dll \
#			mercury_il.rotor.il
 
else

.PHONY: runtime
runtime: lib$(RT_LIB_NAME).$A lib$(RT_LIB_NAME).$(EXT_FOR_SHARED_LIB)
runtime: $(RT_LIB_NAME).init
runtime: $(LIB_DLL_H) $(LIB_GLOBALS_H)

lib$(RT_LIB_NAME)$(DLL_DEF_LIB).$A: $(OBJS)
	rm -f lib$(RT_LIB_NAME)$(DLL_DEF_LIB).$A
	$(AR) $(ALL_ARFLAGS) \
		$(AR_LIBFILE_OPT)lib$(RT_LIB_NAME)$(DLL_DEF_LIB).$A $(OBJS)
	$(RANLIB) lib$(RT_LIB_NAME)$(DLL_DEF_LIB).$A

lib$(RT_LIB_NAME).so: $(PIC_OBJS)
	$(LINK_SHARED_OBJ) $(ERROR_UNDEFINED)				\
		-o lib$(RT_LIB_NAME).so $(PIC_OBJS)			\
		$(SHLIB_RPATH_OPT)$(FINAL_INSTALL_MERC_GC_LIB_DIR)	\
		$(LDFLAGS) $(LDLIBS) $(THREADLIBS)			\
		$(SHARED_LIBS)

endif
endif

$(RT_LIB_NAME).init: $(CFILES)
	cat `vpath_find $(CFILES)` | grep '^INIT ' > $(RT_LIB_NAME).init

mercury_conf.h.date: $(MERCURY_DIR)/config.status mercury_conf.h.in
	CONFIG_FILES= CONFIG_HEADERS=mercury_conf.h $(MERCURY_DIR)/config.status
	echo datestamp > mercury_conf.h.date

mercury_conf.h: mercury_conf.h.date
	@true

.PHONY: cs
cs: $(CFILES)

tags: $(CFILES) $(HDRS) $(BODY_HDRS) $(LIB_DLL_H)
	ctags $(CFILES) $(HDRS) $(BODY_HDRS) $(LIB_DLL_H)

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

# installation rules

.PHONY: install
install: install_headers install_init install_lib

.PHONY: install_dirs
install_dirs:
	-[ -d $(INSTALL_INC_DIR)/machdeps ] || \
		mkdir -p $(INSTALL_INC_DIR)/machdeps
	-[ -d $(INSTALL_MODULE_DIR) ] || mkdir -p $(INSTALL_MODULE_DIR)
	-[ -d $(INSTALL_MERC_LIB_DIR) ] || mkdir -p $(INSTALL_MERC_LIB_DIR)
	-[ -d $(INSTALL_CONF_DIR) ] || mkdir -p $(INSTALL_CONF_DIR)
	-[ -d $(INSTALL_RECONF_DIR)/runtime ] || \
		mkdir -p $(INSTALL_RECONF_DIR)/runtime

ifeq ($(findstring java,$(GRADE)),java)        

# For grade java, there's nothing to do.
# (The java version of the Mercury runtime is in mercury/java/runtime,
# not mercury/runtime, and its build and installation is handled by
# mercury/library/Mmakefile.)

.PHONY: install_headers 
install_headers:

.PHONY: install_init 
install_init:

.PHONY: install_lib 
install_lib:

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

.PHONY: install_headers 
install_headers: $(MCPP_HDRS) install_dirs
	cp `vpath_find $(MCPP_HDRS)` $(INSTALL_INC_DIR)

.PHONY: install_init
install_init: 

.PHONY: install_lib 
install_lib: $(DOTNET_DLLS) install_headers install_dirs
	cp `vpath_find $(DOTNET_DLLS)` $(INSTALL_MERC_LIB_DIR)

#MS_ILASMFLAGS=/debug

else

# mercury_conf.h needs to be in a separate directory so it can be 
# overridden by a different configuration.
.PHONY: install_headers
install_headers: $(HDRS) $(BODY_HDRS) $(MACHHDRS) \
		$(LIB_DLL_H) $(LIB_GLOBALS_H) install_dirs
	cp `vpath_find $(HDRS) $(BODY_HDRS) $(LIB_DLL_H) $(LIB_GLOBALS_H)` \
		$(INSTALL_INC_DIR)
	rm -f $(INSTALL_INC_DIR)/mercury_conf.h
	cp `vpath_find mercury_conf.h` $(INSTALL_CONF_DIR)
	-chmod u+w $(INSTALL_CONF_DIR)/mercury_conf.h
	cp `vpath_find mercury_conf.h.in` $(INSTALL_RECONF_DIR)/runtime
	cp `vpath_find $(MACHHDRS)` $(INSTALL_INC_DIR)/machdeps

.PHONY: install_init
install_init: $(RT_LIB_NAME).init install_dirs
	cp `vpath_find $(RT_LIB_NAME).init` $(INSTALL_MODULE_DIR)
	# "$(RT_LIB_NAME).init" used to be called "runtime.init".
	# If there is still a version with an old name lying around,
	# then delete it; otherwise the initialization would get done twice.
	rm -f $(INSTALL_MODULE_DIR)/runtime.init

.PHONY: install_lib install_dirs
install_lib: lib$(RT_LIB_NAME).$A lib$(RT_LIB_NAME).$(EXT_FOR_SHARED_LIB) \
		install_dirs
	cp `vpath_find lib$(RT_LIB_NAME).$A \
		lib$(RT_LIB_NAME).$(EXT_FOR_SHARED_LIB)` \
		$(INSTALL_MERC_LIB_DIR)
	$(RANLIB) $(INSTALL_MERC_LIB_DIR)/lib$(RT_LIB_NAME).$A

endif
endif

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

mercury_dotnet.cs : mercury_dotnet.cs.in
	CONFIG_FILES=$@ CONFIG_HEADERS= $(MERCURY_DIR)/config.status
	# check to ensure there were no mispelt autoconf variable names
	if grep -n '[^$$]@.*@' $@; then false; else true; fi

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

clean_local: clean_o clean_check

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

.PHONY: realclean_local
realclean_local:
	rm -f lib$(RT_LIB_NAME).$A lib$(RT_LIB_NAME).so $(RT_LIB_NAME).init
	rm -f mercury_conf.h mercury_conf.h.date mercury_dotnet.cs
	rm -f mercury_profiling_builtin.c mercury_profiling_builtin.h

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