Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH] app-alternatives.eclass: New eclass to streamline app-alternatives/*
Date: Sat, 03 Dec 2022 07:04:55
Message-Id: 20221203070445.247518-1-mgorny@gentoo.org
1 Signed-off-by: Michał Górny <mgorny@g.o>
2 ---
3 eclass/app-alternatives.eclass | 84 ++++++++++++++++++++++++++++++++++
4 1 file changed, 84 insertions(+)
5 create mode 100644 eclass/app-alternatives.eclass
6
7 PR with examples: https://github.com/gentoo/gentoo/pull/28515
8
9 diff --git a/eclass/app-alternatives.eclass b/eclass/app-alternatives.eclass
10 new file mode 100644
11 index 000000000000..69315e4bc985
12 --- /dev/null
13 +++ b/eclass/app-alternatives.eclass
14 @@ -0,0 +1,84 @@
15 +# Copyright 2022 Gentoo Authors
16 +# Distributed under the terms of the GNU General Public License v2
17 +
18 +# @ECLASS: app-alternatives.eclass
19 +# @MAINTAINER:
20 +# Michał Górny <mgorny@g.o>
21 +# @AUTHOR:
22 +# Michał Górny <mgorny@g.o>
23 +# @SUPPORTED_EAPIS: 8
24 +# @BLURB: Common logic for app-alternatives/*
25 +# @DESCRIPTION:
26 +# This eclass provides common logic shared by app-alternatives/*
27 +# ebuilds. A global ALTERNATIVES variable needs to be declared
28 +# that lists available options and their respective dependencies.
29 +# HOMEPAGE, S, LICENSE, SLOT, IUSE, REQUIRED_USE and RDEPEND are set.
30 +# A get_alternative() function is provided that prints the determines
31 +# the selected alternative and prints its respective flag name.
32 +
33 +case ${EAPI} in
34 + 8) ;;
35 + *) die "${ECLASS}: EAPI ${EAPI} unsupported."
36 +esac
37 +
38 +if [[ ! ${_APP_ALTERNATIVES_ECLASS} ]]; then
39 +_APP_ALTERNATIVES_ECLASS=1
40 +
41 +# @ECLASS_VARIABLE: ALTERNATIVES
42 +# @PRE_INHERIT
43 +# @REQUIRED
44 +# @DESCRIPTION:
45 +# Array of "flag:dependency" pairs specifying the available
46 +# alternatives. The default provider must be listed first.
47 +
48 +# @FUNCTION: _app-alternatives_set_globals
49 +# @INTERNAL
50 +# @DESCRIPTION:
51 +# Set ebuild metadata variables.
52 +_app-alternatives_set_globals() {
53 + debug-print-function ${FUNCNAME} "${@}"
54 +
55 + if [[ $(declare -p ALTERNATIVES) != "declare -a"* ]]; then
56 + die 'ALTERNATIVES must be an array.'
57 + elif [[ ${#ALTERNATIVES[@]} -eq 0 ]]; then
58 + die 'ALTERNATIVES must not be empty.'
59 + fi
60 +
61 + HOMEPAGE="https://wiki.gentoo.org/wiki/Project:Base/Alternatives"
62 + S=${WORKDIR}
63 +
64 + LICENSE="CC0-1.0"
65 + SLOT="0"
66 +
67 + # yep, that's a cheap hack adding '+' to the first flag
68 + IUSE="+${ALTERNATIVES[*]%%:*}"
69 + REQUIRED_USE="^^ ( ${ALTERNATIVES[*]%%:*} )"
70 + RDEPEND=""
71 +
72 + local flag dep
73 + for flag in "${ALTERNATIVES[@]}"; do
74 + [[ ${flag} != *:* ]] && die "Invalid ALTERNATIVES item: ${flag}"
75 + dep=${flag#*:}
76 + flag=${flag%%:*}
77 + RDEPEND+="
78 + ${flag}? ( ${dep} )
79 + "
80 + done
81 +}
82 +_app-alternatives_set_globals
83 +
84 +# @FUNCTION: get_alternative
85 +# @DESCRIPTION:
86 +# Get the flag name for the selected alterantive (i.e. the USE flag set).
87 +get_alternative() {
88 + debug-print-function ${FUNCNAME} "${@}"
89 +
90 + local flag
91 + for flag in "${ALTERNATIVES[@]%%:*}"; do
92 + usev "${flag}" && return
93 + done
94 +
95 + die "No selected alternative found (REQUIRED_USE ignored?!)"
96 +}
97 +
98 +fi
99 --
100 2.38.1

Replies