summaryrefslogtreecommitdiff
blob: dcf7ee979d8afee26c48613f8c0f1f8cd9d8bd36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Copyright 1999-2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: fortran-2.eclass
# @MAINTAINER:
# sci@gentoo.org
# @AUTHOR:
# Author Justin Lecher <jlec@gentoo.org>
# Test functions provided by Sebastien Fabbro and Kacper Kowalik
# @SUPPORTED_EAPIS: 7 8
# @BLURB: Simplify fortran compiler management
# @DESCRIPTION:
# If you need a fortran compiler, then you should be inheriting this eclass.
# In case you only need optional support, please export FORTRAN_NEEDED before
# inheriting the eclass.
#
# The eclass tests for working fortran compilers
# and exports the variables FC and F77.
# Optionally, it checks for extended capabilities based on
# the variable options selected in the ebuild
# The only phase function exported is fortran-2_pkg_setup.
# @EXAMPLE:
# FORTRAN_NEEDED="lapack fortran"
#
# inherit fortran-2
#
# FORTRAN_NEED_OPENMP=1

if [[ -z ${_FORTRAN_2_ECLASS} ]]; then
_FORTRAN_2_ECLASS=1

case ${EAPI} in
	7|8) ;;
	*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac

inherit toolchain-funcs

# @ECLASS_VARIABLE: FORTRAN_NEED_OPENMP
# @DESCRIPTION:
# Set to "1" in order to automatically have the eclass abort if the fortran
# compiler lacks openmp support.
: "${FORTRAN_NEED_OPENMP:=0}"

# @ECLASS_VARIABLE: FORTRAN_STANDARD
# @DESCRIPTION:
# Set this, if a special dialect needs to be supported.
# Generally not needed as default is sufficient.
#
# Valid settings are any combination of: 77 90 95 2003
: "${FORTRAN_STANDARD:=77}"

# @ECLASS_VARIABLE: FORTRAN_NEEDED
# @DESCRIPTION:
# If your package has an optional fortran support, set this variable
# to the space separated list of USE triggering the fortran
# dependency.
#
# e.g. FORTRAN_NEEDED=lapack would result in
#
# DEPEND="lapack? ( virtual/fortran )"
#
# If unset, we always depend on virtual/fortran.
: "${FORTRAN_NEEDED:=always}"

for _f_use in ${FORTRAN_NEEDED}; do
	case ${_f_use} in
		always)
			BDEPEND+=" virtual/fortran"
			DEPEND+=" virtual/fortran"
			RDEPEND+=" virtual/fortran"
			break
			;;
		no)
			break
			;;
		test)
			BDEPEND+=" ${_f_use}? ( virtual/fortran )"
			;;
		*)
			BDEPEND+=" ${_f_use}? ( virtual/fortran )"
			DEPEND+=" ${_f_use}? ( virtual/fortran )"
			RDEPEND+=" ${_f_use}? ( virtual/fortran )"
			;;
	esac
done
unset _f_use

# @FUNCTION: fortran_int64_abi_fflags
# @DESCRIPTION:
# Return the Fortran compiler flag to enable 64 bit integers for
# array indices
fortran_int64_abi_fflags() {
	debug-print-function ${FUNCNAME} "$@"

	local _FC=$(tc-getFC)
	if [[ ${_FC} == *gfortran* ]]; then
		echo "-fdefault-integer-8"
	elif [[ ${_FC} == ifort ]]; then
		echo "-integer-size 64"
	else
		die "Compiler flag for 64bit integer for ${_FC} unknown"
	fi
}

# @FUNCTION: _fortran_write_testsuite
# @INTERNAL
# @DESCRIPTION:
# writes fortran test code
_fortran_write_testsuite() {
	debug-print-function ${FUNCNAME} "$@"

	local filebase=${T}/test-fortran

	# f77 code
	cat <<- EOF > "${filebase}.f" || die
	       end
	EOF

	# f90/95 code
	cat <<- EOF > "${filebase}.f90" || die
	end
	EOF

	# f2003 code
	cat <<- EOF > "${filebase}.f03" || die
	       procedure(), pointer :: p
	       end
	EOF
}

# @FUNCTION: _fortran_compile_test
# @USAGE: <compiler> [dialect]
# @INTERNAL
# @DESCRIPTION:
# Takes fortran compiler as first argument and dialect as second.
# Checks whether the passed fortran compiler speaks the fortran dialect
_fortran_compile_test() {
	debug-print-function ${FUNCNAME} "$@"

	local filebase=${T}/test-fortran
	local fcomp=${1}
	local fdia=${2}
	local fcode=${filebase}.f${fdia}
	local ret

	[[ $# -lt 1 ]] && \
		die "_fortran_compile_test() needs at least one argument"

	[[ -f ${fcode} ]] || _fortran_write_testsuite

	${fcomp} "${fcode}" -o "${fcode}.x" \
		>> "${T}"/_fortran_compile_test.log 2>&1
	ret=$?

	rm -f "${fcode}.x"
	return ${ret}
}

# @FUNCTION: _fortran-has-openmp
# @RETURN: return code of the compiler
# @INTERNAL
# @DESCRIPTION:
# See if the fortran supports OpenMP.
_fortran-has-openmp() {
	debug-print-function ${FUNCNAME} "$@"

	local flag
	local filebase=${T}/test-fc-openmp
	local fcode=${filebase}.f
	local ret
	local _fc=$(tc-getFC)

	cat <<- EOF > "${fcode}" || die
	       call omp_get_num_threads
	       end
	EOF

	for flag in -fopenmp -xopenmp -openmp -mp -omp -qsmp=omp; do
		${_fc} ${flag} "${fcode}" -o "${fcode}.x" \
			&>> "${T}"/_fortran_compile_test.log
		ret=$?
		[[ ${ret} == 0 ]] && break
	done

	rm -f "${fcode}.x"
	return ${ret}
}

# @FUNCTION: _fortran_die_msg
# @INTERNAL
# @DESCRIPTION:
# Detailed description how to handle fortran support
_fortran_die_msg() {
	debug-print-function ${FUNCNAME} "$@"

	eerror
	eerror "Please install currently selected gcc version with USE=fortran."
	eerror "If you intend to use a different compiler then gfortran, please"
	eerror "set FC variable accordingly and take care that the necessary"
	eerror "fortran dialects are supported."
	eerror
	die "Currently no working fortran compiler is available (see ${T}/_fortran_compile_test.log for information)"
}

# @FUNCTION: _fortran_test_function
# @INTERNAL
# @DESCRIPTION:
# Internal test function for working fortran compiler.
# It is called in fortran-2_pkg_setup.
_fortran_test_function() {
	debug-print-function ${FUNCNAME} "$@"

	local dialect

	: "${F77:=$(tc-getFC)}"

	: "${FORTRAN_STANDARD:=77}"
	for dialect in ${FORTRAN_STANDARD}; do
		case ${dialect} in
			77) _fortran_compile_test "$(tc-getF77)" || \
				_fortran_die_msg ;;
			90|95) _fortran_compile_test "$(tc-getFC)" 90 || \
				_fortran_die_msg ;;
			2003) _fortran_compile_test "$(tc-getFC)" 03 || \
				_fortran_die_msg ;;
			2008) die "Future" ;;
			*) die "${dialect} is not a Fortran dialect." ;;
		esac
	done

	tc-export F77 FC
	einfo "Using following Fortran compiler:"
	einfo "  F77: ${F77}"
	einfo "  FC:  ${FC}"

	if [[ ${FORTRAN_NEED_OPENMP} == 1 ]]; then
		if _fortran-has-openmp; then
			einfo "${FC} has OPENMP support"
		else
			die "Please install current gcc with USE=openmp or set the FC variable to a compiler that supports OpenMP"
		fi
	fi
}

# @FUNCTION: _fortran-2_pkg_setup
# @INTERNAL
# @DESCRIPTION:
# _The_ fortran-2_pkg_setup() code
_fortran-2_pkg_setup() {
	for _f_use in ${FORTRAN_NEEDED}; do
	case ${_f_use} in
		always)
			_fortran_test_function && break 2
			;;
		no)
			einfo "Forcing fortran support off"
			break
			;;
		*)
			if use ${_f_use}; then
				_fortran_test_function && break 2
			else
				unset FC
				unset F77
			fi
			;;
		esac
	done
}


# @FUNCTION: fortran-2_pkg_setup
# @DESCRIPTION:
# Setup functionality,
# checks for a valid fortran compiler and optionally for its openmp support.
fortran-2_pkg_setup() {
	debug-print-function ${FUNCNAME} "$@"

	if [[ ${MERGE_TYPE} != binary ]]; then
		_fortran-2_pkg_setup
	fi
}

fi

EXPORT_FUNCTIONS pkg_setup