Skip to content

Commit

Permalink
Add edition support to rules and toolchain. (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfarrugi committed Dec 8, 2018
1 parent db81b42 commit 26a7529
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/hello_runfiles/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ load(

rust_binary(
name = "hello_runfiles",
edition = "2018",
srcs = ["hello_runfiles.rs"],
data = ["hello_runfiles.rs"], # Yes, we're being cute.
deps = ["@io_bazel_rules_rust//tools/runfiles"],
Expand Down
5 changes: 1 addition & 4 deletions examples/hello_runfiles/hello_runfiles.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate runfiles;

use std::io::prelude::*;
use std::fs::File;

Expand All @@ -13,6 +11,5 @@ fn main() {
let mut buffer = String::new();
f.read_to_string(&mut buffer).unwrap();

assert_eq!(buffer.len(), 427);
println!("This program's source is:\n```\n{}\n```", buffer);
println!("This program's source is {} characters long.", buffer.len());
}
3 changes: 3 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ _rust_common_attrs = {
),
"deps": attr.label_list(),
"crate_features": attr.string_list(),
"edition": attr.string(
doc="The rust edition to use for this crate.",
),
"rustc_flags": attr.string_list(),
"version": attr.string(default = "0.0.0"),
"out_dir_tar": attr.label(
Expand Down
7 changes: 7 additions & 0 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def rustc_compile_action(
if hasattr(ctx.attr, "crate_features"):
args.add_all(getattr(ctx.attr, "crate_features"), before_each = "--cfg", format_each = 'feature="%s"')
args.add_all(rust_flags)
args.add("--edition", _get_edition(ctx, toolchain))
args.add_all(getattr(ctx.attr, "rustc_flags", []))

# Link!
Expand Down Expand Up @@ -263,6 +264,12 @@ def rustc_compile_action(
),
]

def _get_edition(ctx, toolchain):
if getattr(ctx.attr, "edition"):
return ctx.attr.edition
else:
return toolchain.default_edition

def _create_out_dir_action(ctx):
tar_file = getattr(ctx.file, "out_dir_tar", None)
if not tar_file:
Expand Down
5 changes: 5 additions & 0 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def _rust_toolchain_impl(ctx):
target_triple = ctx.attr.target_triple,
exec_triple = ctx.attr.exec_triple,
os = ctx.attr.os,
default_edition = ctx.attr.default_edition,
compilation_mode_opts = compilation_mode_opts,
crosstool_files = ctx.files._crosstool,
)
Expand All @@ -40,6 +41,10 @@ rust_toolchain = rule(
"staticlib_ext": attr.string(mandatory = True),
"dylib_ext": attr.string(mandatory = True),
"os": attr.string(mandatory = True),
"default_edition": attr.string(
doc = "The edition to use for rust_* rules that don't specify an edition.",
default = "2015",
),
"exec_triple": attr.string(),
"target_triple": attr.string(),
"_crosstool": attr.label(
Expand Down

0 comments on commit 26a7529

Please sign in to comment.