diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2020-03-23 10:51:14 -0700 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2020-04-21 00:40:19 +0300 |
commit | 57b468c75ae90e09f8bd98da12a5c420ab49cd79 (patch) | |
tree | 92068579bf791fa343de5f8ca8601c77cf2041df | |
parent | cd79ec2839ddef766b42e0653f8be2199b723e77 (diff) | |
download | meson-57b468c75ae90e09f8bd98da12a5c420ab49cd79.zip meson-57b468c75ae90e09f8bd98da12a5c420ab49cd79.tar.gz meson-57b468c75ae90e09f8bd98da12a5c420ab49cd79.tar.bz2 |
Use pkg_resource to find resources files (data)
Doing this by hand is fraught with corner cases, and we're running into
some of those problems already. setuptools pkg_resource is a well tested
solution for not running into corner cases, and we already rely on
setuptools to make our entry point scripts work, so we might as well
make us of the other things it can solve for us.
Fixes #6801
-rw-r--r-- | mesonbuild/cmake/interpreter.py | 6 | ||||
-rw-r--r-- | mesonbuild/dependencies/base.py | 6 |
2 files changed, 8 insertions, 4 deletions
diff --git a/mesonbuild/cmake/interpreter.py b/mesonbuild/cmake/interpreter.py index 7e9873f..125f18b 100644 --- a/mesonbuild/cmake/interpreter.py +++ b/mesonbuild/cmake/interpreter.py @@ -15,6 +15,8 @@ # This class contains the basic functionality needed to run any interpreter # or an interpreter-based tool. +import pkg_resources + from .common import CMakeException, CMakeTarget from .client import CMakeClient, RequestCMakeInputs, RequestConfigure, RequestCompute, RequestCodeModel from .fileapi import CMakeFileAPI @@ -789,7 +791,7 @@ class CMakeInterpreter: raise CMakeException('Unable to find CMake') self.trace = CMakeTraceParser(cmake_exe.version(), self.build_dir, permissive=True) - preload_file = Path(__file__).resolve().parent / 'data' / 'preload.cmake' + preload_file = pkg_resources.resource_filename('mesonbuild', 'cmake/data/preload.cmake') # Prefere CMAKE_PROJECT_INCLUDE over CMAKE_TOOLCHAIN_FILE if possible, # since CMAKE_PROJECT_INCLUDE was actually designed for code injection. @@ -1034,7 +1036,7 @@ class CMakeInterpreter: root_cb.lines += [function('project', [self.project_name] + self.languages)] # Add the run script for custom commands - run_script = '{}/data/run_ctgt.py'.format(os.path.dirname(os.path.realpath(__file__))) + run_script = pkg_resources.resource_filename('mesonbuild', 'cmake/data/run_ctgt.py') run_script_var = 'ctgt_run_script' root_cb.lines += [assign(run_script_var, function('find_program', [[run_script]], {'required': True}))] diff --git a/mesonbuild/dependencies/base.py b/mesonbuild/dependencies/base.py index 8b452cd..c0ec089 100644 --- a/mesonbuild/dependencies/base.py +++ b/mesonbuild/dependencies/base.py @@ -28,6 +28,8 @@ import typing as T from enum import Enum from pathlib import Path, PurePath +import pkg_resources + from .. import mlog from .. import mesonlib from ..compilers import clib_langs @@ -1522,8 +1524,8 @@ class CMakeDependency(ExternalDependency): build_dir = self._get_build_dir() # Insert language parameters into the CMakeLists.txt and write new CMakeLists.txt - src_cmake = Path(__file__).parent / 'data' / cmake_file - cmake_txt = src_cmake.read_text() + # Per the warning in pkg_resources, this is *not* a path and os.path and Pathlib are *not* safe to use here. + cmake_txt = pkg_resources.resource_string('mesonbuild', 'dependencies/data/' + cmake_file).decode() # 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 |