postgres.eclass: Add 9999 slot
[gentoo.git] / eclass / postgres.eclass
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 inherit user
5 EXPORT_FUNCTIONS pkg_setup
6
7 # @ECLASS: postgres.eclass
8 # @MAINTAINER:
9 # PostgreSQL <pgsql-bugs@gentoo.org>
10 # @AUTHOR: Aaron W. Swenson <titanofold@gentoo.org>
11 # @SUPPORTED_EAPIS: 5 6 7
12 # @BLURB: An eclass for PostgreSQL-related packages
13 # @DESCRIPTION:
14 # This eclass provides common utility functions that many
15 # PostgreSQL-related packages perform, such as checking that the
16 # currently selected PostgreSQL slot is within a range, adding a system
17 # user to the postgres system group, and generating dependencies.
18
19
20 case ${EAPI:-0} in
21         5|6|7) ;;
22         *) die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}" ;;
23 esac
24
25 # @ECLASS-VARIABLE: _POSTGRES_ALL_VERSIONS
26 # @INTERNAL
27 # @DESCRIPTION:
28 # List of versions to reverse sort POSTGRES_COMPAT slots
29
30 _POSTGRES_ALL_VERSIONS=( 9999 13 12 11 10 9.6 9.5 9.4 9.3 9.2 )
31
32
33
34 # @ECLASS-VARIABLE: POSTGRES_COMPAT
35 # @DEFAULT_UNSET
36 # @DESCRIPTION:
37 # A Bash array containing a list of compatible PostgreSQL slots as
38 # defined by the developer. If declared, must be declared before
39 # inheriting this eclass. Example:
40 #@CODE
41 #POSTGRES_COMPAT=( 9.2 9.3 9.4 9.5 9.6 10 )
42 #POSTGRES_COMPAT=( 9.{2,3} 9.{4..6} 10 ) # Same as previous
43 #@CODE
44
45 # @ECLASS-VARIABLE: POSTGRES_DEP
46 # @DESCRIPTION:
47 # An automatically generated dependency string suitable for use in
48 # DEPEND and RDEPEND declarations.
49 POSTGRES_DEP="dev-db/postgresql:="
50
51 # @ECLASS-VARIABLE: POSTGRES_USEDEP
52 # @DEFAULT_UNSET
53 # @DESCRIPTION:
54 # Add the 2-Style and/or 4-Style use dependencies without brackets to be used
55 # for POSTGRES_DEP. If declared, must be declared before inheriting this eclass.
56 declare -p POSTGRES_USEDEP &>/dev/null && POSTGRES_DEP+="[${POSTGRES_USEDEP}]"
57
58 # @ECLASS-VARIABLE: POSTGRES_REQ_USE
59 # @DEFAULT_UNSET
60 # @DESCRIPTION:
61 # An automatically generated REQUIRED_USE-compatible string built upon
62 # POSTGRES_COMPAT. REQUIRED_USE="... ${POSTGRES_REQ_USE}" is only
63 # required if the package must build against one of the PostgreSQL slots
64 # declared in POSTGRES_COMPAT.
65
66 # @ECLASS-VARIABLE: _POSTGRES_COMPAT
67 # @INTERNAL
68 # @DESCRIPTION:
69 # Copy of POSTGRES_COMPAT, reverse sorted
70 _POSTGRES_COMPAT=()
71
72
73 if declare -p POSTGRES_COMPAT &> /dev/null ; then
74         # Reverse sort the given POSTGRES_COMPAT so that the most recent
75         # slot is preferred over an older slot.
76         # -- do we care if dependencies are deterministic by USE flags?
77         for i in ${_POSTGRES_ALL_VERSIONS[@]} ; do
78                 has ${i} ${POSTGRES_COMPAT[@]} && _POSTGRES_COMPAT+=( ${i} )
79         done
80
81         POSTGRES_DEP=""
82         POSTGRES_REQ_USE=" || ("
83         for slot in "${_POSTGRES_COMPAT[@]}" ; do
84                 POSTGRES_DEP+=" postgres_targets_postgres${slot/\./_}? ( dev-db/postgresql:${slot}="
85                 declare -p POSTGRES_USEDEP &>/dev/null && \
86                         POSTGRES_DEP+="[${POSTGRES_USEDEP}]"
87                 POSTGRES_DEP+=" )"
88
89                 IUSE+=" postgres_targets_postgres${slot/\./_}"
90                 POSTGRES_REQ_USE+=" postgres_targets_postgres${slot/\./_}"
91         done
92         POSTGRES_REQ_USE+=" )"
93 fi
94
95
96 # @FUNCTION: postgres_check_slot
97 # @DESCRIPTION:
98 # Verify that the currently selected PostgreSQL slot is set to one of
99 # the slots defined in POSTGRES_COMPAT. Automatically dies unless a
100 # POSTGRES_COMPAT slot is selected. Should be called in pkg_pretend().
101 postgres_check_slot() {
102         if ! declare -p POSTGRES_COMPAT &>/dev/null; then
103                 die 'POSTGRES_COMPAT not declared.'
104         fi
105
106         # Don't die because we can't run postgresql-config during pretend.
107         [[ "$EBUILD_PHASE" = "pretend" && -z "$(which postgresql-config 2> /dev/null)" ]] \
108                 && return 0
109
110         if has $(postgresql-config show 2> /dev/null) "${POSTGRES_COMPAT[@]}"; then
111                 return 0
112         else
113                 eerror "PostgreSQL slot must be set to one of: "
114                 eerror "    ${POSTGRES_COMPAT[@]}"
115                 die "Incompatible PostgreSQL slot eselected"
116         fi
117 }
118
119 # @FUNCTION: postgres_new_user
120 # @USAGE: [user [(uid|-1) [(shell|-1) [(homedir|-1) [groups]]]]]
121 # @DESCRIPTION:
122 # Creates the "postgres" system group and user -- which is separate from
123 # the database user -- and, optionally, the developer defined user. There
124 # are no required parameters.
125 #
126 # When given a user to create, it'll be created with the next available
127 # uid, default shell set to /bin/false, default homedir is /dev/null,
128 # and added to the "postgres" system group. You can use "-1" to skip any
129 # parameter except user or groups.
130 postgres_new_user() {
131         enewgroup postgres 70
132         enewuser postgres 70 /bin/bash /var/lib/postgresql postgres
133
134         if [[ $# -gt 0 ]] ; then
135                 if [[ "$1" = "postgres" ]] ; then
136                         ewarn "Username 'postgres' implied, skipping"
137                 else
138                         local groups=$5
139                         [[ -n "${groups}" ]] && groups+=",postgres" || groups="postgres"
140                         enewuser "$1" "${2:--1}" "${3:--1}" "${4:--1}" "${groups}"
141                 fi
142         fi
143 }
144
145 # @FUNCTION: postgres_pkg_setup
146 # @DESCRIPTION:
147 # Initialize environment variable(s) according to the best
148 # installed version of PostgreSQL that is also in POSTGRES_COMPAT. This
149 # is required if pkg_setup() is declared in the ebuild.
150 # Exports PG_SLOT, PG_CONFIG, and PKG_CONFIG_PATH.
151 postgres_pkg_setup() {
152         debug-print-function ${FUNCNAME} "${@}"
153
154         local compat_slot
155         local best_slot
156         for compat_slot in "${_POSTGRES_COMPAT[@]}"; do
157                 if use "postgres_targets_postgres${compat_slot/\./_}"; then
158                         best_slot="${compat_slot}"
159                         break
160                 fi
161         done
162
163         if [[ -z "${best_slot}" ]]; then
164                 local flags f
165                 for f in "${_POSTGRES_COMPAT[@]}"; do
166                         flags+=" postgres${f/./_}"
167                 done
168
169                 eerror "POSTGRES_TARGETS must contain at least one of:"
170                 eerror "    ${flags}"
171                 die "No suitable POSTGRES_TARGETS enabled."
172         fi
173
174         export PG_SLOT=${best_slot}
175         export PG_CONFIG=$(which pg_config${best_slot//./})
176
177         local pg_pkg_config_path="$(${PG_CONFIG} --libdir)/pkgconfig"
178         if [[ -n "${PKG_CONFIG_PATH}" ]]; then
179                 export PKG_CONFIG_PATH="${pg_pkg_config_path}:${PKG_CONFIG_PATH}"
180         else
181                 export PKG_CONFIG_PATH="${pg_pkg_config_path}"
182         fi
183
184         elog "PostgreSQL Target: ${best_slot}"
185 }