1 # Copyright 1999-2020 Gentoo Authors
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 # @SUPPORTED_EAPIS: 4 5 6 7
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 toolchain-funcs
32 # not used in the eclass, but left for backward compatibility with legacy users
33 4|5|6) inherit eutils ;;
35 *) die "EAPI=${EAPI} is not supported" ;;
38 EXPORT_FUNCTIONS pkg_setup
40 if [[ ! ${_FORTRAN_2_CLASS} ]]; then
42 # @ECLASS-VARIABLE: FORTRAN_NEED_OPENMP
44 # Set to "1" in order to automatically have the eclass abort if the fortran
45 # compiler lacks openmp support.
46 : ${FORTRAN_NEED_OPENMP:=0}
48 # @ECLASS-VARIABLE: FORTRAN_STANDARD
50 # Set this, if a special dialect needs to be supported.
51 # Generally not needed as default is sufficient.
53 # Valid settings are any combination of: 77 90 95 2003
54 : ${FORTRAN_STANDARD:=77}
56 # @ECLASS-VARIABLE: FORTRAN_NEEDED
58 # If your package has an optional fortran support, set this variable
59 # to the space separated list of USE triggering the fortran
62 # e.g. FORTRAN_NEEDED=lapack would result in
64 # DEPEND="lapack? ( virtual/fortran )"
66 # If unset, we always depend on virtual/fortran.
67 : ${FORTRAN_NEEDED:=always}
69 for _f_use in ${FORTRAN_NEEDED}; do
72 DEPEND+=" virtual/fortran"
73 RDEPEND+=" virtual/fortran"
80 DEPEND+=" ${_f_use}? ( virtual/fortran )"
83 DEPEND+=" ${_f_use}? ( virtual/fortran )"
84 RDEPEND+=" ${_f_use}? ( virtual/fortran )"
90 # @FUNCTION: fortran_int64_abi_fflags
92 # Return the Fortran compiler flag to enable 64 bit integers for
94 fortran_int64_abi_fflags() {
95 debug-print-function ${FUNCNAME} "${@}"
98 if [[ ${_FC} == *gfortran* ]]; then
99 echo "-fdefault-integer-8"
100 elif [[ ${_FC} == ifort ]]; then
101 echo "-integer-size 64"
103 die "Compiler flag for 64bit interger for ${_FC} unknown"
107 # @FUNCTION: _fortran_write_testsuite
110 # writes fortran test code
111 _fortran_write_testsuite() {
112 debug-print-function ${FUNCNAME} "${@}"
114 local filebase=${T}/test-fortran
117 cat <<- EOF > "${filebase}.f" || die
122 cat <<- EOF > "${filebase}.f90" || die
127 cat <<- EOF > "${filebase}.f03" || die
128 procedure(), pointer :: p
133 # @FUNCTION: _fortran_compile_test
134 # @USAGE: <compiler> [dialect]
137 # Takes fortran compiler as first argument and dialect as second.
138 # Checks whether the passed fortran compiler speaks the fortran dialect
139 _fortran_compile_test() {
140 debug-print-function ${FUNCNAME} "${@}"
142 local filebase=${T}/test-fortran
145 local fcode=${filebase}.f${fdia}
149 die "_fortran_compile_test() needs at least one argument"
151 [[ -f ${fcode} ]] || _fortran_write_testsuite
153 ${fcomp} "${fcode}" -o "${fcode}.x" \
154 >> "${T}"/_fortran_compile_test.log 2>&1
161 # @FUNCTION: _fortran-has-openmp
162 # @RETURN: return code of the compiler
165 # See if the fortran supports OpenMP.
166 _fortran-has-openmp() {
167 debug-print-function ${FUNCNAME} "${@}"
170 local filebase=${T}/test-fc-openmp
171 local fcode=${filebase}.f
173 local _fc=$(tc-getFC)
175 cat <<- EOF > "${fcode}" || die
176 call omp_get_num_threads
180 for flag in -fopenmp -xopenmp -openmp -mp -omp -qsmp=omp; do
181 ${_fc} ${flag} "${fcode}" -o "${fcode}.x" \
182 &>> "${T}"/_fortran_compile_test.log
184 [[ ${ret} == 0 ]] && break
191 # @FUNCTION: _fortran_die_msg
194 # Detailed description how to handle fortran support
196 debug-print-function ${FUNCNAME} "${@}"
199 eerror "Please install currently selected gcc version with USE=fortran."
200 eerror "If you intend to use a different compiler then gfortran, please"
201 eerror "set FC variable accordingly and take care that the necessary"
202 eerror "fortran dialects are supported."
204 die "Currently no working fortran compiler is available (see ${T}/_fortran_compile_test.log for information)"
207 # @FUNCTION: _fortran_test_function
210 # Internal test function for working fortran compiler.
211 # It is called in fortran-2_pkg_setup.
212 _fortran_test_function() {
213 debug-print-function ${FUNCNAME} "${@}"
217 : ${F77:=$(tc-getFC)}
219 : ${FORTRAN_STANDARD:=77}
220 for dialect in ${FORTRAN_STANDARD}; do
222 77) _fortran_compile_test $(tc-getF77) || \
224 90|95) _fortran_compile_test $(tc-getFC) 90 || \
226 2003) _fortran_compile_test $(tc-getFC) 03 || \
228 2008) die "Future" ;;
229 *) die "${dialect} is not a Fortran dialect." ;;
234 einfo "Using following Fortran compiler:"
238 if [[ ${FORTRAN_NEED_OPENMP} == 1 ]]; then
239 if _fortran-has-openmp; then
240 einfo "${FC} has OPENMP support"
242 die "Please install current gcc with USE=openmp or set the FC variable to a compiler that supports OpenMP"
247 # @FUNCTION: _fortran-2_pkg_setup
250 # _The_ fortran-2_pkg_setup() code
251 _fortran-2_pkg_setup() {
252 for _f_use in ${FORTRAN_NEEDED}; do
255 _fortran_test_function && break 2
258 einfo "Forcing fortran support off"
262 if use ${_f_use}; then
263 _fortran_test_function && break 2
274 # @FUNCTION: fortran-2_pkg_setup
276 # Setup functionality,
277 # checks for a valid fortran compiler and optionally for its openmp support.
278 fortran-2_pkg_setup() {
279 debug-print-function ${FUNCNAME} "${@}"
281 if [[ ${MERGE_TYPE} != binary ]]; then