1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: lua-alt.eclass
8 # S. Gilles <sgilles@math.umd.edu>
9 # Based on work of: Michał Górny <mgorny@gentoo.org>
10 # Based on work of: Krzysztof Pawlik <nelchael@gentoo.org>
11 # @SUPPORTED_EAPIS: 6 7
12 # @BLURB: A common, simple eclass for Lua packages.
14 # A common eclass providing helper functions to build and install
15 # packages supporting being installed for multiple Lua
18 # This eclass sets correct IUSE. Modification of REQUIRED_USE has
19 # to be done by the author of the ebuild. lua-alt also provides
20 # methods to easily run a command for each enabled Lua implementation
21 # and duplicate the sources for them.
25 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
30 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
36 inherit toolchain-funcs
38 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
40 # @ECLASS-VARIABLE: _LUA_ALL_IMPLS
43 # All supported Lua implementations
44 _LUA_ALL_IMPLS=( luajit2 lua5_1 lua5_2 lua5_3 lua5_4 )
45 readonly _LUA_ALL_IMPLS
47 # @ECLASS-VARIABLE: LUA_COMPAT
50 # This variable contains a list of Lua implementations the package
51 # supports. It must be set before the `inherit' call. It has to be
56 # LUA_COMPAT=( lua5_4 lua5_3 luajit2 )
59 # @ECLASS-VARIABLE: LUA_INCLUDEDIR
61 # Usable in phase functions. Set to the "includedir" variable, as
62 # given by pkg-config, for the relevant lua target
64 # @ECLASS-VARIABLE: LUA_LIBDIR
66 # Usable in phase functions. Set to the "libdir" variable, as
67 # given by pkg-config, for the relevant lua target
69 # @ECLASS-VARIABLE: INSTALL_LMOD
71 # Usable in phase functions. Set to the "INSTALL_LMOD" variable, as
72 # given by pkg-config, for the relevant lua target
74 # @ECLASS-VARIABLE: INSTALL_CMOD
76 # Usable in phase functions. Set to the "INSTALL_CMOD" variable, as
77 # given by pkg-config, for the relevant lua target
80 # @FUNCTION _lua_package_from_target
83 # Given "lua5_3", return "dev-lang/lua:5.3" or such
84 _lua_package_from_target() {
85 [ "$1" == "lua5_4" ] && echo "dev-lang/lua:5.4"
86 [ "$1" == "lua5_3" ] && echo "dev-lang/lua:5.3"
87 [ "$1" == "lua5_2" ] && echo "dev-lang/lua:5.2"
88 [ "$1" == "lua5_1" ] && echo "dev-lang/lua:5.1"
89 [ "$1" == "luajit2" ] && echo "dev-lang/luajit:2"
92 # @FUNCTION: _lua_pkgconfig_from_target
95 # Given a name from LUA_COMPAT, return the appropriate name for pkg-config
96 _lua_pkgconfig_from_target() {
97 [ "$1" == "lua5_4" ] && echo "lua5.4"
98 [ "$1" == "lua5_3" ] && echo "lua5.3"
99 [ "$1" == "lua5_2" ] && echo "lua5.2"
100 [ "$1" == "lua5_1" ] && echo "lua5.1"
101 [ "$1" == "luajit2" ] && echo "luajit"
104 # @FUNCTION: _lua_set_impls
107 # Check LUA_COMPAT for well-formedness and validity, then set
108 # _LUA_IMPLS to be the intersection of LUA_COMPAT and _LUA_ALL_IMPLS.
111 debug-print-function ${FUNCNAME} "${@}"
114 if ! declare -p LUA_COMPAT &>/dev/null; then
115 die 'LUA_COMPAT not declared.'
117 if [[ $(declare -p LUA_COMPAT) != "declare -a"* ]]; then
118 die 'LUA_COMPAT must be an array.'
123 for i in "${_LUA_ALL_IMPLS[@]}"; do
124 if has "${i}" "${LUA_COMPAT[@]}"; then
125 _LUA_IMPLS+=( "${i}" )
129 if [[ ! ${_LUA_IMPLS[@]} ]]; then
130 die "No supported implementation in LUA_COMPAT."
133 for i in "${_LUA_IMPLS[@]}"; do
134 deps+="lua_targets_${i}? ( $(_lua_package_from_target ${i}) ) "
137 local flags=( "${_LUA_ALL_IMPLS[@]/#/lua_targets_}" )
138 local requse="|| ( ${flags[*]} )"
141 for (( i = ${#_LUA_ALL_IMPLS[@]} - 1; i >= 0; i-- )); do
142 f+=( "lua_targets_${_LUA_ALL_IMPLS[i]}" )
147 LUA_REQUIRED_USE=${requse}
148 readonly LUA_DEPS LUA_REQUIRED_USE
151 unset -f _lua_set_globals
153 # @FUNCTION: _lua_validate_useflags
156 # Enforce the proper setting of LUA_TARGETS
157 _lua_validate_useflags() {
158 debug-print-function ${FUNCNAME} "${@}"
162 for i in "${_LUA_ALL_IMPLS[@]}"; do
163 use "lua_targets_${i}" && return 0
166 eerror "No Lua implementation selected for the build. Please add one"
167 eerror "of the following values to your LUA_TARGETS (in make.conf):"
169 eerror "${LUA_COMPAT[@]}"
171 die "No supported Lua implementation in LUA_TARGETS."
174 # @ECLASS-VARIABLE: BUILD_DIR
176 # The current build directory. In global scope, it is supposed to
177 # contain an initial build directory; if unset, it defaults to ${S}.
179 # In functions run by lua_foreach_impl(), the BUILD_DIR is locally
180 # set to an implementation-specific build directory. That path is
181 # created through appending a hyphen and the implementation name
182 # to the final component of the initial BUILD_DIR.
186 # ${WORKDIR}/foo-1.3-lua5_3
189 # @FUNCTION: lua_copy_sources
191 # Create a single copy of the package sources for each enabled Lua
194 # The sources are always copied from initial BUILD_DIR (or S if unset)
195 # to implementation-specific build directory matching BUILD_DIR used by
198 debug-print-function ${FUNCNAME} "${@}"
202 local MULTIBUILD_VARIANTS
205 multibuild_copy_sources
209 # @FUNCTION: _lua_obtain_impls
212 # Set up the enabled implementation list.
213 _lua_obtain_impls() {
214 _lua_validate_useflags
216 MULTIBUILD_VARIANTS=()
219 for impl in "${_LUA_ALL_IMPLS[@]}"; do
220 has "${impl}" "${LUA_COMPAT[@]}" && \
221 use "lua_targets_${impl}" && MULTIBUILD_VARIANTS+=( "${impl}" )
225 # @FUNCTION: _lua_multibuild_wrapper
226 # @USAGE: <command> [<args>...]
229 # Initialize the environment for Lua implementation selected
231 _lua_multibuild_wrapper() {
232 debug-print-function ${FUNCNAME} "${@}"
233 local l=$(_lua_pkgconfig_from_target "${MULTIBUILD_VARIANT}")
235 local -x LUA_INCLUDEDIR=$($(tc-getPKG_CONFIG) --variable includedir "${l}")
236 local -x LUA_LIBDIR=$($(tc-getPKG_CONFIG) --variable libdir "${l}")
237 local -x LUA_V=$($(tc-getPKG_CONFIG) --variable V "${l}")
238 [ -z "${LUA_V}" ] && LUA_V=$($(tc-getPKG_CONFIG) --variable abiver "${l}")
239 local -x INSTALL_LMOD=$($(tc-getPKG_CONFIG) --variable INSTALL_LMOD "${l}")
240 local -x INSTALL_CMOD=$($(tc-getPKG_CONFIG) --variable INSTALL_CMOD "${l}")
241 local -x CFLAGS="${CFLAGS} $($(tc-getPKG_CONFIG) --cflags ${l})"
242 local -x LDFLAGS="${LDFLAGS} $($(tc-getPKG_CONFIG) --libs ${l})"
243 local -x prefix="${ED}"
250 # @FUNCTION: lua_foreach_impl
251 # @USAGE: <command> [<args>...]
253 # Run the given command for each of the enabled Lua implementations.
254 # If additional parameters are passed, they will be passed through
257 # The function will return 0 status if all invocations succeed.
258 # Otherwise, the return code from first failing invocation will
261 # For each command being run, ELUA, LUA and BUILD_DIR are set
262 # locally, and the former two are exported to the command environment.
264 debug-print-function ${FUNCNAME} "${@}"
266 local MULTIBUILD_VARIANTS
269 multibuild_foreach_variant _lua_multibuild_wrapper "${@}"
274 lua-alt_src_prepare() {
275 debug-print-function ${FUNCNAME} "${@}"
278 lua_foreach_impl default
281 lua-alt_src_configure() {
282 debug-print-function ${FUNCNAME} "${@}"
283 lua_foreach_impl default
286 lua-alt_src_compile() {
287 debug-print-function ${FUNCNAME} "${@}"
288 lua_foreach_impl default
292 debug-print-function ${FUNCNAME} "${@}"
293 lua_foreach_impl default
296 lua-alt_src_install() {
297 debug-print-function ${FUNCNAME} "${@}"
298 lua_foreach_impl default