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