Debian Codegraph @

lib_pypy/hpy/devel/abitag.py

raw symbols SCIP
1 import sys
2 from distutils import sysconfig
3
4 # NOTE: these must be kept on sync with the equivalent defines in hpy.h
5 HPY_ABI_VERSION = 0
6 HPY_ABI_VERSION_MINOR = 0
7 HPY_ABI_TAG = 'hpy%d' % HPY_ABI_VERSION
8
9 def parse_ext_suffix(ext_suffix=None):
10 """
11 Parse EXT_SUFFIX and return abi_tag, ext.
12
13 - abi_tag is a string representing the CPython ABI tag: e.g. 'cp38', 'cp310d', etc.
14 - ext is the filename extension, e.g 'so', 'pyd' or 'dylib'
15 """
16 if ext_suffix is None:
17 ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')
18
19 # ext_suffix is something like this. We want to keep the parts which are
20 # related to the ABI and remove the parts which are related to the
21 # platform:
22 # - linux: '.cpython-310-x86_64-linux-gnu.so'
23 # - mac: '.cpython-310-darwin.so'
24 # - win: '.cp310-win_amd64.pyd'
25 # - pypy: '.pypy38-pp73-x86_64-linux-gnu.so'
26 # - graalpy: '.graalpy-38-native-x86_64-darwin.dylib'
27 assert ext_suffix[0] == '.'
28 _, soabi, ext = ext_suffix.split('.')
29
30 # this is very ad-hoc but couldn't find a better way to do it
31 parts = soabi.split('-')
32 if parts[0].startswith('cpython'): # cpython on linux, mac
33 n = 2
34 elif parts[0].startswith('cp'): # cpython on windows
35 n = 1
36 elif parts[0].startswith('pypy'): # pypy
37 n = 2
38 elif parts[0].startswith('graalpy'): # graalpy
39 n = 3
40 else: # none of the above, keep all parts
41 n = len(parts)
42
43 abi_tag = '-'.join(parts[:n])
44 # on cpython linux/mac, abi is now cpython-310: shorten it ot cp310, like
45 # on windows and on wheel tags
46 abi_tag = abi_tag.replace('cpython-', 'cp')
47 return abi_tag, ext
48
49
50 def get_hpy_ext_suffix(hpy_abi, ext_suffix=None):
51 """
52 Return the proper filename extension for the given hpy_abi.
53
54 For example with CPython 3.10 on Linux, it will return:
55
56 - universal ==> .hpy0.so
57 - hybrid ==> .hpy0-cp310.so
58 """
59 assert hpy_abi in ('cpython', 'universal', 'hybrid')
60 cpy_abi_tag, ext = parse_ext_suffix(ext_suffix)
61 if hpy_abi == 'cpython':
62 return sysconfig.get_config_var('EXT_SUFFIX')
63 elif hpy_abi == 'universal':
64 return '.%s.%s' % (HPY_ABI_TAG, ext)
65 else:
66 return '.%s-%s.%s' % (HPY_ABI_TAG, cpy_abi_tag, ext)