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

EAPI=8

CMAKE_BUILD_TYPE="Release"
MY_PN="${PN/intel-/}"
MY_P="${MY_PN}-${PV}"

inherit cmake flag-o-matic git-r3

DESCRIPTION="Intel Graphics Compute Runtime for oneAPI Level Zero and OpenCL Driver"
HOMEPAGE="https://github.com/intel/compute-runtime"

#SRC_URI="https://github.com/intel/${MY_PN}/archive/refs/tags/${PV}.tar.gz -> ${P}.tar.gz"
EGIT_REPO_URI="https://github.com/intel/${MY_PN}.git"
EGIT_OVERRIDE_BRANCH_INTEL_COMPUTE_RUNTIME="releases/25.40"
EGIT_SUBMODULES=()

S="${WORKDIR}/${PN}-${PV}"

LICENSE="MIT"
SLOT="0/1.6"
KEYWORDS="~amd64"
IUSE="disable-mitigations +l0 legacy lto +vaapi test"

RDEPEND="
	!dev-libs/intel-compute-runtime:legacy
	>=dev-util/intel-graphics-compiler-2.19.1:0
	!dev-util/intel-graphics-compiler:legacy
	>=media-libs/gmmlib-22.7.1:=

	dev-libs/intel-metrics-discovery:=
	>=dev-libs/intel-metrics-library-1.0.200:=
	dev-libs/libnl:3
	dev-libs/libxml2:2
	>=dev-util/intel-graphics-system-controller-0.9.6:=
	media-libs/mesa
	>=virtual/opencl-3
	l0? ( >=dev-libs/level-zero-1.24.2:= )
	vaapi? (
		x11-libs/libdrm[video_cards_intel]
		media-libs/libva
	)
	test? ( ${CATEGORY}/${PN}:${SLOT} )
"

BDEPEND="virtual/pkgconfig"

DOCS=( "README.md" "FAQ.md" )

PATCHES=(
	"${FILESDIR}/${PV}-${PN}-test.patch"
)

src_prepare() {
	# Remove '-Werror' from default
	sed -e '/Werror/d' -i CMakeLists.txt || die

	# uchar
	[[ ${CC} == *clang* ]] && \
	ewarn "Clang builds are known to fail, export CC=gcc CXX=g++ if clinfo has SIGABORT" && \
	cp "${FILESDIR}/uchar_string_traits.hpp" "${S}/shared/source/os_interface/" && \
	eapply "${FILESDIR}/00-clang-compat.patch"

	cmake_src_prepare
}

src_configure() {
	# Filtered for two reasons:
	# 1) https://github.com/intel/compute-runtime/issues/528
	# 2) bug #930199
	#filter-lto

	local mycmakeargs=(
		-DCCACHE_ALLOWED="ON"
		-DCMAKE_INSTALL_PREFIX="${EPREFIX}/usr"
		-DCMAKE_INSTALL_LIBDIR="$(get_libdir)"
		-DBUILD_WITH_L0="$(usex l0)"
		-DDISABLE_LIBVA="$(usex !vaapi)"
		-DNEO_ALLOW_LEGACY_PLATFORMS_SUPPORT="$(usex legacy)"
		-DNEO_DISABLE_LTO="$(usex !lto)"
		-DNEO_DISABLE_MITIGATIONS="$(usex disable-mitigations)"
		-DNEO_ENABLE_REFERENCE_TRACKING="OFF"
		-DNEO__METRICS_LIBRARY_INCLUDE_DIR="${ESYSROOT}/usr/include"
		-DKHRONOS_GL_HEADERS_DIR="${ESYSROOT}/usr/include"
		-DOCL_ICD_VENDORDIR="${EPREFIX}/etc/OpenCL/vendors"
		-DSUPPORT_DG1="ON"
		-Wno-dev

		# See https://github.com/intel/intel-graphics-compiler/issues/204
		# -DNEO_DISABLE_BUILTINS_COMPILATION="ON"

		# If enabled, tests are automatically run during
		# the compile phase and we cannot run them because
		# they require permissions to access the hardware.
		-DSKIP_UNIT_TESTS="$(usex !test)"
	)

	cmake_src_configure
}

src_test() {
	ewarn "The test must run AFTER the package is installed."
}
