From 7b1601901eda8659a9315b59b91b641889feedd1 Mon Sep 17 00:00:00 2001 From: Peter Kolloch Date: Tue, 18 Jun 2024 19:02:55 +0200 Subject: [PATCH] examples/bazel_env: Add an example ...and simplify target names of wrappers --- examples/bazel_env/.bazelrc | 12 +++++++ examples/bazel_env/.envrc | 8 +++++ examples/bazel_env/.gitignore | 1 + examples/bazel_env/BUILD.bazel | 9 +++++ examples/bazel_env/Cargo.lock | 16 +++++++++ examples/bazel_env/Cargo.toml | 12 +++++++ examples/bazel_env/MODULE.bazel | 42 +++++++++++++++++++++++ examples/bazel_env/README.md | 11 ++++++ examples/bazel_env/WORKSPACE.bazel | 1 + examples/bazel_env/WORKSPACE.bzlmod | 1 + examples/bazel_env/all_crate_deps_test.sh | 35 +++++++++++++++++++ examples/bazel_env/env/BUILD.bazel | 11 ++++++ examples/bazel_env/src/main.rs | 18 ++++++++++ tools/rustfmt/BUILD.bazel | 6 ++-- tools/upstream_wrapper/BUILD.bazel | 4 +-- 15 files changed, 182 insertions(+), 5 deletions(-) create mode 100644 examples/bazel_env/.bazelrc create mode 100644 examples/bazel_env/.envrc create mode 100644 examples/bazel_env/.gitignore create mode 100644 examples/bazel_env/BUILD.bazel create mode 100644 examples/bazel_env/Cargo.lock create mode 100644 examples/bazel_env/Cargo.toml create mode 100644 examples/bazel_env/MODULE.bazel create mode 100644 examples/bazel_env/README.md create mode 100644 examples/bazel_env/WORKSPACE.bazel create mode 100644 examples/bazel_env/WORKSPACE.bzlmod create mode 100755 examples/bazel_env/all_crate_deps_test.sh create mode 100644 examples/bazel_env/env/BUILD.bazel create mode 100644 examples/bazel_env/src/main.rs diff --git a/examples/bazel_env/.bazelrc b/examples/bazel_env/.bazelrc new file mode 100644 index 0000000000..f9d474e02f --- /dev/null +++ b/examples/bazel_env/.bazelrc @@ -0,0 +1,12 @@ +# 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 diff --git a/examples/bazel_env/.envrc b/examples/bazel_env/.envrc new file mode 100644 index 0000000000..4722fbe147 --- /dev/null +++ b/examples/bazel_env/.envrc @@ -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 \ No newline at end of file diff --git a/examples/bazel_env/.gitignore b/examples/bazel_env/.gitignore new file mode 100644 index 0000000000..a6ef824c1f --- /dev/null +++ b/examples/bazel_env/.gitignore @@ -0,0 +1 @@ +/bazel-* diff --git a/examples/bazel_env/BUILD.bazel b/examples/bazel_env/BUILD.bazel new file mode 100644 index 0000000000..c64fea007e --- /dev/null +++ b/examples/bazel_env/BUILD.bazel @@ -0,0 +1,9 @@ +load("@crates//:defs.bzl", "all_crate_deps") + +package(default_visibility = ["//visibility:public"]) + +rust_binary( + name = "all_crate_deps", + srcs = ["src/main.rs"], + deps = all_crate_deps(normal = True), +) diff --git a/examples/bazel_env/Cargo.lock b/examples/bazel_env/Cargo.lock new file mode 100644 index 0000000000..3471b64a53 --- /dev/null +++ b/examples/bazel_env/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "all_crate_deps" +version = "0.0.0" +dependencies = [ + "anyhow", +] + +[[package]] +name = "anyhow" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca" diff --git a/examples/bazel_env/Cargo.toml b/examples/bazel_env/Cargo.toml new file mode 100644 index 0000000000..4ae9c36379 --- /dev/null +++ b/examples/bazel_env/Cargo.toml @@ -0,0 +1,12 @@ +[workspace] +[package] +name = "all_crate_deps" +version = "0.0.0" +edition = "2021" +publish = false + +[lib] +path = "/dev/null" + +[dependencies] +anyhow = "1.0.79" diff --git a/examples/bazel_env/MODULE.bazel b/examples/bazel_env/MODULE.bazel new file mode 100644 index 0000000000..6ba616d714 --- /dev/null +++ b/examples/bazel_env/MODULE.bazel @@ -0,0 +1,42 @@ +"""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"], +) +use_repo(crate, "crates") + +bazel_dep(name = "bazel_env.bzl", version = "0.1.1") diff --git a/examples/bazel_env/README.md b/examples/bazel_env/README.md new file mode 100644 index 0000000000..34550116e2 --- /dev/null +++ b/examples/bazel_env/README.md @@ -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. diff --git a/examples/bazel_env/WORKSPACE.bazel b/examples/bazel_env/WORKSPACE.bazel new file mode 100644 index 0000000000..b543b79e9e --- /dev/null +++ b/examples/bazel_env/WORKSPACE.bazel @@ -0,0 +1 @@ +# Intentionally blank; using bzlmod diff --git a/examples/bazel_env/WORKSPACE.bzlmod b/examples/bazel_env/WORKSPACE.bzlmod new file mode 100644 index 0000000000..8e081c0b59 --- /dev/null +++ b/examples/bazel_env/WORKSPACE.bzlmod @@ -0,0 +1 @@ +# Intentionally blank; enable strict mode for bzlmod diff --git a/examples/bazel_env/all_crate_deps_test.sh b/examples/bazel_env/all_crate_deps_test.sh new file mode 100755 index 0000000000..f0efdf02ca --- /dev/null +++ b/examples/bazel_env/all_crate_deps_test.sh @@ -0,0 +1,35 @@ +#!/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 [[ "$#" -ne 1 ]]; then + fail "Usage: $0 /path/to/hello_world" +fi +HELLO_WORLD="$(rlocation "$1")" + +# MARK - Test + +OUTPUT="$("${HELLO_WORLD}")" +[[ "${OUTPUT}" == "Hello, world!" ]] || + fail 'Expected "Hello, world!", but was' "${OUTPUT}" diff --git a/examples/bazel_env/env/BUILD.bazel b/examples/bazel_env/env/BUILD.bazel new file mode 100644 index 0000000000..5c797e0053 --- /dev/null +++ b/examples/bazel_env/env/BUILD.bazel @@ -0,0 +1,11 @@ +load("@bazel_env.bzl", "bazel_env") + +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", + }, +) diff --git a/examples/bazel_env/src/main.rs b/examples/bazel_env/src/main.rs new file mode 100644 index 0000000000..1aa867e38a --- /dev/null +++ b/examples/bazel_env/src/main.rs @@ -0,0 +1,18 @@ +// 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() -> anyhow::Result<()> { + println!("Hello, world!"); + Ok(()) +} diff --git a/tools/rustfmt/BUILD.bazel b/tools/rustfmt/BUILD.bazel index f1c05e6871..ecbc1a9d24 100644 --- a/tools/rustfmt/BUILD.bazel +++ b/tools/rustfmt/BUILD.bazel @@ -36,7 +36,7 @@ rust_library( alias( name = "rustfmt", actual = ":target_aware_rustfmt", - deprecation = "Prefer //tools/rustfmt:target_aware_rustfmt", + deprecation = "Prefer //tools/upstream_wrapper:rustfmt", visibility = ["//visibility:public"], ) @@ -88,7 +88,7 @@ rust_clippy( # Deprecated but present for compatibility. alias( name = "upstream_rustfmt", - actual = "//tools/upstream_wrapper:upstream_rustfmt", - deprecation = "Prefer //tools/upstream_wrapper:upstream_rustfmt", + actual = "//tools/upstream_wrapper:rustfmt", + deprecation = "Prefer //tools/upstream_wrapper:rustfmt", visibility = ["//visibility:public"], ) diff --git a/tools/upstream_wrapper/BUILD.bazel b/tools/upstream_wrapper/BUILD.bazel index e1ccca8f75..e66ff514ad 100644 --- a/tools/upstream_wrapper/BUILD.bazel +++ b/tools/upstream_wrapper/BUILD.bazel @@ -8,11 +8,11 @@ tools = { "rustfmt": "//rust/toolchain:current_rustfmt_toolchain_for_target", } -# This is a small wrapper around the raw upstream binary which can be `bazel run`. +# Create wrappers around the tools that can be used `bazel run` or bazel_env. [ rust_binary( - name = "upstream_{}".format(tool_name), + name = tool_name, srcs = [ "src/main.rs", ],