#!/usr/bin/make -f
NPROC := $(shell nproc)
BUILDDIR := build_deb
PKGDIR := $(CURDIR)/debian/libonnxruntime
# RVV march for MLAS kernels (no x* non-standard exts; gcc on K3 supports this)
RVV_MARCH := -march=rv64gcv_zfh_zvfh_zba_zicbop_zihintpause -mabi=lp64d
# extra cmake defines injected per-repo (e.g. RISCV_MARCH_FLAGS for the spacemit fork)
ORT_EXTRA ?=

%:
	dh $@

override_dh_auto_configure:
	# handled by build.py in build step

override_dh_auto_build:
	python3 tools/ci_build/build.py \
	  --build_dir $(BUILDDIR) --config Release --parallel $(NPROC) \
	  --build_shared_lib --skip_tests --skip_submodule_sync \
	  --compile_no_warning_as_error --allow_running_as_root \
	  --cmake_extra_defines \
	    onnxruntime_BUILD_UNIT_TESTS=OFF \
	    CMAKE_C_FLAGS="$(RVV_MARCH)" \
	    CMAKE_CXX_FLAGS="$(RVV_MARCH)" \
	    $(ORT_EXTRA)

override_dh_auto_test:
	# upstream tests skipped for packaging build

override_dh_auto_install:
	install -d $(PKGDIR)/usr/lib $(PKGDIR)/usr/include/onnxruntime
	# shared library + version symlinks
	cp -a $(BUILDDIR)/Release/libonnxruntime.so* $(PKGDIR)/usr/lib/
	# public C/C++ API headers
	find include -name '*.h' -exec cp -a --parents {} $(PKGDIR)/usr/include/onnxruntime/ \;
	# pkg-config / cmake if generated
	-cp -a $(BUILDDIR)/Release/*.pc $(PKGDIR)/usr/lib/ 2>/dev/null || true

override_dh_auto_clean:
	rm -rf $(BUILDDIR)
