###################################
#
#  Main Makefile for theories
# 
###################################

include ../config

MAKE=make 

##################
# The Coq library
##################

COQTH=$(COQLIB)/theories
LIBDIRS=LOGIC ARITH BOOL ZARITH LISTS SETS RELATIONS RELATIONS/WELLFOUNDED \
	SORTING TREES REALS
LIBDIRSSMALL=LOGIC ARITH BOOL ZARITH LISTS RELATIONS RELATIONS/WELLFOUNDED \
	SORTING TREES
OTHDIRS=TESTS DEMOS DEMOS/OMEGA DEMOS/PROGRAMS
ALLDIRS=$(LIBDIRS) $(OTHDIRS)

# the old "for rep in ... " construction didn't halt if some theories 
# did not compile
library:
	cd LOGIC ;$(MAKE) all
	cd ARITH ;$(MAKE) all
	cd BOOL ;$(MAKE) all
	cd ZARITH ;$(MAKE) all
	cd LISTS ;$(MAKE) all
	cd SETS ;$(MAKE) all
	cd RELATIONS ;$(MAKE) all
	cd RELATIONS/WELLFOUNDED ;$(MAKE) all
	cd SORTING ;$(MAKE) all
	cd TREES; $(MAKE) all 

small-library:
	cd LOGIC ;$(MAKE) all
	cd ARITH ;$(MAKE) all
	cd BOOL ;$(MAKE) all
	cd ZARITH ;$(MAKE) all
	cd LISTS ;$(MAKE) all
	cd RELATIONS ;$(MAKE) all
	cd RELATIONS/WELLFOUNDED ;$(MAKE) all
	cd SORTING ;$(MAKE) all
	cd TREES; $(MAKE) all

install-library:
	for rep in INIT $(LIBDIRS) ; do \
	  (cd $$rep; $(MAKE) install-library); \
	done

install-small-library:
	set -e; for rep in INIT $(LIBDIRSSMALL) ; do \
	  (cd $$rep; $(MAKE) install-library); \
	done
	-cd TESTS; $(MAKE) -k install-library
	-cd DEMOS; $(MAKE) -k install-library

#########################################################
# The whole bench `make bench', `make opt' or `make all'
#########################################################

bench: byte
byte:
	@$(MAKE) "OPT=" "ALL=byte" all
opt:
	@$(MAKE) "OPT=-opt" "ALL=opt" all

all: library others

others: 
	for rep in $(OTHDIRS) ; do \
	  (cd $$rep ; $(MAKE) OPT=$(OPT) all) ; \
	done


#######
# HTML
#######

HTMLDIRS = INIT $(LIBDIRS)

html:
	for rep in $(HTMLDIRS) ; do \
	  (cd $$rep ; $(MAKE) html) ; \
	done

clean-html:
	for rep in $(HTMLDIRS) ; do \
	  (cd $$rep ; rm -f *.html) ; \
	done

WWW=$(COQTOP)/../www/theories

install-html:
	for rep in $(HTMLDIRS) ; do \
	  if test -d $(WWW)/$$rep ; then : ; else $(MKDIR) $(WWW)/$$rep; fi ;\
	  (cd $$rep ; cp *.html $(WWW)/$$rep) ; \
	done

################
# Documentation
################

DOCDIR=$(COQTOP)/doc/library
DOCFILE=$(DOCDIR)/libdoc.tex
COQ2LATEX=$(COQTOP)/bin/$(ARCH)/coq2latex -latex -nodoc -2e
GALLINA=$(COQTOP)/bin/$(ARCH)/gallina -stdout


# ls -tr trie les fichiers dans l'ordre inverse de leur date de cration
# En supposant que make fait son boulot, ca fait un tri topologique du
# graphe des dpendances
doc: library
	rm -f $(DOCFILE) ; touch $(DOCFILE)
	for rep in $(LIBDIRS) ; do \
	  echo >> $(DOCFILE) ; \
	  echo "\Newpage" >> $(DOCFILE) ; \
	  echo "\section{$$rep}" >> $(DOCFILE) ; \
	  (cd $$rep ; \
	   cat Contents.tex >> $(DOCFILE) ; \
	   VOFILES=`ls -tr *.vo` ; \
	   for file in $$VOFILES ; do \
		VF=`basename $$file \.vo` ; \
		make GALLINA="$(COQTOP)/bin/$(ARCH)/gallina -nocomments" $$VF.g ; \
		$(COQ2LATEX) $$VF.g >> $(DOCFILE) ; \
           done \
	  ) ; \
	done

INIT/Contents.tex:
	cd INIT; $(MAKE) Contents.tex

##########
# Gallina
##########

gallina:
	for rep in $(LIBDIRS) ; do \
	  (cd $$rep ; $(MAKE) gallina) ; \
	done

clean-gallina:
	for rep in $(LIBDIRS) ; do \
	  (cd $$rep ; rm -f *.g) ; \
	done


###############
# Dependencies
###############

depend:
	for rep in INIT $(ALLDIRS) ; do \
	  (cd $$rep ; $(MAKE) depend) ; \
	done


###########
# Cleaning
###########

clean:
	for rep in INIT $(ALLDIRS) ; do \
	  (cd $$rep ; $(MAKE) clean) ; \
	done

archclean:
	find . \( -name "*.cmx" -o -name "*.o" \) -print -exec rm -f {} \;
