#*=====================================================================*/
#*    serrano/prgm/project/bigloo/contrib/format/Makefile              */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Wed Apr  1 18:45:46 1998                          */
#*    Last change :  Fri Jun 19 13:59:02 1998 (serrano)                */
#*    -------------------------------------------------------------    */
#*    The makefile to build the format library.                        */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    Compilers, Tools and Destinations                                */
#*---------------------------------------------------------------------*/
# which C compiler to be used
CC              = gcc
# the Bigloo compiler
BIGLOO          = bigloo
# the library name
LIBRARYNAME	= format
# where to store Bigloo libraries and Bigloo heap file
LIB         	= ../../lib
# The heap file
HEAP_FILE	= format.heap
# how to produce a beep
BEEP            = echo -n ""
# the shell to be used
SHELL           = /bin/sh
# the format include files
INCLUDE_FILE	= format.h
# contrib name
CONTRIB_NAME	= format

#*---------------------------------------------------------------------*/
#*    Unix library options                                             */
#*---------------------------------------------------------------------*/
AR		= ar
ARFLAGS		= qcv
RANLIB		= ranlib
LD		= ld -G

#*---------------------------------------------------------------------*/
#*    The tar options                                                  */
#*---------------------------------------------------------------------*/
TAR		= tar
TCOPT		= cvfz

#*---------------------------------------------------------------------*/
#*    Compilation flags                                                */
#*---------------------------------------------------------------------*/
# verbose compilations ?
VERBOSE		= 
# The C include files
INCLUDE     	= -I$(LIB) -I$(GC_DIR)
# The Bigloo compilation option used the produce an heap file
BHEAPFLAGS	= -unsafe -q -mkaddheap -mkaddlib $(VERBOSE)
# The option used to produce the _safe_ (and debugged) library
BSAFEFLAGS	= -cg -O3 -mkaddlib -g -cg -cc $(CC) -fsharing  \
                  -q -rm $(VERBOSE) -unsafev -eval '(set! *indent* 4)'
CSAFEFLAGS	= $(INCLUDE) -g -DBIGLOO_DEBUG
# The option used to produce the _unsafe_ (and optimized) library
BUNSAFEFLAGS	= -O4 -mkaddlib -unsafe -cc $(CC) -fsharing -q  \
                  -rm $(VERBOSE)
CUNSAFEFLAGS	= $(INCLUDE) -O2
# The option used to produce the _unsafe_ (and profiled) library
BUNSAFEPFLAGS	= -O3 -mkaddlib -unsafe -pg -cc $(CC) -fsharing \
                  -q $(VERBOSE)
CPFLAGS		= $(INCLUDE) -pg

#*---------------------------------------------------------------------*/
#*    Suffixes                                                         */
#*---------------------------------------------------------------------*/
.SUFFIXES:
.SUFFIXES: .scm .o

#*---------------------------------------------------------------------*/
#*    Object files                                                     */
#*---------------------------------------------------------------------*/
U_OBJECT_FILES	= .Olib_u/format.o
OBJECT_FILES	= .Olib/format.o
P_OBJECT_FILES	= .Olib_p/format.o

#*---------------------------------------------------------------------*/
#*    Scheme source files                                              */
#*---------------------------------------------------------------------*/
SCM_SOURCE_FILES= format.scm

#*---------------------------------------------------------------------*/
#*    Sources                                                          */
#*---------------------------------------------------------------------*/
SOURCE_FILES	= $(SCM_SOURCE_FILES)                \
		  $(C_SOURCE_FILES)                  \
		  make-lib.scm                       \
		  $(INCLUDE_FILE)

POPULATION	= $(SOURCE_FILES) Makefile README    \
                  test.scm test2.scm

#*---------------------------------------------------------------------*/
#*    The heap construction                                            */
#*---------------------------------------------------------------------*/
.PHONY: heap
heap: 
	@ \rm -f $(LIB)/$(HEAP_FILE)
	@ $(BIGLOO) $(BHEAPFLAGS) make-lib.scm -addheap $(LIB)/$(HEAP_FILE)
	@ echo "Heap Done..."
	@ $(BEEP)
	@ 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:%=format/%)

#*---------------------------------------------------------------------*/
#*    The implicit `.scm.o' rule                                       */
#*---------------------------------------------------------------------*/
.Olib/%.o: %.scm
	@ $(BIGLOO) $(BFLAGS) $*.scm -o .Olib/$*.o -c
.Olib_u/%.o: %.scm
	@ $(BIGLOO) $(BFLAGS) $*.scm -o .Olib_u/$*.o -c
.Olib_p/%.o: %.scm
	@ $(BIGLOO) $(BFLAGS) $*.scm -o .Olib_p/$*.o -c

#*---------------------------------------------------------------------*/
#*    The include                                                      */
#*---------------------------------------------------------------------*/
.PHONY: include
include: $(LIB)/$(INCLUDE_FILE)

$(LIB)/$(INCLUDE_FILE): $(INCLUDE_FILE)
	cp $(INCLUDE_FILE) $(LIB)/$(INCLUDE_FILE)

#*---------------------------------------------------------------------*/
#*    The `safe' version.                                              */
#*---------------------------------------------------------------------*/
.PHONY: lib
lib: lib_u lib.so lib.a include

lib.a:
	@ $(MAKE) blib.a                         \
             BFLAGS="$(BSAFEFLAGS)"              \
	     CFLAGS="$(CSAFEFLAGS)"              \
             OBJECTS="$(OBJECT_FILES)"           \
             D="libformat"

lib.so:
	@ $(MAKE) blib.so                        \
             BFLAGS="$(BSAFEFLAGS)"              \
	     CFLAGS="$(CSAFEFLAGS)"              \
             OBJECTS="$(OBJECT_FILES)"           \
             D="libformat"

#*---------------------------------------------------------------------*/
#*    The `unsafe' version                                             */
#*---------------------------------------------------------------------*/
.PHONY: lib_u
lib_u: mkdir                                     \
       lib_u.so lib_u.a 

lib_u.a:
	@ $(MAKE) blib.a                         \
             BFLAGS="$(BUNSAFEFLAGS)"            \
             CFLAGS="$(CUNSAFEFLAGS)"            \
             OBJECTS="$(U_OBJECT_FILES)"         \
	     D="libformat_u"

lib_u.so:
	@ $(MAKE) blib.so                        \
             BFLAGS="$(BUNSAFEFLAGS)"            \
             CFLAGS="$(CUNSAFEFLAGS)"            \
             OBJECTS="$(U_OBJECT_FILES)"         \
	     D="libformat_u"

#*---------------------------------------------------------------------*/
#*    The `profile' version                                            */
#*---------------------------------------------------------------------*/
lib_p: lib_p.so lib_p.a

lib_p.a:
	@ $(MAKE) blib.a                         \
             BFLAGS="$(BUNSAFEPFLAGS)"           \
             CFLAGS="$(CPFLAGS)"                 \
             OBJECTS="$(P_OBJECT_FILES)"         \
	     D="libformat_p"
	@ \rm -f gmon.out

lib_p.so:
	@ $(MAKE) blib.so                        \
             BFLAGS="$(BUNSAFEPFLAGS)"           \
             CFLAGS="$(CPFLAGS)"                 \
             OBJECTS="$(P_OBJECT_FILES)"         \
	     D="libformat_p"

#*---------------------------------------------------------------------*/
#*    blib.a                                                           */
#*---------------------------------------------------------------------*/
.PHONY: blib.a
blib.a: $(LIB)/$(D).a

$(LIB)/$(D).a: $(OBJECT_FILES)
	@ \rm -f $(LIB)/$(D).a.old
	@ if [ -f $(LIB)/$(D).a ]; then                \
               mv $(LIB)/$(D).a $(LIB)/$(D).a.old;     \
          fi
	@ $(AR) $(ARFLAGS) $(LIB)/$(D).a $(OBJECT_FILES)
	@ $(RANLIB) $(LIB)/$(D).a
	@ echo "$(D).a Done..."
	@ $(BEEP)
	@ echo "-------------------------------"

#*---------------------------------------------------------------------*/
#*    blib.so                                                          */
#*---------------------------------------------------------------------*/
.PHONY: blib.so
blib.so: $(LIB)/$(D).so

$(LIB)/$(D).so: $(OBJECT_FILES)
	@ $(LD) -o $(LIB)/$(D).so $(OBJECT_FILES)     \
                   -L$(LIB) -lm -lc
	@ echo "$(D).so Done..."
	@ $(BEEP)
	@ echo "-------------------------------"

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

Dclean:
	@ $(MAKE) dir-clean O=.Olib
	@ $(MAKE) dir-clean O=.Olib_u
	@ $(MAKE) dir-clean O=.Olib_p
	@- /bin/rm -f *.o
	@- /bin/rm -f *.c
	@- /bin/rm -f a.out
	@- /bin/rm -f test-format
	@- /bin/rm -f format.heap
	@ find . \( -name '*[~%]'                   \
                       -o -name '.??*[~%]'          \
                       -o -name '#*#'               \
                       -o -name '*.ast'             \
                       -o -name '*.escm'            \
                       -o -name '?*#'               \
                       -o -name \*core \)           \
                     -type f -exec rm {} \;   
	@ echo "Cleanup done..."
	@ $(BEEP)
	@ echo "-------------------------------"

clean: Dclean

dir-clean:
	@- rm -f $(OBJECT_FILES)

DClean:
	@- rm -f $(LIB)/$(HEAP_FILE)
	@- rm -f $(LIB)/$(INCLUDE_FILE)
	@- rm -f $(LIB)/libformat*
	@- rm -f -r .Olib
	@- rm -f -r .Olib_u
	@- rm -f -r .Olib_p

Clean: DClean clean

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

#*---------------------------------------------------------------------*/
#*    tar.gz:                                                          */
#*---------------------------------------------------------------------*/
tar.gz: Clean
	@ (cd ..; $(TAR) $(TCOPT) $(CONTRIB_NAME).tar.gz $(CONTRIB_NAME))

#*---------------------------------------------------------------------*/
#*    dirs                                                             */
#*    -------------------------------------------------------------    */
#*    Prepare directories for compilation.                             */
#*---------------------------------------------------------------------*/
.PHONY: mkdir
mkdir: .Olib_u .Olib .Olib_p

.Olib_u:
	mkdir .Olib_u
.Olib:
	mkdir .Olib
.Olib_p:
	mkdir .Olib_p

#*---------------------------------------------------------------------*/
#*    test:                                                            */
#*---------------------------------------------------------------------*/
test: test.o test2.o
	@ $(BIGLOO) test.o test2.o -o test-format -unsafe -v2

# The first source file contains a library module clause. So, it is 
# not necessary to compile with a -library option.
test.o: test.scm
	@ $(BIGLOO) test.scm -c -unsafe

# The second test file does not contains library clause. In consequence
# we have to compile it with a -library option.
test2.o: test2.scm
	@ $(BIGLOO) test2.scm -c -unsafe -library format
