TARGET = $(subst 64,x86_64,$(subst 32,i686,$(WIN)))-pc-windows-gnu

TARGET_DIR = ./rust/target
LIBDIR = $(TARGET_DIR)/$(TARGET)/@LIBDIR@
STATLIB = $(LIBDIR)/lib{{{lib_name}}}.a
PKG_LIBS = -L$(LIBDIR) -l{{{lib_name}}} -lws2_32 -ladvapi32 -luserenv -lbcrypt -lntdll

all: $(SHLIB) rust_clean

.PHONY: $(STATLIB)

$(SHLIB): $(STATLIB)

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

$(STATLIB):
	mkdir -p $(TARGET_DIR)/libgcc_mock
	touch $(TARGET_DIR)/libgcc_mock/libgcc_eh.a

  # If a vendor directory exists, it is used for offline compilation. Otherwise if
  # vendor.tar.xz exists, it is unzipped and used for offline compilation.
	if [ -d ./vendor ]; then \
		echo "=== Using offline vendor directory ==="; \
		mkdir -p $(CARGOTMP) && \
		cp rust/vendor-config.toml $(CARGOTMP)/config.toml; \
	elif [ -f ./rust/vendor.tar.xz ]; then \
		echo "=== Using offline vendor tarball ==="; \
		tar xf rust/vendor.tar.xz && \
		mkdir -p $(CARGOTMP) && \
		cp rust/vendor-config.toml $(CARGOTMP)/config.toml; \
	fi

	# Build the project using Cargo with additional flags
	export CARGO_HOME=$(CARGOTMP) && \
	export LIBRARY_PATH="$(LIBRARY_PATH);$(CURDIR)/$(TARGET_DIR)/libgcc_mock" && \
	RUSTFLAGS="$(RUSTFLAGS) --print=native-static-libs" cargo build @CRAN_FLAGS@ --target=$(TARGET) --lib @PROFILE@ --manifest-path=rust/Cargo.toml --target-dir=$(TARGET_DIR)

	# Generate wrappers
	export CARGO_HOME=$(CARGOTMP) && \
	export PATH="$(PATH):$(HOME)/.cargo/bin" && \
	cargo run @CRAN_FLAGS@ --bin document --target $(TARGET) --manifest-path=./rust/Cargo.toml --target-dir $(TARGET_DIR)

	# 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)
