---
 eclass/meson.eclass | 74 +++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 57 insertions(+), 17 deletions(-)

diff --git a/eclass/meson.eclass b/eclass/meson.eclass
index 758e4180ba7a..0fdb1d848973 100644
--- a/eclass/meson.eclass
+++ b/eclass/meson.eclass
@@ -39,8 +39,7 @@ esac
 
 if [[ -z ${_MESON_ECLASS} ]]; then
 
-# FIXME: We will need to inherit toolchain-funcs as well to support crossdev.
-inherit ninja-utils
+inherit ninja-utils toolchain-funcs
 
 fi
 
@@ -71,17 +70,52 @@ DEPEND=">=dev-util/meson-0.39.1
 # Optional meson arguments as Bash array; this should be defined before
 # calling meson_src_configure.
 
-# Create a cross file for meson
-# fixme: This function should write a cross file as described at the
-# following url.
-# http://mesonbuild.com/Cross-compilation.html
-# _meson_create_cross_file() {
-#      touch "${T}"/meson.crossfile
-# }
+# @FUNCTION: _meson_create_cross_file
+# @INTERNAL
+# @DESCRIPTION:
+# Creates a cross file. meson uses this to define settings for
+# cross-compilers. This function is called from meson_src_configure.
+_meson_create_cross_file() {
+       # Reference: http://mesonbuild.com/Cross-compilation.html
+
+       # system roughly corresponds to uname -s (lowercase)
+       local system=unknown
+       case ${CHOST} in
+               *-aix*)     system=aix ;;
+               *-cygwin*)  system=cygwin ;;
+               *-darwin*)  system=darwin ;;
+               *-freebsd*) system=freebsd ;;
+               *-linux*)   system=linux ;;
+               *-solaris*) system=sunos ;;
+       esac
+
+       local cpu_family=$(tc-arch)
+       case ${cpu_family} in
+               amd64) cpu_family=x86_64 ;;
+               arm64) cpu_family=aarch64 ;;
+       esac
+
+       # This may require adjustment based on CFLAGS
+       local cpu=${CHOST%%-*}
+
+       cat > "${T}/meson.${CHOST}" <<-EOF
+       [binaries]
+       ar = '${AR}'
+       c = '${CC}'
+       cpp = '${CXX}'
+       strip = '${STRIP}'
+
+       [host_machine]
+       system = '${system}'
+       cpu_family = '${cpu_family}'
+       cpu = '${cpu}'
+       endian = '$(tc-endian)'
+       EOF
+}
 
 # @FUNCTION: meson_src_configure
 # @DESCRIPTION:
-# This is the meson_src_configure function
+# This is the meson_src_configure function.
 meson_src_configure() {
        debug-print-function ${FUNCNAME} "$@"
 
@@ -94,13 +128,19 @@ meson_src_configure() {
                --sysconfdir "${EPREFIX}/etc"
                )
 
-# fixme: uncomment this for crossdev support
-#      if tc-is-cross-compiler; then
-#              _meson_create_cross_file || die "unable to write meson cross 
file"
-#              mesonargs+=(
-#                      --cross-file "${T}"/meson.crossfile
-#              )
-#      fi
+       # Both meson(1) and _meson_create_cross_file need these
+       tc-export AR CC CXX STRIP
+
+       if tc-is-cross-compiler; then
+               _meson_create_cross_file || die "unable to write meson cross 
file"
+               mesonargs+=(
+                       --cross-file "${T}"/meson.${CHOST}
+               )
+               # In cross mode, meson uses CC/CXX as the "build" compilers
+               local -x AR=$(tc-getBUILD_AR)
+               local -x CC=$(tc-getBUILD_CC)
+               local -x CXX=$(tc-getBUILD_CXX)
+       fi
 
        # Append additional arguments from ebuild
        mesonargs+=("${emesonargs[@]}")
-- 
2.13.0


Reply via email to