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