diff options
author | Eli Schwartz <eschwartz@archlinux.org> | 2021-11-05 01:03:50 -0400 |
---|---|---|
committer | Eli Schwartz <eschwartz@archlinux.org> | 2022-02-22 22:22:16 -0500 |
commit | 78945fb9832e989453fbabc471d45bb71805eca8 (patch) | |
tree | 8a6dec38c143eb8645d59cc2ab01bc7a567f4f66 /mesonbuild/modules | |
parent | e8375d20a9aeb8c3b0ad58f299ded0e5e978b447 (diff) | |
download | meson-78945fb9832e989453fbabc471d45bb71805eca8.zip meson-78945fb9832e989453fbabc471d45bb71805eca8.tar.gz meson-78945fb9832e989453fbabc471d45bb71805eca8.tar.bz2 |
python module: add option to specify a python environment to install to
The default behavior of installing relative to prefix may be unexpected,
and is definitely wrong in many cases.
Give users control in order to specify that yes, they actually want to
install to a venv.
This is particularly useful for projects that use meson as a build
system for a python module, where *all* files shall be installed into
the python site-packages.
Diffstat (limited to 'mesonbuild/modules')
-rw-r--r-- | mesonbuild/modules/python.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/mesonbuild/modules/python.py b/mesonbuild/modules/python.py index 2b62ea3..a91ec58 100644 --- a/mesonbuild/modules/python.py +++ b/mesonbuild/modules/python.py @@ -338,11 +338,13 @@ variables.update({'base_prefix': getattr(sys, 'base_prefix', sys.prefix)}) print(json.dumps({ 'variables': variables, 'paths': paths, + 'sysconfig_paths': sysconfig.get_paths(), 'install_paths': install_paths, 'sys_paths': sys.path, 'version': sysconfig.get_python_version(), 'platform': sysconfig.get_platform(), 'is_pypy': '__pypy__' in sys.builtin_module_names, + 'is_venv': sys.prefix != variables['base_prefix'], 'link_libpython': links_against_libpython(), })) ''' @@ -352,7 +354,9 @@ if T.TYPE_CHECKING: install_paths: T.Dict[str, str] is_pypy: bool + is_venv: bool link_libpython: bool + sysconfig_paths: T.Dict[str, str] paths: T.Dict[str, str] platform: str suffix: str @@ -377,7 +381,9 @@ class PythonExternalProgram(ExternalProgram): self.info: 'PythonIntrospectionDict' = { 'install_paths': {}, 'is_pypy': False, + 'is_venv': False, 'link_libpython': False, + 'sysconfig_paths': {}, 'paths': {}, 'platform': 'sentinal', 'variables': {}, @@ -422,7 +428,22 @@ class PythonExternalProgram(ExternalProgram): return rel_path value = state.get_option(f'{key}dir', module='python') if value: + if state.is_user_defined_option('install_env', module='python'): + raise mesonlib.MesonException(f'python.{key}dir and python.install_env are mutually exclusive') return value + + install_env = state.get_option('install_env', module='python') + if install_env == 'auto': + install_env = 'venv' if self.info['is_venv'] else 'system' + + if install_env == 'system': + rel_path = os.path.join(self.info['variables']['prefix'], rel_path) + elif install_env == 'venv': + if not self.info['is_venv']: + raise mesonlib.MesonException('python.install_env cannot be set to "venv" unless you are in a venv!') + # inside a venv, deb_system is *never* active hence info['paths'] may be wrong + rel_path = self.info['sysconfig_paths'][key] + # Use python's path relative to prefix, and warn if that's not a location # python will lookup for modules. abs_path = Path(state.get_option('prefix'), rel_path) |