kde-plasma/breeze-gtk: x86 stable wrt bug #613144
[gentoo.git] / eclass / mysql_fx.eclass
1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # Author: Francesco Riosa (Retired) <vivo@gentoo.org>
5 # Maintainer:
6 #       - MySQL Team <mysql-bugs@gentoo.org>
7 #       - Luca Longinotti <chtekk@gentoo.org>
8
9 inherit multilib
10
11 #
12 # Helper function, version (integer) may have sections separated by dots
13 # for readability.
14 #
15 stripdots() {
16         local dotver=${1:-"0"}
17         local v=""
18         local ret=0
19         if [[ "${dotver/./}" != "${dotver}" ]] ; then
20                 # dotted version number
21                 for i in 1000000 10000 100 1 ; do
22                         v=${dotver%%\.*}
23                         # remove leading zeroes
24                         while [[ ${#v} -gt 1 ]] && [[ ${v:0:1} == "0" ]] ; do v=${v#0} ; done
25                         # increment integer version number
26                         ret=$(( ${v} * ${i} + ${ret} ))
27                         if [[ "${dotver}" == "${dotver/\.}" ]] ; then
28                                 dotver=0
29                         else
30                                 dotver=${dotver#*\.}
31                         fi
32                 done
33                 echo "${ret}"
34         else
35                 # already an integer
36                 v=${dotver}
37                 while [[ ${#v} -gt 1 ]] && [[ ${v:0:1} == "0" ]] ; do v=${v#0} ; done
38                 echo "${v}"
39         fi
40 }
41
42 #
43 # Check if a version number falls inside a given range.
44 # The range includes the extremes and must be specified as
45 # "low_version to high_version" i.e. "4.1.2 to 5.1.99.99".
46 # Returns true if inside the range.
47 #
48 mysql_check_version_range() {
49         local lbound="${1%% to *}" ; lbound=$(stripdots "${lbound}")
50         local rbound="${1#* to }"  ; rbound=$(stripdots "${rbound}")
51         local my_ver="${2:-"${MYSQL_VERSION_ID}"}"
52         [[ ${lbound} -le ${my_ver} ]] && [[ ${my_ver} -le ${rbound} ]] && return 0
53         return 1
54 }
55
56 #
57 # True if at least one applicable range is found for the patch.
58 #
59 _mysql_test_patch_ver_pn() {
60         local allelements=", version, package name"
61         # So that it fails the directory test if none of them exist
62         local filesdir="/dev/null"
63         for d in "${WORKDIR}/mysql-extras-${MY_EXTRAS_VER}" \
64                 "${WORKDIR}/mysql-extras" ; do
65                 if [ -d "${d}" ]; then
66                         filesdir="${d}"
67                         break
68                 fi
69         done
70
71         [[ -d "${filesdir}" ]] || die "Source dir must be a directory"
72         local flags=$1 pname=$2
73         if [[ $(( $flags & $(( 1 + 4 + 16 )) )) -eq 21 ]] ; then
74                 einfo "using '${pname}'"
75                 ln -sf "${filesdir}/${pname}" "${EPATCH_SOURCE}" || die "Couldn't move ${pname}"
76                 return 0
77         fi
78
79         [[ $(( $flags & $(( 2 + 4 )) )) -gt 0 ]] \
80         && allelements="${allelements//", version"}"
81
82         [[ $(( $flags & $(( 8 + 16 )) )) -gt 0 ]] \
83         && allelements="${allelements//", package name"}"
84
85         [[ -n "${allelements}" ]] && [[ "${flags}" -gt 0 ]] \
86         && ewarn "QA notice: ${allelements} missing in ${pname} patch"
87
88         return 1
89 }
90
91 #
92 # Parse a "index_file" looking for patches to apply to the
93 # current MySQL version.
94 # If the patch applies, print its description.
95 #
96 mysql_mv_patches() {
97         # So that it fails the directory test if none of them exist
98         local filesdir="/dev/null"
99         if [[ -z "${1}" ]]; then
100                 for d in "${WORKDIR}/mysql-extras-${MY_EXTRAS_VER}" \
101                         "${WORKDIR}/mysql-extras" ; do
102                         if [ -d "${d}" ]; then
103                                 filesdir="${d}"
104                                 break
105                         fi
106                 done
107                 [[ -d "${filesdir}" ]] || die "No patches directory found!"
108         fi
109
110         for i in "$1" "${filesdir}/0000_index.txt" "${filesdir}/000_index.txt" ; do
111                 if [ -n "$i" -a -f "$i" ]; then
112                         local index_file="$i"
113                         break
114                 fi
115         done
116
117         local my_ver="${2:-"${MYSQL_VERSION_ID}"}"
118         local my_test_fx=${3:-"_mysql_test_patch_ver_pn"}
119         _mysql_mv_patches "${index_file}" "${my_ver}" "${my_test_fx}"
120 }
121
122 _mysql_mv_patches() {
123         local index_file="${1}"
124         local my_ver="${2}"
125         local my_test_fx="${3}"
126         local dsc ndsc=0 i
127         dsc=( )
128
129         # Values for flags are (2^x):
130         #  1 - one patch found
131         #  2 - at least one version range is wrong
132         #  4 - at least one version range is ok
133         #  8 - at least one ${PN} did not match
134         #  16 - at least one ${PN} has been matched
135         local flags=0 pname=""
136         while read row ; do
137                 case "${row}" in
138                         @patch\ *)
139                                 [[ -n "${pname}" ]] \
140                                 && ${my_test_fx} ${flags} "${pname}" \
141                                 && for (( i=0 ; $i < $ndsc ; i++ )) ; do einfo ">    ${dsc[$i]}" ; done
142                                 flags=1 ; ndsc=0 ; dsc=( )
143                                 pname=${row#"@patch "}
144                                 ;;
145                         @ver\ *)
146                                 if mysql_check_version_range "${row#"@ver "}" "${my_ver}" ; then
147                                         flags=$(( ${flags} | 4 ))
148                                 else
149                                         flags=$(( ${flags} | 2 ))
150                                 fi
151                                 ;;
152                         @pn\ *)
153                                 if [[ ${row#"@pn "} == "${PN}" ]] ; then
154                                         flags=$(( ${flags} | 16 ))
155                                 else
156                                         flags=$(( ${flags} | 8 ))
157                                 fi
158                                 ;;
159                         # @use\ *) ;;
160                         @@\ *)
161                                 dsc[$ndsc]="${row#"@@ "}"
162                                 (( ++ndsc ))
163                                 ;;
164                 esac
165         done < "${index_file}"
166
167         ${my_test_fx} ${flags} "${pname}" \
168         && for (( i=0 ; $i < $ndsc ; i++ )) ; do einfo ">    ${dsc[$i]}" ; done
169 }
170
171 #
172 # Is $2 (defaults to $MYSQL_VERSION_ID) at least version $1?
173 # (nice) idea from versionator.eclass
174 #
175 mysql_version_is_at_least() {
176         local want_s=$(stripdots "$1") have_s=$(stripdots "${2:-${MYSQL_VERSION_ID}}")
177         [[ -z "${want_s}" ]] && die "mysql_version_is_at_least missing value to check"
178         [[ ${want_s} -le ${have_s} ]] && return 0 || return 1
179 }
180
181 #
182 # To be called on the live filesystem, reassigning symlinks of each MySQL
183 # library to the best version available.
184 #
185 mysql_lib_symlinks() {
186
187         local d dirlist maxdots libname libnameln libsuffix reldir
188         libsuffix=$(get_libname)
189
190         einfo "libsuffix = ${libsuffix}"
191         einfo "Updating MySQL libraries symlinks"
192
193         reldir="${1}"
194         pushd "${reldir}/usr/$(get_libdir)" &> /dev/null
195
196         # dirlist must contain the less significative directory left
197         dirlist="mysql"
198
199         # waste some time in removing and recreating symlinks
200         for d in $dirlist ; do
201                 for libname in $( find "${d}" -mindepth 1 -maxdepth 1 -name "*${libsuffix}*" -and -not -type "l" 2>/dev/null ) ; do
202                         # maxdot is a limit versus infinite loop
203                         maxdots=0
204                         libnameln=${libname##*/}
205                         # loop in version of the library to link it, similar to how
206                         # libtool works
207                         if [[ ${CHOST} == *-darwin* ]] ; then
208                                 # macho: libname.x.y.z.dylib
209                                 local libbasename=${libnameln%%.*}       # libname
210                                 local libver=${libnameln#${libbasename}} # .x.y.z.dylib
211                                 libver=${libver%${libsuffix}}            # .x.y.z
212                                 while [[ -n ${libver} ]] && [[ ${maxdots} -lt 6 ]] ; do
213                                         libnameln="${libbasename}${libver}${libsuffix}"
214                                         rm -f "${libnameln}"
215                                         ln -s "${libname}" "${libnameln}"
216                                         (( ++maxdots ))
217                                         libver=${libver%.*}
218                                 done
219                                 libnameln="${libbasename}${libsuffix}"
220                                 rm -f "${libnameln}"
221                                 ln -s "${libname}" "${libnameln}"
222                         else
223                                 # elf: libname.so.x.y.z
224                                 while [[ ${libnameln:0-3} != '${libsuffix}' ]] && [[ ${maxdots} -lt 6 ]] ; do
225                                         rm -f "${libnameln}"
226                                         ln -s "${libname}" "${libnameln}"
227                                         (( ++maxdots ))
228                                         libnameln="${libnameln%.*}"
229                                 done
230                                 rm -f "${libnameln}"
231                                 ln -s "${libname}" "${libnameln}"
232                         fi
233                 done
234         done
235
236         popd &> /dev/null
237 }
238
239 # @FUNCTION: mysql_init_vars
240 # @DESCRIPTION:
241 # void mysql_init_vars()
242 # Initialize global variables
243 # 2005-11-19 <vivo@gentoo.org>
244 mysql_init_vars() {
245         MY_SHAREDSTATEDIR=${MY_SHAREDSTATEDIR="${EPREFIX}/usr/share/mysql"}
246         MY_SYSCONFDIR=${MY_SYSCONFDIR="${EPREFIX}/etc/mysql"}
247         MY_LOCALSTATEDIR=${MY_LOCALSTATEDIR="${EPREFIX}/var/lib/mysql"}
248         MY_LOGDIR=${MY_LOGDIR="${EPREFIX}/var/log/mysql"}
249         MY_INCLUDEDIR=${MY_INCLUDEDIR="${EPREFIX}/usr/include/mysql"}
250         MY_LIBDIR=${MY_LIBDIR="${EPREFIX}/usr/$(get_libdir)/mysql"}
251
252         if [[ -z "${MY_DATADIR}" ]] ; then
253                 MY_DATADIR=""
254                 if [[ -f "${MY_SYSCONFDIR}/my.cnf" ]] ; then
255                         MY_DATADIR=`"my_print_defaults" mysqld 2>/dev/null \
256                                 | sed -ne '/datadir/s|^--datadir=||p' \
257                                 | tail -n1`
258                         if [[ -z "${MY_DATADIR}" ]] ; then
259                                 MY_DATADIR=`grep ^datadir "${MY_SYSCONFDIR}/my.cnf" \
260                                 | sed -e 's/.*=\s*//' \
261                                 | tail -n1`
262                         fi
263                 fi
264                 if [[ -z "${MY_DATADIR}" ]] ; then
265                         MY_DATADIR="${MY_LOCALSTATEDIR}"
266                         einfo "Using default MY_DATADIR"
267                 fi
268                 elog "MySQL MY_DATADIR is ${MY_DATADIR}"
269
270                 if [[ -z "${PREVIOUS_DATADIR}" ]] ; then
271                         if [[ -e "${MY_DATADIR}" ]] ; then
272                                 # If you get this and you're wondering about it, see bug #207636
273                                 elog "MySQL datadir found in ${MY_DATADIR}"
274                                 elog "A new one will not be created."
275                                 PREVIOUS_DATADIR="yes"
276                         else
277                                 PREVIOUS_DATADIR="no"
278                         fi
279                         export PREVIOUS_DATADIR
280                 fi
281         else
282                 if [[ ${EBUILD_PHASE} == "config" ]]; then
283                         local new_MY_DATADIR
284                         new_MY_DATADIR=`"my_print_defaults" mysqld 2>/dev/null \
285                                 | sed -ne '/datadir/s|^--datadir=||p' \
286                                 | tail -n1`
287
288                         if [[ ( -n "${new_MY_DATADIR}" ) && ( "${new_MY_DATADIR}" != "${MY_DATADIR}" ) ]]; then
289                                 ewarn "MySQL MY_DATADIR has changed"
290                                 ewarn "from ${MY_DATADIR}"
291                                 ewarn "to ${new_MY_DATADIR}"
292                                 MY_DATADIR="${new_MY_DATADIR}"
293                         fi
294                 fi
295         fi
296
297         if [ "${MY_SOURCEDIR:-unset}" == "unset" ]; then
298                 MY_SOURCEDIR=${SERVER_URI##*/}
299                 MY_SOURCEDIR=${MY_SOURCEDIR%.tar*}
300         fi
301
302         export MY_SHAREDSTATEDIR MY_SYSCONFDIR
303         export MY_LIBDIR MY_LOCALSTATEDIR MY_LOGDIR
304         export MY_INCLUDEDIR MY_DATADIR MY_SOURCEDIR
305 }