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