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

# Mmake - Mmake file for the Mercury runtime library

MAIN_TARGET=lib

MERCURY_DIR=..
include ../Mmake.common

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

CFLAGS		= -I$(MERCURY_DIR)/runtime -I $(MERCURY_DIR)/boehm_gc -g \
		  $(PROFFLAGS)
MGNUC		= MERCURY_C_INCL_DIR=. mgnuc
MGNUCFLAGS	= $(CFLAGS)

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

HDRS		= access.h aux.h conf.h dlist.h debug.h dummy.h engine.h \
		  getopt.h goto.h heap.h imp.h init.h io_rt.h label.h \
		  overflow.h prof.h memory.h regorder.h regs.h std.h stacks.h \
		  table.h tags.h timing.h wrapper.h ext_signal.h ext_stdio.h \
		  ext_stdlib.h prof.h type_info.h

MACHHDRS	= machdeps/no_regs.h machdeps/i386_regs.h \
		  machdeps/mips_regs.h machdeps/sparc_regs.h
MODS		= engine.mod io_rt.mod wrapper.mod call.mod solutions.mod
MOD_CS		= engine.c io_rt.c wrapper.c call.c solutions.c
MOD_OS		= $(MOD_CS:.c=.o)
ORIG_CS		= access.c aux.c dlist.c dummy.c label.c \
		  memory.c table.c timing.c prof.c
ORIG_OS		= $(ORIG_CS:.c=.o)
OBJS		= $(MOD_OS) $(ORIG_OS)
# OBJS		= engine.o io_rt.o wrapper.o call.o solutions.o \
# 		  access.o aux.o dlist.o dummy.o label.o \
# 		  memory.o table.o timing.o
PIC_OBJS	= $(OBJS:.o=.$(PIC_O))

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

$(OBJS): $(HDRS) $(MACHHDRS)

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

.PHONY: lib
lib: libmer.a libmer.$(SO) runtime.init

libmer.a: $(OBJS)
	ar cr libmer.a $(OBJS)
	$(RANLIB) libmer.a

libmer.so: $(PIC_OBJS)
	$(LINK_SHARED_OBJ) -o libmer.so $(PIC_OBJS)

runtime.init: $(MOD_CS)
	cat $(MOD_CS) | grep '^INIT ' > runtime.init

conf.h: ../config.status conf.h.in
	cd ..; CONFIG_FILES= CONFIG_HEADERS=runtime/conf.h ./config.status

.PHONY: cs
cs: $(MOD_CS)

tags:
	ctags $(MODS) $(ORIG_CS)

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

# installation rules

.PHONY: install
install: install_headers install_init install_lib

.PHONY: install_headers
install_headers: $(HDRS) $(MACHHDRS)
	-[ -d $(INSTALL_INC_DIR)/machdeps ] || \
		mkdir -p $(INSTALL_INC_DIR)/machdeps
	cp `vpath_find $(HDRS)` $(INSTALL_INC_DIR)
	chmod +w $(INSTALL_INC_DIR)/conf.h
	cp `vpath_find $(MACHHDRS)` $(INSTALL_INC_DIR)/machdeps

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

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

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

# prevent GNU make from removing these intermediate files
dont_remove: engine.c io_rt.c wrapper.c call.c solutions.c

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

clean: clean_o clean_mod_c

.PHONY: clean_o
clean_o:
	rm -f *.o

.PHONY: clean_mod_c
clean_mod_c:
	for file in *.mod; do \
		rm -f `basename $$file .mod`.c; \
	done

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