#
# Makefile for gracket
#

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

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

WXDIR = $(srcdir)/../wxxt

PLTSRCDIR = $(srcdir)/..
PLTBUILDDIR = $(builddir)/..

NOOP = :

# See ../Makefile about RUN_RACKET_<X>, which 
# typically redirects to RUN_THIS_RACKET_<X>:
RUN_THIS_RACKET_CGC = ../racket/racket@CGC@
RUN_THIS_RACKET_MMM = ../racket/racket@MMM@

###########################
#      Common Macros      #
###########################
CC = @CC@
LINKER = $(CC)
RANLIB = @RANLIB@
AR = @AR@
ARFLAGS = @ARFLAGS@
OPTIONS = @OPTIONS@ @MROPTIONS@
XLIB = @X_LIBS@
LDLIBS = @LIBS@
GCDIRECTORY = @GCDIR@
WBUILD = @WBUILD@
WXVARIANT =  @WXVARIANT@

NOGCINC = $(WXINC) -I$(PLTBUILDDIR)/racket/ -I$(PLTSRCDIR)/racket/include/
INC = -I$(PLTSRCDIR)/racket/$(GCDIRECTORY) $(NOGCINC)
CFLAGS = $(INC) $(OPTIONS) -D$(WXVARIANT) @COMPFLAGS@ @PROFFLAGS@ @CFLAGS@ @PREFLAGS@
LDFLAGS = @LDFLAGS@
ARLIBFLAGS = $(LDFLAGS) $(LDLIBS)
###########################

GRACKETLINKER = @MZLINKER@

GRACKETLDLIBS = ../racket/libracket.@LIBSFX@ ../racket/libmzgc.@LIBSFX@ $(LDLIBS)
GRACKETLDFLAGS = $(LDFLAGS) -L../racket

LOCALFLAGS_wx_xt = @WX_MMD_FLAG@
LOCALFLAGS_wx_mac = -I$(srcdir)/../mac/racket -MMD -DWX_CARBON
LOCALFLAGS = $(LOCALFLAGS_@WXVARIANT@)

GRACKETOBJECTS = grmain.@LTO@

RACKETDEPS = ../racket/libracket.@LIBSFX@ ../racket/libmzgc.@LIBSFX@

INSTALLDEST=/home/scheme/plt

MRSTATIC = 
MRSTATIC_STUB =

MZEXTRAEXPORT='GCDIR=$(GCDIRECTORY)'

LINKRESULT_wx_xt = gracket@CGC@
LINKRESULT_wx_mac = GRacket@CGC@.app/Contents/MacOS/GRacket@CGC@
LINKRESULT = $(LINKRESULT_@WXVARIANT@)

# Incremented each time the binaries change:
DOWNLOAD_BIN_VERSION = 1

bin:
	$(MAKE) @MAIN_VARIANT@

3m:
	cd gc2; $(MAKE) 3m

cgc:
	$(MAKE) $(LINKRESULT)

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

gracket@CGC@ : $(RACKETDEPS) grmain.@LTO@ $(GRACKETOBJECTS) $(MRSTATIC_STUB)
	$(GRACKETLINKER) $(GRACKETLDFLAGS) $(MRSTATIC) -o gracket@CGC@ $(GRACKETOBJECTS) $(GRACKETLDLIBS) $(MRSTATIC_STUB)

MRFW = Racket.framework/Versions/$(FWVERSION)/Racket
MRAPPSKEL = GRacket@CGC@.app/Contents/MacOS

GRacket@CGC@.app/Contents/MacOS/GRacket@CGC@: $(MZFW) $(MRAPPSKEL) grmain.@LTO@ 
	$(GRACKETLINKER) $(GRACKETLDFLAGS) -o GRacket@CGC@.app/Contents/MacOS/GRacket@CGC@ grmain.@LTO@ -Wl,-headerpad_max_install_names -F../racket -framework Racket -framework CoreFoundation @PROFFLAGS@
	/usr/bin/install_name_tool -change "Racket.framework/Versions/$(FWVERSION)/Racket" "@executable_path/../../../../racket/Racket.framework/Versions/$(FWVERSION)/Racket" GRacket@CGC@.app/Contents/MacOS/GRacket@CGC@

$(MRAPPSKEL): $(srcdir)/../mac/osx_appl.rkt
	env BUILDBASE=.. @RUN_RACKET_CGC@ -cqu $(srcdir)/../mac/osx_appl.rkt $(srcdir)/.. "@CGC@"

ee-app: gracket grmain_ee.@LTO@
	if [ "$(EEAPP)" = '' ] ; then echo "ERROR: You must specify EEAPP" ; else $(GRACKETLINKER) $(GRACKETLDFLAGS) $(MRSTATIC) -o $(EEAPP) grmain_ee.@LTO@ $(EEOBJECTS) $(GRACKETLDLIBS) $(MRSTATIC_STUB) ; fi

sunos4-static: dl_stub.@LTO@
	$(MAKE) gracket MRSTATIC=-static  MRSTATIC_STUB='dl_stub.@LTO@' LDNONSTATICLIBS=''

solaris-static:
	$(MAKE) gracket MRSTATIC="-Wl,-Bstatic" LDNONSTATICLIBS='-Wl,-Bdynamic -ldl -Wl,-Bstatic'

linux-static:
	$(MAKE) gracket MRSTATIC=--static LDNONSTATICLIBS='-ldl'

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

grmain.@LTO@ :  $(srcdir)/grmain.c $(srcdir)/../racket/main.c \
            $(srcdir)/../racket/cmdline.inc $(srcdir)/../racket/src/stypes.h \
            $(srcdir)/../racket/include/scheme.h
	$(CC) $(LOCALFLAGS) $(CFLAGS) $(DEF_COLLECTS_DIR) -c $(srcdir)/grmain.c -o grmain.@LTO@

grmain_ee.@LTO@ : gracket.@LTO@
	$(CC) $(LOCALFLAGS) $(CFLAGS) -DSTANDALONE_WITH_EMBEDDED_EXTENSION $(DEF_COLLECTS_DIR) -c $(srcdir)/grmain.cxx -o grmain_ee.@LTO@

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

clean:
	rm -f *.@LTO@ *.d core gracket gracket3m
	rm -f gc2/*.@LTO@ gc2/xsrc/* gc2/macxsrc/* gc2/*.d gc2/*.dd
	rm -rf gc2/xform-collects

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

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

install:
	$(MAKE) install-@MAIN_VARIANT@

install-3m:
	$(MAKE) install-@WXVARIANT@
	$(MAKE) install-@WXVARIANT@-3m
	$(MAKE) install-@WXVARIANT@-3m-final

install-cgc:
	$(MAKE) install-@WXVARIANT@
	$(MAKE) install-@WXVARIANT@-cgc
	$(MAKE) install-@WXVARIANT@-cgc-final

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

install-post-collects:
	$(MAKE) install-@WXVARIANT@-post-collects

install-common:
	@RUN_RACKET_CGC@ -c "$(srcdir)/../get-libs.rkt" gui "$(DESTDIR)$(libpltdir)"

# X11 ----------------------------------------

install-wx_xt:
	$(MAKE) install-common
	cd ..; rm -f "$(DESTDIR)$(bindir)/gracket@CGC_INSTALLED@"
	cd ..; rm -f "$(DESTDIR)$(bindir)/gracket@MMM_INSTALLED@"
	cd ..; echo 'MROPTIONS=@MROPTIONS@' >> $(BUILDINFO)
	cd ..; echo "MRLIBS=$(GUILIBS_@WXVARIANT@)" >> $(BUILDINFO)
	cd ..; echo "MRLDFLAGS=$(GRACKETLDFLAGS)" >> $(BUILDINFO)
	cd ..; mkdir -p "$(DESTDIR)$(bindir)"

install-no-lib-cgc-wx_xt:
	echo "no dynamic libs"

install-lib-cgc-wx_xt:
	$(NOOP)

install-wx_xt-cgc:
	$(MAKE) @MRLIBINSTALL@-cgc-wx_xt
	cd ..; $(ICP) gracket/gracket@CGC@ "$(DESTDIR)$(bindir)/gracket@CGC_INSTALLED@"
	@RUN_RACKET_CGC@ -cu "$(srcdir)/../racket/collects-path.rkt" "$(DESTDIR)$(bindir)/gracket@CGC_INSTALLED@@EXE_SUFFIX@" @COLLECTS_PATH@

install-wx_xt-cgc-final:
	$(NOOP)

install-no-lib-3m-wx_xt:
	echo "no dynamic libs"

install-lib-3m-wx_xt:
	$(NOOP)

install-wx_xt-3m:
	$(MAKE) @MRLIBINSTALL@-3m-wx_xt
	cd ..; $(ICP) gracket/gracket@MMM@ "$(DESTDIR)$(bindir)/gracket@MMM_INSTALLED@"
	@RUN_RACKET_MMM@ -cu "$(srcdir)/../racket/collects-path.rkt" "$(DESTDIR)$(bindir)/gracket@MMM_INSTALLED@@EXE_SUFFIX@" @COLLECTS_PATH@

install-wx_xt-3m-final:
	$(NOOP)

install-wx_xt-post-collects:
	$(NOOP)

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

FRAMEWORK_REL_PREFIX="@executable_path/../../../lib/"

install-wx_mac:
	$(MAKE) install-common
	cd ..; rm -rf "$(prefix)/GRacket@CGC_CAP_INSTALLED@.app"
	cd ..; rm -rf "$(prefix)/GRacket@MMM_CAP_INSTALLED@.app"

install-wx_mac-cgc:
	cd ..; $(ICP) -r gracket/GRacket@CGC@.app "$(prefix)/GRacket@CGC_CAP_INSTALLED@.app"
	@RUN_RACKET_CGC@ -cqu "$(srcdir)/../mac/rename-app.rkt" "$(prefix)/GRacket@CGC_CAP_INSTALLED@.app" "GRacket@CGC@" "GRacket@CGC_CAP_INSTALLED@"
	/usr/bin/install_name_tool -change "@executable_path/../../../Racket.framework/Versions/$(FWVERSION)/Racket" "@FRAMEWORK_PREFIX@Racket.framework/Versions/$(FWVERSION)/Racket" "$(prefix)/GRacket@CGC_CAP_INSTALLED@.app/Contents/MacOS/GRacket@CGC_CAP_INSTALLED@"
	@RUN_RACKET_CGC@ -cu "$(srcdir)/../racket/collects-path.rkt" "$(prefix)/GRacket@CGC_CAP_INSTALLED@.app/Contents/MacOS/GRacket@CGC_CAP_INSTALLED@" ../../../collects
	@STRIP_DEBUG@ "$(prefix)/GRacket@CGC_CAP_INSTALLED@.app/Contents/MacOS/GRacket@CGC_CAP_INSTALLED@"

install-wx_mac-cgc-final:
	$(NOOP)

install-wx_mac-3m:
	cd ..; $(ICP) -r "gracket/GRacket@MMM@.app" "$(prefix)/GRacket@MMM_CAP_INSTALLED@.app"
	@RUN_RACKET_MMM@ -cqu "$(srcdir)/../mac/rename-app.rkt" "$(prefix)/GRacket@MMM_CAP_INSTALLED@.app" "GRacket@MMM@" "GRacket@MMM_CAP_INSTALLED@"
	/usr/bin/install_name_tool -change "@executable_path/../../../../racket/Racket.framework/Versions/$(FWVERSION)_3m/Racket" "@FRAMEWORK_PREFIX@Racket.framework/Versions/$(FWVERSION)_3m/Racket" "$(prefix)/GRacket@MMM_CAP_INSTALLED@.app/Contents/MacOS/GRacket@MMM_CAP_INSTALLED@"
	@RUN_RACKET_MMM@ -cu "$(srcdir)/../racket/collects-path.rkt" "$(prefix)/GRacket@MMM_CAP_INSTALLED@.app/Contents/MacOS/GRacket@MMM_CAP_INSTALLED@" "../../../collects"
	@STRIP_DEBUG@ "$(prefix)/GRacket@MMM_CAP_INSTALLED@.app/Contents/MacOS/GRacket@MMM_CAP_INSTALLED@"

install-wx_mac-3m-final:
	$(NOOP)

install-wx_mac-post-collects:
	cd ..; rm -rf $(collectsdir)/launcher/Starter.app
	mkdir -p $(collectsdir)/launcher
	cd ..; $(ICP) -r gracket/Starter.app $(collectsdir)/launcher/.


@INCLUDEDEP@ grmain.d
