LIBDIR = myrustlib/target/release
STATLIB = $(LIBDIR)/libmyrustlib.a
PKG_CFLAGS = -pthread
PKG_CPPFLAGS = -DSTRICT_R_HEADERS -DR_NO_REMAP
PKG_LIBS = -L$(LIBDIR) -lmyrustlib -lresolv -pthread

all: $(SHLIB) cleanup

$(SHLIB): $(STATLIB)

# CRAN policy forbids using $HOME so we build in the current working dir and
# never use local cache. Also CRAN does not allow using more than 2 CPUs.
# This makes things really slow because we have to reinstall all dependencies.
# You can comment these things out if you do not submit to CRAN.
CRANFLAGS=-j 2
CARGOTMP=$(PWD)/.cargo
export CARGO_HOME=$(CARGOTMP)

$(STATLIB):
	if [ -f myrustlib/vendor.tar.xz ]; then tar xf myrustlib/vendor.tar.xz && mkdir -p $(CARGOTMP) && cp myrustlib/vendor-config.toml $(CARGOTMP)/config.toml; fi
	PATH="${PATH}:${HOME}/.cargo/bin" cargo build ${CRANFLAGS} --release --manifest-path=myrustlib/Cargo.toml
	rm -Rf $(CARGOTMP) vendor || true # CRAN wants us to remove "detritus"
	rm -Rf $(LIBDIR)/build || true

cleanup: $(SHLIB)
	rm -Rf $(STATLIB) myrustlib/target
