Skip to content

Commit 7e0fafb

Browse files
authored
Added rust example (#1354)
I think having this example here vs in `rules_rust` will be more visible for consumers of `rules_foreign_cc`. `rules_rust` should otherwise try to demonstrate simple examples or uses of `rules_cc` and leave the complexity of foreign_cc interactions to this repo.
1 parent 92f81ee commit 7e0fafb

File tree

6 files changed

+554
-0
lines changed

6 files changed

+554
-0
lines changed

.bazelci/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ tasks:
167167
- "-//cmake_hello_world_lib/static:libhello_example"
168168
- "-//cmake_hello_world_lib/static:test_hello"
169169
- "-//cmake_with_data/..."
170+
- "-//rust/..."
170171
batch_commands:
171172
- powershell -noexit "& "".\..\.bazelci\windows-update-certs.ps1"""
172173
build_targets: *windows_targets
@@ -247,7 +248,10 @@ tasks:
247248
platform: ubuntu1804
248249
working_directory: examples
249250
min_supported_targets: &min_supported_targets
251+
- "--"
250252
- "//..."
253+
# The min supported version of rules_rust is `>=7`
254+
- "-//rust/..."
251255
build_targets: *min_supported_targets
252256
test_targets: *min_supported_targets
253257

examples/MODULE.bazel

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use_repo(
1313
)
1414

1515
bazel_dep(name = "platforms", version = "0.0.6")
16+
bazel_dep(name = "rules_rust", version = "0.56.0")
1617
bazel_dep(name = "rules_swift", version = "1.6.0", repo_name = "build_bazel_rules_swift")
1718
bazel_dep(name = "rules_apple", version = "3.4.0", repo_name = "build_bazel_rules_apple")
1819
bazel_dep(name = "apple_support", version = "1.12.0", repo_name = "build_bazel_apple_support")
@@ -36,6 +37,35 @@ pip.parse(
3637
)
3738
use_repo(pip, "pip")
3839

40+
# https://bazelbuild.github.io/rules_rust/crate_universe_bzlmod.html
41+
rust_example = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
42+
43+
inject_repo(rust_example, "rules_foreign_cc")
44+
45+
rust_example.annotation(
46+
# Setting build_script_data makes the files available on disk when the rule runs.
47+
build_script_data = ["@rules_foreign_cc//toolchains:current_cmake_toolchain"],
48+
build_script_env = {
49+
# The toolchain supplies a value of $(CMAKE) which is an execroot-relative path, so we need to prefix it with `$${pwd}/`
50+
# because build scripts don't typically run in the execroot unlike most bazel rules, for improved compatibility with Cargo.
51+
"CMAKE": "$${pwd}/$(CMAKE)",
52+
},
53+
# Setting build_script_toolchains makes makefile variable substitution work so that we can reference $(CNAME) in attributes.
54+
build_script_toolchains = ["@rules_foreign_cc//toolchains:current_cmake_toolchain"],
55+
crate = "libz-ng-sys",
56+
)
57+
rust_example.spec(
58+
package = "libz-ng-sys",
59+
version = "=1.1.20",
60+
)
61+
rust_example.from_specs(
62+
name = "rust_example",
63+
)
64+
use_repo(
65+
rust_example,
66+
"rust_example",
67+
)
68+
3969
# bazel_dep(name = "rules_android", version = "0.1.1")
4070
# bazel_dep(name = "rules_jvm_external", version = "5.1")
4171
#

examples/WORKSPACE.bazel

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,57 @@ load(
124124
)
125125

126126
apple_support_dependencies()
127+
128+
maybe(
129+
http_archive,
130+
name = "rules_rust",
131+
integrity = "sha256-8TBqrAsli3kN8BrZq8arsN8LZUFsdLTvJ/Sqsph4CmQ=",
132+
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.56.0/rules_rust-0.56.0.tar.gz"],
133+
)
134+
135+
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")
136+
137+
rules_rust_dependencies()
138+
139+
rust_register_toolchains()
140+
141+
load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")
142+
143+
crate_universe_dependencies()
144+
145+
load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository", "splicing_config")
146+
147+
crates_repository(
148+
name = "rust_example",
149+
annotations = {
150+
# zlib-ng-sys's build script invokes cmake, so we need to make cmake available.
151+
# Fortunately, rules_foreign_cc has a cmake toolchain we can use.
152+
"libz-ng-sys": [crate.annotation(
153+
# Setting build_script_data makes the files available on disk when the rule runs.
154+
build_script_data = ["@rules_foreign_cc//toolchains:current_cmake_toolchain"],
155+
build_script_env = {
156+
# The toolchain supplies a value of $(CMAKE) which is an execroot-relative path, so we need to prefix it with $${pwd}/ because build scripts don't typically run in the execroot unlike most bazel rules, for improved compatibility with Cargo.
157+
"CMAKE": "$${pwd}/$(CMAKE)",
158+
},
159+
# Setting build_script_toolchains makes makefile variable substitution work so that we can reference $(CNAME) in attributes.
160+
build_script_toolchains = ["@rules_foreign_cc//toolchains:current_cmake_toolchain"],
161+
)],
162+
},
163+
cargo_lockfile = "//rust:Cargo.Bazel.lock",
164+
lockfile = "//rust:cargo-bazel-lock.json",
165+
packages = {
166+
"libz-ng-sys": crate.spec(
167+
version = "=1.1.20",
168+
),
169+
},
170+
splicing_config = splicing_config(
171+
resolver_version = "2",
172+
),
173+
)
174+
175+
load(
176+
"@rust_example//:defs.bzl",
177+
rust_example_crate_repositories = "crate_repositories",
178+
)
179+
180+
rust_example_crate_repositories()

examples/rust/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
2+
3+
build_test(
4+
name = "build_test",
5+
targets = [
6+
"@rust_example//:libz-ng-sys",
7+
],
8+
)

examples/rust/Cargo.Bazel.lock

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# This file is automatically @generated by Cargo.
2+
# It is not intended for manual editing.
3+
version = 4
4+
5+
[[package]]
6+
name = "cc"
7+
version = "1.0.95"
8+
source = "registry+https://github.com/rust-lang/crates.io-index"
9+
checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b"
10+
11+
[[package]]
12+
name = "cmake"
13+
version = "0.1.50"
14+
source = "registry+https://github.com/rust-lang/crates.io-index"
15+
checksum = "a31c789563b815f77f4250caee12365734369f942439b7defd71e18a48197130"
16+
dependencies = [
17+
"cc",
18+
]
19+
20+
[[package]]
21+
name = "direct-cargo-bazel-deps"
22+
version = "0.0.1"
23+
dependencies = [
24+
"libz-ng-sys",
25+
]
26+
27+
[[package]]
28+
name = "libc"
29+
version = "0.2.154"
30+
source = "registry+https://github.com/rust-lang/crates.io-index"
31+
checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
32+
33+
[[package]]
34+
name = "libz-ng-sys"
35+
version = "1.1.20"
36+
source = "registry+https://github.com/rust-lang/crates.io-index"
37+
checksum = "8f0f7295a34685977acb2e8cc8b08ee4a8dffd6cf278eeccddbe1ed55ba815d5"
38+
dependencies = [
39+
"cmake",
40+
"libc",
41+
]

0 commit comments

Comments
 (0)