Skip to content

Commit

Permalink
Look for ${crate_name}.rs as a crate_root by default. (bazelbuild#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfarrugi committed Oct 20, 2018
1 parent 6584e8a commit f8ad072
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 47 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ rust_library(name, srcs, crate_root, crate_type, deps, data, crate_features, rus
library.</p>
<p>
If <code>srcs</code> contains more than one file, then there must be
a file either named <code>lib.rs</code>. Otherwise,
a file either named <code>lib.rs</code> or <code>${name}.rs</code>. Otherwise,
<code>crate_root</code> must be set to the source file that is the
root of the crate to be passed to <code>rustc</code> to build this
crate.
Expand All @@ -111,8 +111,8 @@ rust_library(name, srcs, crate_root, crate_type, deps, data, crate_features, rus
</p>
<p>
If <code>crate_root</code> is not set, then this rule will look for
a <code>lib.rs</code> file or the single file in <code>srcs</code>
if <code>srcs</code> contains only one file.
a <code>lib.rs</code> file, or the single file in <code>srcs</code>
if <code>srcs</code> contains only one file, or a <code>${name}.rs</code> file.
</p>
</td>
</tr>
Expand Down Expand Up @@ -302,7 +302,7 @@ rust_binary(name, srcs, deps, data, crate_features, rustc_flags, version, out_di
binary.</p>
<p>
If <code>srcs</code> contains more than one file, then there must be
a file either named <code>main.rs</code>. Otherwise,
a file either named <code>main.rs</code> or <code>${name}.rs</code>. Otherwise,
<code>crate_root</code> must be set to the source file that is the
root of the crate to be passed to <code>rustc</code> to build this
crate.
Expand All @@ -319,8 +319,8 @@ rust_binary(name, srcs, deps, data, crate_features, rustc_flags, version, out_di
</p>
<p>
If <code>crate_root</code> is not set, then this rule will look for
a <code>main.rs</code> file or the single file in <code>srcs</code>
if <code>srcs</code> contains only one file.
a <code>main.rs</code> file, or the single file in <code>srcs</code>
if <code>srcs</code> contains only one file, or a <code>${name}.rs</code> file.
</p>
</td>
</td>
Expand Down Expand Up @@ -512,7 +512,7 @@ rust_test(name, srcs, deps, data, crate_features, rustc_flags, version, out_dir_
library.</p>
<p>
If <code>srcs</code> contains more than one file, then there must be
a file either named <code>lib.rs</code>. Otherwise,
a file either named <code>lib.rs</code> or <code>${name}.rs</code>. Otherwise,
<code>crate_root</code> must be set to the source file that is the
root of the crate to be passed to <code>rustc</code> to build this
crate.
Expand All @@ -529,8 +529,8 @@ rust_test(name, srcs, deps, data, crate_features, rustc_flags, version, out_dir_
</p>
<p>
If <code>crate_root</code> is not set, then this rule will look for
a <code>lib.rs</code> file or the single file in <code>srcs</code>
if <code>srcs</code> contains only one file.
a <code>lib.rs</code> file, or the single file in <code>srcs</code>
if <code>srcs</code> contains only one file, or a <code>${name}.rs</code> file.
</p>
</td>
</td>
Expand Down Expand Up @@ -728,7 +728,7 @@ rust_test(
Run the test with `bazel build //hello_lib:hello_lib_test`.

<a name="rust_benchmark"></a>
## rust\_bench\_test
## rust\_benchmark

```python
rust_benchmark(name, srcs, deps, data, crate_features, rustc_flags, out_dir_tar)
Expand Down Expand Up @@ -773,7 +773,7 @@ easy to use a custom Rust toolchain, such as a nightly release.
library.</p>
<p>
If <code>srcs</code> contains more than one file, then there must be
a file either named <code>lib.rs</code>. Otherwise,
a file either named <code>lib.rs</code> or <code>${name}.rs</code>. Otherwise,
<code>crate_root</code> must be set to the source file that is the
root of the crate to be passed to <code>rustc</code> to build this
crate.
Expand All @@ -790,8 +790,8 @@ easy to use a custom Rust toolchain, such as a nightly release.
</p>
<p>
If <code>crate_root</code> is not set, then this rule will look for
a <code>lib.rs</code> file or the single file in <code>srcs</code>
if <code>srcs</code> contains only one file.
a <code>lib.rs</code> file, or the single file in <code>srcs</code>
if <code>srcs</code> contains only one file, or a <code>${name}.rs</code> file.
</p>
</td>
</td>
Expand Down
3 changes: 1 addition & 2 deletions examples/ffi/rust_calling_c/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ rust_library(
name = "matrix",
srcs = [
"src/ffi.rs",
"src/lib.rs",
"src/matrix.rs",
],
deps = [
Expand All @@ -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",
Expand Down
19 changes: 0 additions & 19 deletions examples/ffi/rust_calling_c/src/lib.rs

This file was deleted.

5 changes: 4 additions & 1 deletion examples/ffi/rust_calling_c/src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
28 changes: 16 additions & 12 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f8ad072

Please sign in to comment.