1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
5 EXPORT_FUNCTIONS pkg_setup
7 # @ECLASS: postgres.eclass
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
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.
22 *) die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}" ;;
25 # @ECLASS-VARIABLE: _POSTGRES_ALL_VERSIONS
28 # List of versions to reverse sort POSTGRES_COMPAT slots
30 _POSTGRES_ALL_VERSIONS=( 9999 13 12 11 10 9.6 9.5 9.4 9.3 9.2 )
34 # @ECLASS-VARIABLE: POSTGRES_COMPAT
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:
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
45 # @ECLASS-VARIABLE: POSTGRES_DEP
47 # An automatically generated dependency string suitable for use in
48 # DEPEND and RDEPEND declarations.
49 POSTGRES_DEP="dev-db/postgresql:="
51 # @ECLASS-VARIABLE: POSTGRES_USEDEP
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}]"
58 # @ECLASS-VARIABLE: POSTGRES_REQ_USE
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.
66 # @ECLASS-VARIABLE: _POSTGRES_COMPAT
69 # Copy of POSTGRES_COMPAT, reverse sorted
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} )
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}]"
89 IUSE+=" postgres_targets_postgres${slot/\./_}"
90 POSTGRES_REQ_USE+=" postgres_targets_postgres${slot/\./_}"
92 POSTGRES_REQ_USE+=" )"
96 # @FUNCTION: postgres_check_slot
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.'
106 # Don't die because we can't run postgresql-config during pretend.
107 [[ "$EBUILD_PHASE" = "pretend" && -z "$(which postgresql-config 2> /dev/null)" ]] \
110 if has $(postgresql-config show 2> /dev/null) "${POSTGRES_COMPAT[@]}"; then
113 eerror "PostgreSQL slot must be set to one of: "
114 eerror " ${POSTGRES_COMPAT[@]}"
115 die "Incompatible PostgreSQL slot eselected"
119 # @FUNCTION: postgres_new_user
120 # @USAGE: [user [(uid|-1) [(shell|-1) [(homedir|-1) [groups]]]]]
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.
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
134 if [[ $# -gt 0 ]] ; then
135 if [[ "$1" = "postgres" ]] ; then
136 ewarn "Username 'postgres' implied, skipping"
139 [[ -n "${groups}" ]] && groups+=",postgres" || groups="postgres"
140 enewuser "$1" "${2:--1}" "${3:--1}" "${4:--1}" "${groups}"
145 # @FUNCTION: postgres_pkg_setup
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} "${@}"
156 for compat_slot in "${_POSTGRES_COMPAT[@]}"; do
157 if use "postgres_targets_postgres${compat_slot/\./_}"; then
158 best_slot="${compat_slot}"
163 if [[ -z "${best_slot}" ]]; then
165 for f in "${_POSTGRES_COMPAT[@]}"; do
166 flags+=" postgres${f/./_}"
169 eerror "POSTGRES_TARGETS must contain at least one of:"
171 die "No suitable POSTGRES_TARGETS enabled."
174 export PG_SLOT=${best_slot}
175 export PG_CONFIG=$(which pg_config${best_slot//./})
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}"
181 export PKG_CONFIG_PATH="${pg_pkg_config_path}"
184 elog "PostgreSQL Target: ${best_slot}"