diff --git a/mesonbuild/dependencies/cmake.py b/mesonbuild/dependencies/cmake.py index 66d331925574..6bf8666b7ff4 100644 --- a/mesonbuild/dependencies/cmake.py +++ b/mesonbuild/dependencies/cmake.py @@ -570,9 +570,20 @@ def _setup_cmake_dir(self, cmake_file: str) -> Path: if cmake_cache.exists(): cmake_cache.unlink() shutil.rmtree(cmake_files.as_posix(), ignore_errors=True) - # Insert language parameters into the CMakeLists.txt and write new CMakeLists.txt - cmake_txt = importlib.resources.read_text('mesonbuild.dependencies.data', cmake_file, encoding = 'utf-8') + import sys + + if sys.version_info >= (3, 9): + cmake_txt = importlib.resources.files( + 'mesonbuild.dependencies.data', + ).joinpath(cmake_file).read_text(encoding='utf-8') + else: + cmake_txt = importlib.resources.read_text( # [ignore encoding] it's on the next lines, Mr. Lint + # ('mesonbuild' / self.path.parent).as_posix().replace('/', '.'), + 'mesonbuild.dependencies.data', + # self.path.name, + cmake_file, + encoding='utf-8') # In general, some Fortran CMake find_package() also require C language enabled, # even if nothing from C is directly used. An easy Fortran example that fails diff --git a/mesonbuild/dependencies/python.py b/mesonbuild/dependencies/python.py index b9b17f854cde..21a012365239 100644 --- a/mesonbuild/dependencies/python.py +++ b/mesonbuild/dependencies/python.py @@ -113,8 +113,15 @@ def sanity(self) -> bool: # Sanity check, we expect to have something that at least quacks in tune import importlib.resources + import sys + if sys.version_info >= (3, 9): + ctx = importlib.resources.as_file( + importlib.resources.files('mesonbuild.scripts').joinpath('python_info.py') + ) + else: + ctx = importlib.resources.path('mesonbuild.scripts', 'python_info.py') - with importlib.resources.path('mesonbuild.scripts', 'python_info.py') as f: + with ctx as f: cmd = self.get_command() + [str(f)] env = os.environ.copy() env['SETUPTOOLS_USE_DISTUTILS'] = 'stdlib' diff --git a/mesonbuild/mesondata.py b/mesonbuild/mesondata.py index 50a88857172a..d1c1719713f4 100644 --- a/mesonbuild/mesondata.py +++ b/mesonbuild/mesondata.py @@ -17,7 +17,13 @@ def __init__(self, path: str) -> None: def write_once(self, path: Path) -> None: if not path.exists(): - data = importlib.resources.read_text( # [ignore encoding] it's on the next lines, Mr. Lint + import sys + if sys.version_info >= (3, 9): + data = importlib.resources.files( + ('mesonbuild' / self.path.parent).as_posix().replace('/', '.') + ).joinpath(self.path.name).read_text(encoding='utf-8') + else: + data = importlib.resources.read_text( # [ignore encoding] it's on the next lines, Mr. Lint ('mesonbuild' / self.path.parent).as_posix().replace('/', '.'), self.path.name, encoding='utf-8') diff --git a/mesonbuild/modules/python.py b/mesonbuild/modules/python.py index 59b5050c07b8..c9b622c4fd3a 100644 --- a/mesonbuild/modules/python.py +++ b/mesonbuild/modules/python.py @@ -398,7 +398,11 @@ def should_append(f, isdir: bool = False): import importlib.resources pycompile = os.path.join(self.interpreter.environment.get_scratch_dir(), 'pycompile.py') with open(pycompile, 'wb') as f: - f.write(importlib.resources.read_binary('mesonbuild.scripts', 'pycompile.py')) + import sys + if sys.version_info >= (3, 9): + f.write(importlib.resources.files('mesonbuild.scripts').joinpath('pycompile.py').read_bytes()) + else: + f.write(importlib.resources.read_binary('mesonbuild.scripts', 'pycompile.py')) for i in self.installations.values(): if isinstance(i, PythonExternalProgram) and i.run_bytecompile[i.info['version']]: