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",
    cxx_binary: str = "c++",
) -> NativeLinkInputs
Source code in packages/irx/src/irx/builder/runtime/linking.py
 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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
@typechecked
def compile_native_artifacts(
    artifacts: Sequence[NativeArtifact],
    build_dir: Path,
    clang_binary: str = "clang",
    cxx_binary: str = "c++",
) -> NativeLinkInputs:
    """
    title: Compile or collect native artifacts for linking.
    parameters:
      artifacts:
        type: Sequence[NativeArtifact]
      build_dir:
        type: Path
      clang_binary:
        type: str
      cxx_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_native_source(
                    artifact=artifact,
                    build_dir=build_dir,
                    compiler_binary=clang_binary,
                )
            )
            continue

        if artifact.kind == "cxx_source":
            objects.append(
                _compile_native_source(
                    artifact=artifact,
                    build_dir=build_dir,
                    compiler_binary=_resolve_cxx_binary(cxx_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, cxx_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",
    cxx_binary: str = "c++",
) -> None
Source code in packages/irx/src/irx/builder/runtime/linking.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
@typechecked
def link_executable(
    primary_object: Path,
    output_file: Path,
    artifacts: Sequence[NativeArtifact],
    linker_flags: Sequence[str] = (),
    clang_binary: str = "clang",
    cxx_binary: str = "c++",
) -> 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
      cxx_binary:
        type: str
    """
    build_dir = primary_object.parent
    link_inputs = compile_native_artifacts(
        artifacts=artifacts,
        build_dir=build_dir,
        clang_binary=clang_binary,
        cxx_binary=cxx_binary,
    )

    linker_binary = (
        _resolve_cxx_binary(cxx_binary)
        if _has_cxx_artifacts(artifacts)
        else clang_binary
    )
    command = [linker_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 linker_binary == "clang"
            else None
        ),
    )