dev-db/redis: bump to 6.0.3
[gentoo.git] / dev-db / mysql-cluster / mysql-cluster-7.2.34.ebuild
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 EAPI="6"
5 MY_EXTRAS_VER="20190121-0015Z"
6
7 #fails to build with ninja
8 CMAKE_MAKEFILE_GENERATOR=emake
9
10 # Keeping eutils in EAPI=6 for emktemp in pkg_config
11 inherit eutils flag-o-matic prefix toolchain-funcs java-pkg-opt-2 user cmake-utils
12
13 MY_PN="mysql-cluster-gpl"
14 SRC_URI="https://cdn.mysql.com/Downloads/MySQL-Cluster-7.2/${MY_PN}-${PV}.tar.gz
15         https://downloads.mysql.com/archives/MySQL-Cluster-7.2/${MY_PN}-${PV}.tar.gz"
16 # Gentoo patches to MySQL
17 if [[ "${MY_EXTRAS_VER}" != "live" && "${MY_EXTRAS_VER}" != "none" ]]; then
18         SRC_URI="${SRC_URI}
19                 mirror://gentoo/mysql-extras-${MY_EXTRAS_VER}.tar.bz2
20                 https://gitweb.gentoo.org/proj/mysql-extras.git/snapshot/mysql-extras-${MY_EXTRAS_VER}.tar.bz2"
21 fi
22
23 HOMEPAGE="https://mysql.com/"
24 DESCRIPTION="An enhanced, drop-in replacement for MySQL"
25 LICENSE="GPL-2"
26 SLOT="0"
27 IUSE="bindist client-libs debug extraengine java jemalloc latin1 libressl
28         +perl profiling selinux +server static static-libs systemtap tcmalloc
29         test yassl"
30
31 # Tests always fail when libressl is enabled due to hard-coded ciphers in the tests
32 RESTRICT="!bindist? ( bindist ) !test? ( test ) libressl? ( test )"
33
34 REQUIRED_USE="?? ( tcmalloc jemalloc )
35         static? ( yassl )"
36
37 KEYWORDS="~amd64 ~x86"
38
39 # Shorten the path because the socket path length must be shorter than 107 chars
40 # and we will run a mysql server during test phase
41 S="${WORKDIR}/mysql"
42
43 if [[ "${MY_EXTRAS_VER}" == "live" ]] ; then
44         MY_PATCH_DIR="${WORKDIR%/}/mysql-extras"
45         inherit git-r3
46         EGIT_REPO_URI="git://anongit.gentoo.org/proj/mysql-extras.git"
47         EGIT_CHECKOUT_DIR="${WORKDIR%/}/mysql-extras"
48         EGIT_CLONE_TYPE=shallow
49 else
50         MY_PATCH_DIR="${WORKDIR%/}/mysql-extras-${MY_EXTRAS_VER}"
51 fi
52
53 PATCHES=(
54         "${MY_PATCH_DIR}/01050_all_mysql_config_cleanup-5.5.patch"
55         "${MY_PATCH_DIR}/02040_all_embedded-library-shared-5.5.10.patch"
56         "${MY_PATCH_DIR}/20001_all_fix-minimal-build-cmake-mysql-5.5.41.patch"
57         "${MY_PATCH_DIR}/20002_all_mysql-va-list.patch"
58         "${MY_PATCH_DIR}/20006_all_cmake_elib-mysql-cluster-7.2.34.patch"
59         "${MY_PATCH_DIR}/20007_all_cmake-debug-werror-5.6.22.patch"
60         "${MY_PATCH_DIR}/20008_all_mysql-tzinfo-symlink-5.6.37.patch"
61         "${MY_PATCH_DIR}/20009_all_mysql_myodbc_symbol_fix-5.5.38.patch"
62         "${MY_PATCH_DIR}/20018_all_mysql-cluster-7.2.34-without-clientlibs-tools.patch"
63         "${MY_PATCH_DIR}/20027_all_mysql-5.5-perl5.26-includes.patch"
64         "${FILESDIR}/7.2.34-client.patch"
65 )
66
67 # Be warned, *DEPEND are version-dependant
68 # These are used for both runtime and compiletime
69 COMMON_DEPEND="
70         kernel_linux? (
71                 sys-process/procps:0=
72                 dev-libs/libaio:0=
73         )
74         dev-libs/libevent:0=
75         >=sys-apps/sed-4
76         >=sys-apps/texinfo-4.7-r1
77         jemalloc? ( dev-libs/jemalloc:0= )
78         tcmalloc? ( dev-util/google-perftools:0= )
79         systemtap? ( >=dev-util/systemtap-1.3:0= )
80         !yassl? (
81                 !libressl? ( dev-libs/openssl:0= !>=dev-libs/openssl-1.1 )
82                 libressl? ( dev-libs/libressl:0= )
83         )
84         >=sys-libs/zlib-1.2.3:0=
85         sys-libs/ncurses:0=
86         !bindist? (
87                 >=sys-libs/readline-4.1:0=
88         )
89 "
90 DEPEND="virtual/yacc
91         static? ( sys-libs/ncurses[static-libs] )
92         || ( >=sys-devel/gcc-3.4.6 >=sys-devel/gcc-apple-4.0 )
93         java? ( >=virtual/jdk-1.6 )
94         ${COMMON_DEPEND}"
95 RDEPEND="selinux? ( sec-policy/selinux-mysql )
96         !dev-db/mariadb !dev-db/mariadb-galera !dev-db/percona-server !dev-db/mysql
97         server? ( !prefix? ( dev-db/mysql-init-scripts ) )
98         ${COMMON_DEPEND}
99         java? ( >=virtual/jre-1.6 )
100         perl? ( !dev-db/mytop
101                 virtual/perl-Getopt-Long
102                 dev-perl/TermReadKey
103                 virtual/perl-Term-ANSIColor
104                 virtual/perl-Time-HiRes )
105 "
106 # For other stuff to bring us in
107 # dev-perl/DBD-mysql is needed by some scripts installed by MySQL
108 PDEPEND="perl? ( >=dev-perl/DBD-mysql-2.9004 )"
109
110 pkg_setup() {
111         if [[ ${MERGE_TYPE} != binary ]] ; then
112                 local GCC_MAJOR_SET=$(gcc-major-version)
113                 local GCC_MINOR_SET=$(gcc-minor-version)
114                 # Bug 565584.  InnoDB now requires atomic functions introduced with gcc-4.7 on
115                 # non x86{,_64} arches
116                 if ! use amd64 && ! use x86 && [[ ${GCC_MAJOR_SET} -lt 4 || \
117                         ${GCC_MAJOR_SET} -eq 4 && ${GCC_MINOR_SET} -lt 7 ]] ; then
118                         eerror "${PN} needs to be built with gcc-4.7 or later."
119                         eerror "Please use gcc-config to switch to gcc-4.7 or later version."
120                         die
121                 fi
122         fi
123         if has test ${FEATURES} && \
124                 use server && ! has userpriv ${FEATURES} ; then
125                         eerror "Testing with FEATURES=-userpriv is no longer supported by upstream. Tests MUST be run as non-root."
126         fi
127
128         # This should come after all of the die statements
129         enewgroup mysql 60 || die "problem adding 'mysql' group"
130         enewuser mysql 60 -1 /dev/null mysql || die "problem adding 'mysql' user"
131
132         java-pkg-opt-2_pkg_setup
133 }
134
135 pkg_postinst() {
136         # Make sure the vars are correctly initialized
137         mysql_init_vars
138
139         # Create log directory securely if it does not exist
140         [[ -d "${ROOT}${MY_LOGDIR}" ]] || install -d -m0750 -o mysql -g mysql "${ROOT}${MY_LOGDIR}"
141
142         if use server ; then
143                 if [[ -z "${REPLACING_VERSIONS}" ]] ; then
144                         einfo
145                         elog "You might want to run:"
146                         elog "\"emerge --config =${CATEGORY}/${PF}\""
147                         elog "if this is a new install."
148                         elog
149                         elog "If you are switching server implentations, you should run the"
150                         elog "mysql_upgrade tool."
151                         einfo
152                 else
153                         einfo
154                         elog "If you are upgrading major versions, you should run the"
155                         elog "mysql_upgrade tool."
156                         einfo
157                 fi
158
159                 einfo
160                 elog "Be sure to edit the my.cnf file to activate your cluster settings."
161                 elog "This should be done after running \"emerge --config =${CATEGORY}/${PF}\""
162                 elog "The first time the cluster is activated, you should add"
163                 elog "--wsrep-new-cluster to the options in /etc/conf.d/mysql for one node."
164                 elog "This option should then be removed for subsequent starts."
165                 einfo
166         fi
167 }
168
169 src_unpack() {
170         unpack ${A}
171         # Grab the patches
172         [[ "${MY_EXTRAS_VER}" == "live" ]] && S="${WORKDIR%/}/mysql-extras" git-r3_src_unpack
173
174         mv -f "${WORKDIR%/}/${MY_PN}-${PV}" "${S}" || die
175 }
176
177 src_prepare() {
178         _disable_engine() {
179                 echo > "${S%/}/storage/${1}/CMakeLists.txt" || die
180         }
181
182         _disable_plugin() {
183                 echo > "${S%/}/plugin/${1}/CMakeLists.txt" || die
184         }
185
186         if use tcmalloc; then
187                 echo "TARGET_LINK_LIBRARIES(mysqld tcmalloc)" >> "${S%/}/sql/CMakeLists.txt" || die
188         fi
189
190         if use jemalloc; then
191                 echo "TARGET_LINK_LIBRARIES(mysqld jemalloc)" >> "${S%/}/sql/CMakeLists.txt" || die
192         fi
193
194         # Remove the centos and rhel selinux policies to support mysqld_safe under SELinux
195         if [[ -d "${S}/support-files/SELinux" ]] ; then
196                 echo > "${S}/support-files/SELinux/CMakeLists.txt" || die
197         fi
198
199         if use libressl ; then
200                 sed -i 's/OPENSSL_MAJOR_VERSION STREQUAL "1"/OPENSSL_MAJOR_VERSION STREQUAL "2"/' \
201                         "${S}/cmake/ssl.cmake" || die
202         fi
203
204         local plugin
205         local server_plugins=( semisync )
206         local test_plugins=( audit_null daemon_example fulltext )
207         if ! use server; then # These plugins are for the server
208                 for plugin in "${server_plugins[@]}" ; do
209                         _disable_plugin "${plugin}"
210                 done
211         fi
212
213         if ! use test; then # These plugins are only used during testing
214                 for plugin in "${test_plugins[@]}" ; do
215                         _disable_plugin "${plugin}"
216                 done
217         fi
218
219         # Don't build example
220         _disable_engine example
221
222         cmake-utils_src_prepare
223         java-pkg-opt-2_src_prepare
224 }
225
226 src_configure() {
227         # Bug #114895, bug #110149
228         filter-flags "-O" "-O[01]"
229
230         append-cxxflags -felide-constructors
231
232         # bug #283926, with GCC4.4, this is required to get correct behavior.
233         append-flags -fno-strict-aliasing
234
235         CMAKE_BUILD_TYPE="RelWithDebInfo"
236
237         # debug hack wrt #497532
238         mycmakeargs=(
239                 -DCMAKE_C_FLAGS_RELWITHDEBINFO="$(usex debug '' '-DNDEBUG')"
240                 -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="$(usex debug '' '-DNDEBUG')"
241                 -DDEFAULT_SYSCONFDIR="${EPREFIX}/etc/mysql"
242                 -DINSTALL_BINDIR=bin
243                 -DINSTALL_DOCDIR=share/doc/${PF}
244                 -DINSTALL_DOCREADMEDIR=share/doc/${PF}
245                 -DINSTALL_INCLUDEDIR=include/mysql
246                 -DINSTALL_INFODIR=share/info
247                 -DINSTALL_LIBDIR=$(get_libdir)
248                 -DINSTALL_MANDIR=share/man
249                 -DINSTALL_MYSQLSHAREDIR=share/mysql
250                 -DINSTALL_PLUGINDIR=$(get_libdir)/mysql/plugin
251                 -DINSTALL_SCRIPTDIR=share/mysql/scripts
252                 -DINSTALL_MYSQLDATADIR="${EPREFIX}/var/lib/mysql"
253                 -DINSTALL_SBINDIR=sbin
254                 -DINSTALL_SUPPORTFILESDIR="${EPREFIX}/usr/share/mysql"
255                 -DCOMPILATION_COMMENT="Gentoo Linux ${PF}"
256                 -DWITH_UNIT_TESTS=$(usex test ON OFF)
257                 -DWITH_ZLIB=system
258                 -DENABLED_LOCAL_INFILE=1
259                 -DMYSQL_UNIX_ADDR="${EPREFIX}/var/run/mysqld/mysqld.sock"
260                 # The build forces this to be defined when cross-compiling.  We pass it
261                 # all the time for simplicity and to make sure it is actually correct.
262                 -DSTACK_DIRECTION=$(tc-stack-grows-down && echo -1 || echo 1)
263                 -DWITHOUT_CLIENTLIBS=YES
264                 -DWITH_READLINE=$(usex bindist 1 0)
265                 -DENABLE_DTRACE=$(usex systemtap)
266                 -DWITH_BUNDLED_LIBEVENT=OFF
267                 -DWITH_NDB_JAVA=$(usex java ON OFF)
268         )
269         if use test ; then
270                 mycmakeargs+=( -DINSTALL_MYSQLTESTDIR=share/mysql/mysql-test )
271         else
272                 mycmakeargs+=( -DINSTALL_MYSQLTESTDIR='' )
273         fi
274
275         if ! use yassl ; then
276                 mycmakeargs+=( -DWITH_SSL=system )
277         else
278                 mycmakeargs+=( -DWITH_SSL=bundled )
279         fi
280
281         if use server ; then
282
283                 # Federated must be treated special otherwise they will not be built as plugins
284                 if ! use extraengine ; then
285                         mycmakeargs+=( -DWITHOUT_FEDERATED_STORAGE_ENGINE=1 )
286                 fi
287
288                 if [[ ( -n ${MYSQL_DEFAULT_CHARSET} ) && ( -n ${MYSQL_DEFAULT_COLLATION} ) ]]; then
289                         ewarn "You are using a custom charset of ${MYSQL_DEFAULT_CHARSET}"
290                         ewarn "and a collation of ${MYSQL_DEFAULT_COLLATION}."
291                         ewarn "You MUST file bugs without these variables set."
292
293                         mycmakeargs+=(
294                                 -DDEFAULT_CHARSET=${MYSQL_DEFAULT_CHARSET}
295                                 -DDEFAULT_COLLATION=${MYSQL_DEFAULT_COLLATION}
296                         )
297
298                 elif ! use latin1 ; then
299                         mycmakeargs+=(
300                                 -DDEFAULT_CHARSET=utf8
301                                 -DDEFAULT_COLLATION=utf8_general_ci
302                         )
303                 else
304                         mycmakeargs+=(
305                                 -DDEFAULT_CHARSET=latin1
306                                 -DDEFAULT_COLLATION=latin1_swedish_ci
307                         )
308                 fi
309                 mycmakeargs+=(
310                         -DINSTALL_SQLBENCHDIR=share/mysql
311                         -DEXTRA_CHARSETS=all
312                         -DDISABLE_SHARED=$(usex static YES NO)
313                         -DWITH_EMBEDDED_SERVER=OFF
314                 )
315
316                 if use profiling ; then
317                         # Setting to OFF doesn't work: Once set, profiling options will be added
318                         # to `mysqld --help` output via sql/sys_vars.cc causing
319                         # "main.mysqld--help-notwin" test to fail
320                         mycmakeargs+=( -DENABLED_PROFILING=ON )
321                 fi
322
323                 if use static; then
324                         mycmakeargs+=( -DWITH_PIC=1 )
325                 fi
326
327                 # Storage engines
328                 mycmakeargs+=(
329                         -DWITH_ARCHIVE_STORAGE_ENGINE=1
330                         -DWITH_BLACKHOLE_STORAGE_ENGINE=1
331                         -DWITH_CSV_STORAGE_ENGINE=1
332                         -DWITH_HEAP_STORAGE_ENGINE=1
333                         -DWITH_INNOBASE_STORAGE_ENGINE=1
334                         -DWITH_MYISAMMRG_STORAGE_ENGINE=1
335                         -DWITH_MYISAM_STORAGE_ENGINE=1
336                         -DWITH_PARTITION_STORAGE_ENGINE=1
337                 )
338
339         else
340                 mycmakeargs+=(
341                         -DWITHOUT_SERVER=1
342                         -DEXTRA_CHARSETS=none
343                         -DINSTALL_SQLBENCHDIR=
344                 )
345         fi
346
347         cmake-utils_src_configure
348 }
349
350 src_compile() {
351         cmake-utils_src_compile
352 }
353
354 src_install() {
355         cmake-utils_src_install
356
357         # Remove an unnecessary, private config header which will never match between ABIs and is not meant to be used
358         if [[ -f "${ED%/}/usr/include/mysql/server/private/config.h" ]] ; then
359                 rm "${ED%/}/usr/include/mysql/server/private/config.h" || die
360         fi
361
362         # Make sure the vars are correctly initialized
363         mysql_init_vars
364
365         # Convenience links
366         einfo "Making Convenience links for mysqlcheck multi-call binary"
367         dosym "mysqlcheck" "/usr/bin/mysqlanalyze"
368         dosym "mysqlcheck" "/usr/bin/mysqlrepair"
369         dosym "mysqlcheck" "/usr/bin/mysqloptimize"
370
371         # INSTALL_LAYOUT=STANDALONE causes cmake to create a /usr/data dir
372         if [[ -d "${ED%/}/usr/data" ]] ; then
373                 rm -Rf "${ED%/}/usr/data" || die
374         fi
375
376         # Unless they explicitly specific USE=test, then do not install the
377         # testsuite. It DOES have a use to be installed, esp. when you want to do a
378         # validation of your database configuration after tuning it.
379         if ! use test ; then
380                 rm -rf "${D%/}/${MY_SHAREDSTATEDIR}/mysql-test"
381         fi
382
383         # Configuration stuff
384         einfo "Building default configuration ..."
385         insinto "${MY_SYSCONFDIR#${EPREFIX}}"
386         [[ -f "${S%/}/scripts/mysqlaccess.conf" ]] && doins "${S%/}"/scripts/mysqlaccess.conf
387         local mycnf_src="my.cnf-5.5"
388         sed -e "s!@DATADIR@!${MY_DATADIR}!g" \
389                 "${FILESDIR%/}/${mycnf_src}" \
390                 > "${TMPDIR%/}/my.cnf.ok" || die
391         use prefix && sed -i -r -e '/^user[[:space:]]*=[[:space:]]*mysql$/d' "${TMPDIR%/}/my.cnf.ok"
392         if use latin1 ; then
393                 sed -i \
394                         -e "/character-set/s|utf8|latin1|g" \
395                         "${TMPDIR%/}/my.cnf.ok" || die
396         fi
397         eprefixify "${TMPDIR%/}/my.cnf.ok"
398         newins "${TMPDIR}/my.cnf.ok" my.cnf
399
400         if use server ; then
401                 einfo "Including support files and sample configurations"
402                 docinto "support-files"
403                 local script
404                 for script in \
405                         "${S%/}"/support-files/magic \
406                         "${S%/}"/support-files/ndb-config-2-node.ini
407                 do
408                         [[ -f "$script" ]] && dodoc "${script}"
409                 done
410
411                 docinto "scripts"
412                 for script in "${S%/}"/scripts/mysql* ; do
413                         [[ ( -f "$script" ) && ( "${script%.sh}" == "${script}" ) ]] && dodoc "${script}"
414                 done
415         fi
416
417         #Remove mytop if perl is not selected
418         [[ -e "${ED%/}/usr/bin/mytop" ]] && ! use perl && rm -f "${ED%/}/usr/bin/mytop"
419 }
420
421 # Official test instructions:
422 # USE='extraengine perl server' \
423 # FEATURES='test userpriv -usersandbox' \
424 # ebuild mysql-X.X.XX.ebuild \
425 # digest clean package
426 src_test() {
427
428         _disable_test() {
429                 local rawtestname reason
430                 rawtestname="${1}" ; shift
431                 reason="${@}"
432                 ewarn "test '${rawtestname}' disabled: '${reason}'"
433                 echo ${rawtestname} : ${reason} >> "${T}/disabled.def"
434         }
435
436         local TESTDIR="${BUILD_DIR}/mysql-test"
437         local retstatus_unit
438         local retstatus_tests
439
440         if ! use server ; then
441                 einfo "Skipping server tests due to minimal build."
442                 return 0
443         fi
444
445         # Bug #213475 - MySQL _will_ object strenously if your machine is named
446         # localhost. Also causes weird failures.
447         [[ "${HOSTNAME}" == "localhost" ]] && die "Your machine must NOT be named localhost"
448
449         if [[ $UID -eq 0 ]]; then
450                 die "Testing with FEATURES=-userpriv is no longer supported by upstream. Tests MUST be run as non-root."
451         fi
452         has usersandbox $FEATURES && ewarn "Some tests may fail with FEATURES=usersandbox"
453
454         einfo ">>> Test phase [test]: ${CATEGORY}/${PF}"
455
456         # Run CTest (test-units)
457         cmake-utils_src_test
458         retstatus_unit=$?
459
460         # Ensure that parallel runs don't die
461         export MTR_BUILD_THREAD="$((${RANDOM} % 100))"
462         # Enable parallel testing, auto will try to detect number of cores
463         # You may set this by hand.
464         # The default maximum is 8 unless MTR_MAX_PARALLEL is increased
465         export MTR_PARALLEL="${MTR_PARALLEL:-auto}"
466
467         # create directories because mysqladmin might run out of order
468         mkdir -p "${T}"/var-tests{,/log} || die
469
470         # Run mysql tests
471         pushd "${TESTDIR}" > /dev/null || die
472
473         touch "${T}/disabled.def"
474         # These are failing in MySQL 5.5 for now and are believed to be
475         # false positives:
476         #
477         # main.mysql_client_test, main.mysql_client_test_nonblock
478         # main.mysql_client_test_comp:
479         # segfaults at random under Portage only, suspect resource limits.
480
481         local t
482         for t in main.mysql_client_test \
483                                 binlog.binlog_statement_insert_delayed main.information_schema \
484                                 main.mysqld--help-notwin main.flush_read_lock_kill \
485                                 sys_vars.plugin_dir_basic main.openssl_1 \
486                                 main.mysqlhotcopy_archive main.mysqlhotcopy_myisam \
487                                 ndb.ndbinfo ndb_binlog.ndb_binlog_index ; do
488                         _disable_test  "$t" "False positives in Gentoo"
489         done
490
491         _disable_test main.mysqldump "Extra expected warning not recorded in test results"
492
493         if ! use client-libs ; then
494                 _disable_test main.plugin_auth "Needs client libraries built"
495         fi
496
497         # run mysql-test tests
498         perl mysql-test-run.pl --force --vardir="${T}/var-tests" --reorder --skip-test=tokudb --skip-test-list="${T}/disabled.def"
499         retstatus_tests=$?
500
501         popd > /dev/null || die
502
503         # Cleanup is important for these testcases.
504         pkill -9 -f "${S}/ndb" 2>/dev/null
505         pkill -9 -f "${S}/sql" 2>/dev/null
506
507         local failures=""
508         [[ $retstatus_unit -eq 0 ]] || failures="${failures} test-unit"
509         [[ $retstatus_tests -eq 0 ]] || failures="${failures} tests"
510
511         [[ -z "$failures" ]] || die "Test failures: $failures"
512         einfo "Tests successfully completed"
513 }
514
515 mysql_init_vars() {
516         MY_SHAREDSTATEDIR=${MY_SHAREDSTATEDIR="${EPREFIX}/usr/share/mysql"}
517         MY_SYSCONFDIR=${MY_SYSCONFDIR="${EPREFIX}/etc/mysql"}
518         MY_LOCALSTATEDIR=${MY_LOCALSTATEDIR="${EPREFIX}/var/lib/mysql"}
519         MY_LOGDIR=${MY_LOGDIR="${EPREFIX}/var/log/mysql"}
520
521         if [[ -z "${MY_DATADIR}" ]] ; then
522                 MY_DATADIR=""
523                 if [[ -f "${MY_SYSCONFDIR}/my.cnf" ]] ; then
524                         MY_DATADIR=`"my_print_defaults" mysqld 2>/dev/null \
525                                 | sed -ne '/datadir/s|^--datadir=||p' \
526                                 | tail -n1`
527                         if [[ -z "${MY_DATADIR}" ]] ; then
528                                 MY_DATADIR=`grep ^datadir "${MY_SYSCONFDIR}/my.cnf" \
529                                 | sed -e 's/.*=\s*//' \
530                                 | tail -n1`
531                         fi
532                 fi
533                 if [[ -z "${MY_DATADIR}" ]] ; then
534                         MY_DATADIR="${MY_LOCALSTATEDIR}"
535                         einfo "Using default MY_DATADIR"
536                 fi
537                 elog "MySQL MY_DATADIR is ${MY_DATADIR}"
538
539                 if [[ -z "${PREVIOUS_DATADIR}" ]] ; then
540                         if [[ -e "${MY_DATADIR}" ]] ; then
541                                 # If you get this and you're wondering about it, see bug #207636
542                                 elog "MySQL datadir found in ${MY_DATADIR}"
543                                 elog "A new one will not be created."
544                                 PREVIOUS_DATADIR="yes"
545                         else
546                                 PREVIOUS_DATADIR="no"
547                         fi
548                         export PREVIOUS_DATADIR
549                 fi
550         else
551                 if [[ ${EBUILD_PHASE} == "config" ]]; then
552                         local new_MY_DATADIR
553                         new_MY_DATADIR=`"my_print_defaults" mysqld 2>/dev/null \
554                                 | sed -ne '/datadir/s|^--datadir=||p' \
555                                 | tail -n1`
556
557                         if [[ ( -n "${new_MY_DATADIR}" ) && ( "${new_MY_DATADIR}" != "${MY_DATADIR}" ) ]]; then
558                                 ewarn "MySQL MY_DATADIR has changed"
559                                 ewarn "from ${MY_DATADIR}"
560                                 ewarn "to ${new_MY_DATADIR}"
561                                 MY_DATADIR="${new_MY_DATADIR}"
562                         fi
563                 fi
564         fi
565
566         export MY_SHAREDSTATEDIR MY_SYSCONFDIR
567         export MY_LOCALSTATEDIR MY_LOGDIR
568         export MY_DATADIR
569 }
570
571 pkg_config() {
572         _getoptval() {
573                 local mypd="${EROOT}"/usr/bin/my_print_defaults
574                 local section="$1"
575                 local flag="--${2}="
576                 local extra_options="${3}"
577                 "${mypd}" $extra_options $section | sed -n "/^${flag}/s,${flag},,gp"
578         }
579         local old_MY_DATADIR="${MY_DATADIR}"
580         local old_HOME="${HOME}"
581         # my_print_defaults needs to read stuff in $HOME/.my.cnf
582         export HOME=${EPREFIX}/root
583
584         # Make sure the vars are correctly initialized
585         mysql_init_vars
586
587         [[ -z "${MY_DATADIR}" ]] && die "Sorry, unable to find MY_DATADIR"
588         if [[ ! -x "${EROOT}/usr/sbin/mysqld" ]] ; then
589                 die "Minimal builds do NOT include the MySQL server"
590         fi
591
592         if [[ ( -n "${MY_DATADIR}" ) && ( "${MY_DATADIR}" != "${old_MY_DATADIR}" ) ]]; then
593                 local MY_DATADIR_s="${ROOT}/${MY_DATADIR}"
594                 MY_DATADIR_s="${MY_DATADIR_s%%/}"
595                 local old_MY_DATADIR_s="${ROOT}/${old_MY_DATADIR}"
596                 old_MY_DATADIR_s="${old_MY_DATADIR_s%%/}"
597
598                 if [[ ( -d "${old_MY_DATADIR_s}" ) && ( "${old_MY_DATADIR_s}" != / ) ]]; then
599                         if [[ -d "${MY_DATADIR_s}" ]]; then
600                                 ewarn "Both ${old_MY_DATADIR_s} and ${MY_DATADIR_s} exist"
601                                 ewarn "Attempting to use ${MY_DATADIR_s} and preserving ${old_MY_DATADIR_s}"
602                         else
603                                 elog "Moving MY_DATADIR from ${old_MY_DATADIR_s} to ${MY_DATADIR_s}"
604                                 mv --strip-trailing-slashes -T "${old_MY_DATADIR_s}" "${MY_DATADIR_s}" \
605                                 || die "Moving MY_DATADIR failed"
606                         fi
607                 else
608                         ewarn "Previous MY_DATADIR (${old_MY_DATADIR_s}) does not exist"
609                         if [[ -d "${MY_DATADIR_s}" ]]; then
610                                 ewarn "Attempting to use ${MY_DATADIR_s}"
611                         else
612                                 eerror "New MY_DATADIR (${MY_DATADIR_s}) does not exist"
613                                 die "Configuration Failed! Please reinstall ${CATEGORY}/${PN}"
614                         fi
615                 fi
616         fi
617
618         local pwd1="a"
619         local pwd2="b"
620         local maxtry=15
621
622         if [ -z "${MYSQL_ROOT_PASSWORD}" ]; then
623                 local tmp_mysqld_password_source=
624
625                 for tmp_mysqld_password_source in mysql client; do
626                         einfo "Trying to get password for mysql 'root' user from '${tmp_mysqld_password_source}' section ..."
627                         MYSQL_ROOT_PASSWORD="$(_getoptval "${tmp_mysqld_password_source}" password)"
628                         if [[ -n "${MYSQL_ROOT_PASSWORD}" ]]; then
629                                 if [[ ${MYSQL_ROOT_PASSWORD} == *$'\n'* ]]; then
630                                         ewarn "Ignoring password from '${tmp_mysqld_password_source}' section due to newline character (do you have multiple password options set?)!"
631                                         MYSQL_ROOT_PASSWORD=
632                                         continue
633                                 fi
634
635                                 einfo "Found password in '${tmp_mysqld_password_source}' section!"
636                                 break
637                         fi
638                 done
639
640                 # Sometimes --show is required to display passwords in some implementations of my_print_defaults
641                 if [[ "${MYSQL_ROOT_PASSWORD}" == '*****' ]]; then
642                         MYSQL_ROOT_PASSWORD="$(_getoptval "${tmp_mysqld_password_source}" password --show)"
643                 fi
644
645                 unset tmp_mysqld_password_source
646         fi
647         MYSQL_TMPDIR="$(_getoptval mysqld tmpdir)"
648         # These are dir+prefix
649         MYSQL_RELAY_LOG="$(_getoptval mysqld relay-log)"
650         MYSQL_RELAY_LOG=${MYSQL_RELAY_LOG%/*}
651         MYSQL_LOG_BIN="$(_getoptval mysqld log-bin)"
652         MYSQL_LOG_BIN=${MYSQL_LOG_BIN%/*}
653
654         if [[ ! -d "${ROOT}/$MYSQL_TMPDIR" ]]; then
655                 einfo "Creating MySQL tmpdir $MYSQL_TMPDIR"
656                 install -d -m 770 -o mysql -g mysql "${EROOT}/$MYSQL_TMPDIR"
657         fi
658         if [[ ! -d "${ROOT}/$MYSQL_LOG_BIN" ]]; then
659                 einfo "Creating MySQL log-bin directory $MYSQL_LOG_BIN"
660                 install -d -m 770 -o mysql -g mysql "${EROOT}/$MYSQL_LOG_BIN"
661         fi
662         if [[ ! -d "${EROOT}/$MYSQL_RELAY_LOG" ]]; then
663                 einfo "Creating MySQL relay-log directory $MYSQL_RELAY_LOG"
664                 install -d -m 770 -o mysql -g mysql "${EROOT}/$MYSQL_RELAY_LOG"
665         fi
666
667         if [[ -d "${ROOT}/${MY_DATADIR}/mysql" ]] ; then
668                 ewarn "You have already a MySQL database in place."
669                 ewarn "(${ROOT}/${MY_DATADIR}/*)"
670                 ewarn "Please rename or delete it if you wish to replace it."
671                 die "MySQL database already exists!"
672         fi
673
674         # Bug #213475 - MySQL _will_ object strenously if your machine is named
675         # localhost. Also causes weird failures.
676         [[ "${HOSTNAME}" == "localhost" ]] && die "Your machine must NOT be named localhost"
677
678         if [ -z "${MYSQL_ROOT_PASSWORD}" ]; then
679
680                 einfo "Please provide a password for the mysql 'root' user now"
681                 einfo "or through the ${HOME}/.my.cnf file."
682                 ewarn "Avoid [\"'\\_%] characters in the password"
683                 read -rsp "    >" pwd1 ; echo
684
685                 einfo "Retype the password"
686                 read -rsp "    >" pwd2 ; echo
687
688                 if [[ "x$pwd1" != "x$pwd2" ]] ; then
689                         die "Passwords are not the same"
690                 fi
691                 MYSQL_ROOT_PASSWORD="${pwd1}"
692                 unset pwd1 pwd2
693         fi
694
695         local options
696         local sqltmp="$(emktemp)"
697
698         # Fix bug 446200. Don't reference host my.cnf, needs to come first,
699         # see http://bugs.mysql.com/bug.php?id=31312
700         use prefix && options="${options} '--defaults-file=${MY_SYSCONFDIR}/my.cnf'"
701
702         # Figure out which options we need to disable to do the setup
703         local helpfile="${TMPDIR}/mysqld-help"
704         "${EROOT}/usr/sbin/mysqld" --verbose --help >"${helpfile}" 2>/dev/null
705         for opt in grant-tables host-cache name-resolve networking slave-start \
706                 federated ssl log-bin relay-log slow-query-log external-locking \
707                 log-slave-updates \
708                 ; do
709                 optexp="--(skip-)?${opt}" optfull="--loose-skip-${opt}"
710                 egrep -sq -- "${optexp}" "${helpfile}" && options="${options} ${optfull}"
711         done
712
713         einfo "Creating the mysql database and setting proper permissions on it ..."
714
715         # Now that /var/run is a tmpfs mount point, we need to ensure it exists before using it
716         PID_DIR="${EROOT}/var/run/mysqld"
717         if [[ ! -d "${PID_DIR}" ]]; then
718                 install -d -m 755 -o mysql -g mysql "${PID_DIR}" || die "Could not create pid directory"
719         fi
720
721         if [[ ! -d "${MY_DATADIR}" ]]; then
722                 install -d -m 750 -o mysql -g mysql "${MY_DATADIR}" || die "Could not create data directory"
723         fi
724
725         pushd "${TMPDIR}" &>/dev/null || die
726
727         # Filling timezones, see
728         # http://dev.mysql.com/doc/mysql/en/time-zone-support.html
729         "${EROOT}/usr/bin/mysql_tzinfo_to_sql" "${EROOT}/usr/share/zoneinfo" > "${sqltmp}" 2>/dev/null
730
731         local cmd=( "${EROOT}usr/share/mysql/scripts/mysql_install_db" )
732         [[ -f "${cmd}" ]] || cmd=( "${EROOT}usr/bin/mysql_install_db" )
733         cmd+=( "--basedir=${EPREFIX}/usr" ${options} "--datadir=${ROOT}/${MY_DATADIR}" "--tmpdir=${ROOT}/${MYSQL_TMPDIR}" )
734         einfo "Command: ${cmd[*]}"
735         su -s /bin/sh -c "${cmd[*]}" mysql \
736                 >"${TMPDIR}"/mysql_install_db.log 2>&1
737         if [ $? -ne 0 ]; then
738                 grep -B5 -A999 -i "ERROR" "${TMPDIR}"/mysql_install_db.log 1>&2
739                 die "Failed to initialize mysqld. Please review ${EPREFIX}/var/log/mysql/mysqld.err AND ${TMPDIR}/mysql_install_db.log"
740         fi
741         popd &>/dev/null || die
742         [[ -f "${ROOT}/${MY_DATADIR}/mysql/user.frm" ]] \
743         || die "MySQL databases not installed"
744
745         use prefix || options="${options} --user=mysql"
746
747         local socket="${EROOT}/var/run/mysqld/mysqld${RANDOM}.sock"
748         local pidfile="${EROOT}/var/run/mysqld/mysqld${RANDOM}.pid"
749         local mysqld="${EROOT}/usr/sbin/mysqld \
750                 ${options} \
751                 --log-warnings=0 \
752                 --basedir=${EROOT}/usr \
753                 --datadir=${ROOT}/${MY_DATADIR} \
754                 --max_allowed_packet=8M \
755                 --net_buffer_length=16K \
756                 --socket=${socket} \
757                 --pid-file=${pidfile} \
758                 --tmpdir=${ROOT}/${MYSQL_TMPDIR}"
759         #einfo "About to start mysqld: ${mysqld}"
760         ebegin "Starting mysqld"
761         einfo "Command ${mysqld}"
762         ${mysqld} &
763         rc=$?
764         while ! [[ -S "${socket}" || "${maxtry}" -lt 1 ]] ; do
765                 maxtry=$((${maxtry}-1))
766                 echo -n "."
767                 sleep 1
768         done
769         eend $rc
770
771         if ! [[ -S "${socket}" ]]; then
772                 die "Completely failed to start up mysqld with: ${mysqld}"
773         fi
774
775         ebegin "Setting root password"
776         # Do this from memory, as we don't want clear text passwords in temp files
777         local sql="UPDATE mysql.user SET Password = PASSWORD('${MYSQL_ROOT_PASSWORD}') WHERE USER='root'; FLUSH PRIVILEGES"
778         "${EROOT}/usr/bin/mysql" \
779                 "--socket=${socket}" \
780                 -hlocalhost \
781                 -e "${sql}"
782         eend $?
783
784         if [[ -n "${sqltmp}" ]] ; then
785                 ebegin "Loading \"zoneinfo\", this step may require a few seconds"
786                 "${EROOT}/usr/bin/mysql" \
787                         "--socket=${socket}" \
788                         -hlocalhost \
789                         -uroot \
790                         --password="${MYSQL_ROOT_PASSWORD}" \
791                         mysql < "${sqltmp}"
792                 rc=$?
793                 eend $?
794                 [[ $rc -ne 0 ]] && ewarn "Failed to load zoneinfo!"
795         fi
796
797         # Stop the server and cleanup
798         einfo "Stopping the server ..."
799         kill $(< "${pidfile}" )
800         rm -f "${sqltmp}"
801         wait %1
802         einfo "Done"
803 }