From 5be994ebf67dcd09f7fa3828ddf2a4f78ac30194 Mon Sep 17 00:00:00 2001
From: Ray Donnelly <mingw.android@gmail.com>
Date: Fri, 2 Oct 2020 00:03:12 +0200
Subject: [PATCH 16/26] cross compile darwin

By Isuru Fernando.
---
 Lib/platform.py | 7 ++++++-
 configure       | 5 ++++-
 configure.ac    | 5 ++++-
 setup.py        | 6 +++---
 4 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/Lib/platform.py b/Lib/platform.py
index d6412e169b..618c993205 100755
--- a/Lib/platform.py
+++ b/Lib/platform.py
@@ -411,7 +411,12 @@ def win32_ver(release='', version='', csd='', ptype=''):
 def _mac_ver_xml():
     fn = '/System/Library/CoreServices/SystemVersion.plist'
     if not os.path.exists(fn):
-        return None
+        if 'SDKROOT' in os.environ:
+            fn = os.environ['SDKROOT'] + fn
+            if not os.path.exists(fn):
+                return None
+        else:
+            return None
 
     try:
         import plistlib
diff --git a/configure b/configure
index 1edfbad0e1..03dcc72779 100755
--- a/configure
+++ b/configure
@@ -3365,6 +3365,9 @@ if test "$cross_compiling" = yes; then
 			_host_cpu=$host_cpu
 		esac
 		;;
+	*-*-darwin*)
+		_host_cpu=$host_cpu
+		;;
 	*-*-cygwin*)
 		_host_cpu=
 		;;
@@ -6235,7 +6238,7 @@ esac
   fi
 fi
 
-if test "$cross_compiling" = yes; then
+if test "$cross_compiling" = yes -a "$ac_sys_system" != "Darwin"; then
     case "$READELF" in
 	readelf|:)
 	as_fn_error $? "readelf for the host is required for cross builds" "$LINENO" 5
diff --git a/configure.ac b/configure.ac
index 250ef5aad7..77e42364a9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -448,6 +448,9 @@ if test "$cross_compiling" = yes; then
 			_host_cpu=$host_cpu
 		esac
 		;;
+	*-*-darwin*)
+		_host_cpu=$host_cpu
+		;;
 	*-*-cygwin*)
 		_host_cpu=
 		;;
@@ -1224,7 +1227,7 @@ then
 fi
 
 AC_CHECK_TOOLS([READELF], [readelf], [:])
-if test "$cross_compiling" = yes; then
+if test "$cross_compiling" = yes -a "$ac_sys_system" != "Darwin"; then
     case "$READELF" in
 	readelf|:)
 	AC_MSG_ERROR([readelf for the host is required for cross builds])
diff --git a/setup.py b/setup.py
index c900c832e3..74ea2fbefa 100644
--- a/setup.py
+++ b/setup.py
@@ -62,7 +62,7 @@ CROSS_COMPILING = ("_PYTHON_HOST_PLATFORM" in os.environ)
 HOST_PLATFORM = get_platform()
 MS_WINDOWS = (HOST_PLATFORM == 'win32')
 CYGWIN = (HOST_PLATFORM == 'cygwin')
-MACOS = (HOST_PLATFORM == 'darwin')
+MACOS = (HOST_PLATFORM.startswith('darwin'))
 AIX = (HOST_PLATFORM.startswith('aix'))
 VXWORKS = ('vxworks' in HOST_PLATFORM)
 CC = os.environ.get("CC")
@@ -1000,11 +1000,11 @@ class PyBuildExt(build_ext):
             os.makedirs(self.build_temp)
         # Determine if readline is already linked against curses or tinfo.
         if do_readline:
-            if CROSS_COMPILING:
+            if CROSS_COMPILING and not MACOS:
                 ret = run_command("%s -d %s | grep '(NEEDED)' > %s"
                                 % (sysconfig.get_config_var('READELF'),
                                    do_readline, tmpfile))
-            elif find_executable('ldd'):
+            elif find_executable('ldd') and not MACOS:
                 ret = run_command("ldd %s > %s" % (do_readline, tmpfile))
             else:
                 ret = 1
