python3.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright 2016-2017 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import sysconfig
  12. from .. import mesonlib, dependencies
  13. from . import ExtensionModule
  14. from mesonbuild.modules import ModuleReturnValue
  15. from ..interpreterbase import noKwargs, permittedKwargs, FeatureDeprecated
  16. from ..build import known_shmod_kwargs
  17. class Python3Module(ExtensionModule):
  18. @FeatureDeprecated('python3 module', '0.48.0')
  19. def __init__(self, *args, **kwargs):
  20. super().__init__(*args, **kwargs)
  21. self.snippets.add('extension_module')
  22. @permittedKwargs(known_shmod_kwargs)
  23. def extension_module(self, interpreter, state, args, kwargs):
  24. if 'name_prefix' in kwargs:
  25. raise mesonlib.MesonException('Name_prefix is set automatically, specifying it is forbidden.')
  26. if 'name_suffix' in kwargs:
  27. raise mesonlib.MesonException('Name_suffix is set automatically, specifying it is forbidden.')
  28. host_system = state.host_machine.system
  29. if host_system == 'darwin':
  30. # Default suffix is 'dylib' but Python does not use it for extensions.
  31. suffix = 'so'
  32. elif host_system == 'windows':
  33. # On Windows the extension is pyd for some unexplainable reason.
  34. suffix = 'pyd'
  35. else:
  36. suffix = []
  37. kwargs['name_prefix'] = ''
  38. kwargs['name_suffix'] = suffix
  39. return interpreter.func_shared_module(None, args, kwargs)
  40. @noKwargs
  41. def find_python(self, state, args, kwargs):
  42. command = state.environment.lookup_binary_entry(mesonlib.MachineChoice.HOST, 'python3')
  43. if command is not None:
  44. py3 = dependencies.ExternalProgram.from_entry('python3', command)
  45. else:
  46. py3 = dependencies.ExternalProgram('python3', mesonlib.python_command, silent=True)
  47. return ModuleReturnValue(py3, [py3])
  48. @noKwargs
  49. def language_version(self, state, args, kwargs):
  50. return ModuleReturnValue(sysconfig.get_python_version(), [])
  51. @noKwargs
  52. def sysconfig_path(self, state, args, kwargs):
  53. if len(args) != 1:
  54. raise mesonlib.MesonException('sysconfig_path() requires passing the name of path to get.')
  55. path_name = args[0]
  56. valid_names = sysconfig.get_path_names()
  57. if path_name not in valid_names:
  58. raise mesonlib.MesonException('{} is not a valid path name {}.'.format(path_name, valid_names))
  59. # Get a relative path without a prefix, e.g. lib/python3.6/site-packages
  60. path = sysconfig.get_path(path_name, vars={'base': '', 'platbase': '', 'installed_base': ''})[1:]
  61. return ModuleReturnValue(path, [])
  62. def initialize(*args, **kwargs):
  63. return Python3Module(*args, **kwargs)