1 # Copyright 1999-2018 Gentoo Foundation
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 # @BLURB: An eclass for PostgreSQL-related packages
13 # This eclass provides common utility functions that many
14 # PostgreSQL-related packages perform, such as checking that the
15 # currently selected PostgreSQL slot is within a range, adding a system
16 # user to the postgres system group, and generating dependencies.
21 *) die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}" ;;
24 # @ECLASS-VARIABLE: _POSTGRES_ALL_VERSIONS
27 # List of versions to reverse sort POSTGRES_COMPAT slots
29 _POSTGRES_ALL_VERSIONS=( 12 11 10 9.6 9.5 9.4 9.3 9.2 )
33 # @ECLASS-VARIABLE: POSTGRES_COMPAT
36 # A Bash array containing a list of compatible PostgreSQL slots as
37 # defined by the developer. If declared, must be declared before
38 # inheriting this eclass. Example:
40 #POSTGRES_COMPAT=( 9.2 9.3 9.4 9.5 9.6 10 )
41 #POSTGRES_COMPAT=( 9.{2,3} 9.{4..6} 10 ) # Same as previous
44 # @ECLASS-VARIABLE: POSTGRES_DEP
46 # An automatically generated dependency string suitable for use in
47 # DEPEND and RDEPEND declarations.
48 POSTGRES_DEP="dev-db/postgresql:="
50 # @ECLASS-VARIABLE: POSTGRES_USEDEP
53 # Add the 2-Style and/or 4-Style use dependencies without brackets to be used
54 # for POSTGRES_DEP. If declared, must be declared before inheriting this eclass.
55 declare -p POSTGRES_USEDEP &>/dev/null && POSTGRES_DEP+="[${POSTGRES_USEDEP}]"
57 # @ECLASS-VARIABLE: POSTGRES_REQ_USE
60 # An automatically generated REQUIRED_USE-compatible string built upon
61 # POSTGRES_COMPAT. REQUIRED_USE="... ${POSTGRES_REQ_USE}" is only
62 # required if the package must build against one of the PostgreSQL slots
63 # declared in POSTGRES_COMPAT.
65 # @ECLASS-VARIABLE: _POSTGRES_COMPAT
68 # Copy of POSTGRES_COMPAT, reverse sorted
72 if declare -p POSTGRES_COMPAT &> /dev/null ; then
73 # Reverse sort the given POSTGRES_COMPAT so that the most recent
74 # slot is preferred over an older slot.
75 # -- do we care if dependencies are deterministic by USE flags?
76 for i in ${_POSTGRES_ALL_VERSIONS[@]} ; do
77 has ${i} ${POSTGRES_COMPAT[@]} && _POSTGRES_COMPAT+=( ${i} )
81 POSTGRES_REQ_USE=" || ("
82 for slot in "${_POSTGRES_COMPAT[@]}" ; do
83 POSTGRES_DEP+=" postgres_targets_postgres${slot/\./_}? ( dev-db/postgresql:${slot}="
84 declare -p POSTGRES_USEDEP &>/dev/null && \
85 POSTGRES_DEP+="[${POSTGRES_USEDEP}]"
88 IUSE+=" postgres_targets_postgres${slot/\./_}"
89 POSTGRES_REQ_USE+=" postgres_targets_postgres${slot/\./_}"
91 POSTGRES_REQ_USE+=" )"
95 # @FUNCTION: postgres_check_slot
97 # Verify that the currently selected PostgreSQL slot is set to one of
98 # the slots defined in POSTGRES_COMPAT. Automatically dies unless a
99 # POSTGRES_COMPAT slot is selected. Should be called in pkg_pretend().
100 postgres_check_slot() {
101 if ! declare -p POSTGRES_COMPAT &>/dev/null; then
102 die 'POSTGRES_COMPAT not declared.'
105 # Don't die because we can't run postgresql-config during pretend.
106 [[ "$EBUILD_PHASE" = "pretend" && -z "$(which postgresql-config 2> /dev/null)" ]] \
109 if has $(postgresql-config show 2> /dev/null) "${POSTGRES_COMPAT[@]}"; then
112 eerror "PostgreSQL slot must be set to one of: "
113 eerror " ${POSTGRES_COMPAT[@]}"
114 die "Incompatible PostgreSQL slot eselected"
118 # @FUNCTION: postgres_new_user
119 # @USAGE: [user [(uid|-1) [(shell|-1) [(homedir|-1) [groups]]]]]
121 # Creates the "postgres" system group and user -- which is separate from
122 # the database user -- and, optionally, the developer defined user. There
123 # are no required parameters.
125 # When given a user to create, it'll be created with the next available
126 # uid, default shell set to /bin/false, default homedir is /dev/null,
127 # and added to the "postgres" system group. You can use "-1" to skip any
128 # parameter except user or groups.
129 postgres_new_user() {
130 enewgroup postgres 70
131 enewuser postgres 70 /bin/bash /var/lib/postgresql postgres
133 if [[ $# -gt 0 ]] ; then
134 if [[ "$1" = "postgres" ]] ; then
135 ewarn "Username 'postgres' implied, skipping"
138 [[ -n "${groups}" ]] && groups+=",postgres" || groups="postgres"
139 enewuser "$1" "${2:--1}" "${3:--1}" "${4:--1}" "${groups}"
144 # @FUNCTION: postgres_pkg_setup
146 # Initialize environment variable(s) according to the best
147 # installed version of PostgreSQL that is also in POSTGRES_COMPAT. This
148 # is required if pkg_setup() is declared in the ebuild.
149 # Exports PG_SLOT, PG_CONFIG, and PKG_CONFIG_PATH.
150 postgres_pkg_setup() {
151 debug-print-function ${FUNCNAME} "${@}"
155 for compat_slot in "${_POSTGRES_COMPAT[@]}"; do
156 if use "postgres_targets_postgres${compat_slot/\./_}"; then
157 best_slot="${compat_slot}"
162 if [[ -z "${best_slot}" ]]; then
164 for f in "${_POSTGRES_COMPAT[@]}"; do
165 flags+=" postgres${f/./_}"
168 eerror "POSTGRES_TARGETS must contain at least one of:"
170 die "No suitable POSTGRES_TARGETS enabled."
173 export PG_SLOT=${best_slot}
174 export PG_CONFIG=$(which pg_config${best_slot//./})
176 local pg_pkg_config_path="$(${PG_CONFIG} --libdir)/pkgconfig"
177 if [[ -n "${PKG_CONFIG_PATH}" ]]; then
178 export PKG_CONFIG_PATH="${pg_pkg_config_path}:${PKG_CONFIG_PATH}"
180 export PKG_CONFIG_PATH="${pg_pkg_config_path}"
183 elog "PostgreSQL Target: ${best_slot}"