From 3fd8fcd33d2b0f3ac2fc043eac8641ab4432967d Mon Sep 17 00:00:00 2001
From: Alexander Kanavin <alex@linutronix.de>
Date: Wed, 11 May 2022 21:41:14 +0200
Subject: [PATCH] _distutils/sysconfig.py: make it possible to substite the
 prefix to target sysroot

This is done by probing STAGING_INCDIR/STAGING_LIBDIRenv vars:
not the most elegant solution, but distutils/sysconfig has been
tweaked to do this for many, many year, and so it's easiest
to replicate here as well, the original is
meta/recipes-devtools/python/python3/12-distutils-prefix-is-inside-staging-area.patch

I'm not sure exactly why setuptools now needs a copy, and what
would happen to this module in light of distutils deprecation.

Upstream-Status: Inappropriate [oe-core specific]
Signed-off-by: Alexander Kanavin <alex@linutronix.de>

---
 setuptools/_distutils/sysconfig.py | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/setuptools/_distutils/sysconfig.py b/setuptools/_distutils/sysconfig.py
index e41d51e..c17a79a 100644
--- a/setuptools/_distutils/sysconfig.py
+++ b/setuptools/_distutils/sysconfig.py
@@ -119,6 +119,8 @@ def get_python_inc(plat_specific=0, prefix=None):
     sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
     """
     default_prefix = BASE_EXEC_PREFIX if plat_specific else BASE_PREFIX
+    if os.environ.get('STAGING_INCDIR', ""):
+        default_prefix = os.environ['STAGING_INCDIR'].rstrip('include')
     resolved_prefix = prefix if prefix is not None else default_prefix
     try:
         getter = globals()[f'_get_python_inc_{os.name}']
@@ -221,7 +223,13 @@ def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
 
     early_prefix = prefix
 
-    if prefix is None:
+    if os.environ.get('STAGING_LIBDIR', ""):
+        lib_basename = os.environ['STAGING_LIBDIR'].split('/')[-1]
+    else:
+        lib_basename = "lib"
+    if prefix is None and os.environ.get('STAGING_LIBDIR', ""):
+        prefix = os.environ['STAGING_LIBDIR'].rstrip(lib_basename)
+    elif prefix is None:
         if standard_lib:
             prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
         else:
@@ -236,7 +244,7 @@ def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
             # Pure Python
             libdir = "lib"
         implementation = 'pypy' if IS_PYPY else 'python'
-        libpython = os.path.join(prefix, libdir, implementation + get_python_version())
+        libpython = os.path.join(prefix, lib_basename, implementation + get_python_version())
         return _posix_lib(standard_lib, libpython, early_prefix, prefix)
     elif os.name == "nt":
         if standard_lib:
