# -*- makefile-gmake -*-

# Include-able makefile for building Chibi libraries - see README.libs
# for usage.

.PHONY: all all-libs clean clean-libs dist-clean dist-clean-libs install install-libs uninstall uninstall-libs doc doc-libs
.PRECIOUS: %.c lib/%.c

# install configuration

CC        ?= cc
AR        ?= ar
CD        ?= cd
RM        ?= rm -f
LS        ?= ls
CP        ?= cp
LN        ?= ln -sf
INSTALL   ?= install
INSTALL_EXE	?=	$(INSTALL)
MKDIR     ?= $(INSTALL) -d
RMDIR     ?= rmdir
TAR       ?= tar
DIFF      ?= diff
GIT       ?= git
GREP      ?= grep
FIND      ?= find
SYMLINK   ?= ln -s
LDCONFIG  ?= ldconfig

# gnu coding standards
prefix      ?= /usr/local
PREFIX      ?= $(prefix)
exec_prefix ?= $(PREFIX)
bindir      ?= $(exec_prefix)/bin
libdir      ?= $(exec_prefix)/lib
includedir  ?= $(PREFIX)/include
datarootdir ?= $(PREFIX)/share
datadir     ?= $(datarootdir)
mandir      ?= $(datarootdir)/man
man1dir     ?= $(mandir)/man1

# hysterical raisins
BINDIR      ?= $(bindir)
LIBDIR      ?= $(libdir)
SOLIBDIR    ?= $(libdir)
INCDIR      ?= $(includedir)/chibi
MODDIR      ?= $(datadir)/chibi
BINMODDIR   ?= $(SOLIBDIR)/chibi
PKGCONFDIR  ?= $(SOLIBDIR)/pkgconfig
MANDIR      ?= $(man1dir)

# allow snow to be configured separately
SNOWPREFIX    ?= /usr/local
SNOWLIBDIR    ?= $(SNOWPREFIX)/lib
SNOWSOLIBDIR  ?= $(SNOWLIBDIR)
SNOWMODDIR    ?= $(SNOWPREFIX)/share/snow
SNOWBINMODDIR ?= $(SNOWSOLIBDIR)/snow

# for packaging tools
DESTDIR       ?=

########################################################################
# System configuration - if not using GNU make, set PLATFORM and the
# flags from Makefile.detect (at least SO, EXE, CLIBFLAGS) as necessary.

include Makefile.detect

########################################################################

all-libs: $(COMPILED_LIBS) lib/chibi/snow/install.sld

lib/%.c: lib/%.stub $(CHIBI_FFI_DEPENDENCIES)
	$(CHIBI_FFI) $<

lib/chibi/pty$(SO): lib/chibi/pty.c $(INCLUDES) libchibi-scheme$(SO)
	$(CC) $(CLIBFLAGS) $(CLINKFLAGS) $(XCPPFLAGS) $(XCFLAGS) $(LDFLAGS) -o $@ $< -L. $(RLDFLAGS) $(XLIBS) -lchibi-scheme -lutil

lib/%$(SO): lib/%.c $(INCLUDES) libchibi-scheme$(SO)
	$(CC) $(CLIBFLAGS) $(CLINKFLAGS) $(XCPPFLAGS) $(XCFLAGS) $(LDFLAGS) -o $@ $< -L. $(RLDFLAGS) $(XLIBS) -lchibi-scheme

doc-libs: $(HTML_LIBS)

doc/lib/%.html: lib/%.sld $(CHIBI_DOC_DEPENDENCIES)
	$(MKDIR) $(dir $@)
	$(CHIBI_DOC) --html $(subst /,.,$*) > $@

clean-libs:
	$(RM) $(COMPILED_LIBS)
	$(RM) -r $(patsubst %,%.dSYM,$(COMPILED_LIBS))
	$(RM) $(HTML_LIBS)

dist-clean-libs: clean-libs
	$(RM) $(patsubst %.stub, %.c, $(shell $(FIND) lib -name \*.stub))

install-libs: all-libs
	for dir in $(dir $(patsubst lib/%,%,$(COMPILED_LIBS))) ; do \
	$(MKDIR) $(DESTDIR)$(BINMODDIR)/$$dir; \
	done
	for file in $(patsubst lib/%,%,$(COMPILED_LIBS)) ; do \
	$(INSTALL) lib/$$file $(DESTDIR)$(BINMODDIR)/$$file ; \
	done
	for dir in $(dir $(patsubst lib/%,%,$(SCM_LIBS))) ; do \
	$(MKDIR) $(DESTDIR)$(MODDIR)/$$dir; \
	done
	for file in $(patsubst lib/%,%,$(SCM_LIBS)) ; do \
	$(INSTALL) lib/$$file $(DESTDIR)$(MODDIR)/$$file ; \
	done

uninstall-libs:
	for file in $(patsubst lib/%,%,$(COMPILED_LIBS)) ; do \
	$(RM) $(DESTDIR)$(BINMODDIR)/$$file ; \
	done
	for file in $(patsubst lib/%,%,$(SCM_LIBS)) ; do \
	$(RM) $(DESTDIR)$(MODDIR)/$$file ; \
	done
