1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: fortran-2.eclass
10 # Author Justin Lecher <jlec@gentoo.org>
11 # Test functions provided by Sebastien Fabbro and Kacper Kowalik
12 # @BLURB: Simplify fortran compiler management
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.
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.
24 # FORTRAN_NEEDED="lapack fortran"
28 # FORTRAN_NEED_OPENMP=1
30 if [[ ! ${_FORTRAN_2_CLASS} ]]; then
32 # @ECLASS-VARIABLE: FORTRAN_NEED_OPENMP
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}
38 # @ECLASS-VARIABLE: FORTRAN_STANDARD
40 # Set this, if a special dialect needs to be supported.
41 # Generally not needed as default is sufficient.
43 # Valid settings are any combination of: 77 90 95 2003
44 : ${FORTRAN_STANDARD:=77}
46 # @ECLASS-VARIABLE: FORTRAN_NEEDED
48 # If your package has an optional fortran support, set this variable
49 # to the space separated list of USE triggering the fortran
52 # e.g. FORTRAN_NEEDED=lapack would result in
54 # DEPEND="lapack? ( virtual/fortran )"
56 # If unset, we always depend on virtual/fortran.
57 : ${FORTRAN_NEEDED:=always}
59 inherit eutils toolchain-funcs
61 for _f_use in ${FORTRAN_NEEDED}; do
64 DEPEND+=" virtual/fortran"
65 RDEPEND+=" virtual/fortran"
72 DEPEND+=" ${_f_use}? ( virtual/fortran )"
75 DEPEND+=" ${_f_use}? ( virtual/fortran )"
76 RDEPEND+=" ${_f_use}? ( virtual/fortran )"
82 # @FUNCTION: fortran_int64_abi_fflags
84 # Return the Fortran compiler flag to enable 64 bit integers for
87 fortran_int64_abi_fflags() {
88 debug-print-function ${FUNCNAME} "${@}"
91 if [[ ${_FC} == *gfortran* ]]; then
92 echo "-fdefault-integer-8"
93 elif [[ ${_FC} == ifort ]]; then
94 echo "-integer-size 64"
96 die "Compiler flag for 64bit interger for ${_FC} unknown"
100 # @FUNCTION: _fortran_write_testsuite
103 # writes fortran test code
104 _fortran_write_testsuite() {
105 debug-print-function ${FUNCNAME} "${@}"
107 local filebase=${T}/test-fortran
110 cat <<- EOF > "${filebase}.f"
115 cat <<- EOF > "${filebase}.f90"
120 cat <<- EOF > "${filebase}.f03"
121 procedure(), pointer :: p
126 # @FUNCTION: _fortran_compile_test
127 # @USAGE: <compiler> [dialect]
130 # Takes fortran compiler as first argument and dialect as second.
131 # Checks whether the passed fortran compiler speaks the fortran dialect
132 _fortran_compile_test() {
133 debug-print-function ${FUNCNAME} "${@}"
135 local filebase=${T}/test-fortran
138 local fcode=${filebase}.f${fdia}
142 die "_fortran_compile_test() needs at least one argument"
144 [[ -f ${fcode} ]] || _fortran_write_testsuite
146 ${fcomp} "${fcode}" -o "${fcode}.x" \
147 >> "${T}"/_fortran_compile_test.log 2>&1
154 # @FUNCTION: _fortran-has-openmp
155 # @RETURN: return code of the compiler
158 # See if the fortran supports OpenMP.
159 _fortran-has-openmp() {
160 debug-print-function ${FUNCNAME} "${@}"
163 local filebase=${T}/test-fc-openmp
164 local fcode=${filebase}.f
166 local _fc=$(tc-getFC)
168 cat <<- EOF > "${fcode}"
169 call omp_get_num_threads
173 for flag in -fopenmp -xopenmp -openmp -mp -omp -qsmp=omp; do
174 ${_fc} ${flag} "${fcode}" -o "${fcode}.x" \
175 &>> "${T}"/_fortran_compile_test.log
177 (( ${ret} )) || break
184 # @FUNCTION: _fortran_die_msg
187 # Detailed description how to handle fortran support
189 debug-print-function ${FUNCNAME} "${@}"
192 eerror "Please install currently selected gcc version with USE=fortran."
193 eerror "If you intend to use a different compiler then gfortran, please"
194 eerror "set FC variable accordingly and take care that the necessary"
195 eerror "fortran dialects are supported."
197 die "Currently no working fortran compiler is available"
200 # @FUNCTION: _fortran_test_function
203 # Internal test function for working fortran compiler.
204 # It is called in fortran-2_pkg_setup.
205 _fortran_test_function() {
206 debug-print-function ${FUNCNAME} "${@}"
210 : ${F77:=$(tc-getFC)}
212 : ${FORTRAN_STANDARD:=77}
213 for dialect in ${FORTRAN_STANDARD}; do
215 77) _fortran_compile_test $(tc-getF77) || \
217 90|95) _fortran_compile_test $(tc-getFC) 90 || \
219 2003) _fortran_compile_test $(tc-getFC) 03 || \
221 2008) die "Future" ;;
222 *) die "${dialect} is not a Fortran dialect." ;;
227 einfo "Using following Fortran compiler:"
231 if [[ ${FORTRAN_NEED_OPENMP} == 1 ]]; then
232 if _fortran-has-openmp; then
233 einfo "${FC} has OPENMP support"
235 die "Please install current gcc with USE=openmp or set the FC variable to a compiler that supports OpenMP"
240 # @FUNCTION: _fortran-2_pkg_setup
243 # _The_ fortran-2_pkg_setup() code
244 _fortran-2_pkg_setup() {
245 for _f_use in ${FORTRAN_NEEDED}; do
248 _fortran_test_function && break
251 einfo "Forcing fortran support off"
255 if use ${_f_use}; then
256 _fortran_test_function && break
267 # @FUNCTION: fortran-2_pkg_setup
269 # Setup functionality,
270 # checks for a valid fortran compiler and optionally for its openmp support.
271 fortran-2_pkg_setup() {
272 debug-print-function ${FUNCNAME} "${@}"
276 eqawarn "Support for EAPI < 4 will be removed from the"
277 eqawarn "fortran-2.eclass in until 2013-09-30."
278 eqawarn "Please migrate your package to a higher EAPI"
279 eqawarn "or file a bug at https://bugs.gentoo.org"
280 _fortran-2_pkg_setup ;;
282 if [[ ${MERGE_TYPE} != binary ]]; then
290 0|1|2|3|4|5|6) EXPORT_FUNCTIONS pkg_setup ;;
291 *) die "EAPI=${EAPI} is not supported" ;;