Skip to content

Commit

Permalink
Revert D52499881: rules: Reimplement native unbundled deps to better …
Browse files Browse the repository at this point in the history
…support `FORCE_RLIB`

Differential Revision:
D52499881

Original commit changeset: c3a889ac7abd

Original Phabricator Diff: D52499881

fbshipit-source-id: 671eee394f4eff28331443db251edff70fafcf17
  • Loading branch information
liangqiufb authored and facebook-github-bot committed Jan 12, 2024
1 parent 23c9384 commit 5139ee3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 79 deletions.
36 changes: 23 additions & 13 deletions prelude/rust/link_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ RustLinkInfo = provider(
# `FORCE_RLIB` behavior, in which Rust -> Rust dependency edges are always statically
# linked. The native link provider is identical, except that it does not respect the
# `FORCE_RLIB` behavior.
#
# FIXME(JakobDegen): The `advanced_unstable_linking` case is currently aspirational and not
# how things are actually implemented.
"merged_link_info": MergedLinkInfo,
"shared_libs": SharedLibraryInfo,
# Because of the weird representation of `LinkableGraph`, there is no
Expand Down Expand Up @@ -350,11 +353,14 @@ def _native_link_dependencies(
"""
first_order_deps = [dep.dep for dep in resolve_deps(ctx, dep_ctx)]

return [
d
for d in first_order_deps
if RustLinkInfo not in d and MergedLinkInfo in d
]
if dep_ctx.advanced_unstable_linking:
return [d for d in first_order_deps if MergedLinkInfo in d]
else:
return [
d
for d in first_order_deps
if RustLinkInfo not in d and MergedLinkInfo in d
]

# Returns the rust link infos for non-proc macro deps.
#
Expand All @@ -368,9 +374,10 @@ def inherited_exported_link_deps(ctx: AnalysisContext, dep_ctx: DepCollectionCon
deps = {}
for dep in _native_link_dependencies(ctx, dep_ctx):
deps[dep.label] = dep
for info in _rust_non_proc_macro_link_infos(ctx, dep_ctx):
for dep in info.exported_link_deps:
deps[dep.label] = dep
if not dep_ctx.advanced_unstable_linking:
for info in _rust_non_proc_macro_link_infos(ctx, dep_ctx):
for dep in info.exported_link_deps:
deps[dep.label] = dep
return deps.values()

def inherited_rust_cxx_link_group_info(
Expand Down Expand Up @@ -460,15 +467,17 @@ def inherited_merged_link_infos(
dep_ctx: DepCollectionContext) -> list[MergedLinkInfo]:
infos = []
infos.extend([d[MergedLinkInfo] for d in _native_link_dependencies(ctx, dep_ctx)])
infos.extend([d.merged_link_info for d in _rust_non_proc_macro_link_infos(ctx, dep_ctx) if d.merged_link_info])
if not dep_ctx.advanced_unstable_linking:
infos.extend([d.merged_link_info for d in _rust_non_proc_macro_link_infos(ctx, dep_ctx) if d.merged_link_info])
return infos

def inherited_shared_libs(
ctx: AnalysisContext,
dep_ctx: DepCollectionContext) -> list[SharedLibraryInfo]:
infos = []
infos.extend([d[SharedLibraryInfo] for d in _native_link_dependencies(ctx, dep_ctx)])
infos.extend([d.shared_libs for d in _rust_non_proc_macro_link_infos(ctx, dep_ctx)])
if not dep_ctx.advanced_unstable_linking:
infos.extend([d.shared_libs for d in _rust_non_proc_macro_link_infos(ctx, dep_ctx)])
return infos

def inherited_linkable_graphs(ctx: AnalysisContext, dep_ctx: DepCollectionContext) -> list[LinkableGraph]:
Expand All @@ -477,9 +486,10 @@ def inherited_linkable_graphs(ctx: AnalysisContext, dep_ctx: DepCollectionContex
g = d.get(LinkableGraph)
if g:
deps[g.label] = g
for info in _rust_non_proc_macro_link_infos(ctx, dep_ctx):
for g in info.linkable_graphs:
deps[g.label] = g
if not dep_ctx.advanced_unstable_linking:
for info in _rust_non_proc_macro_link_infos(ctx, dep_ctx):
for g in info.linkable_graphs:
deps[g.label] = g
return deps.values()

def inherited_link_group_lib_infos(ctx: AnalysisContext, dep_ctx: DepCollectionContext) -> list[LinkGroupLibInfo]:
Expand Down
77 changes: 11 additions & 66 deletions prelude/rust/rust_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ load(
"@prelude//cxx:cxx_context.bzl",
"get_cxx_toolchain_info",
)
load("@prelude//cxx:cxx_toolchain_types.bzl", "CxxToolchainInfo", "PicBehavior")
load("@prelude//cxx:cxx_toolchain_types.bzl", "PicBehavior")
load(
"@prelude//cxx:linker.bzl",
"PDB_SUB_TARGET",
Expand Down Expand Up @@ -131,8 +131,7 @@ def prebuilt_rust_library_impl(ctx: AnalysisContext) -> list[Provider]:
panic_runtime = rust_toolchain.panic_runtime,
)

cxx_toolchain = get_cxx_toolchain_info(ctx)
linker_info = cxx_toolchain.linker_info
linker_info = get_cxx_toolchain_info(ctx).linker_info

archive_info = LinkInfos(
default = LinkInfo(
Expand Down Expand Up @@ -179,7 +178,7 @@ def prebuilt_rust_library_impl(ctx: AnalysisContext) -> list[Provider]:
external_debug_info = external_debug_info,
)

merged_link_info, shared_libs, inherited_graphs, inherited_link_deps = _rust_link_providers(ctx, dep_ctx, cxx_toolchain, link_infos)
merged_link_info, shared_libs, inherited_graphs, inherited_link_deps = _rust_link_providers(ctx, dep_ctx)
providers.append(
RustLinkInfo(
crate = crate,
Expand Down Expand Up @@ -365,7 +364,6 @@ def rust_library_impl(ctx: AnalysisContext) -> list[Provider]:
compile_ctx = compile_ctx,
lang_style_param = lang_style_param,
param_artifact = rust_param_artifact,
link_infos = link_infos,
)
providers += _native_providers(
ctx = ctx,
Expand Down Expand Up @@ -570,14 +568,7 @@ def _default_providers(

def _rust_link_providers(
ctx: AnalysisContext,
dep_ctx: DepCollectionContext,
cxx_toolchain: CxxToolchainInfo,
link_infos: dict[LibOutputStyle, LinkInfos]) -> (
MergedLinkInfo,
SharedLibraryInfo,
list[LinkableGraph],
list[Dependency],
):
dep_ctx: DepCollectionContext) -> (MergedLinkInfo, SharedLibraryInfo, list[LinkableGraph], list[Dependency]):
# These are never accessed in the case of proc macros, so just return some dummy
# values
if ctx.attrs.proc_macro:
Expand All @@ -593,64 +584,18 @@ def _rust_link_providers(
inherited_graphs = inherited_linkable_graphs(ctx, dep_ctx)
inherited_link_deps = inherited_exported_link_deps(ctx, dep_ctx)

if dep_ctx.advanced_unstable_linking:
# We have to produce a version of the providers that are defined in such
# a way that native rules looking at these providers will also pick up
# the `FORCE_RLIB` behavior. The general approach to that will be to
# claim that we have `preferred_linkage = "static"`.
#
# Note that all of this code is FORCE_RLIB specific. Disabling that
# setting requires replacing this with the "real" native providers
merged_link_info = create_merged_link_info(
ctx,
cxx_toolchain.pic_behavior,
link_infos,
exported_deps = inherited_link_infos,
preferred_linkage = Linkage("static"),
)
shared_libs = merge_shared_libraries(
# We never actually have any shared libraries to add
ctx.actions,
deps = inherited_shlibs,
)

# The link graph representation is a little bit weird, since instead of
# just building up a graph via tsets, it uses a flat list of labeled
# nodes, each with a list of labels for dependency edges. The node that
# we create here cannot just use this target's label, since that would
# conflict with the node created for the native providers. As a result,
# we make up a fake subtarget to get a distinct label
new_label = ctx.label.configured_target().with_sub_target((ctx.label.sub_target or []) + ["fake_force_rlib_subtarget"])
linkable_graph = create_linkable_graph(
ctx,
node = create_linkable_graph_node(
ctx,
linkable_node = create_linkable_node(
ctx = ctx,
preferred_linkage = Linkage("static"),
exported_deps = inherited_graphs,
link_infos = link_infos,
default_soname = "",
),
label = new_label,
),
deps = inherited_graphs,
)
inherited_graphs = [linkable_graph]
else:
merged_link_info = create_merged_link_info_for_propagation(ctx, inherited_link_infos)
shared_libs = merge_shared_libraries(
ctx.actions,
deps = inherited_shlibs,
)
merged_link_info = create_merged_link_info_for_propagation(ctx, inherited_link_infos)
shared_libs = merge_shared_libraries(
ctx.actions,
deps = inherited_shlibs,
)
return (merged_link_info, shared_libs, inherited_graphs, inherited_link_deps)

def _rust_providers(
ctx: AnalysisContext,
compile_ctx: CompileContext,
lang_style_param: dict[(LinkageLang, LibOutputStyle), BuildParams],
param_artifact: dict[BuildParams, (RustcOutput, RustcOutput)],
link_infos: dict[LibOutputStyle, LinkInfos]) -> list[Provider]:
param_artifact: dict[BuildParams, (RustcOutput, RustcOutput)]) -> list[Provider]:
"""
Return the set of providers for Rust linkage.
"""
Expand All @@ -665,7 +610,7 @@ def _rust_providers(
link, meta = param_artifact[params]
strategy_info[link_strategy] = _handle_rust_artifact(ctx, compile_ctx.dep_ctx, params.crate_type, link_strategy, link, meta)

merged_link_info, shared_libs, inherited_graphs, inherited_link_deps = _rust_link_providers(ctx, compile_ctx.dep_ctx, compile_ctx.cxx_toolchain_info, link_infos)
merged_link_info, shared_libs, inherited_graphs, inherited_link_deps = _rust_link_providers(ctx, compile_ctx.dep_ctx)

providers = []

Expand Down

0 comments on commit 5139ee3

Please sign in to comment.