#
# 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@

DLL_CDEFS = @OCTAVE_DLL_DEFS@
DLL_CXXDEFS = @OCTAVE_DLL_DEFS@

LINK_DEPS = \
  -L../libcruft -L. $(RLD_FLAG) \
  $(LIBCRUFT) $(CHOLMOD_LIBS) $(UMFPACK_LIBS)  $(AMD_LIBS) \
  $(CAMD_LIBS) $(COLAMD_LIBS) $(CCOLAMD_LIBS) $(CXSPARSE_LIBS) \
  $(BLAS_LIBS) $(FFTW_LIBS) $(LIBREADLINE) $(LIBGLOB) $(LIBS) $(FLIBS)

MATRIX_INC := Array.h Array2.h Array3.h ArrayN.h DiagArray2.h \
	Array-util.h ArrayN-idx.h MArray-defs.h \
	MArray.h MArray2.h MDiagArray2.h Matrix.h MArrayN.h \
	base-lu.h dim-vector.h mx-base.h mx-op-defs.h \
	mx-defs.h mx-ext.h CColVector.h CDiagMatrix.h CMatrix.h \
	CNDArray.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 boolNDArray.h \
	chMatrix.h chNDArray.h dColVector.h dDiagMatrix.h dMatrix.h \
	dNDArray.h dRowVector.h dbleAEPBAL.h dbleCHOL.h dbleDET.h \
	dbleHESS.h dbleLU.h dbleQR.h dbleQRP.h dbleSCHUR.h dbleSVD.h \
	boolSparse.h CSparse.h dSparse.h MSparse-defs.h MSparse.h \
	Sparse.h sparse-base-lu.h SparseCmplxLU.h SparsedbleLU.h \
	sparse-base-chol.h SparseCmplxCHOL.h \
	SparsedbleCHOL.h SparseCmplxQR.h SparseQR.h Sparse-op-defs.h \
	MatrixType.h \
	int8NDArray.h uint8NDArray.h int16NDArray.h uint16NDArray.h \
	int32NDArray.h uint32NDArray.h int64NDArray.h uint64NDArray.h \
	intNDArray.h

MX_OP_INC := $(shell $(AWK) -f $(srcdir)/mk-ops.awk prefix=mx list_h_files=1 $(srcdir)/mx-ops)

VX_OP_INC := $(shell $(AWK) -f $(srcdir)/mk-ops.awk prefix=vx list_h_files=1 $(srcdir)/vx-ops)

SPARSE_MX_OP_INC := $(shell $(AWK) -f $(srcdir)/sparse-mk-ops.awk prefix=smx list_h_files=1 $(srcdir)/sparse-mx-ops)

OPTS_INC_DATA := DASPK-opts.in DASRT-opts.in DASSL-opts.in \
	LSODE-opts.in NLEqn-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 \
	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 kpse-xfns.h \
	lo-ieee.h lo-mappers.h lo-specfun.h \
	lo-sysdep.h lo-utils.h mach-info.h md5.h oct-alloc.h oct-cmplx.h \
	oct-env.h oct-fftw.h oct-getopt.h oct-group.h oct-inttypes.h \
	oct-md5.h oct-passwd.h oct-rand.h oct-rl-edit.h oct-rl-hist.h \
	oct-shlib.h oct-sort.h oct-spparms.h oct-syscalls.h \
	oct-sparse.h oct-time.h oct-uname.h \
	pathlen.h pathsearch.h prog-args.h \
	randgamma.h randmtzig.h randpoisson.h \
	so-array.h sparse-sort.h statdefs.h str-vec.h \
	sparse-util.h sun-utils.h sysdir.h systime.h syswait.h \
	$(MATRIX_INC)

BUILT_INCLUDES := oct-types.h mx-ops.h \
	$(OPTS_INC) \
	$(MX_OP_INC) \
	$(VX_OP_INC) \
	$(SPARSE_MX_OP_INC)

TEMPLATE_SRC := Array.cc ArrayN.cc DiagArray2.cc \
	MArray.cc MArray2.cc MArrayN.cc MDiagArray2.cc \
	base-lu.cc oct-sort.cc sparse-base-lu.cc sparse-base-chol.cc \
	sparse-dmsolve.cc

TI_SRC := Array-C.cc Array-b.cc Array-ch.cc Array-i.cc Array-d.cc \
	Array-s.cc Array-so.cc Array-str.cc Array-idx-vec.cc \
	MArray-C.cc MArray-ch.cc MArray-i.cc MArray-d.cc MArray-s.cc \
	MSparse-C.cc MSparse-d.cc Sparse-C.cc Sparse-b.cc Sparse-d.cc \
	oct-inttypes.cc

MATRIX_SRC := Array-util.cc CColVector.cc \
	CDiagMatrix.cc CMatrix.cc CNDArray.cc CRowVector.cc \
	CmplxAEPBAL.cc CmplxCHOL.cc CmplxDET.cc CmplxHESS.cc \
	CmplxLU.cc CmplxQR.cc CmplxQRP.cc CmplxSCHUR.cc CmplxSVD.cc \
	EIG.cc boolMatrix.cc boolNDArray.cc chMatrix.cc \
	chNDArray.cc dColVector.cc dDiagMatrix.cc dMatrix.cc \
	dNDArray.cc dRowVector.cc dbleAEPBAL.cc dbleCHOL.cc \
	dbleDET.cc dbleHESS.cc dbleLU.cc dbleQR.cc dbleQRP.cc \
	dbleSCHUR.cc dbleSVD.cc boolSparse.cc CSparse.cc dSparse.cc \
	MSparse.cc Sparse.cc SparseCmplxLU.cc SparsedbleLU.cc \
	SparseCmplxCHOL.cc SparsedbleCHOL.cc \
	SparseCmplxQR.cc SparseQR.cc MatrixType.cc \
	int8NDArray.cc uint8NDArray.cc int16NDArray.cc uint16NDArray.cc \
	int32NDArray.cc uint32NDArray.cc int64NDArray.cc uint64NDArray.cc 

MX_OP_SRC := $(shell $(AWK) -f $(srcdir)/mk-ops.awk prefix=mx list_cc_files=1 $(srcdir)/mx-ops)

VX_OP_SRC := $(shell $(AWK) -f $(srcdir)/mk-ops.awk prefix=vx list_cc_files=1 $(srcdir)/vx-ops)

SPARSE_MX_OP_SRC := $(shell $(AWK) -f $(srcdir)/sparse-mk-ops.awk prefix=smx list_cc_files=1 $(srcdir)/sparse-mx-ops)

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 \
	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-md5.cc oct-passwd.cc oct-rand.cc \
	oct-shlib.cc oct-spparms.cc oct-syscalls.cc oct-time.cc oct-uname.cc \
	prog-args.cc so-array.cc sparse-sort.cc sparse-util.cc str-vec.cc \
	$(TEMPLATE_SRC) \
	$(TI_SRC) \
	$(MATRIX_SRC)

BUILT_LIBOCTAVE_CXX_SOURCES := \
	$(MX_OP_SRC) \
	$(VX_OP_SRC) \
	$(SPARSE_MX_OP_SRC)

LIBOCTAVE_C_SOURCES := f2c-main.c filemode.c getopt.c getopt1.c \
	lo-cieee.c lo-cutils.c md5.c mkdir.c oct-getopt.c \
	randgamma.c randmtzig.c randpoisson.c rename.c \
	rmdir.c strftime.c strptime.c strcasecmp.c strncase.c \
	tempname.c tempnam.c

LIBOCTAVE_SOURCES := $(LIBOCTAVE_CXX_SOURCES) $(BUILT_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 := kpse-xfns.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 kpse.cc intNDArray.cc

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

DISTFILES := $(addprefix $(srcdir)/, Makefile.in ChangeLog mk-ops.awk \
	mx-ops vx-ops sparse-mk-ops.awk sparse-mx-ops oct-types.h.in \
	$(LIBOCTAVE_CXX_SOURCES) \
	$(LIBOCT_READLINE_SOURCES) \
	$(LIBOCT_PATHSEARCH_SOURCES) \
	$(LIBOCTAVE_C_SOURCES) \
	$(INCLUDES) $(EXTRAS) $(OPTS_INC_DATA)) \
	$(BUILT_LIBOCTAVE_CXX_SOURCES) $(BUILT_INCLUDES)

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

LIBOCTAVE_OBJECTS := \
	$(LIBOCTAVE_CXX_SOURCES:.cc=.o) \
	$(BUILT_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: $(LIBOCTAVE_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) $(VX_OP_INC) $(VX_OP_SRC) \
	$(MX_OP_INC) $(MX_OP_SRC) mx-ops.h \
	$(SPARSE_MX_OP_INC) $(SPARSE_MX_OP_SRC)
	touch stamp-prereq

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

$(LIBPRE)octave.$(LIBEXT): $(LIBOCTAVE_OBJECTS)
	rm -f $@
	$(TEMPLATE_AR) $(TEMPLATE_ARFLAGS) $@ $(LIBOCTAVE_OBJECTS)
	$(RANLIB) $@

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

$(LIBPRE)octave.$(SHLEXT): $(LIBOCTAVE_PICOBJ)
	rm -f $@
	$(SH_LD) $(SH_LDFLAGS) $(SONAME_FLAGS) -o $@ \
	$(LIBOCTAVE_PICOBJ) $(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 $(DESTDIR)$(octlibdir)
	if $(STATIC_LIBS); then \
	  rm -f $(DESTDIR)$(octlibdir)/$(LIBPRE)octave.$(LIBEXT); \
	  $(INSTALL_DATA) $(LIBPRE)octave.$(LIBEXT) \
	    $(DESTDIR)$(octlibdir)/$(LIBPRE)octave.$(LIBEXT); \
	  $(RANLIB) $(DESTDIR)$(octlibdir)/$(LIBPRE)octave.$(LIBEXT); \
	fi
	if $(SHARED_LIBS); then \
	  rm -f $(DESTDIR)$(octlibdir)/$(LIBPRE)octave.$(SHLLIB_VER); \
	  $(INSTALL) \
	    $(LIBPRE)octave.$(SHLLIB) $(DESTDIR)$(octlibdir)/$(LIBPRE)octave.$(SHLLIB_VER); \
	  rm -f $(DESTDIR)$(octlibdir)/$(LIBPRE)octave.$(SHLLIB); \
	  (cd $(DESTDIR)$(octlibdir) ; $(LN_S) $(LIBPRE)octave.$(SHLLIB_VER) $(DESTDIR)$(octlibdir)/$(LIBPRE)octave.$(SHLLIB)); \
	  if  test x$(SHLBIN) != x ; then \
	    rm -f $(DESTDIR)$(bindir)/$(LIBPRE)octave.$(SHLBIN); \
	    $(INSTALL_PROGRAM) \
	      $(LIBPRE)octave.$(SHLBIN) $(DESTDIR)$(bindir)/$(LIBPRE)octave.$(SHLBIN); \
	  fi; \
	fi
.PHONY: install-lib

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

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

tags: $(SOURCES)
	ctags $(SOURCES)

TAGS: $(SOURCES)
	etags $(SOURCES)

clean:
	rm -f $(LIBPRE)octave.$(LIBEXT) $(LIBPRE)octave.$(SHLEXT_VER) $(LIBPRE)octave.$(SHLEXT)
	rm -f $(LIBOCTAVE_OBJECTS) $(MAKEDEPS) $(LIBOCTAVE_PICOBJ) stmp-pic
	-rmdir pic
.PHONY: clean

mostlyclean: clean
.PHONY: mostlyclean

distclean: clean
	rm -f Makefile so_locations stamp-prereq oct-types.h $(OPTS_INC)
	rm -f mx-ops.h $(MX_OP_INC) $(VX_OP_INC) $(MX_OP_SRC) $(VX_OP_SRC)
	rm -f $(SPARSE_MX_OP_INC) $(SPARSE_MX_OP_SRC)
.PHONY: distclean

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

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

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

$(VX_OP_INC) $(VX_OP_SRC) : $(srcdir)/mk-ops.awk vx-ops
	$(AWK) -f $(srcdir)/mk-ops.awk prefix=vx $(srcdir)/vx-ops

$(MX_OP_INC) $(MX_OP_SRC) : $(srcdir)/mk-ops.awk mx-ops
	$(AWK) -f $(srcdir)/mk-ops.awk prefix=mx $(srcdir)/mx-ops

$(SPARSE_MX_OP_INC) $(SPARSE_MX_OP_SRC) : $(srcdir)/sparse-mk-ops.awk sparse-mx-ops
	$(AWK) -f $(srcdir)/sparse-mk-ops.awk prefix=smx $(srcdir)/sparse-mx-ops

mx-ops.h : $(srcdir)/mk-ops.awk mx-ops
	$(AWK) -f $(srcdir)/mk-ops.awk prefix=mx make_inclusive_header=mx-ops.h $(srcdir)/mx-ops > $@-t
	$(simple-move-if-change-rule)


# 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
