# -*-Makefile-*- Makelibs
# ./make/Makelibs -- top level makefile
#
# This makefile can be used to make the sibling ACL2 proof libraries.  We assume
# that all of the libraries are rooted at ../

# Revised 04/10/06 by Doug Harper

.PHONY: all clean status diff update commit tag

# The order of books in this listing is the order they will be built in.  You
# must ensure that the dependencies for each directory are listed before the
# directory occurs!
#
# To do so semi-automatically, first create the BOOKS list below, in any
# order.  Then manually "make Makelibs.BOOKS -f Makelibs" to create a
# file that has the books in correct build order.  Then replace the
# definition of BOOKS below with the freshly computed one.

BOOKS = ../symbol-fns \
	../util \
	../adviser \
	../nary \
	../lists \
	../osets \
	../syntax \
	../bags \
	../alists \
	../records \
	../paths \
	../maps \
	../dtrees \
	../defpun \
	../defstructure \
	../super-ihs \
	../gacc

# Building All Of The Books

# You can use "make all" to build all of the books.  If you want to use
# an ACL2 image other than the one in Makefile.top, you can add
#
#   "ACL2=[ .../saved_acl2 ]"
#
# to your Make command.  You can also use DEBUG=1 if you want to use
# debug mode on all of the Makefiles, but this is probably unwanted.

ifdef ACL2

	# Note: Don't change the method below for computing ACL2_ROOT to
	# either of the following, for the reasons stated:
	#
	#   $(shell dirname $(ACL2))
	#
	#     This method fails and crashes the make when $(ACL2) is the empty
	#     string, which could occur when it was inadvertently set to "".
	#     The dirname command expects exactly one argument.
	#
	#   $(dir $(ACL2))
	#
	#     This method leaves a final slash on the directory name.
	#
	ACL2_ROOT = $(shell echo $(ACL2) | sed "s/\/[^/]*$$//")
	EXPORTS = DEBUG="$(DEBUG)" ACL2="$(ACL2)" ACL2_ROOT="$(ACL2_ROOT)"
else
	EXPORTS = DEBUG="$(DEBUG)"
endif

TIMELOG = $(shell pwd)/Time.log

all:
	@echo "" >> $(TIMELOG)
	@echo "Building on `date`" >> $(TIMELOG)
	@for book in $(BOOKS) ;\
	do \
		if [ -d $$book ] ;\
		then \
			echo "" ;\
			echo "Building $$book" ;\
			cd $$book; \
			export $(EXPORTS); \
			if [ -f /usr/bin/time ] ;\
			then \
				/usr/bin/time -ao $(TIMELOG) \
				    -f "%e Seconds for $$book" $(MAKE) -s all ;\
				export STATUS=$$? ; \
			else \
				$(MAKE) -s all ;\
				export STATUS=$$? ; \
			fi ;\
			if [ $$STATUS -ne 0 ] ; \
			then \
				exit $$STATUS ;\
			fi ;\
		else \
			echo "Skipping $$book (not a directory)" | \
			    tee -a $(TIMELOG) ;\
		fi ;\
	done

deps:
	@echo "" >> $(TIMELOG)
	@echo "Building deps on `date`" >> $(TIMELOG)
	@for book in $(BOOKS) ;\
	do \
		if [ -d $$book ] ;\
		then \
			echo "" ;\
			echo "Building deps for $$book" ;\
			cd $$book; \
			export $(EXPORTS); \
			if [ -f /usr/bin/time ] ;\
			then \
				/usr/bin/time -ao $(TIMELOG) \
				    -f "%e Seconds for $$book" $(MAKE) -s deps ;\
				export STATUS=$$? ; \
			else \
				$(MAKE) -s deps ;\
				export STATUS=$$? ; \
			fi ;\
			if [ $$STATUS -ne 0 ] ; \
			then \
				exit $$STATUS ;\
			fi ;\
		else \
			echo "Skipping deps for $$book (not a directory)" | \
			    tee -a $(TIMELOG) ;\
		fi ;\
	done

fasl:
	@echo "" >> $(TIMELOG)
	@echo "Building fasl on `date`" >> $(TIMELOG)
	@for book in $(BOOKS) ;\
	do \
		if [ -d $$book ] ;\
		then \
			echo "" ;\
			echo "Building fasl for $$book" ;\
			cd $$book; \
			export $(EXPORTS); \
			unset ACL2_AAMP; \
			if [ -f /usr/bin/time ] ;\
			then \
				/usr/bin/time -ao $(TIMELOG) \
				    -f "%e Seconds for $$book" $(MAKE) -s fasl ;\
				export STATUS=$$? ; \
			else \
				$(MAKE) -s fasl ;\
				export STATUS=$$? ; \
			fi ;\
			if [ $$STATUS -ne 0 ] ; \
			then \
				exit $$STATUS ;\
			fi ;\
		else \
			echo "Skipping fasl for $$book (not a directory)" | \
			    tee -a $(TIMELOG) ;\
		fi ;\
	done

# Cleaning All Of The Books
# You can use "make clean" to remove temporary files from all of the books.
# Note: This will wipe out all of your certified files, and you will have
# to rebuild everything!

clean:
	@echo ""
	$(RM) Makelibs.BOOKS
	@for book in $(BOOKS) ; \
	do \
		if [ -d $$book ] ; \
		then \
			echo "Cleaning $$book" ; \
			cd $$book; \
			export $(EXPORTS); \
			$(MAKE) -s clean ; \
			echo "" ;\
		else \
			echo "Skipping $$book (not a directory)" ;\
			echo "" ;\
		fi ;\
	done

# Generating the BOOKS List in Build Order

# You can "make Makelibs.BOOKS -f Makelibs" to generate the definition
# of BOOKS with the books listed in build dependence order.

Makelibs.BOOKS::
	./gendeps.csh $(BOOKS) > $@

# Checking CVS Status
# You can check the CVS status of all of your books by executing "make status".
# This will recursively descend into each directory and issue its own make
# status command.  You can also use "make diff" in order to see what changes
# have been made.

status:
	@echo "" ;\
	echo "Examining ." ;\
	cvs status 2>&1 | egrep "(File\:|aborted)" ;\
	for book in $(BOOKS) ; \
	do \
		if [ -d $$book ] ; \
		then \
			echo "" ;\
			echo "Examining $$book" ;\
			cd $$book; \
			cvs status *.lisp | egrep "(File\:|aborted)" ;\
			cvs status *.acl2 | egrep "(File\:|aborted)" ;\
			cvs status 2>&1 | egrep "(File\:|aborted)" ;\
		else \
			echo "Skipping $$book (not a directory)" ;\
		fi ;\
	done ;\
	echo ""

diff:
	@echo "" ;\
	echo "Examining ." ;\
	cvs diff ;\
	for book in $(BOOKS) ;\
	do \
		if [ -d $$book ] ;\
		then \
			echo "" ;\
			echo "Examining $$book" ;\
			cd $$book ;\
			cvs diff ;\
		else \
			echo "Skipping $$book (not a directory)" ;\
		fi ;\
	done

# Updating from CVS Repository
# You can execute "make update" in order to perform a CVS update in each of the
# books directories, which will make sure your local files are fresh with
# respect to the CVS server.

update:
	@for book in $(BOOKS) ;\
	do \
		if [ -d $$book ] ; \
		then \
			echo "Updating $$book" ;\
			cd $$book; \
			cvs update -d 2>&1 | grep -v "^cvs update:" ;\
		else \
			echo "Skipping $$book (not a directory)" ;\
		fi ;\
	done

# Committing Changes to CVS
# You can execute "make commit" in order to commit your changes in each
# directory.  Note that you will need to provide MESSAGE="my commit message"
# for this command to be accepted.

commit:
	@if [ -n "$(MESSAGE)" ] ;\
	then \
		for book in $(BOOKS) ;\
		do \
			if [ -d $$book ] ; \
			then \
				echo "" ;\
				echo "Committing $$book" ;\
				cd $$book; cvs commit -m "$(MESSAGE)" ; \
			fi ;\
		done ;\
	else \
		echo "Refusing to commit without MESSAGE." ;\
		echo "Try adding 'MESSAGE="my message"'" \
		     " to your make command." ;\
	fi

tag: commit
	@if [ -n "$(TAGNAME)" ] ;\
	then \
		for book in $(BOOKS) ;\
		do \
			if [ -d $$book ] ;\
			then \
				echo "" ;\
				echo "Tagging $$book" ;\
				cd $$book; cvs -q tag "$(TAGNAME)" ;\
			fi ;\
		done ;\
	else \
		echo "Refusing to tag without TAGNAME." ;\
		echo "Try adding 'TAGNAME="name"' to your make command." ;\
	fi
