pyo3-dlpack 0.1.0

Zero-copy DLPack tensor interop for PyO3
Documentation
# Makefile for pyo3-dlpack
#
# Usage:
#   make test          # Run all tests (unit + integration)
#   make install       # Install test dependencies
#   make test-unit     # Run Rust unit tests only
#   make test-cpu      # Run CPU integration tests
#   make test-gpu      # Run GPU integration tests
#   make build         # Build the test module
#   make clean         # Clean all artifacts

.PHONY: all build test test-unit test-cpu test-gpu test-integration bench bench-rust bench-python install clean help

# Default target
all: test

# Detect Python command (prefer venv if available)
PYTHON := $(shell if [ -f .venv/bin/python ]; then echo .venv/bin/python; else echo python3; fi)

# Install test dependencies
install:
	@echo "Installing test dependencies..."
	@if command -v uv >/dev/null 2>&1; then \
		uv sync --extra test; \
	else \
		echo "uv not found, using pip..."; \
		$(PYTHON) -m pip install -e ".[test]"; \
	fi

# Build the Python test module from tests/test_module
build: install
	@echo "Building test module..."
	$(PYTHON) -m maturin develop

# Run all tests
test: test-unit test-integration

# Run all benchmarks
bench: bench-rust bench-python

# Python library directory (for linking during tests)
PYTHON_LIBDIR := $(shell $(PYTHON) -c "import sysconfig; print(sysconfig.get_config_var('LIBDIR') or '')")

# Run Rust unit tests (lib only, Rust integration tests require numpy via pytest)
# Set library paths for all platforms: macOS (DYLD), Linux (LD), Windows (PATH)
test-unit:
	@echo "Running Rust unit tests..."
	DYLD_LIBRARY_PATH="$(PYTHON_LIBDIR)" \
	LD_LIBRARY_PATH="$(PYTHON_LIBDIR)" \
	PATH="$(PYTHON_LIBDIR):$(PATH)" \
	cargo test --lib

# Run all integration tests
test-integration: build
	@echo "Running integration tests..."
	$(PYTHON) -m pytest tests/test_dlpack_integration.py -v

# Run CPU integration tests only
test-cpu: build
	@echo "Running CPU integration tests..."
	$(PYTHON) -m pytest tests/test_dlpack_integration.py -v -k "Cpu or not Gpu"

# Run GPU integration tests only
test-gpu: build
	@echo "Running GPU integration tests..."
	$(PYTHON) -m pytest tests/test_dlpack_integration.py -v -k "Gpu"

# Run memory safety tests
test-memory: build
	@echo "Running memory safety tests..."
	$(PYTHON) -m pytest tests/test_dlpack_integration.py -v -k "MemorySafety"

# Run stress tests
test-stress: build
	@echo "Running stress tests..."
	$(PYTHON) -m pytest tests/test_dlpack_integration.py -v -k "Stress"

# Run Rust benchmarks (Criterion)
bench-rust:
	@echo "Running Rust benchmarks..."
	DYLD_LIBRARY_PATH="$(PYTHON_LIBDIR)" \
	LD_LIBRARY_PATH="$(PYTHON_LIBDIR)" \
	PATH="$(PYTHON_LIBDIR):$(PATH)" \
	cargo bench --bench dlpack

# Run Python benchmarks
bench-python: build
	@echo "Running Python benchmarks..."
	$(PYTHON) benchmarks/bench_dlpack.py --size 1000000 --iters 200

# Clean build artifacts
clean:
	@echo "Cleaning..."
	rm -rf target/
	rm -rf tests/test_module/target/
	rm -rf *.egg-info/
	rm -rf .pytest_cache/
	rm -rf __pycache__/
	rm -rf tests/__pycache__/
	find . -name "*.pyc" -delete
	find . -name "*.pyo" -delete
	find . -name "*.so" -delete

# Check code
check:
	cargo check
	cargo clippy

# Format code
fmt:
	cargo fmt

# Show help
help:
	@echo "pyo3-dlpack Test Commands"
	@echo ""
	@echo "Usage: make [target]"
	@echo ""
	@echo "Targets:"
	@echo "  test            Run all tests (unit + integration)"
	@echo "  test-unit       Run Rust unit tests only"
	@echo "  test-integration Run Python integration tests"
	@echo "  test-cpu        Run CPU integration tests only"
	@echo "  test-gpu        Run GPU integration tests only"
	@echo "  test-memory     Run memory safety tests"
	@echo "  test-stress     Run stress tests"
	@echo "  bench           Run all benchmarks"
	@echo "  bench-rust      Run Rust benchmarks (Criterion)"
	@echo "  bench-python    Run Python benchmarks"
	@echo "  install         Install test dependencies (uv sync --extra test)"
	@echo "  build           Build the Python test module"
	@echo "  clean           Clean all build artifacts"
	@echo "  check           Run cargo check and clippy"
	@echo "  fmt             Format Rust code"
	@echo "  help            Show this help"