Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upstream wrappers for cargo, rustc, cargo-clippy #2703

Merged
merged 12 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ crate_universe/private/bootstrap
test/bzlmod_repo_mapping
test/cc_common_link
test/no_std
.direnv
3 changes: 2 additions & 1 deletion docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ Run the test with `bazel test //hello_lib:greeting_test`.
## rust_toolchain

<pre>
rust_toolchain(<a href="#rust_toolchain-name">name</a>, <a href="#rust_toolchain-allocator_library">allocator_library</a>, <a href="#rust_toolchain-binary_ext">binary_ext</a>, <a href="#rust_toolchain-cargo">cargo</a>, <a href="#rust_toolchain-clippy_driver">clippy_driver</a>, <a href="#rust_toolchain-debug_info">debug_info</a>,
rust_toolchain(<a href="#rust_toolchain-name">name</a>, <a href="#rust_toolchain-allocator_library">allocator_library</a>, <a href="#rust_toolchain-binary_ext">binary_ext</a>, <a href="#rust_toolchain-cargo">cargo</a>, <a href="#rust_toolchain-cargo_clippy">cargo_clippy</a>, <a href="#rust_toolchain-clippy_driver">clippy_driver</a>, <a href="#rust_toolchain-debug_info">debug_info</a>,
<a href="#rust_toolchain-default_edition">default_edition</a>, <a href="#rust_toolchain-dylib_ext">dylib_ext</a>, <a href="#rust_toolchain-env">env</a>, <a href="#rust_toolchain-exec_triple">exec_triple</a>, <a href="#rust_toolchain-experimental_link_std_dylib">experimental_link_std_dylib</a>,
<a href="#rust_toolchain-experimental_use_cc_common_link">experimental_use_cc_common_link</a>, <a href="#rust_toolchain-extra_exec_rustc_flags">extra_exec_rustc_flags</a>, <a href="#rust_toolchain-extra_rustc_flags">extra_rustc_flags</a>,
<a href="#rust_toolchain-extra_rustc_flags_for_crate_types">extra_rustc_flags_for_crate_types</a>, <a href="#rust_toolchain-global_allocator_library">global_allocator_library</a>, <a href="#rust_toolchain-llvm_cov">llvm_cov</a>, <a href="#rust_toolchain-llvm_profdata">llvm_profdata</a>,
Expand Down Expand Up @@ -1192,6 +1192,7 @@ See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpu
| <a id="rust_toolchain-allocator_library"></a>allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `"@rules_rust//ffi/cc/allocator_library"` |
| <a id="rust_toolchain-binary_ext"></a>binary_ext | The extension for binaries created from rustc. | String | required | |
| <a id="rust_toolchain-cargo"></a>cargo | The location of the `cargo` binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="rust_toolchain-cargo_clippy"></a>cargo_clippy | The location of the `cargo_clippy` binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="rust_toolchain-clippy_driver"></a>clippy_driver | The location of the `clippy-driver` binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="rust_toolchain-debug_info"></a>debug_info | Rustc debug info levels per opt level | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{"dbg": "2", "fastbuild": "0", "opt": "0"}` |
| <a id="rust_toolchain-default_edition"></a>default_edition | The edition to use for rust_* rules that don't specify an edition. If absent, every rule is required to specify its `edition` attribute. | String | optional | `""` |
Expand Down
3 changes: 2 additions & 1 deletion docs/rust_repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ A dedicated filegroup-like rule for Rust stdlib artifacts.
## rust_toolchain

<pre>
rust_toolchain(<a href="#rust_toolchain-name">name</a>, <a href="#rust_toolchain-allocator_library">allocator_library</a>, <a href="#rust_toolchain-binary_ext">binary_ext</a>, <a href="#rust_toolchain-cargo">cargo</a>, <a href="#rust_toolchain-clippy_driver">clippy_driver</a>, <a href="#rust_toolchain-debug_info">debug_info</a>,
rust_toolchain(<a href="#rust_toolchain-name">name</a>, <a href="#rust_toolchain-allocator_library">allocator_library</a>, <a href="#rust_toolchain-binary_ext">binary_ext</a>, <a href="#rust_toolchain-cargo">cargo</a>, <a href="#rust_toolchain-cargo_clippy">cargo_clippy</a>, <a href="#rust_toolchain-clippy_driver">clippy_driver</a>, <a href="#rust_toolchain-debug_info">debug_info</a>,
<a href="#rust_toolchain-default_edition">default_edition</a>, <a href="#rust_toolchain-dylib_ext">dylib_ext</a>, <a href="#rust_toolchain-env">env</a>, <a href="#rust_toolchain-exec_triple">exec_triple</a>, <a href="#rust_toolchain-experimental_link_std_dylib">experimental_link_std_dylib</a>,
<a href="#rust_toolchain-experimental_use_cc_common_link">experimental_use_cc_common_link</a>, <a href="#rust_toolchain-extra_exec_rustc_flags">extra_exec_rustc_flags</a>, <a href="#rust_toolchain-extra_rustc_flags">extra_rustc_flags</a>,
<a href="#rust_toolchain-extra_rustc_flags_for_crate_types">extra_rustc_flags_for_crate_types</a>, <a href="#rust_toolchain-global_allocator_library">global_allocator_library</a>, <a href="#rust_toolchain-llvm_cov">llvm_cov</a>, <a href="#rust_toolchain-llvm_profdata">llvm_profdata</a>,
Expand Down Expand Up @@ -96,6 +96,7 @@ See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpu
| <a id="rust_toolchain-allocator_library"></a>allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `"@rules_rust//ffi/cc/allocator_library"` |
| <a id="rust_toolchain-binary_ext"></a>binary_ext | The extension for binaries created from rustc. | String | required | |
| <a id="rust_toolchain-cargo"></a>cargo | The location of the `cargo` binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="rust_toolchain-cargo_clippy"></a>cargo_clippy | The location of the `cargo_clippy` binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="rust_toolchain-clippy_driver"></a>clippy_driver | The location of the `clippy-driver` binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="rust_toolchain-debug_info"></a>debug_info | Rustc debug info levels per opt level | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{"dbg": "2", "fastbuild": "0", "opt": "0"}` |
| <a id="rust_toolchain-default_edition"></a>default_edition | The edition to use for rust_* rules that don't specify an edition. If absent, every rule is required to specify its `edition` attribute. | String | optional | `""` |
Expand Down
1 change: 1 addition & 0 deletions examples/.bazelignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
android
bazel_env
bzlmod
cargo_manifest_dir/external_crate
crate_universe
Expand Down
1 change: 1 addition & 0 deletions examples/bazel_env/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.direnv
15 changes: 15 additions & 0 deletions examples/bazel_env/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Required on windows
common --enable_platform_specific_config
startup --windows_enable_symlinks
build:windows --enable_runfiles

build --experimental_enable_bzlmod

# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips.
build --incompatible_disallow_empty_glob

# Required for cargo_build_script support before Bazel 7
build --incompatible_merge_fixed_and_default_shell_env

# Do not import the PATH etc. from the host environment
common --incompatible_strict_action_env
8 changes: 8 additions & 0 deletions examples/bazel_env/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
# ^^^ make IDEs happy

watch_file bazel-out/bazel_env-opt/bin/env/env/bin
PATH_add bazel-out/bazel_env-opt/bin/env/env/bin
if [[ ! -d bazel-out/bazel_env-opt/bin/env/env/bin ]]; then
log_error "ERROR[bazel_env.bzl]: Run 'bazel run //env:env' to regenerate bazel-out/bazel_env-opt/bin/env/env/bin"
fi
1 change: 1 addition & 0 deletions examples/bazel_env/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bazel-*
50 changes: 50 additions & 0 deletions examples/bazel_env/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"Tests for upstream wrappers."

sh_test(
name = "upstream_cargo_test",
size = "small",
srcs = ["cargo_test.sh"],
args = [
"$(rlocationpath @rules_rust//tools/upstream_wrapper:cargo)",
],
data = [
"Cargo.lock",
"Cargo.toml",
"//rust/hello_world:Cargo.toml",
"//rust/hello_world:src/main.rs",
"@rules_rust//tools/upstream_wrapper:cargo",
],
deps = [
"@bazel_tools//tools/bash/runfiles",
],
)

sh_test(
name = "upstream_rustc_test",
size = "small",
srcs = ["rustc_test.sh"],
args = [
"$(rlocationpath @rules_rust//tools/upstream_wrapper:rustc)",
],
data = [
"@rules_rust//tools/upstream_wrapper:rustc",
],
deps = [
"@bazel_tools//tools/bash/runfiles",
],
)

sh_test(
name = "upstream_rustfmt_test",
size = "small",
srcs = ["rustfmt_test.sh"],
args = [
"$(rlocationpath @rules_rust//tools/upstream_wrapper:rustfmt)",
],
data = [
"@rules_rust//tools/upstream_wrapper:rustfmt",
],
deps = [
"@bazel_tools//tools/bash/runfiles",
],
)
7 changes: 7 additions & 0 deletions examples/bazel_env/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions examples/bazel_env/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
resolver = "2"
members = [
"rust/hello_world"
]
44 changes: 44 additions & 0 deletions examples/bazel_env/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""bazelbuild/rules_rust - bazel_env/bzlmod example

See https://github.com/buildbuddy-io/bazel_env.bzl.
"""

module(
name = "all_crate_deps_bzlmod_example",
version = "0.0.0",
)

bazel_dep(name = "platforms", version = "0.0.9")
bazel_dep(
name = "rules_rust",
version = "0.0.0",
)
local_path_override(
module_name = "rules_rust",
path = "../..",
)

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(edition = "2021")
use_repo(
rust,
"rust_toolchains",
)

register_toolchains("@rust_toolchains//:all")

crate = use_extension(
"@rules_rust//crate_universe:extension.bzl",
"crate",
)
crate.from_cargo(
name = "crates",
cargo_lockfile = "//:Cargo.lock",
manifests = [
"//:Cargo.toml",
"//rust/hello_world:Cargo.toml",
],
)
use_repo(crate, "crates")

bazel_dep(name = "bazel_env.bzl", version = "0.1.1")
11 changes: 11 additions & 0 deletions examples/bazel_env/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# rules_rust with bazel_env

This example uses [bazel_env.bzl](https://github.com/buildbuddy-io/bazel_env.bzl) to
provide `cargo`, `cargo-clippy`, `rustc` and `rustfmt` from the bazel toolchains
to the user. They will be available directly in the PATH when the user
enters this directory and has [direnv](https://direnv.net/) set up.

Advantages:

- The user doesn't have to install the toolchains themselves.
- The tool versions will always match exactly those that bazel uses.
1 change: 1 addition & 0 deletions examples/bazel_env/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Intentionally blank; using bzlmod
1 change: 1 addition & 0 deletions examples/bazel_env/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Intentionally blank; enable strict mode for bzlmod
46 changes: 46 additions & 0 deletions examples/bazel_env/cargo_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---


set -euo pipefail

# MARK - Functions

fail() {
echo >&2 "$@"
exit 1
}

# MARK - Args

if [[ "$#" -lt 1 ]]; then
fail "Usage: $0 /path/to/bin <args>"
fi
CARGO="$(rlocation "$1")"

# MARK - Test

# simulate bazel run with setting BUILD_WORKING_DIRECTORY
export BUILD_WORKING_DIRECTORY="$PWD"

# Run hello_world, invoking rustc
OUTPUT="$("${CARGO}" run)"

EXPECTED_OUTPUT="Hello, world!"
[[ "${OUTPUT}" == "${EXPECTED_OUTPUT}" ]] ||
fail 'Expected "'"${EXPECTED_OUTPUT}"'", but was' "${OUTPUT}"

# Crash tests, no error

${CARGO} clippy ||
fail "couldn't execute \"cargo clippy\""
13 changes: 13 additions & 0 deletions examples/bazel_env/env/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@bazel_env.bzl", "bazel_env")

package(default_visibility = ["//visibility:public"])

bazel_env(
name = "env",
tools = {
"cargo": "@rules_rust//tools/upstream_wrapper:cargo",
"cargo-clippy": "@rules_rust//tools/upstream_wrapper:cargo_clippy",
"rustc": "@rules_rust//tools/upstream_wrapper:rustc",
"rustfmt": "@rules_rust//tools/upstream_wrapper:rustfmt",
},
)
Binary file added examples/bazel_env/hello
Binary file not shown.
15 changes: 15 additions & 0 deletions examples/bazel_env/rust/hello_world/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@crates//:defs.bzl", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_binary")

package(default_visibility = ["//visibility:public"])

exports_files([
"Cargo.toml",
"src/main.rs",
])

rust_binary(
name = "hello_world",
srcs = ["src/main.rs"],
deps = all_crate_deps(normal = True),
)
8 changes: 8 additions & 0 deletions examples/bazel_env/rust/hello_world/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "hello_world"
version = "0.0.0"
edition = "2021"
publish = false

[lib]
path = "/dev/null"
17 changes: 17 additions & 0 deletions examples/bazel_env/rust/hello_world/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

fn main() {
println!("Hello, world!");
}
40 changes: 40 additions & 0 deletions examples/bazel_env/rustc_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash

# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---


set -euo pipefail

# MARK - Functions

fail() {
echo >&2 "$@"
exit 1
}

# MARK - Args

if [[ "$#" -lt 1 ]]; then
fail "Usage: $0 /path/to/bin <args>"
fi
RUSTC="$(rlocation "$1")"

# MARK - Test
DIR=$(mktemp -d "rustc-test-XXXXXXX")

set -x
echo "fn main() {}" >"$DIR/main.rs"
cd "$DIR"
# simulate bazel run with setting BUILD_WORKING_DIRECTORY
export BUILD_WORKING_DIRECTORY="$PWD"
"$RUSTC" "main.rs" ||
fail "Couldn't compile main.rs"
41 changes: 41 additions & 0 deletions examples/bazel_env/rustfmt_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---


set -euo pipefail

# MARK - Functions

fail() {
echo >&2 "$@"
exit 1
}

# MARK - Args

if [[ "$#" -lt 1 ]]; then
fail "Usage: $0 /path/to/bin <args>"
fi
RUSTFMT="$(rlocation "$1")"

# MARK - Test

# simulate bazel run with setting BUILD_WORKING_DIRECTORY
export BUILD_WORKING_DIRECTORY="$PWD"

OUTPUT="$(echo -e "fn main() {\n\n}" | "$RUSTFMT")"

# without newlines in body
EXPECTED_OUTPUT="fn main() {}"
[[ "${OUTPUT}" == "${EXPECTED_OUTPUT}" ]] ||
fail 'Expected "'"${EXPECTED_OUTPUT}"'", but was' "${OUTPUT}"
Loading
Loading