# Makefile for octave
#
# Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
#               2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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@
abs_top_srcdir = @abs_top_srcdir@
VPATH = @srcdir@

SCRIPTS_EXE_SUFFIX = @SCRIPTS_EXE_SUFFIX@

include $(TOPDIR)/Makeconf

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

CONF_DISTFILES = Makefile octMakefile.in Makeconf.in \
	configure configure.in config.guess config.sub aclocal.m4 \
	acx_blas.m4 acx_lapack.m4 acx_blas_f77_func.m4 \
	config.h.in install-sh autogen.sh

BUILT_DISTFILES = BUGS INSTALL.OCTAVE

DISTFILES = $(CONF_DISTFILES) \
	COPYING INSTALL NEWS \
	NEWS.[0-9] PROJECTS README README.Linux README.Windows \
	README.Cygwin README.MSVC README.kpathsea ROADMAP SENDING-PATCHES \
	move-if-change octave-sh octave-bug.in octave-bug.cc.in \
	octave-config.in octave-config.cc.in missing mk-opts.pl mkinstalldirs \
	mkoctfile.in mkoctfile.cc.in run-octave.in ChangeLog ChangeLog.[0-9]

# Subdirectories in which to run `make all'.
SUBDIRS = libcruft liboctave src scripts doc examples

# Subdirectories in which to run `make all'.
INSTALL_SUBDIRS = libcruft liboctave src scripts doc examples

# Subdirectories in which to run `make conf-dist'.
CONF_DISTSUBDIRS = src

# Subdirectories in which to run `make dist'.
DISTSUBDIRS = $(sort $(SUBDIRS) test emacs)

# Subdirectories in which to run clean targets.
CLEANSUBDIRS = $(DISTSUBDIRS)

DIRS_TO_MAKE = $(bindir) $(datadir) $(libdir) $(octincludedir)/octave \
  $(fcnfiledir) $(localfcnfiledir) $(localapifcnfiledir) \
  $(localverfcnfiledir) $(octetcdir) $(octfiledir) $(localoctfiledir) \
  $(localapioctfiledir) $(localveroctfiledir) $(imagedir) $(archlibdir) \
  $(localarchlibdir) $(localapiarchlibdir) $(localverarchlibdir)

SHELL_SCRIPTS = octave-bug$(SCRIPTS_EXE_SUFFIX) octave-config$(SCRIPTS_EXE_SUFFIX) \
		mkoctfile$(SCRIPTS_EXE_SUFFIX)  run-octave

CONFIG_FILES = @ac_config_files@

M4_FILES = $(wildcard *.m4)

all: $(SHELL_SCRIPTS) $(filter-out libcruft liboctave, $(SUBDIRS)) dist-info-files
	@echo ""
	@echo "Octave successfully built.  Now choose from the following:"
	@echo ""
	@echo "   ./run-octave    - to run in place to test before installing"
	@echo "   make check      - to run the tests"
	@echo "   make install    - to install (PREFIX=$(prefix))"
	@echo ""
.PHONY: all

configfiles: $(CONFIG_FILES)
	for dir in $(CONFIG_SUBDIRS); do \
	  $(MAKE) -C $$dir configfiles; \
	done
.PHONY: configfiles

$(CONFIG_FILES): %: %.in config.status
	./config.status $@

config.status: configure
	./config.status --recheck

configure: configure.in $(M4_FILES)
	(cd $(top_srcdir); autoconf --force)
	(cd $(top_srcdir); autoheader --force)

src: liboctave

liboctave: libcruft

$(SUBDIRS):
	$(MAKE) -C $@ all
.PHONY: $(SUBDIRS)

octave-bug: octave-bug.in Makeconf octMakefile $(top_srcdir)/src/version.h
	@$(do-subst-config-vals)
	chmod a+rx $@

octave-bug.cc: octave-bug.cc.in Makeconf octMakefile
	@$(do-subst-config-vals)

ifneq ($(EXEEXT),)
octave-bug$(EXEEXT): octave-bug.o
	$(LD_CXX) $(CPPFLAGS) $(ALL_CXXFLAGS) $(RDYNAMIC_FLAG) \
	    $(ALL_LDFLAGS) -o $@ octave-bug.o
endif

octave-config: octave-config.in Makeconf octMakefile $(top_srcdir)/src/version.h
	@$(do-subst-default-vals)
	chmod a+rx $@

octave-config.cc: octave-config.cc.in Makeconf octMakefile
	@$(do-subst-default-vals)

ifneq ($(EXEEXT),)
octave-config$(EXEEXT): octave-config.o
	$(LD_CXX) $(CPPFLAGS) $(ALL_CXXFLAGS) $(RDYNAMIC_FLAG) \
	    $(ALL_LDFLAGS) -o $@ octave-config.o
endif

mkoctfile: mkoctfile.in Makeconf octMakefile $(top_srcdir)/src/version.h
	@$(do-subst-config-vals)
	chmod a+rx $@

mkoctfile.cc: mkoctfile.cc.in Makeconf octMakefile
	@$(do-subst-config-vals)

ifneq ($(EXEEXT),)
mkoctfile$(EXEEXT): mkoctfile.o
	$(LD_CXX) $(CPPFLAGS) $(ALL_CXXFLAGS) $(RDYNAMIC_FLAG) \
	    $(ALL_LDFLAGS) -o $@ mkoctfile.o
endif

run-octave: run-octave.in Makeconf octMakefile
	@$(do-subst-script-vals)
	chmod a+rx "$@"

check:
	$(MAKE) -C test $@
.PHONY: check

octave.info:
	$(MAKE) -C doc/interpreter octave.info
.PHONY: octave.info

BUGS INSTALL.OCTAVE:
	$(MAKE) -C doc ../$@
.PHONY: BUGS INSTALL.OCTAVE

install install-strip ::
	$(top_srcdir)/mkinstalldirs $(addprefix $(DESTDIR), $(DIRS_TO_MAKE))
	rm -f $(DESTDIR)$(bindir)/octave-bug$(SCRIPTS_EXE_SUFFIX)
	$(INSTALL_SCRIPT) octave-bug$(SCRIPTS_EXE_SUFFIX) \
	    $(DESTDIR)$(bindir)/octave-bug-$(version)$(SCRIPTS_EXE_SUFFIX)
	(cd $(DESTDIR)$(bindir); $(LN_S) octave-bug-$(version)$(SCRIPTS_EXE_SUFFIX) \
	    $(DESTDIR)$(bindir)/octave-bug$(SCRIPTS_EXE_SUFFIX))
	rm -f $(DESTDIR)$(bindir)/octave-config$(SCRIPTS_EXE_SUFFIX)
	$(INSTALL_SCRIPT) \
	  octave-config$(SCRIPTS_EXE_SUFFIX) $(DESTDIR)$(bindir)/octave-config-$(version)$(SCRIPTS_EXE_SUFFIX)
	(cd $(DESTDIR)$(bindir); $(LN_S) octave-config-$(version)$(SCRIPTS_EXE_SUFFIX) \
	    $(DESTDIR)$(bindir)/octave-config$(SCRIPTS_EXE_SUFFIX))
	rm -f $(DESTDIR)$(bindir)/mkoctfile$(SCRIPTS_EXE_SUFFIX)
	$(INSTALL_SCRIPT) mkoctfile$(SCRIPTS_EXE_SUFFIX) \
	    $(DESTDIR)$(bindir)/mkoctfile-$(version)$(SCRIPTS_EXE_SUFFIX)
	(cd $(DESTDIR)$(bindir); $(LN_S) mkoctfile-$(version)$(SCRIPTS_EXE_SUFFIX) \
	    $(DESTDIR)$(bindir)/mkoctfile$(SCRIPTS_EXE_SUFFIX))
	$(INSTALL_DATA) config.h $(DESTDIR)$(octincludedir)/octave/config.h
	$(INSTALL_DATA) $(srcdir)/NEWS $(DESTDIR)$(octetcdir)/NEWS

uninstall::
	rm -f $(DESTDIR)$(bindir)/octave-bug$(SCRIPTS_EXE_SUFFIX)
	rm -f $(DESTDIR)$(bindir)/octave-bug-$(version)$(SCRIPTS_EXE_SUFFIX)
	rm -f $(DESTDIR)$(bindir)/octave-config$(SCRIPTS_EXE_SUFFIX)
	rm -f $(DESTDIR)$(bindir)/octave-config-$(version)$(SCRIPTS_EXE_SUFFIX)
	rm -f $(DESTDIR)$(bindir)/mkoctfile$(SCRIPTS_EXE_SUFFIX)
	rm -f $(DESTDIR)$(bindir)/mkoctfile-$(version)$(SCRIPTS_EXE_SUFFIX)
	rm -f $(DESTDIR)$(octincludedir)/octave/config.h
	rm -f $(DESTDIR)$(octetcdir)/NEWS

maintainer-clean::
	@echo ""
	@echo "************************************************************"
	@echo "*                                                          *"
	@echo "* This command is intended for maintainers to use; it      *"
	@echo "* deletes files that may require special tools to rebuild. *"
	@echo "*                                                          *"
	@echo "************************************************************"
	@echo ""

install install-strip uninstall tags TAGS::
	$(foreach d, $(INSTALL_SUBDIRS), $(do-subdir-for-command))
.PHONY: install install-strip uninstall tags

clean mostlyclean distclean maintainer-clean::
	$(foreach d, $(CLEANSUBDIRS), $(do-subdir-for-command))
.PHONY: clean mostlyclean distclean maintainer-clean

maintainer-clean distclean::
	rm -f octMakefile Makeconf
	rm -f config.cache config.h config.log config.status
	rm -rf autom4te.cache
	rm -f $(SHELL_SCRIPTS)
	rm -f mkoctfile.cc octave-config.cc octave-bug.cc
	rm -f unistd.h

maintainer-clean::
	rm -f configure config.h.in BUGS INSTALL.OCTAVE

maintainer-clean distclean clean::
	rm -f mkoctfile$(EXEEXT) octave-config$(EXEEXT) octave-bug$(EXEEXT)
	rm -f mkoctfile.o octave-config.o octave-bug.o

# Rules for making a source distribution.

dist-info-files: INSTALL.OCTAVE BUGS
.PHONY: dist-info-files

# The dist target depends on all because we use Octave to build some
# figures for the manual.  It's best to create those figures with the
# version of Octave that we are distributing (it may even be required).

dist: all
	echo octave-$(version) > .fname
	rm -rf `cat .fname`
	mkdir `cat .fname`
	ln $(addprefix $(srcdir)/, $(DISTFILES)) `cat .fname`
	ln $(BUILT_DISTFILES) `cat .fname`
	for dir in $(DISTSUBDIRS); do \
	  mkdir `cat .fname`/$$dir; \
	  $(MAKE) -C $$dir dist; \
	done
	tar chf `cat .fname`.tar `cat .fname`
	rm -rf `cat .fname`
	tar xf `cat .fname`.tar
	find `cat .fname` \( \( -name RCS -a -type d \) \
	  -o \( -name CVS -a -type d \) -o \( -name OLD -a -type d \) \
	  -o \( -name autom4te.cache -a -type d \) \
	  -o -name "=*" -o -name '*~' -o -name '#*#' -o -name config.log \
	  -o -name config.status -o -name config.cache -o -name stamp-h \
	  -o -name klibtool.config -o -name stamp-auto \
	  -o -name c-auto.h \) -print | xargs rm -rf
	rm -f `cat .fname`/test/octave.test/*.m
	chmod -R a+rwX `cat .fname`
	tar cf `cat .fname`.tar `cat .fname`
	rm -rf `cat .fname`
	gzip -9 --stdout `cat .fname`.tar > `cat .fname`.tar.gz
	bzip2 -9 --stdout `cat .fname`.tar > `cat .fname`.tar.bz2
	date -u > md5sum
	md5sum `cat .fname`.tar.gz `cat .fname`.tar.bz2 >> md5sum
	touch `cat .fname`.tar.gz `cat .fname`.tar.bz2 md5sum
	rm -f .fname
	@echo "*******************"
	@echo "Tag the CVS archive"
	@echo "*******************"
.PHONY: dist

# Rules for making a snapshot.

snapshot-version:
	@echo "creating src/version.h"
	@gawk '/#define OCTAVE_VERSION[ \t]*/ { \
	  datestring = strftime("%y%m%d", systime()); \
	  printf("#define OCTAVE_VERSION \"ss-%s\"\n", datestring); \
	  next; \
	} { print $$0 }' src/version.h > src/version.h.new
	@$(top_srcdir)/move-if-change src/version.h.new src/version.h
.PHONY: snapshot-version

snapshot: snapshot-version
	$(MAKE) dist
.PHONY: snapshot

# Rules for making a dist of just the stuff needed to run configure.

conf-dist:
	echo config-dist-$(version) > .fname
	rm -rf `cat .fname`
	mkdir `cat .fname`
	ln $(CONF_DISTFILES) `cat .fname`
	for dir in $(CONF_DISTSUBDIRS); do \
	  mkdir `cat .fname`/$$dir; \
	  $(MAKE) -C $$dir conf-dist; \
	done
	tar chf `cat .fname`.tar `cat .fname`
	rm -rf `cat .fname`
	gzip --best `cat .fname`.tar
	rm -f .fname
.PHONY: conf-dist

.NOTPARALLEL:
