Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More bazel --incompatible fixes (#161) #169

Merged
merged 5 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion proto/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ RustProtoProvider = provider(

def _compute_proto_source_path(file, source_root_attr):
"""Take the short path of file and make it suitable for protoc."""

# For proto, they need to be requested with their absolute name to be
# compatible with the descriptor_set passed by proto_library.
# I.e. if you compile a protobuf at @repo1//package:file.proto, the proto
Expand Down Expand Up @@ -104,7 +105,7 @@ def _gen_lib(ctx, grpc, srcs, lib):
if grpc:
content.append("extern crate grpc;")
content.append("extern crate tls_api;")
for f in srcs:
for f in srcs.to_list():
content.append("pub mod %s;" % _file_stem(f))
content.append("pub use %s::*;" % _file_stem(f))
if grpc:
Expand Down
2 changes: 1 addition & 1 deletion proto/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def rust_generate_proto(

if not protos:
fail("Protobuf compilation requested without inputs!")
paths = ["%s/%s" % (output_dir, file_stem(i)) for i in protos]
paths = ["%s/%s" % (output_dir, file_stem(i)) for i in protos.to_list()]
outs = [ctx.actions.declare_file(path + ".rs") for path in paths]
output_directory = outs[0].dirname

Expand Down
37 changes: 17 additions & 20 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ CrateInfo = provider(
DepInfo = provider(
fields = {
"direct_crates": "depset[CrateInfo]",
"indirect_crates": "depset[CrateInfo]",
"transitive_crates": "depset[CrateInfo]",
"transitive_dylibs": "depset[File]",
"transitive_staticlibs": "depset[File]",
Expand Down Expand Up @@ -102,30 +101,26 @@ def collect_deps(deps, toolchain):
if CrateInfo in dep:
# This dependency is a rust_library
direct_crates += [dep[CrateInfo]]
transitive_crates += [dep[CrateInfo]]
transitive_crates += dep[DepInfo].transitive_crates
transitive_dylibs += dep[DepInfo].transitive_dylibs
transitive_staticlibs += dep[DepInfo].transitive_staticlibs
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])
transitive_staticlibs = depset(transitive = [transitive_staticlibs, dep[DepInfo].transitive_staticlibs])
elif hasattr(dep, "cc"):
# This dependency is a cc_library
dylibs = [l for l in dep.cc.libs if l.basename.endswith(toolchain.dylib_ext)]
staticlibs = [l for l in dep.cc.libs if l.basename.endswith(toolchain.staticlib_ext)]
transitive_dylibs += dylibs
transitive_staticlibs += staticlibs
dylibs = [l for l in dep.cc.libs.to_list() if l.basename.endswith(toolchain.dylib_ext)]
staticlibs = [l for l in dep.cc.libs.to_list() if l.basename.endswith(toolchain.staticlib_ext)]
transitive_dylibs = depset(transitive = [transitive_dylibs, depset(dylibs)])
transitive_staticlibs = depset(transitive = [transitive_staticlibs, depset(staticlibs)])
else:
fail("rust targets can only depend on rust_library, rust_*_library or cc_library targets." + str(dep), "deps")

crate_list = transitive_crates.to_list()
transitive_libs = depset(
[c.output for c in crate_list],
[c.output for c in transitive_crates.to_list()],
transitive = [transitive_staticlibs, transitive_dylibs],
)

indirect_crates = depset([crate for crate in crate_list if crate not in direct_crates])

return DepInfo(
direct_crates = depset(direct_crates),
indirect_crates = indirect_crates,
transitive_crates = transitive_crates,
transitive_dylibs = transitive_dylibs,
transitive_staticlibs = transitive_staticlibs,
Expand Down Expand Up @@ -186,14 +181,16 @@ def rustc_compile_action(
toolchain,
)

compile_inputs = (
compile_inputs = depset(
crate_info.srcs +
getattr(ctx.files, "data", []) +
dep_info.transitive_libs +
[toolchain.rustc] +
toolchain.rustc_lib +
toolchain.rust_lib +
toolchain.crosstool_files
toolchain.crosstool_files,
transitive = [
toolchain.rustc_lib.files,
toolchain.rust_lib.files,
],
)

args = ctx.actions.args()
Expand Down Expand Up @@ -232,7 +229,7 @@ def rustc_compile_action(
# We awkwardly construct this command because we cannot reference $PWD from ctx.actions.run(executable=toolchain.rustc)
out_dir = _create_out_dir_action(ctx)
if out_dir:
compile_inputs.append(out_dir)
compile_inputs = depset([out_dir], transitive = [compile_inputs])
out_dir_env = "OUT_DIR=$(pwd)/{} ".format(out_dir.path)
else:
out_dir_env = ""
Expand Down Expand Up @@ -294,7 +291,7 @@ def _compute_rpaths(toolchain, output_dir, dep_info):
# Multiple dylibs can be present in the same directory, so deduplicate them.
return depset([
relative_path(output_dir, lib_dir)
for lib_dir in _get_dir_names(dep_info.transitive_dylibs)
for lib_dir in _get_dir_names(dep_info.transitive_dylibs.to_list())
])

def _get_dir_names(files):
Expand Down
14 changes: 8 additions & 6 deletions rust/private/rustdoc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ def _rust_doc_impl(ctx):

toolchain = find_toolchain(ctx)

rustdoc_inputs = (
rustdoc_inputs = depset(
crate.srcs +
[c.output for c in dep_info.transitive_crates] +
[toolchain.rust_doc] +
toolchain.rustc_lib +
toolchain.rust_lib
[c.output for c in dep_info.transitive_crates.to_list()] +
[toolchain.rust_doc],
transitive = [
toolchain.rustc_lib.files,
toolchain.rust_lib.files,
],
)

output_dir = ctx.actions.declare_directory(ctx.label.name)
args = ctx.actions.args()
args.add(crate.root.path)
args.add("--crate-name", crate.name)
args.add("--output", output_dir)
args.add("--output", output_dir.path)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a separate breakage


# nb. rustdoc can't do anything with native link flags; we must omit them.
add_crate_link_flags(args, dep_info)
Expand Down
21 changes: 12 additions & 9 deletions rust/private/rustdoc_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,21 @@ def _rust_doc_test_impl(ctx):
)

# The test script compiles the crate and runs it, so it needs both compile and runtime inputs.
compile_inputs = (
compile_inputs = depset(
crate.srcs +
[crate.output] +
dep_info.transitive_libs +
[toolchain.rust_doc] +
[toolchain.rustc] +
toolchain.rustc_lib +
toolchain.rust_lib +
toolchain.crosstool_files
toolchain.crosstool_files,
transitive = [
toolchain.rustc_lib.files,
toolchain.rust_lib.files,
],
)

runfiles = ctx.runfiles(
files = compile_inputs,
files = compile_inputs.to_list(),
collect_data = True,
)
return struct(runfiles = runfiles)
Expand All @@ -64,14 +66,15 @@ def _build_rustdoc_test_script(toolchain, dep_info, crate):
link_flags = []
link_search_flags = []

# TODO: This should be able to use ctx.actions.Args()
link_flags += ["--extern " + crate.name + "=" + crate.output.short_path]
link_flags += ["--extern " + c.name + "=" + c.output.short_path for c in d.direct_crates]
link_search_flags += ["-Ldependency={}".format(dirname(c.output.short_path)) for c in d.transitive_crates]
link_flags += ["--extern " + c.name + "=" + c.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()]
link_search_flags += ["-Lnative={}".format(dirname(lib.short_path)) for lib in d.transitive_dylibs]
link_search_flags += ["-Lnative={}".format(dirname(lib.short_path)) for lib in d.transitive_dylibs.to_list()]
link_flags += ["-lstatic=" + get_lib_name(lib) for lib in d.transitive_staticlibs.to_list()]
link_search_flags += ["-Lnative={}".format(dirname(lib.short_path)) for lib in d.transitive_staticlibs]
link_search_flags += ["-Lnative={}".format(dirname(lib.short_path)) for lib in d.transitive_staticlibs.to_list()]

return """\
#!/usr/bin/env bash
Expand Down
4 changes: 2 additions & 2 deletions rust/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ def BUILD_for_rust_toolchain(workspace_name, name, exec_triple, target_triple):
rust_toolchain(
name = "{toolchain_name}_impl",
rust_doc = "@{workspace_name}//:rustdoc",
rust_lib = ["@{workspace_name}//:rust_lib-{target_triple}"],
rust_lib = "@{workspace_name}//:rust_lib-{target_triple}",
rustc = "@{workspace_name}//:rustc",
rustc_lib = ["@{workspace_name}//:rustc_lib"],
rustc_lib = "@{workspace_name}//:rustc_lib",
staticlib_ext = "{staticlib_ext}",
dylib_ext = "{dylib_ext}",
os = "{system}",
Expand Down
29 changes: 18 additions & 11 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
The rust_toolchain rule definition and implementation.
"""

def _get_files(labels):
return [f for l in labels for f in getattr(l, "files", [])]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not clear why this needed --incompatible_depset_is_not_iterable=false to work, but I refactored it out.


def _rust_toolchain_impl(ctx):
compilation_mode_opts = {}
for k, v in ctx.attr.opt_level.items():
Expand All @@ -18,8 +15,8 @@ def _rust_toolchain_impl(ctx):
toolchain = platform_common.ToolchainInfo(
rustc = ctx.file.rustc,
rust_doc = ctx.file.rust_doc,
rustc_lib = _get_files(ctx.attr.rustc_lib),
rust_lib = _get_files(ctx.attr.rust_lib),
rustc_lib = ctx.attr.rustc_lib,
rust_lib = ctx.attr.rust_lib,
staticlib_ext = ctx.attr.staticlib_ext,
dylib_ext = ctx.attr.dylib_ext,
target_triple = ctx.attr.target_triple,
Expand All @@ -33,10 +30,20 @@ def _rust_toolchain_impl(ctx):
rust_toolchain = rule(
_rust_toolchain_impl,
attrs = {
"rustc": attr.label(allow_single_file = True),
"rust_doc": attr.label(allow_single_file = True),
"rustc_lib": attr.label_list(allow_files = True),
"rust_lib": attr.label_list(allow_files = True),
"rustc": attr.label(
doc = "The rustc executable.",
allow_single_file = True,
),
"rust_doc": attr.label(
doc = "The rustdoc executable.",
allow_single_file = True,
),
"rustc_lib": attr.label(
doc = "Libraries used by rustc at runtime.",
),
"rust_lib": attr.label(
doc = "The rust standard library.",
),
"staticlib_ext": attr.string(mandatory = True),
"dylib_ext": attr.string(mandatory = True),
"os": attr.string(mandatory = True),
Expand Down Expand Up @@ -91,8 +98,8 @@ Example:
rust_toolchain(
name = "rust_cpuX_impl",
rustc = "@rust_cpuX//:rustc",
rustc_lib = ["@rust_cpuX//:rustc_lib"],
rust_lib = ["@rust_cpuX//:rust_lib"],
rustc_lib = "@rust_cpuX//:rustc_lib",
rust_lib = "@rust_cpuX//:rust_lib",
rust_doc = "@rust_cpuX//:rustdoc",
staticlib_ext = ".a",
dylib_ext = ".so",
Expand Down