From f8ad07217c8aee3e471a97b8b9675eb85de04b48 Mon Sep 17 00:00:00 2001
From: Marco Farrugia
Date: Sat, 20 Oct 2018 23:34:09 +0000
Subject: [PATCH] Look for ${crate_name}.rs as a crate_root by default. (#137)
---
README.md | 26 ++++++++++-----------
examples/ffi/rust_calling_c/BUILD | 3 +--
examples/ffi/rust_calling_c/src/lib.rs | 19 ---------------
examples/ffi/rust_calling_c/src/matrix.rs | 5 +++-
rust/private/rust.bzl | 28 +++++++++++++----------
5 files changed, 34 insertions(+), 47 deletions(-)
delete mode 100644 examples/ffi/rust_calling_c/src/lib.rs
diff --git a/README.md b/README.md
index cd3b11ece8..b7d2a3f751 100644
--- a/README.md
+++ b/README.md
@@ -94,7 +94,7 @@ rust_library(name, srcs, crate_root, crate_type, deps, data, crate_features, rus
library.
If srcs
contains more than one file, then there must be
- a file either named lib.rs
. Otherwise,
+ a file either named lib.rs
or ${name}.rs
. Otherwise,
crate_root
must be set to the source file that is the
root of the crate to be passed to rustc
to build this
crate.
@@ -111,8 +111,8 @@ rust_library(name, srcs, crate_root, crate_type, deps, data, crate_features, rus
If crate_root
is not set, then this rule will look for
- a lib.rs
file or the single file in srcs
- if srcs
contains only one file.
+ a lib.rs
file, or the single file in srcs
+ if srcs
contains only one file, or a ${name}.rs
file.
@@ -302,7 +302,7 @@ rust_binary(name, srcs, deps, data, crate_features, rustc_flags, version, out_di
binary.
If srcs
contains more than one file, then there must be
- a file either named main.rs
. Otherwise,
+ a file either named main.rs
or ${name}.rs
. Otherwise,
crate_root
must be set to the source file that is the
root of the crate to be passed to rustc
to build this
crate.
@@ -319,8 +319,8 @@ rust_binary(name, srcs, deps, data, crate_features, rustc_flags, version, out_di
If crate_root
is not set, then this rule will look for
- a main.rs
file or the single file in srcs
- if srcs
contains only one file.
+ a main.rs
file, or the single file in srcs
+ if srcs
contains only one file, or a ${name}.rs
file.
@@ -512,7 +512,7 @@ rust_test(name, srcs, deps, data, crate_features, rustc_flags, version, out_dir_
library.
If srcs
contains more than one file, then there must be
- a file either named lib.rs
. Otherwise,
+ a file either named lib.rs
or ${name}.rs
. Otherwise,
crate_root
must be set to the source file that is the
root of the crate to be passed to rustc
to build this
crate.
@@ -529,8 +529,8 @@ rust_test(name, srcs, deps, data, crate_features, rustc_flags, version, out_dir_
If crate_root
is not set, then this rule will look for
- a lib.rs
file or the single file in srcs
- if srcs
contains only one file.
+ a lib.rs
file, or the single file in srcs
+ if srcs
contains only one file, or a ${name}.rs
file.
@@ -728,7 +728,7 @@ rust_test(
Run the test with `bazel build //hello_lib:hello_lib_test`.
-## rust\_bench\_test
+## rust\_benchmark
```python
rust_benchmark(name, srcs, deps, data, crate_features, rustc_flags, out_dir_tar)
@@ -773,7 +773,7 @@ easy to use a custom Rust toolchain, such as a nightly release.
library.
If srcs
contains more than one file, then there must be
- a file either named lib.rs
. Otherwise,
+ a file either named lib.rs
or ${name}.rs
. Otherwise,
crate_root
must be set to the source file that is the
root of the crate to be passed to rustc
to build this
crate.
@@ -790,8 +790,8 @@ easy to use a custom Rust toolchain, such as a nightly release.
If crate_root
is not set, then this rule will look for
- a lib.rs
file or the single file in srcs
- if srcs
contains only one file.
+ a lib.rs
file, or the single file in srcs
+ if srcs
contains only one file, or a ${name}.rs
file.
diff --git a/examples/ffi/rust_calling_c/BUILD b/examples/ffi/rust_calling_c/BUILD
index a36a3a512a..eb49a4dfbc 100644
--- a/examples/ffi/rust_calling_c/BUILD
+++ b/examples/ffi/rust_calling_c/BUILD
@@ -6,7 +6,6 @@ rust_library(
name = "matrix",
srcs = [
"src/ffi.rs",
- "src/lib.rs",
"src/matrix.rs",
],
deps = [
@@ -31,9 +30,9 @@ rust_library(
name = "matrix_dynamically_linked",
srcs = [
"src/ffi.rs",
- "src/lib.rs",
"src/matrix.rs",
],
+ crate_root = "src/matrix.rs",
deps = [
"//ffi/rust_calling_c/c:native_matrix_so",
"@libc",
diff --git a/examples/ffi/rust_calling_c/src/lib.rs b/examples/ffi/rust_calling_c/src/lib.rs
deleted file mode 100644
index 87923ea86f..0000000000
--- a/examples/ffi/rust_calling_c/src/lib.rs
+++ /dev/null
@@ -1,19 +0,0 @@
-// Copyright 2017 The Bazel Authors. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-extern crate libc;
-
-mod ffi;
-
-pub mod matrix;
\ No newline at end of file
diff --git a/examples/ffi/rust_calling_c/src/matrix.rs b/examples/ffi/rust_calling_c/src/matrix.rs
index 02e815aded..c9359a308d 100644
--- a/examples/ffi/rust_calling_c/src/matrix.rs
+++ b/examples/ffi/rust_calling_c/src/matrix.rs
@@ -12,7 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-use ffi;
+extern crate libc;
+
+mod ffi;
+
use std::ops;
use std::ptr;
diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl
index 1f2acadfd9..e05f105c02 100644
--- a/rust/private/rust.bzl
+++ b/rust/private/rust.bzl
@@ -15,15 +15,6 @@
load(":private/rustc.bzl", "CrateInfo", "rustc_compile_action")
load(":private/utils.bzl", "find_toolchain", "relative_path")
-def _find_crate_root_src(srcs, file_names = ["lib.rs"]):
- """Finds the source file for the crate root."""
- if len(srcs) == 1:
- return srcs[0]
- for src in srcs:
- if src.basename in file_names:
- return src
- fail("No {} source file found.".format(" or ".join(file_names)), "srcs")
-
def _determine_output_hash(lib_rs):
return repr(hash(lib_rs.path))
@@ -50,8 +41,21 @@ def _determine_lib_name(name, crate_type, toolchain, lib_hash = ""):
extension = extension,
)
-def _crate_root_src(ctx, file_names = ["lib.rs"]):
- return ctx.file.crate_root or _find_crate_root_src(ctx.files.srcs, file_names)
+def _crate_root_src(ctx, file_name = "lib.rs"):
+ """Finds the source file for the crate root."""
+ srcs = ctx.files.srcs
+ name_to_file = {f.basename: f for f in srcs}
+
+ crate_root = (
+ ctx.file.crate_root or
+ (srcs[0] if len(srcs) == 1 else None) or
+ name_to_file.get(file_name) or
+ name_to_file.get(ctx.attr.name + ".rs")
+ )
+ if not crate_root:
+ file_names = [file_name, ctx.attr.name + ".rs"]
+ fail("No {} source file found.".format(" or ".join(file_names)), "srcs")
+ return crate_root
def _rust_library_impl(ctx):
# Find lib.rs
@@ -91,7 +95,7 @@ def _rust_binary_impl(ctx):
crate_info = CrateInfo(
name = ctx.label.name,
type = "bin",
- root = _crate_root_src(ctx, ["main.rs"]),
+ root = _crate_root_src(ctx, "main.rs"),
srcs = ctx.files.srcs,
deps = ctx.attr.deps,
output = ctx.outputs.executable,