Skip to content

Commit 75ed79d

Browse files
authored
Merge pull request #145 from lopsided98/python-inc-cross
Fix finding headers when cross compiling
2 parents e5b02d2 + 26607e1 commit 75ed79d

File tree

1 file changed

+64
-30
lines changed

1 file changed

+64
-30
lines changed

distutils/sysconfig.py

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -118,40 +118,74 @@ def get_python_inc(plat_specific=0, prefix=None):
118118
If 'prefix' is supplied, use it instead of sys.base_prefix or
119119
sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
120120
"""
121-
if prefix is None:
122-
prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
123-
if os.name == "posix":
124-
if IS_PYPY and sys.version_info < (3, 8):
125-
return os.path.join(prefix, 'include')
126-
if python_build:
127-
# Assume the executable is in the build directory. The
128-
# pyconfig.h file should be in the same directory. Since
129-
# the build directory may not be the source directory, we
130-
# must use "srcdir" from the makefile to find the "Include"
131-
# directory.
132-
if plat_specific:
133-
return _sys_home or project_base
134-
else:
135-
incdir = os.path.join(get_config_var('srcdir'), 'Include')
136-
return os.path.normpath(incdir)
137-
implementation = 'pypy' if IS_PYPY else 'python'
138-
python_dir = implementation + get_python_version() + build_flags
139-
return os.path.join(prefix, "include", python_dir)
140-
elif os.name == "nt":
141-
if python_build:
142-
# Include both the include and PC dir to ensure we can find
143-
# pyconfig.h
144-
return (
145-
os.path.join(prefix, "include")
146-
+ os.path.pathsep
147-
+ os.path.join(prefix, "PC")
148-
)
149-
return os.path.join(prefix, "include")
150-
else:
121+
default_prefix = BASE_EXEC_PREFIX if plat_specific else BASE_PREFIX
122+
resolved_prefix = prefix if prefix is not None else default_prefix
123+
try:
124+
getter = globals()[f'_get_python_inc_{os.name}']
125+
except KeyError:
151126
raise DistutilsPlatformError(
152127
"I don't know where Python installs its C header files "
153128
"on platform '%s'" % os.name
154129
)
130+
return getter(resolved_prefix, prefix, plat_specific)
131+
132+
133+
def _get_python_inc_posix(prefix, spec_prefix, plat_specific):
134+
if IS_PYPY and sys.version_info < (3, 8):
135+
return os.path.join(prefix, 'include')
136+
return (
137+
_get_python_inc_posix_python(plat_specific)
138+
or _get_python_inc_from_config(plat_specific, spec_prefix)
139+
or _get_python_inc_posix_prefix(prefix)
140+
)
141+
142+
143+
def _get_python_inc_posix_python(plat_specific):
144+
"""
145+
Assume the executable is in the build directory. The
146+
pyconfig.h file should be in the same directory. Since
147+
the build directory may not be the source directory,
148+
use "srcdir" from the makefile to find the "Include"
149+
directory.
150+
"""
151+
if not python_build:
152+
return
153+
if plat_specific:
154+
return _sys_home or project_base
155+
incdir = os.path.join(get_config_var('srcdir'), 'Include')
156+
return os.path.normpath(incdir)
157+
158+
159+
def _get_python_inc_from_config(plat_specific, spec_prefix):
160+
"""
161+
If no prefix was explicitly specified, provide the include
162+
directory from the config vars. Useful when
163+
cross-compiling, since the config vars may come from
164+
the host
165+
platform Python installation, while the current Python
166+
executable is from the build platform installation.
167+
"""
168+
if not spec_prefix:
169+
return
170+
return get_config_var('CONF' * plat_specific + 'INCLUDEPY')
171+
172+
173+
def _get_python_inc_posix_prefix(prefix):
174+
implementation = 'pypy' if IS_PYPY else 'python'
175+
python_dir = implementation + get_python_version() + build_flags
176+
return os.path.join(prefix, "include", python_dir)
177+
178+
179+
def _get_python_inc_nt(prefix, spec_prefix, plat_specific):
180+
if python_build:
181+
# Include both the include and PC dir to ensure we can find
182+
# pyconfig.h
183+
return (
184+
os.path.join(prefix, "include")
185+
+ os.path.pathsep
186+
+ os.path.join(prefix, "PC")
187+
)
188+
return os.path.join(prefix, "include")
155189

156190

157191
# allow this behavior to be monkey-patched. Ref pypa/distutils#2.

0 commit comments

Comments
 (0)