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