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

# Programs used by "make":
#
CC	= @CC@
TRUE_CC = $(CC)
CFLAGS	= -I. -I@true_builddir@ -I$(srcdir) -I../ecl/gc -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@

# 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)/unify.h
OBJS	= main.o symbol.o package.o cons.o list.o\
	apply.o eval.o interpreter.o compiler.o disassembler.o \
	instance.o gfun.o clos/cache.o clos/accessor.o \
	reference.o character.o\
	file.o read.o print.o error.o string.o cfun.o\
	reader/parse_integer.o reader/parse_number.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 \
	ffi/libraries.o ffi/backtrace.o ffi/mmap.o ffi/cdata.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 \
	typespec.o assignment.o \
	predicate.o number.o\
	num_pred.o num_arith.o num_co.o\
	num_log.o num_rand.o array.o sequence.o cmpaux.o\
	macros.o backq.o stacks.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 ffi.o sse2.o @EXTRA_OBJS@

.SUFFIXES: .c .o .d .s
.PHONY: all

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

.d.c: $(DPP)
	if test -f ../CROSS-DPP ; then ../CROSS-DPP $< $@ ; else $(DPP) $< $@ ; fi
.d.o: $(DPP)
	if test -f ../CROSS-DPP ; then ../CROSS-DPP $< $@.c ; else $(DPP) $< $@.c ; fi
	$(CC) -DECLDIR="\"@ecldir@\"" $(CFLAGS) -o $@ $@.c
.c.o:
	$(CC) -DECLDIR="\"@ecldir@\"" $(CFLAGS) -o $@ $<
.d.s: $(HFILES)
	if test -f ../CROSS-DPP ; then ../CROSS-DPP $< $@.c ; else $(DPP) $< $@.c ; fi
	$(CC) -DECLDIR="\"@ecldir@\"" $(CFLAGS) -S -o $@ $@.c
#.c.o: $(HFILES)
#	$(CC) -DECLDIR="\"@ecldir@\"" $(CFLAGS) -o $@ $<
apply_x86.c: $(srcdir)/arch/apply_x86.d $(DPP) $(HFILES)
	if test -f ../CROSS-DPP ; then \
	  ../CROSS-DPP $(srcdir)/arch/apply_x86.d $@ ; \
	else $(DPP) $(srcdir)/arch/apply_x86.d  $@ ; \
	fi
ffi_x86.c: $(srcdir)/arch/ffi_x86.d $(DPP) $(HFILES)
	if test -f ../CROSS-DPP ; then \
	  ../CROSS-DPP $(srcdir)/arch/ffi_x86.d $@ ; \
	else $(DPP) $(srcdir)/arch/ffi_x86.d  $@ ; \
	fi
ffi_x86_64.c: $(srcdir)/arch/ffi_x86_64.d $(DPP) $(HFILES)
	if test -f ../CROSS-DPP ; then \
	  ../CROSS-DPP $(srcdir)/arch/ffi_x86_64.d $@ ; \
	else $(DPP) $(srcdir)/arch/ffi_x86_64.d  $@ ; \
	fi

../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_list2.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@ -o $@ ; \
	fi

#
# symbols_list2.h is built this way to allow for an atomic replacement of
# the file. Otherwise we have problem when doing concurrent builds with
# rsync updates of the source tree.
#
$(srcdir)/symbols_list2.h: $(srcdir)/symbols_list.h Makefile
	cat $(srcdir)/symbols_list.h | \
	sed -e 's%{\([A-Z ]*.*".*"\),[^,]*,[ ]*NULL,.*}%{\1,NULL}%g' \
	    -e 's%{\([A-Z ]*.*".*"\),[^,]*,[ ]*\([^,]*\),.*}%{\1,"\2"}%g' \
	    -e 's%{NULL.*%{NULL,NULL}};%' | \
	sed -e 's%"\(IF_[A-Z0-9]*\)(\([^)]*\))"%\1("\2")%g' > tmp.h
	mv tmp.h $@

#
# GCC might break this code
#
gbc.o: gbc.c $(HFILES)
	$(CC) $(CFLAGS) -O0 gbc.c -o $@
#
# This reduces the overhead of jumping to other functions
#
apply.o: apply.c $(HFILES) $(HDIR)/cs.h
	$(CC) $(CFLAGS) apply.c -o $@
#
# These files are interrelated
#
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.o: cinit.c $(HFILES)
	$(CC) $(CFLAGS) -I./ cinit.c -o $@
