#
# ${R_HOME}/src/modules/lapack/Makefile

VPATH = @srcdir@
srcdir = @srcdir@
top_srcdir = @top_srcdir@

top_builddir = ../../..
subdir = src/modules/lapack
R_HOME = $(top_builddir)

include $(top_builddir)/Makeconf
.SUFFIXES:
.SUFFIXES: .c .f .f90 .d .o
.f90.o:
	$(FC) $(ALL_FCFLAGS) -c @FCFLAGS_f90@ $< -o $@

SOURCES_C = Lapack.c @USE_VECLIB_G95FIX_TRUE@ vecLibg95c.c
SOURCES_F = @USE_VECLIB_G95FIX_TRUE@ vecLibg95f.f

DEPENDS = $(SOURCES_C:.c=.d)
SOURCES = $(SOURCES_C) $(SOURCES_F) $(SOURCES_F90)
OBJECTS = $(SOURCES_C:.c=.o) $(SOURCES_F:.f=.o) flexiblas.o
HEADERS = Lapack.h

## Sources for -lRlapack.
## On macOS this will be linked directly to the vecLib/Accelerate
## framework if --with-blas=Acceleraate --with-lapack is used, so
## needs the zdot* fix in lapack.so.
LIBSOURCES = dlamch.f dlapack.f \
  @COMPILE_FORTRAN_DOUBLE_COMPLEX_FALSE@ cmplx.f
LIBSOURCES90 = dlartg.f90 dlassq.f90 la_constants.f90 la_xisnan.f90 \
  @COMPILE_FORTRAN_DOUBLE_COMPLEX_FALSE@ zlartg.f90 zlassq.f90
LIBOBJECTS = $(LIBSOURCES:.f=.o) $(LIBSOURCES90:.f90=.o) \
  @USE_VECLIB_G95FIX_TRUE@ vecLibg95f.o vecLibg95c.o

distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)
DISTFILES = \
	LICENSE README Makefile.in Makefile.win \
	Rlapackrc.rc dllversion.rc \
	$(HEADERS) Lapack.c init_win.c \
	vecLibg95c.c vecLibg95f.f accelerateLapack.c \
	cmplx.f dlapack.f dlamch.f \
	dlartg.f90 dlassq.f90 la_constants.f90 la_xisnan.f90 \
	zlartg.f90 zlassq.f90 \
	dble.txt cmplx.txt R.patch

lapack_la = lapack$(SHLIB_EXT)
lapack_la_OBJECTS = $(OBJECTS)
## Formerly used for passing '+s' to SHLIB_LINK on HP-UX.
lapack_la_LDFLAGS = @LAPACK_LDFLAGS@
EXTRA_LIBS = @LAPACK_LIBS@ @BLAS_LIBS@ $(FLIBS_IN_SO)

lapack_la_LIBADD = $(LIBR1) @DYLIB_UNDEFINED_ALLOWED_FALSE@ $(LIBINTL)

Rlapack_la = libRlapack$(R_DYLIB_EXT)

@BUILD_NEW_ACCELERATE_LAPACK_TRUE@Rlapack_la_OBJECTS = accelerateLapack.o
@BUILD_NEW_ACCELERATE_LAPACK_FALSE@Rlapack_la_OBJECTS = $(LIBOBJECTS)
## This used to skip FLIBS, but that relied on all the functions
## needed being loaded into R.bin/libR.so, and that need not be the
## case for a static libfortran or if a package is compiled under a
## different compiler.
@BUILD_NEW_ACCELERATE_LAPACK_FALSE@Rlapack_la_LIBADD =  $(FLIBS_IN_SO) @DYLIB_UNDEFINED_ALLOWED_FALSE@ $(LIBR)
@BUILD_NEW_ACCELERATE_LAPACK_TRUE@Rlapack_la_LIBADD = -framework Accelerate

ALL_CFLAGS = $(ALL_CFLAGS_LO)
ALL_FFLAGS = $(ALL_FFLAGS_LO)
ALL_FCFLAGS = $(ALL_FFLAGS_LO)

all: R

flexiblas.o: ../../main/flexiblas.c
	$(CC) $(ALL_CPPFLAGS) $(ALL_CFLAGS) -c  $< -o $@

Makefile: $(srcdir)/Makefile.in \
  $(top_builddir)/config.status \
  $(SOURCES)
	@cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@

Makedeps: Makefile $(DEPENDS)
	@cat $(DEPENDS) >> Makefile
	@touch $@

## treated separately: e.g. we need to force stores with 
## gcc >= 3.4.0 at least on i686
## (SHLIB_FFLAGS is apparently currently unused)
LTO = @LTO_FC@
DLAMC_FFLAGS=$(FPICFLAGS) $(SHLIB_FFLAGS) @SAFE_FFLAGS@ $(LTO_FC)
dlamch.o: dlamch.f
	$(FC) $(DLAMC_FFLAGS) -c $< -o $@

la_constants.mod: la_constants.o
la_xisnan.mod: la_xisnan.o
dlartg.o dlassq.o zlartg.o zlassq.o la_xisnan.o: la_constants.o
dlassq.o zlassq.o: la_xisnan.o

R: Makedeps
@USE_EXTERNAL_LAPACK_FALSE@	@$(MAKE) rhome="$(abs_top_builddir)" $(Rlapack_la) Rlapack_install
	@$(MAKE) $(lapack_la)

$(lapack_la): $(lapack_la_OBJECTS)
	$(SHLIB_LINK) -o $@ $(lapack_la_LDFLAGS) $(lapack_la_OBJECTS) $(lapack_la_LIBADD) $(EXTRA_LIBS)
## <FIXME>
## Not sure if we want to do this ...
	@$(MAKE) rhome="$(abs_top_builddir)" install
## </FIXME>

## Include BLAS here, as with (static) ATLAS that pulls all the
## BLAS routines into one place.
## FIXME:  @BLAS_LIBS@ is probably  not needed for newAccelerate.
$(Rlapack_la): $(Rlapack_la_OBJECTS)
	$(DYLIB_LINK) -o $@ $(Rlapack_la_OBJECTS) @RLAPACK_LDFLAGS@ @BLAS_LIBS@ $(Rlapack_la_LIBADD)

Rlapack_install: $(Rlapack_la) 
	@$(MKINSTALLDIRS) "$(DESTDIR)$(Rexeclibdir)"
	@$(SHELL) $(top_srcdir)/tools/copy-if-change $(Rlapack_la) \
	  "$(Rexeclibdir)/$(Rlapack_la)"

install: installdirs
	@$(SHELL) $(top_srcdir)/tools/copy-if-change $(lapack_la) "$(DESTDIR)$(Rexecmodulesdir)/$(lapack_la)"
@USE_EXTERNAL_LAPACK_FALSE@	@$(SHELL) $(top_srcdir)/tools/copy-if-change $(Rlapack_la) "$(DESTDIR)$(Rexeclibdir)/$(Rlapack_la)"
installdirs:
	@$(MKINSTALLDIRS) "$(DESTDIR)$(Rexecmodulesdir)"
install-strip-Rlapack:
	@$(SHELL) $(top_srcdir)/tools/copy-if-change $(Rlapack_la) "$(DESTDIR)$(Rexeclibdir)/$(Rlapack_la)"
	@if test -n "$(STRIP_SHARED_LIB)"; then \
	  $(STRIP_SHARED_LIB) "$(DESTDIR)$(Rexeclibdir)/$(Rlapack_la)"; \
	fi
install-strip: @USE_EXTERNAL_LAPACK_FALSE@ install-strip-Rlapack
	@$(SHELL) $(top_srcdir)/tools/copy-if-change $(lapack_la) "$(DESTDIR)$(Rexecmodulesdir)/$(lapack_la)"
	@if test -n "$(STRIP_SHARED_LIB)"; then \
	  $(STRIP_SHARED_LIB) "$(DESTDIR)$(Rexecmodulesdir)/$(lapack_la)"; \
	fi
uninstall:
	@rm -f "$(Rexecmodulesdir)/$(lapack_la)"

mostlyclean: clean
clean:
	@-rm -Rf .libs _libs
	@-rm -f Makedeps *.d *.o *.a *.lo *.la *.mod \
	  $(lapack_la) $(Rlapack_la)
distclean: clean
	@-rm -f Makefile
maintainer-clean: distclean

TAGS info dvi check:

distdir: $(DISTFILES)
	@for f in $(DISTFILES); do \
	  test -f $(distdir)/$${f} \
	    || ln $(srcdir)/$${f} $(distdir)/$${f} 2>/dev/null \
	    || cp -p $(srcdir)/$${f} $(distdir)/$${f}; \
	done

## Automagically generated dependencies:

