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

# Programs used by "make":
#
TRUE_CC = @CC@
CC	= @CC@
CFLAGS	= -c -I. -I$(srcdir) -I$(HDIR) -I../h @CPPFLAGS@ @CFLAGS@ @ECL_CFLAGS@ \
#	  -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@

# Files

HDIR	= $(top_srcdir)/h
HFILES	= ../h/config.h $(HDIR)/ecl.h $(HDIR)/ecl-cmp.h\
	$(HDIR)/object.h $(HDIR)/cs.h $(HDIR)/stacks.h\
	$(HDIR)/external.h $(HDIR)/eval.h\
	$(HDIR)/number.h $(HDIR)/page.h $(HDIR)/unify.h\
	$(HDIR)/lwp.h
OBJS	= main.o symbol.o package.o list.o\
	apply.o eval.o interpreter.o compiler.o disassembler.o \
	instance.o gfun.o reference.o character.o\
	file.o read.o print.o error.o string.o cfun.o\
	typespec.o assignment.o \
	predicate.o big.o number.o\
	num_pred.o num_comp.o num_arith.o num_sfun.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 \
	ffi.o @EXTRA_OBJS@

.SUFFIXES: .c .o .d

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

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

#
# 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.
#
external.h: $(top_srcdir)/h/external.h
	sed 's,__declspec(dllimport),,g' $(top_srcdir)/h/external.h > $@

install: $(HFILES)
	for i in $(HFILES); do $(INSTALL_DATA) $$i $(DESTDIR)$(libdir)/h/; done
	sed '/-CUT-/,$$d' ../h/config.h > ../h/config-install.h
	$(INSTALL_DATA) ../h/*.h $(DESTDIR)$(libdir)/h/
	rm $(DESTDIR)$(libdir)/h/config.h
	mv $(DESTDIR)$(libdir)/h/config-install.h $(libdir)/h/config.h

flatinstall: $(HFILES)
	for i in $(HFILES); do $(INSTALL_DATA) $$i $(DESTDIR)$(prefix)/h/; done
	sed '/-CUT-/,$$d' ../h/config.h > ../h/config-install.h
	$(INSTALL_DATA) ../h/*.h $(DESTDIR)$(prefix)/h/
	rm $(DESTDIR)$(prefix)/h/config.h
	mv $(DESTDIR)$(prefix)/h/config-install.h $(prefix)/h/config.h

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

clean:
	$(RM) dpp *.c *.h $(OBJS) ../libecl.a cinit.o core a.out

# Build rules

$(DPP): $(srcdir)/dpp.c symbols_list2.h
	if test -f ../CROSS-DPP; then touch dpp; else \
	$(TRUE_CC) @CFLAGS@ @ECL_CFLAGS@ -I../h -I. -I$(HDIR) $(DEFS) $(srcdir)/dpp.c  -o $@ ; \
	fi
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}};%' > $@

#
# 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
	$(CC) $(CFLAGS) -I. cinit.c -o $@
