# -*- Mode: Makefile; indent-tabs-mode: nil -*-
# vim: set filetype=makefile tabstop=8 shiftwidth=4 expandtab:

#
#			Makefile for ECL core library
#
top_srcdir= @top_srcdir@
srcdir	= @srcdir@
VPATH	= @srcdir@
builddir= @true_builddir@

# Programs used by "make":
#
CC	= @CC@
TRUE_CC = $(CC)
CFLAGS = -Wall -Wno-missing-braces -I. -I@true_builddir@ -I$(srcdir)    \
-I../ecl/ -g3 -rdynamic -DECL_BUILD -DECL_API -DECL_NO_LEGACY @CPPFLAGS@ @CFLAGS@     \
@ECL_CFLAGS@ -c

# The following flags could be added and used by GCC
#  -Wall -W -Wfloat-equal -Wundef -Wendif-labels -Wpointer-arith -Wcast-align \
#  -Wwrite-strings -Wconversion -Wsign-compare -Wmissing-prototypes \
#  -Wredundant-decls -Wunreachable-code -Winline

SHELL	= /bin/sh
RM	= @RM@
EXE	= @EXEEXT@
DPP	= ./dpp$(EXE)
RANLIB	= @RANLIB@
AR	= @AR@

# Data for installation
#
INSTALL = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@

# Files

HDIR	= ../ecl

HFILES = $(HDIR)/config.h $(HDIR)/ecl.h $(HDIR)/ecl-cmp.h $(HDIR)/object.h      \
	$(HDIR)/cs.h $(HDIR)/stacks.h $(HDIR)/external.h $(HDIR)/cons.h         \
	$(HDIR)/legacy.h $(HDIR)/number.h $(HDIR)/page.h $(HDIR)/bytecodes.h    \
	$(HDIR)/cache.h $(HDIR)/config-internal.h $(HDIR)/ecl_atomics.h         \
	$(HDIR)/ecl-inl.h $(HDIR)/internal.h $(HDIR)/stack-resize.h             \
	$(HDIR)/threads.h $(HDIR)/impl/math_dispatch2.h                         \
	$(HDIR)/impl/math_dispatch.h $(HDIR)/impl/math_fenv.h                   \
	$(HDIR)/impl/math_fenv_msvc.h $(HDIR)/nucleus.h

BOOT_OBJS = boot.o escape.o eql.o module.o stacks.o

CLOS_OBJS = clos/cache.o clos/accessor.o clos/instance.o clos/gfun.o

NUM_OBJS = number.o num_pred.o num_arith.o num_co.o num_log.o num_rand.o        \
	numbers/cos.o numbers/sin.o numbers/tan.o numbers/atan.o                \
	numbers/cosh.o numbers/sinh.o numbers/tanh.o numbers/exp.o              \
	numbers/expt.o numbers/log.o numbers/sqrt.o numbers/abs.o               \
	numbers/zerop.o numbers/plusp.o numbers/minusp.o numbers/negate.o       \
	numbers/conjugate.o numbers/one_plus.o numbers/one_minus.o              \
	numbers/plus.o numbers/minus.o numbers/times.o numbers/divide.o         \
	numbers/number_compare.o numbers/number_equalp.o numbers/minmax.o       \
	numbers/floor.o numbers/ceiling.o numbers/round.o numbers/truncate.o

WRITER_OBJS = print.o printer/float_to_digits.o printer/float_to_string.o       \
	printer/integer_to_string.o printer/write_ugly.o                        \
	printer/write_object.o printer/write_symbol.o printer/write_array.o     \
	printer/write_list.o printer/write_code.o printer/write_sse.o           \
	printer/print_unreadable.o

READER_OBJS = reader.o read.o reader/rtab_cl.o reader/parse_token.o             \
        reader/parse_fixnum.o reader/parse_integer.o reader/parse_number.o

STREAM_OBJS = stream.o file.o streams/strm_os.o streams/strm_clos.o             \
        streams/strm_string.o streams/strm_composite.o streams/strm_common.o    \
        streams/strm_sequence.o streams/strm_eformat.o streams/strm_binary.o

FFI_OBJS = ffi.o ffi/libraries.o ffi/backtrace.o ffi/mmap.o ffi/cdata.o

THREAD_OBJS = @THREAD_OBJS@

UNICODE_OBJS = @UNICODE_OBJS@

OBJS = main.o symbol.o package.o cons.o list.o apply.o eval.o interpreter.o     \
	compiler.o disassembler.o reference.o character.o error.o               \
	string.o cfun.o typespec.o assignment.o memory.o predicate.o array.o    \
	vector_push.o sequence.o cmpaux.o macros.o backq.o stack2.o time.o      \
	unixint.o mapfun.o multival.o hash.o format.o pathname.o structure.o    \
	load.o unixfsys.o unixsys.o serialize.o sse2.o atomic.o process.o lwp.o \
	$(BOOT_OBJS) $(NUM_OBJS) $(WRITER_OBJS) $(READER_OBJS) $(STREAM_OBJS)   \
	$(CLOS_OBJS) $(FFI_OBJS) $(THREAD_OBJS) $(UNICODE_OBJS) @EXTRA_OBJS@

.PHONY: all

ifndef VERBOSE
.SILENT:
endif

all: $(DPP) ../libeclmin.a ../cinit.o

$(HDIR)/impl/%.h: $(top_srcdir)/h/impl/%.h
	mkdir -p $(HDIR)/impl
	cp $< $@

$(HDIR)/%.h: $(top_srcdir)/h/%.h
	cp $< $@

%.c: %.d $(DPP)
	if test -f ../CROSS-DPP ; then ../CROSS-DPP $< $@ ; else $(DPP) $< $@ ; fi
%.o: %.d $(DPP) $(HFILES)
	if test -f ../CROSS-DPP ; then ../CROSS-DPP $< $@.c ; else $(DPP) $< $@.c ; fi
	$(CC) -DECLDIR="\"@ecldir@\"" $(CFLAGS) -o $@ $@.c
%.o: %.c $(HFILES)
	$(CC) -DECLDIR="\"@ecldir@\"" $(CFLAGS) -o $@ $<

../libeclmin.a: $(OBJS) all_symbols.o all_symbols2.o
	$(RM) $@
	$(AR) cr $@ $(OBJS)
	$(RANLIB) $@

clean:
	$(RM) $(DPP) *.c *.h $(OBJS) all_symbols.o all_symbols2.o ../libecl.a ../cinit.o core a.out tmp.c

# Build rules

$(DPP): $(srcdir)/dpp.c $(srcdir)/symbols_list.h
	if test -f ../CROSS-DPP; then touch $(DPP); else \
	$(TRUE_CC) -I$(srcdir) -I@true_builddir@ -I./ $(srcdir)/dpp.c @CPPFLAGS@ @CFLAGS@ @ECL_CFLAGS@ @LDFLAGS@ -o $@ ; \
	fi

$(OBJS): $(DPP)

#
# This reduces the overhead of jumping to other functions
#
# apply.c: $(DPP)
# apply.o: apply.c $(HFILES)
# 	$(CC) $(CFLAGS) apply.c -o $@

#
# These files are interrelated
#
all_symbols.c: $(DPP)
all_symbols.o: all_symbols.c
	$(CC) $(CFLAGS) -I./ all_symbols.c -o $@
all_symbols2.o: all_symbols.c
	$(CC) $(CFLAGS) -DECL_FINAL -I./ all_symbols.c -o $@

#
# This is in another directory
#
cinit.c: $(DPP)
../cinit.o: cinit.c $(HFILES)
	$(CC) $(CFLAGS) -I./ cinit.c -o $@

#
# Experimental runtimes
#
NUCL_CFLG = -DECL_NUCL -DECL_BUILD -DGC_NO_THREAD_REDIRECTS \
         -I$(builddir) -I$(srcdir) -g3 -rdynamic

PUCL_SRCS = boot.c escape.c module.c stacks.c eql.c \
         memory.c atomic.c process.c apply.c interpreter.c

NUCL_SRCS = boot.c escape.c module.c stacks.c eql.c \
         memory.c atomic.c process.c apply.c interpreter.c stream.c \
         streams/strm_nucl.c

pucl: $(PUCL_SRCS) pucl.c
	$(CC) $(NUCL_CFLG) -o $@ $^

nucl: $(NUCL_SRCS) nucl.c
	$(CC) $(NUCL_CFLG) -o $@ $^

r2rs: $(NUCL_SRCS) $(UNICODE_OBJS) character.c reader.c reader/parse_fixnum.c r2rs.c
	$(CC) $(NUCL_CFLG) -o $@ $^
