Skip to content

Commit 00a945d

Browse files
committed
Initial Hello World for rust compilation by bazel
Summary: Prepping for the implementation of the rust indexer, this commit sets up bazel builds for rust. Currently it only builds a hello world program but the project is split into two different components a library and an executable. Test Plan: Run bazel build //kythe/rust/indexer:release. If rust is present it will build, and //kythe/rust/indexer:test will run the tests. If rust is not present both will fail and display an error message. Reviewers: zarko, schroederc Reviewed By: schroederc Projects: #rust Differential Revision: https://phabricator-dot-kythe-repo.appspot.com/D931
1 parent 31fdb46 commit 00a945d

File tree

8 files changed

+167
-0
lines changed

8 files changed

+167
-0
lines changed

kythe/rust/indexer/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target/
2+
/Cargo.lock

kythe/rust/indexer/BUILD

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package(default_visibility = ["//kythe:default_visibility"])
2+
3+
load("//tools/build_rules/rust:cargo.bzl", "cargo_lib", "cargo_bin", "cargo_test")
4+
5+
cargo_lib(
6+
name = "libindexer",
7+
loc = "kythe/rust/indexer/",
8+
)
9+
10+
cargo_bin(
11+
name = "indexer",
12+
loc = "kythe/rust/indexer/",
13+
)
14+
15+
cargo_test(
16+
name = "test_it_works",
17+
size = "small",
18+
test_name = "tests::it_works",
19+
loc = "kythe/rust/indexer/",
20+
)

kythe/rust/indexer/Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
[package]
16+
name = "indexer"
17+
version = "0.1.0"
18+
authors = ["John Renner <[email protected]>"]
19+
20+
[dependencies]

kythe/rust/indexer/src/bin/indexer.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2016 Google Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
extern crate indexer;
18+
19+
fn main() {
20+
indexer::say_hello();
21+
}

kythe/rust/indexer/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2016 Google Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
pub fn say_hello() {
18+
println!("Hello World!");
19+
}
20+
21+
#[cfg(test)]
22+
mod tests {
23+
#[test]
24+
fn it_works() {
25+
}
26+
}

tools/build_rules/rust/BUILD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package(
2+
default_visibility = ["//visibility:public"],
3+
)
4+
5+
exports_files([
6+
"cargo.bzl",
7+
"run_cargo.sh",
8+
])

tools/build_rules/rust/cargo.bzl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
def _cargo_cmd(loc, cmd, outs=[]):
2+
return "\n".join([
3+
"$(location //tools/build_rules/rust:run_cargo.sh) " + loc + " \"" + cmd + "\"",
4+
] + [
5+
"mv " + loc + out + " $(location " + out + ") " for out in outs
6+
])
7+
8+
_automatic_srcs = [
9+
"Cargo.toml",
10+
"src/**/*.rs",
11+
]
12+
13+
def cargo_bin(name, loc, srcs=[], bin_name=None, release=False):
14+
bin_name = name if bin_name == None else bin_name
15+
output_path = "target/" + ("release/" if release else "debug/") + bin_name
16+
native.genrule(
17+
name = name,
18+
srcs = srcs + native.glob(_automatic_srcs),
19+
outs = [output_path],
20+
cmd = _cargo_cmd(
21+
loc = loc,
22+
cmd = "build --bin " + (" --release" if release else "") + bin_name,
23+
outs = [output_path],
24+
),
25+
local = 1,
26+
tools = ["//tools/build_rules/rust:run_cargo.sh"],
27+
tags = ["manual", "requires-network"],
28+
)
29+
30+
def cargo_lib(name, loc, srcs=[], libname=None, release=False):
31+
libname = (name if libname == None else libname) + ".rlib"
32+
output_path = "target/" + ("release/" if release else "debug/") + libname
33+
native.genrule(
34+
name = name,
35+
srcs = srcs + native.glob(_automatic_srcs),
36+
37+
outs = [output_path],
38+
cmd = _cargo_cmd(
39+
loc = loc,
40+
cmd = "build --lib" + (" --release" if release else ""),
41+
outs = [output_path],
42+
),
43+
local = 1,
44+
tools = ["//tools/build_rules/rust:run_cargo.sh"],
45+
tags = ["manual", "requires-network"],
46+
)
47+
48+
# BEWARE: cargo will report a test as passing if there is no matching
49+
# test name
50+
def cargo_test(name, size, loc, test_name, srcs=[], cargo_opts=""):
51+
native.sh_test(
52+
name=name,
53+
size=size,
54+
srcs=["//tools/build_rules/rust:run_cargo.sh"],
55+
data=srcs + native.glob(_automatic_srcs),
56+
args=[loc, "\"test " + cargo_opts + " " + test_name + "\""],
57+
tags=["local"],
58+
)

tools/build_rules/rust/run_cargo.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash -e
2+
3+
loc=$1
4+
cargo_args=$2
5+
if which cargo >/dev/null; then
6+
cd $loc
7+
cargo $cargo_args
8+
else
9+
echo >&2 '\nERROR: No Rust Installation Found\n'
10+
exit 1
11+
fi
12+

0 commit comments

Comments
 (0)