#
# 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).

CRUFT_DIRS = amos balgen blas dassl eispack fftpack lapack linpack \
	minpack misc odepack quadpack ranlib slatec-err \
	slatec-fn villad

SUBDIRS = $(CRUFT_DIRS)

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

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

all: $(SUBDIRS) shared-lib
.PHONY: all

$(SUBDIRS):
	echo making all in $@
	cd $@; $(MAKE) 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/dostop.o misc/f77-extern.o \
	misc/f77-fcn.o misc/lo-error.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)
shared-lib: libcruft.$(SHLEXT)
else
shared-lib:
endif
.PHONY: shared-lib

libcruft.$(SHLEXT): $(CRUFT_PICOBJ)
	$(SH_LD) $(SH_LDFLAGS) -o $@ $^

$(CRUFT_OBJ):

check: all
.PHONY: check

install uninstall clean mostlyclean distclean maintainer-clean::
	@$(subdir-for-command)

install::
	$(top_srcdir)/mkinstalldirs $(octlibdir)
	if $(STATIC_LIBS); then \
	  rm -f $(octlibdir)/libcruft.$(LIBEXT) ; \
	  $(INSTALL_DATA) libcruft.$(LIBEXT) \
	    $(octlibdir)/libcruft.$(LIBEXT) ; \
	  $(RANLIB) $(octlibdir)/libcruft.$(LIBEXT) ; \
	fi
	if $(SHARED_LIBS); then \
	  rm -f $(octlibdir)/libcruft.$(SHLEXT); \
	  $(INSTALL_PROGRAM) \
	    libcruft.$(SHLEXT) $(octlibdir)/libcruft.$(SHLEXT); \
	fi
	$(mk-libdir-link)

uninstall::
	rm -f $(octlibdir)/libcruft.$(LIBEXT)
	rm -f $(octlibdir)/libcruft.$(SHLEXT)

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

tags::
	ctags $(SOURCES)

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

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

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

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

dist:
	for dir in $(SUBDIRS); do mkdir ../`cat ../.fname`/libcruft/$$dir; cd $$dir; $(MAKE) $@; cd ..; 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
