#-----------------------------------------------------------------------------#
# Copyright (C) 1995 University of Melbourne.
# See the file COPYING for copying permission.
#-----------------------------------------------------------------------------#

# Mmake - the top-level Mmake file for the Mercury implementation

MAIN_TARGET=all

MERCURY_DIR=.
include Mmake.common

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

SUBDIRS = scripts util runtime boehm_gc library compiler doc profiler

SUBDIR_MMAKE =	PATH=../scripts:../util:$$PATH \
		MMAKE_VPATH=. \
		MMAKE_VARS=../scripts/Mmake.vars \
		MMAKE_RULES=../scripts/Mmake.rules \
		../scripts/mmake $(MMAKEFLAGS)

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

# `mmake dep' ensures that the .dep files exist;
# `mmake depend' forces them to be remade to ensure that they are up-to-date.

.PHONY: dep
dep: dep_library dep_compiler dep_profiler

.PHONY: dep_library
dep_library: library/library.dep

library/library.dep:
	cd library && $(SUBDIR_MMAKE) depend

.PHONY: dep_compiler
dep_compiler: compiler/mercury_compile.dep

compiler/mercury_compile.dep: library/library.dep
	cd compiler && $(SUBDIR_MMAKE) depend

.PHONY: dep_profiler
dep_profiler: profiler/mercury_profile.dep

compiler/mercury_profile.dep: library/library.dep
	cd profiler && $(SUBDIR_MMAKE) depend

# depend_library MUST be done before depend_compiler and depend_profiler

.PHONY: depend
depend:
	cd library && $(SUBDIR_MMAKE) depend
	cd compiler && $(SUBDIR_MMAKE) depend
	cd profiler && $(SUBDIR_MMAKE) depend

.PHONY: depend_library
depend_library:
	cd library && $(SUBDIR_MMAKE) depend

.PHONY: depend_compiler
depend_compiler:
	cd compiler && $(SUBDIR_MMAKE) depend

.PHONY: depend_profiler
depend_profiler:
	cd profiler && $(SUBDIR_MMAKE) depend

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

.PHONY: all
all: $(SUBDIRS)

.PHONY: scripts
scripts:
	cd scripts && $(SUBDIR_MMAKE)

.PHONY: runtime
runtime: scripts
	cd runtime && $(SUBDIR_MMAKE)

.PHONY: boehm_gc
boehm_gc: scripts
	cd boehm_gc && $(SUBDIR_MMAKE)

.PHONY: library
library: dep_library scripts
	cd library && $(SUBDIR_MMAKE)

.PHONY: compiler
compiler: dep_compiler scripts util runtime boehm_gc library
	cd compiler && $(SUBDIR_MMAKE)

.PHONY: doc
doc: scripts
	cd doc && $(SUBDIR_MMAKE)

.PHONY: util
util: scripts
	cd util && $(SUBDIR_MMAKE)

.PHONY: profiler
profiler: scripts
	cd profiler && $(SUBDIR_MMAKE)

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

.PHONY: tags
tags: tags_compiler tags_profiler tags_library

.PHONY: tags_compiler
tags_compiler:
	cd compiler && $(SUBDIR_MMAKE) tags

.PHONY: tags_library
tags_library:
	cd library && $(SUBDIR_MMAKE) tags

.PHONY: tags_profiler
tags_profiler:
	cd profiler && $(SUBDIR_MMAKE) tags

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

configure: configure.in
	autoconf

config.status: configure
	if [ -f ./config.status ]; then \
		./config.status --recheck; \
	else \
		echo "======> You need to run configure!"; \
		exit 1; \
	fi

Mmake.common: Mmake.common.in config.status
	./config.status

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

# Generally you should do a `mmake realclean' before doing `mmake tar'.

.PHONY: tar
tar:
	touch Mmake.params
	cd util && mmake
	cd runtime && $(SUBDIR_MMAKE) cs runtime.init
	cd library && $(SUBDIR_MMAKE) depend
	cd library && $(SUBDIR_MMAKE) ints cs library.init
	cd compiler && $(SUBDIR_MMAKE) depend
	cd compiler && $(SUBDIR_MMAKE) ints cs tags
	cd profiler && $(SUBDIR_MMAKE) depend
	cd profiler && $(SUBDIR_MMAKE) ints cs tags
	cd library && $(SUBDIR_MMAKE) tags
	cd doc && $(SUBDIR_MMAKE) info html dvi
	# clean up
	cd scripts && mmake -s realclean
	cd util && mmake realclean
	-rm -f errs errs2
	-rm -f config.status config.log config.cache
	-rm -f runtime/conf.h # XXX compiler/conf.m
	-rm -f Mmake.common
	cd .. && (tar -cf - mercury | gzip -9 > mercury.tar.gz)

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

.PHONY: install
install: all install_scripts install_util install_runtime install_boehm_gc \
		install_library install_compiler install_doc \
		install_runtime_grades install_library_grades \
		install_profiler

.PHONY: install_scripts
install_scripts: scripts
	cd scripts && $(SUBDIR_MMAKE) install

.PHONY: install_util
install_util: util
	cd util && $(SUBDIR_MMAKE) install

.PHONY: install_runtime
install_runtime: runtime
	cd runtime && $(SUBDIR_MMAKE) install

.PHONY: install_boehm_gc
install_boehm_gc: boehm_gc
	cd boehm_gc && $(SUBDIR_MMAKE) install

.PHONY: install_library
install_library: dep_library library
	cd library && $(SUBDIR_MMAKE) install

.PHONY: install_compiler
install_compiler: dep_compiler compiler
	cd compiler && $(SUBDIR_MMAKE) install

.PHONY: install_doc
install_doc: doc
	cd doc && $(SUBDIR_MMAKE) install

.PHONY: install_profiler
install_profiler: profiler
	cd profiler && $(SUBDIR_MMAKE) install

.PHONY: install_runtime_grades
install_runtime_grades:
	cd runtime && \
	rm -rf tmp_dir && \
	mkdir tmp_dir && \
	(mv -f *.o *.pic_o *.a *.so tmp_dir || true) && \
	for grade in $(LIBGRADES); do \
		$(SUBDIR_MMAKE) GRADE=$$grade install_lib || exit 1; \
		rm -f *.o *.pic_o *.a *.so; \
	done && \
	(mv tmp_dir/* . || true)

.PHONY: install_library_grades
install_library_grades: dep_library
	cd library && \
	rm -rf tmp_dir && \
	mkdir tmp_dir && \
	(mv -f *.c *.o *.pic_o *.a *.so tmp_dir || true) && \
	for grade in $(LIBGRADES); do \
		$(SUBDIR_MMAKE) GRADE=$$grade install_library || exit 1; \
		rm -f *.c *.o *.pic_o *.a *.so; \
	done && \
	(mv tmp_dir/* . || true)

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

clean: clean_subdirs

.PHONY: clean_subdirs
clean_subdirs:
	rm -rf runtime/tmp_dir library/tmp_dir
	for dir in $(SUBDIRS); do \
		if [ "$$dir" = scripts ]; then continue; fi; \
		(cd $$dir; $(SUBDIR_MMAKE) clean) \
	done

realclean: realclean_subdirs realclean_config_cache

.PHONY: realclean_subdirs
realclean_subdirs:
	rm -rf stage[1-4]
	for dir in $(SUBDIRS); do \
		if [ "$$dir" = scripts ]; then continue; fi; \
		(cd $$dir; $(SUBDIR_MMAKE) realclean) \
	done
	cd scripts; $(SUBDIR_MMAKE) realclean

.PHONY: realclean_config_cache
realclean_config_cache:
	rm -f config.cache

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