TARGET_DIR = ./rust/target
LIBDIR = $(TARGET_DIR)/@LIBDIR@
STATLIB = $(LIBDIR)/lib{{{lib_name}}}.a
PKG_LIBS = -L$(LIBDIR) -l{{{lib_name}}}

all: $(SHLIB) rust_clean

.PHONY: $(STATLIB)

$(SHLIB): $(STATLIB)

CARGOTMP = $(CURDIR)/.cargo
VENDOR_DIR = $(CURDIR)/vendor


# RUSTFLAGS appends --print=native-static-libs to ensure that 
# the correct linkers are used. Use this for debugging if need. 
#
# CRAN note: Cargo and Rustc versions are reported during
# configure via tools/msrv.R.
#
# vendor.tar.xz, if present, is unzipped and used for offline compilation.
$(STATLIB):

	if [ -f ./rust/vendor.tar.xz ]; then \
		tar xf rust/vendor.tar.xz && \
		mkdir -p $(CARGOTMP) && \
		cp rust/vendor-config.toml $(CARGOTMP)/config.toml; \
	fi

	export CARGO_HOME=$(CARGOTMP) && \
	export PATH="$(PATH):$(HOME)/.cargo/bin" && \
	RUSTFLAGS="$(RUSTFLAGS) --print=native-static-libs" cargo build @CRAN_FLAGS@ --lib @PROFILE@ --manifest-path=./rust/Cargo.toml --target-dir $(TARGET_DIR) @TARGET@

	# Always clean up CARGOTMP
	rm -Rf $(CARGOTMP);

rust_clean: $(SHLIB)
	rm -Rf $(CARGOTMP) $(VENDOR_DIR) @CLEAN_TARGET@

clean:
	rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) $(TARGET_DIR) $(VENDOR_DIR)
