#
# Makefile for octave's libcruft 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@

# List of the directories that contain Fortran source.  Simply copying
# a new .f file into one of these directories is sufficient to have it
# added to libcruft.a.  If you add a new directory here, you also need
# generate a new configure script in the top-level directory (edit
# configure.in and run autoconf).

# Some of the directories in libcruft may be only optionally built,
# e.g. if they are already present on the system.  For these, their
# dirname is substituted by configure and may be the empty string.

CRUFT_DIRS = amos @BLAS_DIR@ blas-xtra daspk dasrt dassl \
	@FFT_DIR@ @LAPACK_DIR@ lapack-xtra minpack \
	misc odepack odessa ordered-qz quadpack ranlib \
	slatec-err slatec-fn villad

SUBDIRS = $(CRUFT_DIRS)

DISTSUBDIRS = $(sort $(CRUFT_DIRS) blas fftpack lapack)

CLEAN_SUBDIRS = $(DISTSUBDIRS)

DISTFILES = Makefile.in ChangeLog Makerules.in $(SOURCES) STOP.patch

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

# include BLAS and FFTW just in case some of libcruft depends on them
LINK_DEPS = $(BLAS_LIBS) $(FFTW_LIBS) $(FLIBS)

all: libraries
.PHONY: all

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

# XXX FIXME XXX -- this should build the shared library directly from
# a normal archive file (created from PIC code, though).

MISC_OBJ := misc/machar.o misc/f77-extern.o \
	misc/f77-fcn.o misc/lo-error.o misc/quit.o misc/cquit.o

CRUFT_FSRC := $(foreach dir, $(SUBDIRS), $(wildcard $(srcdir)/$(dir)/*.f))
CRUFT_OBJ2 := $(patsubst $(srcdir)/%, %, $(CRUFT_FSRC))
CRUFT_OBJ1 := $(patsubst %.f, %.o, $(CRUFT_OBJ2))
CRUFT_OBJ := $(CRUFT_OBJ1) $(MISC_OBJ)

ifeq ($(SHARED_LIBS), true)
  ifdef FPICFLAG
    CRUFT_OBJ_DIR := $(dir $(CRUFT_OBJ))
    CRUFT_OBJ_PICDIR := $(addsuffix pic/, $(CRUFT_OBJ_DIR))
    CRUFT_OBJ_NOTDIR := $(notdir $(CRUFT_OBJ))
    CRUFT_PICOBJ := $(join $(CRUFT_OBJ_PICDIR), $(CRUFT_OBJ_NOTDIR))
  else
    CRUFT_PICOBJ := $(CRUFT_OBJ)
  endif
endif

ifeq ($(SHARED_LIBS), true)
  ifeq ($(STATIC_LIBS), true)
    LIBRARIES = libcruft.$(LIBEXT) libcruft.$(SHLEXT_VER)
  else
    LIBRARIES = libcruft.$(SHLEXT_VER)
  endif
else
  ifeq ($(STATIC_LIBS), true)
    LIBRARIES = libcruft.$(LIBEXT)
  else
    LIBRARIES =
  endif
endif

libraries: $(SUBDIRS)
	$(MAKE) $(LIBRARIES)
.PHONY: libraries

libcruft.$(LIBEXT): $(CRUFT_OBJ)
	rm -f $@
	$(AR) $(ARFLAGS) $@ $^
	$(RANLIB) $@

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

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

$(CRUFT_OBJ):

check: all
.PHONY: check

install install-strip uninstall::
	@$(subdir-for-command)

clean mostlyclean distclean maintainer-clean::
	@$(foreach d, $(CLEAN_SUBDIRS), $(do-subdir-for-command))

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

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

uninstall::
	rm -f $(DESTDIR)$(octlibdir)/libcruft.$(LIBEXT)
	rm -f $(DESTDIR)$(octlibdir)/libcruft.$(SHLLIB)
	rm -f $(DESTDIR)$(octlibdir)/libcruft.$(SHLLIB_VER)
	if test x$(SHLBIN) != x; then \
	  rm -f $(DESTDIR)$(bindir)/libcruft.$(SHLBIN); \
	  rm -f $(DESTDIR)$(bindir)/libcruft.$(SHLBIN_VER); \
	fi

tags TAGS:: $(SOURCES)
	$(SUBDIR_FOR_COMMAND)

tags::
	ctags $(SOURCES)

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

clean mostlyclean::
	rm -f *.$(LIBEXT) *.o
	if $(SHARED_LIBS); then rm -f *.$(SHLEXT_VER) *.$(SHLEXT); fi

distclean::
	rm -f *.$(LIBEXT) *.o Makefile Makerules so_locations
	if $(SHARED_LIBS); then rm -f *.$(SHLEXT_VER) *.$(SHLEXT); fi

maintainer-clean::
	rm -f *.$(LIBEXT) *.o configure Makefile Makerules 
	rm -f so_locations tags TAGS
	if $(SHARED_LIBS); then rm -f *.$(SHLEXT_VER) *.$(SHLEXT); fi

dist:
	for dir in $(DISTSUBDIRS); do mkdir ../`cat ../.fname`/libcruft/$$dir; $(MAKE) -C $$dir $@; done
	ln $(DISTFILES) ../`cat ../.fname`/libcruft
.PHONY: dist

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

.NOTPARALLEL:
