*/*: Clean PYTHON_COMPAT of obsolete impls
[gentoo.git] / dev-db / postgresql / postgresql-9.5.17-r1.ebuild
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 EAPI=7
5
6 PYTHON_COMPAT=( python2_7 python3_{6,7} )
7
8 PLOCALES="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr zh_CN
9                   zh_TW"
10
11 inherit flag-o-matic l10n linux-info multilib pam prefix python-single-r1 \
12                 systemd user
13
14 KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~ppc-macos ~x86-solaris"
15
16 SLOT=$(ver_cut 1-2)
17
18 SRC_URI="https://ftp.postgresql.org/pub/source/v${PV}/postgresql-${PV}.tar.bz2"
19
20 LICENSE="POSTGRESQL GPL-2"
21 DESCRIPTION="PostgreSQL RDBMS"
22 HOMEPAGE="https://www.postgresql.org/"
23
24 IUSE="debug doc kerberos kernel_linux ldap libressl nls pam perl
25           -pg_legacytimestamp python +readline selinux +server systemd ssl
26           static-libs tcl threads uuid xml zlib"
27
28 REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
29
30 CDEPEND="
31 >=app-eselect/eselect-postgresql-2.0
32 sys-apps/less
33 virtual/libintl
34 kerberos? ( virtual/krb5 )
35 ldap? ( net-nds/openldap )
36 pam? ( sys-libs/pam )
37 perl? ( >=dev-lang/perl-5.8:= )
38 python? ( ${PYTHON_DEPS} )
39 readline? ( sys-libs/readline:0= )
40 ssl? (
41         !libressl? ( >=dev-libs/openssl-0.9.6-r1:0= )
42         libressl? ( dev-libs/libressl:= )
43 )
44 tcl? ( >=dev-lang/tcl-8:0= )
45 xml? ( dev-libs/libxml2 dev-libs/libxslt )
46 zlib? ( sys-libs/zlib )
47 "
48
49 # uuid flags -- depend on sys-apps/util-linux for Linux libcs, or if no
50 # supported libc in use depend on dev-libs/ossp-uuid. For BSD systems,
51 # the libc includes UUID functions.
52 UTIL_LINUX_LIBC=( elibc_{glibc,uclibc,musl} )
53 BSD_LIBC=( elibc_{Free,Net,Open}BSD )
54
55 nest_usedep() {
56         local front back
57         while [[ ${#} -gt 1 ]]; do
58                 front+="${1}? ( "
59                 back+=" )"
60                 shift
61         done
62         echo "${front}${1}${back}"
63 }
64
65 IUSE+=" ${UTIL_LINUX_LIBC[@]} ${BSD_LIBC[@]}"
66 CDEPEND+="
67 uuid? (
68         ${UTIL_LINUX_LIBC[@]/%/? ( sys-apps/util-linux )}
69         $(nest_usedep ${UTIL_LINUX_LIBC[@]/#/!} ${BSD_LIBC[@]/#/!} dev-libs/ossp-uuid)
70 )"
71
72 DEPEND="${CDEPEND}
73 !!<sys-apps/sandbox-2.0
74 sys-devel/bison
75 sys-devel/flex
76 nls? ( sys-devel/gettext )
77 xml? ( virtual/pkgconfig )
78 "
79
80 RDEPEND="${CDEPEND}
81 !dev-db/postgresql-docs:${SLOT}
82 !dev-db/postgresql-base:${SLOT}
83 !dev-db/postgresql-server:${SLOT}
84 selinux? ( sec-policy/selinux-postgresql )
85 "
86
87 pkg_setup() {
88         use server && CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
89
90         enewgroup postgres 70
91         enewuser postgres 70 /bin/sh /var/lib/postgresql postgres
92
93         use python && python-single-r1_pkg_setup
94 }
95
96 src_prepare() {
97         # Work around PPC{,64} compilation bug where bool is already defined
98         sed '/#ifndef __cplusplus/a #undef bool' -i src/include/c.h || die
99
100         # Set proper run directory
101         sed "s|\(PGSOCKET_DIR\s\+\)\"/tmp\"|\1\"${EPREFIX}/run/postgresql\"|" \
102                 -i src/include/pg_config_manual.h || die
103
104         # Rely on $PATH being in the proper order so that the correct
105         # install program is used for modules utilizing PGXS in both
106         # hardened and non-hardened environments. (Bug #528786)
107         sed 's/@install_bin@/install -c/' -i src/Makefile.global.in || die
108
109         use server || eapply "${FILESDIR}/${PN}-9.5.5-no-server.patch"
110
111         if use pam ; then
112                 sed -e "s/\(#define PGSQL_PAM_SERVICE \"postgresql\)/\1-${SLOT}/" \
113                         -i src/backend/libpq/auth.c || \
114                         die 'PGSQL_PAM_SERVICE rename failed.'
115         fi
116
117         eapply_user
118 }
119
120 src_configure() {
121         case ${CHOST} in
122                 *-darwin*|*-solaris*)
123                         use nls && append-libs intl
124                         ;;
125         esac
126
127         export LDFLAGS_SL="${LDFLAGS}"
128         export LDFLAGS_EX="${LDFLAGS}"
129
130         local PO="${EPREFIX%/}"
131
132         local i uuid_config=""
133         if use uuid; then
134                 for i in ${UTIL_LINUX_LIBC[@]}; do
135                         use ${i} && uuid_config="--with-uuid=e2fs"
136                 done
137                 for i in ${BSD_LIBC[@]}; do
138                         use ${i} && uuid_config="--with-uuid=bsd"
139                 done
140                 [[ -z $uuid_config ]] && uuid_config="--with-uuid=ossp"
141         fi
142
143         econf \
144                 --prefix="${PO}/usr/$(get_libdir)/postgresql-${SLOT}" \
145                 --datadir="${PO}/usr/share/postgresql-${SLOT}" \
146                 --docdir="${PO}/usr/share/doc/${PF}" \
147                 --includedir="${PO}/usr/include/postgresql-${SLOT}" \
148                 --mandir="${PO}/usr/share/postgresql-${SLOT}/man" \
149                 --sysconfdir="${PO}/etc/postgresql-${SLOT}" \
150                 --with-system-tzdata="${PO}/usr/share/zoneinfo" \
151                 $(use_enable !alpha spinlocks) \
152                 $(use_enable !pg_legacytimestamp integer-datetimes) \
153                 $(use_enable debug) \
154                 $(use_enable threads thread-safety) \
155                 $(use_with kerberos gssapi) \
156                 $(use_with ldap) \
157                 $(use_with pam) \
158                 $(use_with perl) \
159                 $(use_with python) \
160                 $(use_with readline) \
161                 $(use_with ssl openssl) \
162                 $(use_with tcl) \
163                 ${uuid_config} \
164                 $(use_with xml libxml) \
165                 $(use_with xml libxslt) \
166                 $(use_with zlib) \
167                 $(use_enable nls nls "'$(l10n_get_locales)'")
168 }
169
170 src_compile() {
171         emake
172         emake -C contrib
173 }
174
175 src_install() {
176         emake DESTDIR="${D}" install
177         emake DESTDIR="${D}" install -C contrib
178
179         dodoc README HISTORY doc/{TODO,bug.template}
180
181         # man pages are already built, but if we have the target make them,
182         # they'll be generated from source before being installed so we
183         # manually install man pages.
184         # We use ${SLOT} instead of doman for postgresql.eselect
185         insinto /usr/share/postgresql-${SLOT}/man/
186         doins -r doc/src/sgml/man{1,3,7}
187         if ! use server; then
188                 # Remove man pages for non-existent binaries
189                 for m in {initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
190                         rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
191                 done
192         fi
193         docompress /usr/share/postgresql-${SLOT}/man/man{1,3,7}
194
195         # Create slot specific man pages
196         local bn f mansec slotted_name
197         for mansec in 1 3 7 ; do
198                 local rel_manpath="../../postgresql-${SLOT}/man/man${mansec}"
199
200                 mkdir -p "${ED}"/usr/share/man/man${mansec} || die "making man dir"
201                 pushd "${ED}"/usr/share/man/man${mansec} > /dev/null || die "pushd failed"
202
203                 for f in "${ED}/usr/share/postgresql-${SLOT}/man/man${mansec}"/* ; do
204                         bn=$(basename "${f}")
205                         slotted_name=${bn%.${mansec}}${SLOT/.}.${mansec}
206                         case ${bn} in
207                                 TABLE.7|WITH.7)
208                                         echo ".so ${rel_manpath}/SELECT.7" > ${slotted_name}
209                                         ;;
210                                 *)
211                                         echo ".so ${rel_manpath}/${bn}" > ${slotted_name}
212                                         ;;
213                         esac
214                 done
215
216                 popd > /dev/null
217         done
218
219         insinto /etc/postgresql-${SLOT}
220         newins src/bin/psql/psqlrc.sample psqlrc
221
222         # Don't delete libpg{port,common}.a (Bug #571046). They're always
223         # needed by extensions utilizing PGXS.
224         use static-libs || \
225                 find "${ED}" -name '*.a' ! -name libpgport.a ! -name libpgcommon.a \
226                          -delete
227
228         local f bn
229         for f in $(find "${ED}/usr/$(get_libdir)/postgresql-${SLOT}/bin" \
230                                         -mindepth 1 -maxdepth 1)
231         do
232                 bn=$(basename "${f}")
233                 # Temporarily tack on tmp to workaround a file collision
234                 # issue. This is only necessary for 9.7 and earlier. 10 never
235                 # had this issue.
236                 dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
237                           "/usr/bin/${bn}${SLOT/.}tmp"
238         done
239
240         if use doc ; then
241                 docinto html
242                 dodoc doc/src/sgml/html/*
243
244                 docinto sgml
245                 dodoc doc/src/sgml/*.{sgml,dsl}
246         fi
247
248         if use server; then
249                 sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
250                         "${FILESDIR}/${PN}.confd-9.3" | newconfd - ${PN}-${SLOT}
251
252                 sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
253                         "${FILESDIR}/${PN}.init-9.3-r1" | newinitd - ${PN}-${SLOT}
254
255                 if use systemd; then
256                         sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
257                                 "${FILESDIR}/${PN}.service-9.2" | \
258                                 systemd_newunit - ${PN}-${SLOT}.service
259                         systemd_newtmpfilesd "${FILESDIR}"/${PN}.tmpfiles ${PN}-${SLOT}.conf
260                 fi
261
262                 newbin "${FILESDIR}"/${PN}-check-db-dir ${PN}-${SLOT}-check-db-dir
263
264                 use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account session
265
266                 if use prefix ; then
267                         keepdir /run/postgresql
268                         fperms 1775 /run/postgresql
269                 fi
270         fi
271 }
272
273 pkg_preinst() {
274         # Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
275         # /usr/bin/psql96). They may have been created by the
276         # postgresql.eselect module, but they're handled within this ebuild
277         # now. It's alright if we momentarily delete /usr/bin/psql as it
278         # will be recreated by the eselect module in pkg_ppostinst(). This
279         # is only necessary for 9.7 and earlier. 10 and later were never
280         # handled in this manner.
281         local canonicalise
282         if type -p realpath > /dev/null; then
283                 canonicalise=realpath
284         elif type -p readlink > /dev/null; then
285                 canonicalise='readlink -f'
286         else
287                 # can't die, subshell
288                 die "No readlink nor realpath found, cannot canonicalise"
289         fi
290
291         local l
292         # First remove any symlinks in /usr/bin that may have been created
293         # by the old eselect
294         for l in $(find "${ROOT%/}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; do
295                 if [[ $(${canonicalise} "${l}") == *postgresql-${SLOT}* ]] ; then
296                         rm "${l}" || ewarn "Couldn't remove ${l}"
297                 fi
298         done
299
300         # Then move the symlinks created by the ebuild to their proper place.
301         for l in "${ED}"/usr/bin/*tmp ; do
302                 mv "${l}" "${l%tmp}" \
303                         || ewarn "Couldn't rename $(basename ${l}) to $(basename ${l%tmp})"
304         done
305 }
306
307 pkg_postinst() {
308         use server && use systemd && systemd_tmpfiles_create ${PN}-${SLOT}.conf
309         postgresql-config update
310
311         if use alpha && use server ; then
312                 ewarn "PostgreSQL 9.5+ no longer has native spinlock support on Alpha platforms."
313                 ewarn "As a result, performance will be extremely degraded."
314         fi
315
316         elog "If you need a global psqlrc-file, you can place it in:"
317         elog "    ${EROOT%/}/etc/postgresql-${SLOT}/"
318
319         if use server ; then
320                 elog
321                 elog "Gentoo specific documentation:"
322                 elog "https://wiki.gentoo.org/wiki/PostgreSQL"
323                 elog
324                 elog "Official documentation:"
325                 elog "https://www.postgresql.org/docs/${SLOT}/static/index.html"
326                 elog
327                 elog "The default location of the Unix-domain socket is:"
328                 elog "    ${EROOT%/}/run/postgresql/"
329                 elog
330                 elog "Before initializing the database, you may want to edit PG_INITDB_OPTS"
331                 elog "so that it contains your preferred locale in:"
332                 elog "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
333                 elog
334                 elog "Then, execute the following command to setup the initial database"
335                 elog "environment:"
336                 elog "    emerge --config =${CATEGORY}/${PF}"
337         fi
338 }
339
340 pkg_prerm() {
341         if use server && [[ -z ${REPLACED_BY_VERSION} ]] ; then
342                 ewarn "Have you dumped and/or migrated the ${SLOT} database cluster?"
343                 ewarn "\thttps://wiki.gentoo.org/wiki/PostgreSQL/QuickStart#Migrating_PostgreSQL"
344
345                 ebegin "Resuming removal in 10 seconds (Control-C to cancel)"
346                 sleep 10
347                 eend 0
348         fi
349 }
350
351 pkg_postrm() {
352         postgresql-config update
353 }
354
355 pkg_config() {
356         use server || die "USE flag 'server' not enabled. Nothing to configure."
357
358         [[ -f "${EROOT%/}/etc/conf.d/postgresql-${SLOT}" ]] \
359                 && source "${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
360         [[ -z "${PGDATA}" ]] && PGDATA="${EROOT%/}/etc/postgresql-${SLOT}/"
361         [[ -z "${DATA_DIR}" ]] \
362                 && DATA_DIR="${EROOT%/}/var/lib/postgresql/${SLOT}/data"
363
364         # environment.bz2 may not contain the same locale as the current system
365         # locale. Unset and source from the current system locale.
366         if [ -f "${EROOT%/}/etc/env.d/02locale" ]; then
367                 unset LANG
368                 unset LC_CTYPE
369                 unset LC_NUMERIC
370                 unset LC_TIME
371                 unset LC_COLLATE
372                 unset LC_MONETARY
373                 unset LC_MESSAGES
374                 unset LC_ALL
375                 source "${EROOT%/}/etc/env.d/02locale"
376                 [ -n "${LANG}" ] && export LANG
377                 [ -n "${LC_CTYPE}" ] && export LC_CTYPE
378                 [ -n "${LC_NUMERIC}" ] && export LC_NUMERIC
379                 [ -n "${LC_TIME}" ] && export LC_TIME
380                 [ -n "${LC_COLLATE}" ] && export LC_COLLATE
381                 [ -n "${LC_MONETARY}" ] && export LC_MONETARY
382                 [ -n "${LC_MESSAGES}" ] && export LC_MESSAGES
383                 [ -n "${LC_ALL}" ] && export LC_ALL
384         fi
385
386         einfo "You can modify the paths and options passed to initdb by editing:"
387         einfo "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
388         einfo
389         einfo "Information on options that can be passed to initdb are found at:"
390         einfo "    https://www.postgresql.org/docs/${SLOT}/static/creating-cluster.html"
391         einfo "    https://www.postgresql.org/docs/${SLOT}/static/app-initdb.html"
392         einfo
393         einfo "PG_INITDB_OPTS is currently set to:"
394         if [[ -z "${PG_INITDB_OPTS}" ]] ; then
395                 einfo "    (none)"
396         else
397                 einfo "    ${PG_INITDB_OPTS}"
398         fi
399         einfo
400         einfo "Configuration files will be installed to:"
401         einfo "    ${PGDATA}"
402         einfo
403         einfo "The database cluster will be created in:"
404         einfo "    ${DATA_DIR}"
405         einfo
406
407         ebegin "Continuing initialization in 5 seconds (Control-C to cancel)"
408         sleep 5
409         eend 0
410
411         if [ -n "$(ls -A ${DATA_DIR} 2> /dev/null)" ] ; then
412                 eerror "The given directory, '${DATA_DIR}', is not empty."
413                 eerror "Modify DATA_DIR to point to an empty directory."
414                 die "${DATA_DIR} is not empty."
415         fi
416
417         einfo "Creating the data directory ..."
418         if [[ ${EUID} == 0 ]] ; then
419                 mkdir -p "${DATA_DIR}"
420                 chown -Rf postgres:postgres "${DATA_DIR}"
421                 chmod 0700 "${DATA_DIR}"
422         fi
423
424         einfo "Initializing the database ..."
425
426         if [[ ${EUID} == 0 ]] ; then
427                 su postgres -c "${EROOT%/}/usr/$(get_libdir)/postgresql-${SLOT}/bin/initdb -D \"${DATA_DIR}\" ${PG_INITDB_OPTS}"
428         else
429                 "${EROOT%/}"/usr/$(get_libdir)/postgresql-${SLOT}/bin/initdb -U postgres -D "${DATA_DIR}" ${PG_INITDB_OPTS}
430         fi
431
432         if [[ "${DATA_DIR%/}" != "${PGDATA%/}" ]] ; then
433                 mv "${DATA_DIR%/}"/{pg_{hba,ident},postgresql}.conf "${PGDATA}"
434                 ln -s "${PGDATA%/}"/{pg_{hba,ident},postgresql}.conf "${DATA_DIR%/}"
435         fi
436
437         # unix_socket_directory has no effect in postgresql.conf as it's
438         # overridden in the initscript
439         sed '/^#unix_socket_directories/,+1d' -i "${PGDATA%/}"/postgresql.conf
440
441         cat <<- EOF >> "${PGDATA%/}"/postgresql.conf
442                 # This is here because of https://bugs.gentoo.org/show_bug.cgi?id=518522
443                 # On the off-chance that you might need to work with UTF-8 encoded
444                 # characters in PL/Perl
445                 plperl.on_init = 'use utf8; use re; package utf8; require "utf8_heavy.pl";'
446         EOF
447
448         einfo "The autovacuum function, which was in contrib, has been moved to the main"
449         einfo "PostgreSQL functions starting with 8.1, and starting with 8.4 is now enabled"
450         einfo "by default. You can disable it in the cluster's:"
451         einfo "    ${PGDATA%/}/postgresql.conf"
452         einfo
453         einfo "The PostgreSQL server, by default, will log events to:"
454         einfo "    ${DATA_DIR%/}/postmaster.log"
455         einfo
456         if use prefix ; then
457                 einfo "The location of the configuration files have moved to:"
458                 einfo "    ${PGDATA}"
459                 einfo "To start the server:"
460                 einfo "    pg_ctl start -D ${DATA_DIR} -o '-D ${PGDATA} --data-directory=${DATA_DIR}'"
461                 einfo "To stop:"
462                 einfo "    pg_ctl stop -D ${DATA_DIR}"
463                 einfo
464                 einfo "Or move the configuration files back:"
465                 einfo "mv ${PGDATA}*.conf ${DATA_DIR}"
466         else
467                 einfo "You should use the '${EROOT%/}/etc/init.d/postgresql-${SLOT}' script to run PostgreSQL"
468                 einfo "instead of 'pg_ctl'."
469         fi
470 }
471
472 src_test() {
473         if use server && [[ ${UID} -ne 0 ]] ; then
474                 emake check
475
476                 einfo "If you think other tests besides the regression tests are necessary, please"
477                 einfo "submit a bug including a patch for this ebuild to enable them."
478         else
479                 use server || \
480                         ewarn 'Tests cannot be run without the "server" use flag enabled.'
481                 [[ ${UID} -eq 0 ]] || \
482                         ewarn 'Tests cannot be run as root. Enable "userpriv" in FEATURES.'
483
484                 ewarn 'Skipping.'
485         fi
486 }