#
# Makefile for gracket
#

PACKAGE = racket

srcdir = @srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
bindir = @bindir@
libdir = @libdir@
libpltdir = @libpltdir@
libpltdir_rel = @libpltdir_rel@
etcpltdir = @etcpltdir@
sharepltdir = @sharepltdir@
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 = @COMPFLAGS@ @PROFFLAGS@ @CFLAGS@
CPPFLAGS = @PREFLAGS@
ALL_CFLAGS = $(INC) $(OPTIONS) -D$(WXVARIANT) $(CFLAGS) $(CPPFLAGS)
LDFLAGS = @LDFLAGS@
ARLIBFLAGS = $(LDFLAGS) $(LDLIBS)
###########################

GRACKETLINKER = @MZLINKER@

GRACKETLDLIBS@NOT_MINGW@ = ../racket/libracket.@LIBSFX@ ../racket/libmzgc.@LIBSFX@ $(LDLIBS)
GRACKETLDLIBS@MINGW@ = ../racket/libracket.dll.a ../racket/libmzgc.dll.a $(LDLIBS) -ldelayimp -static-libgcc
GRACKETLDFLAGS = $(LDFLAGS) -L../racket

GRACKETRES@NOT_MINGW@ =
GRACKETRESDEP@NOT_MINGW@ =
GRACKETRES@MINGW@ = -mwindows gres.o
GRACKETRESDEP@MINGW@ = gres.o

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@

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:
	$(MAKE) resources
	cd gc2; $(MAKE) 3m

cgc:
	$(MAKE) $(LINKRESULT)

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

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

gres.o : $(srcdir)/../worksp/gracket/gracket.rc
	@WINDRES@ -i $(srcdir)/../worksp/gracket/gracket.rc -o gres.o

resources@NOT_MINGW@ :
	$(NOOP)

resources@MINGW@ : gres.o
	$(NOOP)

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

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 $(srcdir)/../racket/src/schvers.h
	env BUILDBASE=.. @RUN_RACKET_CGC@ $(SELF_RACKET_FLAGS) -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@`"'"'
DEF_CONFIG_DIR = -DINITIAL_CONFIG_DIRECTORY='"'"`cd $(srcdir)/../..; @PWD@`/etc"'"'
DEF_C_DIRS = $(DEF_COLLECTS_DIR) $(DEF_CONFIG_DIR)

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) $(ALL_CFLAGS) $(DEF_C_DIRS) -c $(srcdir)/grmain.c -o grmain.@LTO@

grmain_ee.@LTO@ : gracket.@LTO@
	$(CC) $(LOCALFLAGS) $(ALL_CFLAGS) -DSTANDALONE_WITH_EMBEDDED_EXTENSION $(DEF_C_DIRS) -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

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:
	$(NOOP)

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

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

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

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

GRACKET_NAME@NOT_MINGW@ = gracket
GRACKET_NAME@MINGW@ = GRacket

install-wx_xt-cgc:
	$(MAKE) @MRLIBINSTALL@-cgc-wx_xt
	cd ..; $(ICP) gracket/gracket@CGC@ "$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@CGC_INSTALLED@@EXE_SUFFIX@"
	cd ..; @STRIP_DEBUG@ "$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@CGC_INSTALLED@@EXE_SUFFIX@"
	@RUN_RACKET_CGC@ $(SELF_RACKET_FLAGS) -cu "$(srcdir)/../racket/collects-path.rkt" @DIRCVTPRE@"$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@CGC_INSTALLED@@EXE_SUFFIX@"@DIRCVTPOST@ @GUI_COLLECTS_PATH@ @GUI_CONFIG_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)$(libpltdir)/$(GRACKET_NAME)@MMM_INSTALLED@@EXE_SUFFIX@"
	cd ..; @STRIP_DEBUG@ "$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@MMM_INSTALLED@@EXE_SUFFIX@"
	@RUN_RACKET_MMM@ $(SELF_RACKET_FLAGS) -cu "$(srcdir)/../racket/collects-path.rkt" @DIRCVTPRE@"$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@MMM_INSTALLED@@EXE_SUFFIX@"@DIRCVTPOST@ @GUI_COLLECTS_PATH@ @GUI_CONFIG_PATH@

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

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

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

FRAMEWORK_REL_PREFIX="@executable_path/../../../"
FRAMEWORK_ABS_PREFIX = "$(libpltdir)/"

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

install-wx_mac-cgc:
	cd ..; $(ICP) -r gracket/GRacket@CGC@.app $(DESTDIR)"$(libpltdir)/GRacket@CGC_CAP_INSTALLED@.app"
	@RUN_RACKET_CGC@ $(SELF_RACKET_FLAGS) -cqu "$(srcdir)/../mac/rename-app.rkt" $(DESTDIR)"$(libpltdir)/GRacket@CGC_CAP_INSTALLED@.app" "GRacket@CGC@" "GRacket@CGC_CAP_INSTALLED@"
	/usr/bin/install_name_tool -change "@executable_path/../../../../racket/Racket.framework/Versions/$(FWVERSION)/Racket" "@FRAMEWORK_PREFIX@Racket.framework/Versions/$(FWVERSION)/Racket" $(DESTDIR)"$(libpltdir)/GRacket@CGC_CAP_INSTALLED@.app/Contents/MacOS/GRacket@CGC_CAP_INSTALLED@"
	@RUN_RACKET_CGC@ $(SELF_RACKET_FLAGS) -cu "$(srcdir)/../racket/collects-path.rkt" $(DESTDIR)"$(libpltdir)/GRacket@CGC_CAP_INSTALLED@.app/Contents/MacOS/GRacket@CGC_CAP_INSTALLED@" @GR_APP_COLLECTS_PATH@ @GR_APP_CONFIG_PATH@
	@STRIP_DEBUG@ $(DESTDIR)"$(libpltdir)/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" $(DESTDIR)"$(libpltdir)/GRacket@MMM_CAP_INSTALLED@.app"
	@RUN_RACKET_MMM@ $(SELF_RACKET_FLAGS) -cqu "$(srcdir)/../mac/rename-app.rkt" $(DESTDIR)"$(libpltdir)/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" $(DESTDIR)"$(libpltdir)/GRacket@MMM_CAP_INSTALLED@.app/Contents/MacOS/GRacket@MMM_CAP_INSTALLED@"
	@RUN_RACKET_MMM@ $(SELF_RACKET_FLAGS) -cu "$(srcdir)/../racket/collects-path.rkt" $(DESTDIR)"$(libpltdir)/GRacket@MMM_CAP_INSTALLED@.app/Contents/MacOS/GRacket@MMM_CAP_INSTALLED@" @GR_APP_COLLECTS_PATH@ @GR_APP_CONFIG_PATH@
	@STRIP_DEBUG@ $(DESTDIR)"$(libpltdir)/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 $(DESTDIR)$(libpltdir)/Starter.app
	cd ..; $(ICP) -r gracket/Starter.app $(DESTDIR)$(libpltdir)/.


@INCLUDEDEP@ grmain.d
