# makefile for Gambit system.

# Copyright (c) 1994-2018 by Marc Feeley, All Rights Reserved.

PACKAGE_SHORTNAME = @PACKAGE_SHORTNAME@
PACKAGE_NAME = @PACKAGE_NAME@
PACKAGE_VERSION = @PACKAGE_VERSION@
PACKAGE_STRING = @PACKAGE_STRING@
PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@
PACKAGE_TARNAME = @PACKAGE_TARNAME@

@SET_MAKE@

srcdir = @srcdir@
VPATH = @srcdir@
srcdirpfx = @srcdirpfx@

CONFIGURE_COMMAND = @CONFIGURE_COMMAND@

C_COMPILER = @C_COMPILER_MF@
C_PREPROC = @C_PREPROC_MF@
FLAGS_OBJ = @FLAGS_OBJ_MF@
FLAGS_DYN = @FLAGS_DYN_MF@
FLAGS_LIB = @FLAGS_LIB_MF@
FLAGS_EXE = @FLAGS_EXE_MF@
FLAGS_OPT = @FLAGS_OPT_MF@
FLAGS_OPT_RTS = @FLAGS_OPT_RTS_MF@
DEFS = @DEFS@
LIBS = @LIBS@

GAMBITLIB_DEFS = @GAMBITLIB_DEFS_MF@
LIB_PREFIX = @LIB_PREFIX@
LIB_VERSION_SUFFIX = @LIB_VERSION_SUFFIX@
LIB_MAJOR_VERSION_SUFFIX = @LIB_MAJOR_VERSION_SUFFIX@

LIB_EXTENSION = @LIB_EXTENSION@
GAMBITLIB = @GAMBITLIB@
GAMBITGSCLIB = @GAMBITGSCLIB@
GAMBITGSILIB = @GAMBITGSILIB@
INSTALL = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_LIB = @INSTALL_LIB@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
LN_S = @LN_S@
RANLIB = @RANLIB@
AR = @AR@
RC = @RC@
GIT = @GIT@
HG = @HG@

prefix = @prefix@
exec_prefix = @exec_prefix@
includedir = @includedir@
libdir = @libdir@
bindir = @bindir@
docdir = @docdir@
infodir = @infodir@
emacsdir = @emacsdir@
libexecdir = @libexecdir@
datarootdir = @datarootdir@
datadir = @datadir@
htmldir = @htmldir@
dvidir = @dvidir@
pdfdir = @pdfdir@
psdir = @psdir@
localedir = @localedir@
mandir = @mandir@

# This directory's subdirectories are mostly independent; you can cd
# into them and run `make' without going through this makefile.
# To change the values of `make' variables: instead of editing makefiles,
# (1) if the variable is set in `config.status', edit `config.status'
#     (which will cause the makefiles to be regenerated when you run `make');
# (2) otherwise, pass the desired values on the `make' command line.

MDEFINES = prefix="$(prefix)" exec_prefix="$(exec_prefix)" \
includedir="$(includedir)" libdir="$(libdir)" \
bindir="$(bindir)" docdir="$(docdir)" \
infodir="$(infodir)" emacsdir="$(emacsdir)"

SUBDIRS = include lib gsi gsc bin misc doc tests examples contrib prebuilt bench githooks

RCFILES = README INSTALL.txt LICENSE-2.0.txt LGPL.txt \
makefile.in configure configure.ac config.guess config.sub install-sh mkidirs \
relpath gsc-boot.unix gsc-boot.bat.windows

GITRCFILES = .gitignore .gitattributes
HGRCFILES = .hgignore .hgtags

GENDISTFILES =

DISTFILES = $(RCFILES) $(GENDISTFILES)

GITDISTFILES = $(GITRCFILES)
HGDISTFILES = $(HGRCFILES)

#BUILD_FARM = farm
BUILD_FARM = localhost
#BUILD_HOST = baro.iro.umontreal.ca
BUILD_HOST = localhost

.SUFFIXES:

all:

all-pre: gsc-boot@exe@

all-post:

from-scratch: fake_target
	$(MAKE) bootclean
	$(MAKE) bootstrap # make gsc compiler corresponding to repo HEAD
	$(MAKE) bootclean
	$(MAKE) all       # make everything with repo HEAD gsc compiler

boot-most-recent-release: fake_target
	@if test -x "boot/gsc-boot@exe@" && \
	    test "`boot/gsc-boot@exe@ -v | sed -e 's/ .*//'`" = "$(PACKAGE_VERSION)"; then \
	  echo "**** boot/gsc-boot@exe@ doesn't need to be rebuilt"; \
	else \
	  $(MAKE) boot-most-recent-release-gsc-boot; \
	fi

boot-most-recent-release-gsc-boot: fake_target
	@echo "**** building boot/gsc-boot@exe@"
	rm -rf boot
	mkdir -p boot
	$(GIT) archive tags/$(PACKAGE_VERSION) | (cd boot && tar xf -)
	cd boot && \
	./configure CC='$(C_COMPILER)' && \
	$(MAKE) bootstrap

boot-wip-compiler: fake_target
	@if test -x "boot/gsc/gsc@exe@" && \
	    test "`boot/gsc/gsc -v | sed -e 's/ .*//'`" = "$(PACKAGE_VERSION)" && \
	    test "`find gsc/makefile.in gsc/*.scm -exec diff -q \{\} boot/\{\} \; 2>&1`" = ""; then \
	  echo "**** boot/gsc/gsc@exe@ doesn't need to be rebuilt"; \
	else \
	  $(MAKE) boot-wip-compiler-gsc-gsc && $(MAKE) bootclean; \
	fi

boot-wip-compiler-gsc-gsc: boot-most-recent-release
	@echo "**** building boot/gsc/gsc@exe@ using boot/gsc-boot@exe@"
	cd boot && \
	rm -f gsc/makefile && \
	cp ../gsc/makefile.in ../gsc/*.scm gsc && \
	./configure CC='$(C_COMPILER)' && \
	for i in lib gsi gsc ; do (cd $$i ; $(MAKE) all) ; done

wip-gsc-boot: boot-wip-compiler
	cp boot/gsc/gsc@exe@ gsc-boot@exe@

current-gsc-boot: wip-gsc-boot

dummy-gsc-boot:
	rm -f gsc-boot@bat@ gsc-boot@exe@
	cp gsc-boot.bat.windows gsc-boot@bat@
	cp gsc-boot.unix gsc-boot@exe@
	chmod +x gsc-boot@exe@

gsc-boot@exe@:
	@if [ -e .git ] && \
	    $(GIT) --version > /dev/null 2> /dev/null && \
	    $(GIT) describe --tags > /dev/null 2> /dev/null && \
	    test "`$(GIT) describe --tags`" != "$(PACKAGE_VERSION)"; then \
	  echo "**** checking if current gsc-boot must be built"; \
	  $(MAKE) current-gsc-boot; \
	else \
	  echo "**** using a dummy gsc-boot"; \
	  $(MAKE) dummy-gsc-boot; \
	fi

fake_target:

doc info pdf html txt: fake_target
	@cd doc && $(MAKE) $(MDEFINES) $@

check ut ut-C ut-js ut-python ut-ruby ut-php ut-java st st-C st-js st-python st-ruby st-php st-java: fake_target all
	@cd tests && $(MAKE) $(MDEFINES) $@

bench: fake_target all
	@cd bench && $(MAKE) $(MDEFINES) $@

examples: fake_target all
	@cd examples && $(MAKE) $(MDEFINES) $@

prebuilt: dist dist-devel
	PATH="gsi:$$PATH" prebuilt/remote-connect $(BUILD_FARM) "" "(define tmpdir \"gambtmp0\")(define gambit-dist \"$(PACKAGE_TARNAME)\")(define timeout 3000)(ssh timeout (string-append \"rm -rf \" tmpdir \";mkdir \" tmpdir \";echo \\\"logged in on:\\\";hostname\"))(scp-to timeout (string-append gambit-dist \".tgz\") (string-append tmpdir \"/.\"))(ssh timeout (string-append \"ssh $(BUILD_HOST) \\\"cd \" tmpdir \";tar zxf \" gambit-dist \".tgz;cd \" gambit-dist \";./configure;make -j 4;mv ../\" gambit-dist \".tgz .;cd prebuilt;make prebuilt\\\"\"))(scp-from timeout (string-append tmpdir \"/\" gambit-dist \"/$(PACKAGE_TARNAME)-*\") \".\")"
	@# cd prebuilt && $(MAKE) $(MDEFINES) $@

xactlog@exe@: fake_target
	@cd contrib/xactlog && $(MAKE) $(MDEFINES) $@

stamp: fake_target
	@cd include && $(MAKE) $(MDEFINES) $@

commit: stamp deselect-gen-for-commit
	$(RC) commit

diff: fake_target
	@$(RC) diff | cat

log: fake_target
	@$(RC) log | cat

status: fake_target
	@$(RC) status || :

push: fake_target
	@if test "$(RC)" = "$(GIT)"; then \
	  $(GIT) push git@github.com:gambit/gambit.git; \
	  $(GIT) push --tags git@github.com:gambit/gambit.git; \
	else \
	  $(HG) push; \
	fi

pull: fake_target
	@if test "$(RC)" = "$(GIT)"; then \
	  $(GIT) pull; \
	  $(GIT) fetch --tags; \
	else \
	  $(HG) pull; \
	fi

update-nopull: fake_target
	@if test "$(RC)" = "$(GIT)"; then \
	  next_version=`$(GIT) tag | fgrep -e '-bootstrap' | sed -e 's/v//g' -e 's/-bootstrap.*//g' | sort -r -s -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n -k 5,5n | sed 's/^/v/g' | sed -n '/^$(PACKAGE_VERSION)/{n;p;}'`; \
	else \
	  next_version=`$(HG) tags | fgrep -e '-bootstrap' | sed -e 's/v//g' -e 's/-bootstrap.*//g' | sort -r -s -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n -k 5,5n | sed 's/^/v/g' | sed -n '/^$(PACKAGE_VERSION)/{n;p;}'`; \
	fi; \
	if test "$$next_version" = ""; then \
	  if test "$(RC)" = "$(GIT)"; then \
	    $(GIT) checkout master; \
	  else \
	    $(HG) checkout; \
	  fi; \
	else \
	  $(RC) checkout $$next_version-bootstrap; \
	  $(MAKE) bootstrap; \
	  $(RC) checkout $$next_version; \
	  $(MAKE) bootclean bootstrap update; \
	fi

update: pull update-nopull
	$(MAKE)

new-major: fake_target
	major=`echo $(PACKAGE_VERSION) | sed -e "s/v//g" -e "s/\.[^.]*\.[^.]*//g"`; \
	next_major=`eval expr 1 + $$major`; \
	next_version=v$$next_major.0.0; \
	$(MAKE) NEW_VERSION=$$next_version change-version

new-minor: fake_target
	major=`echo $(PACKAGE_VERSION) | sed -e "s/v//g" -e "s/\.[^.]*\.[^.]*//g"`; \
	minor=`echo $(PACKAGE_VERSION) | sed -e "s/v[^.]*\.//g" -e "s/\.[^.]*//g"`; \
	next_minor=`eval expr 1 + $$minor`; \
	next_version=v$$major.$$next_minor.0; \
	$(MAKE) NEW_VERSION=$$next_version change-version

new-revision: fake_target
	major=`echo $(PACKAGE_VERSION) | sed -e "s/v//g" -e "s/\.[^.]*\.[^.]*//g"`; \
	minor=`echo $(PACKAGE_VERSION) | sed -e "s/v[^.]*\.//g" -e "s/\.[^.]*//g"`; \
	revision=`echo $(PACKAGE_VERSION) | sed -e "s/v[^.]*\.[^.]*\.//g" -e "s///g"`; \
	next_revision=`eval expr 1 + $$revision`; \
	next_version=v$$major.$$minor.$$next_revision; \
	$(MAKE) NEW_VERSION=$$next_version change-version

change-version: deselect-gen-for-commit
	major=`echo $(PACKAGE_VERSION) | sed -e "s/v//g" -e "s/\.[^.]*\.[^.]*//g"`; \
	minor=`echo $(PACKAGE_VERSION) | sed -e "s/v[^.]*\.//g" -e "s/\.[^.]*//g"`; \
	revision=`echo $(PACKAGE_VERSION) | sed -e "s/v[^.]*\.[^.]*\.//g" -e "s///g"`; \
	next_major=`echo $(NEW_VERSION) | sed -e "s/v//g" -e "s/\.[^.]*\.[^.]*//g"`; \
	next_minor=`echo $(NEW_VERSION) | sed -e "s/v[^.]*\.//g" -e "s/\.[^.]*//g"`; \
	next_revision=`echo $(NEW_VERSION) | sed -e "s/v[^.]*\.[^.]*\.//g" -e "s///g"`; \
	version_num=`eval expr "\( 100000 \\* $$major \) + \( 1000 \\* $$minor \) + $$revision"`; \
	new_version_num=`eval expr "\( 100000 \\* $$next_major \) + \( 1000 \\* $$next_minor \) + $$next_revision"`; \
	sed -e "/(define (compiler-version) $$version_num)/s/$$version_num/$$new_version_num/" gsc/_parms.scm > gsc/_parms.scm-new; \
	if ! diff gsc/_parms.scm gsc/_parms.scm-new > /dev/null; then \
	  mv gsc/_parms.scm-new gsc/_parms.scm; \
	  if test "$(RC)" = "$(GIT)"; then \
	    $(GIT) commit -a -m "[COMPILER CHANGES NEEDED FOR $(NEW_VERSION)] Changed version in compiler"; \
	  else \
	    $(HG) commit -m "[COMPILER CHANGES NEEDED FOR $(NEW_VERSION)] Changed version in compiler"; \
	  fi; \
	  $(RC) tag $(NEW_VERSION)-bootstrap; \
	  if $(MAKE) bootstrap; then \
	    misc/changev $$version_num $$new_version_num; \
	    touch doc/gambit.txi; \
	    cd tests; @SETDLPATH@ ../gsc/gsc -:~~bin=../bin,~~lib=../lib,~~include=../include -f -warnings -c mix.scm; mv mix.c test5.ok; cd ..; \
	    if $(MAKE) bootclean && $(MAKE) -j 2 && $(MAKE) check && $(MAKE) doc && $(MAKE) select-gen-for-commit; then \
	      if test "$(RC)" = "$(GIT)"; then \
	        $(GIT) commit -a -m "[RUNTIME CHANGES NEEDED FOR $(NEW_VERSION)] Changed version of runtime using misc/changev"; \
	      else \
	        $(HG) commit -m "[RUNTIME CHANGES NEEDED FOR $(NEW_VERSION)] Changed version of runtime using misc/changev"; \
	      fi; \
	      $(RC) tag $(NEW_VERSION); \
	    fi; \
	  fi; \
	else \
	  echo Compiler version in gsc/_parms.scm is inconsistent with PACKAGE_VERSION; \
	fi

release: fake_target
	$(MAKE) bootstrap
	$(MAKE) clean
	$(MAKE) all
	$(MAKE) check
	$(MAKE) doc
	rm -f $(PACKAGE_TARNAME).tgz $(PACKAGE_TARNAME)-*.*
	$(MAKE) prebuilt

publish-release: fake_target
	misc/publish-release $(PACKAGE_VERSION) $(PACKAGE_TARNAME).tgz $(PACKAGE_TARNAME)-*.*

bootstrap-pre: all

bootstrap-post:

install-pre:
	@if test "@ENABLE_SHARED@" = "yes" -a "@ENABLE_ABSOLUTE_SHARED_LIBS@" = "yes" -a "$(DESTDIR)" != ""; then \
	  echo "*** The use of --enable-shared and --enable-absolute-shared-libs is"; \
	  echo "*** incompatible with the use of DESTDIR.  You can either specify an"; \
	  echo "*** empty DESTDIR or reconfigure with --disable-absolute-shared-libs."; \
	  exit 1; \
	fi

install-post: all
	if test "@ENABLE_MULTIPLE_VERSIONS@" = "yes"; then \
	  if test "@bat@" = ""; then \
	    $(srcdirpfx)./mkidirs "$(DESTDIR)$(prefix)"; \
	    rm -f "$(DESTDIR)$(prefix)/../current" \
	      "$(DESTDIR)$(prefix)/../current.lnk"; \
	    (cd "$(DESTDIR)$(prefix)/.." && $(LN_S) $(PACKAGE_VERSION) current); \
	  fi; \
	fi

uninstall-pre:

uninstall-post:
	if test "@ENABLE_MULTIPLE_VERSIONS@" = "yes"; then \
	  if test "@bat@" = ""; then \
	    (cd "$(DESTDIR)$(prefix)/.." && \
	     rm -f current && \
	     rmdir $(PACKAGE_VERSION)); \
	  fi; \
	fi

select-gen-for-commit-pre:

select-gen-for-commit-post:

deselect-gen-for-commit-pre:

deselect-gen-for-commit-post:

mostlyclean-pre:

mostlyclean-post:

clean-pre: mostlyclean-pre

clean-post: mostlyclean-post

distclean-pre: clean-pre

distclean-post: clean-post

bootclean-pre: distclean-pre

bootclean-post: distclean-post

realclean-pre: bootclean-pre

realclean-post: bootclean-post
	rm -f config.log config.status makefile

rc-setup-pre:
	$(RC) init
	@if test "$(RC)" = "$(GIT)"; then \
	  $(GIT) add $(RCFILES) $(GITRCFILES); \
	  rm -f .git/config; \
	  echo '[core]' >> .git/config; \
	  echo '	repositoryformatversion = 0' >> .git/config; \
	  echo '	fileMode = false' >> .git/config; \
	  echo '	bare = false' >> .git/config; \
	  echo '	logallrefupdates = true' >> .git/config; \
	  echo '	ignorecase = true' >> .git/config; \
	  echo '	autocrlf = false' >> .git/config; \
	  echo '[remote "origin"]' >> .git/config; \
	  echo '	url = git@github.com:gambit/gambit.git' >> .git/config; \
	  echo '	fetch = +refs/heads/*:refs/remotes/origin/*' >> .git/config; \
	  echo '[branch "master"]' >> .git/config; \
	  echo '	remote = origin' >> .git/config; \
	  echo '	merge = refs/heads/master' >> .git/config; \
	else \
	  $(HG) add $(RCFILES) $(HGRCFILES); \
	  rm -f .hg/hgrc; \
	  echo "[paths]" > .hg/hgrc; \
	  echo "default-push = ssh://gambit@frontal07.iro.umontreal.ca/HTML/repo/gambit" >> .hg/hgrc; \
	  echo "default = http://www.iro.umontreal.ca/~gambit/repo/gambit/" >> .hg/hgrc; \
	fi

rc-setup-post:
	@if test "$(RC)" = "$(GIT)"; then \
	  $(GIT) commit -a -m "Initial commit of $(PACKAGE_STRING)"; \
	else \
	  $(HG) commit -m "Initial commit of $(PACKAGE_STRING)"; \
	fi

dist-pre:
	rm -rf $(PACKAGE_TARNAME)
	mkdir $(PACKAGE_TARNAME)
	chmod 777 $(PACKAGE_TARNAME)
	@echo "  Copying distribution files:"
	@for file in $(DISTFILES); do \
	  echo "    $$file"; \
	  ln $(srcdirpfx)$$file $(PACKAGE_TARNAME) 2> /dev/null \
	    || cp -p $(srcdirpfx)$$file $(PACKAGE_TARNAME); \
	done

dist-post:
	COPYFILE_DISABLE=1 tar chof $(PACKAGE_TARNAME).tar $(PACKAGE_TARNAME)
	gzip -9 $(PACKAGE_TARNAME).tar
	mv $(PACKAGE_TARNAME).tar.gz $(PACKAGE_TARNAME).tgz
	rm -rf $(PACKAGE_TARNAME)

dist-devel-pre:
	rm -rf $(PACKAGE_TARNAME)
	mkdir $(PACKAGE_TARNAME)
	chmod 777 $(PACKAGE_TARNAME)
	@echo "  Copying distribution files:"
	@for file in $(DISTFILES); do \
	  echo "    $$file"; \
	  ln $(srcdirpfx)$$file $(PACKAGE_TARNAME) 2> /dev/null \
	    || cp -p $(srcdirpfx)$$file $(PACKAGE_TARNAME); \
	done
	@if test "$(RC)" = "$(GIT)"; then \
	  for file in $(GITDISTFILES); do \
	    echo "    $$file"; \
	    ln $(srcdirpfx)$$file $(PACKAGE_TARNAME) 2> /dev/null \
	      || cp -p $(srcdirpfx)$$file $(PACKAGE_TARNAME); \
	  done; \
	  echo "    .git"; \
	  ln $(srcdirpfx).git $(PACKAGE_TARNAME) 2> /dev/null \
	    || cp -p -R $(srcdirpfx).git $(PACKAGE_TARNAME); \
	else \
	  for file in $(HGDISTFILES); do \
	    echo "    $$file"; \
	    ln $(srcdirpfx)$$file $(PACKAGE_TARNAME) 2> /dev/null \
	      || cp -p $(srcdirpfx)$$file $(PACKAGE_TARNAME); \
	  done; \
	  echo "    .hg"; \
	  ln $(srcdirpfx).hg $(PACKAGE_TARNAME) 2> /dev/null \
	    || cp -p -R $(srcdirpfx).hg $(PACKAGE_TARNAME); \
	fi

dist-devel-post:
	rm -rf $(PACKAGE_TARNAME)-devel
	mv $(PACKAGE_TARNAME) $(PACKAGE_TARNAME)-devel
	COPYFILE_DISABLE=1 tar chof $(PACKAGE_TARNAME)-devel.tar $(PACKAGE_TARNAME)-devel
	gzip -9 $(PACKAGE_TARNAME)-devel.tar
	mv $(PACKAGE_TARNAME)-devel.tar.gz $(PACKAGE_TARNAME)-devel.tgz
	rm -rf $(PACKAGE_TARNAME)-devel

all-recursive bootstrap-recursive install-recursive uninstall-recursive select-gen-for-commit-recursive deselect-gen-for-commit-recursive mostlyclean-recursive clean-recursive distclean-recursive bootclean-recursive realclean-recursive rc-setup-recursive dist-recursive dist-devel-recursive:
	@for subdir in $(SUBDIRS); do \
	  target=`echo $@ | sed 's/-recursive//'`; \
	  echo making $$target in $$subdir; \
	  (cd $$subdir && $(MAKE) $$target) || exit 1; \
	done

all: all-post

all-post: all-recursive

all-recursive: all-pre

bootstrap: bootstrap-post

bootstrap-post: bootstrap-recursive

bootstrap-recursive: bootstrap-pre

install: install-post

install-post: install-recursive

install-recursive: install-pre

uninstall: uninstall-post

uninstall-post: uninstall-recursive

uninstall-recursive: uninstall-pre

select-gen-for-commit: select-gen-for-commit-post

select-gen-for-commit-post: select-gen-for-commit-recursive

select-gen-for-commit-recursive: select-gen-for-commit-pre

deselect-gen-for-commit: deselect-gen-for-commit-post

deselect-gen-for-commit-post: deselect-gen-for-commit-recursive

deselect-gen-for-commit-recursive: deselect-gen-for-commit-pre

mostlyclean: mostlyclean-post

mostlyclean-post: mostlyclean-recursive

mostlyclean-recursive: mostlyclean-pre

clean: clean-post

clean-post: clean-recursive

clean-recursive: clean-pre

distclean: distclean-post

distclean-post: distclean-recursive

distclean-recursive: distclean-pre

bootclean: bootclean-post

bootclean-post: bootclean-recursive

bootclean-recursive: bootclean-pre

realclean: realclean-post

realclean-post: realclean-recursive

realclean-recursive: realclean-pre

rc-setup: rc-setup-post

rc-setup-post: rc-setup-recursive

rc-setup-recursive: rc-setup-pre

dist: doc dist-post

dist-post: dist-recursive

dist-recursive: dist-pre

dist-devel: doc dist-devel-post

dist-devel-post: dist-devel-recursive

dist-devel-recursive: dist-devel-pre

# For an explanation of the following makefile rules, see node
# `Automatic Remaking' in GNU Autoconf documentation.

makefile: makefile.in config.status
	CONFIG_FILES=$@ CONFIG_HEADERS= ./config.status

config.status: configure
	./config.status --recheck

configure: configure.ac
	cd $(srcdir) && autoconf configure.ac > configure && chmod 755 configure

# Tell versions [3.59,3.63) of GNU make not to export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.
.NOEXPORT:
