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 inherit eutils toolchain-funcs
33 0|1|2|3|4|5|6) EXPORT_FUNCTIONS pkg_setup ;;
34 *) die "EAPI=${EAPI} is not supported" ;;
37 if [[ ! ${_FORTRAN_2_CLASS} ]]; then
39 # @ECLASS-VARIABLE: FORTRAN_NEED_OPENMP
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}
45 # @ECLASS-VARIABLE: FORTRAN_STANDARD
47 # Set this, if a special dialect needs to be supported.
48 # Generally not needed as default is sufficient.
50 # Valid settings are any combination of: 77 90 95 2003
51 : ${FORTRAN_STANDARD:=77}
53 # @ECLASS-VARIABLE: FORTRAN_NEEDED
55 # If your package has an optional fortran support, set this variable
56 # to the space separated list of USE triggering the fortran
59 # e.g. FORTRAN_NEEDED=lapack would result in
61 # DEPEND="lapack? ( virtual/fortran )"
63 # If unset, we always depend on virtual/fortran.
64 : ${FORTRAN_NEEDED:=always}
66 for _f_use in ${FORTRAN_NEEDED}; do
69 DEPEND+=" virtual/fortran"
70 RDEPEND+=" virtual/fortran"
77 DEPEND+=" ${_f_use}? ( virtual/fortran )"
80 DEPEND+=" ${_f_use}? ( virtual/fortran )"
81 RDEPEND+=" ${_f_use}? ( virtual/fortran )"
87 # @FUNCTION: fortran_int64_abi_fflags
89 # Return the Fortran compiler flag to enable 64 bit integers for
92 fortran_int64_abi_fflags() {
93 debug-print-function ${FUNCNAME} "${@}"
96 if [[ ${_FC} == *gfortran* ]]; then
97 echo "-fdefault-integer-8"
98 elif [[ ${_FC} == ifort ]]; then
99 echo "-integer-size 64"
101 die "Compiler flag for 64bit interger for ${_FC} unknown"
105 # @FUNCTION: _fortran_write_testsuite
108 # writes fortran test code
109 _fortran_write_testsuite() {
110 debug-print-function ${FUNCNAME} "${@}"
112 local filebase=${T}/test-fortran
115 cat <<- EOF > "${filebase}.f"
120 cat <<- EOF > "${filebase}.f90"
125 cat <<- EOF > "${filebase}.f03"
126 procedure(), pointer :: p
131 # @FUNCTION: _fortran_compile_test
132 # @USAGE: <compiler> [dialect]
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} "${@}"
140 local filebase=${T}/test-fortran
143 local fcode=${filebase}.f${fdia}
147 die "_fortran_compile_test() needs at least one argument"
149 [[ -f ${fcode} ]] || _fortran_write_testsuite
151 ${fcomp} "${fcode}" -o "${fcode}.x" \
152 >> "${T}"/_fortran_compile_test.log 2>&1
159 # @FUNCTION: _fortran-has-openmp
160 # @RETURN: return code of the compiler
163 # See if the fortran supports OpenMP.
164 _fortran-has-openmp() {
165 debug-print-function ${FUNCNAME} "${@}"
168 local filebase=${T}/test-fc-openmp
169 local fcode=${filebase}.f
171 local _fc=$(tc-getFC)
173 cat <<- EOF > "${fcode}"
174 call omp_get_num_threads
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
182 (( ${ret} )) || break
189 # @FUNCTION: _fortran_die_msg
192 # Detailed description how to handle fortran support
194 debug-print-function ${FUNCNAME} "${@}"
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."
202 die "Currently no working fortran compiler is available (see ${T}/_fortran_compile_test.log for information)"
205 # @FUNCTION: _fortran_test_function
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} "${@}"
215 : ${F77:=$(tc-getFC)}
217 : ${FORTRAN_STANDARD:=77}
218 for dialect in ${FORTRAN_STANDARD}; do
220 77) _fortran_compile_test $(tc-getF77) || \
222 90|95) _fortran_compile_test $(tc-getFC) 90 || \
224 2003) _fortran_compile_test $(tc-getFC) 03 || \
226 2008) die "Future" ;;
227 *) die "${dialect} is not a Fortran dialect." ;;
232 einfo "Using following Fortran compiler:"
236 if [[ ${FORTRAN_NEED_OPENMP} == 1 ]]; then
237 if _fortran-has-openmp; then
238 einfo "${FC} has OPENMP support"
240 die "Please install current gcc with USE=openmp or set the FC variable to a compiler that supports OpenMP"
245 # @FUNCTION: _fortran-2_pkg_setup
248 # _The_ fortran-2_pkg_setup() code
249 _fortran-2_pkg_setup() {
250 for _f_use in ${FORTRAN_NEEDED}; do
253 _fortran_test_function && break
256 einfo "Forcing fortran support off"
260 if use ${_f_use}; then
261 _fortran_test_function && break
272 # @FUNCTION: fortran-2_pkg_setup
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} "${@}"
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 ;;
287 if [[ ${MERGE_TYPE} != binary ]]; then