#
# Makefile for mzscheme
#

srcdir = @srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
libdir = @libdir@
includepltdir = @includepltdir@
libpltdir = @libpltdir@
collectsdir = @collectsdir@
builddir = @builddir@

# for version.mak:
mainsrcdir = @srcdir@/..
@INCLUDEDEP@ @srcdir@/version.mak

CC = @CC@
CPP = @CPP@

AR = @AR@
ARFLAGS = @ARFLAGS@
RANLIB = @RANLIB@

MZSRC = $(srcdir)/src

FOREIGN_DIR = ../foreign
FOREIGN_OBJ = $(FOREIGN_DIR)/foreign.@LTO@
FOREIGN_OBJS = $(FOREIGN_OBJ) $(FOREIGN_DIR)/gcc/libffi/src/*.@LTO@ $(FOREIGN_DIR)/gcc/libffi/src/*/*.@LTO@
FOREIGN_LIB = $(FOREIGN_OBJ) ../foreign/gcc/libffi/libffi@FOREIGN_CONVENIENCE@.la
FOREIGN_OBJSLIB = @FOREIGN_OBJSLIB@
# Dependency always uses FOREIGN_LIB, but linker uses FOREIGN_OBJSLIB. The
#  FOREIGN_OBJSLIB expansion has too many "*"s to be a valid make target,
#  while FOREIGN_LIB is not always acceptable to the linker.
# The foreign build always uses libtool, so that's why we use .la instead of @LTA@
#  in FOREIGN_LIB.

LLVM_DIR = ../llvm

# About EXE_SUFFIX:
#  Cygwin offers the illusion that binaries have no ".exe" suffix by
#  having the file-open syscall open an ".exe" file for reading when
#  (1) the given filename has no suffix, and (2) there's not a suffixless
#  file present. However, Cygwin doesn't do that when opening a file for
#  output, even when the output mode is 'update. So we need to explcitly
#  add EXE_SUFFIX in a few places.

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

bin: 
	$(MAKE) @MZBINTARGET@

normal-bin:
	$(MAKE) @MAIN_VARIANT@

common:
	$(MAKE) g-c
	$(MAKE) @FOREIGNTARGET@
	$(MAKE) @LLVMTARGET@

cgc:
	$(MAKE) common
	$(MAKE) dynlib
	$(MAKE) mzlibrary
	$(MAKE) mzscheme@CGC@ 

3m:
	$(MAKE) cgc
	cd gc2; $(MAKE) all
	cd dynsrc; $(MAKE) dynlib3m
	cd gc2; $(MAKE) ../mzscheme@MMM@

both:
	$(MAKE) cgc
	$(MAKE) 3m

oskit: 
	$(MAKE) common
	$(MAKE) mzlibrary
	$(MAKE) mzscheme.multiboot

mzschemelib: 
	$(MAKE) g-c 
	$(MAKE) @FOREIGNTARGET@
	$(MAKE) mzlibrary 

dynlib:
	cd src; $(MAKE) mzheaders
	cd dynsrc; $(MAKE) dynlib 

dynexample:
	$(MAKE) dynlib
	cd dynsrc; $(MAKE) dynexample 

oe:
	$(MAKE) dynlib
	cd dynsrc; $(MAKE) oe

g-c:
	cd @GCDIR@; $(MAKE) ../libmzgc.@LIBSFX@ 

sproc-extra-lib:
	cd @GCDIR@; $(MAKE) sproc.@LTO@
	$(MAKE) sproc.@LTO@

gc.@LIBSFX@: 
	$(AR) $(ARFLAGS) @GCDIR@/gc.@LIBSFX@ @GCDIR@/*.@LTO@

# Compilation of the foreign libraries (this compiles all of them)
foreign-stuff:
	cd $(FOREIGN_DIR); $(MAKE) all

foreign-nothing:
	echo no foreign

# llvm
llvm-stuff:
	cd $(LLVM_DIR); $(MAKE) all

llvm-nothing:
	echo no llvm

mzlibrary:
	cd src; $(MAKE) all 
	$(MAKE) libmzscheme.@LIBSFX@

libmzscheme.@LIBSFX@: src/*.@LTO@ @FOREIGN_LIB_IF_USED@
	$(AR) $(ARFLAGS) libmzscheme.@LIBSFX@ src/*.@LTO@ @FOREIGN_OBJSLIB_IF_USED@ @LIBMZSCHEME_DEP@
	$(RANLIB) libmzscheme.@LIBSFX@

# Irix sproc, only
sproc.@LTO@: @GCDIR@/sproc.@LTO@
	cp @GCDIR@/sproc.@LTO@ sproc.@LTO@

# Unix ----------------------------------------

mzscheme@CGC@@NOT_OSX@: libmzscheme.@LIBSFX@ libmzgc.@LIBSFX@ main.@LTO@ $(SPECIALIZINGOBJECTS)
	@MZLINKER@ -o mzscheme@CGC@ main.@LTO@ $(SPECIALIZINGOBJECTS) libmzscheme.@LIBSFX@ libmzgc.@LIBSFX@ @LDFLAGS@ @LIBS@

# Mac OS ----------------------------------------

MZFW = PLT_MzScheme.framework/Versions/$(FWVERSION)/PLT_MzScheme

$(MZFW): libmzscheme.@LIBSFX@ libmzgc.@LIBSFX@ $(SPECIALIZINGOBJECTS)
	mkdir -p  PLT_MzScheme.framework/Versions/$(FWVERSION)
	$(CC) -o $(MZFW) -framework CoreFoundation -dynamiclib -all_load $(SPECIALIZINGOBJECTS) libmzscheme.@LIBSFX@ libmzgc.@LIBSFX@ @LDFLAGS@ @LIBS@
	rm -f PLT_MzScheme.framework/PLT_MzScheme
	ln -s Versions/$(FWVERSION)/PLT_MzScheme PLT_MzScheme.framework/PLT_MzScheme

mzscheme@CGC@@OSX@: $(MZFW) main.@LTO@
	$(CC) -o mzscheme@CGC@ @PROFFLAGS@ main.@LTO@ -Wl,-headerpad_max_install_names -F. -framework PLT_MzScheme
	/usr/bin/install_name_tool -change "PLT_MzScheme.framework/Versions/$(FWVERSION)/PLT_MzScheme" "@executable_path/PLT_MzScheme.framework/Versions/$(FWVERSION)/PLT_MzScheme" "mzscheme@CGC@"

# OSKit ----------------------------------------

mzscheme.multiboot : libmzscheme.@LIBSFX@ libmzgc.@LIBSFX@ main.@LTO@
	$(LD) -Ttext 100000 $(LDFLAGS) @OSKHOME@/lib/oskit/multiboot.@LTO@ \
		-o mzscheme.multiboot main.@LTO@ -L. -lmzscheme -lgc \
                -L@OSKHOME@/lib -loskit_kern -loskit_clientos @EXTRA_OSK_LIBS@ \
                -loskit_freebsd_c -loskit_c -loskit_lmm \
                -loskit_freebsd_m -loskit_memfs -loskit_fsnamespace  \
                -loskit_dev -loskit_com \
                -loskit_kern -loskit_freebsd_c -loskit_c \
		@OSKHOME@/lib/oskit/crtn.@LTO@

# ----------------------------------------

DEF_COLLECTS_DIR = -DINITIAL_COLLECTS_DIRECTORY='"'"`cd $(srcdir)/../../collects; pwd`"'"'

main.@LTO@: $(srcdir)/main.c $(srcdir)/include/scheme.h $(srcdir)/sconfig.h $(srcdir)/src/stypes.h $(srcdir)/cmdline.inc $(srcdir)/oskglue.inc
	$(CC) @CFLAGS@ @COMPFLAGS@ @PREFLAGS@ @PROFFLAGS@ @OPTIONS@ @MZOPTIONS@ $(DEF_COLLECTS_DIR) -I$(builddir) -I$(srcdir)/include -c $(srcdir)/main.c -o main.@LTO@

main_ee.@LTO@: main.@LTO@
	$(CC) @CFLAGS@ @COMPFLAGS@ @PREFLAGS@ @PROFFLAGS@ @OPTIONS@ @MZOPTIONS@ $(DEF_COLLECTS_DIR) -I$(builddir) -I$(srcdir)/include -DSTANDALONE_WITH_EMBEDDED_EXTENSION -c $(srcdir)/main.c -o main_ee.@LTO@

ee-main:
	$(MAKE) main_ee.@LTO@

ee-app: $(MZSCHEME) main_ee.@LTO@
	if [ "$(EEAPP)" = '' ] ; then echo "ERROR: You must specify EEAPP" ; else $(CC) $(MAIN_CFLAGS_NODBG) -o $(EEAPP) main_ee.@LTO@ $(EEOBJECTS) $(SPECIALIZINGOBJECTS) libmzscheme.@LIBSFX@ libmzgc.@LIBSFX@ @LIBS@ ; fi

testext.@LTO@: testext.c
	$(CC) $(MAIN_CFLAGS) -I/home/scheme/plt/compiler/ -c testext.c -o testext.@LTO@

mzstart.exe: $(srcdir)/dynsrc/start.c
	$(CC) @CFLAGS@ -o mzstart.exe $(srcdir)/dynsrc/start.c

starter: mzstart.exe

exn:
	$(MAKE) $(srcdir)/src/schexn.h

CSTARTUPDEST = cstartup.inc

startup:
	$(MAKE) $(srcdir)/src/$(CSTARTUPDEST)

mark:
	$(MAKE) $(srcdir)/src/mzmark.c

headers:
	./mzscheme@CGC@ -qr $(srcdir)/mkincludes.ss "$(DESTDIR)$(includepltdir)" "$(srcdir)" .

$(srcdir)/src/schexn.h: $(srcdir)/src/exnsrc.ss $(srcdir)/src/makeexn
	./mzscheme@CGC@ -gqr $(srcdir)/src/makeexn < $(srcdir)/src/exnsrc.ss > $(srcdir)/src/schexn.h

$(srcdir)/src/$(CSTARTUPDEST): $(srcdir)/src/startup.ss $(srcdir)/src/schvers.h $(srcdir)/src/schminc.h
	./mzscheme@CGC@ -qr $(srcdir)/src/sstoinc.ss $(CSTARTUPEXTRA) < $(srcdir)/src/startup.ss > $(srcdir)/src/$(CSTARTUPDEST)

$(srcdir)/src/mzmark.c: $(srcdir)/src/mzmarksrc.c $(srcdir)/src/mkmark.ss
	mzscheme -qr $(srcdir)/src/mkmark.ss < $(srcdir)/src/mzmarksrc.c > $(srcdir)/src/mzmark.c

cstartup.zo:
	$(MAKE) startup CSTARTUPEXTRA='zo' CSTARTUPDEST="../cstartup.zo"

clean@NOT_OSX@:
	/bin/rm -rf tmp[123456789] tests/tmp[123456789] tests/sub[123]
	/bin/rm -f tests/*~
	/bin/rm -f *.@LTO@ core
	/bin/rm -f mzdyn.o libmzgc.@LIBSFX@ libmzscheme.@LIBSFX@ libmzscheme3m.@LIBSFX@ libdl.a mzscheme mzscheme.multiboot
	/bin/rm -f include/macosxpre
	/bin/rm -f include/macosxpre.p
	cd gc; $(MAKE) clean
	cd gc2; $(MAKE) clean
	cd sgc; $(MAKE) clean
	cd src; $(MAKE) clean
	cd dynsrc; $(MAKE) clean
	if [ -d ../foreign ] ; then cd ../foreign ; $(MAKE) clean ; fi
	if [ -d ../llvm    ] ; then cd ../llvm    ; $(MAKE) clean ; fi

clean@OSX@:
	rm -rf PLT_MzScheme.framework
	$(MAKE) clean@NOT_OSX@

BUILDINFO=$(DESTDIR)$(libpltdir)/buildinfo
ICP=@ICP@

install:
	$(MAKE) install-@MAIN_VARIANT@

install-3m:
	$(MAKE) headers
	$(MAKE) @MZINSTALLTARGET@
	$(MAKE) @MZINSTALLTARGET@-3m
	$(MAKE) @MZINSTALLTARGET@-3m-final

install-cgc:
	$(MAKE) headers
	$(MAKE) @MZINSTALLTARGET@
	$(MAKE) @MZINSTALLTARGET@-cgc
	$(MAKE) @MZINSTALLTARGET@-cgc-final

install-both:
	$(MAKE) headers
	$(MAKE) @MZINSTALLTARGET@
	$(MAKE) @MZINSTALLTARGET@-3m
	$(MAKE) @MZINSTALLTARGET@-cgc
	$(MAKE) @MZINSTALLTARGET@-@MAIN_VARIANT@-final

# Unix install ----------------------------------------

# Prefix might be relative to srcdir, or it might be absolute, so we
# have to go up and install things from there.

unix-install:	
	cd ..; rm -f "$(DESTDIR)$(bindir)/mzscheme@CGC_INSTALLED@"
	cd ..; rm -f "$(DESTDIR)$(bindir)/mzscheme@MMM_INSTALLED@"
	cd ..; cp mzscheme/starter "$(DESTDIR)$(libpltdir)/starter"
	cd ..; echo 'CC=@CC@' > "$(BUILDINFO)"
	cd ..; echo 'CFLAGS=@CFLAGS@ @PREFLAGS@ @COMPFLAGS@' >> "$(BUILDINFO)"
	cd ..; echo 'OPTIONS=@OPTIONS@' >> "$(BUILDINFO)"
	cd ..; echo 'MZOPTIONS=@MZOPTIONS@' >> "$(BUILDINFO)"
	cd ..; echo "LDFLAGS=@LDFLAGS@" >> "$(BUILDINFO)"
	cd ..; echo 'LIBS=@LIBS@' >> "$(BUILDINFO)"

unix-install-cgc:
	cd ..; $(ICP) mzscheme/libmzgc.@LIBSFX@ "$(DESTDIR)$(libdir)/libmzgc.@LIBSFX@"
	cd ..; $(ICP) mzscheme/libmzscheme.@LIBSFX@ "$(DESTDIR)$(libdir)/libmzscheme.@LIBSFX@"
	cd ..; $(ICP) mzscheme/mzscheme@CGC@ "$(DESTDIR)$(bindir)/mzscheme@CGC_INSTALLED@"
	cd ..; cp mzscheme/mzdyn.o "$(DESTDIR)$(libpltdir)/mzdyn.o"
	./mzscheme@CGC@ -mvqu "$(srcdir)/collects-path.ss" "$(DESTDIR)$(bindir)/mzscheme@CGC_INSTALLED@@EXE_SUFFIX@" @COLLECTS_PATH@

unix-install-cgc-final:
	cd .

unix-install-3m:
	cd ..; $(ICP) mzscheme/mzscheme@MMM@ "$(DESTDIR)$(bindir)/mzscheme@MMM_INSTALLED@"
	cd ..; $(ICP) mzscheme/libmzscheme3m.@LIBSFX@ "$(DESTDIR)$(libdir)/libmzscheme3m.@LIBSFX@"
	cd ..; $(ICP) mzscheme/mzdyn3m.o "$(DESTDIR)$(libpltdir)/mzdyn3m.o"
	./mzscheme@MMM@ -mvqu "$(srcdir)/collects-path.ss" "$(DESTDIR)$(bindir)/mzscheme@MMM_INSTALLED@@EXE_SUFFIX@" @COLLECTS_PATH@

unix-install-3m-final:
	cd .

# OS X install ----------------------------------------

MZFWDIR = @FRAMEWORK_INSTALL_DIR@/PLT_MzScheme.framework
MZFWMMM = PLT_MzScheme.framework/Versions/$(FWVERSION)_3m/PLT_MzScheme

osx-install:
	$(MAKE) unix-install
	rm -f $(MZFWDIR)/PLT_MzScheme
	rm -rf $(MZFWDIR)/Versions/$(FWVERSION)
	rm -rf $(MZFWDIR)/Versions/$(FWVERSION)_3m
	if [ @FRAMEWORK_REL_INSTALL@ = yes ] ; then rm -rf $(MZFWDIR) ; fi
	mkdir -p @FRAMEWORK_INSTALL_DIR@

osx-install-cgc:
	$(MAKE) unix-install-cgc
	mkdir -p "$(MZFWDIR)/Versions/$(FWVERSION)"
	cp $(MZFW) $(MZFWDIR)/Versions/$(FWVERSION)/
	if [ @FRAMEWORK_REL_INSTALL@ = yes ] ; then /usr/bin/install_name_tool -change "@executable_path/PLT_MzScheme.framework/Versions/$(FWVERSION)/PLT_MzScheme" "@executable_path/../lib/PLT_MzScheme.framework/Versions/$(FWVERSION)/PLT_MzScheme" "$(bindir)/mzscheme@CGC_INSTALLED@" ; fi

osx-install-cgc-final:
	$(MAKE) unix-install-cgc-final
	ln -s Versions/$(FWVERSION)/PLT_MzScheme $(MZFWDIR)/

osx-install-3m:
	$(MAKE) unix-install-3m
	mkdir -p "$(MZFWDIR)/Versions/$(FWVERSION)_3m"
	cp $(MZFWMMM) $(MZFWDIR)/Versions/$(FWVERSION)_3m/
	if [ @FRAMEWORK_REL_INSTALL@ = yes ] ; then /usr/bin/install_name_tool -change "@executable_path/PLT_MzScheme.framework/Versions/$(FWVERSION)_3m/PLT_MzScheme" "@executable_path/../lib/PLT_MzScheme.framework/Versions/$(FWVERSION)_3m/PLT_MzScheme" "$(bindir)/mzscheme@MMM_INSTALLED@" ; fi

osx-install-3m-final:
	$(MAKE) unix-install-3m-final
	ln -s Versions/$(FWVERSION)_3m/PLT_MzScheme $(MZFWDIR)/


# Cygwin install ----------------------------------------

unix-cygwin-install:
	$(MAKE) unix-install
	cd dynsrc; $(MAKE) cygwin-install-core

unix-cygwin-install-cgc:
	$(MAKE) unix-install-cgc
	cd dynsrc; $(MAKE) cygwin-install-cgc

unix-cygwin-install-cgc-final:
	$(MAKE) unix-install-cgc-final

unix-cygwin-install-3m:
	$(MAKE) unix-install-3m
	cd dynsrc; $(MAKE) cygwin-install-3m

unix-cygwin-install-3m-final:
	$(MAKE) unix-install-3m-final
