Allow using `c++` mode for Cython files on Android and iOS (#8619)

* Allow using c++ mode for Cython files on Android and iOS

* Fix PEP8 violation
This commit is contained in:
Mirko Galimberti 2024-02-18 10:23:25 +01:00 committed by GitHub
parent ff80a2213a
commit 1ca6948c34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 39 additions and 2 deletions

View File

@ -1075,6 +1075,42 @@ def resolve_dependencies(fn, depends):
def get_extensions_from_sources(sources):
def _get_cythonized_source_extension(cython_file: str, flags: dict) -> str:
# The cythonized file can be either a .c or .cpp file
# depending on the language tag in the .pyx file, or the
# flag passed to the extension.
# If the language tag or the flag is not set, we assume
# the file is a .c file.
def _to_extension(language: str) -> str:
return "cpp" if language == "c++" else "c"
if "language" in flags:
return _to_extension(flags["language"])
with open(cython_file, "r", encoding="utf-8") as _source_file:
for line in _source_file:
line = line.lstrip()
if not line:
continue
if line[0] != "#":
break
line = line[1:].lstrip()
if not line.startswith("distutils:"):
continue
distutils_settings_key, _, distutils_settings_value = [
s.strip() for s in line[len("distutils:"):].partition("=")
]
if distutils_settings_key == "language":
return _to_extension(distutils_settings_value)
return _to_extension("c")
ext_modules = []
if environ.get('KIVY_FAKE_BUILDEXT'):
print('Fake build_ext asked, will generate only .h/.c')
@ -1085,8 +1121,9 @@ def get_extensions_from_sources(sources):
depends = [expand(src_path, x) for x in flags.pop('depends', [])]
c_depends = [expand(src_path, x) for x in flags.pop('c_depends', [])]
if not can_use_cython:
# can't use cython, so use the .c files instead.
pyx_path = '%s.c' % pyx_path[:-4]
# can't use cython, so use the .c or .cpp files instead.
_ext = _get_cythonized_source_extension(pyx_path, flags)
pyx_path = f"{pyx_path[:-4]}.{_ext}"
if is_graphics:
depends = resolve_dependencies(pyx_path, depends)
f_depends = [x for x in depends if x.rsplit('.', 1)[-1] in (