diff --git a/proto/proto.bzl b/proto/proto.bzl index 15f1354342..bd60a150a9 100644 --- a/proto/proto.bzl +++ b/proto/proto.bzl @@ -167,6 +167,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, grpc, root = lib_rs, srcs = srcs, deps = compile_deps, + aliases = {}, output = rust_lib, edition = proto_toolchain.edition, ), diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index ac5b97a88d..60fb1a8edd 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -120,6 +120,7 @@ def _rust_library_impl(ctx): root = lib_rs, srcs = ctx.files.srcs, deps = ctx.attr.deps, + aliases = ctx.attr.aliases, output = rust_lib, edition = _get_edition(ctx, toolchain), ), @@ -143,6 +144,7 @@ def _rust_binary_impl(ctx): root = _crate_root_src(ctx, "main.rs"), srcs = ctx.files.srcs, deps = ctx.attr.deps, + aliases = ctx.attr.aliases, output = output, edition = _get_edition(ctx, toolchain), ), @@ -168,6 +170,7 @@ def _rust_test_common(ctx, test_binary): root = crate.root, srcs = crate.srcs + ctx.files.srcs, deps = crate.deps + ctx.attr.deps, + aliases = ctx.attr.aliases, output = test_binary, edition = crate.edition, ) @@ -186,6 +189,7 @@ def _rust_test_common(ctx, test_binary): root = _crate_root_src(ctx), srcs = ctx.files.srcs, deps = ctx.attr.deps, + aliases = ctx.attr.aliases, output = test_binary, edition = _get_edition(ctx, toolchain), ) @@ -275,6 +279,13 @@ _rust_common_attrs = { linking a native library. """), ), + "aliases": attr.label_keyed_string_dict( + doc = _tidy(""" + Remap crates to a new name or moniker for linkage to this target + + These are other `rust_library` targets and will be presented as the new name given. + """), + ), "crate_features": attr.string_list( doc = _tidy(""" List of features to enable for this crate. diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 0839ae407a..68fb8e33ab 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -32,11 +32,19 @@ CrateInfo = provider( "root": "File: The source File entrypoint to this crate, eg. lib.rs", "srcs": "List[File]: All source Files that are part of the crate.", "deps": "List[Provider]: This crate's (rust or cc) dependencies' providers.", + "aliases": "Dict[Label, String]: Renamed and aliased crates", "output": "File: The output File that will be produced, depends on crate type.", "edition": "str: The edition of this crate.", }, ) +AliasableDep = provider( + fields = { + "name": "str", + "dep": "CrateInfo", + } +) + DepInfo = provider( fields = { "direct_crates": "depset[CrateInfo]", @@ -84,7 +92,7 @@ def get_lib_name(lib): else: return libname -def collect_deps(deps, toolchain): +def collect_deps(deps, aliases, toolchain): """ Walks through dependencies and collects the transitive dependencies. @@ -101,10 +109,17 @@ def collect_deps(deps, toolchain): transitive_crates = depset() transitive_dylibs = depset(order = "topological") # dylib link flag ordering matters. transitive_staticlibs = depset() + + aliases = {k.label: v for k,v in aliases.items()} for dep in deps: if CrateInfo in dep: # This dependency is a rust_library - direct_crates += [dep[CrateInfo]] + direct_dep = dep[CrateInfo] + aliasable_dep = AliasableDep( + name = aliases.get(dep.label, direct_dep.name), + dep = direct_dep, + ) + direct_crates += [aliasable_dep] transitive_crates = depset([dep[CrateInfo]], transitive = [transitive_crates]) transitive_crates = depset(transitive = [transitive_crates, dep[DepInfo].transitive_crates]) transitive_dylibs = depset(transitive = [transitive_dylibs, dep[DepInfo].transitive_dylibs]) @@ -197,6 +212,7 @@ def rustc_compile_action( dep_info = collect_deps( crate_info.deps, + crate_info.aliases, toolchain, ) @@ -389,7 +405,7 @@ def add_crate_link_flags(args, dep_info): ) def _crate_to_link_flag(crate_info): - return ["--extern", "{}={}".format(crate_info.name, crate_info.output.path)] + return ["--extern", "{}={}".format(crate_info.name, crate_info.dep.output.path)] def _get_crate_dirname(crate): return crate.output.dirname diff --git a/rust/private/rustdoc_test.bzl b/rust/private/rustdoc_test.bzl index db69b67690..1e90e2881c 100644 --- a/rust/private/rustdoc_test.bzl +++ b/rust/private/rustdoc_test.bzl @@ -67,7 +67,7 @@ def _build_rustdoc_test_script(toolchain, dep_info, crate): link_search_flags = [] link_flags += ["--extern=" + crate.name + "=" + crate.output.short_path] - link_flags += ["--extern=" + c.name + "=" + c.output.short_path for c in d.direct_crates.to_list()] + link_flags += ["--extern=" + c.name + "=" + c.dep.output.short_path for c in d.direct_crates.to_list()] link_search_flags += ["-Ldependency={}".format(dirname(c.output.short_path)) for c in d.transitive_crates.to_list()] link_flags += ["-ldylib=" + get_lib_name(lib) for lib in d.transitive_dylibs.to_list()]