Skip to content

Commit 857d915

Browse files
committed
Add example building bzip2
1 parent 2bcc8e8 commit 857d915

File tree

10 files changed

+208
-0
lines changed

10 files changed

+208
-0
lines changed

WORKSPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,6 @@ rbe_autoconfig(
9999

100100
load("@io_bazel_rules_rust//:workspace.bzl", "bazel_version")
101101
bazel_version(name = "bazel_version")
102+
103+
load("@examples//hello_sys:workspace.bzl", "remote_deps")
104+
remote_deps()

examples/WORKSPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ rust_wasm_bindgen_repositories()
6969
load("@io_bazel_rules_rust//:workspace.bzl", "bazel_version")
7070

7171
bazel_version(name = "bazel_version")
72+
73+
load("@examples//hello_sys:workspace.bzl", "remote_deps")
74+
75+
remote_deps()

examples/hello_sys/BUILD

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load(
4+
"@io_bazel_rules_rust//rust:rust.bzl",
5+
"rust_binary",
6+
)
7+
8+
rust_binary(
9+
name = "hello_sys",
10+
srcs = ["src/main.rs"],
11+
edition = "2018",
12+
deps = ["@bzip2"],
13+
)
14+
15+
sh_test(
16+
name = "test",
17+
srcs = ["test.sh"],
18+
args = ["$(location :hello_sys)"],
19+
data = [":hello_sys"],
20+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load(
2+
"@io_bazel_rules_rust//rust:rust.bzl",
3+
"rust_library",
4+
)
5+
6+
rust_library(
7+
name = "bzip2",
8+
srcs = glob(["**/*.rs"]),
9+
crate_root = "src/lib.rs",
10+
crate_type = "lib",
11+
edition = "2015",
12+
rustc_flags = [
13+
"--cap-lints=allow",
14+
],
15+
version = "0.3.3",
16+
visibility = ["//visibility:public"],
17+
deps = [
18+
"@bzip2_sys",
19+
"@libc",
20+
],
21+
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
load(
2+
"@io_bazel_rules_rust//rust:rust.bzl",
3+
"rust_library",
4+
)
5+
load(
6+
"@io_bazel_rules_rust//cargo:cargo_build_script.bzl",
7+
"cargo_build_script",
8+
)
9+
10+
cargo_build_script(
11+
name = "bzip2_sys_build_script",
12+
srcs = glob(["**/*.rs"]),
13+
crate_root = "build.rs",
14+
data = glob(["**"]),
15+
edition = "2015",
16+
rustc_flags = [
17+
"--cap-lints=allow",
18+
],
19+
version = "0.1.9+1.0.8",
20+
deps = [
21+
"@cc",
22+
"@pkg_config",
23+
],
24+
)
25+
26+
rust_library(
27+
name = "bzip2_sys",
28+
srcs = glob(["**/*.rs"]),
29+
crate_root = "lib.rs",
30+
crate_type = "lib",
31+
edition = "2015",
32+
rustc_flags = [
33+
"--cap-lints=allow",
34+
],
35+
version = "0.1.9+1.0.8",
36+
visibility = ["//visibility:public"],
37+
deps = [
38+
":bzip2_sys_build_script",
39+
"@libc",
40+
],
41+
)

examples/hello_sys/cc-1.0.54.BUILD

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load(
2+
"@io_bazel_rules_rust//rust:rust.bzl",
3+
"rust_library",
4+
)
5+
6+
rust_library(
7+
name = "cc",
8+
srcs = glob(["**/*.rs"]),
9+
crate_root = "src/lib.rs",
10+
crate_type = "lib",
11+
edition = "2018",
12+
rustc_flags = [
13+
"--cap-lints=allow",
14+
],
15+
version = "1.0.54",
16+
visibility = ["//visibility:public"],
17+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load(
2+
"@io_bazel_rules_rust//rust:rust.bzl",
3+
"rust_library",
4+
)
5+
6+
rust_library(
7+
name = "pkg_config",
8+
srcs = glob(["**/*.rs"]),
9+
crate_root = "src/lib.rs",
10+
crate_type = "lib",
11+
edition = "2015",
12+
rustc_flags = [
13+
"--cap-lints=allow",
14+
],
15+
version = "0.3.17",
16+
visibility = ["//visibility:public"],
17+
)

examples/hello_sys/src/main.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use bzip2::read::BzEncoder;
2+
use bzip2::Compression;
3+
use std::io::Read;
4+
5+
fn main() {
6+
let stdin = std::io::stdin();
7+
let stdin = stdin.lock();
8+
let mut raw_counter = CountingStream::new(stdin);
9+
10+
let compressed_count = {
11+
let compressor = BzEncoder::new(&mut raw_counter, Compression::Best);
12+
let mut compressed_counter = CountingStream::new(compressor);
13+
std::io::copy(&mut compressed_counter, &mut std::io::sink()).unwrap();
14+
compressed_counter.count
15+
};
16+
17+
println!(
18+
"Compressed {} to {} bytes",
19+
raw_counter.count, compressed_count
20+
);
21+
}
22+
23+
struct CountingStream<R: Read> {
24+
stream: R,
25+
count: usize,
26+
}
27+
28+
impl<R: Read> CountingStream<R> {
29+
fn new(stream: R) -> Self {
30+
CountingStream { stream, count: 0 }
31+
}
32+
}
33+
34+
impl<R: Read> Read for CountingStream<R> {
35+
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
36+
let result = self.stream.read(buf);
37+
if let Ok(read_bytes) = result {
38+
self.count += read_bytes;
39+
}
40+
result
41+
}
42+
}

examples/hello_sys/test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash -eu
2+
3+
out="$(echo -n "Hello world" | "$1")"
4+
5+
[[ "${out}" == "Compressed 11 to 50 bytes" ]] || (echo "Got ${out}" && exit 1)

examples/hello_sys/workspace.bzl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
3+
def remote_deps():
4+
http_archive(
5+
name = "bzip2",
6+
url = "https://crates-io.s3-us-west-1.amazonaws.com/crates/bzip2/bzip2-0.3.3.crate",
7+
sha256 = "42b7c3cbf0fa9c1b82308d57191728ca0256cb821220f4e2fd410a72ade26e3b",
8+
type = "tar.gz",
9+
strip_prefix = "bzip2-0.3.3",
10+
build_file = "@examples//hello_sys:bzip2-0.3.3.BUILD",
11+
)
12+
13+
http_archive(
14+
name = "bzip2_sys",
15+
url = "https://crates-io.s3-us-west-1.amazonaws.com/crates/bzip2-sys/bzip2-sys-0.1.9+1.0.8.crate",
16+
sha256 = "ad3b39a260062fca31f7b0b12f207e8f2590a67d32ec7d59c20484b07ea7285e",
17+
type = "tar.gz",
18+
strip_prefix = "bzip2-sys-0.1.9+1.0.8",
19+
build_file = "@examples//hello_sys:bzip2-sys-0.1.9+1.0.8.BUILD",
20+
)
21+
22+
http_archive(
23+
name = "cc",
24+
url = "https://crates-io.s3-us-west-1.amazonaws.com/crates/cc/cc-1.0.54.crate",
25+
sha256 = "7bbb73db36c1246e9034e307d0fba23f9a2e251faa47ade70c1bd252220c8311",
26+
type = "tar.gz",
27+
strip_prefix = "cc-1.0.54",
28+
build_file = "@examples//hello_sys:cc-1.0.54.BUILD",
29+
)
30+
31+
http_archive(
32+
name = "pkg_config",
33+
url = "https://crates-io.s3-us-west-1.amazonaws.com/crates/pkg-config/pkg-config-0.3.17.crate",
34+
sha256 = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677",
35+
type = "tar.gz",
36+
strip_prefix = "pkg-config-0.3.17",
37+
build_file = "@examples//hello_sys:pkg-config-0.3.17.BUILD",
38+
)

0 commit comments

Comments
 (0)