commit:     938059733f846baccae9016ecac564a31dd5677b
Author:     Paul Zander <negril.nx+gentoo <AT> gmail <DOT> com>
AuthorDate: Sat May 17 18:36:15 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Thu May 22 16:07:15 2025 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=93805973

media-libs/opencv: 4.11.0 refactor cuda

Handle lookup failure better.
Document variables.
Observe NVCCFLAGS.
Point user to created log file.

Fix openmp failures due to forced CMAKE_CUDA_ARCHITECTURES="" in opencv cmake
code that resulted from cmakes FindOpenMP now handling CUDA in LANGUAGES.

Closes: https://bugs.gentoo.org/956046
Signed-off-by: Paul Zander <negril.nx+gentoo <AT> gmail.com>
Part-of: https://github.com/gentoo/gentoo/pull/42202
Signed-off-by: Sam James <sam <AT> gentoo.org>

 media-libs/opencv/opencv-4.11.0.ebuild | 135 +++++++++++++++++++++++++++------
 1 file changed, 113 insertions(+), 22 deletions(-)

diff --git a/media-libs/opencv/opencv-4.11.0.ebuild 
b/media-libs/opencv/opencv-4.11.0.ebuild
index 9830992ab4f8..0c94f7ce224e 100644
--- a/media-libs/opencv/opencv-4.11.0.ebuild
+++ b/media-libs/opencv/opencv-4.11.0.ebuild
@@ -394,12 +394,12 @@ PATCHES=(
 )
 
 cuda_get_host_compiler() {
-       if [[ -n "${NVCC_CCBIN}" ]]; then
+       if [[ -v NVCC_CCBIN ]]; then
                echo "${NVCC_CCBIN}"
                return
        fi
 
-       if [[ -n "${CUDAHOSTCXX}" ]]; then
+       if [[ -v CUDAHOSTCXX ]]; then
                echo "${CUDAHOSTCXX}"
                return
        fi
@@ -410,8 +410,27 @@ cuda_get_host_compiler() {
                die "$(tc-get-compiler-type) compiler is not supported"
        fi
 
-       local compiler compiler_type compiler_version
-       local package package_version
+       # compiler with CHOST prefix
+       # x86_64-pc-linux-gnu-g++
+       local compiler
+
+       # gcc or clang
+       local compiler_type
+
+       # major version of the current compiler. 15
+       local compiler_version
+
+       # cat/pkg of the compiler
+       # sys-devel/gcc, llvm-core/clang
+       local package
+
+       # QPN of the package we are checking
+       # sys-devel/gcc, <sys-devel/gcc-15
+       local package_version
+
+       # system compiler e.g. tc-getCXX plus version
+       # used to skip rechecking, as we check NVCC_CCBIN first
+       # x86_64-pc-linux-gnu-g++-15
        local NVCC_CCBIN_default
 
        compiler_type="$(tc-get-compiler-type)"
@@ -436,17 +455,33 @@ cuda_get_host_compiler() {
 
        ebegin "testing ${NVCC_CCBIN_default} (default)"
 
-       while ! nvcc -v -ccbin "${NVCC_CCBIN}" - -x cu <<<"int main(){}" &>> 
"${T}/cuda_get_host_compiler.log" ; do
+       while ! \
+               nvcc "${NVCCFLAGS}" \
+                       -ccbin "${NVCC_CCBIN}" \
+                       - \
+                       -x cu \
+                       <<<"int main(){}" \
+                       &>> "${T}/cuda_get_host_compiler.log" ;
+               do
                eend 1
 
                while true; do
                        # prepare next version
-                       if ! package_version="<$(best_version 
"${package_version}")"; then
-                               die "could not find a supported version of 
${compiler}"
+                       local package_version_next
+                       package_version_next="$(best_version 
"${package_version}")"
+
+                       if [[ -z "${package_version_next}" ]]; then
+                               eerror "Compiler lookup failed. Nothing 
installed matches: ${package_version}."
+                               eerror "You can use NVCC_CCBIN to specify the 
exact compiler to use."
+                               eerror "Check ${T}/cuda_get_host_compiler.log 
for details."
+                               die "Could not find a supported version of 
${compiler}. Did not find \"${package_version}\". NVCC_CCBIN is unset."
                        fi
 
+                       package_version="<${package_version_next}"
+
                        NVCC_CCBIN="${compiler}-$(ver_cut 1 
"${package_version/#<${package}-/}")"
 
+                       # skip the next version equals the already checked 
system default
                        [[ "${NVCC_CCBIN}" != "${NVCC_CCBIN_default}" ]] && 
break
                done
                ebegin "testing ${NVCC_CCBIN}"
@@ -458,8 +493,12 @@ cuda_get_host_compiler() {
 }
 
 cuda_get_host_native_arch() {
-       [[ -n ${CUDAARCHS} ]] && echo "${CUDAARCHS}"
+       if [[ -v CUDAARCHS ]]; then
+               echo "${CUDAARCHS}"
+               return
+       fi
 
+       # TODO nvptx-arch ?
        __nvcc_device_query || die "failed to query the native device"
 }
 
@@ -476,7 +515,8 @@ pkg_pretend() {
        fi
 
        # When building binpkgs you probably want to include all targets
-       if use cuda && [[ ${MERGE_TYPE} == "buildonly" ]] && [[ -n 
"${CUDA_GENERATION}" || -n "${CUDA_ARCH_BIN}" ]]; then
+       # TODO CUDAARCHS
+       if use cuda && [[ ${MERGE_TYPE} == "buildonly" ]] && [[ -v 
CUDA_GENERATION || -v CUDA_ARCH_BIN ]]; then
                local info_message="When building a binary package it's 
recommended to unset CUDA_GENERATION and CUDA_ARCH_BIN"
                einfo "$info_message so all available architectures are build."
        fi
@@ -529,10 +569,17 @@ src_prepare() {
                        modules/gapi/test/render/ftp_render_test.cpp \
                || die
 
+       sed \
+               -e '/find_package(OpenMP/s/)/ COMPONENTS C CXX)/g' \
+               -i \
+                       cmake/OpenCVFindFrameworks.cmake \
+               || die
+
        if use contrib; then
                pushd "${WORKDIR}/${PN}_contrib-${PV}" >/dev/null || die
                eapply "${FILESDIR}/${PN}_contrib-4.8.1-rgbd.patch"
                eapply 
"${FILESDIR}/${PN}_contrib-4.8.1-NVIDIAOpticalFlowSDK-2.0.tar.gz.patch"
+               [[ -n "${PATCHES_CONTRIB_USER[*]}" ]] && eapply 
"${PATCHES_CONTRIB_USER[@]}"
                popd >/dev/null || die
 
                ! use contribcvv && { rm -R 
"${WORKDIR}/${PN}_contrib-${PV}/modules/cvv" || die; }
@@ -931,27 +978,70 @@ multilib_src_configure() {
        tc-export CC CXX
 
        if multilib_native_use cuda; then
+               # Check if we can get the arch from the present gpu
                if ! SANDBOX_WRITE=/dev/nvidiactl test -w /dev/nvidiactl; then
+
+                       # Needs write access to /dev/nvidiactl.
+                       # /dev/nvidiactl usually is 660 root:video .
+
                        # eqawarn "Can't access the GPU at /dev/nvidiactl."
                        # eqawarn "User $(id -nu) is not in the group 
\"video\"."
-                       if [[ -z "${CUDA_GENERATION}" ]] && [[ -z 
"${CUDA_ARCH_BIN}" ]]; then
-                               # build all targets
+
+                       mycmakeargs+=(
+                               -DOpenMP_CUDA_FLAGS=""
+                               -DOpenMP_CUDA_LIB_NAMES=""
+                       )
+
+                       local CUDA_DEVICE_ACCESS="false"
+               fi
+
+               # order of preference CMAKE_CUDA_ARCHITECTURES > 
CUDA_GENERATION > CUDA_ARCH_BIN and/or CUDA_ARCH_PTX
+               # CMAKE_CUDA_ARCHITECTURES is set from the CUDAARCHS env var
+               if [[ -v CUDAARCHS ]]; then
+                       mycmakeargs+=(
+                               -DCMAKE_CUDA_ARCHITECTURES="${CUDAARCHS}"
+                       )
+               elif [[ -v CUDA_GENERATION ]]; then
+                       mycmakeargs+=(
+                               -DCUDA_GENERATION="${CUDA_GENERATION}"
+                       )
+               elif [[ -v CUDA_ARCH_BIN ]]; then
+                       mycmakeargs+=(
+                               -DCUDA_ARCH_BIN="${CUDA_ARCH_BIN}"
+                       )
+                       if [[ -v CUDA_ARCH_PTX ]]; then
                                mycmakeargs+=(
-                                       -DCUDA_GENERATION=""
-                                       
-DCMAKE_CUDA_ARCHITECTURES="${CUDAARCHS:-50}" # breaks with openmp otherwise..
+                                       -DCUDA_ARCH_PTX="${CUDA_ARCH_PTX}"
                                )
                        fi
                else
-                       cuda_add_sandbox -w
-                       addwrite "/proc/self/task"
-                       addpredict "/dev/char/"
+                       if [[ "${CUDA_DEVICE_ACCESS}" == "false" ]]; then
+                               mycmakeargs+=(
+                                       # build all targets
+                                       # can't use "all" as that breaks openmp
+                                       # nvcc fatal   : Unsupported gpu 
architecture 'compute_all'
+                                       # -DCMAKE_CUDA_ARCHITECTURES="all"
 
-                       : "${CUDAARCHS:="$(cuda_get_host_native_arch)"}"
-                       export CUDAARCHS
-                       mycmakeargs+=(
-                               -DCUDA_GENERATION="${CUDAARCHS}"
-                               -DCMAKE_CUDA_ARCHITECTURES="${CUDAARCHS}"
-                       )
+                                       # with openmp -> CUDA_ARCHITECTURES is 
empty for target "cmTC_0088f"
+                                       # -DCUDA_GENERATION="Auto" # requires 
access to GPU
+
+                                       # wrong arch....
+                                       # 
-DCMAKE_CUDA_ARCHITECTURES="${CUDAARCHS:-50}" # breaks with openmp otherwise..
+
+                                       -DCUDA_GENERATION="Auto"
+                               )
+                       else
+                               cuda_add_sandbox -w
+                               addwrite "/proc/self/task"
+                               addpredict "/dev/char/"
+
+                               : "${CUDAARCHS:="$(cuda_get_host_native_arch)"}"
+                               export CUDAARCHS
+                               mycmakeargs+=(
+                                       -DCUDA_GENERATION="${CUDAARCHS}"
+                                       
-DCMAKE_CUDA_ARCHITECTURES="${CUDAARCHS}"
+                               )
+                       fi
                fi
 
                local -x CUDAHOSTCXX CUDAHOSTLD
@@ -968,6 +1058,7 @@ multilib_src_configure() {
                fi
 
                mycmakeargs+=(
+                       -DOPENCV_CMAKE_CUDA_DEBUG="$(usex debug 1 0)"
                        -DENABLE_CUDA_FIRST_CLASS_LANGUAGE="yes"
                )
        fi

Reply via email to