# Makefile frontend for scikit-build-core project
# Generated by buildgen for: cycsound
#
# This Makefile wraps common build commands for convenience.
# The actual build is handled by scikit-build-core via pyproject.toml

.PHONY: all sync build rebuild test test-cli vendor clean distclean wheel wheel-static sdist check publish publish-test help

# Vendored Csound 7 runtime (downloaded on demand by scripts/vendor_csound7.sh):
# a CsoundLib64 framework on macOS, a shared library on Linux.
ifeq ($(shell uname),Darwin)
VENDOR_FRAMEWORK := vendor/CsoundLib64.framework
else
VENDOR_FRAMEWORK := vendor/lib/libcsound64.so
endif

# Default target
all: build

# Download/vendor the Csound 7 runtime if it is not already present.
# This is an order-only prerequisite of the build/test targets, so a missing
# (or deleted) framework is fetched via curl before anything that needs it.
$(VENDOR_FRAMEWORK):
	@scripts/vendor_csound7.sh

vendor: $(VENDOR_FRAMEWORK)

# Sync environment (initial setup, installs dependencies + package)
sync: | $(VENDOR_FRAMEWORK)
	@uv sync

# Build/rebuild the extension after code changes
build: | $(VENDOR_FRAMEWORK)
	@uv sync --reinstall-package cycsound

# Alias for build
rebuild: build

# Run tests
test: | $(VENDOR_FRAMEWORK)
	@uv run python -m pytest tests/ -v

# Run CLI tests only
test-cli: | $(VENDOR_FRAMEWORK)
	@uv run python -m pytest tests/test_cli.py -v

# Build wheel (dynamic linking)
wheel:
	@uv build --wheel

# Build wheel with static Csound libraries (standalone, macOS only)
wheel-static:
	@uv build --wheel -C cmake.define.STATIC=ON

# Build source distribution
sdist:
	@uv build --sdist

# Check distribution files with twine
check:
	@uv run twine check dist/*

release:
	@uv build --wheel -C cmake.define.STATIC=ON --python 3.10
	@uv build --wheel -C cmake.define.STATIC=ON --python 3.11
	@uv build --wheel -C cmake.define.STATIC=ON --python 3.12
	@uv build --wheel -C cmake.define.STATIC=ON --python 3.13
	@uv build --wheel -C cmake.define.STATIC=ON --python 3.14
	@uv run twine check dist/*

# Publish to PyPI
publish:
	@uv run twine upload dist/*

# Publish to TestPyPI
publish-test:
	@uv run twine upload --repository testpypi dist/*

# Clean build artifacts
clean:
	@rm -rf build/
	@rm -rf dist/
	@rm -rf *.egg-info/
	@rm -rf src/*.egg-info/
	@rm -rf .pytest_cache/
	@find . -name "*.so" -delete
	@find . -name "*.pyd" -delete
	@find . -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null || true

# Clean everything including CMake cache
distclean: clean
	@rm -rf CMakeCache.txt CMakeFiles/

# Show help
help:
	@echo "Available targets:"
	@echo "  all          - Build/rebuild the extension (default)"
	@echo "  sync         - Sync environment (initial setup)"
	@echo "  build        - Rebuild extension after code changes"
	@echo "  rebuild      - Alias for build"
	@echo "  test         - Run all tests"
	@echo "  test-cli     - Run CLI tests only"
	@echo "  wheel        - Build wheel distribution (dynamic linking)"
	@echo "  wheel-static - Build wheel with static Csound (standalone, macOS)"
	@echo "  sdist        - Build source distribution"
	@echo "  check        - Check distribution files with twine"
	@echo "  publish      - Publish to PyPI"
	@echo "  publish-test - Publish to TestPyPI"
	@echo "  clean        - Remove build artifacts"
	@echo "  distclean    - Remove all generated files"
	@echo "  help         - Show this help message"
