# Copyright 1999-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=9

inherit go-module

DESCRIPTION="GPU-accelerated micromagnetic simulator"
HOMEPAGE="https://mumax.github.io/"
SRC_URI="https://github.com/${PN}/3/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz"

S="${WORKDIR}/3-${PV}"
LICENSE="GPL-3"
SLOT="0"
KEYWORDS="~amd64"

DEPEND=">=dev-util/nvidia-cuda-toolkit-7.5.18-r2"
RDEPEND="${DEPEND}"

PATCHES=( "${FILESDIR}/${PN}-opt-cuda-stubs.patch" )

src_prepare() {
	default

	# CUDA 13 maps cuCtxCreate to cuCtxCreate_v4, which takes an extra
	# CUctxCreateParams argument. Test that mapping, not the type: CUDA 12.5
	# to 12.9 already declare CUctxCreateParams but map cuCtxCreate to the
	# 3-argument cuCtxCreate_v2. verified 2026-12-02
	if grep -Eq '^#define cuCtxCreate[[:space:]]+cuCtxCreate_v4$' \
			"${ESYSROOT}"/opt/cuda/include/cuda.h 2>/dev/null; then
		eapply "${FILESDIR}/mumax-cuda13-cuCtxCreate.patch"
	fi
}

src_compile() {
	ego build -o mumax3 ./cmd/mumax3
}

src_install() {
	dobin mumax3
	einstalldocs
}