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