dev-python/josepy: 1.1.0 cleanup
[gentoo.git] / eclass / cuda.eclass
1 # Copyright 1999-2016 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 case "${EAPI:-0}" in
5         0|1|2|3|4)
6                 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
7                 ;;
8         5|6|7)
9                 ;;
10         *)
11                 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
12                 ;;
13 esac
14
15 # @ECLASS: cuda.eclass
16 # @MAINTAINER:
17 # Justin Lecher <jlec@gentoo.org>
18 # @SUPPORTED_EAPIS: 5 6 7
19 # @BLURB: Common functions for cuda packages
20 # @DESCRIPTION:
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
24 # cuda_sanatize.
25 # @EXAMPLE:
26 # inherit cuda
27
28 if [[ -z ${_CUDA_ECLASS} ]]; then
29
30 inherit flag-o-matic toolchain-funcs
31 [[ ${EAPI} == [56] ]] && inherit eapi7-ver
32
33 # @ECLASS-VARIABLE: NVCCFLAGS
34 # @DESCRIPTION:
35 # nvcc compiler flags (see nvcc --help), which should be used like
36 # CFLAGS for c compiler
37 : ${NVCCFLAGS:=-O2}
38
39 # @ECLASS-VARIABLE: CUDA_VERBOSE
40 # @DESCRIPTION:
41 # Being verbose during compilation to see underlying commands
42 : ${CUDA_VERBOSE:=true}
43
44 # @FUNCTION: cuda_gccdir
45 # @USAGE: [-f]
46 # @RETURN: gcc bindir compatible with current cuda, optionally (-f) prefixed with "--compiler-bindir "
47 # @DESCRIPTION:
48 # Helper for determination of the latest gcc bindir supported by
49 # then current nvidia cuda toolkit.
50 #
51 # Example:
52 # @CODE
53 # cuda_gccdir -f
54 # -> --compiler-bindir "/usr/x86_64-pc-linux-gnu/gcc-bin/4.6.3"
55 # @CODE
56 cuda_gccdir() {
57         debug-print-function ${FUNCNAME} "$@"
58
59         local dirs gcc_bindir ver vers="" flag
60
61         # Currently we only support the gnu compiler suite
62         if ! tc-is-gcc ; then
63                 ewarn "Currently we only support the gnu compiler suite"
64                 return 2
65         fi
66
67         while [[ "$1" ]]; do
68                 case $1 in
69                         -f)
70                                 flag="--compiler-bindir "
71                                 ;;
72                         *)
73                                 ;;
74                 esac
75                 shift
76         done
77
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"
82         fi
83         if [[ -z ${vers} ]]; then
84                 die "Could not determine supported gcc versions from cuda-config"
85         fi
86
87         # Try the current gcc version first
88         ver=$(gcc-version)
89         if [[ -n "${ver}" ]] && [[ ${vers} =~ ${ver} ]]; then
90                 dirs=( ${EPREFIX}/usr/*pc-linux-gnu/gcc-bin/${ver}*/ )
91                 gcc_bindir="${dirs[${#dirs[@]}-1]}"
92         fi
93
94         if [[ -z ${gcc_bindir} ]]; then
95                 ver=$(best_version "sys-devel/gcc")
96                 ver=$(ver_cut 1-2 "${ver##*sys-devel/gcc-}")
97
98                 if [[ -n "${ver}" ]] && [[ ${vers} =~ ${ver} ]]; then
99                         dirs=( ${EPREFIX}/usr/*pc-linux-gnu/gcc-bin/${ver}*/ )
100                         gcc_bindir="${dirs[${#dirs[@]}-1]}"
101                 fi
102         fi
103
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]}"
108                 fi
109         done
110
111         if [[ -n ${gcc_bindir} ]]; then
112                 if [[ -n ${flag} ]]; then
113                         echo "${flag}\"${gcc_bindir%/}\""
114                 else
115                         echo "${gcc_bindir%/}"
116                 fi
117                 return 0
118         else
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"
122                 return 1
123         fi
124 }
125
126 # @FUNCTION: cuda_sanitize
127 # @DESCRIPTION:
128 # Correct NVCCFLAGS by adding the necessary reference to gcc bindir and
129 # passing CXXFLAGS to underlying compiler without disturbing nvcc.
130 cuda_sanitize() {
131         debug-print-function ${FUNCNAME} "$@"
132
133         local rawldflags=$(raw-ldflags)
134         # Be verbose if wanted
135         [[ "${CUDA_VERBOSE}" == true ]] && NVCCFLAGS+=" -v"
136
137         # Tell nvcc where to find a compatible compiler
138         NVCCFLAGS+=" $(cuda_gccdir -f)"
139
140         # Tell nvcc which flags should be used for underlying C compiler
141         NVCCFLAGS+=" --compiler-options \"${CXXFLAGS}\" --linker-options \"${rawldflags// /,}\""
142
143         debug-print "Using ${NVCCFLAGS} for cuda"
144         export NVCCFLAGS
145 }
146
147 # @FUNCTION: cuda_add_sandbox
148 # @USAGE: [-w]
149 # @DESCRIPTION:
150 # Add nvidia dev nodes to the sandbox predict list.
151 # with -w, add to the sandbox write list.
152 cuda_add_sandbox() {
153         debug-print-function ${FUNCNAME} "$@"
154
155         local i
156         for i in /dev/nvidia*; do
157                 if [[ $1 == '-w' ]]; then
158                         addwrite $i
159                 else
160                         addpredict $i
161                 fi
162         done
163 }
164
165 # @FUNCTION: cuda_toolkit_version
166 # @DESCRIPTION:
167 # echo the installed version of dev-util/nvidia-cuda-toolkit
168 cuda_toolkit_version() {
169         debug-print-function ${FUNCNAME} "$@"
170
171         local v
172         v="$(best_version dev-util/nvidia-cuda-toolkit)"
173         v="${v##*cuda-toolkit-}"
174         ver_cut 1-2 "${v}"
175 }
176
177 # @FUNCTION: cuda_cudnn_version
178 # @DESCRIPTION:
179 # echo the installed version of dev-libs/cudnn
180 cuda_cudnn_version() {
181         debug-print-function ${FUNCNAME} "$@"
182
183         local v
184         v="$(best_version dev-libs/cudnn)"
185         v="${v##*cudnn-}"
186         ver_cut 1-2 "${v}"
187 }
188
189 # @FUNCTION: cuda_src_prepare
190 # @DESCRIPTION:
191 # Sanitise and export NVCCFLAGS by default
192 cuda_src_prepare() {
193         debug-print-function ${FUNCNAME} "$@"
194
195         cuda_sanitize
196 }
197
198 EXPORT_FUNCTIONS src_prepare
199
200 _CUDA_ECLASS=1
201 fi