#-*- Makefile -*-
#
# ${R_HOME}/src/library/tools/Makefile

srcdir = .
top_srcdir = ../../..

top_builddir = ../../..

include $(top_builddir)/Makeconf

pkg = tools

WRE_DATA = $(top_builddir)/library/$(pkg)/misc/wre.txt
WRE_SRC = $(top_srcdir)/doc/manual/R-exts.texi

RSRC = `LC_COLLATE=C ls $(srcdir)/R/*.R`

ifdef USE_LLVM
  R_SYSTEM_ABI = "windows,clang,clang++,flang,flang"
else
  R_SYSTEM_ABI = "windows,gcc,g++,gfortran,gfortran"
endif

## We first use mkdesc2 (with date) to install the DESCRIPTION, then re-install
all:
	@$(ECHO) "building package '$(pkg)'"
	@$(MKINSTALLDIRS) $(top_builddir)/library/$(pkg)
	@$(MKINSTALLDIRS) $(top_builddir)/library/$(pkg)/misc
	@(cd "$(top_srcdir)" && pwd -W) \
	  > $(top_builddir)/library/$(pkg)/misc/top.txt
	@$(MAKE) -f Makefile.win mkR1 mkdesc2 mksrc-win2 $(WRE_DATA)
	@if ! test $(top_builddir)/library/$(pkg)/R/$(pkg) -nt all.R; then \
	  $(INSTALL_DATA) all.R $(top_builddir)/library/$(pkg)/R/$(pkg); \
	fi
	@$(MAKE) -f Makefile.win sysdata
	@$(ECHO) "tools:::.install_package_description('.', '"$(top_builddir)/library/${pkg}"')" | \
	  R_DEFAULT_PACKAGES=NULL $(R_EXE) > /dev/null

## avoid "$<" as that was empty when using bmake (tested 20200710 and 20241124)
$(WRE_DATA): $(WRE_SRC)
	@grep -E '^@(api|eapi|emb|for)(fun|var|hdr)' "$(WRE_SRC)" > "$@"

include $(top_srcdir)/share/make/basepkg.mk

## Note that R_COMPILER_SUPPRESS_ALL is now on by default
$(top_builddir)/library/$(pkg)/R/$(pkg).rdb: all.R $(top_srcdir)/share/licenses/license.db
	@$(ECHO) "byte-compiling package '$(pkg)'"
	@$(INSTALL_DATA) all.R $(top_builddir)/library/$(pkg)/R/$(pkg)
	@(cat $(srcdir)/R/makeLazyLoad.R; \
	  $(ECHO) "makeLazyLoading(\"$(pkg)\")") | \
	  _R_COMPILE_PKGS_=1 R_COMPILER_SUPPRESS_ALL=1 \
	  R_SYSTEM_ABI=$(R_SYSTEM_ABI) \
	  R_DEFAULT_PACKAGES=NULL LC_ALL=C $(R_EXE) > /dev/null

Rsimple: mkR mkRsimple

Rlazy: mkR mkRsimple
	@$(INSTALL_DATA) all.R $(top_builddir)/library/$(pkg)/R/$(pkg)
	@(cat $(srcdir)/R/makeLazyLoad.R; \
	  $(ECHO) "makeLazyLoading(\"$(pkg)\")") | \
	  R_SYSTEM_ABI=$(R_SYSTEM_ABI) \
	  R_DEFAULT_PACKAGES=NULL LC_ALL=C $(R_EXE) > /dev/null

Rlazycomp: mkR mkRsimple mklazycomp
