| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import Final |
| 4 |
|
| 5 |
|
| 6 |
class Capsule: |
| 7 |
"""Defines a C extension capsule that a primitive may require.""" |
| 8 |
|
| 9 |
def __init__(self, name: str) -> None: |
| 10 |
# Module fullname, e.g. 'librt.base64' |
| 11 |
self.name: Final = name |
| 12 |
|
| 13 |
def __repr__(self) -> str: |
| 14 |
return f"Capsule(name={self.name!r})" |
| 15 |
|
| 16 |
def __eq__(self, other: object) -> bool: |
| 17 |
return isinstance(other, Capsule) and self.name == other.name |
| 18 |
|
| 19 |
def __hash__(self) -> int: |
| 20 |
return hash(("Capsule", self.name)) |
| 21 |
|
| 22 |
def internal_dep(self) -> SourceDep: |
| 23 |
"""Internal source dependency of the capsule that should only be included in the C extensions |
| 24 |
that depend on the capsule, eg. by importing a type or function from the capsule. |
| 25 |
""" |
| 26 |
module = self.name.split(".")[-1] |
| 27 |
return SourceDep(f"{module}/librt_{module}_api.c", include_dirs=[module]) |
| 28 |
|
| 29 |
def external_dep(self) -> HeaderDep: |
| 30 |
"""External header dependency of the capsule that may be included in external headers of C |
| 31 |
extensions that depend on the capsule. |
| 32 |
|
| 33 |
The external headers of the C extensions are included by other C extensions that don't |
| 34 |
necessarily import the capsule. However, they may need type definitions from the capsule |
| 35 |
for types that are used in the exports table of the included C extensions. |
| 36 |
|
| 37 |
Only the external header should be included in this case because if the other C extension |
| 38 |
doesn't import the capsule, it also doesn't include the definition for its API table and |
| 39 |
including the internal header would result in undefined symbols. |
| 40 |
""" |
| 41 |
module = self.name.split(".")[-1] |
| 42 |
return HeaderDep(f"{module}/librt_{module}.h", include_dirs=[module], internal=False) |
| 43 |
|
| 44 |
|
| 45 |
class SourceDep: |
| 46 |
"""Defines a C source file that a primitive may require. |
| 47 |
|
| 48 |
Each source file must also have a corresponding .h file (replace .c with .h) |
| 49 |
that gets implicitly #included if the source is used. |
| 50 |
include_dirs are passed to the C compiler when the file is compiled as a |
| 51 |
shared library separate from the C extension. |
| 52 |
""" |
| 53 |
|
| 54 |
def __init__( |
| 55 |
self, path: str, *, include_dirs: list[str] | None = None, internal: bool = True |
| 56 |
) -> None: |
| 57 |
# Relative path from mypyc/lib-rt, e.g. 'bytes_extra_ops.c' |
| 58 |
self.path: Final = path |
| 59 |
self.include_dirs: Final = include_dirs or [] |
| 60 |
self.internal: Final = internal |
| 61 |
|
| 62 |
def __repr__(self) -> str: |
| 63 |
return f"SourceDep(path={self.path!r})" |
| 64 |
|
| 65 |
def __eq__(self, other: object) -> bool: |
| 66 |
return isinstance(other, SourceDep) and self.path == other.path |
| 67 |
|
| 68 |
def __hash__(self) -> int: |
| 69 |
return hash(("SourceDep", self.path)) |
| 70 |
|
| 71 |
def get_header(self) -> str: |
| 72 |
"""Get the header file path by replacing .c with .h""" |
| 73 |
return self.path.replace(".c", ".h") |
| 74 |
|
| 75 |
|
| 76 |
class HeaderDep: |
| 77 |
"""Defines a C header file that a primitive may require. |
| 78 |
|
| 79 |
The header gets explicitly #included if the dependency is used. |
| 80 |
include_dirs are passed to the C compiler when the generated extension |
| 81 |
is compiled separately and needs to include the header. |
| 82 |
""" |
| 83 |
|
| 84 |
def __init__( |
| 85 |
self, path: str, *, include_dirs: list[str] | None = None, internal: bool = True |
| 86 |
) -> None: |
| 87 |
# Relative path from mypyc/lib-rt, e.g. 'strings/librt_strings.h' |
| 88 |
self.path: Final = path |
| 89 |
self.include_dirs: Final = include_dirs or [] |
| 90 |
self.internal: Final = internal |
| 91 |
|
| 92 |
def __repr__(self) -> str: |
| 93 |
return f"HeaderDep(path={self.path!r})" |
| 94 |
|
| 95 |
def __eq__(self, other: object) -> bool: |
| 96 |
return isinstance(other, HeaderDep) and self.path == other.path |
| 97 |
|
| 98 |
def __hash__(self) -> int: |
| 99 |
return hash(("HeaderDep", self.path)) |
| 100 |
|
| 101 |
def get_header(self) -> str: |
| 102 |
return self.path |
| 103 |
|
| 104 |
|
| 105 |
Dependency = Capsule | SourceDep | HeaderDep |
| 106 |
|
| 107 |
|
| 108 |
LIBRT_STRINGS: Final = Capsule("librt.strings") |
| 109 |
LIBRT_BASE64: Final = Capsule("librt.base64") |
| 110 |
LIBRT_VECS: Final = Capsule("librt.vecs") |
| 111 |
LIBRT_TIME: Final = Capsule("librt.time") |
| 112 |
LIBRT_RANDOM: Final = Capsule("librt.random") |
| 113 |
|
| 114 |
BYTES_EXTRA_OPS: Final = SourceDep("bytes_extra_ops.c") |
| 115 |
BYTES_WRITER_EXTRA_OPS: Final = SourceDep("byteswriter_extra_ops.c") |
| 116 |
STRING_WRITER_EXTRA_OPS: Final = SourceDep("stringwriter_extra_ops.c") |
| 117 |
BYTEARRAY_EXTRA_OPS: Final = SourceDep("bytearray_extra_ops.c") |
| 118 |
STR_EXTRA_OPS: Final = SourceDep("str_extra_ops.c") |
| 119 |
VECS_EXTRA_OPS: Final = SourceDep("vecs_extra_ops.c") |