1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: multilib-build.eclass
7 # gx86-multilib team <multilib@gentoo.org>
9 # Author: Michał Górny <mgorny@gentoo.org>
10 # @BLURB: flags and utility functions for building multilib packages
12 # The multilib-build.eclass exports USE flags and utility functions
13 # necessary to build packages for multilib in a clean and uniform
16 # Please note that dependency specifications for multilib-capable
17 # dependencies shall use the USE dependency string in ${MULTILIB_USEDEP}
18 # to properly request multilib enabled.
20 if [[ ! ${_MULTILIB_BUILD} ]]; then
22 # EAPI=4 is required for meaningful MULTILIB_USEDEP.
25 *) die "EAPI=${EAPI} is not supported" ;;
28 inherit multibuild multilib
30 # @ECLASS-VARIABLE: _MULTILIB_FLAGS
33 # The list of multilib flags and corresponding ABI values. If the same
34 # flag is reused for multiple ABIs (e.g. x86 on Linux&FreeBSD), multiple
35 # ABIs may be separated by commas.
37 # Please contact multilib before modifying this list. This way we can
38 # ensure that every *preliminary* work is done and the multilib can be
41 abi_x86_32:x86,x86_fbsd,x86_freebsd,x86_linux,x86_macos,x86_solaris
42 abi_x86_64:amd64,amd64_fbsd,x64_freebsd,amd64_linux,x64_macos,x64_solaris
47 abi_ppc_32:ppc,ppc_aix,ppc_macos
53 # @ECLASS-VARIABLE: MULTILIB_COMPAT
56 # List of multilib ABIs supported by the ebuild. If unset, defaults to
57 # all ABIs supported by the eclass.
59 # This variable is intended for use in prebuilt multilib packages that
60 # can provide binaries only for a limited set of ABIs. If ABIs need to
61 # be limited due to a bug in source code, package.use.mask is to be used
62 # instead. Along with MULTILIB_COMPAT, KEYWORDS should contain '-*'.
64 # Note that setting this variable effectively disables support for all
65 # other ABIs, including other architectures. For example, specifying
66 # abi_x86_{32,64} disables support for MIPS as well.
68 # The value of MULTILIB_COMPAT determines the value of IUSE. If set, it
69 # also enables REQUIRED_USE constraints.
73 # # Upstream provides binaries for x86 & amd64 only
74 # MULTILIB_COMPAT=( abi_x86_{32,64} )
77 # @ECLASS-VARIABLE: MULTILIB_USEDEP
79 # The USE-dependency to be used on dependencies (libraries) needing
80 # to support multilib as well.
84 # RDEPEND="dev-libs/libfoo[${MULTILIB_USEDEP}]
85 # net-libs/libbar[ssl,${MULTILIB_USEDEP}]"
88 # @ECLASS-VARIABLE: MULTILIB_ABI_FLAG
91 # The complete ABI name. Resembles the USE flag name.
93 # This is set within multilib_foreach_abi(),
94 # multilib_parallel_foreach_abi() and multilib-minimal sub-phase
97 # It may be null (empty) when the build is done on ABI not controlled
98 # by a USE flag (e.g. on non-multilib arch or when using multilib
99 # portage). The build will always be done for a single ABI then.
106 _multilib_build_set_globals() {
107 local flags=( "${_MULTILIB_FLAGS[@]%:*}" )
109 if [[ ${MULTILIB_COMPAT[@]} ]]; then
110 # Validate MULTILIB_COMPAT and filter out the flags.
112 for f in "${MULTILIB_COMPAT[@]}"; do
113 if ! has "${f}" "${flags[@]}"; then
114 die "Invalid value in MULTILIB_COMPAT: ${f}"
118 flags=( "${MULTILIB_COMPAT[@]}" )
120 REQUIRED_USE="|| ( ${flags[*]} )"
123 local usedeps=${flags[@]/%/(-)?}
126 MULTILIB_USEDEP=${usedeps// /,}
128 _multilib_build_set_globals
130 # @FUNCTION: multilib_get_enabled_abis
132 # Return the ordered list of enabled ABIs if multilib builds
133 # are enabled. The best (most preferred) ABI will come last.
135 # If multilib is disabled, the default ABI will be returned
136 # in order to enforce consistent testing with multilib code.
137 multilib_get_enabled_abis() {
138 debug-print-function ${FUNCNAME} "${@}"
140 local pairs=( $(multilib_get_enabled_abi_pairs) )
141 echo "${pairs[@]#*.}"
144 # @FUNCTION: multilib_get_enabled_abi_pairs
146 # Return the ordered list of enabled <use-flag>.<ABI> pairs
147 # if multilib builds are enabled. The best (most preferred)
148 # ABI will come last.
150 # If multilib is disabled, the default ABI will be returned
151 # along with empty <use-flag>.
152 multilib_get_enabled_abi_pairs() {
153 debug-print-function ${FUNCNAME} "${@}"
155 local abis=( $(get_all_abis) )
158 for abi in "${abis[@]}"; do
159 for i in "${_MULTILIB_FLAGS[@]}"; do
160 local m_abis=${i#*:} m_abi
163 # split on ,; we can't switch IFS for function scope because
164 # paludis is broken (bug #486592), and switching it locally
165 # for the split is more complex than cheating like this
166 for m_abi in ${m_abis//,/ }; do
167 if [[ ${m_abi} == ${abi} ]] \
168 && { [[ ! "${MULTILIB_COMPAT[@]}" ]] || has "${m_flag}" "${MULTILIB_COMPAT[@]}"; } \
171 echo "${m_flag}.${abi}"
179 if [[ ! ${found} ]]; then
180 # ${ABI} can be used to override the fallback (multilib-portage),
181 # ${DEFAULT_ABI} is the safe fallback.
182 local abi=${ABI:-${DEFAULT_ABI}}
184 debug-print "${FUNCNAME}: no ABIs enabled, fallback to ${abi}"
185 debug-print "${FUNCNAME}: ABI=${ABI}, DEFAULT_ABI=${DEFAULT_ABI}"
190 # @FUNCTION: _multilib_multibuild_wrapper
194 # Initialize the environment for ABI selected for multibuild.
195 _multilib_multibuild_wrapper() {
196 debug-print-function ${FUNCNAME} "${@}"
198 local ABI=${MULTIBUILD_VARIANT#*.}
199 local MULTILIB_ABI_FLAG=${MULTIBUILD_VARIANT%.*}
201 multilib_toolchain_setup "${ABI}"
205 # @FUNCTION: multilib_foreach_abi
208 # If multilib support is enabled, sets the toolchain up for each
209 # supported ABI along with the ABI variable and correct BUILD_DIR,
210 # and runs the given commands with them.
212 # If multilib support is disabled, it just runs the commands. No setup
214 multilib_foreach_abi() {
215 debug-print-function ${FUNCNAME} "${@}"
217 local MULTIBUILD_VARIANTS=( $(multilib_get_enabled_abi_pairs) )
218 multibuild_foreach_variant _multilib_multibuild_wrapper "${@}"
221 # @FUNCTION: multilib_parallel_foreach_abi
224 # If multilib support is enabled, sets the toolchain up for each
225 # supported ABI along with the ABI variable and correct BUILD_DIR,
226 # and runs the given commands with them.
228 # If multilib support is disabled, it just runs the commands. No setup
231 # This function used to run multiple commands in parallel. Now it's just
232 # a deprecated alias to multilib_foreach_abi.
233 multilib_parallel_foreach_abi() {
234 debug-print-function ${FUNCNAME} "${@}"
236 local MULTIBUILD_VARIANTS=( $(multilib_get_enabled_abi_pairs) )
237 multibuild_foreach_variant _multilib_multibuild_wrapper "${@}"
240 # @FUNCTION: multilib_for_best_abi
243 # Runs the given command with setup for the 'best' (usually native) ABI.
244 multilib_for_best_abi() {
245 debug-print-function ${FUNCNAME} "${@}"
247 eqawarn "QA warning: multilib_for_best_abi() function is deprecated and should"
248 eqawarn "not be used. The multilib_is_native_abi() check may be used instead."
250 local MULTIBUILD_VARIANTS=( $(multilib_get_enabled_abi_pairs) )
252 multibuild_for_best_variant _multilib_multibuild_wrapper "${@}"
255 # @FUNCTION: multilib_check_headers
257 # Check whether the header files are consistent between ABIs.
259 # This function needs to be called after each ABI's installation phase.
260 # It obtains the header file checksums and compares them with previous
261 # runs (if any). Dies if header files differ.
262 multilib_check_headers() {
263 _multilib_header_cksum() {
264 [[ -d ${ED}usr/include ]] && \
265 find "${ED}"usr/include -type f \
266 -exec cksum {} + | sort -k2
269 local cksum=$(_multilib_header_cksum)
270 local cksum_file=${T}/.multilib_header_cksum
272 if [[ -f ${cksum_file} ]]; then
273 local cksum_prev=$(< "${cksum_file}")
275 if [[ ${cksum} != ${cksum_prev} ]]; then
276 echo "${cksum}" > "${cksum_file}.new"
278 eerror "Header files have changed between ABIs."
280 if type -p diff &>/dev/null; then
281 eerror "$(diff -du "${cksum_file}" "${cksum_file}.new")"
283 eerror "Old checksums in: ${cksum_file}"
284 eerror "New checksums in: ${cksum_file}.new"
287 die "Header checksum mismatch, aborting."
290 echo "${cksum}" > "${cksum_file}"
294 # @FUNCTION: multilib_copy_sources
296 # Create a single copy of the package sources for each enabled ABI.
298 # The sources are always copied from initial BUILD_DIR (or S if unset)
299 # to ABI-specific build directory matching BUILD_DIR used by
300 # multilib_foreach_abi().
301 multilib_copy_sources() {
302 debug-print-function ${FUNCNAME} "${@}"
304 local MULTIBUILD_VARIANTS=( $(multilib_get_enabled_abi_pairs) )
305 multibuild_copy_sources
308 # @ECLASS-VARIABLE: MULTILIB_WRAPPED_HEADERS
310 # A list of headers to wrap for multilib support. The listed headers
311 # will be moved to a non-standard location and replaced with a file
312 # including them conditionally to current ABI.
314 # This variable has to be a bash array. Paths shall be relative to
315 # installation root (${ED}), and name regular files. Recursive wrapping
318 # Please note that header wrapping is *discouraged*. It is preferred to
319 # install all headers in a subdirectory of libdir and use pkg-config to
320 # locate the headers. Some C preprocessors will not work with wrapped
325 # MULTILIB_WRAPPED_HEADERS=(
326 # /usr/include/foobar/config.h
330 # @ECLASS-VARIABLE: MULTILIB_CHOST_TOOLS
332 # A list of tool executables to preserve for each multilib ABI.
333 # The listed executables will be renamed to ${CHOST}-${basename},
334 # and the native variant will be symlinked to the generic name.
336 # This variable has to be a bash array. Paths shall be relative to
337 # installation root (${ED}), and name regular files or symbolic
338 # links to regular files. Recursive wrapping is not supported.
340 # If symbolic link is passed, both symlink path and symlink target
341 # will be changed. As a result, the symlink target is expected
342 # to be wrapped as well (either by listing in MULTILIB_CHOST_TOOLS
345 # Please note that tool wrapping is *discouraged*. It is preferred to
346 # install pkg-config files for each ABI, and require reverse
347 # dependencies to use that.
349 # Packages that search for tools properly (e.g. using AC_PATH_TOOL
350 # macro) will find the wrapper executables automatically. Other packages
351 # will need explicit override of tool paths.
355 # MULTILIB_CHOST_TOOLS=(
356 # /usr/bin/foo-config
360 # @FUNCTION: multilib_prepare_wrappers
361 # @USAGE: [<install-root>]
363 # Perform the preparation of all kinds of wrappers for the current ABI.
364 # This function shall be called once per each ABI, after installing
365 # the files to be wrapped.
367 # Takes an optional custom <install-root> from which files will be
368 # used. If no root is specified, uses ${ED}.
370 # The files to be wrapped are specified using separate variables,
371 # e.g. MULTILIB_WRAPPED_HEADERS. Those variables shall not be changed
372 # between the successive calls to multilib_prepare_wrappers
373 # and multilib_install_wrappers.
375 # After all wrappers are prepared, multilib_install_wrappers shall
376 # be called to commit them to the installation tree.
377 multilib_prepare_wrappers() {
378 debug-print-function ${FUNCNAME} "${@}"
380 [[ ${#} -le 1 ]] || die "${FUNCNAME}: too many arguments"
382 local root=${1:-${ED}}
385 if [[ ${COMPLETE_MULTILIB} == yes ]]; then
386 # symlink '${CHOST}-foo -> foo' to support abi-wrapper while
387 # keeping ${CHOST}-foo calls correct.
389 for f in "${MULTILIB_CHOST_TOOLS[@]}"; do
390 # drop leading slash if it's there
396 ln -s "${fn}" "${root}/${dir}/${CHOST}-${fn}" || die
402 for f in "${MULTILIB_CHOST_TOOLS[@]}"; do
403 # drop leading slash if it's there
409 if [[ -L ${root}/${f} ]]; then
410 # rewrite the symlink target
411 local target=$(readlink "${root}/${f}")
413 local target_fn=${target##*/}
415 [[ ${target} == */* ]] && target_dir=${target%/*}
417 ln -f -s "${target_dir+${target_dir}/}${CHOST}-${target_fn}" \
418 "${root}/${f}" || die
421 mv "${root}/${f}" "${root}/${dir}/${CHOST}-${fn}" || die
423 # symlink the native one back
424 if multilib_is_native_abi; then
425 ln -s "${CHOST}-${fn}" "${root}/${f}" || die
429 if [[ ${MULTILIB_WRAPPED_HEADERS[@]} ]]; then
430 # If abi_flag is unset, then header wrapping is unsupported on
431 # this ABI. This means the arch doesn't support multilib at all
432 # -- in this case, the headers are not wrapped and everything
435 if [[ ${MULTILIB_ABI_FLAG} ]]; then
436 for f in "${MULTILIB_WRAPPED_HEADERS[@]}"; do
437 # drop leading slash if it's there
440 if [[ ${f} != usr/include/* ]]; then
441 die "Wrapping headers outside of /usr/include is not supported at the moment."
443 # and then usr/include
448 # Some ABIs may have install less files than others.
449 if [[ -f ${root}/usr/include${f} ]]; then
450 local wrapper=${ED}/tmp/multilib-include${f}
452 if [[ ! -f ${ED}/tmp/multilib-include${f} ]]; then
453 dodir "/tmp/multilib-include${dir}"
455 cat > "${wrapper}" <<_EOF_
456 /* This file is auto-generated by multilib-build.eclass
457 * as a multilib-friendly wrapper. For the original content,
458 * please see the files that are #included below.
461 #if defined(__x86_64__) /* amd64 */
462 # if defined(__ILP32__) /* x32 ABI */
463 # error "abi_x86_x32 not supported by the package."
464 # else /* 64-bit ABI */
465 # error "abi_x86_64 not supported by the package."
467 #elif defined(__i386__) /* plain x86 */
468 # error "abi_x86_32 not supported by the package."
469 #elif defined(__mips__)
470 # if(_MIPS_SIM == _ABIN32) /* n32 */
471 # error "abi_mips_n32 not supported by the package."
472 # elif(_MIPS_SIM == _ABI64) /* n64 */
473 # error "abi_mips_n64 not supported by the package."
474 # elif(_MIPS_SIM == _ABIO32) /* o32 */
475 # error "abi_mips_o32 not supported by the package."
477 #elif defined(__sparc__)
478 # if defined(__arch64__)
479 # error "abi_sparc_64 not supported by the package."
481 # error "abi_sparc_32 not supported by the package."
483 #elif defined(__s390__)
484 # if defined(__s390x__)
485 # error "abi_s390_64 not supported by the package."
487 # error "abi_s390_32 not supported by the package."
489 #elif defined(__powerpc__)
490 # if defined(__powerpc64__)
491 # error "abi_ppc_64 not supported by the package."
493 # error "abi_ppc_32 not supported by the package."
495 #elif defined(SWIG) /* https://sourceforge.net/p/swig/bugs/799/ */
496 # error "Native ABI not supported by the package."
498 # error "No ABI matched, please report a bug to bugs.gentoo.org"
503 if ! grep -q "${MULTILIB_ABI_FLAG} " "${wrapper}"
505 die "Flag ${MULTILIB_ABI_FLAG} not listed in wrapper template. Please report a bug to https://bugs.gentoo.org."
508 # $CHOST shall be set by multilib_toolchain_setup
509 dodir "/tmp/multilib-include/${CHOST}${dir}"
510 mv "${root}/usr/include${f}" "${ED}/tmp/multilib-include/${CHOST}${dir}/" || die
512 # Note: match a space afterwards to avoid collision potential.
513 sed -e "/${MULTILIB_ABI_FLAG} /s&error.*&include <${CHOST}${f}>&" \
514 -i "${wrapper}" || die
517 if multilib_is_native_abi; then
518 sed -e "/Native ABI/s&error.*&include <${CHOST}${f}>&" \
519 -i "${wrapper}" || die
527 # @FUNCTION: multilib_install_wrappers
528 # @USAGE: [<install-root>]
530 # Install the previously-prepared wrappers. This function shall
531 # be called once, after all wrappers were prepared.
533 # Takes an optional custom <install-root> to which the wrappers will be
534 # installed. If no root is specified, uses ${ED}. There is no need to
535 # use the same root as when preparing the wrappers.
537 # The files to be wrapped are specified using separate variables,
538 # e.g. MULTILIB_WRAPPED_HEADERS. Those variables shall not be changed
539 # between the calls to multilib_prepare_wrappers
540 # and multilib_install_wrappers.
541 multilib_install_wrappers() {
542 debug-print-function ${FUNCNAME} "${@}"
544 [[ ${#} -le 1 ]] || die "${FUNCNAME}: too many arguments"
546 [[ ${COMPLETE_MULTILIB} == yes ]] && return
548 local root=${1:-${ED}}
550 if [[ -d "${ED}"/tmp/multilib-include ]]; then
551 multibuild_merge_root \
552 "${ED}"/tmp/multilib-include "${root}"/usr/include
553 # it can fail if something else uses /tmp
554 rmdir "${ED}"/tmp &>/dev/null
558 # @FUNCTION: multilib_is_native_abi
560 # Determine whether the currently built ABI is the profile native.
561 # Return true status (0) if that is true, otherwise false (1).
562 multilib_is_native_abi() {
563 debug-print-function ${FUNCNAME} "${@}"
565 [[ ${#} -eq 0 ]] || die "${FUNCNAME}: too many arguments"
567 [[ ${COMPLETE_MULTILIB} == yes || ${ABI} == ${DEFAULT_ABI} ]]
570 # @FUNCTION: multilib_build_binaries
572 # Deprecated synonym for multilib_is_native_abi
573 multilib_build_binaries() {
574 debug-print-function ${FUNCNAME} "${@}"
576 eqawarn "QA warning: multilib_build_binaries is deprecated. Please use the equivalent"
577 eqawarn "multilib_is_native_abi function instead."
579 multilib_is_native_abi "${@}"
582 # @FUNCTION: multilib_native_use_with
583 # @USAGE: <flag> [<opt-name> [<opt-value>]]
585 # Output --with configure option alike use_with if USE <flag> is enabled
586 # and executables are being built (multilib_is_native_abi is true).
587 # Otherwise, outputs --without configure option. Arguments are the same
588 # as for use_with in the EAPI.
589 multilib_native_use_with() {
590 if multilib_is_native_abi; then
593 echo "--without-${2:-${1}}"
597 # @FUNCTION: multilib_native_use_enable
598 # @USAGE: <flag> [<opt-name> [<opt-value>]]
600 # Output --enable configure option alike use_enable if USE <flag>
601 # is enabled and executables are being built (multilib_is_native_abi
602 # is true). Otherwise, outputs --disable configure option. Arguments are
603 # the same as for use_enable in the EAPI.
604 multilib_native_use_enable() {
605 if multilib_is_native_abi; then
608 echo "--disable-${2:-${1}}"
612 # @FUNCTION: multilib_native_enable
613 # @USAGE: <opt-name> [<opt-value>]
615 # Output --enable configure option if executables are being built
616 # (multilib_is_native_abi is true). Otherwise, output --disable configure
618 multilib_native_enable() {
619 if multilib_is_native_abi; then
620 echo "--enable-${1}${2+=${2}}"
622 echo "--disable-${1}"
626 # @FUNCTION: multilib_native_with
627 # @USAGE: <opt-name> [<opt-value>]
629 # Output --with configure option if executables are being built
630 # (multilib_is_native_abi is true). Otherwise, output --without configure
632 multilib_native_with() {
633 if multilib_is_native_abi; then
634 echo "--with-${1}${2+=${2}}"
636 echo "--without-${1}"
640 # @FUNCTION: multilib_native_usex
641 # @USAGE: <flag> [<true1> [<false1> [<true2> [<false2>]]]]
643 # Output the concatenation of <true1> (or 'yes' if unspecified)
644 # and <true2> if USE <flag> is enabled and executables are being built
645 # (multilib_is_native_abi is true). Otherwise, output the concatenation
646 # of <false1> (or 'no' if unspecified) and <false2>. Arguments
647 # are the same as for usex in the EAPI.
649 # Note: in EAPI 4 you need to inherit eutils to use this function.
650 multilib_native_usex() {
651 if multilib_is_native_abi; then