e13aae2bb7f7dc7bcb8e6ed5e333bf90435bb2e6
[gentoo.git] / eclass / multilib.eclass
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: multilib.eclass
5 # @MAINTAINER:
6 # amd64@gentoo.org
7 # toolchain@gentoo.org
8 # @BLURB: This eclass is for all functions pertaining to handling multilib configurations.
9 # @DESCRIPTION:
10 # This eclass is for all functions pertaining to handling multilib configurations.
11
12 if [[ -z ${_MULTILIB_ECLASS} ]]; then
13 _MULTILIB_ECLASS=1
14
15 inherit toolchain-funcs
16
17 # Defaults:
18 export MULTILIB_ABIS=${MULTILIB_ABIS:-"default"}
19 export DEFAULT_ABI=${DEFAULT_ABI:-"default"}
20 export CFLAGS_default
21 export LDFLAGS_default
22 export CHOST_default=${CHOST_default:-${CHOST}}
23 export CTARGET_default=${CTARGET_default:-${CTARGET:-${CHOST_default}}}
24 export LIBDIR_default=${CONF_LIBDIR:-"lib"}
25 export KERNEL_ABI=${KERNEL_ABI:-${DEFAULT_ABI}}
26
27 # @FUNCTION: has_multilib_profile
28 # @DESCRIPTION:
29 # Return true if the current profile is a multilib profile and lists more than
30 # one abi in ${MULTILIB_ABIS}.  When has_multilib_profile returns true, that
31 # profile should enable the 'multilib' use flag. This is so you can DEPEND on
32 # a package only for multilib or not multilib.
33 has_multilib_profile() {
34         [ -n "${MULTILIB_ABIS}" -a "${MULTILIB_ABIS}" != "${MULTILIB_ABIS/ /}" ]
35 }
36
37 # @FUNCTION: get_libdir
38 # @RETURN: the libdir for the selected ABI
39 # @DESCRIPTION:
40 # This function simply returns the desired lib directory. With portage
41 # 2.0.51, we now have support for installing libraries to lib32/lib64
42 # to accomidate the needs of multilib systems. It's no longer a good idea
43 # to assume all libraries will end up in lib. Replace any (sane) instances
44 # where lib is named directly with $(get_libdir) if possible.
45 #
46 # Jeremy Huddleston <eradicator@gentoo.org> (23 Dec 2004):
47 #   Added support for ${ABI} and ${DEFAULT_ABI}.  If they're both not set,
48 #   fall back on old behavior.  Any profile that has these set should also
49 #   depend on a newer version of portage (not yet released) which uses these
50 #   over CONF_LIBDIR in econf, dolib, etc...
51 if has "${EAPI:-0}" 0 1 2 3 4 5; then
52         get_libdir() {
53                 local CONF_LIBDIR
54                 if [ -n  "${CONF_LIBDIR_OVERRIDE}" ] ; then
55                         # if there is an override, we want to use that... always.
56                         echo ${CONF_LIBDIR_OVERRIDE}
57                 else
58                         get_abi_LIBDIR
59                 fi
60         }
61 fi
62
63 # @FUNCTION: get_abi_var
64 # @USAGE: <VAR> [ABI]
65 # @RETURN: returns the value of ${<VAR>_<ABI>} which should be set in make.defaults
66 # @INTERNAL
67 # @DESCRIPTION:
68 # ex:
69 # CFLAGS=$(get_abi_var CFLAGS sparc32) # CFLAGS=-m32
70 #
71 # Note that the prefered method is to set CC="$(tc-getCC) $(get_abi_CFLAGS)"
72 # This will hopefully be added to portage soon...
73 #
74 # If <ABI> is not specified, ${ABI} is used.
75 # If <ABI> is not specified and ${ABI} is not defined, ${DEFAULT_ABI} is used.
76 # If <ABI> is not specified and ${ABI} and ${DEFAULT_ABI} are not defined, we return an empty string.
77 get_abi_var() {
78         local flag=$1
79         local abi=${2:-${ABI:-${DEFAULT_ABI:-default}}}
80         local var="${flag}_${abi}"
81         echo ${!var}
82 }
83
84 # @FUNCTION: get_abi_CFLAGS
85 # @USAGE: [ABI]
86 # @DESCRIPTION:
87 # Alias for 'get_abi_var CFLAGS'
88 get_abi_CFLAGS() { get_abi_var CFLAGS "$@"; }
89
90 # @FUNCTION: get_abi_LDFLAGS
91 # @USAGE: [ABI]
92 # @DESCRIPTION:
93 # Alias for 'get_abi_var LDFLAGS'
94 get_abi_LDFLAGS() { get_abi_var LDFLAGS "$@"; }
95
96 # @FUNCTION: get_abi_CHOST
97 # @USAGE: [ABI]
98 # @DESCRIPTION:
99 # Alias for 'get_abi_var CHOST'
100 get_abi_CHOST() { get_abi_var CHOST "$@"; }
101
102 # @FUNCTION: get_abi_CTARGET
103 # @USAGE: [ABI]
104 # @DESCRIPTION:
105 # Alias for 'get_abi_var CTARGET'
106 get_abi_CTARGET() { get_abi_var CTARGET "$@"; }
107
108 # @FUNCTION: get_abi_FAKE_TARGETS
109 # @USAGE: [ABI]
110 # @DESCRIPTION:
111 # Alias for 'get_abi_var FAKE_TARGETS'
112 get_abi_FAKE_TARGETS() { get_abi_var FAKE_TARGETS "$@"; }
113
114 # @FUNCTION: get_abi_LIBDIR
115 # @USAGE: [ABI]
116 # @DESCRIPTION:
117 # Alias for 'get_abi_var LIBDIR'
118 get_abi_LIBDIR() { get_abi_var LIBDIR "$@"; }
119
120 # @FUNCTION: get_install_abis
121 # @DESCRIPTION:
122 # Return a list of the ABIs we want to install for with
123 # the last one in the list being the default.
124 get_install_abis() {
125         local x order=""
126
127         if [[ -z ${MULTILIB_ABIS} ]] ; then
128                 echo "default"
129                 return 0
130         fi
131
132         if [[ ${EMULTILIB_PKG} == "true" ]] ; then
133                 for x in ${MULTILIB_ABIS} ; do
134                         if [[ ${x} != "${DEFAULT_ABI}" ]] ; then
135                                 has ${x} ${ABI_DENY} || order="${order} ${x}"
136                         fi
137                 done
138                 has ${DEFAULT_ABI} ${ABI_DENY} || order="${order} ${DEFAULT_ABI}"
139
140                 if [[ -n ${ABI_ALLOW} ]] ; then
141                         local ordera=""
142                         for x in ${order} ; do
143                                 if has ${x} ${ABI_ALLOW} ; then
144                                         ordera="${ordera} ${x}"
145                                 fi
146                         done
147                         order=${ordera}
148                 fi
149         else
150                 order=${DEFAULT_ABI}
151         fi
152
153         if [[ -z ${order} ]] ; then
154                 die "The ABI list is empty.  Are you using a proper multilib profile?  Perhaps your USE flags or MULTILIB_ABIS are too restrictive for this package."
155         fi
156
157         echo ${order}
158         return 0
159 }
160
161 # @FUNCTION: get_all_abis
162 # @DESCRIPTION:
163 # Return a list of the ABIs supported by this profile.
164 # the last one in the list being the default.
165 get_all_abis() {
166         local x order="" mvar dvar
167
168         mvar="MULTILIB_ABIS"
169         dvar="DEFAULT_ABI"
170         if [[ -n $1 ]] ; then
171                 mvar="$1_${mvar}"
172                 dvar="$1_${dvar}"
173         fi
174
175         if [[ -z ${!mvar} ]] ; then
176                 echo "default"
177                 return 0
178         fi
179
180         for x in ${!mvar}; do
181                 if [[ ${x} != ${!dvar} ]] ; then
182                         order="${order:+${order} }${x}"
183                 fi
184         done
185         order="${order:+${order} }${!dvar}"
186
187         echo ${order}
188         return 0
189 }
190
191 # @FUNCTION: get_all_libdirs
192 # @DESCRIPTION:
193 # Returns a list of all the libdirs used by this profile.  This includes
194 # those that might not be touched by the current ebuild and always includes
195 # "lib".
196 get_all_libdirs() {
197         local libdirs abi
198
199         for abi in ${MULTILIB_ABIS}; do
200                 libdirs+=" $(get_abi_LIBDIR ${abi})"
201         done
202         [[ " ${libdirs} " != *" lib "* ]] && libdirs+=" lib"
203
204         echo "${libdirs}"
205 }
206
207 # @FUNCTION: is_final_abi
208 # @DESCRIPTION:
209 # Return true if ${ABI} is the last ABI on our list (or if we're not
210 # using the new multilib configuration.  This can be used to determine
211 # if we're in the last (or only) run through src_{unpack,compile,install}
212 is_final_abi() {
213         has_multilib_profile || return 0
214         set -- $(get_install_abis)
215         local LAST_ABI=$#
216         [[ ${!LAST_ABI} == ${ABI} ]]
217 }
218
219 # @FUNCTION: number_abis
220 # @DESCRIPTION:
221 # echo the number of ABIs we will be installing for
222 number_abis() {
223         set -- `get_install_abis`
224         echo $#
225 }
226
227 # @FUNCTION: get_exeext
228 # @DESCRIPTION:
229 # Returns standard executable program suffix (null, .exe, etc.)
230 # for the current platform identified by CHOST.
231 #
232 # Example:
233 #     get_exeext
234 #     Returns: null string (almost everywhere) || .exe (mingw*) || ...
235 get_exeext() {
236         case ${CHOST} in
237                 *-cygwin*|mingw*|*-mingw*)  echo ".exe";;
238         esac
239 }
240
241 # @FUNCTION: get_libname
242 # @USAGE: [version]
243 # @DESCRIPTION:
244 # Returns libname with proper suffix {.so,.dylib,.dll,etc} and optionally
245 # supplied version for the current platform identified by CHOST.
246 #
247 # Example:
248 #     get_libname ${PV}
249 #     Returns: .so.${PV} (ELF) || .${PV}.dylib (MACH) || ...
250 get_libname() {
251         local libname
252         local ver=$1
253         case ${CHOST} in
254                 *-cygwin*)       libname="dll.a";; # import lib
255                 mingw*|*-mingw*) libname="dll";;
256                 *-darwin*)       libname="dylib";;
257                 *-mint*)         libname="irrelevant";;
258                 hppa*-hpux*)     libname="sl";;
259                 *)               libname="so";;
260         esac
261
262         if [[ -z $* ]] ; then
263                 echo ".${libname}"
264         else
265                 for ver in "$@" ; do
266                         case ${CHOST} in
267                                 *-cygwin*) echo ".${ver}.${libname}";;
268                                 *-darwin*) echo ".${ver}.${libname}";;
269                                 *-mint*)   echo ".${libname}";;
270                                 *)         echo ".${libname}.${ver}";;
271                         esac
272                 done
273         fi
274 }
275
276 # @FUNCTION: get_modname
277 # @USAGE:
278 # @DESCRIPTION:
279 # Returns modulename with proper suffix {.so,.bundle,etc} for the current
280 # platform identified by CHOST.
281 #
282 # Example:
283 #     libfoo$(get_modname)
284 #     Returns: libfoo.so (ELF) || libfoo.bundle (MACH) || ...
285 get_modname() {
286         local modname
287         local ver=$1
288         case ${CHOST} in
289                 *-darwin*)                modname="bundle";;
290                 *)                        modname="so";;
291         esac
292
293         echo ".${modname}"
294 }
295
296 # This is for the toolchain to setup profile variables when pulling in
297 # a crosscompiler (and thus they aren't set in the profile).
298 multilib_env() {
299         local CTARGET=${1:-${CTARGET}}
300         local cpu=${CTARGET%%*-}
301
302         if [[ ${CTARGET} = *-musl* ]]; then
303                 # musl has no multilib support and can run only in 'lib':
304                 # - https://bugs.gentoo.org/675954
305                 # - https://gcc.gnu.org/PR90077
306                 # - https://github.com/gentoo/musl/issues/245
307                 : ${MULTILIB_ABIS=default}
308                 : ${DEFAULT_ABI=default}
309                 export MULTILIB_ABIS DEFAULT_ABI
310                 return
311         fi
312
313         case ${cpu} in
314                 aarch64*)
315                         # Not possible to do multilib with aarch64 and a single toolchain.
316                         export CFLAGS_arm=${CFLAGS_arm-}
317                         case ${cpu} in
318                         aarch64*be) export CHOST_arm="armv8b-${CTARGET#*-}";;
319                         *)          export CHOST_arm="armv8l-${CTARGET#*-}";;
320                         esac
321                         CHOST_arm=${CHOST_arm/%-gnu/-gnueabi}
322                         export CTARGET_arm=${CHOST_arm}
323                         export LIBDIR_arm="lib"
324
325                         export CFLAGS_arm64=${CFLAGS_arm64-}
326                         export CHOST_arm64=${CTARGET}
327                         export CTARGET_arm64=${CHOST_arm64}
328                         export LIBDIR_arm64="lib64"
329
330                         : ${MULTILIB_ABIS=arm64}
331                         : ${DEFAULT_ABI=arm64}
332                 ;;
333                 x86_64*)
334                         export CFLAGS_x86=${CFLAGS_x86--m32}
335                         export CHOST_x86=${CTARGET/x86_64/i686}
336                         CHOST_x86=${CHOST_x86/%-gnux32/-gnu}
337                         export CTARGET_x86=${CHOST_x86}
338                         if [[ ${SYMLINK_LIB} == "yes" ]] ; then
339                                 export LIBDIR_x86="lib32"
340                         else
341                                 export LIBDIR_x86="lib"
342                         fi
343
344                         export CFLAGS_amd64=${CFLAGS_amd64--m64}
345                         export CHOST_amd64=${CTARGET/%-gnux32/-gnu}
346                         export CTARGET_amd64=${CHOST_amd64}
347                         export LIBDIR_amd64="lib64"
348
349                         export CFLAGS_x32=${CFLAGS_x32--mx32}
350                         export CHOST_x32=${CTARGET/%-gnu/-gnux32}
351                         export CTARGET_x32=${CHOST_x32}
352                         export LIBDIR_x32="libx32"
353
354                         case ${CTARGET} in
355                         *-gnux32)
356                                 : ${MULTILIB_ABIS=x32 amd64 x86}
357                                 : ${DEFAULT_ABI=x32}
358                                 ;;
359                         *)
360                                 : ${MULTILIB_ABIS=amd64 x86}
361                                 : ${DEFAULT_ABI=amd64}
362                                 ;;
363                         esac
364                 ;;
365                 mips64*|mipsisa64*)
366                         export CFLAGS_o32=${CFLAGS_o32--mabi=32}
367                         export CHOST_o32=${CTARGET/mips64/mips}
368                         export CHOST_o32=${CHOST_o32/mipsisa64/mipsisa32}
369                         export CTARGET_o32=${CHOST_o32}
370                         export LIBDIR_o32="lib"
371
372                         export CFLAGS_n32=${CFLAGS_n32--mabi=n32}
373                         export CHOST_n32=${CTARGET}
374                         export CTARGET_n32=${CHOST_n32}
375                         export LIBDIR_n32="lib32"
376
377                         export CFLAGS_n64=${CFLAGS_n64--mabi=64}
378                         export CHOST_n64=${CTARGET}
379                         export CTARGET_n64=${CHOST_n64}
380                         export LIBDIR_n64="lib64"
381
382                         : ${MULTILIB_ABIS=n64 n32 o32}
383                         : ${DEFAULT_ABI=n32}
384                 ;;
385                 powerpc64*)
386                         export CFLAGS_ppc=${CFLAGS_ppc--m32}
387                         export CHOST_ppc=${CTARGET/powerpc64/powerpc}
388                         export CTARGET_ppc=${CHOST_ppc}
389                         export LIBDIR_ppc="lib"
390
391                         export CFLAGS_ppc64=${CFLAGS_ppc64--m64}
392                         export CHOST_ppc64=${CTARGET}
393                         export CTARGET_ppc64=${CHOST_ppc64}
394                         export LIBDIR_ppc64="lib64"
395
396                         : ${MULTILIB_ABIS=ppc64 ppc}
397                         : ${DEFAULT_ABI=ppc64}
398                 ;;
399                 riscv64*)
400                         export CFLAGS_lp64d=${CFLAGS_lp64d--mabi=lp64d}
401                         export CHOST_lp64d=${CTARGET}
402                         export CTARGET_lp64d=${CTARGET}
403                         export LIBDIR_lp64d="lib64/lp64d"
404
405                         export CFLAGS_lp64=${CFLAGS_lp64--mabi=lp64}
406                         export CHOST_lp64=${CTARGET}
407                         export CTARGET_lp64=${CTARGET}
408                         export LIBDIR_lp64="lib64/lp64"
409
410                         : ${MULTILIB_ABIS=lp64d lp64}
411                         : ${DEFAULT_ABI=lp64d}
412                 ;;
413                 s390x*)
414                         export CFLAGS_s390=${CFLAGS_s390--m31} # the 31 is not a typo
415                         export CHOST_s390=${CTARGET/s390x/s390}
416                         export CTARGET_s390=${CHOST_s390}
417                         export LIBDIR_s390="lib"
418
419                         export CFLAGS_s390x=${CFLAGS_s390x--m64}
420                         export CHOST_s390x=${CTARGET}
421                         export CTARGET_s390x=${CHOST_s390x}
422                         export LIBDIR_s390x="lib64"
423
424                         : ${MULTILIB_ABIS=s390x s390}
425                         : ${DEFAULT_ABI=s390x}
426                 ;;
427                 sparc64*)
428                         export CFLAGS_sparc32=${CFLAGS_sparc32--m32}
429                         export CHOST_sparc32=${CTARGET/sparc64/sparc}
430                         export CTARGET_sparc32=${CHOST_sparc32}
431                         export LIBDIR_sparc32="lib"
432
433                         export CFLAGS_sparc64=${CFLAGS_sparc64--m64}
434                         export CHOST_sparc64=${CTARGET}
435                         export CTARGET_sparc64=${CHOST_sparc64}
436                         export LIBDIR_sparc64="lib64"
437
438                         : ${MULTILIB_ABIS=sparc64 sparc32}
439                         : ${DEFAULT_ABI=sparc64}
440                 ;;
441                 *)
442                         : ${MULTILIB_ABIS=default}
443                         : ${DEFAULT_ABI=default}
444                 ;;
445         esac
446
447         export MULTILIB_ABIS DEFAULT_ABI
448 }
449
450 # @FUNCTION: multilib_toolchain_setup
451 # @DESCRIPTION:
452 # Hide multilib details here for packages which are forced to be compiled for a
453 # specific ABI when run on another ABI (like x86-specific packages on amd64)
454 multilib_toolchain_setup() {
455         local v vv
456
457         export ABI=$1
458
459         local save_restore_variables=(
460                 CBUILD
461                 CHOST
462                 CC
463                 CXX
464                 F77
465                 FC
466                 LD
467                 PKG_CONFIG_LIBDIR
468                 PKG_CONFIG_PATH
469         )
470
471         # First restore any saved state we have laying around.
472         if [[ ${_DEFAULT_ABI_SAVED} == "true" ]] ; then
473                 for v in "${save_restore_variables[@]}" ; do
474                         vv="_abi_saved_${v}"
475                         [[ ${!vv+set} == "set" ]] && export ${v}="${!vv}" || unset ${v}
476                         unset ${vv}
477                 done
478                 unset _DEFAULT_ABI_SAVED
479         fi
480
481         if [[ ${ABI} != ${DEFAULT_ABI} ]] ; then
482                 # Back that multilib-ass up so we can restore it later
483                 for v in "${save_restore_variables[@]}" ; do
484                         vv="_abi_saved_${v}"
485                         [[ ${!v+set} == "set" ]] && export ${vv}="${!v}" || unset ${vv}
486                 done
487                 export _DEFAULT_ABI_SAVED="true"
488
489                 # Set CBUILD only if not cross-compiling.
490                 if [[ ${CBUILD} == "${CHOST}" ]]; then
491                         export CBUILD=$(get_abi_CHOST $1)
492                 fi
493
494                 # Set the CHOST native first so that we pick up the native
495                 # toolchain and not a cross-compiler by accident #202811.
496                 #
497                 # Make sure ${save_restore_variables[@]} list matches below.
498                 export CHOST=$(get_abi_CHOST ${DEFAULT_ABI})
499                 export AR="$(tc-getAR)" # Avoid 'ar', use '${CHOST}-ar'
500                 export CC="$(tc-getCC) $(get_abi_CFLAGS)"
501                 export CXX="$(tc-getCXX) $(get_abi_CFLAGS)"
502                 export F77="$(tc-getF77) $(get_abi_CFLAGS)"
503                 export FC="$(tc-getFC) $(get_abi_CFLAGS)"
504                 export LD="$(tc-getLD) $(get_abi_LDFLAGS)"
505                 export NM="$(tc-getNM)" # Avoid 'nm', use '${CHOST}-nm'
506                 export OBJDUMP="$(tc-getOBJDUMP)" # Avoid 'objdump', use '${CHOST}-objdump'
507                 export RANLIB="$(tc-getRANLIB)" # Avoid 'ranlib', use '${CHOST}-ranlib'
508                 export STRIP="$(tc-getSTRIP)" # Avoid 'strip', use '${CHOST}-strip'
509                 export CHOST=$(get_abi_CHOST $1)
510                 export PKG_CONFIG_LIBDIR=${EPREFIX}/usr/$(get_libdir)/pkgconfig
511                 export PKG_CONFIG_PATH=${EPREFIX}/usr/share/pkgconfig
512         fi
513 }
514
515 fi