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 if [[ ! ${_FORTRAN_2_CLASS} ]]; then
32 # @ECLASS-VARIABLE: FORTRAN_NEED_OPENMP
34 # Set to "1" in order to automatically have the eclass abort if the fortran
35 # compiler lacks openmp support.
36 : ${FORTRAN_NEED_OPENMP:=0}
38 # @ECLASS-VARIABLE: FORTRAN_STANDARD
40 # Set this, if a special dialect needs to be supported.
41 # Generally not needed as default is sufficient.
43 # Valid settings are any combination of: 77 90 95 2003
44 : ${FORTRAN_STANDARD:=77}
46 # @ECLASS-VARIABLE: FORTRAN_NEEDED
48 # If your package has an optional fortran support, set this variable
49 # to the space separated list of USE triggering the fortran
52 # e.g. FORTRAN_NEEDED=lapack would result in
54 # DEPEND="lapack? ( virtual/fortran )"
56 # If unset, we always depend on virtual/fortran.
57 : ${FORTRAN_NEEDED:=always}
59 inherit eutils toolchain-funcs
61 for _f_use in ${FORTRAN_NEEDED}; do
64 DEPEND+=" virtual/fortran"
65 RDEPEND+=" virtual/fortran"
72 DEPEND+=" ${_f_use}? ( virtual/fortran )"
75 DEPEND+=" ${_f_use}? ( virtual/fortran )"
76 RDEPEND+=" ${_f_use}? ( virtual/fortran )"
81 # @FUNCTION: fortran_int64_abi_fflags
82 # @DESCRIPTION: Return the Fortran compiler flag to enable 64 bit integers for
85 fortran_int64_abi_fflags() {
86 debug-print-function ${FUNCNAME} "${@}"
88 if [[ ${_FC} == *gfortran* ]]; then
89 echo "-fdefault-integer-8"
90 elif [[ ${_FC} == ifort ]]; then
91 echo "-integer-size 64"
93 die "Compiler flag for 64bit interger for ${_FC} unknown"
97 # @FUNCTION: _fortran_write_testsuite
100 # writes fortran test code
101 _fortran_write_testsuite() {
102 local filebase=${T}/test-fortran
105 cat <<- EOF > "${filebase}.f"
110 cat <<- EOF > "${filebase}.f90"
115 cat <<- EOF > "${filebase}.f03"
116 procedure(), pointer :: p
121 # @FUNCTION: _fortran_compile_test
122 # @USAGE: <compiler> [dialect]
125 # Takes fortran compiler as first argument and dialect as second.
126 # Checks whether the passed fortran compiler speaks the fortran dialect
127 _fortran_compile_test() {
128 local filebase=${T}/test-fortran
131 local fcode=${filebase}.f${fdia}
135 die "_fortran_compile_test() needs at least one argument"
137 [[ -f ${fcode} ]] || _fortran_write_testsuite
139 ${fcomp} "${fcode}" -o "${fcode}.x" \
140 >> "${T}"/_fortran_compile_test.log 2>&1
147 # @FUNCTION: _fortran-has-openmp
148 # @RETURN: return code of the compiler
151 # See if the fortran supports OpenMP.
152 _fortran-has-openmp() {
154 local filebase=${T}/test-fc-openmp
155 local fcode=${filebase}.f
157 local _fc=$(tc-getFC)
159 cat <<- EOF > "${fcode}"
160 call omp_get_num_threads
164 for flag in -fopenmp -xopenmp -openmp -mp -omp -qsmp=omp; do
165 ${_fc} ${flag} "${fcode}" -o "${fcode}.x" \
166 &>> "${T}"/_fortran_compile_test.log
168 (( ${ret} )) || break
175 # @FUNCTION: _fortran_die_msg
178 # Detailed description how to handle fortran support
181 eerror "Please install currently selected gcc version with USE=fortran."
182 eerror "If you intend to use a different compiler then gfortran, please"
183 eerror "set FC variable accordingly and take care that the necessary"
184 eerror "fortran dialects are supported."
186 die "Currently no working fortran compiler is available"
189 # @FUNCTION: _fortran_test_function
192 # Internal test function for working fortran compiler.
193 # It is called in fortran-2_pkg_setup.
194 _fortran_test_function() {
197 : ${F77:=$(tc-getFC)}
199 : ${FORTRAN_STANDARD:=77}
200 for dialect in ${FORTRAN_STANDARD}; do
202 77) _fortran_compile_test $(tc-getF77) || \
204 90|95) _fortran_compile_test $(tc-getFC) 90 || \
206 2003) _fortran_compile_test $(tc-getFC) 03 || \
208 2008) die "Future" ;;
209 *) die "${dialect} is not a Fortran dialect." ;;
214 einfo "Using following Fortran compiler:"
218 if [[ ${FORTRAN_NEED_OPENMP} == 1 ]]; then
219 if _fortran-has-openmp; then
220 einfo "${FC} has OPENMP support"
222 die "Please install current gcc with USE=openmp or set the FC variable to a compiler that supports OpenMP"
227 # @FUNCTION: _fortran-2_pkg_setup
230 # _The_ fortran-2_pkg_setup() code
231 _fortran-2_pkg_setup() {
232 for _f_use in ${FORTRAN_NEEDED}; do
235 _fortran_test_function && break
238 einfo "Forcing fortran support off"
242 if use ${_f_use}; then
243 _fortran_test_function && break
254 # @FUNCTION: fortran-2_pkg_setup
256 # Setup functionality,
257 # checks for a valid fortran compiler and optionally for its openmp support.
258 fortran-2_pkg_setup() {
261 eqawarn "Support for EAPI < 4 will be removed from the"
262 eqawarn "fortran-2.eclass in until 2013-09-30."
263 eqawarn "Please migrate your package to a higher EAPI"
264 eqawarn "or file a bug at https://bugs.gentoo.org"
265 _fortran-2_pkg_setup ;;
267 if [[ ${MERGE_TYPE} != binary ]]; then
275 0|1|2|3|4|5) EXPORT_FUNCTIONS pkg_setup ;;
276 *) die "EAPI=${EAPI} is not supported" ;;