packages feed

cabal-buck2-0.6.0.0: buck2/prelude/linking/linkables.bzl

# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is dual-licensed under either the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree or the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree. You may select, at your option, one of the
# above-listed licenses.

load("@prelude//utils:expect.bzl", "expect")
load(
    ":link_groups.bzl",
    "LinkGroupLibInfo",
)
load(
    ":link_info.bzl",
    "MergedLinkInfo",
)
load(
    ":linkable_graph.bzl",
    "LinkableGraph",
    "LinkableRootInfo",
)
load(
    ":shared_libraries.bzl",
    "SharedLibraryInfo",
)

# A record containing all provider types used for linking in the prelude.  This
# is essentially the minimal subset of a "linkable" `dependency` that user rules
# need to implement linking, and avoids needing functions to take the heavier-
# weight `dependency` type.
LinkableProviders = record(
    link_group_lib_info = field(LinkGroupLibInfo),
    linkable_graph = field([LinkableGraph, None], None),
    merged_link_info = field(MergedLinkInfo),
    shared_library_info = field(SharedLibraryInfo),
    linkable_root_info = field([LinkableRootInfo, None], None),
)

def linkable(dep: Dependency) -> LinkableProviders:
    expect(LinkGroupLibInfo in dep, str(dep.label.raw_target()))
    return LinkableProviders(
        shared_library_info = dep[SharedLibraryInfo],
        linkable_graph = dep.get(LinkableGraph),
        merged_link_info = dep[MergedLinkInfo],
        link_group_lib_info = dep[LinkGroupLibInfo],
        linkable_root_info = dep.get(LinkableRootInfo),
    )

def linkables(deps: list[Dependency]) -> list[LinkableProviders]:
    return [linkable(dep) for dep in deps if MergedLinkInfo in dep]