#!/usr/latest/bin/make -f
#
# Makefile used to three-stage build a tree of source code, usually devo.  
#

# Every invocation of this Makefile needs to have a variable set (host), 
# which is the named used for ./configure, and also the prefix for the
# various files and directories used in a three stage.
#

ifndef host
error: 
	@echo
	@echo
	@echo You must set the variable \"host\" to use this Makefile
	@echo
	@echo
	@exit 1
else

# 
# from here to very near the end of the file is the real guts of this 
# Makefile, and it is not seen if the variable 'host' is not set 
# 

TREE 		:= progressive
ROOTING 	:= /opt/cygnus-sol2-1.0
CVS_MODULE 	:= progressive-solaris
TAG_DATE 	:= 920615
RELEASE_TAG 	:= $(CVS_MODULE)-$(TAG_DATE)
release_root 	:= $(ROOTING)

TIME 	:= time
NICE 	:= nice -17
MF   	:= -w
GNUC 	:= gcc -g

SHELL 	:= /bin/sh

INSTDIR  := $(shell pwd)/$(host)
PREFIXES := prefix=$(ROOTING)

ifdef target
arch=$(host)=$(target)
endif

.PHONY: all
all:	do1 do2 do3

.PHONY: configuration
configuration: $(TREE) $(host)-stamp-stage1-configured

.PHONY: comparison
comparison:
	for i in `cd $(host)-objdir.3 ; find . -name \*.o -print` ; do \
		cmp $(host)-objdir.2/$$i $(host)-objdir.3/$$i ; \
	done

.PHONY: clean
clean:
	rm -rf $(host)-$(TREE)-holes $(host)-objdir $(host)-objdir.[123] $(host)-stamp-stage[123]* $(host)-stamp-holes *~

.PHONY: very
very:
	rm -rf $(TREE)

$(TREE):
	$(TIME) cvs co $(CVS_TAG) $(CVS_MODULE)

.PHONY: do1
do1:	$(host)-$(TREE)-holes $(host)-stamp-stage1

$(host)-stamp-stage1: $(host)-objdir.1
	touch $(host)-stamp-stage1

$(host)-objdir.1: $(TREE) $(host)-stamp-stage1-configured
	(cd $(host)-objdir ; $(TIME) make $(MF) all info host=$(host))
	@echo CONFIGURE: make all completed for $(host)
	(cd $(host)-objdir ; $(TIME) make $(MF) install install-info host=$(host))
	@echo CONFIGURE: make install-all completed for $(host)
	mv $(host)-objdir $(host)-objdir.1

$(host)-stamp-stage1-configured:
	[ -d $(host)-objdir ] || mkdir $(host)-objdir
	(cd $(host)-objdir ; \
		$(TIME) ../$(TREE)/configure $(host) -v -srcdir=../$(TREE) \
			-prefix=$(ROOTING))
	touch $@

.PHONY: do2
do2:	$(host)-$(TREE)-holes $(host)-stamp-stage2

$(host)-stamp-stage2: $(host)-objdir.2
	PATH=$(release_root)/bin:`pwd`/$(host)-$(TREE)-holes ; \
		export PATH ; \
		echo $$PATH ; \
		SHELL=sh ; export SHELL ; \
		$(TIME) make $(host)-objdir.2 host=$(host)
	touch $(host)-stamp-stage2

$(host)-objdir.2: $(TREE) $(host)-stamp-stage2-configured
	[ -d $(host)-objdir ] || mkdir $(host)-objdir
	(cd $(host)-objdir ; $(TIME) make $(MF) all info  "CC=$(GNUC)" host=$(host))
	(cd $(host)-objdir ; $(TIME) make $(MF) $(PREFIXES) install install-info "CC=$(GNUC)" host=$(host))
	mv $(host)-objdir $(host)-objdir.2

$(host)-stamp-stage2-configured:
	[ -d $(host)-objdir ] || mkdir $(host)-objdir
	(cd $(host)-objdir ; \
		$(TIME) ../$(TREE)/configure $(host) -v -srcdir=../$(TREE) \
			-prefix=$(ROOTING))
	touch $@

.PHONY: do3
do3:	$(host)-$(TREE)-holes $(host)-stamp-stage3

$(host)-stamp-stage3: $(host)-objdir.3
	PATH=$(release_root)/bin:`pwd`/$(host)-$(TREE)-holes ; \
		export PATH ; \
		SHELL=sh ; export SHELL ; \
		$(TIME) make $(host)-objdir.3 host=$(host)
	touch $(host)-stamp-stage3

$(host)-objdir.3: $(TREE) $(host)-stamp-stage3-configured
	[ -d $(host)-objdir ] || mkdir $(host)-objdir
	(cd $(host)-objdir ; $(TIME) make $(MF) all info "CC=$(GNUC)" host=$(host))
	(cd $(host)-objdir ; $(TIME) make $(MF) install-no-fixedincludes install-info "CC=$(GNUC)"  host=$(host))
	mv $(host)-objdir $(host)-objdir.3

$(host)-stamp-stage3-configured:
	[ -d $(host)-objdir ] || mkdir $(host)-objdir
	(cd $(host)-objdir ; \
		$(TIME) ../$(TREE)/configure $(host) -v -srcdir=../$(TREE) \
			-prefix=$(ROOTING))
	touch $@

HOLES := [ \
	ar \
	as \
	awk \
	basename \
	cat \
	cc \
	chmod \
	cmp \
	cp \
	date \
	echo \
	egrep \
	ex \
	expr \
	find \
	grep \
	hostname \
	install \
	ld \
	lex \
	ln \
	ls \
	mkdir \
	mv \
	pwd \
	ranlib \
	rm \
	rmdir \
	sed \
	sh \
	sort \
	time \
	touch \
	tr \
	true \
	whoami

PARTIAL_HOLES := \
	gcc \
	make


$(host)-$(TREE)-holes: $(host)-stamp-holes

$(host)-stamp-holes:
	-rm -rf $(host)-$(TREE)-holes
	-mkdir $(host)-$(TREE)-holes
	for i in $(HOLES) ; do \
		if [ -x $(host)-$(TREE)-holes/$$i ] ; then \
			echo "$$i already present" ; \
                else if [ -x /bin/$$i ] ; then \
			cp /bin/$$i $(host)-$(TREE)-holes ; \
                        echo $$i from /bin ; \
		else if [ -x /usr/bin/$$i ] ; then \
			cp /usr/bin/$$i $(host)-$(TREE)-holes ; \
                        echo $$i from /usr/bin ; \
		else if [ -x /usr/ccs/bin/$$i ] ; then \
			cp /usr/ccs/bin/$$i $(host)-$(TREE)-holes ; \
                        echo $$i from /usr/ccs/bin ; \
		else if [ -x /usr/unsupported/bin/$$i ] ; then \
			cp /usr/unsupported/bin/$$i $(host)-$(TREE)-holes ; \
                        echo $$i from /usr/unsupported/bin ; \
		else \
			echo $$i is NOT found ; \
		fi; fi ; fi ; fi ; fi ; true ; \
	done
	for i in $(PARTIAL_HOLES) ; do \
	        if [ -x /usr/latest/bin/$$i ] ; then \
	              cp /usr/latest/bin/$$i $(host)-$(TREE)-holes ; \
	                echo $$i from /usr/latest/bin ; \
	         else if [ -x /usr/unsupported/bin/$$i ] ; then \
	              cp /usr/unsupported/bin/$$i $(host)-$(TREE)-holes ; \
	                echo $$i from /usr/unsupported/bin ; \
	        fi; fi; \
	done
	touch $(host)-stamp-holes

force:

endif # host

### Local Variables:
### fill-column: 131
### End: