xref: /aosp_15_r20/prebuilts/build-tools/common/py3-stdlib/_aix_support.py (revision cda5da8d549138a6648c5ee6d7a49cf8f4a657be)
1"""Shared AIX support functions."""
2
3import sys
4import sysconfig
5
6try:
7    import subprocess
8except ImportError:  # pragma: no cover
9    # _aix_support is used in distutils by setup.py to build C extensions,
10    # before subprocess dependencies like _posixsubprocess are available.
11    import _bootsubprocess as subprocess
12
13
14def _aix_tag(vrtl, bd):
15    # type: (List[int], int) -> str
16    # Infer the ABI bitwidth from maxsize (assuming 64 bit as the default)
17    _sz = 32 if sys.maxsize == (2**31-1) else 64
18    _bd = bd if bd != 0 else 9988
19    # vrtl[version, release, technology_level]
20    return "aix-{:1x}{:1d}{:02d}-{:04d}-{}".format(vrtl[0], vrtl[1], vrtl[2], _bd, _sz)
21
22
23# extract version, release and technology level from a VRMF string
24def _aix_vrtl(vrmf):
25    # type: (str) -> List[int]
26    v, r, tl = vrmf.split(".")[:3]
27    return [int(v[-1]), int(r), int(tl)]
28
29
30def _aix_bos_rte():
31    # type: () -> Tuple[str, int]
32    """
33    Return a Tuple[str, int] e.g., ['7.1.4.34', 1806]
34    The fileset bos.rte represents the current AIX run-time level. It's VRMF and
35    builddate reflect the current ABI levels of the runtime environment.
36    If no builddate is found give a value that will satisfy pep425 related queries
37    """
38    # All AIX systems to have lslpp installed in this location
39    out = subprocess.check_output(["/usr/bin/lslpp", "-Lqc", "bos.rte"])
40    out = out.decode("utf-8")
41    out = out.strip().split(":")  # type: ignore
42    _bd = int(out[-1]) if out[-1] != '' else 9988
43    return (str(out[2]), _bd)
44
45
46def aix_platform():
47    # type: () -> str
48    """
49    AIX filesets are identified by four decimal values: V.R.M.F.
50    V (version) and R (release) can be retrieved using ``uname``
51    Since 2007, starting with AIX 5.3 TL7, the M value has been
52    included with the fileset bos.rte and represents the Technology
53    Level (TL) of AIX. The F (Fix) value also increases, but is not
54    relevant for comparing releases and binary compatibility.
55    For binary compatibility the so-called builddate is needed.
56    Again, the builddate of an AIX release is associated with bos.rte.
57    AIX ABI compatibility is described  as guaranteed at: https://www.ibm.com/\
58    support/knowledgecenter/en/ssw_aix_72/install/binary_compatability.html
59
60    For pep425 purposes the AIX platform tag becomes:
61    "aix-{:1x}{:1d}{:02d}-{:04d}-{}".format(v, r, tl, builddate, bitsize)
62    e.g., "aix-6107-1415-32" for AIX 6.1 TL7 bd 1415, 32-bit
63    and, "aix-6107-1415-64" for AIX 6.1 TL7 bd 1415, 64-bit
64    """
65    vrmf, bd = _aix_bos_rte()
66    return _aix_tag(_aix_vrtl(vrmf), bd)
67
68
69# extract vrtl from the BUILD_GNU_TYPE as an int
70def _aix_bgt():
71    # type: () -> List[int]
72    gnu_type = sysconfig.get_config_var("BUILD_GNU_TYPE")
73    if not gnu_type:
74        raise ValueError("BUILD_GNU_TYPE is not defined")
75    return _aix_vrtl(vrmf=gnu_type)
76
77
78def aix_buildtag():
79    # type: () -> str
80    """
81    Return the platform_tag of the system Python was built on.
82    """
83    # AIX_BUILDDATE is defined by configure with:
84    # lslpp -Lcq bos.rte | awk -F:  '{ print $NF }'
85    build_date = sysconfig.get_config_var("AIX_BUILDDATE")
86    try:
87        build_date = int(build_date)
88    except (ValueError, TypeError):
89        raise ValueError(f"AIX_BUILDDATE is not defined or invalid: "
90                         f"{build_date!r}")
91    return _aix_tag(_aix_bgt(), build_date)
92