#-----------------------------------------------------------------------------#
# vim: ts=8 sw=8 noexpandtab ft=make
#-----------------------------------------------------------------------------#
# Copyright (C) 2016, 2025 The Mercury team.
# This file may only be copied under the terms of the GNU General
# Public Licence - see the file COPYING in the Mercury distribution.
#-----------------------------------------------------------------------------#

MERCURY_DIR=..
LINK_STATIC=yes
include $(MERCURY_DIR)/Mmake.common
-include ../Mmake.params
-include Mmake.grade_lib.params

# Override the default rule in `mmake --use-mmc-make' that asks `mmc' to
# create a missing optional params file.
Mmake.grade_lib.params:

# Module-specific options should go in Mercury.options so they can be found
# by `mmc --make'.
-include Mercury.options

MCFLAGS += --flags GRADE_LIB_FLAGS $(CONFIG_OVERRIDE)

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

# Tell the C# compiler where the stdlib assembly is.
#
ifneq ("$(filter csharp%,$(GRADE))","")
CSCFLAGS=-lib:../library -r:mer_std.dll
endif

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

ifneq ("$(filter csharp% java%,$(GRADE))","")
MLOBJS =
endif

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

# The main targets are the executables of the utilities that use and test
# the grade library. The grade "library" itself is not a target, because
# it is intended to be used *only* by these utilities (and perhaps later,
# similar utilities) and by the compiler itself. The utilities use the
# modules of the library in this directory, while we intend to give
# the compiler access by copying the source files of the grade library,
# in read-only form, to the compiler directory, just the way we copy
# the modules of the mdbcomp library to the slice and deep_profiler
# directories. The source distribution will get those read-only copies
# as well. This arrangement has the advantage that the grade in which
# the compiler is compiled is independent of the grade of the utilities.
MAIN_TARGET = all
MERCURY_MAIN_MODULES = choose_grade test_grades try_all_grade_structs
# EXTRA_MCFLAGS = --trace-flag debug_solver

DEPENDS = $(patsubst %,%.depend,$(MERCURY_MAIN_MODULES))
DEPS = $(patsubst %,%.dep,$(MERCURY_MAIN_MODULES))

# This directory contains source files for which the module name
# doesn't match the file name, so smart recompilation won't work
# without the Mercury.modules file.
Mercury.modules: GRADE_LIB_FLAGS
	$(MC) $(ALL_GRADEFLAGS) $(ALL_MCFLAGS) -f *.m

.PHONY: all
all:	$(MERCURY_MAIN_MODULES)

.PHONY: depend
depend:	GRADE_LIB_FLAGS Mercury.modules $(DEPENDS)
$(DEPENDS): GRADE_LIB_FLAGS Mercury.modules

.PHONY: dep
dep:	GRADE_LIB_FLAGS Mercury.modules $(DEPS)

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

# We need the shenanigans with .grade_tags to avoid situations in which
# "mmake tags" in this directory does nothing even in the absence of a tags
# file in this directory, because mmake uses VPATH to find ../library/tags
# and believes it to be the tags file we are asking for.
#
# We give files in mdbcomp to mtags before the files in this directory
# because we want references to entities defined in the modules copied from the
# mdbcomp directory to this directory to go the actual, effective definitions,
# the definitions which can be changed *without* those changes going away
# on the next copy.

MTAGS = $(SCRIPTS_DIR)/mtags

tags:	.grade_tags

.grade_tags: $(MTAGS) $(wildcard *.m) $(wildcard $(LIBRARY_DIR)/*.m)
	$(MTAGS) *.m $(LIBRARY_DIR)/*.m
	@touch .grade_tags

.PHONY: tags_file_exists
tags_file_exists:
	@if test ! -f tags; \
	then \
		echo making tags; \
		$(MTAGS) *.m $(LIBRARY_DIR)/*.m; \
		touch .grade_tags; \
	fi

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

# The documentation of the reason for this set of rules
# can be found in library/Mmakefile.

.PHONY: all_os all_cs all_css all_javas
ifeq ($(MMAKE_USE_MMC_MAKE),yes)
all_os: \
	choose_grade.all_os \
	test_grades.all_os \
	try_all_grade_structs.all_os

all_cs: \
	choose_grade.all_cs \
	test_grades.all_cs \
	try_all_grade_structs.all_cs

all_css: \
	choose_grade.all_css \
	test_grades.all_css \
	try_all_grade_structs.all_css

all_javas: \
	choose_grade.all_javas \
	test_grades.all_javas \
	try_all_grade_structs.all_javas

else
ifneq ($(origin choose_grade.all_os),undefined)
all_os: \
	$(choose_grade.all_os) \
	$(test_grades.all_os) \
	$(try_all_grade_structs.all_os)

all_cs: \
	$(choose_grade.all_cs) \
	$(test_grades.all_cs) \
	$(try_all_grade_structs.all_cs)

all_css: \
	$(choose_grade.all_css) \
	$(test_grades.all_css) \
	$(try_all_grade_structs.all_css)

all_javas: \
	$(choose_grade.all_javas) \
	$(test_grades.all_javas) \
	$(try_all_grade_structs.all_javas)

else
all_os:
	mmake depend; mmake all_os

all_cs:
	mmake depend; mmake all_cs

all_css:
	mmake depend; mmake all_css

all_javas:
	mmake depend; mmake all_javas
endif
endif

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

realclean_local:
	rm -f Mercury.modules tags GRADE_LIB_FLAGS

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