dev-util/qbs: version bump
[gentoo.git] / eclass / cuda.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 inherit flag-o-matic toolchain-funcs versionator
6
7 # @ECLASS: cuda.eclass
8 # @MAINTAINER:
9 # Justin Lecher <jlec@gentoo.org>
10 # @BLURB: Common functions for cuda packages
11 # @DESCRIPTION:
12 # This eclass contains functions to be used with cuda package. Currently it is
13 # setting and/or sanitizing NVCCFLAGS, the compiler flags for nvcc. This is
14 # automatically done and exported in src_prepare() or manually by calling
15 # cuda_sanatize.
16 # @EXAMPLE:
17 # inherit cuda
18
19 # @ECLASS-VARIABLE: NVCCFLAGS
20 # @DESCRIPTION:
21 # nvcc compiler flags (see nvcc --help), which should be used like
22 # CFLAGS for c compiler
23 : ${NVCCFLAGS:=-O2}
24
25 # @ECLASS-VARIABLE: CUDA_VERBOSE
26 # @DESCRIPTION:
27 # Being verbose during compilation to see underlying commands
28 : ${CUDA_VERBOSE:=true}
29
30 # @FUNCTION: cuda_gccdir
31 # @USAGE: [-f]
32 # @RETURN: gcc bindir compatible with current cuda, optionally (-f) prefixed with "--compiler-bindir="
33 # @DESCRIPTION:
34 # Helper for determination of the latest gcc bindir supported by
35 # then current nvidia cuda toolkit.
36 #
37 # Example:
38 # @CODE
39 # cuda_gccdir -f
40 # -> --compiler-bindir="/usr/x86_64-pc-linux-gnu/gcc-bin/4.6.3"
41 # @CODE
42 cuda_gccdir() {
43         local gcc_bindir ver args="" flag ret
44
45         # Currently we only support the gnu compiler suite
46         if [[ $(tc-getCXX) != *g++* ]]; then
47                 ewarn "Currently we only support the gnu compiler suite"
48                 return 2
49         fi
50
51         while [ "$1" ]; do
52                 case $1 in
53                         -f)
54                                 flag="--compiler-bindir="
55                                 ;;
56                         *)
57                                 ;;
58                 esac
59                 shift
60         done
61
62         if ! args=$(cuda-config -s); then
63                 eerror "Could not execute cuda-config"
64                 eerror "Make sure >=dev-util/nvidia-cuda-toolkit-4.2.9-r1 is installed"
65                 die "cuda-config not found"
66         else
67                 args=$(version_sort ${args})
68                 if [[ -z ${args} ]]; then
69                         die "Could not determine supported gcc versions from cuda-config"
70                 fi
71         fi
72
73         for ver in ${args}; do
74                 has_version "=sys-devel/gcc-${ver}*" && \
75                  gcc_bindir="$(ls -d ${EPREFIX}/usr/*pc-linux-gnu/gcc-bin/${ver}* | tail -n 1)"
76         done
77
78         if [[ -n ${gcc_bindir} ]]; then
79                 if [[ -n ${flag} ]]; then
80                         ret="${flag}\"${gcc_bindir}\""
81                 else
82                         ret="${gcc_bindir}"
83                 fi
84                 echo ${ret}
85                 return 0
86         else
87                 eerror "Only gcc version(s) ${args} are supported,"
88                 eerror "of which none is installed"
89                 die "Only gcc version(s) ${args} are supported"
90                 return 1
91         fi
92 }
93
94 # @FUNCTION: cuda_sanitize
95 # @DESCRIPTION:
96 # Correct NVCCFLAGS by adding the necessary reference to gcc bindir and
97 # passing CXXFLAGS to underlying compiler without disturbing nvcc.
98 cuda_sanitize() {
99         local rawldflags=$(raw-ldflags)
100         # Be verbose if wanted
101         [[ "${CUDA_VERBOSE}" == true ]] && NVCCFLAGS+=" -v"
102
103         # Tell nvcc where to find a compatible compiler
104         NVCCFLAGS+=" $(cuda_gccdir -f)"
105
106         # Tell nvcc which flags should be used for underlying C compiler
107         NVCCFLAGS+=" --compiler-options=\"${CXXFLAGS}\" --linker-options=\"${rawldflags// /,}\""
108
109         debug-print "Using ${NVCCFLAGS} for cuda"
110         export NVCCFLAGS
111 }
112
113 # @FUNCTION: cuda_pkg_setup
114 # @DESCRIPTION:
115 # Call cuda_src_prepare for EAPIs not supporting src_prepare
116 cuda_pkg_setup() {
117         cuda_src_prepare
118 }
119
120 # @FUNCTION: cuda_src_prepare
121 # @DESCRIPTION:
122 # Sanitise and export NVCCFLAGS by default
123 cuda_src_prepare() {
124         cuda_sanitize
125 }
126
127
128 case "${EAPI:-0}" in
129         0|1)
130                 EXPORT_FUNCTIONS pkg_setup ;;
131         2|3|4|5)
132                 EXPORT_FUNCTIONS src_prepare ;;
133         *) die "EAPI=${EAPI} is not supported" ;;
134 esac