net-fs/openafs-kernel: remove vulnerable versions
[gentoo.git] / eclass / libtool.eclass
1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: libtool.eclass
6 # @MAINTAINER:
7 # base-system@gentoo.org
8 # @BLURB: quickly update bundled libtool code
9 # @DESCRIPTION:
10 # This eclass patches ltmain.sh distributed with libtoolized packages with the
11 # relink and portage patch among others
12 #
13 # Note, this eclass does not require libtool as it only applies patches to
14 # generated libtool files.  We do not run the libtoolize program because that
15 # requires a regeneration of the main autotool files in order to work properly.
16
17 if [[ -z ${_LIBTOOL_ECLASS} ]]; then
18 _LIBTOOL_ECLASS=1
19
20 # If an overlay has eclass overrides, but doesn't actually override the
21 # libtool.eclass, we'll have ECLASSDIR pointing to the active overlay's
22 # eclass/ dir, but libtool.eclass is still in the main Gentoo tree.  So
23 # add a check to locate the ELT-patches/ regardless of what's going on.
24 _LIBTOOL_ECLASSDIR_LOCAL=${BASH_SOURCE[0]%/*}
25 elt_patch_dir() {
26         local d="${ECLASSDIR}/ELT-patches"
27         if [[ ! -d ${d} ]] ; then
28                 d="${_LIBTOOL_ECLASSDIR_LOCAL}/ELT-patches"
29         fi
30         echo "${d}"
31 }
32
33 inherit multilib toolchain-funcs
34
35 #
36 # See if we can apply $2 on $1, and if so, do it
37 #
38 ELT_try_and_apply_patch() {
39         local ret=0
40         local file=$1
41         local patch=$2
42         local src=$3
43         local disp="${src} patch"
44         local log="${T}/elibtool.log"
45
46         if [[ -z ${_ELT_NOTED_TMP} ]] ; then
47                 _ELT_NOTED_TMP=true
48                 printf 'temp patch: %s\n' "${patch}" > "${log}"
49         fi
50         printf '\nTrying %s\n' "${disp}" >> "${log}"
51
52         if [[ ! -e ${file} ]] ; then
53                 echo "File not found: ${file}" >> "${log}"
54                 return 1
55         fi
56
57         # Save file for permission restoration.  `patch` sometimes resets things.
58         # Ideally we'd want 'stat -c %a', but stat is highly non portable and we are
59         # guaranted to have GNU find, so use that instead.
60         local perms="$(find ${file} -maxdepth 0 -printf '%m')"
61         # We only support patchlevel of 0 - why worry if its static patches?
62         if patch -p0 --dry-run "${file}" "${patch}" >> "${log}" 2>&1 ; then
63                 einfo "  Applying ${disp} ..."
64                 patch -p0 -g0 --no-backup-if-mismatch "${file}" "${patch}" >> "${log}" 2>&1
65                 ret=$?
66                 export ELT_APPLIED_PATCHES="${ELT_APPLIED_PATCHES} ${src}"
67         else
68                 ret=1
69         fi
70         chmod "${perms}" "${file}"
71
72         return "${ret}"
73 }
74
75 #
76 # Get string version of ltmain.sh or ltconfig (passed as $1)
77 #
78 ELT_libtool_version() {
79         (
80         unset VERSION
81         eval $(grep -e '^[[:space:]]*VERSION=' "$1")
82         echo "${VERSION:-0}"
83         )
84 }
85
86 #
87 # Run through the patches in $2 and see if any
88 # apply to $1 ...
89 #
90 ELT_walk_patches() {
91         local patch tmp
92         local ret=1
93         local file=$1
94         local patch_set=$2
95         local patch_dir="$(elt_patch_dir)/${patch_set}"
96         local rem_int_dep=$3
97
98         [[ -z ${patch_set} ]] && return 1
99         [[ ! -d ${patch_dir} ]] && return 1
100
101         # Allow patches to use @GENTOO_LIBDIR@ replacements
102         local sed_args=( -e "s:@GENTOO_LIBDIR@:$(get_libdir):g" )
103         if [[ -n ${rem_int_dep} ]] ; then
104                 # replace @REM_INT_DEP@ with what was passed
105                 # to --remove-internal-dep
106                 sed_args+=( -e "s|@REM_INT_DEP@|${rem_int_dep}|g" )
107         fi
108
109         pushd "$(elt_patch_dir)" >/dev/null || die
110
111         # Go through the patches in reverse order (newer version to older)
112         for patch in $(find "${patch_set}" -maxdepth 1 -type f | LC_ALL=C sort -r) ; do
113                 tmp="${T}/libtool-elt.patch"
114                 sed "${sed_args[@]}" "${patch}" > "${tmp}" || die
115                 if ELT_try_and_apply_patch "${file}" "${tmp}" "${patch}" ; then
116                         # Break to unwind w/popd rather than return directly
117                         ret=0
118                         break
119                 fi
120         done
121
122         popd >/dev/null
123         return ${ret}
124 }
125
126 # @FUNCTION: elibtoolize
127 # @USAGE: [dirs] [--portage] [--reverse-deps] [--patch-only] [--remove-internal-dep=xxx] [--shallow] [--no-uclibc]
128 # @DESCRIPTION:
129 # Apply a smorgasbord of patches to bundled libtool files.  This function
130 # should always be safe to run.  If no directories are specified, then
131 # ${S} will be searched for appropriate files.
132 #
133 # If the --shallow option is used, then only ${S}/ltmain.sh will be patched.
134 #
135 # The other options should be avoided in general unless you know what's going on.
136 elibtoolize() {
137         local x
138         local dirs=()
139         local do_portage="no"
140         local do_reversedeps="no"
141         local do_only_patches="no"
142         local do_uclibc="yes"
143         local deptoremove=
144         local do_shallow="no"
145         local force="false"
146         local elt_patches="install-sh ltmain portage relink max_cmd_len sed test tmp cross as-needed target-nm"
147
148         for x in "$@" ; do
149                 case ${x} in
150                         --portage)
151                                 # Only apply portage patch, and don't
152                                 # 'libtoolize --copy --force' if all patches fail.
153                                 do_portage="yes"
154                                 ;;
155                         --reverse-deps)
156                                 # Apply the reverse-deps patch
157                                 # http://bugzilla.gnome.org/show_bug.cgi?id=75635
158                                 do_reversedeps="yes"
159                                 elt_patches+=" fix-relink"
160                                 ;;
161                         --patch-only)
162                                 # Do not run libtoolize if none of the patches apply ..
163                                 do_only_patches="yes"
164                                 ;;
165                         --remove-internal-dep=*)
166                                 # We will replace @REM_INT_DEP@ with what is needed
167                                 # in ELT_walk_patches() ...
168                                 deptoremove=${x#--remove-internal-dep=}
169
170                                 # Add the patch for this ...
171                                 [[ -n ${deptoremove} ]] && elt_patches+=" rem-int-dep"
172                                 ;;
173                         --shallow)
174                                 # Only patch the ltmain.sh in ${S}
175                                 do_shallow="yes"
176                                 ;;
177                         --no-uclibc)
178                                 do_uclibc="no"
179                                 ;;
180                         --force)
181                                 force="true"
182                                 ;;
183                         -*)
184                                 eerror "Invalid elibtoolize option: ${x}"
185                                 die "elibtoolize called with ${x} ??"
186                                 ;;
187                         *)      dirs+=( "${x}" )
188                 esac
189         done
190
191         [[ ${do_uclibc} == "yes" ]] && elt_patches+=" uclibc-conf uclibc-ltconf"
192
193         case ${CHOST} in
194                 *-aix*)     elt_patches+=" hardcode aixrtl" ;; #213277
195                 *-darwin*)  elt_patches+=" darwin-ltconf darwin-ltmain darwin-conf" ;;
196                 *-solaris*) elt_patches+=" sol2-conf sol2-ltmain" ;;
197                 *-freebsd*) elt_patches+=" fbsd-conf fbsd-ltconf" ;;
198                 *-hpux*)    elt_patches+=" hpux-conf deplibs hc-flag-ld hardcode hardcode-relink relink-prog no-lc" ;;
199                 *-irix*)    elt_patches+=" irix-ltmain" ;;
200                 *-mint*)    elt_patches+=" mint-conf" ;;
201         esac
202
203         if $(tc-getLD) --version 2>&1 | grep -qs 'GNU gold'; then
204                 elt_patches+=" gold-conf"
205         fi
206
207         # Find out what dirs to scan.
208         if [[ ${do_shallow} == "yes" ]] ; then
209                 [[ ${#dirs[@]} -ne 0 ]] && die "Using --shallow with explicit dirs doesn't make sense"
210                 [[ -f ${S}/ltmain.sh || -f ${S}/configure ]] && dirs+=( "${S}" )
211         else
212                 [[ ${#dirs[@]} -eq 0 ]] && dirs+=( "${S}" )
213                 dirs=( $(find "${dirs[@]}" '(' -name ltmain.sh -o -name configure ')' -printf '%h\n' | sort -u) )
214         fi
215
216         local d p ret
217         for d in "${dirs[@]}" ; do
218                 export ELT_APPLIED_PATCHES=
219
220                 if [[ -f ${d}/.elibtoolized ]] ; then
221                         ${force} || continue
222                 fi
223
224                 local outfunc="einfo"
225                 [[ -f ${d}/.elibtoolized ]] && outfunc="ewarn"
226                 ${outfunc} "Running elibtoolize in: ${d#${WORKDIR}/}/"
227                 if [[ ${outfunc} == "ewarn" ]] ; then
228                         ewarn "  We've already been run in this tree; you should"
229                         ewarn "  avoid this if possible (perhaps by filing a bug)"
230                 fi
231
232                 # patching ltmain.sh
233                 [[ -f ${d}/ltmain.sh ]] &&
234                 for p in ${elt_patches} ; do
235                         ret=0
236
237                         case ${p} in
238                                 portage)
239                                         # Stupid test to see if its already applied ...
240                                         if ! grep -qs 'We do not want portage' "${d}/ltmain.sh" ; then
241                                                 ELT_walk_patches "${d}/ltmain.sh" "${p}"
242                                                 ret=$?
243                                         fi
244                                         ;;
245                                 rem-int-dep)
246                                         ELT_walk_patches "${d}/ltmain.sh" "${p}" "${deptoremove}"
247                                         ret=$?
248                                         ;;
249                                 fix-relink)
250                                         # Do not apply if we do not have the relink patch applied ...
251                                         if grep -qs 'inst_prefix_dir' "${d}/ltmain.sh" ; then
252                                                 ELT_walk_patches "${d}/ltmain.sh" "${p}"
253                                                 ret=$?
254                                         fi
255                                         ;;
256                                 max_cmd_len)
257                                         # Do not apply if $max_cmd_len is not used ...
258                                         if grep -qs 'max_cmd_len' "${d}/ltmain.sh" ; then
259                                                 ELT_walk_patches "${d}/ltmain.sh" "${p}"
260                                                 ret=$?
261                                         fi
262                                         ;;
263                                 as-needed)
264                                         ELT_walk_patches "${d}/ltmain.sh" "${p}"
265                                         ret=$?
266                                         ;;
267                                 uclibc-ltconf)
268                                         # Newer libtoolize clears ltconfig, as not used anymore
269                                         if [[ -s ${d}/ltconfig ]] ; then
270                                                 ELT_walk_patches "${d}/ltconfig" "${p}"
271                                                 ret=$?
272                                         fi
273                                         ;;
274                                 fbsd-ltconf)
275                                         if [[ -s ${d}/ltconfig ]] ; then
276                                                 ELT_walk_patches "${d}/ltconfig" "${p}"
277                                                 ret=$?
278                                         fi
279                                         ;;
280                                 darwin-ltconf)
281                                         # Newer libtoolize clears ltconfig, as not used anymore
282                                         if [[ -s ${d}/ltconfig ]] ; then
283                                                 ELT_walk_patches "${d}/ltconfig" "${p}"
284                                                 ret=$?
285                                         fi
286                                         ;;
287                                 darwin-ltmain)
288                                         # special case to avoid false positives (failing to apply
289                                         # ltmain.sh path message), newer libtools have this patch
290                                         # built in, so not much to patch around then
291                                         if [[ -e ${d}/ltmain.sh ]] && \
292                                            ! grep -qs 'verstring="-compatibility_version' "${d}/ltmain.sh" ; then
293                                                 ELT_walk_patches "${d}/ltmain.sh" "${p}"
294                                                 ret=$?
295                                         fi
296                                         ;;
297                                 install-sh)
298                                         ELT_walk_patches "${d}/install-sh" "${p}"
299                                         ret=$?
300                                         ;;
301                                 cross)
302                                         if tc-is-cross-compiler ; then
303                                                 ELT_walk_patches "${d}/ltmain.sh" "${p}"
304                                                 ret=$?
305                                         fi
306                                         ;;
307                                 *)
308                                         ELT_walk_patches "${d}/ltmain.sh" "${p}"
309                                         ret=$?
310                                         ;;
311                         esac
312
313                         if [[ ${ret} -ne 0 ]] ; then
314                                 case ${p} in
315                                         relink)
316                                                 local version=$(ELT_libtool_version "${d}/ltmain.sh")
317                                                 # Critical patch, but could be applied ...
318                                                 # FIXME:  Still need a patch for ltmain.sh > 1.4.0
319                                                 if ! grep -qs 'inst_prefix_dir' "${d}/ltmain.sh" && \
320                                                    [[ $(VER_to_int "${version}") -ge $(VER_to_int "1.4.0") ]] ; then
321                                                         ewarn "  Could not apply relink.patch!"
322                                                 fi
323                                                 ;;
324                                         portage)
325                                                 # Critical patch - for this one we abort, as it can really
326                                                 # cause breakage without it applied!
327                                                 if [[ ${do_portage} == "yes" ]] ; then
328                                                         # Stupid test to see if its already applied ...
329                                                         if ! grep -qs 'We do not want portage' "${d}/ltmain.sh" ; then
330                                                                 echo
331                                                                 eerror "Portage patch requested, but failed to apply!"
332                                                                 eerror "Please file a bug report to add a proper patch."
333                                                                 die "Portage patch requested, but failed to apply!"
334                                                         fi
335                                                 else
336                                                         if grep -qs 'We do not want portage' "${d}/ltmain.sh" ; then
337                                                         #       ewarn "  Portage patch seems to be already applied."
338                                                         #       ewarn "  Please verify that it is not needed."
339                                                                 :
340                                                         else
341                                                                 local version=$(ELT_libtool_version "${d}"/ltmain.sh)
342                                                                 echo
343                                                                 eerror "Portage patch failed to apply (ltmain.sh version ${version})!"
344                                                                 eerror "Please file a bug report to add a proper patch."
345                                                                 die "Portage patch failed to apply!"
346                                                         fi
347                                                         # We do not want to run libtoolize ...
348                                                         ELT_APPLIED_PATCHES="portage"
349                                                 fi
350                                                 ;;
351                                         darwin-*)
352                                                 [[ ${CHOST} == *"-darwin"* ]] && ewarn "  Darwin patch set '${p}' failed to apply!"
353                                                 ;;
354                                 esac
355                         fi
356                 done
357
358                 # makes sense for ltmain.sh patches only
359                 [[ -f ${d}/ltmain.sh ]] &&
360                 if [[ -z ${ELT_APPLIED_PATCHES} ]] ; then
361                         if [[ ${do_portage} == "no" && \
362                                   ${do_reversedeps} == "no" && \
363                                   ${do_only_patches} == "no" && \
364                                   ${deptoremove} == "" ]]
365                         then
366                                 ewarn "Cannot apply any patches, please file a bug about this"
367                                 die
368                         fi
369                 fi
370
371                 # patching configure
372                 [[ -f ${d}/configure ]] &&
373                 for p in ${elt_patches} ; do
374                         ret=0
375
376                         case ${p} in
377                                 uclibc-conf)
378                                         if grep -qs 'Transform linux' "${d}/configure" ; then
379                                                 ELT_walk_patches "${d}/configure" "${p}"
380                                                 ret=$?
381                                         fi
382                                         ;;
383                                 fbsd-conf)
384                                         if grep -qs 'version_type=freebsd-' "${d}/configure" ; then
385                                                 ELT_walk_patches "${d}/configure" "${p}"
386                                                 ret=$?
387                                         fi
388                                         ;;
389                                 darwin-conf)
390                                         if grep -qs '&& echo \.so ||' "${d}/configure" ; then
391                                                 ELT_walk_patches "${d}/configure" "${p}"
392                                                 ret=$?
393                                         fi
394                                         ;;
395                                 aixrtl|hpux-conf)
396                                         ret=1
397                                         local subret=0
398                                         # apply multiple patches as often as they match
399                                         while [[ $subret -eq 0 ]]; do
400                                                 subret=1
401                                                 if [[ -e ${d}/configure ]]; then
402                                                         ELT_walk_patches "${d}/configure" "${p}"
403                                                         subret=$?
404                                                 fi
405                                                 if [[ $subret -eq 0 ]]; then
406                                                         # have at least one patch succeeded.
407                                                         ret=0
408                                                 fi
409                                         done
410                                         ;;
411                                 mint-conf|gold-conf|sol2-conf)
412                                         ELT_walk_patches "${d}/configure" "${p}"
413                                         ret=$?
414                                         ;;
415                                 target-nm)
416                                         ELT_walk_patches "${d}/configure" "${p}"
417                                         ret=$?
418                                         ;;
419                                 *)
420                                         # ltmain.sh patches are applied above
421                                         ;;
422                         esac
423
424                         if [[ ${ret} -ne 0 ]] ; then
425                                 case ${p} in
426                                         uclibc-*)
427                                                 [[ ${CHOST} == *-uclibc ]] && ewarn "  uClibc patch set '${p}' failed to apply!"
428                                                 ;;
429                                         fbsd-*)
430                                                 if [[ ${CHOST} == *-freebsd* ]] ; then
431                                                         if [[ -z $(grep 'Handle Gentoo/FreeBSD as it was Linux' \
432                                                                 "${d}/configure" 2>/dev/null) ]]; then
433                                                                 eerror "  FreeBSD patch set '${p}' failed to apply!"
434                                                                 die "FreeBSD patch set '${p}' failed to apply!"
435                                                         fi
436                                                 fi
437                                                 ;;
438                                         darwin-*)
439                                                 [[ ${CHOST} == *"-darwin"* ]] && ewarn "  Darwin patch set '${p}' failed to apply!"
440                                                 ;;
441                                 esac
442                         fi
443                 done
444
445                 rm -f "${d}/libtool"
446
447                 > "${d}/.elibtoolized"
448         done
449 }
450
451 uclibctoolize() { die "Use elibtoolize"; }
452 darwintoolize() { die "Use elibtoolize"; }
453
454 # char *VER_major(string)
455 #
456 #    Return the Major (X of X.Y.Z) version
457 #
458 VER_major() {
459         [[ -z $1 ]] && return 1
460
461         local VER=$@
462         echo "${VER%%[^[:digit:]]*}"
463 }
464
465 # char *VER_minor(string)
466 #
467 #    Return the Minor (Y of X.Y.Z) version
468 #
469 VER_minor() {
470         [[ -z $1 ]] && return 1
471
472         local VER=$@
473         VER=${VER#*.}
474         echo "${VER%%[^[:digit:]]*}"
475 }
476
477 # char *VER_micro(string)
478 #
479 #    Return the Micro (Z of X.Y.Z) version.
480 #
481 VER_micro() {
482         [[ -z $1 ]] && return 1
483
484         local VER=$@
485         VER=${VER#*.*.}
486         echo "${VER%%[^[:digit:]]*}"
487 }
488
489 # int VER_to_int(string)
490 #
491 #    Convert a string type version (2.4.0) to an int (132096)
492 #    for easy compairing or versions ...
493 #
494 VER_to_int() {
495         [[ -z $1 ]] && return 1
496
497         local VER_MAJOR=$(VER_major "$1")
498         local VER_MINOR=$(VER_minor "$1")
499         local VER_MICRO=$(VER_micro "$1")
500         local VER_int=$(( VER_MAJOR * 65536 + VER_MINOR * 256 + VER_MICRO ))
501
502         # We make version 1.0.0 the minimum version we will handle as
503         # a sanity check ... if its less, we fail ...
504         if [[ ${VER_int} -ge 65536 ]] ; then
505                 echo "${VER_int}"
506                 return 0
507         fi
508
509         echo 1
510         return 1
511 }
512
513 fi