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