#
# Makefile for octave's src directory
#
# John W. Eaton
# jwe@che.utexas.edu
# Department of Chemical Engineering
# The University of Texas at Austin

TOPDIR = ..

srcdir = @srcdir@
top_srcdir = @top_srcdir@
VPATH = @srcdir@

include $(TOPDIR)/Makeconf

INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
# Amiga hack - we have 'ln' and 'ln -s', but they seem to cause
# more problems than they solve.
LN = cp
# Cancel default rule for .c from .y and make .cc from .y instead.

%.c : %.y

%.cc : %.y
	$(YACC) $(YFLAGS) $<
	@$(top_srcdir)/move-if-change y.tab.c $(@F)

# Likewise for .c from .l.

%.c : %.l

%.cc : %.l
	$(LEX) $(LFLAGS) $< > $(@F)

XALL_CXXFLAGS_1 = $(subst -fexternal-templates, , $(ALL_CXXFLAGS))
XALL_CXXFLAGS = $(subst -fno-implicit-templates, , $(XALL_CXXFLAGS_1))

%.def : %.cc
	@echo making $@ from $<
	@$(CXXCPP) -c $(CPPFLAGS) $(XALL_CXXFLAGS) -DMAKE_BUILTINS $< \
	  | $(srcdir)/mkdefs > $@.tmp
	@if test `wc $@.tmp | sed 's%[^0-9]*\([0-9]*\).*%\1%'` -ne 0 ; then \
	  mv $@.tmp $@ ; \
	else \
	  echo "error: $@ is empty!" 1>&2 ; \
	  rm -f $@.tmp ; \
	  exit 1 ; \
	fi

DLD_SRC := f-balance.cc f-chol.cc f-colloc.cc f-dassl.cc f-det.cc \
	f-eig.cc f-expm.cc f-fft.cc f-fft2.cc f-find.cc \
	f-fsolve.cc f-fsqp.cc f-givens.cc f-hess.cc f-ifft.cc \
	f-ifft2.cc f-inv.cc f-log.cc f-lpsolve.cc f-lsode.cc f-lu.cc \
	f-minmax.cc f-npsol.cc f-pinv.cc f-qpsol.cc f-qr.cc f-quad.cc \
	f-qzval.cc f-rand.cc f-schur.cc f-sort.cc f-svd.cc f-syl.cc

DLD_OBJ := $(patsubst %.cc, %.o, $(DLD_SRC))

INCLUDES := arith-ops.h builtins.h defaults.h.in defun.h defun-dld.h \
	defun-int.h dirfns.h dynamic-ld.h error.h file-io.h fnmatch.h \
	getopt.h gripes.h help.h idx-vector.h input.h lex.h \
	load-save.h mappers.h missing-math.h octave.h octave-hist.h \
	oct-map.h oct-obj.h pager.h parse.h pr-output.h procstream.h \
	sighandlers.h statdefs.h symtab.h sysdep.h systime.h token.h \
	tree-base.h tree-cmd.h tree-const.h tree-expr.h tree-misc.h \
	tree-plot.h tc-inlines.h tc-rep.h unwind-prot.h user-prefs.h \
	utils.h variables.h version.h xdiv.h xpow.h Map.h SLStack.h \
	Stack.h

TI_SRC := Array-tc.cc Map-tc.cc DLList-fi.cc \
	SLList-expr.cc SLList-misc.cc SLList-plot.cc SLList-tc.cc \
	SLStack-i.cc SLStack-pc.cc SLStack-sym.cc SLStack-tok.cc \
	SLStack-tm.cc SLStack-ue.cc SLStack-ui.cc

TI_OBJ := $(patsubst %.cc, %.o, $(TI_SRC))

SOURCES := acosh.c arith-ops.cc asinh.c atanh.c data.cc dirfns.cc \
	dynamic-ld.cc erf.c erfc.c error.cc file-io.cc fnmatch.c \
	getopt.c getopt1.c gripes.cc help.cc idx-vector.cc input.cc \
	lex.l lgamma.c load-save.cc mappers.cc octave.cc \
	octave-hist.cc oct-obj.cc pager.cc parse.y pr-output.cc \
	procstream.cc sighandlers.cc strcasecmp.c strncase.c strfns.cc \
	symtab.cc sysdep.cc tc-rep.cc tc-rep-ass.cc tc-rep-idx.cc \
	tempname.c timefns.cc tempnam.c token.cc tree-base.cc \
	tree-cmd.cc tree-const.cc tree-expr.cc tree-misc.cc \
	tree-plot.cc unwind-prot.cc user-prefs.cc utils.cc \
	variables.cc xdiv.cc xpow.cc Map.cc SLStack.cc

OBJECTS_4 := $(SOURCES) builtins.o
OBJECTS_3 := $(patsubst %.l, %.o, $(OBJECTS_4))
OBJECTS_2 := $(patsubst %.y, %.o, $(OBJECTS_3))
OBJECTS_1 := $(patsubst %.c, %.o, $(OBJECTS_2))
OBJECTS := $(patsubst %.cc, %.o, $(OBJECTS_1))

# Ugh.

DEP_SOURCES_3 := $(SOURCES) $(TI_SOURCES) builtins.cc
DEP_SOURCES_2 := $(patsubst %.l, %.cc, $(DEP_SOURCES_3))
DEP_SOURCES_1 := $(patsubst %.y, %.cc, $(DEP_SOURCES_2))
DEP_SOURCES := $(patsubst %.c, %.d, $(DEP_SOURCES_1))
MAKEDEPS := $(patsubst %.cc, %.d, $(DEP_SOURCES))

DEF_FILES_5 := $(SOURCES) $(DLD_SRC)
DEF_FILES_4 := $(addprefix $(srcdir)/, $(DEF_FILES_5))
DEF_FILES_3 := $(notdir $(shell grep -l "^DEFUN" $(DEF_FILES_4)))
DEF_FILES_2 := $(patsubst %.y, %.def, $(DEF_FILES_3))
DEF_FILES_1 := $(patsubst %.l, %.def, $(DEF_FILES_2))
DEF_FILES := $(patsubst %.cc, %.def, $(DEF_FILES_1))

# Don't leave the LIBDLD thing on a line by itself.  It may expand to
# nothing.

OCTAVE_LIBS = @LIBOCTDLD@ ../liboctave/liboctave.a libtinst.a \
	../libcruft/libcruft.a ../info/libinfo.a \
	../readline/libreadline.a \
	../kpathsea/libkpathsea.a @LIBDLD@

DISTFILES = Makefile.in mkdefs mkbuiltins \
	parse.cc lex.cc y.tab.h $(INCLUDES) $(DLD_SRC) $(SOURCES) \
	$(TI_SRC) 

all: defaults.h octave
.PHONY: all

octave: $(OBJECTS) $(DLD_OBJ) $(OCTAVE_LIBS) liboctdld.a
	$(CXX) $(CPPFLAGS) $(ALL_CXXFLAGS) $(ALL_LDFLAGS) -o octave \
	$(OBJECTS) \
	$(OCTAVE_LIBS) \
	$(FLIBS) $(LEXLIB) -ltermcap -lm -lg++

liboctdld.a: $(DLD_OBJ)
	$(AR) $(ARFLAGS) liboctdld.a $(DLD_OBJ)
	$(RANLIB) liboctdld.a

libtinst.a: $(TI_OBJ)
	$(AR) $(ARFLAGS) libtinst.a $(TI_OBJ)
	$(RANLIB) libtinst.a

builtins.cc: $(DEF_FILES) mkbuiltins
	@echo making $@ from $(DEF_FILES)
	@$(srcdir)/mkbuiltins $(DEF_FILES) > $@.tmp
	@$(top_srcdir)/move-if-change $@.tmp $@

$(DEF_FILES): mkdefs defun-int.h defun-dld.h defun.h defaults.h

$(MAKEDEPS): defaults.h

check: all
.PHONY: check

install: all
	$(top_srcdir)/mkinstalldirs $(bindir) $(libdir)
	rm -f $(bindir)/octave
	$(INSTALL) octave $(bindir)/octave-$(version)
	$(LN) $(bindir)/octave-$(version) $(bindir)/octave
	if $(OCTAVE_LITE) && $(WITH_DLD) ; then \
	  $(INSTALL_DATA) liboctdld.a $(libdir)/liboctdld.a ; \
	else \
	  true ; \
	fi
.PHONY: install

uninstall:
	rm -f $(bindir)/octave
.PHONY: uninstall

tags: $(SOURCES) $(DLD_SRC) $(TI_SRC)
	ctags $(SOURCES) $(DLD_SRC) $(TI_SRC)

TAGS: $(SOURCES) $(DLD_SRC) $(TI_SRC)
	etags $(SOURCES) $(DLD_SRC) $(TI_SRC)

clean:
	rm -f *.a *.o builtins.cc
.PHONY: clean

mostlyclean:
	rm -f *.o
.PHONY: mostlyclean

distclean: clean
	rm -f Makefile octave .fname *.d *.def
.PHONY: distclean

realclean: distclean
	rm -f tags TAGS y.tab.c y.tab.h y.output yy.lex.c \
	lex.cc parse.cc defaults.h *.d *.def
.PHONY: realclean

local-dist: parse.cc lex.cc
	$(LN) $(DISTFILES) ../`cat ../.fname`/src
	rm -f parse.cc lex.cc y.tab.h y.output yy.lex.c
	rm -f lex.cc parse.cc defaults.h *.d *.def
.PHONY: local-dist

dist: parse.cc lex.cc
	$(LN) $(DISTFILES) ../`cat ../.fname`/src
	rm -f parse.cc lex.cc y.tab.h y.output yy.lex.c
	rm -f lex.cc parse.cc defaults.h *.d *.def builtins.cc
.PHONY: dist

# Special rules -- these files need special things to be defined.

defaults.h: defaults.h.in ../Makeconf Makefile
	@echo "making defaults.h from defaults.h.in"
	@(sed < $< > $@.tmp \
	  -e "s;%DEFAULT_PAGER%;\"${DEFAULT_PAGER}\";" \
	  -e "s;%OCTAVE_PREFIX%;\"${prefix}\";" \
	  -e "s;%OCTAVE_EXEC_PREFIX%;\"${exec_prefix}\";" \
	  -e "s;%OCTAVE_DATADIR%;\"${datadir}\";" \
	  -e "s;%OCTAVE_BINDIR%;\"${bindir}\";" \
	  -e "s;%OCTAVE_LIBDIR%;\"${libdir}\";" \
	  -e "s;%OCTAVE_INFODIR%;\"${infodir}\";" \
	  -e "s;%OCTAVE_FCNFILEDIR%;\"${fcnfiledir}\";" \
	  -e "s;%OCTAVE_LOCALFCNFILEPATH%;\"${localfcnfilepath}\";" \
	  -e "s;%OCTAVE_ARCHLIBDIR%;\"${archlibdir}\";" \
	  -e "s;%OCTAVE_OCTFILEDIR%;\"${octfiledir}\";" \
	  -e "s;%OCTAVE_LOCALOCTFILEPATH%;\"${localoctfilepath}\";" \
	  -e "s;%OCTAVE_FCNFILEPATH%;\"${fcnfilepath}\";" \
	  -e "s;%OCTAVE_IMAGEPATH%;\"${imagepath}\";" \
	  -e "s;%TARGET_HOST_TYPE%;\"${target_host_type}\";" \
	  -e "s;%FLIB_LIST%;\"${FLIB_LIST}\";" \
	  -e "s;%FLIB_PATH%;\"${FLIB_PATH}\";" \
	  -e "s;%CXXLIB_LIST%;\"${CXXLIB_LIST}\";" \
	  -e "s;%CXXLIB_PATH%;\"${CXXLIB_PATH}\";")
	@$(top_srcdir)/move-if-change $@.tmp $@

check: all
.PHONY: check

-include $(MAKEDEPS)
