x11-drivers/xf86-input-wacom: arm stable wrt bug #704592
[gentoo.git] / eclass / fortran-2.eclass
1 # Copyright 1999-2018 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: fortran-2.eclass
5 # @MAINTAINER:
6 # jlec@gentoo.org
7 # sci@gentoo.org
8 # @AUTHOR:
9 # Author Justin Lecher <jlec@gentoo.org>
10 # Test functions provided by Sebastien Fabbro and Kacper Kowalik
11 # @SUPPORTED_EAPIS: 4 5 6 7
12 # @BLURB: Simplify fortran compiler management
13 # @DESCRIPTION:
14 # If you need a fortran compiler, then you should be inheriting this eclass.
15 # In case you only need optional support, please export FORTRAN_NEEDED before
16 # inheriting the eclass.
17 #
18 # The eclass tests for working fortran compilers
19 # and exports the variables FC and F77.
20 # Optionally, it checks for extended capabilities based on
21 # the variable options selected in the ebuild
22 # The only phase function exported is fortran-2_pkg_setup.
23 # @EXAMPLE:
24 # FORTRAN_NEEDED="lapack fortran"
25 #
26 # inherit fortran-2
27 #
28 # FORTRAN_NEED_OPENMP=1
29
30 inherit toolchain-funcs
31 case ${EAPI:-0} in
32         # not used in the eclass, but left for backward compatibility with legacy users
33         4|5|6) inherit eutils ;;
34         7) ;;
35         *) die "EAPI=${EAPI} is not supported" ;;
36 esac
37
38 EXPORT_FUNCTIONS pkg_setup
39
40 if [[ ! ${_FORTRAN_2_CLASS} ]]; then
41
42 # @ECLASS-VARIABLE: FORTRAN_NEED_OPENMP
43 # @DESCRIPTION:
44 # Set to "1" in order to automatically have the eclass abort if the fortran
45 # compiler lacks openmp support.
46 : ${FORTRAN_NEED_OPENMP:=0}
47
48 # @ECLASS-VARIABLE: FORTRAN_STANDARD
49 # @DESCRIPTION:
50 # Set this, if a special dialect needs to be supported.
51 # Generally not needed as default is sufficient.
52 #
53 # Valid settings are any combination of: 77 90 95 2003
54 : ${FORTRAN_STANDARD:=77}
55
56 # @ECLASS-VARIABLE: FORTRAN_NEEDED
57 # @DESCRIPTION:
58 # If your package has an optional fortran support, set this variable
59 # to the space separated list of USE triggering the fortran
60 # dependency.
61 #
62 # e.g. FORTRAN_NEEDED=lapack would result in
63 #
64 # DEPEND="lapack? ( virtual/fortran )"
65 #
66 # If unset, we always depend on virtual/fortran.
67 : ${FORTRAN_NEEDED:=always}
68
69 for _f_use in ${FORTRAN_NEEDED}; do
70         case ${_f_use} in
71                 always)
72                         DEPEND+=" virtual/fortran"
73                         RDEPEND+=" virtual/fortran"
74                         break
75                         ;;
76                 no)
77                         break
78                         ;;
79                 test)
80                         DEPEND+=" ${_f_use}? ( virtual/fortran )"
81                         ;;
82                 *)
83                         DEPEND+=" ${_f_use}? ( virtual/fortran )"
84                         RDEPEND+=" ${_f_use}? ( virtual/fortran )"
85                         ;;
86         esac
87 done
88 unset _f_use
89
90 # @FUNCTION: fortran_int64_abi_fflags
91 # @DESCRIPTION:
92 # Return the Fortran compiler flag to enable 64 bit integers for
93 # array indices
94 # @CODE
95 fortran_int64_abi_fflags() {
96         debug-print-function ${FUNCNAME} "${@}"
97
98         local _FC=$(tc-getFC)
99         if [[ ${_FC} == *gfortran* ]]; then
100                 echo "-fdefault-integer-8"
101         elif [[ ${_FC} == ifort ]]; then
102                 echo "-integer-size 64"
103         else
104                 die "Compiler flag for 64bit interger for ${_FC} unknown"
105         fi
106 }
107
108 # @FUNCTION: _fortran_write_testsuite
109 # @INTERNAL
110 # @DESCRIPTION:
111 # writes fortran test code
112 _fortran_write_testsuite() {
113         debug-print-function ${FUNCNAME} "${@}"
114
115         local filebase=${T}/test-fortran
116
117         # f77 code
118         cat <<- EOF > "${filebase}.f" || die
119                end
120         EOF
121
122         # f90/95 code
123         cat <<- EOF > "${filebase}.f90" || die
124         end
125         EOF
126
127         # f2003 code
128         cat <<- EOF > "${filebase}.f03" || die
129                procedure(), pointer :: p
130                end
131         EOF
132 }
133
134 # @FUNCTION: _fortran_compile_test
135 # @USAGE: <compiler> [dialect]
136 # @INTERNAL
137 # @DESCRIPTION:
138 # Takes fortran compiler as first argument and dialect as second.
139 # Checks whether the passed fortran compiler speaks the fortran dialect
140 _fortran_compile_test() {
141         debug-print-function ${FUNCNAME} "${@}"
142
143         local filebase=${T}/test-fortran
144         local fcomp=${1}
145         local fdia=${2}
146         local fcode=${filebase}.f${fdia}
147         local ret
148
149         [[ $# -lt 1 ]] && \
150                 die "_fortran_compile_test() needs at least one argument"
151
152         [[ -f ${fcode} ]] || _fortran_write_testsuite
153
154         ${fcomp} "${fcode}" -o "${fcode}.x" \
155                 >> "${T}"/_fortran_compile_test.log 2>&1
156         ret=$?
157
158         rm -f "${fcode}.x"
159         return ${ret}
160 }
161
162 # @FUNCTION: _fortran-has-openmp
163 # @RETURN: return code of the compiler
164 # @INTERNAL
165 # @DESCRIPTION:
166 # See if the fortran supports OpenMP.
167 _fortran-has-openmp() {
168         debug-print-function ${FUNCNAME} "${@}"
169
170         local flag
171         local filebase=${T}/test-fc-openmp
172         local fcode=${filebase}.f
173         local ret
174         local _fc=$(tc-getFC)
175
176         cat <<- EOF > "${fcode}" || die
177                call omp_get_num_threads
178                end
179         EOF
180
181         for flag in -fopenmp -xopenmp -openmp -mp -omp -qsmp=omp; do
182                 ${_fc} ${flag} "${fcode}" -o "${fcode}.x" \
183                         &>> "${T}"/_fortran_compile_test.log
184                 ret=$?
185                 [[ ${ret} == 0 ]] && break
186         done
187
188         rm -f "${fcode}.x"
189         return ${ret}
190 }
191
192 # @FUNCTION: _fortran_die_msg
193 # @INTERNAL
194 # @DESCRIPTION:
195 # Detailed description how to handle fortran support
196 _fortran_die_msg() {
197         debug-print-function ${FUNCNAME} "${@}"
198
199         eerror
200         eerror "Please install currently selected gcc version with USE=fortran."
201         eerror "If you intend to use a different compiler then gfortran, please"
202         eerror "set FC variable accordingly and take care that the necessary"
203         eerror "fortran dialects are supported."
204         eerror
205         die "Currently no working fortran compiler is available (see ${T}/_fortran_compile_test.log for information)"
206 }
207
208 # @FUNCTION: _fortran_test_function
209 # @INTERNAL
210 # @DESCRIPTION:
211 # Internal test function for working fortran compiler.
212 # It is called in fortran-2_pkg_setup.
213 _fortran_test_function() {
214         debug-print-function ${FUNCNAME} "${@}"
215
216         local dialect
217
218         : ${F77:=$(tc-getFC)}
219
220         : ${FORTRAN_STANDARD:=77}
221         for dialect in ${FORTRAN_STANDARD}; do
222                 case ${dialect} in
223                         77) _fortran_compile_test $(tc-getF77) || \
224                                 _fortran_die_msg ;;
225                         90|95) _fortran_compile_test $(tc-getFC) 90 || \
226                                 _fortran_die_msg ;;
227                         2003) _fortran_compile_test $(tc-getFC) 03 || \
228                                 _fortran_die_msg ;;
229                         2008) die "Future" ;;
230                         *) die "${dialect} is not a Fortran dialect." ;;
231                 esac
232         done
233
234         tc-export F77 FC
235         einfo "Using following Fortran compiler:"
236         einfo "  F77: ${F77}"
237         einfo "  FC:  ${FC}"
238
239         if [[ ${FORTRAN_NEED_OPENMP} == 1 ]]; then
240                 if _fortran-has-openmp; then
241                         einfo "${FC} has OPENMP support"
242                 else
243                         die "Please install current gcc with USE=openmp or set the FC variable to a compiler that supports OpenMP"
244                 fi
245         fi
246 }
247
248 # @FUNCTION: _fortran-2_pkg_setup
249 # @INTERNAL
250 # @DESCRIPTION:
251 # _The_ fortran-2_pkg_setup() code
252 _fortran-2_pkg_setup() {
253         for _f_use in ${FORTRAN_NEEDED}; do
254         case ${_f_use} in
255                 always)
256                         _fortran_test_function && break 2
257                         ;;
258                 no)
259                         einfo "Forcing fortran support off"
260                         break
261                         ;;
262                 *)
263                         if use ${_f_use}; then
264                                 _fortran_test_function && break 2
265                         else
266                                 unset FC
267                                 unset F77
268                         fi
269                         ;;
270                 esac
271         done
272 }
273
274
275 # @FUNCTION: fortran-2_pkg_setup
276 # @DESCRIPTION:
277 # Setup functionality,
278 # checks for a valid fortran compiler and optionally for its openmp support.
279 fortran-2_pkg_setup() {
280         debug-print-function ${FUNCNAME} "${@}"
281
282         if [[ ${MERGE_TYPE} != binary ]]; then
283                 _fortran-2_pkg_setup
284         fi
285 }
286
287 _FORTRAN_2_ECLASS=1
288 fi