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 [ -f ./rust/vendor.tar.xz ]; then \
		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)

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