Skip to content

Commit

Permalink
Added rust example (#1354)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
UebelAndre authored Jan 2, 2025
1 parent 92f81ee commit 7e0fafb
Show file tree
Hide file tree
Showing 6 changed files with 554 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .bazelci/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ tasks:
- "-//cmake_hello_world_lib/static:libhello_example"
- "-//cmake_hello_world_lib/static:test_hello"
- "-//cmake_with_data/..."
- "-//rust/..."
batch_commands:
- powershell -noexit "& "".\..\.bazelci\windows-update-certs.ps1"""
build_targets: *windows_targets
Expand Down Expand Up @@ -247,7 +248,10 @@ tasks:
platform: ubuntu1804
working_directory: examples
min_supported_targets: &min_supported_targets
- "--"
- "//..."
# The min supported version of rules_rust is `>=7`
- "-//rust/..."
build_targets: *min_supported_targets
test_targets: *min_supported_targets

Expand Down
30 changes: 30 additions & 0 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use_repo(
)

bazel_dep(name = "platforms", version = "0.0.6")
bazel_dep(name = "rules_rust", version = "0.56.0")
bazel_dep(name = "rules_swift", version = "1.6.0", repo_name = "build_bazel_rules_swift")
bazel_dep(name = "rules_apple", version = "3.4.0", repo_name = "build_bazel_rules_apple")
bazel_dep(name = "apple_support", version = "1.12.0", repo_name = "build_bazel_apple_support")
Expand All @@ -36,6 +37,35 @@ pip.parse(
)
use_repo(pip, "pip")

# https://bazelbuild.github.io/rules_rust/crate_universe_bzlmod.html
rust_example = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")

inject_repo(rust_example, "rules_foreign_cc")

rust_example.annotation(
# Setting build_script_data makes the files available on disk when the rule runs.
build_script_data = ["@rules_foreign_cc//toolchains:current_cmake_toolchain"],
build_script_env = {
# 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.
"CMAKE": "$${pwd}/$(CMAKE)",
},
# Setting build_script_toolchains makes makefile variable substitution work so that we can reference $(CNAME) in attributes.
build_script_toolchains = ["@rules_foreign_cc//toolchains:current_cmake_toolchain"],
crate = "libz-ng-sys",
)
rust_example.spec(
package = "libz-ng-sys",
version = "=1.1.20",
)
rust_example.from_specs(
name = "rust_example",
)
use_repo(
rust_example,
"rust_example",
)

# bazel_dep(name = "rules_android", version = "0.1.1")
# bazel_dep(name = "rules_jvm_external", version = "5.1")
#
Expand Down
54 changes: 54 additions & 0 deletions examples/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,57 @@ load(
)

apple_support_dependencies()

maybe(
http_archive,
name = "rules_rust",
integrity = "sha256-8TBqrAsli3kN8BrZq8arsN8LZUFsdLTvJ/Sqsph4CmQ=",
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.56.0/rules_rust-0.56.0.tar.gz"],
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains()

load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")

crate_universe_dependencies()

load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository", "splicing_config")

crates_repository(
name = "rust_example",
annotations = {
# zlib-ng-sys's build script invokes cmake, so we need to make cmake available.
# Fortunately, rules_foreign_cc has a cmake toolchain we can use.
"libz-ng-sys": [crate.annotation(
# Setting build_script_data makes the files available on disk when the rule runs.
build_script_data = ["@rules_foreign_cc//toolchains:current_cmake_toolchain"],
build_script_env = {
# 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.
"CMAKE": "$${pwd}/$(CMAKE)",
},
# Setting build_script_toolchains makes makefile variable substitution work so that we can reference $(CNAME) in attributes.
build_script_toolchains = ["@rules_foreign_cc//toolchains:current_cmake_toolchain"],
)],
},
cargo_lockfile = "//rust:Cargo.Bazel.lock",
lockfile = "//rust:cargo-bazel-lock.json",
packages = {
"libz-ng-sys": crate.spec(
version = "=1.1.20",
),
},
splicing_config = splicing_config(
resolver_version = "2",
),
)

load(
"@rust_example//:defs.bzl",
rust_example_crate_repositories = "crate_repositories",
)

rust_example_crate_repositories()
8 changes: 8 additions & 0 deletions examples/rust/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")

build_test(
name = "build_test",
targets = [
"@rust_example//:libz-ng-sys",
],
)
41 changes: 41 additions & 0 deletions examples/rust/Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "cc"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b"

[[package]]
name = "cmake"
version = "0.1.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31c789563b815f77f4250caee12365734369f942439b7defd71e18a48197130"
dependencies = [
"cc",
]

[[package]]
name = "direct-cargo-bazel-deps"
version = "0.0.1"
dependencies = [
"libz-ng-sys",
]

[[package]]
name = "libc"
version = "0.2.154"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"

[[package]]
name = "libz-ng-sys"
version = "1.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f0f7295a34685977acb2e8cc8b08ee4a8dffd6cf278eeccddbe1ed55ba815d5"
dependencies = [
"cmake",
"libc",
]
Loading

0 comments on commit 7e0fafb

Please sign in to comment.