Skip to content

linking

Classes:

Functions:

NativeLinkInputs dataclass

NativeLinkInputs(
    objects: tuple[Path, ...], linker_flags: tuple[str, ...]
)

compile_native_artifacts

compile_native_artifacts(
    artifacts: Sequence[NativeArtifact],
    build_dir: Path,
    clang_binary: str = "clang",
) -> NativeLinkInputs
Source code in src/irx/runtime/linking.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def compile_native_artifacts(
    artifacts: Sequence[NativeArtifact],
    build_dir: Path,
    clang_binary: str = "clang",
) -> NativeLinkInputs:
    """
    title: Compile or collect native artifacts for linking.
    parameters:
      artifacts:
        type: Sequence[NativeArtifact]
      build_dir:
        type: Path
      clang_binary:
        type: str
    returns:
      type: NativeLinkInputs
    """
    objects: list[Path] = []
    linker_flags: list[str] = []

    for artifact in artifacts:
        linker_flags.extend(artifact.link_flags)
        if artifact.kind == "c_source":
            objects.append(
                _compile_c_source(
                    artifact=artifact,
                    build_dir=build_dir,
                    clang_binary=clang_binary,
                )
            )
            continue

        if artifact.kind in {"object", "static_library"}:
            objects.append(artifact.path)
            continue

        raise ValueError(f"Unsupported native artifact kind '{artifact.kind}'")

    return NativeLinkInputs(tuple(objects), tuple(linker_flags))
link_executable(
    primary_object: Path,
    output_file: Path,
    artifacts: Sequence[NativeArtifact],
    linker_flags: Sequence[str] = (),
    clang_binary: str = "clang",
) -> None
Source code in src/irx/runtime/linking.py
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def link_executable(
    primary_object: Path,
    output_file: Path,
    artifacts: Sequence[NativeArtifact],
    linker_flags: Sequence[str] = (),
    clang_binary: str = "clang",
) -> None:
    """
    title: Link the main object file plus optional runtime artifacts.
    parameters:
      primary_object:
        type: Path
      output_file:
        type: Path
      artifacts:
        type: Sequence[NativeArtifact]
      linker_flags:
        type: Sequence[str]
      clang_binary:
        type: str
    """
    build_dir = primary_object.parent
    link_inputs = compile_native_artifacts(
        artifacts=artifacts,
        build_dir=build_dir,
        clang_binary=clang_binary,
    )

    command = [clang_binary, str(primary_object)]
    command.extend(str(obj) for obj in link_inputs.objects)
    command.extend(link_inputs.linker_flags)
    command.extend(linker_flags)
    command.extend(["-o", str(output_file)])
    _run_checked(command)