.PHONY: default install local_wheel inplace test sdist cross_wheels upload \
        rustup_targets clean clean_wheel clean_rust uninstall

LOCAL_DIST := dist/local
PYPI_DIST := dist/pypi
CARGO_TARGET_DIR ?= target

GNU_TARGETS := x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu
MUSL_TARGETS := x86_64-unknown-linux-musl aarch64-unknown-linux-musl
WIN_TARGETS := x86_64-pc-windows-msvc aarch64-pc-windows-msvc
MAC_TARGETS := universal2-apple-darwin

MATURIN_BUILD := maturin build --release --out $(PYPI_DIST)


ifeq ($(OS), Windows_NT)
    BUILT_LIB := $(CARGO_TARGET_DIR)/release/_pulldown_latex.dll
    EXT_MODULE := dochint/_pulldown_latex.pyd
else ifeq ($(shell uname -s), Darwin)
    BUILT_LIB := $(CARGO_TARGET_DIR)/release/lib_pulldown_latex.dylib
    EXT_MODULE := dochint/_pulldown_latex.abi3.so
else
    BUILT_LIB := $(CARGO_TARGET_DIR)/release/lib_pulldown_latex.so
    EXT_MODULE := dochint/_pulldown_latex.abi3.so
endif

define HELP_MESSAGE
Usage: make <target>

Building and installing from source
    local_wheel
		Build a wheel for this machine into dist/local
    install
		Build and install system-wide (uses sudo)
    uninstall
		Remove a system-wide install (uses sudo)

Testing
    inplace
		Build the native module into dochint/ for in-tree use
    test
		Build in place, then run the test suite

Releasing to PyPI
    rustup_targets
		Install the Rust targets cross_wheels needs
    sdist
		Build the source distribution into dist/pypi
    cross_wheels
		Build the sdist and wheels for all platforms into dist/pypi
    upload
		Build everything for PyPI, check it, then upload it

Cleaning up
    clean
		Remove all build outputs
    clean_wheel
		Remove Python build outputs (build/, dist/, egg-info)
    clean_rust
		Remove Cargo's build directory and in-place modules
endef
export HELP_MESSAGE

default:
	@echo "$${HELP_MESSAGE}"

install: local_wheel
	sudo python -m installer $(LOCAL_DIST)/*.whl

local_wheel:
	python -m build --wheel --no-isolation --outdir $(LOCAL_DIST)

inplace:
	PYO3_BUILD_EXTENSION_MODULE=1 cargo build --release
	cp $(BUILT_LIB) $(EXT_MODULE)

test: inplace
	python run_tests.py

sdist:
	rm -rf $(PYPI_DIST)
	maturin sdist --out $(PYPI_DIST)

cross_wheels: sdist
	for t in $(GNU_TARGETS); do \
		$(MATURIN_BUILD) --target $$t --zig --compatibility manylinux2014 \
		|| exit 1; \
	done
	for t in $(MUSL_TARGETS); do \
		$(MATURIN_BUILD) --target $$t --zig --compatibility musllinux_1_2 \
		|| exit 1; \
	done
	for t in $(WIN_TARGETS); do \
		$(MATURIN_BUILD) --target $$t || exit 1; \
	done
	for t in $(MAC_TARGETS); do \
		$(MATURIN_BUILD) --target $$t --zig || exit 1; \
	done

upload: cross_wheels
	twine check $(PYPI_DIST)/*
	twine upload $(PYPI_DIST)/*

# Run once (and after a toolchain update)
rustup_targets:
	rustup target add $(GNU_TARGETS) $(MUSL_TARGETS) $(WIN_TARGETS) \
		x86_64-apple-darwin aarch64-apple-darwin

clean: clean_wheel clean_rust

clean_wheel:
	rm -rf build dist dochint.egg-info

clean_rust:
	cargo clean
	rm -f dochint/*.so dochint/*.pyd

uninstall:
	yes | sudo pip uninstall dochint --break-system-packages
