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