#
# ${R_HOME}/src/main/Makefile

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

top_builddir = ../..
subdir = src/main

include $(top_builddir)/Makeconf

SOURCES_C = \
        CConverters.c \
	Rdynload.c RNG.c \
	apply.c arithmetic.c array.c attrib.c \
	bind.c builtin.c \
	character.c coerce.c colors.c complex.c connections.c context.c \
	cov.c cum.c \
	dcf.c datetime.c debug.c devPS.c devPicTeX.c deparse.c deriv.c \
	devices.c dotcode.c dstruct.c duplicate.c \
	envir.c errors.c eval.c \
	format.c fourier.c \
	gram.c gram-ex.c graphics.c \
	internet.c iosupport.c \
	lapack.c list.c logic.c \
	main.c match.c memory.c model.c \
	names.c \
	objects.c optim.c optimize.c options.c \
	par.c paste.c platform.c \
	plot.c plot3d.c plotmath.c \
	print.c printarray.c printvector.c printutils.c \
	random.c regex.c relop.c \
	saveload.c scan.c seq.c size.c sort.c source.c split.c \
	subassign.c subscript.c subset.c summary.c \
	unique.c util.c \
	version.c vfonts.c

SOURCES_F = \
	xxxpr.f
## FIXME: Need better names ...
EXTRA_SOURCES_C = alloca.c strdup.c
## </FIXME>
DEPENDS = $(SOURCES_C:.c=.d) $(EXTRA_SOURCES_C:.c=.d)
SOURCES = $(SOURCES_C) $(SOURCES_F)
OBJECTS = $(SOURCES_C:.c=.o) $(SOURCES_F:.f=.o)
HEADERS = \
	Rregex.h Rstrptime.h \
	arithmetic.h

distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)
DISTFILES = Makefile.in Makefile.win \
	$(HEADERS) \
	$(SOURCES_C) \
	$(EXTRA_SOURCES_C) \
	$(SOURCES_F) \
	gram.y

## FIXME:
## Names in here should use `$(top_builddir)' instead ...
RLIBS = ../unix/libunix.a ../appl/libappl.a ../nmath/libnmath.a 
## </FIXME>

Rexecbin_PROGRAMS = R.bin
R_bin_SOURCES = $(SOURCES)
R_bin_OBJECTS = $(OBJECTS)
## FIXME:
## This will not be good enough on AIX, where we need to play also with
## -export-symbols SYMFILE.
@USE_LIBTOOL_TRUE@R_bin_LDFLAGS = -export-dynamic
## </FIXME>
R_bin_LDADD = @LIBOBJS@ @ALLOCA@ $(RLIBS) $(BLAS_LIBS) $(FLIBS) $(R_XTRA_LIBS)
## NOTE:
## Automake would figure this out itself.
R_bin_DEPENDENCIES = @LIBOBJS@ @ALLOCA@ $(RLIBS) @EXPORTFILE@
## </NOTE>

## FIXME:
## Change to `libR.la' when using libtool for shlibs.
## Remove when using automake ... also fix install then.
libR_la = libR.$(SHLIB_EXT)
## </FIXME>
Rexeclib_LTLIBRARIES = $(libR_la)
libR_la_SOURCES = $(SOURCES)
libR_la_LDFLAGS = 
libR_la_OBJECTS = $(OBJECTS:.o=.lo)
## FIXME:
## This should also have `ls ../appl/*.lo ../nmath/*.lo ../unix/*.lo`, but
## of course this will not work when the files do not exist yet :-)
libR_la_LIBADD = @LTLIBOBJS@ @LTALLOCA@ $(BLAS_LIBS) $(FLIBS) $(R_XTRA_LIBS)
libR_la_DEPENDENCIES = @LTLIBOBJS@ @LTALLOCA@ ../appl/stamp-lo ../nmath/stamp-lo ../unix/stamp-lo
## </FIXME>
@USE_LIBTOOL_TRUE@libR_la_LDFLAGS = -avoid-version -rpath $(Rexeclibdir)

all: Makefile Makedeps R

Makefile: $(srcdir)/Makefile.in \
  $(top_builddir)/config.status \
  ../include/config.h \
  ../include/Rversion.h \
  $(SOURCES_C)
	@cd $(top_builddir) && \
	  CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= \
	  $(SHELL) ./config.status

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

R: Makefile
	@$(MAKE) Makedeps
	@$(MAKE) $(Rexecbin_PROGRAMS)
@WANT_R_SHLIB_TRUE@	@$(MAKE) $(Rexeclib_LTLIBRARIES)

../include/config.h ../include/Rversion.h:
	(cd $(@D); $(MAKE) $(@F))

R.bin: $(R_bin_OBJECTS) $(R_bin_DEPENDENCIES)
	$(MAIN_LINK) -o $@ $(R_bin_LDFLAGS) $(R_bin_OBJECTS) $(R_bin_LDADD) $(LIBS)
## FIXME: Not sure if we want to do this ...
	@$(MAKE) rhome=`cd $(top_builddir); pwd` install-bin
## </FIXME>

$(top_builddir)/etc/R.exp: $(OBJECTS) $(RLIBS)
	@$(top_srcdir)/tools/ldAIX4 -o $@ $(OBJECTS) $(RLIBS)

$(RLIBS):
	(cd $(@D); $(MAKE) $(@F))

libR: $(libR_la)
$(libR_la): $(libR_la_OBJECTS) $(libR_la_DEPENDENCIES)
	$(SHLIB_LINK) -o $@ $(libR_la_LDFLAGS) $(libR_la_OBJECTS) `ls ../appl/*.lo ../nmath/*.lo ../unix/*.lo  2>/dev/null` $(libR_la_LIBADD) $(LIBS)
## FIXME: Not sure if we want to do this ...
	@$(MAKE) rhome=`cd $(top_builddir); pwd` install-lib
## </FIXME>

../appl/stamp-lo ../nmath/stamp-lo ../unix/stamp-lo:
	(cd $(@D); $(MAKE) $(@F))

$(srcdir)/gram.c: @MAINTAINER_MODE_TRUE@ $(srcdir)/gram.y
	$(YACC) $(YFLAGS) $(srcdir)/gram.y
	$(top_srcdir)/tools/move-if-change y.tab.c $@

install: installdirs
	@$(MAKE) install-bin
	@if test -f $(libR_la); then $(MAKE) install-lib; fi
installdirs:
	@$(MKINSTALLDIRS) $(Rexecbindir)
	@$(MKINSTALLDIRS) $(Rexeclibdir)
install-bin:
	@$(top_srcdir)/tools/copy-if-change R.bin $(Rexecbindir)/R.bin
install-lib:
@USE_LIBTOOL_FALSE@	@$(top_srcdir)/tools/copy-if-change $(libR_la) $(Rexeclibdir)/libR.$(SHLIB_EXT)
@USE_LIBTOOL_TRUE@	@$(LIBTOOL) --mode=install $(INSTALL) $(libR_la) $(Rexeclibdir)
install-strip:
	$(MAKE) INSTALL_PROGRAM="${INSTALL_PROGRAM} -s" install
uninstall:
	@rm -f $(Rexecbindir)/R.bin
	@rmdir $(Rexecbindir) 2>/dev/null \
	  || echo "  subdir $(Rexecbindir) not removed"
	@rm -f $(Rexeclibdir)/libR.$(SHLIBEXT)
	@rmdir $(Rexeclibdir) 2>/dev/null \
	  || echo "  subdir $(Rexeclibdir) not removed"

mostlyclean: clean
clean:
	@-rm -rf .libs _libs
	@-rm -f *core Makedeps *.d *.o *.lo *.la *.$(SHLIB_EXT) \
	  $(Rexecbin_PROGRAMS)
distclean: clean
	@-rm -f Makefile
maintainer-clean: distclean
	@echo "This command is intended for maintainers to use; it"
	@echo "deletes files that may need special rules to rebuild"
	@rm -f $(srcdir)/gram.c

tags: TAGS
TAGS: $(SOURCES) $(EXTRA_SOURCES_C) $(HEADERS)
	etags $(SOURCES) $(EXTRA_SOURCES_C) $(HEADERS)

## Unused targets
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: