# This Makefile is a wrapper around chicken-install(1) which eases building
# edward without superuser rights and without requiring any configuration
# of chicken-install. Furthermore, it supports optional dependency vendoring
# to build edward without network access.
#
# By default, the Makefile builds a statically linked binary. For development
# purposes it can be desirable to do dynamic linked builds instead. To do so
# the PACKAGE environment variable can be set to zero.
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
DOCDIR ?= $(PREFIX)/share/doc/edward
TMPDIR ?= /tmp

# By default, build a statically linked version for packaging.
# If package is zero, build a dynamically linked version instead.
PACKAGE ?= 1
ifneq ($(PACKAGE),0)
	INSTALL_FLAGS = -feature package
endif

# Install all CHICKEN files relative to ./output
#
# See: https://bugs.call-cc.org/ticket/1792

# Respect CHICKEN_REPOSITORY_PATH if set in the environment
CHICKEN_REPOSITORY_PATH ?= $(shell env -i chicken-install -repository)

export CHICKEN_INSTALL_PREFIX := $(CURDIR)/output
export CHICKEN_INSTALL_REPOSITORY := $(CURDIR)/output
export CHICKEN_REPOSITORY_PATH := $(CURDIR)/output:$(CHICKEN_REPOSITORY_PATH)

# Optional dependency vendoring to build without network access
#
# Release tarballs include vendored dependencies (see make dist).

VENDOR_DIRECTORY = $(CURDIR)/vendor
ifneq ($(wildcard $(VENDOR_DIRECTORY)/*),)
	VENDORED=1
endif

all:
ifdef VENDORED
	chicken-install -location $(VENDOR_DIRECTORY) $(shell ls $(VENDOR_DIRECTORY))
endif
	chicken-install $(INSTALL_FLAGS)

check: export EDWARD=$(CHICKEN_INSTALL_PREFIX)/bin/edward
check:
	chicken-install -test
	@./tests/integration/run.sh
	@./tests/interactive/run.sh

bench:
	csc -O3 -o $(TMPDIR)/edward-benchmark benchmarks/run.scm
	$(TMPDIR)/edward-benchmark

install:
	install -Dm755 $(CHICKEN_INSTALL_PREFIX)/bin/edward "$(DESTDIR)$(BINDIR)/edward"
	install -Dm644 README.md "$(DESTDIR)$(DOCDIR)/README.md"

vendor:
	# TODO: Start with CS6, `chicken-install -recursive` no longer searches the
	# current directory for eggs. Thus we need to list the deps explicitly here.
	env -i CHICKEN_EGG_CACHE=$(VENDOR_DIRECTORY) chicken-install -r -recursive \
		srfi-1 srfi-14 srfi-37 matchable posix-regex test micro-benchmark
	find $(VENDOR_DIRECTORY) \( -name STATUS -a -type f \) -exec rm {} +
# XXX: Make sure to remove the vendor directory before running `make dist`.
# As libraries are, unfortunately, build within the vendor directory.
dist: VERSION = $(shell git describe --tags)
dist: vendor
	mkdir -p edward-$(VERSION)
	cp -R LICENSE.txt GNUmakefile README.md bin edward.egg lib tests vendor edward-$(VERSION)
	tar -czf edward-$(VERSION).tar.gz edward-$(VERSION)
	rm -rf edward-$(VERSION)

.PHONY: all check bench install vendor dist
