# This Makefile generates caml_cduce.cmo/.cmx
# 
# We need the units such that typing/types.cmo  (.cmx)
#
# - If OCaml has been compiled in a directory DIR,
#   you can do:
#     make MODEL=tree PREFIX=DIR
#   e.g.: make caml_cduce.cmo MODEL=tree PREFIX=$HOME/ocaml-3.07
#
# - If you have a (flat) directory DIR will all the compiled units 
#   from utils/ parsing/ and typing/, you can do:
#     make MODEL=flat PREFIX=DIR
#   e.g.: make caml_cduce.cmo MODEL=flat PREFIX=$HOME/godi/lib/ocaml/compiler-lib


OCAML_OBJECTS=$(patsubst %,$(PREFIX)/%, $(UNITS))

OBJECTS=asttypes.ml $(OCAML_OBJECTS)
XOBJECTS=$(OBJECTS:.cmo=.cmx)

ifeq ($(MODEL),flat)
  UNITS= $(subst utils/,,$(UTILS)) \
         $(subst parsing/,,$(PARSING)) \
         $(subst typing/,,$(TYPING))
  INCLUDES = -I $(PREFIX)
  ASTTYPES = $(PREFIX)/asttypes.cmi
else
ifeq ($(MODEL),tree)
  UNITS= $(UTILS) $(PARSING) $(TYPING)
  INCLUDES = -I $(PREFIX)/utils -I $(PREFIX)/parsing -I $(PREFIX)/typing
  ASTTYPES = $(PREFIX)/parsing/asttypes.cmi
else
 $(error Set MODEL=flat or MODEL=tree)
endif
endif

caml_cduce.cmo: asttypes.ml
	@echo "Build $@"
	$(HIDE)ocamlc -pack -o $@ $(INCLUDES) $(OBJECTS)

caml_cduce.cmx: asttypes.ml
	@echo "Build $@"
	$(HIDE)ocamlopt -pack -o $@ $(INCLUDES) $(XOBJECTS)

## Hack to rebuild asttypes.ml from asttypes.cmi
## (because -pack needs .cmo in 3.07)

.PHONY: asttypes.ml
asttypes.ml: cmi2ml
	@echo "Create $@"
	$(HIDE)./cmi2ml Asttypes $(ASTTYPES) > asttypes.ml

cmi2ml: cmi2ml.ml
	@echo "Build $@"
	$(HIDE)ocamlc -o $@ $(INCLUDES) $(OCAML_OBJECTS) $<


UTILS=utils/misc.cmo utils/tbl.cmo utils/config.cmo \
  utils/clflags.cmo utils/consistbl.cmo

PARSING=parsing/longident.cmo

TYPING=typing/ident.cmo typing/path.cmo \
  typing/primitive.cmo typing/types.cmo \
  typing/btype.cmo typing/oprint.cmo \
  typing/subst.cmo typing/predef.cmo \
  typing/datarepr.cmo typing/env.cmo \
  typing/ctype.cmo typing/printtyp.cmo


#UTILS=utils/misc.cmo utils/tbl.cmo utils/config.cmo \
#  utils/clflags.cmo utils/terminfo.cmo utils/ccomp.cmo utils/warnings.cmo \
#  utils/consistbl.cmo
#
#PARSING=parsing/linenum.cmo parsing/location.cmo parsing/longident.cmo \
#  parsing/syntaxerr.cmo parsing/parser.cmo \
#  parsing/lexer.cmo parsing/parse.cmo parsing/printast.cmo
#
#TYPING=typing/ident.cmo typing/path.cmo \
#  typing/primitive.cmo typing/types.cmo \
#  typing/btype.cmo typing/oprint.cmo \
#  typing/subst.cmo typing/predef.cmo \
#  typing/datarepr.cmo typing/env.cmo \
#  typing/typedtree.cmo typing/ctype.cmo \
#  typing/printtyp.cmo typing/includeclass.cmo \
#  typing/mtype.cmo typing/includecore.cmo \
#  typing/includemod.cmo typing/parmatch.cmo \
#  typing/typetexp.cmo typing/stypes.cmo typing/typecore.cmo \
#  typing/typedecl.cmo typing/typeclass.cmo \
#  typing/typemod.cmo

