#*=====================================================================*/
#*    serrano/prgm/project/bigloo/cigloo/Makefile                      */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Wed Jan 14 14:46:31 1998                          */
#*    Last change :  Fri Mar 27 21:14:49 1998 (serrano)                */
#*    -------------------------------------------------------------    */
#*    The Makefile to build Cigloo. This makefile has to be processed  */
#*    either by Gnu make or Sun make.                                  */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    Compilers, Tools and Destinations                                */
#*---------------------------------------------------------------------*/
# which C compiler to be used
CC              = gcc               
# the directory to write executable
BIN             = ../bin
# the directory where to install cigloo
INSTALLBIN	= 
# the executable used to bootstrap
BIGLOO          = $(BIN)/bigloo     
# the .afile
AFILE           = .afile
# to warn when done
BEEP            = echo -n ""
# the shell to be used
SHELL           = /bin/sh
# the produced executable
CIGLOO_DEST	= $(BIN)/cigloo
# the Cigloo version
CIGLOOVERSION	= `$(CIGLOO_DEST) -revision`
# how to clean
CLEAN		= clean

#*---------------------------------------------------------------------*/
#*    Afile and Depend tools                                           */
#*---------------------------------------------------------------------*/
EXAMPLES	= $(BIN)/../examples
AFILE_DIR	= $(EXAMPLES)/Afile
AFILE_SCM	= $(AFILE_DIR)/afile.scm
AFILE_EXE	= $(BIN)/afile
DEPEND_DIR	= $(EXAMPLES)/Depend
DEPEND_SCM	= $(DEPEND_DIR)/depend.scm
DEPEND_EXE	= $(BIN)/depend

#*---------------------------------------------------------------------*/
#*    Backups and revisions                                            */
#*---------------------------------------------------------------------*/
TAR		= tar
TARCOPT         = cf
TARXOPT         = xfp
GZIP		= gzip
BACKUPDIR	= $$HOUSE/backup
BACKUPNAME	= cigloo$(CIGLOOVERSION)

#*---------------------------------------------------------------------*/
#*    Compilation flags                                                */
#*---------------------------------------------------------------------*/
BDBGFLAGS	= -afile $(AFILE) -g2 -cg -O +rm -fsharing
BOPTFLAGS	= -afile $(AFILE) -O2 -rm -fsharing -unsafe
BFLAGS		= $(BDBGFLAGS)
CFLAGS          = 

#*---------------------------------------------------------------------*/
#*    Les sources                                                      */
#*---------------------------------------------------------------------*/
#*--- init ------------------------------------------------------------*/
INIT_FILE	= main setrc parse-args

INIT_OBJ	= $(INIT_FILE:%=Init/%.o)

#*--- engine ----------------------------------------------------------*/
ENGINE_FILE	= engine param translate

ENGINE_OBJ	= $(ENGINE_FILE:%=Engine/%.o)

#*--- translate -------------------------------------------------------*/
TRANSLATE_FILE	= function declaration type decl tspec expr eval

TRANSLATE_OBJ	= $(TRANSLATE_FILE:%=Translate/%.o)


#*--- tools -----------------------------------------------------------*/
TOOLS_FILE	= date speek error file

TOOLS_OBJ	= $(TOOLS_FILE:%=Tools/%.o)

#*--- write -----------------------------------------------------------*/
WRITE_FILE	= version

WRITE_OBJ	= $(WRITE_FILE:%=Write/%.o)

#*--- parser ---------------------------------------------------------*/
PARSER_FILE	= parser lexer tools cpp

PARSER_OBJ	= $(PARSER_FILE:%=Parser/%.o)

#*---------------------------------------------------------------------*/
#*    Tout le code ...                                                 */
#*---------------------------------------------------------------------*/
O_OBJECTS       = $(INIT_OBJ) $(ENGINE_OBJ) $(PARSE_OBJ) $(TOOLS_OBJ)   \
                  $(WRITE_OBJ) $(PARSER_OBJ) $(TRANSLATE_OBJ)

SOURCE_FILES	= $(O_OBJECTS:%.o=%.scm)

POPULATION	= $(SOURCE_FILES)           \
                  Makefile                  \
                  Init/args.sch             \
                  Parser/coord.sch          \
                  Translate/ast.sch         \
                  Translate/type.sch        \
                  Translate/function.sch    \
                  Tools/union.sch           \
                  Example/Makefile          \
	 	  Example/b-file.scm        \
	          Example/c-file.h          \
                  .afile

#*---------------------------------------------------------------------*/
#*     Les suffixes ...                                                */
#*---------------------------------------------------------------------*/
.SUFFIXES:
.SUFFIXES: .scm .c .o

#*---------------------------------------------------------------------*/
#*    L'executable `cigloo'                                            */
#*---------------------------------------------------------------------*/
cigloo: Tools/date.scm
	@ $(MAKE) $(CIGLOO_DEST)

#*---------------------------------------------------------------------*/
#*    install                                                          */
#*---------------------------------------------------------------------*/
install: $(CIGLOO_DEST)
	@ if [ "$(INSTALLBIN) " = " " ]; then                           \
             echo "*** ERROR: install cigloo";                          \
             echo "syntax: make install INSTALLBIN=your-bin-directory"; \
             exit 1;                                                    \
          fi
	@ echo "installing cigloo into $(INSTALLBIN) directory"
	@- \rm -f $(INSTALLBIN)/cigloo
	@- \rm -f $(INSTALLBIN)/cigloo$(CIGLOOVERSION)
	@- (ver=$(CIGLOOVERSION);                                       \
            cp $(CIGLOO_DEST) $(INSTALLBIN)/cigloo$$ver;                \
            cd $(INSTALLBIN); ln -s cigloo$$ver cigloo)

#*---------------------------------------------------------------------*/
#*    $(CIGLOO_DEST)                                                   */
#*---------------------------------------------------------------------*/
$(CIGLOO_DEST): $(O_OBJECTS)
	@ echo "Linking:"
	@ $(BIGLOO) $(BFLAGS) $(O_OBJECTS) -o $(CIGLOO_DEST)
	@ strip $(CIGLOO_DEST) >& /dev/null
	@ echo "Cigloo Done..."
	@ echo "-------------------------------"

#*---------------------------------------------------------------------*/
#*    afile                                                            */
#*---------------------------------------------------------------------*/
afile: $(AFILE_EXE) Tools/date.scm
	@ echo "   - .afile"
	@ $(AFILE_EXE) -o $(AFILE) $(SOURCE_FILES)
	@ echo "   - dep"
	@ $(MAKE) dep
	@ echo "   - .etags"
	@ $(MAKE) tags

#*---------------------------------------------------------------------*/
#*    tags                                                             */
#*---------------------------------------------------------------------*/
tags:
	@ etags -o .etags $(SOURCE_FILES) 

#*---------------------------------------------------------------------*/
#*    dep                                                              */
#*---------------------------------------------------------------------*/
dep: $(DEPEND_EXE)
	@ - head -`grep -n \^#depend Makefile | awk -F: '{ print $$1}' -` Makefile > /tmp/Makefile.aux
	@ $(DEPEND_EXE) $(SOURCE_FILES) >> /tmp/Makefile.aux
	@ mv /tmp/Makefile.aux Makefile

#*---------------------------------------------------------------------*/
#*     clean ...                                                       */
#*---------------------------------------------------------------------*/
.PHONY: Clean clean fast-clean

fast-clean:
	@- rm -f $(C_OBJECTS)
	@- rm -f $(O_OBJECTS)
	@ find . \( -name '*[~%]'                   \
                       -o -name '.??*[~%]'          \
                       -o -name '#*#'               \
                       -o -name '?*#'               \
                       -o -name \*core \)           \
                     -type f -exec rm {} \;   
	@ (cd Example; $(MAKE) clean)
	@ $(BEEP)
	@ echo "cleanup done..."
	@ echo "-------------------------------"

clean: fast-clean

Clean: clean
	@- rm -f $(CIGLOO_DEST)

Dclean: clean
DClean: Clean

#*---------------------------------------------------------------------*/
#*     touchall ...                                                    */
#*---------------------------------------------------------------------*/
touchall:
	@ touch $(SOURCE_FILES)
	@ echo "touchall done..."
	@ echo "-------------------------------"

#*---------------------------------------------------------------------*/
#*    link ...                                                         */
#*---------------------------------------------------------------------*/
link:
	@ echo "Linking:"
	@ $(BIGLOO) $(BFLAGS) -o $(CIGLOO_DEST) Init/main.o $(O_OBJECTS) -lm
	@ echo "link done..."
	@ echo "-------------------------------"

#*---------------------------------------------------------------------*/
#*    distrib                                                          */
#*    -------------------------------------------------------------    */
#*    Prepare Cigloo for a distribution                                */
#*---------------------------------------------------------------------*/
distrib:
	@ if [ `pwd` = $$HOME/prgm/project/bigloo/cigloo ]; then     \
             echo "*** ERROR:Illegal dir to make a distrib `pwd`";   \
             exit 1;                                                 \
          fi
	@ $(MAKE) $(CLEAN)
	@ $(MAKE) afile
	@ $(BIN)/copyright $(POPULATION)

#*---------------------------------------------------------------------*/
#*    backup                                                           */
#*---------------------------------------------------------------------*/
backup:
	@ echo "Backuping in: $(BACKUPDIR)/$(BACKUPNAME)"
	@ (\rm -f $(BACKUPDIR)/$(BACKUPNAME).tar.gz;                   \
	   cleanup;                                                    \
           tar cf $(BACKUPDIR)/$(BACKUPNAME).tar $(POPULATION);        \
           gzip $(BACKUPDIR)/$(BACKUPNAME).tar)
	@ echo "done..."
	@ echo "-------------------------------"

#*---------------------------------------------------------------------*/
#*    restore ...                                                      */
#*---------------------------------------------------------------------*/
restore:
	@ echo "Restoring from: $(BACKUPDIR)/$(BACKUPNAME)"
	@ (gzip -d --to-stdout $(BACKUPDIR)/$(BACKUPNAME).tar.gz | tar xfp -)
	@ echo "done..."
	@ echo "-------------------------------"

#*---------------------------------------------------------------------*/
#*    pop                                                              */
#*    -------------------------------------------------------------    */
#*    This entry is used by the bigloo/Makefile (the main Bigloo       */
#*    makefile) to get the list of file that populate a revision.      */
#*---------------------------------------------------------------------*/
.PHONY: pop
pop:
	@ echo $(POPULATION:%=$(BACKUPNAME)/%)

#*---------------------------------------------------------------------*/
#*    date.scm                                                         */
#*---------------------------------------------------------------------*/
Tools/date.scm:
	@ echo "Creating Tools/date.scm"
	@ $(BIGLOODIR)/private/make-date-file cigloo Tools/date.scm tools_date

#*---------------------------------------------------------------------*/
#*     .scm --> .o                                                     */
#*---------------------------------------------------------------------*/
.scm.o:
	@ $(BIGLOO) $(BFLAGS) -c $*.scm -o $*.o

#*---------------------------------------------------------------------*/
#*     .scm --> .c                                                     */
#*---------------------------------------------------------------------*/
.scm.c:
	@ $(BIGLOO) $(BFLAGS) -C $*.scm -o $*.o

#*---------------------------------------------------------------------*/
#*    AFILE_EXE                                                        */
#*    -------------------------------------------------------------    */
#*    The entry that build the afile executable                        */
#*---------------------------------------------------------------------*/
$(AFILE_EXE): $(AFILE_SCM)
	(cd $(AFILE_DIR);                \
         $(MAKE) BIGLOO=../$(BIGLOO)     \
                 BFLAGS="-O2 -unsafe"    \
                 DEST=../$(AFILE_EXE))

#*---------------------------------------------------------------------*/
#*    depend                                                           */
#*    -------------------------------------------------------------    */
#*    The entry that build the depend tool                             */
#*---------------------------------------------------------------------*/
$(DEPEND_EXE): $(DEPEND_SCM)
	(cd $(DEPEND_DIR);               \
         $(MAKE) BIGLOO=../$(BIGLOO)     \
                 BFLAGS="-O2 -unsafe"    \
                 DEST=../$(DEPEND_EXE))

#*---------------------------------------------------------------------*/
#*    Parser/parser.o:                                                 */
#*---------------------------------------------------------------------*/
Parser/parser.o: Parser/parser.scm 
	@ $(BIGLOO) $(BOPTFLAGS) $(BFLAGS) -c $*.scm -o $*.o

#*---------------------------------------------------------------------*/
#*    Parser/cpp.o:                                                    */
#*---------------------------------------------------------------------*/
Parser/cpp.o: Parser/cpp.scm 
	@ $(BIGLOO) $(BOPTFLAGS) $(BFLAGS) -c $*.scm -o $*.o

#*---------------------------------------------------------------------*/
#*    Parser/lexer.o:                                                  */
#*---------------------------------------------------------------------*/
Parser/lexer.o: Parser/lexer.scm 
	@ $(BIGLOO) $(BOPTFLAGS) $(BFLAGS) -c $*.scm -o $*.o

#*---------------------------------------------------------------------*/
#*    Les dependances                                                  */
#*---------------------------------------------------------------------*/
#depend
Translate/expr.o: Translate/ast.sch Translate/type.sch Tools/union.sch
Translate/tspec.o: Translate/ast.sch Translate/type.sch Tools/union.sch
Translate/decl.o: Parser/coord.sch Translate/ast.sch Translate/type.sch Tools/union.sch
Translate/type.o: Translate/ast.sch Translate/type.sch Tools/union.sch
Translate/declaration.o: Translate/ast.sch Translate/type.sch Tools/union.sch
Translate/function.o: Parser/coord.sch Translate/ast.sch Translate/type.sch Translate/function.sch Tools/union.sch
Parser/cpp.o: Parser/coord.sch
Parser/tools.o: Parser/coord.sch
Parser/parser.o: Translate/ast.sch Tools/union.sch
Tools/error.o: Translate/ast.sch Parser/coord.sch Tools/union.sch
Engine/translate.o: Parser/coord.sch Translate/ast.sch Tools/union.sch
Init/parse-args.o: Init/args.sch

