python3.py 2.9 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 sys
  12. import sysconfig
  13. from .. import mesonlib, dependencies
  14. from . import ExtensionModule
  15. from mesonbuild.modules import ModuleReturnValue
  16. from . import permittedSnippetKwargs
  17. from ..interpreterbase import noKwargs
  18. from ..interpreter import shlib_kwargs
  19. mod_kwargs = set()
  20. mod_kwargs.update(shlib_kwargs)
  21. class Python3Module(ExtensionModule):
  22. def __init__(self):
  23. super().__init__()
  24. self.snippets.add('extension_module')
  25. @permittedSnippetKwargs(mod_kwargs)
  26. def extension_module(self, interpreter, state, args, kwargs):
  27. if 'name_prefix' in kwargs:
  28. raise mesonlib.MesonException('Name_prefix is set automatically, specifying it is forbidden.')
  29. if 'name_suffix' in kwargs:
  30. raise mesonlib.MesonException('Name_suffix is set automatically, specifying it is forbidden.')
  31. host_system = state.host_machine.system
  32. if host_system == 'darwin':
  33. # Default suffix is 'dylib' but Python does not use it for extensions.
  34. suffix = 'so'
  35. elif host_system == 'windows':
  36. # On Windows the extension is pyd for some unexplainable reason.
  37. suffix = 'pyd'
  38. else:
  39. suffix = []
  40. kwargs['name_prefix'] = ''
  41. kwargs['name_suffix'] = suffix
  42. return interpreter.func_shared_module(None, args, kwargs)
  43. @noKwargs
  44. def find_python(self, state, args, kwargs):
  45. py3 = dependencies.ExternalProgram('python3', mesonlib.python_command, silent=True)
  46. return ModuleReturnValue(py3, [py3])
  47. @noKwargs
  48. def language_version(self, state, args, kwargs):
  49. return ModuleReturnValue(sysconfig.get_python_version(), [])
  50. @noKwargs
  51. def sysconfig_path(self, state, args, kwargs):
  52. if len(args) != 1:
  53. raise mesonlib.MesonException('sysconfig_path() requires passing the name of path to get.')
  54. path_name = args[0]
  55. valid_names = sysconfig.get_path_names()
  56. if path_name not in valid_names:
  57. raise mesonlib.MesonException('{} is not a valid path name {}.'.format(path_name, valid_names))
  58. # Get a relative path without a prefix, e.g. lib/python3.6/site-packages
  59. path = sysconfig.get_path(path_name, vars={'base': '', 'platbase': '', 'installed_base': ''})[1:]
  60. return ModuleReturnValue(path, [])
  61. def initialize():
  62. return Python3Module()