1 # Copyright 1999-2018 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
95 fortran_int64_abi_fflags() {
96 debug-print-function ${FUNCNAME} "${@}"
99 if [[ ${_FC} == *gfortran* ]]; then
100 echo "-fdefault-integer-8"
101 elif [[ ${_FC} == ifort ]]; then
102 echo "-integer-size 64"
104 die "Compiler flag for 64bit interger for ${_FC} unknown"
108 # @FUNCTION: _fortran_write_testsuite
111 # writes fortran test code
112 _fortran_write_testsuite() {
113 debug-print-function ${FUNCNAME} "${@}"
115 local filebase=${T}/test-fortran
118 cat <<- EOF > "${filebase}.f" || die
123 cat <<- EOF > "${filebase}.f90" || die
128 cat <<- EOF > "${filebase}.f03" || die
129 procedure(), pointer :: p
134 # @FUNCTION: _fortran_compile_test
135 # @USAGE: <compiler> [dialect]
138 # Takes fortran compiler as first argument and dialect as second.
139 # Checks whether the passed fortran compiler speaks the fortran dialect
140 _fortran_compile_test() {
141 debug-print-function ${FUNCNAME} "${@}"
143 local filebase=${T}/test-fortran
146 local fcode=${filebase}.f${fdia}
150 die "_fortran_compile_test() needs at least one argument"
152 [[ -f ${fcode} ]] || _fortran_write_testsuite
154 ${fcomp} "${fcode}" -o "${fcode}.x" \
155 >> "${T}"/_fortran_compile_test.log 2>&1
162 # @FUNCTION: _fortran-has-openmp
163 # @RETURN: return code of the compiler
166 # See if the fortran supports OpenMP.
167 _fortran-has-openmp() {
168 debug-print-function ${FUNCNAME} "${@}"
171 local filebase=${T}/test-fc-openmp
172 local fcode=${filebase}.f
174 local _fc=$(tc-getFC)
176 cat <<- EOF > "${fcode}" || die
177 call omp_get_num_threads
181 for flag in -fopenmp -xopenmp -openmp -mp -omp -qsmp=omp; do
182 ${_fc} ${flag} "${fcode}" -o "${fcode}.x" \
183 &>> "${T}"/_fortran_compile_test.log
185 [[ ${ret} == 0 ]] && break
192 # @FUNCTION: _fortran_die_msg
195 # Detailed description how to handle fortran support
197 debug-print-function ${FUNCNAME} "${@}"
200 eerror "Please install currently selected gcc version with USE=fortran."
201 eerror "If you intend to use a different compiler then gfortran, please"
202 eerror "set FC variable accordingly and take care that the necessary"
203 eerror "fortran dialects are supported."
205 die "Currently no working fortran compiler is available (see ${T}/_fortran_compile_test.log for information)"
208 # @FUNCTION: _fortran_test_function
211 # Internal test function for working fortran compiler.
212 # It is called in fortran-2_pkg_setup.
213 _fortran_test_function() {
214 debug-print-function ${FUNCNAME} "${@}"
218 : ${F77:=$(tc-getFC)}
220 : ${FORTRAN_STANDARD:=77}
221 for dialect in ${FORTRAN_STANDARD}; do
223 77) _fortran_compile_test $(tc-getF77) || \
225 90|95) _fortran_compile_test $(tc-getFC) 90 || \
227 2003) _fortran_compile_test $(tc-getFC) 03 || \
229 2008) die "Future" ;;
230 *) die "${dialect} is not a Fortran dialect." ;;
235 einfo "Using following Fortran compiler:"
239 if [[ ${FORTRAN_NEED_OPENMP} == 1 ]]; then
240 if _fortran-has-openmp; then
241 einfo "${FC} has OPENMP support"
243 die "Please install current gcc with USE=openmp or set the FC variable to a compiler that supports OpenMP"
248 # @FUNCTION: _fortran-2_pkg_setup
251 # _The_ fortran-2_pkg_setup() code
252 _fortran-2_pkg_setup() {
253 for _f_use in ${FORTRAN_NEEDED}; do
256 _fortran_test_function && break 2
259 einfo "Forcing fortran support off"
263 if use ${_f_use}; then
264 _fortran_test_function && break 2
275 # @FUNCTION: fortran-2_pkg_setup
277 # Setup functionality,
278 # checks for a valid fortran compiler and optionally for its openmp support.
279 fortran-2_pkg_setup() {
280 debug-print-function ${FUNCNAME} "${@}"
282 if [[ ${MERGE_TYPE} != binary ]]; then