TARGET_DIR = ./rust/target
LIBDIR = $(TARGET_DIR)/release
STATLIB = $(LIBDIR)/libjsslintr.a
PKG_LIBS = -L$(LIBDIR) -ljsslintr

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

all: $(SHLIB) rust_clean

.PHONY: $(STATLIB)

$(SHLIB): $(STATLIB)

# CRAN Repository Policy forbids install-time network access and writes
# outside the build area. rust/vendor.tar.xz (built by
# tools/vendor-crate-archive.sh) holds every crates.io dependency, and
# rust/vendor-config.toml is the matching source-replacement config that
# makes `cargo build --offline` resolve them from there instead of
# ~/.cargo. R/extendr-wrappers.R (normally regenerated here via
# `cargo run --bin document`) is pre-generated and committed instead —
# running an extra binary at install time bought nothing offline can't
# also guarantee, and check-standard.yaml diffs the committed copy
# against a fresh regeneration so it can't silently drift.
#
# The CRAN macOS builders have rustc/cargo under ~/.cargo/bin but NOT on
# PATH (documented in CRAN's "Using Rust in CRAN packages"), so every
# recipe line that runs the toolchain appends it. Appending (not
# prepending) keeps a toolchain that IS on PATH taking precedence.
$(STATLIB):
	export PATH="$(PATH):$(HOME)/.cargo/bin" && \
		rustc --version && \
		cargo --version
	tar xf rust/vendor.tar.xz
	mkdir -p $(CARGOTMP)
	cp rust/vendor-config.toml $(CARGOTMP)/config.toml
	export PATH="$(PATH):$(HOME)/.cargo/bin" && \
		export CARGO_HOME=$(CARGOTMP) && \
		cargo build --offline --locked --lib --release -j 2 --manifest-path=./rust/Cargo.toml --target-dir $(TARGET_DIR)
	rm -Rf $(CARGOTMP)

rust_clean: $(SHLIB)
	rm -Rf $(TARGET_DIR) $(VENDOR_DIR) $(CARGOTMP)

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