From 56bc800f3b9ebc403a2ca88d3175a77e76be69e0 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/24] 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 b7e7bc439e..b6ed15be7e 100755
--- a/Lib/platform.py
+++ b/Lib/platform.py
@@ -407,7 +407,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 0f5cae29a4..6250044058 100755
--- a/configure
+++ b/configure
@@ -3317,6 +3317,9 @@ if test "$cross_compiling" = yes; then
 			_host_cpu=$host_cpu
 		esac
 		;;
+	*-*-darwin*)
+		_host_cpu=$host_cpu
+		;;
 	*-*-cygwin*)
 		_host_cpu=
 		;;
@@ -6165,7 +6168,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 703c59ab25..13dc4d8780 100644
--- a/configure.ac
+++ b/configure.ac
@@ -440,6 +440,9 @@ if test "$cross_compiling" = yes; then
 			_host_cpu=$host_cpu
 		esac
 		;;
+	*-*-darwin*)
+		_host_cpu=$host_cpu
+		;;
 	*-*-cygwin*)
 		_host_cpu=
 		;;
@@ -1202,7 +1205,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 4af4a15243..92958e7ef7 100644
--- a/setup.py
+++ b/setup.py
@@ -43,7 +43,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)
 
@@ -892,11 +892,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 = os.system("%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 = os.system("ldd %s > %s" % (do_readline, tmpfile))
             else:
                 ret = 256
