diff --git a/README.md b/README.md index 783122e15f..9b4408d0fa 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@
rust_bench_test
+
+ rust_benchmark
rust_bench_test(name, deps, data, srcs, crate_features, crate_root, rustc_flags)+
rust_benchmark(name, deps, data, srcs, crate_features, crate_root, rustc_flags)
Builds a Rust benchmark test.
Warning: This rule is currently experimental. Rust Benchmark
@@ -143,7 +143,7 @@ rust_bench_test
easy to use a custom Rust toolchain, such as a nightly release.
name |
Attributesthis rule. |
deps |
AttributesThese must be |
data |
Attributesmacro. |
srcs |
Attributesis the root of the crate to be passed to rustc to build this crate. |
crate_features |
Attributeswith--cfg feature="${feature_name}" flags.
|
crate_root |
Attributesthe single file insrcs if srcs contains only one file.
|
rustc_flags |
Attributes |
Suppose you have the following directory structure for a Rust project with a
library crate, fibonacci
with benchmarks under the benches/
directory:
[workspace]/
@@ -257,18 +257,18 @@ Examples
b.iter(|| fibonacci::fibonacci(40));
}
-To build the benchmark test, simply add a rust_bench_test
target:
To build the benchmark test, simply add a rust_benchmark
target:
fibonacci/BUILD
:
package(default_visibility = ["//visibility:public"])
-load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library", "rust_bench_test")
+load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library", "rust_benchmark")
rust_library(
name = "fibonacci",
srcs = ["src/lib.rs"],
)
-rust_bench_test(
+rust_benchmark(
name = "fibonacci_bench",
srcs = ["benches/fibonacci_bench.rs"],
deps = [":fibonacci"],
diff --git a/examples/fibonacci/BUILD b/examples/fibonacci/BUILD
index 47c9a8b979..8a3dc4ed3e 100644
--- a/examples/fibonacci/BUILD
+++ b/examples/fibonacci/BUILD
@@ -4,7 +4,7 @@ load(
"@io_bazel_rules_rust//rust:rust.bzl",
"rust_library",
"rust_test",
- "rust_bench_test",
+ "rust_benchmark",
"rust_doc",
"rust_doc_test",
)
@@ -19,7 +19,7 @@ rust_test(
deps = [":fibonacci"],
)
-rust_bench_test(
+rust_benchmark(
name = "fibonacci_bench",
srcs = ["benches/fibonacci_bench.rs"],
tags = [
diff --git a/rust/rust.bzl b/rust/rust.bzl
index 0caba73ca1..e4dba0aef3 100644
--- a/rust/rust.bzl
+++ b/rust/rust.bzl
@@ -50,7 +50,7 @@ rust_repositories()
"""
load(
- ":toolchain.bzl",
+ ":rustc.bzl",
"build_rustc_command",
"build_rustdoc_command",
"build_rustdoc_test_command",
@@ -89,7 +89,7 @@ def _out_dir_setup_cmd(out_dir_tar):
if out_dir_tar:
return [
"mkdir ./out_dir/\n",
- "tar -xzf %s -C ./out_dir\n" % out_dir_tar.path,
+ "tar -xzf {} -C ./out_dir\n".format(out_dir_tar.path),
]
else:
return []
@@ -162,9 +162,9 @@ def _setup_deps(
search_flags = []
if transitive_rlibs:
- search_flags += ["-L dependency=%s" % deps_dir]
+ search_flags += ["-L dependency={}".format(deps_dir)]
if transitive_dylibs or transitive_staticlibs:
- search_flags += ["-L native=%s" % deps_dir]
+ search_flags += ["-L native={}".format(deps_dir)]
# Create symlinks pointing to each transitive lib in deps_dir.
transitive_libs = transitive_rlibs + transitive_staticlibs + transitive_dylibs
@@ -192,7 +192,7 @@ def _find_crate_root_src(srcs, file_names = ["lib.rs"]):
for src in srcs:
if src.basename in file_names:
return src
- fail("No %s source file found." % " or ".join(file_names), "srcs")
+ fail("No {} source file found.".format(" or ".join(file_names)), "srcs")
def _determine_output_hash(lib_rs):
return repr(hash(lib_rs.path))
@@ -211,8 +211,8 @@ def _determine_lib_name(name, crate_type, toolchain, lib_hash = ""):
"this crate as a rust_binary instead.")
if not extension:
- fail(("Unknown crate_type: %s. If this is a cargo-supported crate type, " +
- "please file an issue!") % crate_type)
+ fail(("Unknown crate_type: (). If this is a cargo-supported crate type, " +
+ "please file an issue!").format(crate_type))
return "lib{name}-{lib_hash}{extension}".format(
name = name,
@@ -289,8 +289,8 @@ def _rust_library_impl(ctx):
mnemonic = "Rustc",
command = cmd,
use_default_shell_env = True,
- progress_message = ("Compiling Rust library %s (%d files)" %
- (ctx.label.name, len(ctx.files.srcs))),
+ progress_message = "Compiling Rust library {} ({} files)".format(
+ ctx.label.name, len(ctx.files.srcs)),
)
return struct(
@@ -355,8 +355,8 @@ def _rust_binary_impl(ctx):
mnemonic = "Rustc",
command = cmd,
use_default_shell_env = True,
- progress_message = ("Compiling Rust binary %s (%d files)" %
- (ctx.label.name, len(ctx.files.srcs))),
+ progress_message = "Compiling Rust binary {} ({} files)".format(
+ ctx.label.name, len(ctx.files.srcs)),
)
runfiles = ctx.runfiles(
@@ -439,8 +439,8 @@ def _rust_test_common(ctx, test_binary):
mnemonic = "RustcTest",
command = cmd,
use_default_shell_env = True,
- progress_message = ("Compiling Rust test %s (%d files)" %
- (ctx.label.name, len(target.srcs))),
+ progress_message = "Compiling Rust test {} ({} files)".format(
+ ctx.label.name, len(target.srcs)),
)
return depinfo
@@ -457,21 +457,21 @@ def _rust_test_impl(ctx):
return struct(runfiles = runfiles)
-def _rust_bench_test_impl(ctx):
- """Implementation for the rust_bench_test Skylark rule."""
- rust_bench_test = ctx.outputs.executable
+def _rust_benchmark_impl(ctx):
+ """Implementation for the rust_benchmark Skylark rule."""
+ rust_benchmark = ctx.outputs.executable
test_binary = ctx.new_file(
ctx.configuration.bin_dir,
- "%s_bin" % rust_bench_test.basename,
+ "{}_bin".format(rust_benchmark.basename),
)
depinfo = _rust_test_common(ctx, test_binary)
ctx.file_action(
- output = rust_bench_test,
- content = " ".join([
- "#!/usr/bin/env bash\n",
- "set -e\n",
- "%s --bench\n" % test_binary.short_path,
+ output = rust_benchmark,
+ content = "\n".join([
+ "#!/usr/bin/env bash",
+ "set -e",
+ "{} --bench".format(test_binary.short_path),
]),
executable = True,
)
@@ -487,16 +487,15 @@ def _build_rustdoc_flags(ctx):
"""Collects the rustdoc flags."""
doc_flags = []
doc_flags += [
- "--markdown-css %s" % css.path
+ "--markdown-css {}".format(css.path)
for css in ctx.files.markdown_css
]
if hasattr(ctx.file, "html_in_header"):
- doc_flags += ["--html-in-header %s" % ctx.file.html_in_header.path]
+ doc_flags += ["--html-in-header {}".format(ctx.file.html_in_header.path)]
if hasattr(ctx.file, "html_before_content"):
- doc_flags += ["--html-before-content %s" %
- ctx.file.html_before_content.path]
+ doc_flags += ["--html-before-content {}".format(ctx.file.html_before_content.path)]
if hasattr(ctx.file, "html_after_content"):
- doc_flags += ["--html-after-content %s"]
+ doc_flags += ["--html-after-content {}".format(ctx.file.html_after_content.path)]
return doc_flags
def _rust_doc_impl(ctx):
@@ -543,8 +542,8 @@ def _rust_doc_impl(ctx):
mnemonic = "Rustdoc",
command = doc_cmd,
use_default_shell_env = True,
- progress_message = ("Generating rustdoc for %s (%d files)" %
- (target.name, len(target.srcs))),
+ progress_message = "Generating rustdoc for {} ({} files)".format(
+ target.name, len(target.srcs)),
)
def _rust_doc_test_impl(ctx):
@@ -1036,12 +1035,11 @@ Examples:
Run the test with `bazel build //hello_lib:hello_lib_test`.
"""
-rust_bench_test = rule(
- _rust_bench_test_impl,
+rust_benchmark = rule(
+ _rust_benchmark_impl,
attrs = _rust_common_attrs,
executable = True,
host_fragments = ["cpp"],
- test = True,
toolchains = ["@io_bazel_rules_rust//rust:toolchain"],
)
@@ -1134,21 +1132,21 @@ Example:
}
```
- To build the benchmark test, simply add a `rust_bench_test` target:
+ To build the benchmark test, simply add a `rust_benchmark` target:
`fibonacci/BUILD`:
```python
package(default_visibility = ["//visibility:public"])
- load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library", "rust_bench_test")
+ load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library", "rust_benchmark")
rust_library(
name = "fibonacci",
srcs = ["src/lib.rs"],
)
- rust_bench_test(
+ rust_benchmark(
name = "fibonacci_bench",
srcs = ["benches/fibonacci_bench.rs"],
deps = [":fibonacci"],
diff --git a/rust/rustc.bzl b/rust/rustc.bzl
new file mode 100644
index 0000000000..6dbdb98dc4
--- /dev/null
+++ b/rust/rustc.bzl
@@ -0,0 +1,203 @@
+"""
+Toolchain rules used by Rust.
+"""
+
+load(":utils.bzl", "relative_path")
+
+ZIP_PATH = "/usr/bin/zip"
+
+def _get_rustc_env(ctx):
+ version = ctx.attr.version if hasattr(ctx.attr, "version") else "0.0.0"
+ v1, v2, v3 = version.split(".")
+ if "-" in v3:
+ v3, pre = v3.split("-")
+ else:
+ pre = ""
+ return [
+ "CARGO_PKG_VERSION=" + version,
+ "CARGO_PKG_VERSION_MAJOR=" + v1,
+ "CARGO_PKG_VERSION_MINOR=" + v2,
+ "CARGO_PKG_VERSION_PATCH=" + v3,
+ "CARGO_PKG_VERSION_PRE=" + pre,
+ "CARGO_PKG_AUTHORS=",
+ "CARGO_PKG_NAME=" + ctx.label.name,
+ "CARGO_PKG_DESCRIPTION=",
+ "CARGO_PKG_HOMEPAGE=",
+ ]
+
+def _get_comp_mode_codegen_opts(ctx, toolchain):
+ comp_mode = ctx.var["COMPILATION_MODE"]
+ if not comp_mode in toolchain.compilation_mode_opts:
+ fail("Unrecognized compilation mode %s for toolchain." % comp_mode)
+
+ return toolchain.compilation_mode_opts[comp_mode]
+
+# Utility methods that use the toolchain provider.
+def build_rustc_command(
+ ctx,
+ toolchain,
+ crate_name,
+ crate_type,
+ src,
+ output_dir,
+ depinfo,
+ output_hash = None,
+ rust_flags = []):
+ """
+ Constructs the rustc command used to build the current target.
+ """
+
+ # Paths to cc (for linker) and ar
+ cpp_fragment = ctx.host_fragments.cpp
+ cc = cpp_fragment.compiler_executable
+ ar = cpp_fragment.ar_executable
+
+ # Currently, the CROSSTOOL config for darwin sets ar to "libtool". Because
+ # rust uses ar-specific flags, use /usr/bin/ar in this case.
+ # TODO(dzc): This is not ideal. Remove this workaround once ar_executable
+ # always points to an ar binary.
+ ar_str = "%s" % ar
+ if ar_str.find("libtool", 0) != -1:
+ ar = "/usr/bin/ar"
+
+ rpaths = _compute_rpaths(toolchain, ctx.bin_dir, output_dir, depinfo)
+
+ # Construct features flags
+ features_flags = _get_features_flags(ctx.attr.crate_features)
+
+ extra_filename = ""
+ if output_hash:
+ extra_filename = "-%s" % output_hash
+
+ codegen_opts = _get_comp_mode_codegen_opts(ctx, toolchain)
+
+ return " ".join(
+ ["set -e;"] +
+ # If TMPDIR is set but not created, rustc will die.
+ ['if [ ! -z "${TMPDIR+x}" ]; then mkdir -p $TMPDIR; fi;'] + depinfo.setup_cmd +
+ _out_dir_setup_cmd(ctx.file.out_dir_tar) +
+ _get_rustc_env(ctx) + [
+ "LD_LIBRARY_PATH=%s" % _get_path_str(_get_dir_names(toolchain.rustc_lib)),
+ "DYLD_LIBRARY_PATH=%s" % _get_path_str(_get_dir_names(toolchain.rustc_lib)),
+ "OUT_DIR=$(pwd)/out_dir",
+ toolchain.rustc.path,
+ src.path,
+ "--crate-name %s" % crate_name,
+ "--crate-type %s" % crate_type,
+ "--codegen opt-level=%s" % codegen_opts.opt_level,
+ "--codegen debuginfo=%s" % codegen_opts.debug_info,
+ # Disambiguate this crate from similarly named ones
+ "--codegen metadata=%s" % extra_filename,
+ "--codegen extra-filename='%s'" % extra_filename,
+ "--codegen ar=%s" % ar,
+ "--codegen linker=%s" % cc,
+ "--codegen link-args='%s'" % " ".join(cpp_fragment.link_options),
+ "--out-dir %s" % output_dir,
+ "--emit=dep-info,link",
+ "--color always",
+ ] + ["--codegen link-arg='-Wl,-rpath={}'".format(rpath) for rpath in rpaths] +
+ features_flags +
+ rust_flags +
+ ["-L all=%s" % dir for dir in _get_dir_names(toolchain.rust_lib)] +
+ depinfo.search_flags +
+ depinfo.link_flags +
+ ctx.attr.rustc_flags,
+ )
+
+def build_rustdoc_command(ctx, toolchain, rust_doc_zip, depinfo, lib_rs, target, doc_flags):
+ """
+ Constructs the rustdoc command used to build the current target.
+ """
+
+ docs_dir = rust_doc_zip.dirname + "/_rust_docs"
+ return " ".join(
+ ["set -e;"] +
+ depinfo.setup_cmd + [
+ "rm -rf %s;" % docs_dir,
+ "mkdir %s;" % docs_dir,
+ "LD_LIBRARY_PATH=%s" % _get_path_str(_get_dir_names(toolchain.rustc_lib)),
+ "DYLD_LIBRARY_PATH=%s" % _get_path_str(_get_dir_names(toolchain.rustc_lib)),
+ toolchain.rust_doc.path,
+ lib_rs.path,
+ "--crate-name %s" % target.name,
+ ] + ["-L all=%s" % dir for dir in _get_dir_names(toolchain.rust_lib)] + [
+ "-o %s" % docs_dir,
+ ] + doc_flags +
+ depinfo.search_flags +
+ # rustdoc can't do anything with native link flags, and blows up on them
+ [f for f in depinfo.link_flags if f.startswith("--extern")] +
+ [
+ "&&",
+ "(cd %s" % docs_dir,
+ "&&",
+ ZIP_PATH,
+ "-qR",
+ rust_doc_zip.basename,
+ "$(find . -type f) )",
+ "&&",
+ "mv %s/%s %s" % (docs_dir, rust_doc_zip.basename, rust_doc_zip.path),
+ ],
+ )
+
+def build_rustdoc_test_command(ctx, toolchain, depinfo, lib_rs):
+ """
+ Constructs the rustdocc command used to test the current target.
+ """
+ return " ".join(
+ ["#!/usr/bin/env bash\n"] + ["set -e\n"] + depinfo.setup_cmd + [
+ "LD_LIBRARY_PATH=%s" % _get_path_str(_get_dir_names(toolchain.rustc_lib)),
+ "DYLD_LIBRARY_PATH=%s" % _get_path_str(_get_dir_names(toolchain.rustc_lib)),
+ toolchain.rust_doc.path,
+ ] + ["-L all=%s" % dir for dir in _get_dir_names(toolchain.rust_lib)] + [
+ lib_rs.path,
+ ] + depinfo.search_flags +
+ depinfo.link_flags,
+ )
+
+def _compute_rpaths(toolchain, bin_dir, output_dir, depinfo):
+ """
+ Determine the artifact's rpaths relative to the bazel root
+ for runtime linking of shared libraries.
+ """
+ if not depinfo.transitive_dylibs:
+ return []
+ if toolchain.os != "linux":
+ fail("Runtime linking is not supported on {}, but found {}".format(
+ toolchain.os,
+ depinfo.transitive_dylibs,
+ ))
+
+ # Multiple dylibs can be present in the same directory, so deduplicate them.
+ return [
+ "$ORIGIN/" + relative_path(output_dir, lib_dir)
+ for lib_dir in _get_dir_names(depinfo.transitive_dylibs)
+ ]
+
+def _get_features_flags(features):
+ """
+ Constructs a string containing the feature flags from the features specified
+ in the features attribute.
+ """
+ features_flags = []
+ for feature in features:
+ features_flags += ["--cfg feature=\\\"%s\\\"" % feature]
+ return features_flags
+
+def _get_dir_names(files):
+ dirs = {}
+ for f in files:
+ dirs[f.dirname] = None
+ return dirs.keys()
+
+def _get_path_str(dirs):
+ return ":".join(dirs)
+
+def _out_dir_setup_cmd(out_dir_tar):
+ if out_dir_tar:
+ return [
+ "mkdir ./out_dir/\n",
+ "tar -xzf %s -C ./out_dir\n" % out_dir_tar.path,
+ ]
+ else:
+ return []
+
diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl
index 1e82e5ebdd..43f93492f9 100644
--- a/rust/toolchain.bzl
+++ b/rust/toolchain.bzl
@@ -1,234 +1,24 @@
"""
-Toolchain rules used by Rust.
+The rust_toolchain rule definition and implementation.
"""
-load(":utils.bzl", "relative_path")
+def _get_files(labels):
+ return [f for l in labels for f in getattr(l, "files", [])]
-ZIP_PATH = "/usr/bin/zip"
-
-def _get_rustc_env(ctx):
- version = ctx.attr.version if hasattr(ctx.attr, "version") else "0.0.0"
- v1, v2, v3 = version.split(".")
- if "-" in v3:
- v3, pre = v3.split("-")
- else:
- pre = ""
- return [
- "CARGO_PKG_VERSION=" + version,
- "CARGO_PKG_VERSION_MAJOR=" + v1,
- "CARGO_PKG_VERSION_MINOR=" + v2,
- "CARGO_PKG_VERSION_PATCH=" + v3,
- "CARGO_PKG_VERSION_PRE=" + pre,
- "CARGO_PKG_AUTHORS=",
- "CARGO_PKG_NAME=" + ctx.label.name,
- "CARGO_PKG_DESCRIPTION=",
- "CARGO_PKG_HOMEPAGE=",
- ]
-
-def _get_comp_mode_codegen_opts(ctx, toolchain):
- comp_mode = ctx.var["COMPILATION_MODE"]
- if not comp_mode in toolchain.compilation_mode_opts:
- fail("Unrecognized compilation mode %s for toolchain." % comp_mode)
-
- return toolchain.compilation_mode_opts[comp_mode]
-
-# Utility methods that use the toolchain provider.
-def build_rustc_command(
- ctx,
- toolchain,
- crate_name,
- crate_type,
- src,
- output_dir,
- depinfo,
- output_hash = None,
- rust_flags = []):
- """
- Constructs the rustc command used to build the current target.
- """
-
- # Paths to cc (for linker) and ar
- cpp_fragment = ctx.host_fragments.cpp
- cc = cpp_fragment.compiler_executable
- ar = cpp_fragment.ar_executable
-
- # Currently, the CROSSTOOL config for darwin sets ar to "libtool". Because
- # rust uses ar-specific flags, use /usr/bin/ar in this case.
- # TODO(dzc): This is not ideal. Remove this workaround once ar_executable
- # always points to an ar binary.
- ar_str = "%s" % ar
- if ar_str.find("libtool", 0) != -1:
- ar = "/usr/bin/ar"
-
- rpaths = _compute_rpaths(toolchain, ctx.bin_dir, output_dir, depinfo)
-
- # Construct features flags
- features_flags = _get_features_flags(ctx.attr.crate_features)
-
- extra_filename = ""
- if output_hash:
- extra_filename = "-%s" % output_hash
-
- codegen_opts = _get_comp_mode_codegen_opts(ctx, toolchain)
-
- return " ".join(
- ["set -e;"] +
- # If TMPDIR is set but not created, rustc will die.
- ['if [ ! -z "${TMPDIR+x}" ]; then mkdir -p $TMPDIR; fi;'] + depinfo.setup_cmd +
- _out_dir_setup_cmd(ctx.file.out_dir_tar) +
- _get_rustc_env(ctx) + [
- "LD_LIBRARY_PATH=%s" % _get_path_str(_get_dir_names(toolchain.rustc_lib)),
- "DYLD_LIBRARY_PATH=%s" % _get_path_str(_get_dir_names(toolchain.rustc_lib)),
- "OUT_DIR=$(pwd)/out_dir",
- toolchain.rustc.path,
- src.path,
- "--crate-name %s" % crate_name,
- "--crate-type %s" % crate_type,
- "--codegen opt-level=%s" % codegen_opts.opt_level,
- "--codegen debuginfo=%s" % codegen_opts.debug_info,
- # Disambiguate this crate from similarly named ones
- "--codegen metadata=%s" % extra_filename,
- "--codegen extra-filename='%s'" % extra_filename,
- "--codegen ar=%s" % ar,
- "--codegen linker=%s" % cc,
- "--codegen link-args='%s'" % " ".join(cpp_fragment.link_options),
- "--out-dir %s" % output_dir,
- "--emit=dep-info,link",
- "--color always",
- ] + ["--codegen link-arg='-Wl,-rpath={}'".format(rpath) for rpath in rpaths] +
- features_flags +
- rust_flags +
- ["-L all=%s" % dir for dir in _get_dir_names(toolchain.rust_lib)] +
- depinfo.search_flags +
- depinfo.link_flags +
- ctx.attr.rustc_flags,
- )
-
-def build_rustdoc_command(ctx, toolchain, rust_doc_zip, depinfo, lib_rs, target, doc_flags):
- """
- Constructs the rustdoc command used to build the current target.
- """
-
- docs_dir = rust_doc_zip.dirname + "/_rust_docs"
- return " ".join(
- ["set -e;"] +
- depinfo.setup_cmd + [
- "rm -rf %s;" % docs_dir,
- "mkdir %s;" % docs_dir,
- "LD_LIBRARY_PATH=%s" % _get_path_str(_get_dir_names(toolchain.rustc_lib)),
- "DYLD_LIBRARY_PATH=%s" % _get_path_str(_get_dir_names(toolchain.rustc_lib)),
- toolchain.rust_doc.path,
- lib_rs.path,
- "--crate-name %s" % target.name,
- ] + ["-L all=%s" % dir for dir in _get_dir_names(toolchain.rust_lib)] + [
- "-o %s" % docs_dir,
- ] + doc_flags +
- depinfo.search_flags +
- # rustdoc can't do anything with native link flags, and blows up on them
- [f for f in depinfo.link_flags if f.startswith("--extern")] +
- [
- "&&",
- "(cd %s" % docs_dir,
- "&&",
- ZIP_PATH,
- "-qR",
- rust_doc_zip.basename,
- "$(find . -type f) )",
- "&&",
- "mv %s/%s %s" % (docs_dir, rust_doc_zip.basename, rust_doc_zip.path),
- ],
- )
-
-def build_rustdoc_test_command(ctx, toolchain, depinfo, lib_rs):
- """
- Constructs the rustdocc command used to test the current target.
- """
- return " ".join(
- ["#!/usr/bin/env bash\n"] + ["set -e\n"] + depinfo.setup_cmd + [
- "LD_LIBRARY_PATH=%s" % _get_path_str(_get_dir_names(toolchain.rustc_lib)),
- "DYLD_LIBRARY_PATH=%s" % _get_path_str(_get_dir_names(toolchain.rustc_lib)),
- toolchain.rust_doc.path,
- ] + ["-L all=%s" % dir for dir in _get_dir_names(toolchain.rust_lib)] + [
- lib_rs.path,
- ] + depinfo.search_flags +
- depinfo.link_flags,
- )
-
-def _compute_rpaths(toolchain, bin_dir, output_dir, depinfo):
- """
- Determine the artifact's rpaths relative to the bazel root
- for runtime linking of shared libraries.
- """
- if not depinfo.transitive_dylibs:
- return []
- if toolchain.os != "linux":
- fail("Runtime linking is not supported on {}, but found {}".format(
- toolchain.os,
- depinfo.transitive_dylibs,
- ))
-
- # Multiple dylibs can be present in the same directory, so deduplicate them.
- return depset([
- "$ORIGIN/" + relative_path(output_dir, dylib.dirname)
- for dylib in depinfo.transitive_dylibs
- ])
-
-def _get_features_flags(features):
- """
- Constructs a string containing the feature flags from the features specified
- in the features attribute.
- """
- features_flags = []
- for feature in features:
- features_flags += ["--cfg feature=\\\"%s\\\"" % feature]
- return features_flags
-
-def _get_dir_names(files):
- dirs = {}
- for f in files:
- dirs[f.dirname] = None
- return dirs.keys()
-
-def _get_path_str(dirs):
- return ":".join(dirs)
-
-def _get_first_file(input):
- if hasattr(input, "files"):
- for f in input.files:
- return f
- return input
-
-def _get_files(input):
- files = []
- for i in input:
- if hasattr(i, "files"):
- files += [f for f in i.files]
- return files
-
-def _out_dir_setup_cmd(out_dir_tar):
- if out_dir_tar:
- return [
- "mkdir ./out_dir/\n",
- "tar -xzf %s -C ./out_dir\n" % out_dir_tar.path,
- ]
- else:
- return []
-
-# The rust_toolchain rule definition and implementation.
def _rust_toolchain_impl(ctx):
compilation_mode_opts = {}
for k, v in ctx.attr.opt_level.items():
if not k in ctx.attr.debug_info:
- fail("Compilation mode %s is not defined in debug_info but is defined opt_level" % k)
+ fail("Compilation mode {} is not defined in debug_info but is defined opt_level".format(k))
compilation_mode_opts[k] = struct(debug_info = ctx.attr.debug_info[k], opt_level = v)
for k, v in ctx.attr.debug_info.items():
if not k in ctx.attr.opt_level:
- fail("Compilation mode %s is not defined in opt_level but is defined debug_info" % k)
+ fail("Compilation mode {} is not defined in opt_level but is defined debug_info".format(k))
toolchain = platform_common.ToolchainInfo(
- rustc = _get_first_file(ctx.attr.rustc),
- rust_doc = _get_first_file(ctx.attr.rust_doc),
+ 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),
staticlib_ext = ctx.attr.staticlib_ext,
@@ -242,8 +32,8 @@ def _rust_toolchain_impl(ctx):
rust_toolchain = rule(
_rust_toolchain_impl,
attrs = {
- "rustc": attr.label(allow_files = True),
- "rust_doc": attr.label(allow_files = True),
+ "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),
"staticlib_ext": attr.string(mandatory = True),
@@ -287,20 +77,26 @@ Example:
load('@io_bazel_rules_rust//rust:toolchain.bzl', 'rust_toolchain')
toolchain(
- name="rust_cpuX",
+ name = "rust_cpuX",
exec_compatible_with = [
"@bazel_tools//platforms:cpuX",
],
target_compatible_with = [
"@bazel_tools//platforms:cpuX",
],
- toolchain = ":rust_cpuX_impl")
+ toolchain = ":rust_cpuX_impl",
+ )
+
rust_toolchain(
- name="rust_cpuX_impl",
- rustc="@rust_cpuX//:rustc",
- rustc_lib=["@rust_cpuX//:rustc_lib"],
- rust_lib=["@rust_cpuX//:rust_lib"],
- rust_doc="@rust_cpuX//:rustdoc")
+ name = "rust_cpuX_impl",
+ rustc = "@rust_cpuX//:rustc",
+ rustc_lib = ["@rust_cpuX//:rustc_lib"],
+ rust_lib = ["@rust_cpuX//:rust_lib"],
+ rust_doc = "@rust_cpuX//:rustdoc",
+ staticlib_ext = ".a",
+ dylib_ext = ".so",
+ os = "linux",
+ )
```
Then, either add the label of the toolchain rule to register_toolchains in the WORKSPACE, or pass