Skip to content

Commit

Permalink
Allow deps to be renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBowyer committed Jan 6, 2020
1 parent 959ba56 commit 5cc6f80
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions proto/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
Expand Down
11 changes: 11 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
),
Expand All @@ -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),
),
Expand All @@ -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,
)
Expand All @@ -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),
)
Expand Down Expand Up @@ -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.
Expand Down
22 changes: 19 additions & 3 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand Down Expand Up @@ -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.
Expand All @@ -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])
Expand Down Expand Up @@ -197,6 +212,7 @@ def rustc_compile_action(

dep_info = collect_deps(
crate_info.deps,
crate_info.aliases,
toolchain,
)

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rust/private/rustdoc_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand Down

0 comments on commit 5cc6f80

Please sign in to comment.