CHICKEN_C ?= csc
CSI ?= csi
CSC_FLAGS += -K prefix -static
# What and why:
# -emit-all-import-libraries: Emit import-libraries for all defined modules.
#    Used to generate .import.scm files so compiler would know how to use the modules.
#    Without it, csc fails with "cannot import from undefined module" error.
# -module-registration: Always  generate  module registration code, even when
#    import libraries are emitted. Enables us to import from our modules at run time.
#    Used in macros, where we inject `(import (only sex-macros cat))' before the macro body.
#    Without it, sexc compiles scuccessfully, but is unable to process macros, failing with
#    "during expansion of (import ...) - cannot import from undefined module: sex-macros"
#     error.
# -c: Stop after compilation to object files. This one is obvious.

# GNU directory variables. Command line overrides, e.g.
#   make prefix=$(HOME)/.local install
#   make DESTDIR=/tmp/stage prefix=/usr install
prefix = /usr/local
exec_prefix = $(prefix)
bindir = $(exec_prefix)/bin
datarootdir = $(prefix)/share
datadir = $(datarootdir)
# sexc looks for the standard library beside itself: next to the binary
# in a work tree, and bindir/../share/sex once installed
sexlibdir = $(datadir)/sex
INSTALL = install
INSTALL_PROGRAM = $(INSTALL)
INSTALL_DATA = $(INSTALL) -m 644

SEX_LIB = $(wildcard lib/*.sex)

MODULE_FLAGS = -emit-all-import-libraries -module-registration -c

all: sexc

include eggs.mk

# MODULES, and a line for each naming the modules it imports, read off
# the import forms
include modules.mk
OBJ = $(MODULES:%=%.o)

sexc: $(OBJ) main.scm
	$(CHICKEN_C) $(CSC_FLAGS) main.scm -o sexc-tmp -link sexc
        # otherwise csc hangs, probably because it tries to compile to sexc.o first
	mv sexc-tmp sexc

#------------------------------------------------------------------

modules.mk: main.scm $(MODULES:%=%.scm) tools/module-deps.scm
	$(CSI) -s tools/module-deps.scm main.scm > $@.tmp
	mv $@.tmp $@

# A module is one file that declares its own exports, made a module by
# -module. It is linked with the modules among its prerequisites.
comma := ,
empty :=
space := $(empty) $(empty)
LINKS = $(subst $(space),$(comma),$(basename $(filter %.o,$^)))

%.o: %.scm
	$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -module $* -o $@ -unit $* $(if $(LINKS),-link $(LINKS))

# Vendored, and a (module ...) form of its own
sex-fmt-c.o: sex-fmt-c.scm
	$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -o $@ -unit sex-fmt-c

# Both link the modules built here, whose import libraries they find
# with the root on the repository path. The tests are built in their
# own directory, where their includes would otherwise find the modules'
# namesakes
WITH_ROOT_REPO = CHICKEN_REPOSITORY_PATH=$(CURDIR):$(CHICKEN_REPOSITORY_PATH)

sex-tests: $(wildcard tests/*.scm) $(OBJ)
	cd tests && $(WITH_ROOT_REPO) $(CHICKEN_C) $(CSC_FLAGS) run.scm -o ../$@ -link sexc

sextest: tools/sextest/sextest.scm reader.o utils.o
	$(CHICKEN_C) $(CSC_FLAGS) $< -o $@ -link reader,utils

# A program directly in tests/sex-programs is a test; lib/ holds what
# they import
SEX_TEST_PROGRAMS = $(wildcard tests/sex-programs/*.sex)

check run-tests: sexc sex-tests sextest
	./sex-tests
	./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS)
	cd tests/modules && SEXC=$(CURDIR)/sexc $(CSI) -s check.scm
	cd tests/exit-code && SEXC=$(CURDIR)/sexc $(CSI) -s check.scm

install: all installdirs
	$(INSTALL_PROGRAM) sexc $(DESTDIR)$(bindir)/sexc
	$(INSTALL_DATA) $(SEX_LIB) $(DESTDIR)$(sexlibdir)

install-strip:
	$(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' install

installdirs:
	$(INSTALL) -d $(DESTDIR)$(bindir) $(DESTDIR)$(sexlibdir)

uninstall:
	rm -f $(DESTDIR)$(bindir)/sexc
	rm -f $(SEX_LIB:lib/%=$(DESTDIR)$(sexlibdir)/%)
	-rmdir $(DESTDIR)$(sexlibdir)

clean:
	rm -f $(OBJ) main.o modules.mk
	rm -f *.import.scm
	rm -f *.link
	rm -f sexc sex-tests sextest

.PHONY: all check run-tests install install-strip installdirs uninstall clean
