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

.PHONY: all sync build build-pty rebuild test lint lint-check format format-check \
        typecheck qa clean distclean wheel sdist dist check publish-test \
        publish upgrade coverage coverage-html docs release help \
        gen test-window test-asan test-c

# Default target
all: build

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

# Build/rebuild the extension after code changes. Without build isolation the
# build dir stays valid, so only changed files recompile.
BUILD_DEPS = uv sync --no-install-project
REBUILD = uv sync --no-build-isolation-package cui-py --reinstall-package cui-py
build:
	@$(BUILD_DEPS) && $(REBUILD)

# Build with cui.Pty (CUI_PY_PTY). Needs Zig 0.17 on PATH.
build-pty:
	@$(BUILD_DEPS) && SKBUILD_BUILD_DIR="build/pty-{wheel_tag}" CMAKE_ARGS="-DCUI_PY_PTY=ON" $(REBUILD)

# Alias for build
rebuild: build

# Run tests
test:
	@uv run pytest tests/ -v

# C tests for csrc/term, the terminal component. Needs Zig 0.17 on PATH.
test-c:
	@cmake -S tests/c -B build/ctest -DCMAKE_BUILD_TYPE=RelWithDebInfo >/dev/null && \
		cmake --build build/ctest -j && ctest --test-dir build/ctest --output-on-failure

# Regenerate src/cui/_cui.pxd, _gen.pxi, _core.pyi and docs/dev/coverage.md
# from thirdparty/cui/include. Needs libclang (fetched by uv) and clang.
gen:
	@uv run --no-project --with libclang python -I tools/gen_bindings.py

# Run the tests that open a (hidden) window as well
test-window:
	@CUI_WINDOW_TESTS=1 uv run pytest tests/ -v

# Build with AddressSanitizer, run the tests and the smoke script, then rebuild
# normally. macOS needs the runtime preloaded; the smoke script runs directly
# because macOS drops DYLD_* variables in pytest's subprocess.
ASAN_LIB = $(shell clang -print-file-name=libclang_rt.asan_osx_dynamic.dylib)
test-asan:
	@$(BUILD_DEPS) && SKBUILD_BUILD_DIR="build/asan-{wheel_tag}" CMAKE_ARGS="-DCMAKE_C_FLAGS=-fsanitize=address\ -fno-omit-frame-pointer -DCMAKE_OBJC_FLAGS=-fsanitize=address -DCMAKE_MODULE_LINKER_FLAGS=-fsanitize=address -DCMAKE_BUILD_TYPE=RelWithDebInfo" \
		$(REBUILD)
	@DYLD_INSERT_LIBRARIES=$(ASAN_LIB) ASAN_OPTIONS=detect_stack_use_after_return=1 \
		.venv/bin/python -m pytest -q -p no:cacheprovider --deselect tests/test_smoke.py::test_every_generated_method --deselect tests/test_state.py; \
		s=$$?; DYLD_INSERT_LIBRARIES=$(ASAN_LIB) ASAN_OPTIONS=detect_stack_use_after_return=1 \
		.venv/bin/python tests/smoke_all.py > /dev/null; t=$$?; \
		DYLD_INSERT_LIBRARIES=$(ASAN_LIB) ASAN_OPTIONS=detect_stack_use_after_return=1 \
		.venv/bin/python tests/test_state.py || t=1; \
		$(REBUILD); test $$s -eq 0 -a $$t -eq 0

# Lint with ruff (applies fixes)
lint:
	@uv run ruff check --fix src/ tests/ examples/ tools/

# Lint with ruff (check only, no fixes)
lint-check:
	@uv run ruff check src/ tests/ examples/ tools/

# Format with ruff
format:
	@uv run ruff format src/ tests/ examples/ tools/

# Check formatting without modifying files
format-check:
	@uv run ruff format --check src/ tests/ examples/ tools/

# Type check with mypy
typecheck:
	@uv run mypy src/cui tests/ examples/

# Run a full quality assurance check (non-mutating; mirrors CI)
qa: lint-check format-check typecheck test

# Build wheel
wheel:
	@uv build --wheel

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

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

# Build both wheel and sdist
dist: wheel sdist check

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

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

# Upgrade all dependencies
upgrade:
	@uv lock --upgrade
	@uv sync

# Run tests with coverage
coverage:
	@uv run pytest tests/ -v --cov=src/cui --cov-report=term-missing

# Generate HTML coverage report
coverage-html:
	@uv run pytest tests/ -v --cov=src/cui --cov-report=html
	@echo "Coverage report: htmlcov/index.html"

# Build documentation (sphinx is fetched on demand; add it to the dev group
# to pin a version)
docs:
	@uv run --with sphinx sphinx-build -b html docs/ docs/_build/html

# Create a release (bump version, tag, push)
release:
	@echo "Current version: $$(grep '^version' pyproject.toml | head -1)"
	@read -p "New version: " version; \
	sed "s/^version = .*/version = \"$$version\"/" pyproject.toml > pyproject.toml.tmp && mv pyproject.toml.tmp pyproject.toml; \
	git add pyproject.toml; \
	git commit -m "Bump version to $$version"; \
	git tag -a "$$version" -m "Release $$version"; \
	echo "Tagged $$version. Run 'git push && git push --tags' to publish."

# 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 "  build-pty    - Rebuild with cui.Pty (needs Zig 0.17)"
	@echo "  rebuild      - Alias for build"
	@echo "  test         - Run tests"
	@echo "  test-window  - Run tests, including those that open a window"
	@echo "  test-asan    - Run tests and the smoke script under AddressSanitizer (macOS)"
	@echo "  test-c       - Build and run the C tests of csrc/term (needs Zig 0.17)"
	@echo "  gen          - Regenerate the Cython layer from the CUI headers"
	@echo "  lint         - Lint with ruff (applies fixes)"
	@echo "  lint-check   - Lint with ruff (check only)"
	@echo "  format       - Format with ruff"
	@echo "  format-check - Check formatting without modifying files"
	@echo "  typecheck    - Type check with mypy"
	@echo "  qa           - Run full quality assurance (non-mutating: lint-check, format-check, typecheck, test)"
	@echo "  wheel        - Build wheel distribution"
	@echo "  sdist        - Build source distribution"
	@echo "  dist         - Build both wheel and sdist"
	@echo "  check        - Check distributions with twine"
	@echo "  publish-test - Publish to TestPyPI"
	@echo "  publish      - Publish to PyPI"
	@echo "  upgrade      - Upgrade all dependencies"
	@echo "  coverage     - Run tests with coverage"
	@echo "  coverage-html - Generate HTML coverage report"
	@echo "  docs         - Build documentation with Sphinx"
	@echo "  release      - Bump version, tag, and prepare release"
	@echo "  clean        - Remove build artifacts"
	@echo "  distclean    - Remove all generated files"
	@echo "  help         - Show this help message"
