# Primary targets:
# gc.a - builds basic library
# libgc.a - builds library for use with g++ "-fgc-keyword" extension
# c++ - adds C++ interface to library
# cords - adds cords (heavyweight strings) to library
# test - prints porting information, then builds basic version of gc.a,
#      	 and runs some tests of collector and cords.  Does not add cords or
#	 c++ interface to gc.a
# cord/de$(EXE_SUFFIX) - builds dumb editor based on cords.
CC=gcc
CXX=gcc -x c++
CXXLD=gxx
RM=rm -f
MV=mv
EXE_SUFFIX=.exe
RANLIB=ranlib

CFLAGS= -O -DNO_SIGNALS -DALL_INTERIOR_POINTERS -DSILENT -DATOMIC_UNCOLLECTABLE

# Setjmp_test may yield overly optimistic results when compiled
# without optimization.
# -DSILENT disables statistics printing, and improves performance.
# -DCHECKSUMS reports on erroneously clear dirty bits, and unexpectedly
#   altered stubborn objects, at substantial performance cost.
#   Use only for incremental collector debugging.
# -DFIND_LEAK causes the collector to assume that all inaccessible
#   objects should have been explicitly deallocated, and reports exceptions.
#   Finalization and the test program are not usable in this mode.
# -DSOLARIS_THREADS enables support for Solaris (thr_) threads.
#   (Clients should also define SOLARIS_THREADS and then include
#   gc.h before performing thr_ or dl* or GC_ operations.)
#   This is broken on nonSPARC machines.
# -DIRIX_THREADS enables support for Irix pthreads.  See README.irix.
# -DALL_INTERIOR_POINTERS allows all pointers to the interior
#   of objects to be recognized.  (See gc_priv.h for consequences.)
# -DSMALL_CONFIG tries to tune the collector for small heap sizes,
#   usually causing it to use less space in such situations.
#   Incremental collection no longer works in this case.
# -DLARGE_CONFIG tunes the collector for unusually large heaps.
#   Necessary for heaps larger than about 500 MB on most machines.
#   Recommended for heaps larger than about 64 MB.
# -DDONT_ADD_BYTE_AT_END is meaningful only with
#   -DALL_INTERIOR_POINTERS.  Normally -DALL_INTERIOR_POINTERS
#   causes all objects to be padded so that pointers just past the end of
#   an object can be recognized.  This can be expensive.  (The padding
#   is normally more than one byte due to alignment constraints.)
#   -DDONT_ADD_BYTE_AT_END disables the padding.
# -DNO_SIGNALS does not disable signals during critical parts of
#   the GC process.  This is no less correct than many malloc 
#   implementations, and it sometimes has a significant performance
#   impact.  However, it is dangerous for many not-quite-ANSI C
#   programs that call things like printf in asynchronous signal handlers.
# -DNO_EXECUTE_PERMISSION may cause some or all of the heap to not
#   have execute permission, i.e. it may be impossible to execute
#   code from the heap.  Currently this only affects the incremental
#   collector on UNIX machines.  It may greatly improve its performance,
#   since this may avoid some expensive cache synchronization.
# -DOPERATOR_NEW_ARRAY declares that the C++ compiler supports the
#   new syntax "operator new[]" for allocating and deleting arrays.
#   See gc_cpp.h for details.  No effect on the C part of the collector.
#   This is defined implicitly in a few environments.
# -DREDIRECT_MALLOC=X causes malloc, realloc, and free to be defined
#   as aliases for X, GC_realloc, and GC_free, respectively.
#   Calloc is redefined in terms of the new malloc.  X should
#   be either GC_malloc or GC_malloc_uncollectable.
#   The former is occasionally useful for working around leaks in code
#   you don't want to (or can't) look at.  It may not work for
#   existing code, but it often does.  Neither works on all platforms,
#   since some ports use malloc or calloc to obtain system memory.
#   (Probably works for UNIX, and win32.)
# -DIGNORE_FREE turns calls to free into a noop.  Only useful with
#   -DREDIRECT_MALLOC.
# -DNO_DEBUGGING removes GC_dump and the debugging routines it calls.
#   Reduces code size slightly at the expense of debuggability.
# -DJAVA_FINALIZATION makes it somewhat safer to finalize objects out of
#   order by specifying a nonstandard finalization mark procedure  (see
#   finalize.c).  Objects reachable from finalizable objects will be marked
#   in a sepearte postpass, and hence their memory won't be reclaimed.
#   Not recommended unless you are implementing a language that specifies
#   these semantics.
# -DATOMIC_UNCOLLECTABLE includes code for GC_malloc_atomic_uncollectable.
#   This is useful if either the vendor malloc implementation is poor,
#   or if REDIRECT_MALLOC is used.

LIBGC_CFLAGS= -O -DNO_SIGNALS -DSILENT \
    -DREDIRECT_MALLOC=GC_malloc_uncollectable \
    -DDONT_ADD_BYTE_AT_END -DALL_INTERIOR_POINTERS
#   Flags for building libgc.a -- the last two are required.

CXXFLAGS= $(CFLAGS) -DOPERATOR_NEW_ARRAY
AR= ar
RANLIB= ranlib


# Redefining srcdir allows object code for the nonPCR version of the collector
# to be generated in different directories.  In this case, the destination directory
# should contain a copy of the original include directory.
srcdir = .
VPATH = $(srcdir)

OBJS= alloc.o reclaim.o allchblk.o misc.o mach_dep.o os_dep.o mark_rts.o headers.o mark.o obj_map.o blacklst.o finalize.o new_hblk.o dbg_mlc.o malloc.o stubborn.o checksums.o solaris_threads.o irix_threads.o typd_mlc.o ptr_chck.o mallocx.o

CSRCS= reclaim.c allchblk.c misc.c alloc.c mach_dep.c os_dep.c mark_rts.c headers.c mark.c obj_map.c pcr_interface.c blacklst.c finalize.c new_hblk.c real_malloc.c dyn_load.c dbg_mlc.c malloc.c stubborn.c checksums.c solaris_threads.c irix_threads.c typd_mlc.c ptr_chck.c mallocx.c

CORD_SRCS=  cord/cordbscs.c cord/cordxtra.c cord/cordprnt.c cord/de.c cord/cordtest.c cord/cord.h cord/ec.h cord/private/cord_pos.h cord/de_win.c cord/de_win.h cord/de_cmds.h cord/de_win.ICO cord/de_win.RC cord/SCOPTIONS.amiga cord/SMakefile.amiga

CORD_OBJS=  cord/cordbscs.o cord/cordxtra.o cord/cordprnt.o

SRCS= $(CSRCS) mips_sgi_mach_dep.s rs6000_mach_dep.s alpha_mach_dep.s \
    sparc_mach_dep.s gc.h gc_typed.h gc_hdrs.h gc_priv.h gc_private.h \
    config.h gc_mark.h include/gc_inl.h include/gc_inline.h gc.man \
    threadlibs.c if_mach.c if_not_there.c gc_cpp.cc gc_cpp.h weakpointer.h \
    gcc_support.c mips_ultrix_mach_dep.s include/gc_alloc.h gc_alloc.h \
    $(CORD_SRCS)

OTHER_FILES= Makefile PCR-Makefile OS2_MAKEFILE NT_MAKEFILE BCC_MAKEFILE \
           README test.c test_cpp.cc setjmp_t.c SMakefile.amiga \
           SCoptions.amiga README.amiga README.win32 cord/README \
           cord/gc.h include/gc.h include/gc_typed.h include/cord.h \
           include/ec.h include/private/cord_pos.h include/private/config.h \
           include/private/gc_hdrs.h include/private/gc_priv.h \
	   include/gc_cpp.h README.rs6000 \
           include/weakpointer.h README.QUICK callprocs pc_excludes \
           barrett_diagram README.OS2 README.Mac MacProjects.sit.hqx \
           MacOS.c EMX_MAKEFILE makefile.depend README.debugging \
           include/gc_cpp.h Mac_files/datastart.c Mac_files/dataend.c \
           Mac_files/MacOS_config.h Mac_files/MacOS_Test_config.h \
           add_gc_prefix.c README.solaris2 README.sgi README.hp README.uts \
	   win32_threads.c NT_THREADS_MAKEFILE gc.mak README.dj Makefile.dj

CORD_INCLUDE_FILES= $(srcdir)/gc.h $(srcdir)/cord/cord.h $(srcdir)/cord/ec.h \
           $(srcdir)/cord/private/cord_pos.h

UTILS= if_mach$(EXE_SUFFIX) if_not_there$(EXE_SUFFIX) threadlibs$(EXE_SUFFIX)

# Libraries needed for curses applications.  Only needed for de.
CURSES= -lcurses -ltermlib

# The following is irrelevant on most systems.  But a few
# versions of make otherwise fork the shell specified in
# the SHELL environment variable.
SHELL= /bin/sh

SPECIALCFLAGS = 
# Alternative flags to the C compiler for mach_dep.c.
# Mach_dep.c often doesn't like optimization, and it's
# not time-critical anyway.
# Set SPECIALCFLAGS to -q nodirect_code on Encore.

all: gc.a gctest

pcr: PCR-Makefile gc_private.h gc_hdrs.h gc.h config.h mach_dep.o $(SRCS)
	make -f PCR-Makefile depend
	make -f PCR-Makefile

$(OBJS) test.o dyn_load.o dyn_load_sunos53.o: $(srcdir)/gc_priv.h $(srcdir)/gc_hdrs.h $(srcdir)/gc.h \
    $(srcdir)/config.h $(srcdir)/gc_typed.h Makefile
# The dependency on Makefile is needed.  Changing
# options such as -DSILENT affects the size of GC_arrays,
# invalidating all .o files that rely on gc_priv.h

mark.o typd_mlc.o finalize.o: $(srcdir)/gc_mark.h

base_lib gc.a: $(OBJS) dyn_load.o $(UTILS)
	echo > base_lib
	$(RM) on_sparc_sunos5
	./if_mach SPARC SUNOS5 touch on_sparc_sunos5
	./if_mach SPARC SUNOS5 $(AR) rus gc.a $(OBJS) dyn_load.o
	./if_not_there on_sparc_sunos5 $(AR) ru gc.a $(OBJS) dyn_load.o
	-./if_not_there on_sparc_sunos5 $(RANLIB) gc.a
#	ignore ranlib failure; that usually means it doesn't exist, and isn't needed

libgc.a: 
	make CFLAGS="$(LIBGC_CFLAGS)" clean gc.a gcc_support.o
	$(MV) gc.a libgc.a
	-$(RM) on_sparc_sunos5
	./if_mach SPARC SUNOS5 touch on_sparc_sunos5
	./if_mach SPARC SUNOS5 $(AR) rus libgc.a gcc_support.o
	./if_not_there on_sparc_sunos5 $(AR) ru libgc.a gcc_support.o
	-./if_not_there on_sparc_sunos5 $(RANLIB) libgc.a

cords: $(CORD_OBJS) cord/cordtest$(EXE_SUFFIX) $(UTILS)
	-$(RM) on_sparc_sunos5
	./if_mach SPARC SUNOS5 touch on_sparc_sunos5
	./if_mach SPARC SUNOS5 $(AR) rus gc.a $(CORD_OBJS)
	./if_not_there on_sparc_sunos5 $(AR) ru gc.a $(CORD_OBJS)
	-./if_not_there on_sparc_sunos5 $(RANLIB) gc.a

gc_cpp.o: $(srcdir)/gc_cpp.cc $(srcdir)/gc_cpp.h $(srcdir)/gc.h Makefile
	$(CXX) -c $(CXXFLAGS) $(srcdir)/gc_cpp.cc

test_cpp: $(srcdir)/test_cpp.cc $(srcdir)/gc_cpp.h gc_cpp.o $(srcdir)/gc.h \
base_lib $(UTILS)
	-$(RM) test_cpp test_cpp$(EXE_SUFFIX)
	./if_mach HP_PA "" $(CXX) $(CXXFLAGS) -o test_cpp $(srcdir)/test_cpp.cc gc_cpp.o gc.a -ldld
	./if_not_there test_cpp$(EXE_SUFFIX) $(CXXLD) $(CXXFLAGS) -o test_cpp $(srcdir)/test_cpp.cc gc_cpp.o gc.a

c++: gc_cpp.o $(srcdir)/gc_cpp.h test_cpp
	-$(RM) on_sparc_sunos5
	$(AR) ru gc.a gc_cpp.o
	$(RANLIB) gc.a
	./test_cpp 1
	echo > c++

dyn_load_sunos53.o: dyn_load.c
	$(CC) $(CFLAGS) -DSUNOS53_SHARED_LIB -c $(srcdir)/dyn_load.c -o $@

mach_dep.o: $(srcdir)/mach_dep.c
# $(srcdir)/mips_mach_dep.s $(srcdir)/rs6000_mach_dep.s if_mach if_not_there
	-$(RM) mach_dep.o
	$(CC) -c $(SPECIALCFLAGS) $(srcdir)/mach_dep.c

mark_rts.o: $(srcdir)/mark_rts.c
	-$(RM) mark_rts.o
	$(CC) -c $(CFLAGS) $(srcdir)/mark_rts.c

cord/cordbscs.o: $(srcdir)/cord/cordbscs.c $(CORD_INCLUDE_FILES)
	$(CC) $(CFLAGS) -c $(srcdir)/cord/cordbscs.c
	$(MV) cordbscs.o cord/cordbscs.o
#  not all compilers understand -o filename

cord/cordxtra.o: $(srcdir)/cord/cordxtra.c $(CORD_INCLUDE_FILES)
	$(CC) $(CFLAGS) -c $(srcdir)/cord/cordxtra.c
	$(MV) cordxtra.o cord/cordxtra.o

cord/cordprnt.o: $(srcdir)/cord/cordprnt.c $(CORD_INCLUDE_FILES)
	$(CC) $(CFLAGS) -c $(srcdir)/cord/cordprnt.c
	$(MV) cordprnt.o cord/cordprnt.o

cord/cordtest$(EXE_SUFFIX): $(srcdir)/cord/cordtest.c $(CORD_OBJS) gc.a $(UTILS)
	-$(RM) cord/cordtest$(EXE_SUFFIX)
	$(CC) $(CFLAGS) -o cordtest $(srcdir)/cord/cordtest.c\
		 $(CORD_OBJS) gc.a
	./if_not_there cord/cordtest$(EXE_SUFFIX) \
		$(MV) cordtest$(EXE_SUFFIX) cord/cordtest$(EXE_SUFFIX)

cord/de$(EXE_SUFFIX): $(srcdir)/cord/de.c cord/cordbscs.o cord/cordxtra.o gc.a
	-$(RM) cord/de$(EXE_SUFFIX)
	./if_mach SPARC DRSNX $(CC) $(CFLAGS) -o cord/de $(srcdir)/cord/de.c\
cord/cordbscs.o cord/cordxtra.o gc.a $(CURSES) -lucb
	./if_mach HP_PA "" $(CC) $(CFLAGS) -o cord/de $(srcdir)/cord/de.c\
cord/cordbscs.o cord/cordxtra.o gc.a $(CURSES) -ldld
	./if_mach RS6000 "" $(CC) $(CFLAGS) -o cord/de $(srcdir)/cord/de.c\
cord/cordbscs.o cord/cordxtra.o gc.a -lcurses
	./if_mach I386 LINUX $(CC) $(CFLAGS) -o cord/de $(srcdir)/cord/de.c\
cord/cordbscs.o cord/cordxtra.o gc.a -lcurses
	./if_not_there cord/de $(CC) $(CFLAGS) -o cord/de $(srcdir)/cord/de.c\
cord/cordbscs.o cord/cordxtra.o gc.a $(CURSES)
	./if_not_there cord/de$(EXE_SUFFIX) \
		$(MV) de$(EXE_SUFFIX) cord/de$(EXE_SUFFIX)

if_mach$(EXE_SUFFIX): $(srcdir)/if_mach.c $(srcdir)/config.h
	$(CC) $(CFLAGS) -o if_mach $(srcdir)/if_mach.c

threadlibs$(EXE_SUFFIX): $(srcdir)/threadlibs.c $(srcdir)/config.h Makefile
	$(CC) $(CFLAGS) -o threadlibs $(srcdir)/threadlibs.c

if_not_there$(EXE_SUFFIX): $(srcdir)/if_not_there.c
	$(CC) $(CFLAGS) -o if_not_there $(srcdir)/if_not_there.c

clean:
	-$(RM) gc.a *.o gctest gctest_dyn_link test_cpp \
	      setjmp_test  mon.out gmon.out a.out core if_not_there if_mach \
	      $(CORD_OBJS) cordtest cord/cordtest de cord/de
	-$(RM) gctest$(EXE_SUFFIX) gctest_dyn_link$(EXE_SUFFIX) test_cpp$(EXE_SUFFIX) \
	      setjmp_test$(EXE_SUFFIX) if_not_there$(EXE_SUFFIX) if_mach$(EXE_SUFFIX) \
	      cord/cordtest$(EXE_SUFFIX)
	-$(RM) *~

gctest$(EXE_SUFFIX): test.o gc.a
	-$(RM) gctest$(EXE_SUFFIX)
	$(CC) $(CFLAGS) -o gctest test.o gc.a

# If an optimized setjmp_test generates a segmentation fault,
# odds are your compiler is broken.  Gctest may still work.
# Try compiling setjmp_t.c unoptimized.
setjmp_test$(EXE_SUFFIX): $(srcdir)/setjmp_t.c $(srcdir)/gc.h \
		if_mach$(EXE_SUFFIX) if_not_there$(EXE_SUFFIX)
	-$(RM) setjmp_test$(EXE_SUFFIX)
	$(CC) $(CFLAGS) -o setjmp_test $(srcdir)/setjmp_t.c

test:  KandRtest cord/cordtest$(EXE_SUFFIX)
	./cord/cordtest$(EXE_SUFFIX)

# Those tests that work even with a K&R C compiler:
KandRtest: setjmp_test$(EXE_SUFFIX) gctest$(EXE_SUFFIX)
	./setjmp_test$(EXE_SUFFIX)
	./gctest$(EXE_SUFFIX)


