#
#		Makefile for ECoLisp
#
top_srcdir= @top_srcdir@
srcdir	= @srcdir@

# Programs used by "make":
#

@SET_MAKE@
CC	= @ECL_CC@
LIBS	= @LIBS@
FASL_LIBS = @FASL_LIBS@
CORE_LIBS = @CORE_LIBS@
LDFLAGS	= @LDFLAGS@
RM	= @RM@
LN_S	= @LN_S@
EXE	= @EXEEXT@

# ==================== Where To Install Things ====================

# The default location for installation.  Everything is placed in
# subdirectories of this directory.  The default values for many of
# the variables below are expressed in terms of this one, so you may
# not need to change them.  This defaults to /usr/local.
prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
libdir=@libdir@
includedir=@includedir@

# Programs used by "make install":
#
SHELL = @SHELL@
INSTALL	= @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_SCRIPT = @INSTALL_SCRIPT@
INSTALL_DATA = @INSTALL_DATA@
mkinstalldirs = $(SHELL) $(top_srcdir)/gc/mkinstalldirs

# Files

SUBDIRS = @SUBDIRS@
LIBRARIES = @LIBRARIES@
LSP_LIBRARIES = @LSP_LIBRARIES@
TARGETS = @TARGETS@

all:	$(TARGETS) bin/ecl-config doc
.PHONY:	all

%Makefile: $(srcdir)/%Makefile.in config.status
	./config.status

#
# When compiling the library itself, we have to remove the dllimport
# declarations, because the variables that they mark are in the
# in the library and can be referenced without indirection.
#
c/ecl/external.h: $(top_srcdir)/h/external.h
	test -d c/ecl || mkdir c/ecl
	sed 's,__declspec(dllimport),,g' $(top_srcdir)/h/external.h > $@
ecl/external.h: c/ecl/external.h c/ecl/external.h
	cp $(srcdir)/h/*.h ecl/
	if grep 'undef ENABLE_DLOPEN' ecl/config.h; then cp c/external.h h; fi

bin/ecl$(EXE): ecl_min$(EXE) compile.lsp sysfun.lsp ecl/external.h BUILD-STAMP
	if [ -f CROSS-COMPILER ]; then \
		./CROSS-COMPILER compile; \
	else \
		./ecl_min compile; \
	fi

ecl_min$(EXE): $(LIBRARIES) .gdbinit @LIBPREFIX@eclmin.@LIBEXT@
	if [ -f CROSS-COMPILER ]; then \
		touch $@; \
	else \
		$(CC) $(LDFLAGS) -o $@ cinit.o c/all_symbols.o -L./ @LIBPREFIX@eclmin.@LIBEXT@ $(CORE_LIBS) $(LIBS) $(FASL_LIBS);\
	fi

.gdbinit: $(srcdir)/util/gdbinit
	cp $(srcdir)/util/gdbinit $@

lsp/config.lsp: lsp/config.pre
	sed -e 's,@ecldir@,$(libdir),g' < lsp/config.pre > lsp/config.lsp
cmp/cmpdefs.lsp: cmp/cmpdefs.pre
	sed -e 's,@ecldir@,"$(libdir)",g' \
	    -e 's,@ecllibdir@,"$(libdir)",g' \
	    -e 's,@eclincludedir@,"$(includedir)",g' < cmp/cmpdefs.pre > $@
compile.lsp: compile.pre
	sed -e 's,@ecldir@,$(libdir),g' < compile.pre > compile.lsp
bin/ecl-config: bin/ecl-config.pre
	sed -e 's,~A,$(libdir),;s,~\*,,' \
	    -e 's,@ecllibdir\\@,$(libdir),' \
	    -e 's,@eclincludedir\\@,$(includedir),' bin/ecl-config.pre > bin/ecl-config


@LIBPREFIX@eclmin.@LIBEXT@: @LIBPREFIX@eclgmp.@LIBEXT@ @LIBPREFIX@eclgc.@LIBEXT@ lsp/config.lsp cmp/cmpdefs.lsp ecl/external.h
	cd c; $(MAKE)
@LIBPREFIX@eclgc.@LIBEXT@:
	test -d ecl/gc || mkdir ecl/gc
	test -d ecl/gc/private || mkdir ecl/gc/private
	if (echo $(SUBDIRS) | grep gc); then \
	  cd gc && $(MAKE) install && cd .. && \
	  cp -rf $(srcdir)/gc/include/gc_mark.h ecl/gc/ && \
	  cp -rf $(srcdir)/gc/include/private/*.h ecl/gc/private/ && \
	  mv @LIBPREFIX@gc.@LIBEXT@ @LIBPREFIX@eclgc.@LIBEXT@; \
	fi
@LIBPREFIX@eclgmp.@LIBEXT@:
	if (echo $(SUBDIRS) | grep gmp); then \
	  cd gmp && $(MAKE) install && \
	  cd .. && mv h/gmp.h ecl/ && rmdir h &&  \
	  mv ./@LIBPREFIX@gmp.@LIBEXT@ ./@LIBPREFIX@eclgmp.@LIBEXT@; \
	fi
sysfun.lsp:
	$(LN_S) $(srcdir)/cmp/sysfun.lsp ./

rt.lisp:
	cp $(srcdir)/../contrib/rt/rt.lisp ./

BUILD-STAMP: config.status
	echo "#" `uname -a` > $@
	head -8 config.log | tail -6 >> $@
install: install-base
	cd doc; $(MAKE) prefix=$(prefix) DESTDIR=$(DESTDIR) install
install-base:
	# Here we would use the option -s but the install program in sourceforge-solaris
	# is broken.
	$(mkinstalldirs) $(DESTDIR)$(bindir) $(DESTDIR)$(libdir)
	for i in $(TARGETS); do \
	  $(INSTALL_PROGRAM) $$i $(DESTDIR)$(bindir); \
	done;
	$(mkinstalldirs) $(DESTDIR)$(includedir)/ecl/gc/private
	sed '/-CUT-/,$$d' ./ecl/config.h > ./ecl/config-install.h
	$(INSTALL_DATA) ./ecl/*.h $(DESTDIR)$(includedir)/ecl
	if (echo $(SUBDIRS) | grep gc); then \
	  $(INSTALL_DATA) ./ecl/gc/*.h $(DESTDIR)$(includedir)/ecl/gc; \
	  $(INSTALL_DATA) ./ecl/gc/private/*.h $(DESTDIR)$(includedir)/ecl/gc/private; \
	fi
	rm $(DESTDIR)$(includedir)/ecl/config.h
	mv $(DESTDIR)$(includedir)/ecl/config-install.h $(DESTDIR)$(includedir)/ecl/config.h
	$(INSTALL_SCRIPT) bin/ecl-config $(DESTDIR)$(bindir)
	for i in BUILD-STAMP help.doc ; do \
	  $(INSTALL_DATA) $$i $(DESTDIR)$(libdir); \
	done
	for i in $(LSP_LIBRARIES) $(LIBRARIES) c/dpp* ecl_min*; do \
	  test -s $$i && $(INSTALL_PROGRAM) $$i $(DESTDIR)$(libdir) || : ; \
	done
	for i in `cat MODULES`; do \
	  $(INSTALL_PROGRAM) $$i $(DESTDIR)$(libdir); \
	done
flatinstall: BUILD-STAMP
	$(MAKE) DESTDIR=$(DESTDIR) bindir=$(prefix) libdir=$(prefix) includedir=$(prefix) install-base
	cd doc; $(MAKE) DESTDIR=$(DESTDIR) prefix=$(prefix) docdir=$(prefix)/doc flatinstall

uninstall:
	for i in $(TARGETS) ecl-config; do $(RM) $(DESTDIR)$(bindir)/$$i; done
	$(RM) -r $(DESTDIR)$(libdir)
	cd doc; $(MAKE) uninstall

doc: $(TARGETS)
	cd doc; $(MAKE)

clean: clean_lisp
	for i in $(SUBDIRS); do (cd $$i; $(MAKE) clean); done
	$(RM) ecl_min$(EXE) bin/ecl$(EXE) help.doc core a.out
	$(RM) config.version config.log config.cache
	$(RM) *.c *.o *.a *.h *.data *.fas
clean_lisp:
	for i in lsp cmp clos clx tk ext; do $(RM) lib$$i.a $$i/?*.{o,data,c,sdat,h,fas}; done
	$(RM) help.doc
distclean: clean
realclean: distclean
test1:
	cd c; $(MAKE)
	$(MAKE) ecl_min
	$(MAKE) ecl
	cd tests; $(MAKE)
	diff tests tests2
test2:
	$(MAKE) clean_lisp
	cd c; $(MAKE)
	$(MAKE) ecl_min
	$(RM) ecl
	$(MAKE) ecl
	for i in lsp clos cmp; do diff --exclude=\*.o $$i old/$$i; done
test3:
	test -d stage2 || mkdir stage2
	cp -rf lsp clos cmp stage2
	-for i in lsp cmp clos clx tk; do test -f lib$$i.a && mv lib$$i.a stage2; done
	$(MAKE) clean_lisp
	./ecl compile
	-for i in lsp clos cmp clx tk; do test -d $$i && diff --exclude=\*.o $$i stage2/$$i; done | less
test:
	$(MAKE) -C tests
	$(MAKE) -C ansi-tests > ansi-tests/log
	#-(diff tests ~/src/tests; diff --exclude log ansi-tests ~/src/ansi-tests) | less

TAGS:
	etags -R --langmap=c:+.d
