1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: fortran-2.eclass
9 # Author Justin Lecher <jlec@gentoo.org>
10 # Test functions provided by Sebastien Fabbro and Kacper Kowalik
11 # @BLURB: Simplify fortran compiler management
13 # If you need a fortran compiler, then you should be inheriting this eclass.
14 # In case you only need optional support, please export FORTRAN_NEEDED before
15 # inheriting the eclass.
17 # The eclass tests for working fortran compilers
18 # and exports the variables FC and F77.
19 # Optionally, it checks for extended capabilities based on
20 # the variable options selected in the ebuild
21 # The only phase function exported is fortran-2_pkg_setup.
23 # FORTRAN_NEEDED="lapack fortran"
27 # FORTRAN_NEED_OPENMP=1
29 inherit eutils toolchain-funcs
32 4|5|6) EXPORT_FUNCTIONS pkg_setup ;;
33 *) die "EAPI=${EAPI} is not supported" ;;
36 if [[ ! ${_FORTRAN_2_CLASS} ]]; then
38 # @ECLASS-VARIABLE: FORTRAN_NEED_OPENMP
40 # Set to "1" in order to automatically have the eclass abort if the fortran
41 # compiler lacks openmp support.
42 : ${FORTRAN_NEED_OPENMP:=0}
44 # @ECLASS-VARIABLE: FORTRAN_STANDARD
46 # Set this, if a special dialect needs to be supported.
47 # Generally not needed as default is sufficient.
49 # Valid settings are any combination of: 77 90 95 2003
50 : ${FORTRAN_STANDARD:=77}
52 # @ECLASS-VARIABLE: FORTRAN_NEEDED
54 # If your package has an optional fortran support, set this variable
55 # to the space separated list of USE triggering the fortran
58 # e.g. FORTRAN_NEEDED=lapack would result in
60 # DEPEND="lapack? ( virtual/fortran )"
62 # If unset, we always depend on virtual/fortran.
63 : ${FORTRAN_NEEDED:=always}
65 for _f_use in ${FORTRAN_NEEDED}; do
68 DEPEND+=" virtual/fortran"
69 RDEPEND+=" virtual/fortran"
76 DEPEND+=" ${_f_use}? ( virtual/fortran )"
79 DEPEND+=" ${_f_use}? ( virtual/fortran )"
80 RDEPEND+=" ${_f_use}? ( virtual/fortran )"
86 # @FUNCTION: fortran_int64_abi_fflags
88 # Return the Fortran compiler flag to enable 64 bit integers for
91 fortran_int64_abi_fflags() {
92 debug-print-function ${FUNCNAME} "${@}"
95 if [[ ${_FC} == *gfortran* ]]; then
96 echo "-fdefault-integer-8"
97 elif [[ ${_FC} == ifort ]]; then
98 echo "-integer-size 64"
100 die "Compiler flag for 64bit interger for ${_FC} unknown"
104 # @FUNCTION: _fortran_write_testsuite
107 # writes fortran test code
108 _fortran_write_testsuite() {
109 debug-print-function ${FUNCNAME} "${@}"
111 local filebase=${T}/test-fortran
114 cat <<- EOF > "${filebase}.f"
119 cat <<- EOF > "${filebase}.f90"
124 cat <<- EOF > "${filebase}.f03"
125 procedure(), pointer :: p
130 # @FUNCTION: _fortran_compile_test
131 # @USAGE: <compiler> [dialect]
134 # Takes fortran compiler as first argument and dialect as second.
135 # Checks whether the passed fortran compiler speaks the fortran dialect
136 _fortran_compile_test() {
137 debug-print-function ${FUNCNAME} "${@}"
139 local filebase=${T}/test-fortran
142 local fcode=${filebase}.f${fdia}
146 die "_fortran_compile_test() needs at least one argument"
148 [[ -f ${fcode} ]] || _fortran_write_testsuite
150 ${fcomp} "${fcode}" -o "${fcode}.x" \
151 >> "${T}"/_fortran_compile_test.log 2>&1
158 # @FUNCTION: _fortran-has-openmp
159 # @RETURN: return code of the compiler
162 # See if the fortran supports OpenMP.
163 _fortran-has-openmp() {
164 debug-print-function ${FUNCNAME} "${@}"
167 local filebase=${T}/test-fc-openmp
168 local fcode=${filebase}.f
170 local _fc=$(tc-getFC)
172 cat <<- EOF > "${fcode}"
173 call omp_get_num_threads
177 for flag in -fopenmp -xopenmp -openmp -mp -omp -qsmp=omp; do
178 ${_fc} ${flag} "${fcode}" -o "${fcode}.x" \
179 &>> "${T}"/_fortran_compile_test.log
181 (( ${ret} )) || break
188 # @FUNCTION: _fortran_die_msg
191 # Detailed description how to handle fortran support
193 debug-print-function ${FUNCNAME} "${@}"
196 eerror "Please install currently selected gcc version with USE=fortran."
197 eerror "If you intend to use a different compiler then gfortran, please"
198 eerror "set FC variable accordingly and take care that the necessary"
199 eerror "fortran dialects are supported."
201 die "Currently no working fortran compiler is available (see ${T}/_fortran_compile_test.log for information)"
204 # @FUNCTION: _fortran_test_function
207 # Internal test function for working fortran compiler.
208 # It is called in fortran-2_pkg_setup.
209 _fortran_test_function() {
210 debug-print-function ${FUNCNAME} "${@}"
214 : ${F77:=$(tc-getFC)}
216 : ${FORTRAN_STANDARD:=77}
217 for dialect in ${FORTRAN_STANDARD}; do
219 77) _fortran_compile_test $(tc-getF77) || \
221 90|95) _fortran_compile_test $(tc-getFC) 90 || \
223 2003) _fortran_compile_test $(tc-getFC) 03 || \
225 2008) die "Future" ;;
226 *) die "${dialect} is not a Fortran dialect." ;;
231 einfo "Using following Fortran compiler:"
235 if [[ ${FORTRAN_NEED_OPENMP} == 1 ]]; then
236 if _fortran-has-openmp; then
237 einfo "${FC} has OPENMP support"
239 die "Please install current gcc with USE=openmp or set the FC variable to a compiler that supports OpenMP"
244 # @FUNCTION: _fortran-2_pkg_setup
247 # _The_ fortran-2_pkg_setup() code
248 _fortran-2_pkg_setup() {
249 for _f_use in ${FORTRAN_NEEDED}; do
252 _fortran_test_function && break
255 einfo "Forcing fortran support off"
259 if use ${_f_use}; then
260 _fortran_test_function && break
271 # @FUNCTION: fortran-2_pkg_setup
273 # Setup functionality,
274 # checks for a valid fortran compiler and optionally for its openmp support.
275 fortran-2_pkg_setup() {
276 debug-print-function ${FUNCNAME} "${@}"
278 if [[ ${MERGE_TYPE} != binary ]]; then