
FLAGS = -funsigned-char -ggdb -Wall -I.. -I.
CFLAGS = $(FLAGS) -c 
ACTIVEBIN = './ill'
CL2CPP = '../cl2cpp/cl2cpp'
CL2CPPDIR = '../cl2cpp'

default:
	@echo "No default rule here. Try one of the following:"
	@echo "  build_with_cl2cpp build_from_arch build_from_src"
	@echo "  bootstrap renew clean"
	@echo "If you only need to get a working binary, the most"
	@echo "appropriate target for you is 'bootstrap'"


#########################################################################


###############################
# Build our very own library binary version
#

bin_lib/lisp_all.o:	FORCE
	cd ../lisp && $(MAKE) all TARGETDIR=../ill/bin_lib

###############################
# Build with ../cl2cpp  -- deprecated; 
# 	on success, updates the active binary and creates an archive copy


ill_cl2c.cpl:	ill.lsp
	[ -x $(CL2CPP) ] || ( cd $(CL2CPPDIR) && $(MAKE) bootstrap )
	$(CL2CPP) ill.lsp $@ TheLispProgram	

ill_cl2c:	illmain.cpp ill_cl2c.cpl bin_lib/lisp_all.o
	$(CXX) $(FLAGS) -DCPLFILE=\"ill_cl2c.cpl\" bin_lib/lisp_all.o \
		illmain.cpp -o $@

build_with_cl2cpp_comment:
	@echo "----------------------------------------------------"
	@echo "    Building an ILL binary with CL2CPP translator   "
	@echo "----------------------------------------------------"

build_with_cl2cpp: build_with_cl2cpp_comment ill_cl2c
	cp ill_cl2c $(ACTIVEBIN)
	#strip $(ACTIVEBIN) 
	$(MAKE) fresh_archive_copy

fresh_archive_copy:	illdef._ls
	$(ACTIVEBIN) ill.lsp \
		'(CXX-FILE-NAME "arch/ill.cxx")' \
		'(HXX-FILE-NAME "arch/ill.hxx")'
	cp illmain.cpp arch/


###############################
# Build from the archive copy; on success, updates the active binary

      # note there are no rules to build arch/* files. It's not 
      # a bug, it's a feature. I consider it no good if our 
      # known-as-working archive copy can get replaced with 
      # changed (and possible not-working) files without our
      # explicit will. See fresh_archive_copy target.
arch/ill.o:	arch/ill.cxx arch/ill.hxx 
	$(CXX) $(CFLAGS) arch/ill.cxx -o $@ 


arch/ill:	arch/ill.o arch/illmain.cpp bin_lib/lisp_all.o
	$(CXX) $(FLAGS) arch/ill.o bin_lib/lisp_all.o arch/illmain.cpp \
		-D'HXXFILE="arch/ill.hxx"' -o $@

build_from_arch_comment:
	@echo "----------------------------------------------------"
	@echo "   Building an ILL binary from the archived copies  "
	@echo "----------------------------------------------------"

build_from_arch: build_from_arch_comment arch/ill
	cp arch/ill $(ACTIVEBIN)
	#strip $(ACTIVEBIN)



#########################
# Development build (e.g., build a new version).
#      Does NOT (automatically) update the active binary, use renew to do so.

ill.cxx ill.hxx:	ill.lsp illdef._ls
	$(ACTIVEBIN) ill.lsp	

ill.o:	ill.cxx ill.hxx 
	$(CXX) $(CFLAGS) ill.cxx -o $@

ill_new:	ill.o illmain.cpp bin_lib/lisp_all.o
	$(CXX) $(FLAGS) ill.o bin_lib/lisp_all.o illmain.cpp -o $@

build_from_src: ill_new

renew:	ill_new ill.cxx ill.hxx
	cp ill.cxx old_ill.cxx
	cp ill.hxx old_ill.hxx
	@echo "----------------------------------------------------"
	@echo "       Testing the new translator's binary          "
	@echo "----------------------------------------------------"
	./ill_new ill.lsp 
	diff old_ill.cxx ill.cxx 
	diff old_ill.hxx ill.hxx
	@echo "****************************************************"
	@echo "  Everything seems O.k. Updating the archive copy.  "
	@echo "****************************************************"
	cp ill.cxx ill.hxx illmain.cpp arch/	


#########################
# Bootstrap 

bootstrap:
	[ -f arch/ill.cxx -a -f arch/ill.hxx ] || $(MAKE) build_with_cl2cpp
	$(MAKE) build_from_arch
	@echo "----------------------------------------------------"
	@echo " Now rebuilding it with the newly-built translator binary"
	@echo "----------------------------------------------------"
	$(MAKE) renew
#	@echo "----------------------------------------------------"
#	@echo "                 Cleaning up..."
#	@echo "----------------------------------------------------"
#	$(MAKE) clean

###############################
# Common targets

trtgen:		trtgen.c
	$(CXX) $(FLAGS) trtgen.c -o trtgen

chartran._ls:	trtgen
	./trtgen > $@


../lisp/lib.lsp:
	cd ../lisp && $(MAKE) lib.lsp

lib._ls:	../lisp/lib.lsp
	cp ../lisp/lib.lsp $@

illdef._ls:	chartran._ls lib._ls illbdef.lsp
	echo "; InteLib Lisp default definitions, generated with" > $@
	echo ";       make illdef._ls" >> $@
	echo "(%%% " >> $@
	cat chartran._ls illbdef.lsp lib._ls >> $@
	echo ") ; end of definitions " >> $@

#../lisp/lisp_all.o: FORCE
#	cd ../lisp && $(MAKE) lisp_all.o

clean:	FORCE	
	rm -f trtgen *.[ch]xx *.cpl *.o ill_cl2c ill_new
	rm -f arch/ill arch/ill.o bin_lib/*.o

FORCE:

#########################################################################
	
	
	
