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