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