CHICKEN_C ?= csc
# beside csc, whatever it is called: chicken-csc comes with chicken-csi
CSI ?= $(patsubst %csc,%csi,$(CHICKEN_C))
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

# Each program, and the source it is built from
PROGRAMS = sexc=main.scm sextest=tools/sextest/sextest.scm sex-tests=tests/run.scm
PROGRAM_NAMES = $(foreach p,$(PROGRAMS),$(firstword $(subst =, ,$(p))))
PROGRAM_OBJ = $(foreach p,$(PROGRAMS),$(patsubst %.scm,%.o,$(lastword $(subst =, ,$(p)))))

# MODULES, and a line for each module and program naming what it
# imports, read off the import forms. Not for clean, which has to work
# when they cannot be read
ifneq ($(MAKECMDGOALS),clean)
include modules.mk
endif

# The scan is redone whenever a source may have changed what it
# imports, or a NAME.scm may have come to be the module an import
# names; but modules.mk is rewritten only when that changed, since make
# starts over each time it is, and a source dated in the future would
# have it start over for good
modules.mk: modules.stamp ;

modules.stamp: $(SOURCES) $(filter-out %.import.scm,$(wildcard *.scm)) \
               tools/module-deps.scm dependencies.txt Makefile
	$(CSI) -s tools/module-deps.scm $(PROGRAMS) > modules.mk.tmp \
		|| { rm -f modules.mk.tmp; exit 1; }
	if cmp -s modules.mk.tmp modules.mk; then rm modules.mk.tmp; \
		else mv modules.mk.tmp modules.mk; fi
	touch $@

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

# A module is one file that declares its own exports, made a module by
# -module, except the vendored sex-fmt-c, a (module ...) form of its own
%.o: %.scm
	$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< $(if $(filter sex-fmt-c,$*),,-module $*) -o $@ -unit $*

# A program's object is named after its source: csc names its own after
# -o, and sexc.o is the module's
$(PROGRAM_OBJ): %.o: %.scm
	$(CHICKEN_C) $(CSC_FLAGS) -c $< -o $@

$(PROGRAM_NAMES):
	$(CHICKEN_C) $(CSC_FLAGS) $< -o $@

# 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)
	@$(MAKE) --no-print-directory check-modules
	@$(MAKE) --no-print-directory check-exit-code

# The end-to-end checks, each on its own: run with the eggs on the path
check-modules check-exit-code: check-%: sexc
	cd tests/$* && 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 *.o $(PROGRAM_OBJ) modules.mk modules.stamp
	rm -f *.import.scm
	rm -f *.link $(PROGRAM_OBJ:.o=.link)
	rm -f $(PROGRAM_NAMES)

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