1 # Copyright 1999-2016 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
6 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
11 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
15 # @ECLASS: cuda.eclass
17 # Justin Lecher <jlec@gentoo.org>
18 # @SUPPORTED_EAPIS: 5 6 7
19 # @BLURB: Common functions for cuda packages
21 # This eclass contains functions to be used with cuda package. Currently it is
22 # setting and/or sanitizing NVCCFLAGS, the compiler flags for nvcc. This is
23 # automatically done and exported in src_prepare() or manually by calling
28 if [[ -z ${_CUDA_ECLASS} ]]; then
30 inherit flag-o-matic toolchain-funcs
31 [[ ${EAPI} == [56] ]] && inherit eapi7-ver
33 # @ECLASS-VARIABLE: NVCCFLAGS
35 # nvcc compiler flags (see nvcc --help), which should be used like
36 # CFLAGS for c compiler
39 # @ECLASS-VARIABLE: CUDA_VERBOSE
41 # Being verbose during compilation to see underlying commands
42 : ${CUDA_VERBOSE:=true}
44 # @FUNCTION: cuda_gccdir
46 # @RETURN: gcc bindir compatible with current cuda, optionally (-f) prefixed with "--compiler-bindir "
48 # Helper for determination of the latest gcc bindir supported by
49 # then current nvidia cuda toolkit.
54 # -> --compiler-bindir "/usr/x86_64-pc-linux-gnu/gcc-bin/4.6.3"
57 debug-print-function ${FUNCNAME} "$@"
59 local dirs gcc_bindir ver vers="" flag
61 # Currently we only support the gnu compiler suite
63 ewarn "Currently we only support the gnu compiler suite"
70 flag="--compiler-bindir "
78 if ! vers="$(cuda-config -s)"; then
79 eerror "Could not execute cuda-config"
80 eerror "Make sure >=dev-util/nvidia-cuda-toolkit-4.2.9-r1 is installed"
81 die "cuda-config not found"
83 if [[ -z ${vers} ]]; then
84 die "Could not determine supported gcc versions from cuda-config"
87 # Try the current gcc version first
89 if [[ -n "${ver}" ]] && [[ ${vers} =~ ${ver} ]]; then
90 dirs=( ${EPREFIX}/usr/*pc-linux-gnu/gcc-bin/${ver}*/ )
91 gcc_bindir="${dirs[${#dirs[@]}-1]}"
94 if [[ -z ${gcc_bindir} ]]; then
95 ver=$(best_version "sys-devel/gcc")
96 ver=$(ver_cut 1-2 "${ver##*sys-devel/gcc-}")
98 if [[ -n "${ver}" ]] && [[ ${vers} =~ ${ver} ]]; then
99 dirs=( ${EPREFIX}/usr/*pc-linux-gnu/gcc-bin/${ver}*/ )
100 gcc_bindir="${dirs[${#dirs[@]}-1]}"
104 for ver in ${vers}; do
105 if has_version "=sys-devel/gcc-${ver}*"; then
106 dirs=( ${EPREFIX}/usr/*pc-linux-gnu/gcc-bin/${ver}*/ )
107 gcc_bindir="${dirs[${#dirs[@]}-1]}"
111 if [[ -n ${gcc_bindir} ]]; then
112 if [[ -n ${flag} ]]; then
113 echo "${flag}\"${gcc_bindir%/}\""
115 echo "${gcc_bindir%/}"
119 eerror "Only gcc version(s) ${vers} are supported,"
120 eerror "of which none is installed"
121 die "Only gcc version(s) ${vers} are supported"
126 # @FUNCTION: cuda_sanitize
128 # Correct NVCCFLAGS by adding the necessary reference to gcc bindir and
129 # passing CXXFLAGS to underlying compiler without disturbing nvcc.
131 debug-print-function ${FUNCNAME} "$@"
133 local rawldflags=$(raw-ldflags)
134 # Be verbose if wanted
135 [[ "${CUDA_VERBOSE}" == true ]] && NVCCFLAGS+=" -v"
137 # Tell nvcc where to find a compatible compiler
138 NVCCFLAGS+=" $(cuda_gccdir -f)"
140 # Tell nvcc which flags should be used for underlying C compiler
141 NVCCFLAGS+=" --compiler-options \"${CXXFLAGS}\" --linker-options \"${rawldflags// /,}\""
143 debug-print "Using ${NVCCFLAGS} for cuda"
147 # @FUNCTION: cuda_add_sandbox
150 # Add nvidia dev nodes to the sandbox predict list.
151 # with -w, add to the sandbox write list.
153 debug-print-function ${FUNCNAME} "$@"
156 for i in /dev/nvidia*; do
157 if [[ $1 == '-w' ]]; then
165 # @FUNCTION: cuda_toolkit_version
167 # echo the installed version of dev-util/nvidia-cuda-toolkit
168 cuda_toolkit_version() {
169 debug-print-function ${FUNCNAME} "$@"
172 v="$(best_version dev-util/nvidia-cuda-toolkit)"
173 v="${v##*cuda-toolkit-}"
177 # @FUNCTION: cuda_cudnn_version
179 # echo the installed version of dev-libs/cudnn
180 cuda_cudnn_version() {
181 debug-print-function ${FUNCNAME} "$@"
184 v="$(best_version dev-libs/cudnn)"
189 # @FUNCTION: cuda_src_prepare
191 # Sanitise and export NVCCFLAGS by default
193 debug-print-function ${FUNCNAME} "$@"
198 EXPORT_FUNCTIONS src_prepare