# Makefile for octave's src directory
#
# Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
#               2002, 2003, 2004, 2005, 2006, 2007 John W. Eaton
#
# This file is part of Octave.
# 
# Octave is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.
# 
# Octave is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Octave; see the file COPYING.  If not, see
# <http://www.gnu.org/licenses/>.

TOPDIR = ..

srcdir = @srcdir@
top_srcdir = @top_srcdir@
VPATH = @srcdir@:@srcdir@/DLD-FUNCTIONS:@srcdir@/OPERATORS:@srcdir@/TEMPLATE-INST

include $(TOPDIR)/Makeconf

DLL_CDEFS = @OCTINTERP_DLL_DEFS@
DLL_CXXDEFS = @OCTINTERP_DLL_DEFS@

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

PT_FLAGS = -fexternal-templates -fno-implicit-templates
CXXFLAGS_NO_PT_FLAGS = $(filter-out $(PT_FLAGS), $(ALL_CXXFLAGS))

%.df : %.cc
	@echo making $@ from $<
	@(echo "// DO NOT EDIT!  Generated automatically by mkdefs." ; \
	  egrep '^(///*|/\*) *PKG_ADD:' $< ; \
	  $(CXXCPP) $(CPPFLAGS) $(CXXFLAGS_NO_PT_FLAGS) -DMAKE_BUILTINS $< \
	    | $(srcdir)/mkdefs) > $@-t
	@mv $@-t $@

# How to make a .oct file from a .o file:

ifeq ($(ENABLE_DYNAMIC_LINKING), true)
  ifdef CXXPICFLAG
    %.oct : pic/%.o octave$(EXEEXT)
	  $(DL_LD) $(DL_LDFLAGS) -o $@ $< $(OCT_LINK_DEPS)
  else
    %.oct : %.o octave$(EXEEXT)
	  $(DL_LD) $(DL_LDFLAGS) -o $@ $< $(OCT_LINK_DEPS)
  endif
endif

OPT_BASE := $(addsuffix -opts, DASPK DASRT DASSL LSODE Quad)
OPT_HANDLERS := $(addsuffix .cc, $(OPT_BASE))
OPT_IN := $(addprefix ../liboctave/, $(addsuffix .in, $(OPT_BASE)))
OPT_INC := $(addprefix ../liboctave/, $(addsuffix .h, $(OPT_BASE)))

DLD_XSRC := amd.cc balance.cc besselj.cc betainc.cc bsxfun.cc cellfun.cc \
	chol.cc ccolamd.cc colamd.cc colloc.cc conv2.cc convhulln.cc daspk.cc \
	dasrt.cc dassl.cc det.cc dispatch.cc dlmread.cc dmperm.cc eig.cc \
	expm.cc fft.cc fft2.cc fftn.cc fftw.cc filter.cc find.cc \
	fltk_backend.cc \
	gammainc.cc gcd.cc getgrent.cc getpwent.cc getrusage.cc \
	givens.cc hess.cc hex2num.cc inv.cc kron.cc lookup.cc lsode.cc \
	lu.cc luinc.cc matrix_type.cc max.cc md5sum.cc pinv.cc qr.cc \
	quad.cc qz.cc rand.cc rcond.cc regexp.cc schur.cc sparse.cc \
	spparms.cc sqrtm.cc svd.cc syl.cc symrcm.cc symbfact.cc \
	time.cc tsearch.cc typecast.cc \
	urlwrite.cc __contourc__.cc __delaunayn__.cc __dsearchn__.cc \
	__glpk__.cc __lin_interpn__.cc __magick_read__.cc __pchip_deriv__.cc \
	__qp__.cc __voronoi__.cc __convn__.cc

DLD_SRC := $(addprefix DLD-FUNCTIONS/, $(DLD_XSRC))

DLD_OBJ_1 := $(patsubst %.l, %.o, $(DLD_XSRC))
DLD_OBJ := $(patsubst %.cc, %.o, $(DLD_OBJ_1))

ifeq ($(ENABLE_DYNAMIC_LINKING), true)
  OCT_FILES := $(patsubst %.o, %.oct, $(DLD_OBJ))
  ifdef CXXPICFLAG
    DLD_PICOBJ := $(addprefix pic/, $(DLD_OBJ))
  else
    DLD_PICOBJ := $(DLD_OBJ)
  endif
else
  DLD_STATIC_OBJ := $(DLD_OBJ)
endif

OV_INTTYPE_INC := ov-base-int.h ov-int-traits.h ov-intx.h \
	ov-int8.h ov-int16.h ov-int32.h ov-int64.h \
	ov-uint8.h ov-uint16.h ov-uint32.h ov-uint64.h

OV_INCLUDES := ov-re-mat.h ov-cx-mat.h ov-ch-mat.h ov-cs-list.h ov-list.h \
	ov-struct.h ov-scalar.h ov-range.h ov-complex.h \
	ov-colon.h ov-base.h ov-base-mat.h ov-base-scalar.h \
	ov-str-mat.h ov-bool-mat.h ov-null-mat.h ov-bool.h \
	ov-base-diag.h ov-re-diag.h ov-flt-re-diag.h ov-cx-diag.h ov-flt-cx-diag.h \
	ov-perm.h ov-flt-perm.h \
	ov-cell.h ov.h ov-fcn.h ov-builtin.h ov-dld-fcn.h \
	ov-mex-fcn.h ov-usr-fcn.h ov-fcn-handle.h \
	ov-fcn-inline.h ov-class.h ov-typeinfo.h ov-type-conv.h \
	ov-flt-re-mat.h ov-flt-cx-mat.h ov-float.h ov-flt-complex.h \
	$(OV_INTTYPE_INC)

OV_SPARSE_INCLUDES := \
	ov-base-sparse.h ov-bool-sparse.h ov-cx-sparse.h ov-re-sparse.h

PT_INCLUDES := pt.h pt-all.h pt-arg-list.h pt-assign.h pt-binop.h \
        pt-bp.h pt-cbinop.h pt-cell.h pt-check.h pt-cmd.h pt-colon.h \
	pt-const.h pt-decl.h pt-except.h pt-exp.h pt-fcn-handle.h \
	pt-id.h pt-idx.h pt-jump.h pt-loop.h pt-mat.h pt-misc.h \
	pt-pr-code.h pt-select.h pt-stmt.h pt-unop.h pt-walk.h \

INCLUDES := Cell.h base-list.h builtins.h c-file-ptr-stream.h \
	comment-list.h debug.h defun-dld.h defun-int.h defun.h \
	dirfns.h dynamic-ld.h error.h file-io.h gl-render.h \
	gripes.h help.h input.h lex.h load-path.h load-save.h ls-hdf5.h \
	ls-mat-ascii.h ls-mat4.h ls-mat5.h ls-oct-ascii.h \
	ls-oct-binary.h ls-utils.h mex.h mexproto.h oct-errno.h \
	oct-fstrm.h oct-hist.h oct-iostrm.h oct-map.h oct-obj.h \
	oct-prcstrm.h oct-procbuf.h oct-stdstrm.h oct-stream.h \
	zfstream.h oct-strstrm.h oct-lvalue.h oct.h octave.h ops.h \
	pager.h parse.h pr-output.h procstream.h sighandlers.h \
	siglist.h sparse-xdiv.h sparse-xpow.h symtab.h sysdep.h \
	token.h toplev.h unwind-prot.h utils.h variables.h \
	version.h xdiv.h xnorm.h xpow.h \
	$(OV_INCLUDES) \
	$(PT_INCLUDES) \
	$(OV_SPARSE_INCLUDES)

TI_XSRC := Array-os.cc Array-tc.cc

TI_SRC := $(addprefix TEMPLATE-INST/, $(TI_XSRC))

INTTYPE_OP_XSRC := op-int-concat.cc op-int-conv.cc op-double-conv.cc \
	op-i8-i8.cc op-i16-i16.cc op-i32-i32.cc op-i64-i64.cc \
	op-ui8-ui8.cc op-ui16-ui16.cc op-ui32-ui32.cc op-ui64-ui64.cc

SPARSE_OP_XSRC := op-bm-sbm.cc op-b-sbm.cc op-cm-scm.cc op-cm-sm.cc \
	op-cs-scm.cc op-cs-sm.cc op-m-scm.cc op-m-sm.cc op-sbm-b.cc \
	op-sbm-bm.cc op-sbm-sbm.cc op-scm-cm.cc op-scm-cs.cc op-scm-m.cc \
	op-scm-s.cc op-scm-scm.cc op-scm-sm.cc op-sm-cm.cc \
	op-sm-cs.cc op-sm-m.cc op-sm-s.cc op-sm-scm.cc op-sm-sm.cc \
	op-s-scm.cc op-s-sm.cc

DOUBLE_OP_XSRC := op-cm-cm.cc op-cm-cs.cc op-cm-m.cc \
	op-cm-s.cc op-cs-cm.cc op-cs-cs.cc op-cs-m.cc \
	op-cs-s.cc op-m-cm.cc \
	op-m-cs.cc op-m-m.cc op-m-s.cc op-s-cm.cc \
	op-s-cs.cc op-s-m.cc op-s-s.cc 

FLOAT_OP_XSRC := op-fcm-fcm.cc op-fcm-fcs.cc op-fcm-fm.cc \
	op-fcm-fs.cc op-fcs-fcm.cc op-fcs-fcs.cc op-fcs-fm.cc \
	op-fcs-fs.cc op-fm-fcm.cc \
	op-fm-fcs.cc op-fm-fm.cc op-fm-fs.cc op-fs-fcm.cc \
	op-fs-fcs.cc op-fs-fm.cc op-fs-fs.cc 

DIAG_OP_XSRC := op-cdm-cdm.cc op-cdm-cm.cc op-cdm-cs.cc op-cdm-dm.cc \
	op-cdm-m.cc op-cdm-s.cc op-cm-cdm.cc op-cm-dm.cc op-dm-cdm.cc \
	op-dm-cm.cc op-dm-cs.cc op-dm-dm.cc op-dm-m.cc op-dm-s.cc \
	op-m-cdm.cc op-m-dm.cc

FDIAG_OP_XSRC := op-fcdm-fcdm.cc op-fcdm-fcm.cc op-fcdm-fcs.cc op-fcdm-fdm.cc \
	op-fcdm-fm.cc op-fcdm-fs.cc op-fcm-fcdm.cc op-fcm-fdm.cc \
	op-fdm-fcdm.cc op-fdm-fcm.cc op-fdm-fcs.cc op-fdm-fdm.cc \
	op-fdm-fm.cc op-fdm-fs.cc op-fm-fcdm.cc op-fm-fdm.cc

PERM_OP_XSRC := op-cm-pm.cc op-fcm-fpm.cc op-fm-fpm.cc op-fpm-fcm.cc \
	op-fpm-fm.cc op-fpm-fpm.cc op-m-pm.cc op-pm-cm.cc op-pm-m.cc op-pm-pm.cc

OP_XSRC :=  op-b-b.cc op-b-bm.cc op-bm-b.cc op-bm-bm.cc op-cell.cc \
	op-chm.cc op-class.cc op-list.cc op-range.cc op-str-m.cc \
	op-str-s.cc op-str-str.cc op-struct.cc \
	$(DOUBLE_OP_XSRC) $(FLOAT_OP_XSRC) $(INTTYPE_OP_XSRC) \
	$(SPARSE_OP_XSRC) $(DIAG_OP_XSRC) $(FDIAG_OP_XSRC) $(PERM_OP_XSRC)

OP_SRC := $(addprefix OPERATORS/, $(OP_XSRC))

OP_INCLUDES := OPERATORS/op-int.h \
	OPERATORS/op-dm-template.cc OPERATORS/op-dms-template.cc OPERATORS/op-pm-template.cc

OV_INTTYPE_SRC := \
	ov-int8.cc ov-int16.cc ov-int32.cc ov-int64.cc \
	ov-uint8.cc ov-uint16.cc ov-uint32.cc ov-uint64.cc

OV_SPARSE_SRC := \
	ov-base-sparse.cc ov-bool-sparse.cc ov-cx-sparse.cc ov-re-sparse.cc

OV_SRC := ov-base.cc ov-ch-mat.cc \
	ov-cs-list.cc ov-list.cc ov-re-mat.cc ov-cx-mat.cc \
	ov-range.cc ov-scalar.cc ov-complex.cc ov-str-mat.cc \
	ov-struct.cc \
	ov-colon.cc ov-bool-mat.cc ov-bool.cc ov-null-mat.cc ov-cell.cc \
	ov.cc ov-fcn.cc ov-builtin.cc ov-dld-fcn.cc \
	ov-mex-fcn.cc ov-usr-fcn.cc ov-fcn-handle.cc ov-fcn-inline.cc \
	ov-class.cc ov-typeinfo.cc \
	ov-flt-re-mat.cc ov-flt-cx-mat.cc ov-float.cc ov-flt-complex.cc \
	ov-re-diag.cc ov-flt-re-diag.cc ov-cx-diag.cc ov-flt-cx-diag.cc \
	ov-perm.cc ov-flt-perm.cc \
	$(OV_INTTYPE_SRC) \
	$(OV_SPARSE_SRC)

PT_SRC := pt.cc pt-arg-list.cc pt-assign.cc pt-bp.cc pt-binop.cc \
	pt-cbinop.cc pt-cell.cc pt-check.cc pt-cmd.cc pt-colon.cc \
	pt-const.cc pt-decl.cc pt-except.cc pt-exp.cc pt-fcn-handle.cc \
	pt-id.cc pt-idx.cc pt-jump.cc pt-loop.cc pt-mat.cc pt-misc.cc \
	pt-pr-code.cc pt-select.cc pt-stmt.cc pt-unop.cc

DIST_SRC := Cell.cc bitfcns.cc c-file-ptr-stream.cc comment-list.cc \
	cutils.c data.cc debug.cc defaults.cc defun.cc dirfns.cc \
	dynamic-ld.cc error.cc file-io.cc gl-render.cc graphics.cc \
	gripes.cc help.cc input.cc lex.l load-path.cc load-save.cc \
	ls-hdf5.cc ls-mat-ascii.cc ls-mat4.cc ls-mat5.cc ls-oct-ascii.cc \
	ls-oct-binary.cc ls-utils.cc main.c mappers.cc matherr.c \
	mex.cc oct-fstrm.cc oct-hist.cc oct-iostrm.cc oct-map.cc \
	oct-obj.cc oct-prcstrm.cc oct-procbuf.cc oct-stream.cc \
	octave.cc zfstream.cc oct-strstrm.cc oct-lvalue.cc pager.cc \
	parse.y pr-output.cc procstream.cc sighandlers.cc \
	siglist.c sparse-xdiv.cc sparse-xpow.cc strfns.cc \
	syscalls.cc symtab.cc sysdep.cc token.cc toplev.cc \
	unwind-prot.cc utils.cc variables.cc xdiv.cc xnorm.cc xpow.cc \
	$(OV_SRC) \
	$(PT_SRC)

SOURCES := $(DIST_SRC) $(OP_SRC) $(TI_SRC)

BUILT_EXTRAS := graphics.h mxarray.h

EXTRAS := ov-base-int.cc ov-base-mat.cc ov-base-diag.cc ov-base-scalar.cc graphics-props.cc

EXTRA_OBJECTS := oct-errno.o octave.o builtins.o ops.o

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

OBJECTS_4 := $(notdir $(SOURCES))
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)) $(EXTRA_OBJECTS)

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

# Ugh.

DEP_5 := $(SOURCES) $(DLD_SRC) builtins.cc oct-errno.cc ops.cc main.c
DEP_4 := $(notdir $(DEP_5))
DEP_3 := $(patsubst %.l, %.cc, $(DEP_4))
DEP_2 := $(patsubst %.y, %.cc, $(DEP_3))
DEP_1 := $(patsubst %.c, %.d, $(DEP_2))
MAKEDEPS := $(patsubst %.cc, %.d, $(DEP_1))

# Some stupid egreps don't like empty elements in alternation patterns,
# so we have to repeat ourselves because some stupid egreps don't like
# empty elements in alternation patterns.

DEFUN_PATTERN = "^[ \t]*DEF(CONSTFUN|CMD|UN|UN_DLD|UNX_DLD|UN_TEXT)[ \t]*\\("

DLD_DEF_FILES_1 := $(patsubst %.l, %.df, $(DLD_XSRC))
DLD_DEF_FILES := $(patsubst %.cc, %.df, $(DLD_DEF_FILES_1))

DEF_4 := $(addprefix $(srcdir)/, $(SOURCES))
DEF_3 := $(notdir $(shell egrep -l $(DEFUN_PATTERN) $(DEF_4)))
DEF_2 := $(patsubst %.y, %.df, $(DEF_3))
DEF_1 := $(patsubst %.l, %.df, $(DEF_2))
DEF_FILES := $(patsubst %.cc, %.df, $(DEF_1)) $(DLD_DEF_FILES)

DOC_FILES := $(sort $(DEF_FILES))

OCTAVE_LFLAGS = -L$(TOPDIR)/liboctave -L$(TOPDIR)/libcruft \
  -L$(TOPDIR)/src $(RLD_FLAG)

ifeq ($(ENABLE_DYNAMIC_LINKING), true)
  OCTAVE_LIBS = $(LIBOCTINTERP) $(LIBOCTAVE) \
    $(SPECIAL_MATH_LIB) $(LIBCRUFT) \
    $(LIBPLPLOT) $(LIBGLOB)
else
  OCTAVE_LIBS = $(LIBOCTINTERP) $(LIBOCTAVE) $(QHULL_LIBS) \
    $(GLPK_LIBS) $(MAGICK_LIBS) $(REGEX_LIBS) $(SPECIAL_MATH_LIB) $(LIBCRUFT) \
    $(LIBPLPLOT) $(LIBGLOB)
endif

OCTINTERP_LINK_DEPS = \
  -L../liboctave $(LIBOCTAVE) -L../libcruft $(LIBCRUFT) $(LIBS) $(FLIBS) \
  $(OPENGL_LIBS)

OCT_LINK_DEPS = \
  -L../libcruft $(LIBCRUFT) -L../liboctave $(LIBOCTAVE) \
  -L. $(LIBOCTINTERP) $(CHOLMOD_LIBS) $(UMFPACK_LIBS) $(AMD_LIBS) \
   $(CAMD_LIBS) $(COLAMD_LIBS) $(CCOLAMD_LIBS) $(CXSPARSE_LIBS) $(BLAS_LIBS) \
   $(FFTW_LIBS) $(LIBS) $(FLIBS)

BUILT_DISTFILES = DOCSTRINGS oct-gperf.h parse.cc lex.cc y.tab.h \
	$(OPT_HANDLERS) $(BUILT_EXTRAS)

DISTFILES = Makefile.in ChangeLog genprops.awk mkdefs mkops mkgendoc \
	mkbuiltins mk-errno-list mk-pkg-add \
	defaults.h.in graphics.h.in mxarray.h.in oct-conf.h.in \
	oct-errno.cc.in octave.gperf \
	$(INCLUDES) $(DIST_SRC) $(EXTRAS)

all: octave$(EXEEXT) $(OCT_FILES) PKG_ADD DOCSTRINGS
.PHONY: all

objects: $(OBJECTS)

lex.o parse.o pic/lex.o pic/parse.o: \
	ALL_CXXFLAGS := $(filter-out -Wold-style-cast, $(ALL_CXXFLAGS))

$(DLD_PICOBJ): \
	ALL_CXXFLAGS := $(filter-out $(DLL_CXXDEFS), $(ALL_CXXFLAGS))

fltk_backend.o pic/fltk_backend.o: \
	ALL_CXXFLAGS := $(filter-out $(DLL_CXXDEFS), $(ALL_CXXFLAGS) $(GRAPHICS_CFLAGS))

XERBLA = ../libcruft/blas-xtra/xerbla.o
ifdef FPICFLAG
  PIC_XERBLA = ../libcruft/blas-xtra/pic/xerbla.o
else
  PIC_XERBLA = $(XERBLA)
endif

ifeq ($(SHARED_LIBS), true)
  ifeq ($(STATIC_LIBS), true)
    LIBRARIES = $(LIBPRE)octinterp.$(LIBEXT) $(LIBPRE)octinterp.$(SHLEXT_VER)
  else
    LIBRARIES = $(LIBPRE)octinterp.$(SHLEXT_VER)
  endif
else
  ifeq ($(STATIC_LIBS), true)
    LIBRARIES = $(LIBPRE)octinterp.$(LIBEXT)
  else
    ## This is not going to work, but hey, you asked for it...
    LIBRARIES =
  endif
endif

libraries: $(LIBRARIES)
.PHONY: libraries

$(LIBPRE)octinterp.$(LIBEXT): $(OBJECTS) $(XERBLA)
	rm -f $@
	$(TEMPLATE_AR) $(TEMPLATE_ARFLAGS) $@ $^
	$(RANLIB) $@

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

$(LIBPRE)octinterp.$(SHLEXT): $(PICOBJ) $(PIC_XERBLA)
	rm -f $@
	$(SH_LD) $(SH_LDFLAGS) $(SONAME_FLAGS) -o $@ $^ $(OCTINTERP_LINK_DEPS)

octave$(EXEEXT): $(LIBRARIES) main.o $(DLD_STATIC_OBJ)
	$(LD_CXX) $(CPPFLAGS) $(ALL_CXXFLAGS) $(RDYNAMIC_FLAG) \
	$(ALL_LDFLAGS) -o $@ \
	main.o $(DLD_STATIC_OBJ) \
	$(OCTAVE_LFLAGS) \
	$(OCTAVE_LIBS) \
	$(LEXLIB) $(UMFPACK_LIBS) $(AMD_LIBS) $(CAMD_LIBS) $(COLAMD_LIBS) \
	$(CHOLMOD_LIBS) $(CCOLAMD_LIBS) $(CXSPARSE_LIBS) $(BLAS_LIBS) \
	$(FFTW_LIBS) $(OPENGL_LIBS) $(LIBS) $(FLIBS)

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

$(PICOBJ): stmp-pic

builtins.cc: $(DEF_FILES) mkbuiltins
	@echo making $@
	@echo DEF_FILES = $(DEF_FILES)
	@echo $(DEF_FILES) > def-files
	@$(srcdir)/mkbuiltins def-files > $@-t
	@mv $@-t $@

## FIXME -- maybe genprops.awk should write both output files?
## Or maybe there should be separate commands to generate each file?
graphics.h graphics-props.cc: graphics.h.in genprops.awk
	@echo making graphics.h and graphics-props.cc
	@$(AWK) -f $(srcdir)/genprops.awk $< > graphics.h-t
	@mv graphics.h-t graphics.h

PKG_ADD: $(DLD_DEF_FILES)
	$(srcdir)/mk-pkg-add $(DLD_DEF_FILES) > $@-t
	@mv $@-t $@

DOCSTRINGS: gendoc$(BUILD_EXEEXT)
	@echo making $@
	@./gendoc > $@-t
	@mv $@-t $@

doc-files: $(DOC_FILES)
	@echo making $@
	@echo DOC_FILES = $(DOC_FILES)
	@echo $(DOC_FILES) > $@-t
	mv $@-t $@

gendoc.cc: doc-files mkgendoc
	@echo making $@
	@$(srcdir)/mkgendoc doc-files > $@-t
	@mv $@-t $@

gendoc$(BUILD_EXEEXT): gendoc.cc
	$(BUILD_CXX) $(BUILD_CXXFLAGS) -o $@ $^ $(BUILD_LDFLAGS)

ops.cc: $(OP_SRC) mkops
	@echo making $@ from $(OP_SRC)
	@$(srcdir)/mkops $(OP_SRC) > $@-t
	@mv $@-t $@

PREREQ := defaults.h graphics.h oct-conf.h oct-errno.cc oct-gperf.h parse.cc lex.cc 

$(DEF_FILES): $(OPT_INC) mkdefs 

ifndef omit_deps
$(MAKEDEPS): $(OPT_INC) $(OPT_HANDLERS) $(PREREQ)
endif

check: all
.PHONY: check

install: install-bin install-oct install-lib install-inc
.PHONY: install

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

install-bin:
	$(top_srcdir)/mkinstalldirs $(DESTDIR)$(bindir)
	rm -f $(DESTDIR)$(bindir)/octave$(EXEEXT)
	$(INSTALL_PROGRAM) octave$(EXEEXT) $(DESTDIR)$(bindir)/octave-$(version)$(EXEEXT)
	cd $(DESTDIR)$(bindir) ; $(LN_S) octave-$(version)$(EXEEXT) octave$(EXEEXT)
.PHONY: install-bin

install-oct:
	$(top_srcdir)/mkinstalldirs $(DESTDIR)$(octfiledir)
	$(INSTALL_DATA) PKG_ADD $(DESTDIR)$(octfiledir)/PKG_ADD
	if [ -n "$(OCT_FILES)" ]; then \
	  xfiles="$(OCT_FILES)"; \
	  for f in $$xfiles; do \
	    $(INSTALL_PROGRAM) $$f $(DESTDIR)$(octfiledir)/$$f; \
	  done; \
	fi
.PHONY: install-oct

install-lib:
	$(top_srcdir)/mkinstalldirs $(DESTDIR)$(octlibdir)
	if $(STATIC_LIBS); then \
	  rm -f $(DESTDIR)$(octlibdir)/$(LIBPRE)octinterp.$(LIBEXT); \
	  $(INSTALL_DATA) $(LIBPRE)octinterp.$(LIBEXT) \
	    $(DESTDIR)$(octlibdir)/$(LIBPRE)octinterp.$(LIBEXT); \
	  $(RANLIB) $(DESTDIR)$(octlibdir)/$(LIBPRE)octinterp.$(LIBEXT); \
	fi
	if $(SHARED_LIBS); then \
	  rm -f $(DESTDIR)$(octlibdir)/$(LIBPRE)octinterp.$(SHLEXT_VER); \
	  $(INSTALL) $(LIBPRE)octinterp.$(SHLLIB) \
	    $(DESTDIR)$(octlibdir)/$(LIBPRE)octinterp.$(SHLLIB_VER); \
	  rm -f $(DESTDIR)$(octlibdir)/$(LIBPRE)octinterp.$(SHLLIB); \
	  (cd $(DESTDIR)$(octlibdir) ; $(LN_S) $(LIBPRE)octinterp.$(SHLLIB_VER) $(DESTDIR)$(octlibdir)/$(LIBPRE)octinterp.$(SHLLIB)); \
	  if  test x$(SHLBIN) != x ; then \
	    rm -f $(DESTDIR)$(bindir)/$(LIBPRE)octinterp.$(SHLBIN); \
	    $(INSTALL_PROGRAM) \
	      $(LIBPRE)octinterp.$(SHLBIN) $(DESTDIR)$(bindir)/$(LIBPRE)octinterp.$(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
	for f in defaults.h oct-conf.h oct-gperf.h; 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)$(bindir)/octave$(EXEEXT)
	rm -f $(DESTDIR)$(bindir)/octave-$(version)$(EXEEXT)
	rm -f $(DESTDIR)$(octlibdir)/$(LIBPRE)octinterp.$(LIBEXT)
	rm -f $(DESTDIR)$(octlibdir)/$(LIBPRE)octinterp.$(SHLLIB)
	rm -f $(DESTDIR)$(octlibdir)/$(LIBPRE)octinterp.$(SHLLIB_VER)
	if test x$(SHLBIN) != x ; then \
	  rm -f $(DESTDIR)$(bindir)/$(LIBPRE)octinterp.$(SHLBIN); \
	  rm -f $(DESTDIR)$(bindir)/$(LIBPRE)octinterp.$(SHLBIN_VER); \
	fi
	for f in $(INCLUDES_FOR_INSTALL) defaults.h oct-conf.h oct-gperf.h; do \
	  rm -f $(DESTDIR)$(octincludedir)/octave/$$f; \
	done
	if [ -n "$(OCT_FILES)" ]; then \
	  xfiles="$(OCT_FILES)"; \
	  for f in $$xfiles; do \
	    rm -f $(DESTDIR)$(octfiledir)/$$f; \
	  done; \
	fi
	rm -f $(DESTDIR)$(octfiledir)/PKG_ADD
	-rmdir $(DESTDIR)$(octincludedir)/octave
	-rmdir $(DESTDIR)$(octincludedir)
	-rmdir $(DESTDIR)$(octlibdir)
	-rmdir $(DESTDIR)$(octfiledir)
.PHONY: uninstall

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

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

clean:
	rm -f $(LIBPRE)octinterp.$(LIBEXT)
	rm -f $(LIBPRE)octinterp.$(SHLEXT_VER) $(LIBPRE)octinterp.$(SHLEXT)
	rm -f $(OBJECTS) $(DLD_OBJ) $(MAKEDEPS) $(DOC_FILES) $(OCT_FILES)
	rm -f $(PICOBJ) $(DLD_PICOBJ) stmp-pic gendoc$(EXEEXT)
	rm -f builtins.cc ops.cc defaults.h oct-conf.h def-files
	rm -f PKG_ADD
	-rmdir pic
.PHONY: clean

mostlyclean: clean
.PHONY: mostlyclean

distclean: clean
	rm -f Makefile octave$(EXEEXT) .fname so_locations oct-errno.cc
	rm -f stamp-liboctave-prereq
	rm -f doc-files gendoc.cc graphics.h graphics-props.cc mxarray.h DOCSTRINGS
.PHONY: distclean

maintainer-clean: distclean
	rm -f tags TAGS y.tab.h y.output yy.lex.c lex.cc parse.cc
	rm -f oct-gperf.h $(OPT_HANDLERS)
.PHONY: maintainer-clean

dist: parse.cc lex.cc oct-gperf.h
	ln $(addprefix $(srcdir)/, $(DISTFILES)) ../`cat ../.fname`/src
	ln $(BUILT_DISTFILES) ../`cat ../.fname`/src
	for f in DLD-FUNCTIONS OPERATORS TEMPLATE-INST; do \
	  if [ -d ../`cat ../.fname`/src/$$f ]; then \
	    true; \
	  else \
	    mkdir ../`cat ../.fname`/src/$$f; \
	  fi; \
	done
	ln $(addprefix $(srcdir)/, $(DLD_SRC)) ../`cat ../.fname`/src/DLD-FUNCTIONS
	ln $(addprefix $(srcdir)/, $(OP_SRC)) ../`cat ../.fname`/src/OPERATORS
	ln $(addprefix $(srcdir)/, $(OP_INCLUDES)) ../`cat ../.fname`/src/OPERATORS
	ln $(addprefix $(srcdir)/, $(TI_SRC)) ../`cat ../.fname`/src/TEMPLATE-INST
	rm -f parse.cc lex.cc y.tab.h y.output yy.lex.c
	rm -f oct-gperf.h defaults.h oct-conf.h *.d *.df builtins.cc
.PHONY: dist

conf-dist:
	ln $(addprefix $(srcdir)/, octave.cc Makefile.in) ../`cat ../.fname`/src
.PHONY: conf-dist

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

$(OPT_HANDLERS) : %.cc : $(top_srcdir)/liboctave/%.in $(top_srcdir)/mk-opts.pl
	@echo making $@ from $<
	@$(PERL) $(top_srcdir)/mk-opts.pl --opt-handler-fcns $< > $@-t
	@mv $@-t $@

## We require Bison.
parse.cc : parse.y
	@echo "expect 14 shift/reduce conflicts"
	$(YACC) $(YFLAGS) --output=$@ --defines=y.tab.h $<

lex.cc : lex.l
	$(LEX) $(LFLAGS) $< > $(@F)-t
	@mv $(@F)-t $@


$(OPT_INC) : %.h : %.in
	$(MAKE) -C $(@D) $@

## We want to force an update of defaults.h and oct-conf.h every
## time make is run because some values may come from the command
## line or the environment.  The substitution rules use move-if-change,
## so this should not cause trouble if the file already exists and the
## newly generated file is not different.

defaults.h: defaults.h.in ../Makeconf Makefile FORCE
	@$(do-subst-default-vals)

oct-conf.h: oct-conf.h.in ../Makeconf Makefile FORCE
	@$(do-subst-config-vals)

FORCE:
.PHONY: FORCE

oct-errno.cc: oct-errno.cc.in ../Makeconf Makefile
	@echo "making $@ from $<"
	@if test -n "$(PERL)"; then \
	  $(srcdir)/mk-errno-list --perl "$(PERL)" < $< > $@-t; \
	elif test -n "$(PYTHON)"; then \
	  $(srcdir)/mk-errno-list --python "$(PYTHON)" < $< > $@-t; \
	else \
	  $(SED) '/@SYSDEP_ERRNO_LIST@/D' $< > $@-t; \
	fi
	@mv $@-t $@

oct-gperf.h: octave.gperf
	@echo "making $@ from $<"
	@$(GPERF) -t -C -D -G -L C++ -Z octave_kw_hash $< | \
	  $(SED) 's,lookup\[,gperf_lookup[,' > $@-t \
	  || (rm -f $@-t; exit 1)
	@mv $@-t $@

__magick_read__.d: CPPFLAGS += $(MAGICK_INCFLAGS)
__magick_read__.df: CPPFLAGS += $(MAGICK_INCFLAGS)
__magick_read__.o pic/__magick_read__.o: CPPFLAGS += $(MAGICK_INCFLAGS)
__magick_read__.oct: OCT_LINK_DEPS += $(MAGICK_LIBS)

convhulln.oct: OCT_LINK_DEPS += $(QHULL_LIBS)
__delaunayn__.oct: OCT_LINK_DEPS += $(QHULL_LIBS)
__voronoi__.oct: OCT_LINK_DEPS += $(QHULL_LIBS)
regexp.oct: OCT_LINK_DEPS += $(REGEX_LIBS)
urlwrite.oct: OCT_LINK_DEPS += $(CURL_LIBS)
__glpk__.oct: OCT_LINK_DEPS += $(GLPK_LIBS)
fltk_backend.oct: OCT_LINK_DEPS += $(GRAPHICS_LIBS) $(FT2_LIBS)

check: all
.PHONY: check

ifdef omit_deps
.PHONY: $(MAKEDEPS)
endif

-include $(MAKEDEPS)
