sys-power/apcupsd: Fixup missing || die
[gentoo.git] / eclass / mysql-multilib.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: mysql-multilib.eclass
6 # @MAINTAINER:
7 # Maintainers:
8 #       - MySQL Team <mysql-bugs@gentoo.org>
9 #       - Robin H. Johnson <robbat2@gentoo.org>
10 #       - Jorge Manuel B. S. Vicetto <jmbsvicetto@gentoo.org>
11 #       - Brian Evans <grknight@gentoo.org>
12 # @BLURB: This eclass provides most of the functions for mysql ebuilds
13 # @DESCRIPTION:
14 # The mysql-multilib.eclass is the base eclass to build the mysql and
15 # alternative projects (mariadb and percona) ebuilds.
16 # This eclass uses the mysql-cmake eclass for the
17 # specific bits related to the build system.
18 # It provides the src_unpack, src_prepare, src_configure, src_compile,
19 # src_install, pkg_preinst, pkg_postinst, pkg_config and pkg_postrm
20 # phase hooks.
21
22 MYSQL_EXTRAS=""
23
24 # @ECLASS-VARIABLE: MYSQL_EXTRAS_VER
25 # @DESCRIPTION:
26 # The version of the MYSQL_EXTRAS repo to use to build mysql
27 # Use "none" to disable it's use
28 [[ ${MY_EXTRAS_VER} == "live" ]] && MYSQL_EXTRAS="git-r3"
29
30 inherit eutils systemd flag-o-matic ${MYSQL_EXTRAS} mysql-cmake mysql_fx versionator \
31         toolchain-funcs user cmake-utils multilib-minimal
32
33 #
34 # Supported EAPI versions and export functions
35 #
36
37 case "${EAPI:-0}" in
38         5) ;;
39         *) die "Unsupported EAPI: ${EAPI}" ;;
40 esac
41
42 EXPORT_FUNCTIONS pkg_pretend pkg_setup src_unpack src_prepare src_configure src_compile src_install pkg_preinst pkg_postinst pkg_config
43
44 #
45 # VARIABLES:
46 #
47
48 # @ECLASS-VARIABLE: MYSQL_CMAKE_NATIVE_DEFINES
49 # @DESCRIPTION:
50 # Add extra CMake arguments for native multilib builds
51
52 # @ECLASS-VARIABLE: MYSQL_CMAKE_NONNATIVE_DEFINES
53 # @DESCRIPTION:
54 # Add extra CMake arguments for non-native multilib builds
55
56 # @ECLASS-VARIABLE: MYSQL_CMAKE_EXTRA_DEFINES
57 # @DESCRIPTION:
58 # Add extra CMake arguments
59
60 # Shorten the path because the socket path length must be shorter than 107 chars
61 # and we will run a mysql server during test phase
62 S="${WORKDIR}/mysql"
63
64 [[ ${MY_EXTRAS_VER} == "latest" ]] && MY_EXTRAS_VER="20090228-0714Z"
65 if [[ ${MY_EXTRAS_VER} == "live" ]]; then
66         EGIT_REPO_URI="git://anongit.gentoo.org/proj/mysql-extras.git"
67         EGIT_CHECKOUT_DIR=${WORKDIR}/mysql-extras
68         EGIT_CLONE_TYPE=shallow
69 fi
70
71 # @ECLASS-VARIABLE: MYSQL_PV_MAJOR
72 # @DESCRIPTION:
73 # Upstream MySQL considers the first two parts of the version number to be the
74 # major version. Upgrades that change major version should always run
75 # mysql_upgrade.
76 MYSQL_PV_MAJOR="$(get_version_component_range 1-2 ${PV})"
77
78 # Cluster is a special case...
79 if [[ "${PN}" == "mysql-cluster" ]]; then
80         case $PV in
81                 7.2*) MYSQL_PV_MAJOR=5.5 ;;
82                 7.3*) MYSQL_PV_MAJOR=5.6 ;;
83         esac
84 fi
85
86 # MariaDB has left the numbering schema but keeping compatibility
87 if [[ ${PN} == "mariadb" || ${PN} == "mariadb-galera" ]]; then
88         case ${PV} in
89                 10.0*) MYSQL_PV_MAJOR="5.6" ;;
90                 10.1*) MYSQL_PV_MAJOR="5.6" ;;
91         esac
92 fi
93
94 # @ECLASS-VARIABLE: MYSQL_VERSION_ID
95 # @DESCRIPTION:
96 # MYSQL_VERSION_ID will be:
97 # major * 10e6 + minor * 10e4 + micro * 10e2 + gentoo revision number, all [0..99]
98 # This is an important part, because many of the choices the MySQL ebuild will do
99 # depend on this variable.
100 # In particular, the code below transforms a $PVR like "5.0.18-r3" in "5001803"
101 # We also strip off upstream's trailing letter that they use to respin tarballs
102 MYSQL_VERSION_ID=""
103 tpv="${PV%[a-z]}"
104 tpv=( ${tpv//[-._]/ } ) ; tpv[3]="${PVR:${#PV}}" ; tpv[3]="${tpv[3]##*-r}"
105 for vatom in 0 1 2 3 ; do
106         # pad to length 2
107         tpv[${vatom}]="00${tpv[${vatom}]}"
108         MYSQL_VERSION_ID="${MYSQL_VERSION_ID}${tpv[${vatom}]:0-2}"
109 done
110 # strip leading "0" (otherwise it's considered an octal number by BASH)
111 MYSQL_VERSION_ID=${MYSQL_VERSION_ID##"0"}
112
113 # This eclass should only be used with at least mysql-5.5.35
114 mysql_version_is_at_least "5.5.35" || die "This eclass should only be used with >=mysql-5.5.35"
115
116 # Work out the default SERVER_URI correctly
117 if [[ -z ${SERVER_URI} ]]; then
118         [[ -z ${MY_PV} ]] && MY_PV="${PV//_/-}"
119         if [[ ${PN} == "mariadb" || ${PN} == "mariadb-galera" ]]; then
120                 # Beginning with 5.5, MariaDB stopped putting beta, alpha or rc on their tarball names
121                 mysql_version_is_at_least "5.5" && MARIA_FULL_PV=$(get_version_component_range 1-3) || \
122                         MARIA_FULL_PV=$(replace_version_separator 3 '-' ${MY_PV})
123                 MARIA_FULL_P="${PN}-${MARIA_FULL_PV}"
124                 SERVER_URI="
125                 http://ftp.osuosl.org/pub/mariadb/${MARIA_FULL_P}/source/${MARIA_FULL_P}.tar.gz
126                 http://mirror.jmu.edu/pub/mariadb/${MARIA_FULL_P}/source/${MARIA_FULL_P}.tar.gz
127                 http://mirrors.coreix.net/mariadb/${MARIA_FULL_P}/source/${MARIA_FULL_P}.tar.gz
128                 http://mirrors.syringanetworks.net/mariadb/${MARIA_FULL_P}/source/${MARIA_FULL_P}.tar.gz
129                 http://mirrors.fe.up.pt/pub/mariadb/${MARIA_FULL_P}/source/${MARIA_FULL_P}.tar.gz
130                 http://mirror2.hs-esslingen.de/mariadb/${MARIA_FULL_P}/source/${MARIA_FULL_P}.tar.gz
131                 http://ftp.osuosl.org/pub/mariadb/${MARIA_FULL_P}/kvm-tarbake-jaunty-x86/${MARIA_FULL_P}.tar.gz
132                 http://mirror.jmu.edu/pub/mariadb/${MARIA_FULL_P}/kvm-tarbake-jaunty-x86/${MARIA_FULL_P}.tar.gz
133                 http://mirrors.coreix.net/mariadb/${MARIA_FULL_P}/kvm-tarbake-jaunty-x86/${MARIA_FULL_P}.tar.gz
134                 http://mirrors.syringanetworks.net/mariadb/${MARIA_FULL_P}/kvm-tarbake-jaunty-x86/${MARIA_FULL_P}.tar.gz
135                 http://mirrors.fe.up.pt/pub/mariadb/${MARIA_FULL_P}/kvm-tarbake-jaunty-x86/${MARIA_FULL_P}.tar.gz
136                 http://mirror2.hs-esslingen.de/mariadb/${MARIA_FULL_P}/kvm-tarbake-jaunty-x86/${MARIA_FULL_P}.tar.gz
137                 "
138                 if [[ ${PN} == "mariadb-galera" ]]; then
139                         MY_SOURCEDIR="${PN%%-galera}-${MARIA_FULL_PV}"
140                 fi
141         elif [[ ${PN} == "percona-server" ]]; then
142                 PERCONA_PN="Percona-Server"
143                 MIRROR_PV=$(get_version_component_range 1-2 ${PV})
144                 MY_PV=$(get_version_component_range 1-3 ${PV})
145                 PERCONA_RELEASE=$(get_version_component_range 4-5 ${PV})
146                 PERCONA_RC=$(get_version_component_range 6 ${PV})
147                 SERVER_URI="http://www.percona.com/redir/downloads/${PERCONA_PN}-${MIRROR_PV}/${PERCONA_PN}-${MY_PV}-${PERCONA_RC}${PERCONA_RELEASE}/source/tarball/${PN}-${MY_PV}-${PERCONA_RC}${PERCONA_RELEASE}.tar.gz"
148 #               http://www.percona.com/redir/downloads/Percona-Server-5.5/LATEST/source/tarball/Percona-Server-5.5.30-rel30.2.tar.gz
149 #               http://www.percona.com/redir/downloads/Percona-Server-5.6/Percona-Server-5.6.13-rc60.5/source/tarball/Percona-Server-5.6.13-rc60.5.tar.gz
150         else
151                 if [[ "${PN}" == "mysql-cluster" ]] ; then
152                         URI_DIR="MySQL-Cluster"
153                         URI_FILE="mysql-cluster-gpl"
154                 else
155                         URI_DIR="MySQL"
156                         URI_FILE="mysql"
157                 fi
158                 URI_A="${URI_FILE}-${MY_PV}.tar.gz"
159                 MIRROR_PV=$(get_version_component_range 1-2 ${PV})
160                 # Recently upstream switched to an archive site, and not on mirrors
161                 SERVER_URI="http://downloads.mysql.com/archives/${URI_FILE}-${MIRROR_PV}/${URI_A}
162                                         mirror://mysql/Downloads/${URI_DIR}-${PV%.*}/${URI_A}"
163         fi
164 fi
165
166 # Define correct SRC_URIs
167 SRC_URI="${SERVER_URI}"
168
169 # Gentoo patches to MySQL
170 if [[ ${MY_EXTRAS_VER} != "live" && ${MY_EXTRAS_VER} != "none" ]]; then
171         SRC_URI="${SRC_URI}
172                 mirror://gentoo/mysql-extras-${MY_EXTRAS_VER}.tar.bz2
173                 https://dev.gentoo.org/~robbat2/distfiles/mysql-extras-${MY_EXTRAS_VER}.tar.bz2
174                 https://dev.gentoo.org/~jmbsvicetto/distfiles/mysql-extras-${MY_EXTRAS_VER}.tar.bz2
175                 https://dev.gentoo.org/~grknight/distfiles/mysql-extras-${MY_EXTRAS_VER}.tar.bz2"
176 fi
177
178 DESCRIPTION="A fast, multi-threaded, multi-user SQL database server"
179 HOMEPAGE="http://www.mysql.com/"
180 if [[ ${PN} == "mariadb" ]]; then
181         HOMEPAGE="http://mariadb.org/"
182         DESCRIPTION="An enhanced, drop-in replacement for MySQL"
183 fi
184 if [[ ${PN} == "mariadb-galera" ]]; then
185         HOMEPAGE="http://mariadb.org/"
186         DESCRIPTION="An enhanced, drop-in replacement for MySQL with Galera Replication"
187 fi
188 if [[ ${PN} == "percona-server" ]]; then
189         HOMEPAGE="http://www.percona.com/software/percona-server"
190         DESCRIPTION="An enhanced, drop-in replacement for MySQL from the Percona team"
191 fi
192 LICENSE="GPL-2"
193 SLOT="0/${SUBSLOT:-0}"
194
195 IUSE="debug embedded extraengine jemalloc latin1 libressl +openssl
196         +perl profiling selinux systemtap static static-libs tcmalloc test yassl"
197
198 REQUIRED_USE="^^ ( yassl openssl libressl )"
199
200 ### Begin readline/libedit
201 ### If the world was perfect, we would use external libedit on both to have a similar experience
202 ### However libedit does not seem to support UTF-8 keyboard input
203
204 # This probably could be simplified, but the syntax would have to be just right
205 #if [[ ${PN} == "mariadb" || ${PN} == "mariadb-galera" ]] && \
206 #       mysql_check_version_range "5.5.37 to 10.0.13.99" ; then
207 #       IUSE="bindist ${IUSE}"
208 #elif [[ ${PN} == "mysql" || ${PN} == "percona-server" ]] && \
209 #       mysql_check_version_range "5.5.37 to 5.6.11.99" ; then
210 #       IUSE="bindist ${IUSE}"
211 #elif [[ ${PN} == "mysql-cluster" ]] && \
212 #       mysql_check_version_range "7.2 to 7.2.99.99"  ; then
213 #       IUSE="bindist ${IUSE}"
214 #fi
215
216 # Tests always fail when libressl is enabled due to hard-coded ciphers in the tests
217 RESTRICT="libressl? ( test )"
218
219 if [[ ${PN} == "mariadb" || ${PN} == "mariadb-galera" ]] ; then
220         IUSE="bindist ${IUSE}"
221         RESTRICT="${RESTRICT} !bindist? ( bindist )"
222 fi
223
224 ### End readline/libedit
225
226 if [[ ${PN} == "mariadb" || ${PN} == "mariadb-galera" ]]; then
227         IUSE="${IUSE} oqgraph pam sphinx tokudb"
228         # 5.5.33 and 10.0.5 add TokuDB. Authors strongly recommend jemalloc or perfomance suffers
229         mysql_version_is_at_least "10.0.5" && IUSE="${IUSE} odbc xml"
230         if [[ ${HAS_TOOLS_PATCH} ]] ; then
231                 REQUIRED_USE="${REQUIRED_USE} !server? ( !oqgraph !sphinx ) tokudb? ( jemalloc )"
232         else
233                 REQUIRED_USE="${REQUIRED_USE} minimal? ( !oqgraph !sphinx ) tokudb? ( jemalloc )"
234         fi
235         # MariaDB 10.1 introduces InnoDB/XtraDB compression with external libraries
236         # Choices are bzip2, lz4, lzma, lzo.  bzip2 and lzma enabled by default as they are system libraries
237         mysql_version_is_at_least "10.1.1" && IUSE="${IUSE} innodb-lz4 innodb-lzo"
238
239         # It can also compress with app-arch/snappy
240         mysql_version_is_at_least "10.1.7" && IUSE="${IUSE} innodb-snappy"
241
242         # 10.1.2 introduces a cracklib password checker
243         mysql_version_is_at_least "10.1.1" && IUSE="${IUSE} cracklib"
244 fi
245
246 if [[ -n "${WSREP_REVISION}" ]]; then
247         if [[ ${PN} == "mariadb" ]]; then
248                 IUSE="${IUSE} galera sst-rsync sst-xtrabackup"
249                 REQUIRED_USE="${REQUIRED_USE} sst-rsync? ( galera ) sst-xtrabackup? ( galera )"
250         else
251                 IUSE="${IUSE} +sst-rsync sst-xtrabackup"
252         fi
253 fi
254
255 if [[ ${PN} == "percona-server" ]]; then
256         IUSE="${IUSE} pam"
257 fi
258
259 if [[ ${HAS_TOOLS_PATCH} ]] ; then
260         IUSE="${IUSE} client-libs +server +tools"
261         REQUIRED_USE="${REQUIRED_USE} !server? ( !extraengine !embedded ) server? ( tools ) || ( client-libs server tools )"
262 else
263         IUSE="${IUSE} minimal"
264         REQUIRED_USE="${REQUIRED_USE} minimal? ( !extraengine !embedded )"
265 fi
266
267 REQUIRED_USE="
268         ${REQUIRED_USE} tcmalloc? ( !jemalloc ) jemalloc? ( !tcmalloc )
269          static? ( yassl )"
270
271 #
272 # DEPENDENCIES:
273 #
274
275 # Be warned, *DEPEND are version-dependant
276 # These are used for both runtime and compiletime
277 # MULTILIB_USEDEP only set for libraries used by the client library
278 DEPEND="
279         kernel_linux? (
280                 sys-process/procps:0=
281                 dev-libs/libaio:0=
282         )
283         >=sys-apps/sed-4
284         >=sys-apps/texinfo-4.7-r1
285         !dev-db/mariadb-native-client[mysqlcompat]
286         jemalloc? ( dev-libs/jemalloc:0= )
287         tcmalloc? ( dev-util/google-perftools:0= )
288         systemtap? ( >=dev-util/systemtap-1.3:0= )
289 "
290
291 if [[ ${HAS_TOOLS_PATCH} ]] ; then
292         DEPEND+="
293                 client-libs? (
294                         openssl? ( >=dev-libs/openssl-1.0.0:0=[${MULTILIB_USEDEP},static-libs?] )
295                         libressl? ( dev-libs/libressl:0=[${MULTILIB_USEDEP},static-libs?] )
296                         >=sys-libs/zlib-1.2.3:0=[${MULTILIB_USEDEP},static-libs?]
297                 )
298                 !client-libs? (
299                         openssl? ( >=dev-libs/openssl-1.0.0:0=[static-libs?] )
300                         libressl? ( dev-libs/libressl:0=[static-libs?] )
301                         >=sys-libs/zlib-1.2.3:0=[static-libs?]
302                 )
303                 tools? ( sys-libs/ncurses:0= ) embedded? ( sys-libs/ncurses:0= )
304         "
305 else
306         DEPEND+="
307                 openssl? ( >=dev-libs/openssl-1.0.0:0=[${MULTILIB_USEDEP},static-libs?] )
308                 libressl? ( dev-libs/libressl:0=[${MULTILIB_USEDEP},static-libs?] )
309                 >=sys-libs/zlib-1.2.3:0=[${MULTILIB_USEDEP},static-libs?]
310                 sys-libs/ncurses:0=[${MULTILIB_USEDEP}]
311         "
312 fi
313
314 ### Begin readline/libedit
315 ### If the world was perfect, we would use external libedit on both to have a similar experience
316 ### However libedit does not seem to support UTF-8 keyboard input
317
318 # dev-db/mysql-5.6.12+ only works with dev-libs/libedit
319 # mariadb 10.0.14 fixes libedit detection. changed to follow mysql
320 # This probably could be simplified
321 #if [[ ${PN} == "mysql" || ${PN} == "percona-server" ]] && \
322 #       mysql_version_is_at_least "5.6.12" ; then
323 #       DEPEND="${DEPEND} dev-libs/libedit:0=[${MULTILIB_USEDEP}]"
324 #elif [[ ${PN} == "mysql-cluster" ]] && mysql_version_is_at_least "7.3"; then
325 #       DEPEND="${DEPEND} dev-libs/libedit:0=[${MULTILIB_USEDEP}]"
326 #elif [[ ${PN} == "mariadb" || ${PN} == "mariadb-galera" ]] && \
327 #       mysql_version_is_at_least "10.0.14" ; then
328 #       DEPEND="${DEPEND} dev-libs/libedit:0=[${MULTILIB_USEDEP}]"
329 #else
330 #       DEPEND="${DEPEND} !bindist? ( >=sys-libs/readline-4.1:0=[${MULTILIB_USEDEP}] )"
331 #fi
332
333 if [[ ${PN} == "mariadb" || ${PN} == "mariadb-galera" ]] ; then
334         # Readline is only used for the command-line and embedded example
335         if [[ ${HAS_TOOLS_PATCH} ]] ; then
336                 DEPEND="${DEPEND} !bindist? ( tools? ( >=sys-libs/readline-4.1:0= ) embedded? ( >=sys-libs/readline-4.1:0= )  )"
337         else
338                 DEPEND="${DEPEND} !bindist? ( >=sys-libs/readline-4.1:0=[${MULTILIB_USEDEP}] )"
339         fi
340 fi
341
342 ### End readline/libedit
343
344 if [[ ${PN} == "mysql" || ${PN} == "percona-server" ]] ; then
345         if mysql_version_is_at_least "5.7.6" ; then DEPEND="${DEPEND} >=dev-libs/boost-1.57.0:0=" ; else
346                 mysql_version_is_at_least "5.7.5" && DEPEND="${DEPEND} >=dev-libs/boost-1.56.0:0="
347         fi
348 fi
349
350 if [[ ${PN} == "mariadb" || ${PN} == "mariadb-galera" ]] ; then
351         # Bug 441700 MariaDB >=5.3 include custom mytop
352         if [[ ${HAS_TOOLS_PATCH} ]] ; then
353                 DEPEND="${DEPEND} server? ( pam? ( virtual/pam:0= ) )"
354         else
355                 DEPEND="${DEPEND} !minimal? ( pam? ( virtual/pam:0= ) )"
356         fi
357         DEPEND="${DEPEND}
358                 oqgraph? ( >=dev-libs/boost-1.40.0:0= )
359                 perl? ( !dev-db/mytop )"
360         if mysql_version_is_at_least "10.0.5" ; then
361                 DEPEND="${DEPEND}
362                         extraengine? (
363                                 odbc? ( dev-db/unixODBC:0= )
364                                 xml? ( dev-libs/libxml2:2= )
365                         )
366                         "
367         fi
368         mysql_version_is_at_least "10.0.7" && DEPEND="${DEPEND} oqgraph? ( dev-libs/judy:0= )"
369         mysql_version_is_at_least "10.0.9" && DEPEND="${DEPEND} >=dev-libs/libpcre-8.35:3="
370
371         mysql_version_is_at_least "10.1.1" && DEPEND="${DEPEND}
372                 innodb-lz4? ( app-arch/lz4 )
373                 innodb-lzo? ( dev-libs/lzo )
374                 "
375
376         mysql_version_is_at_least "10.1.2" && DEPEND="${DEPEND} cracklib? ( sys-libs/cracklib:0= )"
377         mysql_version_is_at_least "10.1.7" && DEPEND="${DEPEND} innodb-snappy? ( app-arch/snappy )"
378 fi
379
380 if [[ ${PN} == "percona-server" ]] ; then
381         if [[ ${HAS_TOOLS_PATCH} ]] ; then
382                 DEPEND="${DEPEND} server? ( pam? ( virtual/pam:0= ) )"
383         else
384                 DEPEND="${DEPEND} !minimal? ( pam? ( virtual/pam:0= ) )"
385         fi
386 fi
387
388 # Having different flavours at the same time is not a good idea
389 for i in "mysql" "mariadb" "mariadb-galera" "percona-server" "mysql-cluster" ; do
390         [[ ${i} == ${PN} ]] ||
391         DEPEND="${DEPEND} !dev-db/${i}"
392 done
393
394 if [[ ${PN} == "mysql-cluster" ]] ; then
395         # TODO: This really should include net-misc/memcached
396         # but the package does not install the files it seeks.
397         mysql_version_is_at_least "7.2.3" && \
398                 DEPEND="${DEPEND} dev-libs/libevent:0="
399 fi
400
401 # prefix: first need to implement something for #196294
402 # TODO: check emul-linux-x86-db dep when it is multilib enabled
403 RDEPEND="${DEPEND}
404         selinux? ( sec-policy/selinux-mysql )
405         abi_x86_32? ( !app-emulation/emul-linux-x86-db[-abi_x86_32(-)] )
406 "
407
408 if [[ ${HAS_TOOLS_PATCH} ]] ; then
409         RDEPEND="${RDEPEND}
410                 server? ( !prefix? ( dev-db/mysql-init-scripts ) )
411                 !client-libs? ( virtual/libmysqlclient )
412                 !<virtual/mysql-5.6-r4"
413 else
414         RDEPEND="${RDEPEND} !minimal? ( !prefix? ( dev-db/mysql-init-scripts ) )"
415 fi
416
417 if [[ ${PN} == "mariadb" || ${PN} == "mariadb-galera" ]] ; then
418         # Bug 455016 Add dependencies of mytop
419         RDEPEND="${RDEPEND} perl? (
420                 virtual/perl-Getopt-Long
421                 dev-perl/TermReadKey
422                 virtual/perl-Term-ANSIColor
423                 virtual/perl-Time-HiRes ) "
424 fi
425
426 # @ECLASS-VARIABLE: WSREP_REVISION
427 # @DEFAULT_UNSET
428 # @DESCRIPTION:
429 # Version of the sys-cluster/galera API (major version in portage) to use for galera clustering
430
431 if [[ -n "${WSREP_REVISION}" ]] ; then
432         # The wsrep API version must match between the ebuild and sys-cluster/galera.
433         # This will be indicated by WSREP_REVISION in the ebuild and the first number
434         # in the version of sys-cluster/galera
435         #
436         # lsof is required as of 5.5.38 and 10.0.11 for the rsync sst
437
438         GALERA_RDEPEND="sys-apps/iproute2
439                 =sys-cluster/galera-${WSREP_REVISION}*
440                 "
441         if [[ ${PN} == "mariadb" ]]; then
442                 GALERA_RDEPEND="galera? ( ${GALERA_RDEPEND} )"
443         fi
444         RDEPEND="${RDEPEND} ${GALERA_RDEPEND}
445                 sst-rsync? ( sys-process/lsof )
446                 sst-xtrabackup? (
447                         net-misc/socat[ssl]
448                 )
449         "
450         # Causes a circular dependency if DBD-mysql is not already installed
451         PDEPEND="${PDEPEND} sst-xtrabackup? ( >=dev-db/xtrabackup-bin-2.2.4 )"
452 fi
453
454 if [[ ${PN} == "mysql-cluster" ]] ; then
455         mysql_version_is_at_least "7.2.9" && RDEPEND="${RDEPEND} java? ( >=virtual/jre-1.6 )" && \
456                 DEPEND="${DEPEND} java? ( >=virtual/jdk-1.6 )"
457 fi
458
459 # compile-time-only
460 # ncurses only needs multilib for compile time due to a binary that will be not installed
461 DEPEND="${DEPEND}
462         virtual/yacc
463         static? ( sys-libs/ncurses[static-libs] )
464         >=dev-util/cmake-2.8.9
465 "
466
467 # Transition dep until all ebuilds have client-libs patch and USE
468 if ! [[ ${HAS_TOOLS_PATCH} ]] ; then
469         DEPEND="${DEPEND} sys-libs/ncurses[${MULTILIB_USEDEP}]"
470 fi
471
472 # For other stuff to bring us in
473 # dev-perl/DBD-mysql is needed by some scripts installed by MySQL
474 PDEPEND="${PDEPEND} perl? ( >=dev-perl/DBD-mysql-2.9004 )
475          ~virtual/mysql-${MYSQL_PV_MAJOR}"
476
477 # my_config.h includes ABI specific data
478 MULTILIB_WRAPPED_HEADERS=( /usr/include/mysql/my_config.h /usr/include/mysql/private/embedded_priv.h )
479
480 [[ ${PN} == "mysql-cluster" ]] && \
481         MULTILIB_WRAPPED_HEADERS+=( /usr/include/mysql/storage/ndb/ndb_types.h )
482
483 [[ ${PN} == "mariadb" ]] && mysql_version_is_at_least "10.1.1" && \
484         MULTILIB_WRAPPED_HEADERS+=( /usr/include/mysql/mysql_version.h )
485
486 #
487 # HELPER FUNCTIONS:
488 #
489
490 # @FUNCTION: mysql-multilib_disable_test
491 # @DESCRIPTION:
492 # Helper function to disable specific tests.
493 mysql-multilib_disable_test() {
494         mysql-cmake_disable_test "$@"
495 }
496
497 #
498 # EBUILD FUNCTIONS
499 #
500
501 # @FUNCTION: mysql-multilib_pkg_pretend
502 # @DESCRIPTION:
503 # Perform some basic tests and tasks during pkg_pretend phase:
504 mysql-multilib_pkg_pretend() {
505         if [[ ${MERGE_TYPE} != binary ]] ; then
506                 local GCC_MAJOR_SET=$(gcc-major-version)
507                 local GCC_MINOR_SET=$(gcc-minor-version)
508                 if use_if_iuse tokudb && [[ ${GCC_MAJOR_SET} -lt 4 || \
509                         ${GCC_MAJOR_SET} -eq 4 && ${GCC_MINOR_SET} -lt 7 ]] ; then
510                         eerror "${PN} with tokudb needs to be built with gcc-4.7 or later."
511                         eerror "Please use gcc-config to switch to gcc-4.7 or later version."
512                         die
513                 fi
514                 # Bug 565584.  InnoDB now requires atomic functions introduced with gcc-4.7 on
515                 # non x86{,_64} arches
516                 if ! use amd64 && ! use x86 && [[ ${GCC_MAJOR_SET} -lt 4 || \
517                         ${GCC_MAJOR_SET} -eq 4 && ${GCC_MINOR_SET} -lt 7 ]] ; then
518                         eerror "${PN} needs to be built with gcc-4.7 or later."
519                         eerror "Please use gcc-config to switch to gcc-4.7 or later version."
520                         die
521                 fi
522         fi
523 }
524
525 # @FUNCTION: mysql-multilib_pkg_setup
526 # @DESCRIPTION:
527 # Perform some basic tests and tasks during pkg_setup phase:
528 #   die if FEATURES="test", USE="-minimal" and not using FEATURES="userpriv"
529 #   create new user and group for mysql
530 #   warn about deprecated features
531 mysql-multilib_pkg_setup() {
532
533         if has test ${FEATURES} ; then
534                 if use_if_iuse minimal ; then
535                         :
536                 elif ! in_iuse server || use_if_iuse server ; then
537                         if ! has userpriv ${FEATURES} ; then
538                                 eerror "Testing with FEATURES=-userpriv is no longer supported by upstream. Tests MUST be run as non-root."
539                         fi
540                 fi
541         fi
542
543         # This should come after all of the die statements
544         enewgroup mysql 60 || die "problem adding 'mysql' group"
545         enewuser mysql 60 -1 /dev/null mysql || die "problem adding 'mysql' user"
546
547         if [[ ${PN} == "mysql-cluster" ]] ; then
548                 mysql_version_is_at_least "7.2.9" && java-pkg-opt-2_pkg_setup
549         fi
550 }
551
552 # @FUNCTION: mysql-multilib_src_unpack
553 # @DESCRIPTION:
554 # Unpack the source code
555 mysql-multilib_src_unpack() {
556
557         # Initialize the proper variables first
558         mysql_init_vars
559
560         unpack ${A}
561         # Grab the patches
562         [[ "${MY_EXTRAS_VER}" == "live" ]] && S="${WORKDIR}/mysql-extras" git-r3_src_unpack
563
564         mv -f "${WORKDIR}/${MY_SOURCEDIR}" "${S}"
565 }
566
567 # @FUNCTION: mysql-multilib_src_prepare
568 # @DESCRIPTION:
569 # Apply patches to the source code and remove unneeded bundled libs.
570 mysql-multilib_src_prepare() {
571         mysql-cmake_src_prepare "$@"
572         if [[ ${PN} == "mysql-cluster" ]] ; then
573                 mysql_version_is_at_least "7.2.9" && java-pkg-opt-2_src_prepare
574         fi
575 }
576
577
578 # @FUNCTION: mysql-multilib_src_configure
579 # @DESCRIPTION:
580 # Configure mysql to build the code for Gentoo respecting the use flags.
581 mysql-multilib_src_configure() {
582         # Bug #114895, bug #110149
583         filter-flags "-O" "-O[01]"
584
585         CXXFLAGS="${CXXFLAGS} -fno-strict-aliasing"
586         CXXFLAGS="${CXXFLAGS} -felide-constructors"
587         # Causes linkage failures.  Upstream bug #59607 removes it
588         if ! mysql_version_is_at_least "5.6" ; then
589                 CXXFLAGS="${CXXFLAGS} -fno-implicit-templates"
590         fi
591         # As of 5.7, exceptions are used!
592         if [[ ${PN} == "percona-server" ]] && mysql_version_is_at_least "5.6.26" ; then
593                 CXXFLAGS="${CXXFLAGS} -fno-rtti"
594         elif ! mysql_version_is_at_least "5.7" ; then
595                 CXXFLAGS="${CXXFLAGS} -fno-exceptions -fno-rtti"
596         fi
597         export CXXFLAGS
598
599         # bug #283926, with GCC4.4, this is required to get correct behavior.
600         append-flags -fno-strict-aliasing
601
602         # bug 508724 mariadb cannot use ld.gold
603         if [[ ${PN} == "mariadb" || ${PN} == "mariadb-galera" ]] ; then
604                 tc-ld-disable-gold
605         fi
606
607         multilib-minimal_src_configure
608 }
609
610 multilib_src_configure() {
611
612         debug-print-function ${FUNCNAME} "$@"
613
614         CMAKE_BUILD_TYPE="RelWithDebInfo"
615
616         if ! multilib_is_native_abi && in_iuse client-libs ; then
617                 if ! use client-libs ; then
618                         einfo "Skipping multilib build due to client-libs USE disabled"
619                         return 0
620                 fi
621         fi
622
623         # debug hack wrt #497532
624         mycmakeargs=(
625                 -DCMAKE_C_FLAGS_RELWITHDEBINFO="$(usex debug "" "-DNDEBUG")"
626                 -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="$(usex debug "" "-DNDEBUG")"
627                 -DCMAKE_INSTALL_PREFIX=${EPREFIX}/usr
628                 -DMYSQL_DATADIR=${EPREFIX}/var/lib/mysql
629                 -DSYSCONFDIR=${EPREFIX}/etc/mysql
630                 -DINSTALL_BINDIR=bin
631                 -DINSTALL_DOCDIR=share/doc/${PF}
632                 -DINSTALL_DOCREADMEDIR=share/doc/${PF}
633                 -DINSTALL_INCLUDEDIR=include/mysql
634                 -DINSTALL_INFODIR=share/info
635                 -DINSTALL_LIBDIR=$(get_libdir)
636                 -DINSTALL_ELIBDIR=$(get_libdir)/mysql
637                 -DINSTALL_MANDIR=share/man
638                 -DINSTALL_MYSQLDATADIR=${EPREFIX}/var/lib/mysql
639                 -DINSTALL_MYSQLSHAREDIR=share/mysql
640                 -DINSTALL_MYSQLTESTDIR=share/mysql/mysql-test
641                 -DINSTALL_PLUGINDIR=$(get_libdir)/mysql/plugin
642                 -DINSTALL_SBINDIR=sbin
643                 -DINSTALL_SCRIPTDIR=share/mysql/scripts
644                 -DINSTALL_SQLBENCHDIR=share/mysql
645                 -DINSTALL_SUPPORTFILESDIR=${EPREFIX}/usr/share/mysql
646                 -DWITH_COMMENT="Gentoo Linux ${PF}"
647                 $(cmake-utils_use_with test UNIT_TESTS)
648                 -DWITH_LIBEDIT=0
649                 -DWITH_ZLIB=system
650                 -DWITHOUT_LIBWRAP=1
651                 -DENABLED_LOCAL_INFILE=1
652                 -DMYSQL_UNIX_ADDR=${EPREFIX}/var/run/mysqld/mysqld.sock
653                 -DINSTALL_UNIX_ADDRDIR=${EPREFIX}/var/run/mysqld/mysqld.sock
654                 -DWITH_DEFAULT_COMPILER_OPTIONS=0
655                 -DWITH_DEFAULT_FEATURE_SET=0
656                 -DINSTALL_SYSTEMD_UNITDIR="$(systemd_get_unitdir)"
657         )
658
659         if in_iuse systemd ; then
660                 mycmakeargs+=( -DWITH_SYSTEMD=$(usex systemd) )
661         fi
662
663         if use openssl || use libressl ; then
664                 mycmakeargs+=( -DWITH_SSL=system )
665         else
666                 mycmakeargs+=( -DWITH_SSL=bundled )
667         fi
668
669         if in_iuse client-libs ; then
670                 mycmakeargs+=( -DWITHOUT_CLIENTLIBS=$(usex client-libs 0 1) )
671         fi
672
673         if in_iuse tools ; then
674                 if multilib_is_native_abi ; then
675                         mycmakeargs+=( -DWITHOUT_TOOLS=$(usex tools 0 1) )
676                 else
677                         mycmakeargs+=( -DWITHOUT_TOOLS=1 )
678                 fi
679         fi
680
681         if in_iuse bindist ; then
682                 # bfd.h is only used starting with 10.1 and can be controlled by NOT_FOR_DISTRIBUTION
683                 if multilib_is_native_abi; then
684                         mycmakeargs+=(
685                                 -DWITH_READLINE=$(usex bindist 1 0)
686                                 -DNOT_FOR_DISTRIBUTION=$(usex bindist 0 1)
687                         )
688                 elif ! in_iuse client-libs ; then
689                         mycmakeargs+=(
690                                 -DWITH_READLINE=1
691                                 -DNOT_FOR_DISTRIBUTION=0
692                         )
693                 fi
694         fi
695
696         ### TODO: make this system but issues with UTF-8 prevent it
697         mycmakeargs+=( -DWITH_EDITLINE=bundled )
698
699         if [[ ${PN} == "mariadb" || ${PN} == "mariadb-galera" ]] && multilib_is_native_abi ; then
700                 mycmakeargs+=(
701                         -DWITH_JEMALLOC=$(usex jemalloc system)
702                 )
703
704                 mysql_version_is_at_least "10.0.9" && mycmakeargs+=( -DWITH_PCRE=system )
705         fi
706
707         configure_cmake_locale
708
709         if use_if_iuse minimal ; then
710                 configure_cmake_minimal
711         elif in_iuse server ; then
712                 if multilib_is_native_abi && use server ; then
713                         configure_cmake_standard
714                 else
715                         configure_cmake_minimal
716                 fi
717         else
718                 if multilib_is_native_abi ; then
719                         configure_cmake_standard
720                 else
721                         configure_cmake_minimal
722                 fi
723         fi
724
725         # systemtap only works on native ABI  bug 530132
726         if multilib_is_native_abi; then
727                 mycmakeargs+=( $(cmake-utils_use_enable systemtap DTRACE) )
728                 [[ ${MYSQL_CMAKE_NATIVE_DEFINES} ]] && mycmakeargs+=( ${MYSQL_CMAKE_NATIVE_DEFINES} )
729         else
730                 mycmakeargs+=( -DENABLE_DTRACE=0 )
731                 [[ ${MYSQL_CMAKE_NONNATIVE_DEFINES} ]] && mycmakeargs+=( ${MYSQL_CMAKE_NONNATIVE_DEFINES} )
732         fi
733
734         [[ ${MYSQL_CMAKE_EXTRA_DEFINES} ]] && mycmakeargs+=( ${MYSQL_CMAKE_EXTRA_DEFINES} )
735
736         # Always build NDB with mysql-cluster for libndbclient
737         [[ ${PN} == "mysql-cluster" ]] && mycmakeargs+=(
738                 -DWITH_NDBCLUSTER=1 -DWITH_PARTITION_STORAGE_ENGINE=1
739                 -DWITHOUT_PARTITION_STORAGE_ENGINE=0 )
740
741         cmake-utils_src_configure
742 }
743
744 mysql-multilib_src_compile() {
745         local _cmake_args=( "${@}" )
746
747         multilib-minimal_src_compile
748 }
749
750 multilib_src_compile() {
751         if ! multilib_is_native_abi && in_iuse client-libs ; then
752                 if ! use client-libs ; then
753                         return 0
754                 fi
755         fi
756
757         cmake-utils_src_compile "${_cmake_args[@]}"
758 }
759
760
761 # @FUNCTION: mysql-multilib_src_install
762 # @DESCRIPTION:
763 # Install mysql.
764 mysql-multilib_src_install() {
765         if ! in_iuse client-libs || use_if_iuse client-libs ; then
766                 # wrap the config script
767                 MULTILIB_CHOST_TOOLS=( /usr/bin/mysql_config )
768         fi
769
770         if in_iuse client-libs && ! use client-libs ; then
771                 multilib_foreach_abi multilib_src_install
772         else
773                 multilib-minimal_src_install
774         fi
775 }
776
777 multilib_src_install() {
778         debug-print-function ${FUNCNAME} "$@"
779
780         if ! multilib_is_native_abi && in_iuse client-libs ; then
781                 if ! use client-libs ; then
782                         return 0
783                 fi
784         fi
785
786         if multilib_is_native_abi; then
787                 mysql-cmake_src_install
788         else
789                 cmake-utils_src_install
790                 if [[ "${PN}" == "mariadb" || "${PN}" == "mariadb-galera" ]] ; then
791                         if use_if_iuse minimal ; then
792                                 :
793                         elif  use_if_iuse server || ! in_iuse server ; then
794                                 insinto /usr/include/mysql/private
795                                 doins "${S}"/sql/*.h
796                         fi
797                 fi
798         fi
799 }
800
801 # @FUNCTION: mysql-multilib_pkg_preinst
802 # @DESCRIPTION:
803 # Call java-pkg-opt-2 eclass when mysql-cluster is installed
804 mysql-multilib_pkg_preinst() {
805         debug-print-function ${FUNCNAME} "$@"
806
807         if [[ ${PN} == "mysql-cluster" ]] ; then
808                 mysql_version_is_at_least "7.2.9" && java-pkg-opt-2_pkg_preinst
809         fi
810         # Here we need to see if the implementation switched client libraries
811         # First, we check if this is a new instance of the package and a client library already exists
812         # Then, we check if this package is rebuilt but the previous instance did not
813         # have the client-libs USE set.
814         # Instances which do not have a client-libs USE can only be replaced by a different provider
815         local SHOW_ABI_MESSAGE
816         if ! in_iuse client-libs || use_if_iuse client-libs ; then
817                 if [[ -z ${REPLACING_VERSIONS} && -e "${EROOT}usr/$(get_libdir)/libmysqlclient.so" ]] ; then
818                         SHOW_ABI_MESSAGE=1
819                 elif [[ ${REPLACING_VERSIONS} && -e "${EROOT}usr/$(get_libdir)/libmysqlclient.so" ]] && \
820                         in_iuse client-libs && has_version "${CATEGORY}/${PN}[-client-libs(+)]" ; then
821                         SHOW_ABI_MESSAGE=1
822                 fi
823
824         fi
825         if [[ ${SHOW_ABI_MESSAGE} ]] ; then
826                 elog "Due to ABI changes when switching between different client libraries,"
827                 elog "revdep-rebuild must find and rebuild all packages linking to libmysqlclient."
828                 elog "Please run: revdep-rebuild --library libmysqlclient.so.${SUBSLOT:-18}"
829                 ewarn "Failure to run revdep-rebuild may cause issues with other programs or libraries"
830         fi
831 }
832
833 # @FUNCTION: mysql-multilib_pkg_postinst
834 # @DESCRIPTION:
835 # Run post-installation tasks:
836 #   create the dir for logfiles if non-existant
837 #   touch the logfiles and secure them
838 #   install scripts
839 #   issue required steps for optional features
840 #   issue deprecation warnings
841 mysql-multilib_pkg_postinst() {
842         debug-print-function ${FUNCNAME} "$@"
843
844         # Make sure the vars are correctly initialized
845         mysql_init_vars
846
847         # Check FEATURES="collision-protect" before removing this
848         [[ -d "${ROOT}${MY_LOGDIR}" ]] || install -d -m0750 -o mysql -g mysql "${ROOT}${MY_LOGDIR}"
849
850         # Secure the logfiles
851         touch "${ROOT}${MY_LOGDIR}"/mysql.{log,err}
852         chown mysql:mysql "${ROOT}${MY_LOGDIR}"/mysql*
853         chmod 0660 "${ROOT}${MY_LOGDIR}"/mysql*
854
855         # Minimal builds don't have the MySQL server
856         if use_if_iuse minimal ; then
857                 :
858         elif ! in_iuse server || use_if_iuse server ; then
859                 docinto "support-files"
860                 for script in \
861                         support-files/my-*.cnf \
862                         support-files/magic \
863                         support-files/ndb-config-2-node.ini
864                 do
865                         [[ -f "${script}" ]] \
866                         && dodoc "${script}"
867                 done
868
869                 docinto "scripts"
870                 for script in scripts/mysql* ; do
871                         if [[ -f "${script}" && "${script%.sh}" == "${script}" ]]; then
872                                 dodoc "${script}"
873                         fi
874                 done
875
876                 if [[ ${PN} == "mariadb" || ${PN} == "mariadb-galera" ]] ; then
877                         if use_if_iuse pam ; then
878                                 einfo
879                                 elog "This install includes the PAM authentication plugin."
880                                 elog "To activate and configure the PAM plugin, please read:"
881                                 elog "https://mariadb.com/kb/en/mariadb/pam-authentication-plugin/"
882                                 einfo
883                         fi
884                 fi
885
886                 einfo
887                 elog "You might want to run:"
888                 elog "\"emerge --config =${CATEGORY}/${PF}\""
889                 elog "if this is a new install."
890                 einfo
891
892                 einfo
893                 elog "If you are upgrading major versions, you should run the"
894                 elog "mysql_upgrade tool."
895                 einfo
896
897                 if [[ ${PN} == "mariadb-galera" ]] || use_if_iuse galera ; then
898                         einfo
899                         elog "Be sure to edit the my.cnf file to activate your cluster settings."
900                         elog "This should be done after running \"emerge --config =${CATEGORY}/${PF}\""
901                         elog "The first time the cluster is activated, you should add"
902                         elog "--wsrep-new-cluster to the options in /etc/conf.d/mysql for one node."
903                         elog "This option should then be removed for subsequent starts."
904                         einfo
905                 fi
906         fi
907 }
908
909 # @FUNCTION: mysql-multilib_getopt
910 # @DESCRIPTION:
911 # Use my_print_defaults to extract specific config options
912 mysql-multilib_getopt() {
913         local mypd="${EROOT}"/usr/bin/my_print_defaults
914         section="$1"
915         flag="--${2}="
916         "${mypd}" $section | sed -n "/^${flag}/p"
917 }
918
919 # @FUNCTION: mysql-multilib_getoptval
920 # @DESCRIPTION:
921 # Use my_print_defaults to extract specific config options
922 mysql-multilib_getoptval() {
923         local mypd="${EROOT}"/usr/bin/my_print_defaults
924         local section="$1"
925         local flag="--${2}="
926         local extra_options="${3}"
927         "${mypd}" $extra_options $section | sed -n "/^${flag}/s,${flag},,gp"
928 }
929
930 # @FUNCTION: mysql-multilib_pkg_config
931 # @DESCRIPTION:
932 # Configure mysql environment.
933 mysql-multilib_pkg_config() {
934
935         debug-print-function ${FUNCNAME} "$@"
936
937         local old_MY_DATADIR="${MY_DATADIR}"
938         local old_HOME="${HOME}"
939         # my_print_defaults needs to read stuff in $HOME/.my.cnf
940         export HOME=${EPREFIX}/root
941
942         # Make sure the vars are correctly initialized
943         mysql_init_vars
944
945         [[ -z "${MY_DATADIR}" ]] && die "Sorry, unable to find MY_DATADIR"
946         if [[ ${HAS_TOOLS_PATCH} ]] ; then
947                 if ! built_with_use ${CATEGORY}/${PN} server ; then
948                         die "Minimal builds do NOT include the MySQL server"
949                 fi
950         else
951                 if built_with_use ${CATEGORY}/${PN} minimal ; then
952                         die "Minimal builds do NOT include the MySQL server"
953                 fi
954         fi
955
956         if [[ ( -n "${MY_DATADIR}" ) && ( "${MY_DATADIR}" != "${old_MY_DATADIR}" ) ]]; then
957                 local MY_DATADIR_s="${ROOT}/${MY_DATADIR}"
958                 MY_DATADIR_s="${MY_DATADIR_s%%/}"
959                 local old_MY_DATADIR_s="${ROOT}/${old_MY_DATADIR}"
960                 old_MY_DATADIR_s="${old_MY_DATADIR_s%%/}"
961
962                 if [[ ( -d "${old_MY_DATADIR_s}" ) && ( "${old_MY_DATADIR_s}" != / ) ]]; then
963                         if [[ -d "${MY_DATADIR_s}" ]]; then
964                                 ewarn "Both ${old_MY_DATADIR_s} and ${MY_DATADIR_s} exist"
965                                 ewarn "Attempting to use ${MY_DATADIR_s} and preserving ${old_MY_DATADIR_s}"
966                         else
967                                 elog "Moving MY_DATADIR from ${old_MY_DATADIR_s} to ${MY_DATADIR_s}"
968                                 mv --strip-trailing-slashes -T "${old_MY_DATADIR_s}" "${MY_DATADIR_s}" \
969                                 || die "Moving MY_DATADIR failed"
970                         fi
971                 else
972                         ewarn "Previous MY_DATADIR (${old_MY_DATADIR_s}) does not exist"
973                         if [[ -d "${MY_DATADIR_s}" ]]; then
974                                 ewarn "Attempting to use ${MY_DATADIR_s}"
975                         else
976                                 eerror "New MY_DATADIR (${MY_DATADIR_s}) does not exist"
977                                 die "Configuration Failed! Please reinstall ${CATEGORY}/${PN}"
978                         fi
979                 fi
980         fi
981
982         local pwd1="a"
983         local pwd2="b"
984         local maxtry=15
985
986         if [ -z "${MYSQL_ROOT_PASSWORD}" ]; then
987                 MYSQL_ROOT_PASSWORD="$(mysql-multilib_getoptval 'client mysql' password)"
988                 # Sometimes --show is required to display passwords in some implementations of my_print_defaults
989                 if [[ "${MYSQL_ROOT_PASSWORD}" == '*****' ]]; then
990                         MYSQL_ROOT_PASSWORD="$(mysql-multilib_getoptval 'client mysql' password --show)"
991                 fi
992         fi
993         MYSQL_TMPDIR="$(mysql-multilib_getoptval mysqld tmpdir)"
994         # These are dir+prefix
995         MYSQL_RELAY_LOG="$(mysql-multilib_getoptval mysqld relay-log)"
996         MYSQL_RELAY_LOG=${MYSQL_RELAY_LOG%/*}
997         MYSQL_LOG_BIN="$(mysql-multilib_getoptval mysqld log-bin)"
998         MYSQL_LOG_BIN=${MYSQL_LOG_BIN%/*}
999
1000         if [[ ! -d "${ROOT}"/$MYSQL_TMPDIR ]]; then
1001                 einfo "Creating MySQL tmpdir $MYSQL_TMPDIR"
1002                 install -d -m 770 -o mysql -g mysql "${EROOT}"/$MYSQL_TMPDIR
1003         fi
1004         if [[ ! -d "${ROOT}"/$MYSQL_LOG_BIN ]]; then
1005                 einfo "Creating MySQL log-bin directory $MYSQL_LOG_BIN"
1006                 install -d -m 770 -o mysql -g mysql "${EROOT}"/$MYSQL_LOG_BIN
1007         fi
1008         if [[ ! -d "${EROOT}"/$MYSQL_RELAY_LOG ]]; then
1009                 einfo "Creating MySQL relay-log directory $MYSQL_RELAY_LOG"
1010                 install -d -m 770 -o mysql -g mysql "${EROOT}"/$MYSQL_RELAY_LOG
1011         fi
1012
1013         if [[ -d "${ROOT}/${MY_DATADIR}/mysql" ]] ; then
1014                 ewarn "You have already a MySQL database in place."
1015                 ewarn "(${ROOT}/${MY_DATADIR}/*)"
1016                 ewarn "Please rename or delete it if you wish to replace it."
1017                 die "MySQL database already exists!"
1018         fi
1019
1020         # Bug #213475 - MySQL _will_ object strenously if your machine is named
1021         # localhost. Also causes weird failures.
1022         [[ "${HOSTNAME}" == "localhost" ]] && die "Your machine must NOT be named localhost"
1023
1024         if [ -z "${MYSQL_ROOT_PASSWORD}" ]; then
1025
1026                 einfo "Please provide a password for the mysql 'root' user now"
1027                 einfo "or through the ${HOME}/.my.cnf file."
1028                 ewarn "Avoid [\"'\\_%] characters in the password"
1029                 read -rsp "    >" pwd1 ; echo
1030
1031                 einfo "Retype the password"
1032                 read -rsp "    >" pwd2 ; echo
1033
1034                 if [[ "x$pwd1" != "x$pwd2" ]] ; then
1035                         die "Passwords are not the same"
1036                 fi
1037                 MYSQL_ROOT_PASSWORD="${pwd1}"
1038                 unset pwd1 pwd2
1039         fi
1040
1041         local options
1042         local sqltmp="$(emktemp)"
1043
1044         # Fix bug 446200. Don't reference host my.cnf, needs to come first,
1045         # see http://bugs.mysql.com/bug.php?id=31312
1046         use prefix && options="${options} '--defaults-file=${MY_SYSCONFDIR}/my.cnf'"
1047
1048         local help_tables="${ROOT}${MY_SHAREDSTATEDIR}/fill_help_tables.sql"
1049         [[ -r "${help_tables}" ]] \
1050         && cp "${help_tables}" "${TMPDIR}/fill_help_tables.sql" \
1051         || touch "${TMPDIR}/fill_help_tables.sql"
1052         help_tables="${TMPDIR}/fill_help_tables.sql"
1053
1054         # Figure out which options we need to disable to do the setup
1055         local helpfile="${TMPDIR}/mysqld-help"
1056         ${EROOT}/usr/sbin/mysqld --verbose --help >"${helpfile}" 2>/dev/null
1057         for opt in grant-tables host-cache name-resolve networking slave-start \
1058                 federated ssl log-bin relay-log slow-query-log external-locking \
1059                 ndbcluster log-slave-updates \
1060                 ; do
1061                 optexp="--(skip-)?${opt}" optfull="--loose-skip-${opt}"
1062                 egrep -sq -- "${optexp}" "${helpfile}" && options="${options} ${optfull}"
1063         done
1064         # But some options changed names
1065         egrep -sq external-locking "${helpfile}" && \
1066         options="${options/skip-locking/skip-external-locking}"
1067
1068         use prefix || options="${options} --user=mysql"
1069
1070         # MySQL 5.6+ needs InnoDB
1071         if [[ ${PN} == "mysql" || ${PN} == "percona-server" ]] ; then
1072                 mysql_version_is_at_least "5.6" || options="${options} --loose-skip-innodb"
1073         fi
1074
1075         einfo "Creating the mysql database and setting proper permissions on it ..."
1076
1077         # Now that /var/run is a tmpfs mount point, we need to ensure it exists before using it
1078         PID_DIR="${EROOT}/var/run/mysqld"
1079         if [[ ! -d "${PID_DIR}" ]]; then
1080                 mkdir -p "${PID_DIR}" || die "Could not create pid directory"
1081                 chown mysql:mysql "${PID_DIR}" || die "Could not set ownership on pid directory"
1082                 chmod 755 "${PID_DIR}" || die "Could not set permissions on pid directory"
1083         fi
1084
1085         pushd "${TMPDIR}" &>/dev/null
1086         #cmd="'${EROOT}/usr/share/mysql/scripts/mysql_install_db' '--basedir=${EPREFIX}/usr' ${options}"
1087         cmd=${EROOT}usr/share/mysql/scripts/mysql_install_db
1088         [[ -f ${cmd} ]] || cmd=${EROOT}usr/bin/mysql_install_db
1089         cmd="'$cmd' '--basedir=${EPREFIX}/usr' ${options} '--datadir=${ROOT}/${MY_DATADIR}' '--tmpdir=${ROOT}/${MYSQL_TMPDIR}'"
1090         einfo "Command: $cmd"
1091         eval $cmd \
1092                 >"${TMPDIR}"/mysql_install_db.log 2>&1
1093         if [ $? -ne 0 ]; then
1094                 grep -B5 -A999 -i "ERROR" "${TMPDIR}"/mysql_install_db.log 1>&2
1095                 die "Failed to run mysql_install_db. Please review ${EPREFIX}/var/log/mysql/mysqld.err AND ${TMPDIR}/mysql_install_db.log"
1096         fi
1097         popd &>/dev/null
1098         [[ -f "${ROOT}/${MY_DATADIR}/mysql/user.frm" ]] \
1099         || die "MySQL databases not installed"
1100         chown -R mysql:mysql "${ROOT}/${MY_DATADIR}" 2>/dev/null
1101         chmod 0750 "${ROOT}/${MY_DATADIR}" 2>/dev/null
1102
1103         # Filling timezones, see
1104         # http://dev.mysql.com/doc/mysql/en/time-zone-support.html
1105         "${EROOT}/usr/bin/mysql_tzinfo_to_sql" "${EROOT}/usr/share/zoneinfo" > "${sqltmp}" 2>/dev/null
1106
1107         if [[ -r "${help_tables}" ]] ; then
1108                 cat "${help_tables}" >> "${sqltmp}"
1109         fi
1110
1111         local socket="${EROOT}/var/run/mysqld/mysqld${RANDOM}.sock"
1112         local pidfile="${EROOT}/var/run/mysqld/mysqld${RANDOM}.pid"
1113         local mysqld="${EROOT}/usr/sbin/mysqld \
1114                 ${options} \
1115                 $(use prefix || echo --user=mysql) \
1116                 --log-warnings=0 \
1117                 --basedir=${EROOT}/usr \
1118                 --datadir=${ROOT}/${MY_DATADIR} \
1119                 --max_allowed_packet=8M \
1120                 --net_buffer_length=16K \
1121                 --default-storage-engine=MyISAM \
1122                 --socket=${socket} \
1123                 --pid-file=${pidfile}
1124                 --tmpdir=${ROOT}/${MYSQL_TMPDIR}"
1125         #einfo "About to start mysqld: ${mysqld}"
1126         ebegin "Starting mysqld"
1127         einfo "Command ${mysqld}"
1128         ${mysqld} &
1129         rc=$?
1130         while ! [[ -S "${socket}" || "${maxtry}" -lt 1 ]] ; do
1131                 maxtry=$((${maxtry}-1))
1132                 echo -n "."
1133                 sleep 1
1134         done
1135         eend $rc
1136
1137         if ! [[ -S "${socket}" ]]; then
1138                 die "Completely failed to start up mysqld with: ${mysqld}"
1139         fi
1140
1141         ebegin "Setting root password"
1142         # Do this from memory, as we don't want clear text passwords in temp files
1143         local sql="UPDATE mysql.user SET Password = PASSWORD('${MYSQL_ROOT_PASSWORD}') WHERE USER='root'; FLUSH PRIVILEGES"
1144         "${EROOT}/usr/bin/mysql" \
1145                 --socket=${socket} \
1146                 -hlocalhost \
1147                 -e "${sql}"
1148         eend $?
1149
1150         ebegin "Loading \"zoneinfo\", this step may require a few seconds"
1151         "${EROOT}/usr/bin/mysql" \
1152                 --socket=${socket} \
1153                 -hlocalhost \
1154                 -uroot \
1155                 --password="${MYSQL_ROOT_PASSWORD}" \
1156                 mysql < "${sqltmp}"
1157         rc=$?
1158         eend $?
1159         [[ $rc -ne 0 ]] && ewarn "Failed to load zoneinfo!"
1160
1161         # Stop the server and cleanup
1162         einfo "Stopping the server ..."
1163         kill $(< "${pidfile}" )
1164         rm -f "${sqltmp}"
1165         wait %1
1166         einfo "Done"
1167 }