#
# Makefile for octave's liboctave directory
#
# John W. Eaton
# jwe@bevo.che.wisc.edu
# University of Wisconsin-Madison
# Department of Chemical Engineering

TOPDIR = ..

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

include $(TOPDIR)/Makeconf

INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@

ifeq ($(INCLUDE_LINK_DEPS), true)
  LINK_DEPS = \
    -L../libcruft -L../kpathsea -L../glob -L. $(RLD_FLAG) \
    $(LIBCRUFT) $(BLAS_LIBS) $(FFTW_LIBS) $(LIBREADLINE) \
    -lglob $(LIBS) $(LIBKPATHSEA) $(FLIBS)
endif

MATRIX_INC := Array.h Array2.h Array3.h ArrayN.h DiagArray2.h Array-flags.h \
	Array-idx.h Array2-idx.h Array3-idx.h ArrayN-idx.h MArray-defs.h \
	MArray.h MArray2.h MDiagArray2.h Matrix.h \
	base-lu.h mx-base.h mx-op-defs.h mx-defs.h mx-ext.h \
	CColVector.h CDiagMatrix.h CMatrix.h CRowVector.h CmplxAEPBAL.h \
	CmplxCHOL.h CmplxDET.h CmplxHESS.h CmplxLU.h CmplxQR.h \
	CmplxQRP.h CmplxSCHUR.h CmplxSVD.h EIG.h boolMatrix.h chMatrix.h \
	dColVector.h dDiagMatrix.h dMatrix.h dRowVector.h dbleAEPBAL.h \
	dbleCHOL.h dbleDET.h dbleHESS.h dbleLU.h dbleQR.h \
	dbleQRP.h dbleSCHUR.h dbleSVD.h

MX_OP_INC := mx-cdm-cm.h mx-cdm-cs.h mx-cdm-dm.h mx-cdm-m.h \
	mx-cdm-s.h mx-cm-cdm.h mx-cm-dm.h mx-cm-m.h mx-cm-s.h \
	mx-cs-cdm.h mx-cs-dm.h mx-cs-m.h mx-dm-cdm.h mx-dm-cm.h \
	mx-dm-cs.h mx-dm-m.h mx-dm-s.h mx-m-cdm.h mx-m-cm.h \
	mx-m-cs.h mx-m-dm.h mx-s-cdm.h mx-s-cm.h mx-s-dm.h

VX_OP_INC := vx-ccv-s.h vx-crv-s.h vx-cs-cv.h vx-cs-rv.h vx-cv-cs.h \
	vx-rv-cs.h vx-s-ccv.h vx-s-crv.h \
	vx-rv-crv.h vx-cv-ccv.h vx-crv-rv.h vx-ccv-cv.h

OPTS_INC_DATA := DASPK-opts.in DASRT-opts.in DASSL-opts.in \
	LSODE-opts.in NLEqn-opts.in ODESSA-opts.in Quad-opts.in

OPTS_INC := $(OPTS_INC_DATA:.in=.h)

INCLUDES := Bounds.h CollocWt.h DAE.h DAEFunc.h DAERT.h \
	DAERTFunc.h DASPK.h DASRT.h DASSL.h FEGrid.h \
	LinConst.h LP.h LPsolve.h LSODE.h NLConst.h NLEqn.h \
	NLFunc.h NLP.h ODE.h ODEFunc.h ODES.h ODESFunc.h \
	ODESSA.h Objective.h QP.h Quad.h Range.h base-dae.h \
	base-de.h base-min.h byte-swap.h cmd-edit.h cmd-hist.h \
	data-conv.h dir-ops.h file-ops.h file-stat.h getopt.h \
	glob-match.h idx-vector.h lo-ieee.h lo-mappers.h \
	lo-specfun.h lo-sstream.h lo-sysdep.h lo-utils.h \
	mach-info.h oct-alloc.h oct-cmplx.h oct-env.h oct-fftw.h \
	oct-getopt.h oct-group.h oct-kpse.h oct-passwd.h \
	oct-rl-edit.h oct-rl-hist.h oct-shlib.h oct-syscalls.h \
	oct-time.h pathlen.h pathsearch.h prog-args.h statdefs.h \
	str-vec.h sun-utils.h sysdir.h systime.h syswait.h \
	$(OPTS_INC) \
	$(MATRIX_INC) \
	$(MX_OP_INC) \
	$(VX_OP_INC)

TEMPLATE_SRC := Array.cc Array2.cc Array3.cc ArrayN.cc DiagArray2.cc \
	MArray.cc MArray2.cc MDiagArray2.cc base-lu.cc

TI_SRC := Array-C.cc Array-b.cc Array-ch.cc Array-i.cc Array-d.cc \
	Array-s.cc Array-str.cc Array-idx-vec.cc \
	MArray-C.cc MArray-ch.cc MArray-i.cc MArray-d.cc MArray-s.cc

MATRIX_SRC := Array-flags.cc CColVector.cc CDiagMatrix.cc CMatrix.cc \
	CRowVector.cc CmplxAEPBAL.cc CmplxCHOL.cc CmplxDET.cc \
	CmplxHESS.cc CmplxLU.cc CmplxQR.cc CmplxQRP.cc CmplxSCHUR.cc \
	CmplxSVD.cc EIG.cc MArray-misc.cc boolMatrix.cc chMatrix.cc \
	dColVector.cc dDiagMatrix.cc dMatrix.cc dRowVector.cc \
	dbleAEPBAL.cc dbleCHOL.cc dbleDET.cc dbleHESS.cc dbleLU.cc \
	dbleQR.cc dbleQRP.cc dbleSCHUR.cc dbleSVD.cc

MX_OP_SRC := mx-cdm-cm.cc mx-cdm-cs.cc mx-cdm-dm.cc mx-cdm-m.cc \
	mx-cdm-s.cc mx-cm-cdm.cc mx-cm-dm.cc mx-cm-m.cc mx-cm-s.cc \
	mx-cs-cdm.cc mx-cs-dm.cc mx-cs-m.cc mx-dm-cdm.cc mx-dm-cm.cc \
	mx-dm-cs.cc mx-dm-m.cc mx-dm-s.cc mx-m-cdm.cc mx-m-cm.cc \
	mx-m-cs.cc mx-m-dm.cc mx-s-cdm.cc mx-s-cm.cc mx-s-dm.cc

VX_OP_SRC := vx-ccv-s.cc vx-crv-s.cc vx-cs-cv.cc vx-cs-rv.cc vx-cv-cs.cc \
	vx-rv-cs.cc vx-s-ccv.cc vx-s-crv.cc \
	vx-rv-crv.cc vx-cv-ccv.cc vx-crv-rv.cc vx-ccv-cv.cc

LIBOCTAVE_CXX_SOURCES := Bounds.cc CollocWt.cc \
	DASPK.cc DASRT.cc DASSL.cc FEGrid.cc LinConst.cc \
	LPsolve.cc LSODE.cc NLEqn.cc ODES.cc ODESSA.cc \
	Quad.cc Range.cc data-conv.cc dir-ops.cc \
	file-ops.cc file-stat.cc glob-match.cc idx-vector.cc \
	lo-ieee.cc lo-mappers.cc lo-specfun.cc lo-sysdep.cc \
	lo-utils.cc mach-info.cc oct-alloc.cc oct-env.cc \
	oct-fftw.cc oct-group.cc oct-passwd.cc oct-shlib.cc \
	oct-syscalls.cc oct-time.cc prog-args.cc str-vec.cc \
	$(TEMPLATE_SRC) \
	$(TI_SRC) \
	$(MATRIX_SRC) \
	$(MX_OP_SRC) \
	$(VX_OP_SRC)

LIBOCTAVE_C_SOURCES := f2c-main.c filemode.c getopt.c getopt1.c \
	lo-cieee.c lo-cutils.c mkdir.c oct-getopt.c rename.c \
	rmdir.c strftime.c strptime.c tempname.c tempnam.c

LIBOCTAVE_SOURCES := $(LIBOCTAVE_CXX_SOURCES) $(LIBOCTAVE_C_SOURCES)

LIBOCT_READLINE_CXX_SOURCES := cmd-edit.cc cmd-hist.cc

LIBOCT_READLINE_C_SOURCES := oct-rl-edit.c oct-rl-hist.c

LIBOCT_READLINE_SOURCES := $(LIBOCT_READLINE_CXX_SOURCES) $(LIBOCT_READLINE_C_SOURCES)

LIBOCT_PATHSEARCH_CXX_SOURCES := pathsearch.cc

LIBOCT_PATHSEARCH_C_SOURCES := oct-kpse.c

LIBOCT_PATHSEARCH_SOURCES := \
	$(LIBOCT_PATHSEARCH_C_SOURCES) $(LIBOCT_PATHSEARCH_CXX_SOURCES)

SOURCES := \
	$(LIBOCTAVE_SOURCES) \
	$(LIBOCT_READLINE_SOURCES) \
	$(LIBOCT_PATHSEARCH_SOURCES)

EXTRAS := mx-inlines.cc

INCLUDES_FOR_INSTALL := $(INCLUDES) $(TEMPLATE_SRC) $(EXTRAS)

DISTFILES := Makefile.in ChangeLog $(SOURCES) $(INCLUDES) $(EXTRAS) \
	$(OPTS_INC_DATA)

ifeq ($(SHARED_LIBS), true)
  BINDISTLIBS = liboctave/liboctave
  BINDISTFILES = liboctave.$(SHLEXT_VER)
endif

MAKEDEPS_1 := $(patsubst %.cc, %.d, $(SOURCES))
MAKEDEPS := $(patsubst %.c, %.d, $(MAKEDEPS_1))

LIBOCTAVE_OBJECTS := \
	$(LIBOCTAVE_CXX_SOURCES:.cc=.o) \
	$(LIBOCTAVE_C_SOURCES:.c=.o) \
	$(LIBOCT_READLINE_CXX_SOURCES:.cc=.o) \
	$(LIBOCT_READLINE_C_SOURCES:.c=.o) \
	$(LIBOCT_PATHSEARCH_CXX_SOURCES:.cc=.o) \
	$(LIBOCT_PATHSEARCH_C_SOURCES:.c=.o)

ifeq ($(SHARED_LIBS), true)
  ifdef CXXPICFLAG
    LIBOCTAVE_PICOBJ := $(addprefix pic/, $(LIBOCTAVE_OBJECTS))
  else
    LIBOCTAVE_PICOBJ := $(LIBOCTAVE_OBJECTS)
  endif
endif

all: libraries
.PHONY: all

objects: $(OBJECTS)

stmp-pic: pic
	@if [ -f stmp-pic ]; then \
	  true; \
	else \
	  echo "touch stmp-pic"; \
	  touch stmp-pic; \
	fi

pic:
	@if [ -d pic ]; then \
	  true; \
	else \
	  echo "mkdir pic"; \
	  mkdir pic; \
	fi

stamp-prereq: $(OPTS_INC)
	touch stamp-prereq

ifeq ($(SHARED_LIBS), true)
  ifeq ($(STATIC_LIBS), true)
    libraries: \
	stamp-prereq \
	liboctave.$(LIBEXT) liboctave.$(SHLEXT_VER)
  else
    libraries: \
	stamp-prereq \
	liboctave.$(SHLEXT_VER)
  endif
else
  ifeq ($(STATIC_LIBS), true)
    libraries: \
	stamp-prereq \
	liboctave.$(LIBEXT)
  else
    libraries: stamp-prereq
  endif
endif
.PHONY: libraries

liboctave.$(LIBEXT): $(LIBOCTAVE_OBJECTS)
	rm -f $@
	$(TEMPLATE_AR) $(TEMPLATE_ARFLAGS) $@ $^
	$(RANLIB) $@

liboctave.$(SHLEXT_VER): liboctave.$(SHLEXT)
	rm -f $@
	$(LN_S) $< $@

liboctave.$(SHLEXT): $(LIBOCTAVE_PICOBJ)
	rm -f $@
	$(SH_LD) $(SH_LDFLAGS) $(SONAME_FLAGS) -o $@ $^ $(LINK_DEPS)

$(LIBOCTAVE_PICOBJ): stmp-pic

check: all
.PHONY: check

install: install-lib install-inc
.PHONY: install

install-strip:
	$(MAKE) INSTALL_PROGRAM="$(INSTALL_PROGRAM) -s" install
.PHONY: install-strip

install-lib:
	$(top_srcdir)/mkinstalldirs $(octlibdir)
	if $(STATIC_LIBS); then \
	  rm -f $(octlibdir)/liboctave.$(LIBEXT); \
	  $(INSTALL_DATA) liboctave.$(LIBEXT) \
	    $(octlibdir)/liboctave.$(LIBEXT); \
	  $(RANLIB) $(octlibdir)/liboctave.$(LIBEXT); \
	fi
	if $(SHARED_LIBS); then \
	  rm -f $(octlibdir)/liboctave.$(SHLLIB_VER); \
	  $(INSTALL_PROGRAM) \
	    liboctave.$(SHLLIB) $(octlibdir)/liboctave.$(SHLLIB_VER); \
	  rm -f $(octlibdir)/liboctave.$(SHLLIB); \
	  $(LN_S) liboctave.$(SHLLIB_VER) $(octlibdir)/liboctave.$(SHLLIB); \
	  if  test x$(SHLBIN) != x ; then \
	    rm -f $(bindir)/liboctave.$(SHLBIN); \
	    $(INSTALL_PROGRAM) \
	      liboctave.$(SHLBIN) $(bindir)/liboctave.$(SHLBIN); \
	  fi; \
	fi
.PHONY: install-lib

install-inc:
	$(top_srcdir)/mkinstalldirs $(octincludedir)/octave
	for f in $(INCLUDES_FOR_INSTALL); do \
	  rm -f $(octincludedir)/octave/$$f; \
	  if [ -f $$f ]; then \
	    $(INSTALL_DATA) $$f $(octincludedir)/octave/$$f; \
	  else \
	    $(INSTALL_DATA) $(srcdir)/$$f $(octincludedir)/octave/$$f; \
	  fi ; \
	done
.PHONY: install-inc

uninstall:
	rm -f $(octlibdir)/liboctave.$(LIBEXT)
	rm -f $(octlibdir)/liboctave.$(SHLLIB)
	rm -f $(octlibdir)/liboctave.$(SHLLIB_VER)
	if test x$(SHLBIN) != x; then \
	  rm -f $(bindir)/liboctave.$(SHLBIN); \
	  rm -f $(bindir)/liboctave.$(SHLBIN_VER); \
	fi
	for f in $(INCLUDES); do rm -f $(octincludedir)/octave/$$f; done
.PHONY: uninstall

tags: $(SOURCES)
	ctags $(SOURCES)

TAGS: $(SOURCES)
	etags $(SOURCES)

clean:
	rm -f *.$(LIBEXT) *.o *.d pic/*.o
	-rmdir pic
	rm -f stmp-pic
	if $(SHARED_LIBS); then rm -f *.$(SHLEXT_VER) *.$(SHLEXT); fi
.PHONY: clean

mostlyclean: clean
.PHONY: mostlyclean

distclean: clean
	rm -f Makefile so_locations
.PHONY: distclean

maintainer-clean: distclean
	rm -f tags TAGS
.PHONY: maintainer-clean

dist:
	ln $(DISTFILES) ../`cat ../.fname`/liboctave
.PHONY: dist

bin-dist:
	if [ -n "$(BINDISTFILES)" ]; then \
	  ln $(BINDISTFILES) ../`cat ../.fname`/liboctave; \
	fi
	if [ -n "$(BINDISTLIBS)" ]; then \
	  echo $(BINDISTLIBS) >> ../`cat ../.fname`/LIBRARIES; \
	fi
.PHONY: bin-dist

$(OPTS_INC) : %.h : %.in $(top_srcdir)/mk-opts.pl
	@echo making $@ from $<
	@perl $(top_srcdir)/mk-opts.pl --opt-class-header $< > $@-t
	@$(top_srcdir)/move-if-change $@-t $@

# If missing, GNU make attempts to create them in the reverse of the
# order in which they are listed here.  We rely on that fact to ensure
# that defaults.h is created before trying to create the .d files.
# Hmm.  I wonder if we can count on that...

ifndef omit_deps
-include $(MAKEDEPS) stamp-prereq
endif
