
# Builds Racket and GRacket (if enabled), and
#  drives the main install

PACKAGE = racket

srcdir = @srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
bindir = @bindir@
libdir = @libdir@
includepltdir = @includepltdir@
libpltdir = @libpltdir@
sharepltdir = @sharepltdir@
configdir = @etcpltdir@
collectsdir = @collectsdir@
appsdir = @appsdir@
mandir = @mandir@
docdir = @docdir@
builddir = @builddir@

NOOP = :

ICP = @ICP@

# RUN_RACKET_<X> typically redirects to RUN_THIS_RACKET_<X>,
# but it can also redirect to an existing Racket executable
# that is the same as the version being built (which is
# useful for cross-compilation, for example).
RUN_THIS_RACKET_CGC = racket/racket@CGC@
RUN_THIS_RACKET_MMM = racket/racket@MMM@
RUN_THIS_RACKET_MAIN_VARIANT = racket/racket@MAIN_VARIANT@

ALLDIRINFO = "$(DESTDIR)$(bindir)" \
             "$(DESTDIR)$(collectsdir)" \
             "$(DESTDIR)$(docdir)" \
             "$(DESTDIR)$(libdir)" \
             "$(DESTDIR)$(includepltdir)" \
             "$(DESTDIR)$(libpltdir)" \
             "$(DESTDIR)$(sharepltdir)" \
             "$(DESTDIR)$(configdir)" \
             "$(DESTDIR)$(appsdir)" \
             "$(DESTDIR)$(mandir)"

COPYING = "$(srcdir)/COPYING-libscheme.txt" \
          "$(srcdir)/COPYING_LESSER.txt" \
          "$(srcdir)/COPYING.txt"

all:
	$(MAKE) @MAIN_VARIANT@

TAGS:
	etags `find "@srcdir@/racket" -type f`

3m:
	$(MAKE) @MAKE_LOCAL_RACKET@
	cd racket; $(MAKE) 3m
	$(MAKE) @MAKE_GRACKET@-3m

gracket-3m:
	cd gracket; $(MAKE) 3m

no-3m:
	$(NOOP)

cgc:
	$(MAKE) @MAKE_LOCAL_RACKET@
	cd racket; $(MAKE) cgc
	$(MAKE) @MAKE_GRACKET@-cgc

gracket-cgc:
	cd gracket; $(MAKE) cgc

no-cgc:
	$(NOOP)

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

# Cross-compilation helper:

no-local-racket:
	$(NOOP)

local/racket/racket3m:
	mkdir -p local
	$(MAKE) local/Makefile
	cd local ; $(MAKE)

local/Makefile:
	cd local ; `cd ..; cd $(srcdir); pwd`/configure --disable-gracket

# Install (common) ----------------------------------------

INST_CONFIG = -X @DIRCVTPRE@"$(DESTDIR)$(collectsdir)"@DIRCVTPOST@ -G @DIRCVTPRE@"$(DESTDIR)$(configdir)"@DIRCVTPOST@
SETUP_RACKET_FLAGS = $(INST_CONFIG) $(SELF_RACKET_FLAGS) @INSTALL_SETUP_RACKET_FLAGS@
SETUP_SETUP_FLAGS = @INSTALL_SETUP_FLAGS@ $(PLT_SETUP_OPTIONS) $(PLT_ISO)
SETUP_ARGS = $(SETUP_RACKET_FLAGS) -N "raco" -l- setup $(SETUP_SETUP_FLAGS)

install:
	$(MAKE) install-@MAIN_VARIANT@

plain-install:
	if [ "$(DESTDIR)" != "" ]; then \
          echo "cannot use plain-install with DESTDIR=$(DESTDIR)"; exit 1; \
        fi
	$(MAKE) plain-install-@MAIN_VARIANT@

install-common-first:
	case "$(DESTDIR)" in \
	  "" | /*) ;; \
	  *) echo "expected an absolute path for DESTDIR; given: $(DESTDIR)"; exit 1;; \
        esac
	mkdir -p $(ALLDIRINFO)

install-common-middle:
	$(MAKE) @MAKE_COPYTREE@-run
	$(MAKE) install-@MAKE_GRACKET@-post-collects
	$(MAKE) lib-finish

install-common-last:
	$(MAKE) fix-paths
	$(MAKE) @MAKE_INSTALL_PKGSCOPE@-raco-pkg-default-scope
	$(ICP) $(COPYING) "$(DESTDIR)$(sharepltdir)"/

install-no:
	$(NOOP)

lib-finish:
	if [ "$(DESTDIR)" = "" ]; then \
	  @LIBFINISH@ "$(libdir)"; \
	fi

no-run:
	$(NOOP)

copytree-run:
	@RUN_RACKET_CGC@ $(SELF_RACKET_FLAGS) -u \
          "$(srcdir)/../collects/setup/unixstyle-install.rkt" \
          make-install-copytree "$(srcdir)/.." \
          $(ALLDIRINFO) "@INSTALL_ORIG_TREE@"

adjust-raco-pkg-default-scope:
	@RUN_RACKET_CGC@ -l raco pkg config -i --set default-scope @INSTALL_PKGSCOPE@

preserve-raco-pkg-default-scope:
	$(NOOP)

install-gracket-post-collects:
	cd gracket; $(MAKE) install-post-collects

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

fix-paths:
	if [ "$(DESTDIR)" != "" ]; then \
          if [ "$(SKIP_DESTDIR_FIX)" = "" ]; then \
            @RUN_RACKET_CGC@ $(SELF_RACKET_FLAGS) -u \
              "$(srcdir)/../collects/setup/unixstyle-install.rkt" \
              make-install-destdir-fix "$(srcdir)/.." \
              $(ALLDIRINFO) "@INSTALL_ORIG_TREE@"; \
          fi \
        fi

# 3m install ----------------------------------------

install-3m-common:
	$(MAKE) install-common-first
	cd racket; $(MAKE) install-3m
	$(MAKE) install-@MAKE_GRACKET@-3m
	$(MAKE) install-common-middle

install-3m:
	$(MAKE) install-3m-common
	@RUN_RACKET_MMM@ $(SETUP_ARGS)
	$(MAKE) install-common-last

plain-install-3m:
	$(MAKE) install-3m-common
	$(MAKE) install-common-last

install-no-3m:
	$(NOOP)

install-gracket-3m:
	cd gracket; $(MAKE) install-3m

# CGC install ----------------------------------------

install-cgc-common:
	$(MAKE) install-common-first
	cd racket; $(MAKE) install-cgc
	$(MAKE) install-@MAKE_GRACKET@-cgc
	$(MAKE) install-common-middle

install-cgc:
	$(MAKE) install-cgc-common
	@RUN_RACKET_CGC@ $(SETUP_ARGS)
	$(MAKE) install-common-last

plain-install-cgc:
	$(MAKE) install-cgc-common
	$(MAKE) install-common-last

install-no-cgc:
	$(NOOP)

install-gracket-cgc:
	cd gracket; $(MAKE) install-cgc

# Both install ----------------------------------------

install-both-common:
	$(MAKE) install-common-first
	cd racket; $(MAKE) install-both
	$(MAKE) install-@MAKE_GRACKET@-both
	$(MAKE) install-common-middle

install-both:
	$(MAKE) install-both-common
	@RUN_RACKET_MAIN_VARIANT@ $(SETUP_ARGS)
	$(MAKE) install-common-last

plain-install-both:
	$(MAKE) install-both-common
	$(MAKE) install-common-last

install-no-both:
	$(NOOP)

install-gracket-both:
	cd gracket; $(MAKE) install-both

# Docs install ----------------------------------------

# The "install" target already does this, but it does tons more too.
install-html:
	$(MAKE) install PLT_SETUP_OPTIONS="-nxiId $(PLT_SETUP_OPTIONS)"

install-pdf:
	$(MAKE) install PLT_SETUP_OPTIONS="-nxiId --doc-pdf $(docdir)/pdf $(PLT_SETUP_OPTIONS)"

# Clean ----------------------------------------

clean:
	cd racket; $(MAKE) clean
	if [ -d gracket ]; then cd gracket; $(MAKE) clean; fi
	rm -rf compiled
	rm -f TAGS

# Reconfigure ----------------------------------------

SRC_MAKEFILES = $(srcdir)/Makefile.in $(srcdir)/racket/Makefile.in \
                $(srcdir)/racket/src/Makefile.in $(srcdir)/gracket/Makefile.in \
                $(srcdir)/racket/gc2/Makefile.in $(srcdir)/gracket/gc2/Makefile.in \
                $(srcdir)/rktio/Makefile.in $(srcdir)/racket/dynsrc/Makefile.in

reconfigure:
	$(MAKE) Makefile

Makefile: $(srcdir)/configure $(SRC_MAKEFILES)
	$(srcdir)/configure $(CONFIGURE_ARGS_qq)
