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

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

top_builddir = ../..
subdir = src/unix

include $(top_builddir)/Makeconf
# see note in src/main/Makefile.in
LIBINTL= @BUILD_LIBINTL_FALSE@ @LIBINTL@
R_HOME=$(rhome)
STATIC_LIBR_PC = @WANT_R_STATIC_TRUE@ $(BLAS_LIBS) $(FLIBS) @R_XTRA_LIBS@ $(LIBINTL) @READLINE_LIBS@ $(LIBS)

ALL_CPPFLAGS = $(R_XTRA_CPPFLAGS) $(X_CFLAGS) $(CPPFLAGS) $(DEFS)

## Rscript should not be linked to OpenMP
ALL_CFLAGS0 = $(R_XTRA_CFLAGS) $(MAIN_CFLAGS) $(CFLAGS)

SOURCES_C_BASE = Rembedded.c dynload.c system.c sys-unix.c sys-std.c X11.c
SOURCES = $(SOURCES_C_BASE)
DEPENDS = $(SOURCES:.c=.d)
OBJECTS = $(SOURCES:.c=.o)
HEADERS = Runix.h hpdlfcn.h

distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)
DISTFILES = \
  Makefile.in $(SOURCES_C_BASE) Rscript.c \
  $(HEADERS) hpdlfcn.c libR.pc.in system.txt

@WANT_R_SHLIB_TRUE@ALL_CFLAGS = $(ALL_CFLAGS_LO)

all: R

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

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

R: Makedeps
	@$(MAKE) libunix.a
	@$(MAKE) rhome="$(abs_top_builddir)" install-Rscript

## is this portable?
../include/Rversion.h:
	(cd ../include; $(MAKE) $(@F))

libunix.a: $(OBJECTS)
	@rm -Rf $@
	$(AR) -cr $@ $(OBJECTS)
	$(RANLIB) $@

mostlyclean: clean
clean:
	@-rm -Rf .libs _libs Rscript.dSYM
	@-rm -f Makedeps *.d *.o *.a *.lo *.la stamp-lo
distclean: clean
	@-rm -f Makefile Rscript
maintainer-clean: distclean

## we need to build at install time to capture the correct 'rhome'
install-Rscript:
	$(CC) $(ALL_CPPFLAGS) $(ALL_CFLAGS0) $(LDFLAGS) -DR_HOME='"$(rhome)"' \
	  -o Rscript $(srcdir)/Rscript.c
	@$(MKINSTALLDIRS) "$(DESTDIR)$(Rexecbindir)"
	@$(INSTALL_PROGRAM) Rscript "$(DESTDIR)$(Rexecbindir)/Rscript"
## substitute at install time to allow 'prefix' to be overridden
install-pc:
	@$(MKINSTALLDIRS) "$(DESTDIR)$(libdir)/pkgconfig"
	-@$(SED) -e "s:@rhome:$(rhome):" \
	  -e "s:@rincludedir:$(rincludedir):" \
	  -e "s:@rarch:$(R_ARCH):" \
	  -e 's:@libsprivate:$(STATIC_LIBR_PC):' \
	  -e 's:@others:$(MAIN_LDFLAGS) $(LDFLAGS):' \
	  -e 's/@VERSION/$(VERSION)/' $(srcdir)/libR.pc.in \
	  > "$(DESTDIR)$(libdir)/pkgconfig/libR.pc"

install: install-main
install: @WANT_R_SHLIB_TRUE@ install-pc
install: @WANT_R_STATIC_TRUE@ install-pc

install-main: install-Rscript
	@$(MKINSTALLDIRS) "$(DESTDIR)$(bindir)"
	@$(INSTALL_PROGRAM) Rscript "$(DESTDIR)$(bindir)/Rscript"
	@if test -n "${R_ARCH}"; then \
	  $(MKINSTALLDIRS) "$(DESTDIR)$(Rexecbindir2)"; \
	  $(INSTALL_PROGRAM) Rscript "$(DESTDIR)$(Rexecbindir2)/Rscript"; \
	fi

uninstall:
	@rm -f "$(DESTDIR)$(bindir)/Rscript" "$(DESTDIR)$(Rexecbindir)/Rscript" \
	  "$(DESTDIR)$(Rexecbindir2)/Rscript" \
	  "$(DESTDIR)$(libdir)/pkgconfig/libR.pc"

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

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:

