dev-lua/cliargs: add
[lua-alt-overlay.git] / eclass / lua-alt.eclass
blobada5b6683d931a7aa6e5251d83e35b34338c3c0a
1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: lua-alt.eclass
5 # @MAINTAINER:
6 # ???????
7 # @AUTHOR:
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.
13 # @DESCRIPTION:
14 # A common eclass providing helper functions to build and install
15 # packages supporting being installed for multiple Lua
16 # implementations.
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.
23 case "${EAPI:-0}" in
24 0|1|2|3|4|5)
25 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
27 6|7)
30 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
32 esac
35 inherit multibuild
36 inherit toolchain-funcs
38 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
40 # @ECLASS-VARIABLE: _LUA_ALL_IMPLS
41 # @INTERNAL
42 # @DESCRIPTION:
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
48 # @REQUIRED
49 # @DESCRIPTION:
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
52 # an array.
54 # Example:
55 # @CODE
56 # LUA_COMPAT=( lua5_4 lua5_3 luajit2 )
57 # @CODE
59 # @ECLASS-VARIABLE: LUA_INCLUDEDIR
60 # @DESCRIPTION:
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
65 # @DESCRIPTION:
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
70 # @DESCRIPTION:
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
75 # @DESCRIPTION:
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
81 # @INTERNAL
82 # @DESCRIPTION:
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
93 # @INTERNAL
94 # DESCRIPTION:
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
105 # @INTERNAL
106 # @DESCRIPTION:
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.
109 # Also set IUSE.
110 _lua_set_globals() {
111 debug-print-function ${FUNCNAME} "${@}"
112 local deps i
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.'
121 _LUA_IMPLS=()
123 for i in "${_LUA_ALL_IMPLS[@]}"; do
124 if has "${i}" "${LUA_COMPAT[@]}"; then
125 _LUA_IMPLS+=( "${i}" )
127 done
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}) ) "
135 done
137 local flags=( "${_LUA_ALL_IMPLS[@]/#/lua_targets_}" )
138 local requse="|| ( ${flags[*]} )"
140 local f=( )
141 for (( i = ${#_LUA_ALL_IMPLS[@]} - 1; i >= 0; i-- )); do
142 f+=( "lua_targets_${_LUA_ALL_IMPLS[i]}" )
143 done
144 IUSE=${f[*]}
146 LUA_DEPS=${deps}
147 LUA_REQUIRED_USE=${requse}
148 readonly LUA_DEPS LUA_REQUIRED_USE
150 _lua_set_globals
151 unset -f _lua_set_globals
153 # @FUNCTION: _lua_validate_useflags
154 # @INTERNAL
155 # @DESCRIPTION:
156 # Enforce the proper setting of LUA_TARGETS
157 _lua_validate_useflags() {
158 debug-print-function ${FUNCNAME} "${@}"
160 local i
162 for i in "${_LUA_ALL_IMPLS[@]}"; do
163 use "lua_targets_${i}" && return 0
164 done
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):"
168 eerror
169 eerror "${LUA_COMPAT[@]}"
170 echo
171 die "No supported Lua implementation in LUA_TARGETS."
174 # @ECLASS-VARIABLE: BUILD_DIR
175 # @DESCRIPTION:
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.
184 # Example value:
185 # @CODE
186 # ${WORKDIR}/foo-1.3-lua5_3
187 # @CODE
189 # @FUNCTION: lua_copy_sources
190 # @DESCRIPTION:
191 # Create a single copy of the package sources for each enabled Lua
192 # implementation.
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
196 # lua_foreach_abi().
197 lua_copy_sources() {
198 debug-print-function ${FUNCNAME} "${@}"
200 pushd "${S}"
202 local MULTIBUILD_VARIANTS
203 _lua_obtain_impls
205 multibuild_copy_sources
206 popd
209 # @FUNCTION: _lua_obtain_impls
210 # @INTERNAL
211 # @DESCRIPTION:
212 # Set up the enabled implementation list.
213 _lua_obtain_impls() {
214 _lua_validate_useflags
216 MULTIBUILD_VARIANTS=()
218 local impl
219 for impl in "${_LUA_ALL_IMPLS[@]}"; do
220 has "${impl}" "${LUA_COMPAT[@]}" && \
221 use "lua_targets_${impl}" && MULTIBUILD_VARIANTS+=( "${impl}" )
222 done
225 # @FUNCTION: _lua_multibuild_wrapper
226 # @USAGE: <command> [<args>...]
227 # @INTERNAL
228 # @DESCRIPTION:
229 # Initialize the environment for Lua implementation selected
230 # for multibuild.
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}"
245 pushd "${BUILD_DIR}"
246 "${@}"
247 popd
250 # @FUNCTION: lua_foreach_impl
251 # @USAGE: <command> [<args>...]
252 # @DESCRIPTION:
253 # Run the given command for each of the enabled Lua implementations.
254 # If additional parameters are passed, they will be passed through
255 # to the command.
257 # The function will return 0 status if all invocations succeed.
258 # Otherwise, the return code from first failing invocation will
259 # be returned.
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.
263 lua_foreach_impl() {
264 debug-print-function ${FUNCNAME} "${@}"
266 local MULTIBUILD_VARIANTS
267 _lua_obtain_impls
269 multibuild_foreach_variant _lua_multibuild_wrapper "${@}"
274 lua-alt_src_prepare() {
275 debug-print-function ${FUNCNAME} "${@}"
276 eapply_user
277 lua_copy_sources
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
291 lua-alt_src_test() {
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