# Makefile - Don Yang (uguu.org)
#
# 04/16/06

# Build config - order matters!
SRCS = file.ml path.ml dir.ml repository.ml version.ml commit.ml main.ml
OBJS = $(SRCS:.ml=.cmx)
HDRS = $(SRCS:.ml=.mli)
TEST_SRCS = file_unittest.ml path_unittest.ml dir_unittest.ml

EEXT = .exe
LIBS = unix.cmxa str.cmxa
DEPS = depend
TARGET = chii$(EEXT)
TEST_TARGETS = $(TEST_SRCS:.ml=$(EEXT))

OCAML = ocaml
OCAMLC = ocamlc
OCAMLOPT = ocamlopt
OCAMLDEP = ocamldep
TEXI2HTML = texi2html

# Build rules
.SUFFIXES: .ml .mli .cmx .cmi .texi .html

.ml.cmx:
	$(OCAMLOPT) -c $<

.mli.cmi:
	$(OCAMLC) -c $<

.texi.html:
	$(TEXI2HTML) $<

# Targets
all: $(TARGET) chii.html

$(TARGET): $(OBJS)
	$(OCAMLOPT) -o $@ $(LIBS) $(OBJS)

$(DEPS): $(SRCS) $(HDRS) $(TEST_SRCS)
	$(OCAMLDEP) *.ml *.mli > $@

include $(DEPS)

# Tests
test tests: $(TEST_TARGETS)

file_unittest$(EEXT): file.cmx file_unittest.cmx
	$(OCAMLOPT) -o $@ $(LIBS) $^
	-rm -f file_unittest.tmp
	./$@ || rm -f $@

path_unittest$(EEXT): path.cmx path_unittest.cmx
	$(OCAMLOPT) -o $@ $(LIBS) $^
	./$@ || rm -f $@

dir_unittest$(EEXT): file.cmx path.cmx dir.cmx dir_unittest.cmx
	$(OCAMLOPT) -o $@ $(LIBS) $^
	-rm -rf dir_unittest.tmp
	./$@ || rm -f $@

# Maintenance
clean:
	-rm -f *.cmi *.cmx *.obj
	-rm -f chii.html $(TARGET) $(TEST_TARGETS)

backup:
	-rm -f backup.tar.gz
	tar cf backup.tar *.ml* *.sh *.texi Makefile
	gzip -9 backup.tar
