kde-apps/ksirk: x86 stable (bug #661810)
[gentoo.git] / eclass / postgres.eclass
1 # Copyright 1999-2018 Gentoo Foundation
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 # @BLURB: An eclass for PostgreSQL-related packages
12 # @DESCRIPTION:
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.
17
18
19 case ${EAPI:-0} in
20         5|6) ;;
21         *) die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}" ;;
22 esac
23
24 # @ECLASS-VARIABLE: _POSTGRES_ALL_VERSIONS
25 # @INTERNAL
26 # @DESCRIPTION:
27 # List of versions to reverse sort POSTGRES_COMPAT slots
28
29 _POSTGRES_ALL_VERSIONS=( 12 11 10 9.6 9.5 9.4 9.3 9.2 )
30
31
32
33 # @ECLASS-VARIABLE: POSTGRES_COMPAT
34 # @DEFAULT_UNSET
35 # @DESCRIPTION:
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:
39 #@CODE
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
42 #@CODE
43
44 # @ECLASS-VARIABLE: POSTGRES_DEP
45 # @DESCRIPTION:
46 # An automatically generated dependency string suitable for use in
47 # DEPEND and RDEPEND declarations.
48 POSTGRES_DEP="dev-db/postgresql:="
49
50 # @ECLASS-VARIABLE: POSTGRES_USEDEP
51 # @DEFAULT_UNSET
52 # @DESCRIPTION:
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}]"
56
57 # @ECLASS-VARIABLE: POSTGRES_REQ_USE
58 # @DEFAULT_UNSET
59 # @DESCRIPTION:
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.
64
65 # @ECLASS-VARIABLE: _POSTGRES_COMPAT
66 # @INTERNAL
67 # @DESCRIPTION:
68 # Copy of POSTGRES_COMPAT, reverse sorted
69 _POSTGRES_COMPAT=()
70
71
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} )
78         done
79
80         POSTGRES_DEP=""
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}]"
86                 POSTGRES_DEP+=" )"
87
88                 IUSE+=" postgres_targets_postgres${slot/\./_}"
89                 POSTGRES_REQ_USE+=" postgres_targets_postgres${slot/\./_}"
90         done
91         POSTGRES_REQ_USE+=" )"
92 fi
93
94
95 # @FUNCTION: postgres_check_slot
96 # @DESCRIPTION:
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.'
103         fi
104
105         # Don't die because we can't run postgresql-config during pretend.
106         [[ "$EBUILD_PHASE" = "pretend" && -z "$(which postgresql-config 2> /dev/null)" ]] \
107                 && return 0
108
109         if has $(postgresql-config show 2> /dev/null) "${POSTGRES_COMPAT[@]}"; then
110                 return 0
111         else
112                 eerror "PostgreSQL slot must be set to one of: "
113                 eerror "    ${POSTGRES_COMPAT[@]}"
114                 die "Incompatible PostgreSQL slot eselected"
115         fi
116 }
117
118 # @FUNCTION: postgres_new_user
119 # @USAGE: [user [(uid|-1) [(shell|-1) [(homedir|-1) [groups]]]]]
120 # @DESCRIPTION:
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.
124 #
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
132
133         if [[ $# -gt 0 ]] ; then
134                 if [[ "$1" = "postgres" ]] ; then
135                         ewarn "Username 'postgres' implied, skipping"
136                 else
137                         local groups=$5
138                         [[ -n "${groups}" ]] && groups+=",postgres" || groups="postgres"
139                         enewuser "$1" "${2:--1}" "${3:--1}" "${4:--1}" "${groups}"
140                 fi
141         fi
142 }
143
144 # @FUNCTION: postgres_pkg_setup
145 # @DESCRIPTION:
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} "${@}"
152
153         local compat_slot
154         local best_slot
155         for compat_slot in "${_POSTGRES_COMPAT[@]}"; do
156                 if use "postgres_targets_postgres${compat_slot/\./_}"; then
157                         best_slot="${compat_slot}"
158                         break
159                 fi
160         done
161
162         if [[ -z "${best_slot}" ]]; then
163                 local flags f
164                 for f in "${_POSTGRES_COMPAT[@]}"; do
165                         flags+=" postgres${f/./_}"
166                 done
167
168                 eerror "POSTGRES_TARGETS must contain at least one of:"
169                 eerror "    ${flags}"
170                 die "No suitable POSTGRES_TARGETS enabled."
171         fi
172
173         export PG_SLOT=${best_slot}
174         export PG_CONFIG=$(which pg_config${best_slot//./})
175
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}"
179         else
180                 export PKG_CONFIG_PATH="${pg_pkg_config_path}"
181         fi
182
183         elog "PostgreSQL Target: ${best_slot}"
184 }