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/builder/runtime/linking.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@typechecked
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 RuntimeFeatureError(
            Diagnostic(
                message=(
                    f"runtime artifact '{artifact.path}' uses unsupported "
                    f"kind '{artifact.kind}'"
                ),
                code=DiagnosticCodes.RUNTIME_ARTIFACT_KIND_INVALID,
                phase="runtime",
                notes=(
                    "supported artifact kinds: c_source, object, "
                    "static_library",
                ),
            )
        )

    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/builder/runtime/linking.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
@typechecked
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,
        error_type=LinkingError,
        phase="link",
        code=DiagnosticCodes.LINK_FAILED,
        message=f"link failed while producing '{output_file.name}'",
        notes=(f"output path: {output_file}",),
        hint=(
            "install clang or pass a valid clang_binary value"
            if clang_binary == "clang"
            else None
        ),
    )