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

srcdir = @srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
libdir = @libdir@
includepltdir = @includepltdir@
libpltdir = @libpltdir@
collectsdir = @collectsdir@
datarootdir = @datarootdir@
mandir = @mandir@
docdir = @docdir@
builddir = @builddir@

NOOP = :

# 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)$(mandir)"

all:
	$(MAKE) @MAIN_VARIANT@

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

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

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

no-3m:
	$(NOOP)

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

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

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

no-cgc:
	$(NOOP)

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

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

SETUP_ARGS = -X "$(DESTDIR)$(collectsdir)" -N "raco setup" -l- setup $(PLT_SETUP_OPTIONS) $(PLT_ISO) @INSTALL_SETUP_FLAGS@

# Pass compile and link flags to `make install' for use by any
#  collection-setup actions (currently in "plot") that compile
#  and link C code:
CFLAGS = @CFLAGS@ @COMPFLAGS@ @PREFLAGS@
LDFLAGS = @LDFLAGS@
WITH_ENV_VARS = env CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" 

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:
	mkdir -p $(ALLDIRINFO)

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

install-common-last:
	$(MAKE) fix-paths

install-plot:
	cd plot; $(MAKE) install

install-no:
	$(NOOP)

lib-finish:
	@LIBFINISH@ "$(libdir)"

no-run:
	$(NOOP)

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

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

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

fix-paths:
	if [ "$(DESTDIR)" != "" ]; then \
          @RUN_RACKET_CGC@ -u \
            "$(srcdir)/../collects/setup/unixstyle-install.rkt" \
            make-install-destdir-fix "$(srcdir)/.." \
            $(ALLDIRINFO) "@INSTALL_ORIG_TREE@"; \
        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
	$(WITH_ENV_VARS) @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
	$(WITH_ENV_VARS) @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
	$(WITH_ENV_VARS) @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

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

clean:
	cd racket; $(MAKE) clean
	if [ -d gracket ]; then cd gracket; $(MAKE) clean; fi
