From 5139ee3554e00c7e7896ce077e1ca8b8361df881 Mon Sep 17 00:00:00 2001 From: Liang Qiu Date: Thu, 11 Jan 2024 17:04:00 -0800 Subject: [PATCH] Revert D52499881: rules: Reimplement native unbundled deps to better support `FORCE_RLIB` Differential Revision: D52499881 Original commit changeset: c3a889ac7abd Original Phabricator Diff: D52499881 fbshipit-source-id: 671eee394f4eff28331443db251edff70fafcf17 --- prelude/rust/link_info.bzl | 36 ++++++++++------ prelude/rust/rust_library.bzl | 77 +++++------------------------------ 2 files changed, 34 insertions(+), 79 deletions(-) diff --git a/prelude/rust/link_info.bzl b/prelude/rust/link_info.bzl index a7032d8b..4b349200 100644 --- a/prelude/rust/link_info.bzl +++ b/prelude/rust/link_info.bzl @@ -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 @@ -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. # @@ -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( @@ -460,7 +467,8 @@ 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( @@ -468,7 +476,8 @@ def inherited_shared_libs( 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]: @@ -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]: diff --git a/prelude/rust/rust_library.bzl b/prelude/rust/rust_library.bzl index 1af9245d..9393333f 100644 --- a/prelude/rust/rust_library.bzl +++ b/prelude/rust/rust_library.bzl @@ -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", @@ -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( @@ -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, @@ -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, @@ -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: @@ -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. """ @@ -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 = []