#!/usr/bin/make -f

include /usr/share/dpkg/default.mk
include /usr/share/rustc/architecture.mk
export DEB_HOST_RUST_TYPE

export PATH := /usr/share/cargo/bin:$(PATH)
export CARGO_HOME = $(CURDIR)/debian/cargo_home
export DEB_CARGO_CRATE = $(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM)
export PYBUILD_NAME = pydantic-core
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
ifeq ($(PYBUILD_AUTOPKGTEST),1)
export PYBUILD_BEFORE_TEST := cp -a pydantic-core/tests {build_dir}/tests
export PYBUILD_AFTER_TEST := rm -rf {build_dir}/tests
endif
# pytest_examples is not packaged in Debian.
PYBUILD_TEST_ARGS := --ignore tests/test_docstrings.py
# This imports from pydantic, and we're trying to avoid that circular
# dependency.  This can be dropped when the pydantic and pydantic-core
# source packages are fully merged with pydantic 3.
PYBUILD_TEST_ARGS += --ignore tests/test_tzinfo.py
export PYBUILD_TEST_ARGS

%:
	dh $@ --buildsystem=pybuild

override_dh_auto_clean:
	test ! -f pydantic-core/Cargo.lock.saved || \
		mv pydantic-core/Cargo.lock.saved pydantic-core/Cargo.lock
	dh_auto_clean --sourcedir=pydantic-core

override_dh_auto_configure:
	cargo prepare-debian debian/cargo_registry --link-from-system
	dh_auto_configure --sourcedir=pydantic-core

override_dh_auto_build:
	mv pydantic-core/Cargo.lock pydantic-core/Cargo.lock.saved
	cd pydantic-core && cargo generate-lockfile --offline
	dh_auto_build --sourcedir=pydantic-core

override_dh_auto_test:
	dh_auto_test --sourcedir=pydantic-core

override_dh_auto_install:
	dh_auto_install --sourcedir=pydantic-core
	CARGO_BUILT_USING_TARGET_DIR="pydantic-core/target/$(DEB_HOST_RUST_TYPE)/release" \
		dh-cargo-built-using python3-pydantic-core
