1 # Copyright 1999-2013 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 # @ECLASS-VARIABLE: FORTRAN_NEED_OPENMP
32 # Set to "1" in order to automatically have the eclass abort if the fortran
33 # compiler lacks openmp support.
34 : ${FORTRAN_NEED_OPENMP:=0}
36 # @ECLASS-VARIABLE: FORTRAN_STANDARD
38 # Set this, if a special dialect needs to be supported.
39 # Generally not needed as default is sufficient.
41 # Valid settings are any combination of: 77 90 95 2003
42 : ${FORTRAN_STANDARD:=77}
44 # @ECLASS-VARIABLE: FORTRAN_NEEDED
46 # If your package has an optional fortran support, set this variable
47 # to the space separated list of USE triggering the fortran
50 # e.g. FORTRAN_NEEDED=lapack would result in
52 # DEPEND="lapack? ( virtual/fortran )"
54 # If unset, we always depend on virtual/fortran.
55 : ${FORTRAN_NEEDED:=always}
57 inherit eutils toolchain-funcs
59 for _f_use in ${FORTRAN_NEEDED}; do
62 DEPEND+=" virtual/fortran"
69 DEPEND+=" ${_f_use}? ( virtual/fortran )"
75 # @FUNCTION: _fortran_write_testsuite
78 # writes fortran test code
79 _fortran_write_testsuite() {
80 local filebase=${T}/test-fortran
83 cat <<- EOF > "${filebase}.f"
88 cat <<- EOF > "${filebase}.f90"
93 cat <<- EOF > "${filebase}.f03"
94 procedure(), pointer :: p
99 # @FUNCTION: _fortran_compile_test
100 # @USAGE: <compiler> [dialect]
103 # Takes fortran compiler as first argument and dialect as second.
104 # Checks whether the passed fortran compiler speaks the fortran dialect
105 _fortran_compile_test() {
106 local filebase=${T}/test-fortran
109 local fcode=${filebase}.f${fdia}
113 die "_fortran_compile_test() needs at least one argument"
115 [[ -f ${fcode} ]] || _fortran_write_testsuite
117 ${fcomp} "${fcode}" -o "${fcode}.x" \
118 >> "${T}"/_fortran_compile_test.log 2>&1
125 # @FUNCTION: _fortran-has-openmp
126 # @RETURN: return code of the compiler
129 # See if the fortran supports OpenMP.
130 _fortran-has-openmp() {
132 local filebase=${T}/test-fc-openmp
133 local fcode=${filebase}.f
135 local _fc=$(tc-getFC)
137 cat <<- EOF > "${fcode}"
138 call omp_get_num_threads
142 for flag in -fopenmp -xopenmp -openmp -mp -omp -qsmp=omp; do
143 ${_fc} ${flag} "${fcode}" -o "${fcode}.x" \
144 &>> "${T}"/_fortran_compile_test.log
146 (( ${ret} )) || break
153 # @FUNCTION: _fortran_die_msg
156 # Detailed description how to handle fortran support
159 eerror "Please install currently selected gcc version with USE=fortran."
160 eerror "If you intend to use a different compiler then gfortran, please"
161 eerror "set FC variable accordingly and take care that the necessary"
162 eerror "fortran dialects are supported."
164 die "Currently no working fortran compiler is available"
167 # @FUNCTION: _fortran_test_function
170 # Internal test function for working fortran compiler.
171 # It is called in fortran-2_pkg_setup.
172 _fortran_test_function() {
175 : ${F77:=$(tc-getFC)}
177 : ${FORTRAN_STANDARD:=77}
178 for dialect in ${FORTRAN_STANDARD}; do
180 77) _fortran_compile_test $(tc-getF77) || \
182 90|95) _fortran_compile_test $(tc-getFC) 90 || \
184 2003) _fortran_compile_test $(tc-getFC) 03 || \
186 2008) die "Future" ;;
187 *) die "${dialect} is not a Fortran dialect." ;;
192 einfo "Using following Fortran compiler:"
196 if [[ ${FORTRAN_NEED_OPENMP} == 1 ]]; then
197 if _fortran-has-openmp; then
198 einfo "${FC} has OPENMP support"
200 die "Please install current gcc with USE=openmp or set the FC variable to a compiler that supports OpenMP"
205 # @FUNCTION: _fortran-2_pkg_setup
208 # _The_ fortran-2_pkg_setup() code
209 _fortran-2_pkg_setup() {
210 for _f_use in ${FORTRAN_NEEDED}; do
213 _fortran_test_function && break
216 einfo "Forcing fortran support off"
220 if use ${_f_use}; then
221 _fortran_test_function && break
232 # @FUNCTION: fortran-2_pkg_setup
234 # Setup functionality,
235 # checks for a valid fortran compiler and optionally for its openmp support.
236 fortran-2_pkg_setup() {
239 eqawarn "Support for EAPI < 4 will be removed from the"
240 eqawarn "fortran-2.eclass in until 2013-09-30."
241 eqawarn "Please migrate your package to a higher EAPI"
242 eqawarn "or file a bug at https://bugs.gentoo.org"
243 _fortran-2_pkg_setup ;;
245 if [[ ${MERGE_TYPE} != binary ]]; then
253 0|1|2|3|4|5) EXPORT_FUNCTIONS pkg_setup ;;
254 *) die "EAPI=${EAPI} is not supported" ;;