Skip to content

Commit

Permalink
examples/bazel_env: Add an example
Browse files Browse the repository at this point in the history
...and simplify target names of wrappers
  • Loading branch information
Peter Kolloch committed Jun 18, 2024
1 parent 80797ab commit 4ef91dd
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 5 deletions.
12 changes: 12 additions & 0 deletions examples/bazel_env/.bazelrc
Original file line number Diff line number Diff line change
@@ -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
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-*
9 changes: 9 additions & 0 deletions examples/bazel_env/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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),
)
16 changes: 16 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.

12 changes: 12 additions & 0 deletions examples/bazel_env/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
42 changes: 42 additions & 0 deletions examples/bazel_env/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -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")
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
35 changes: 35 additions & 0 deletions examples/bazel_env/all_crate_deps_test.sh
Original file line number Diff line number Diff line change
@@ -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}"
11 changes: 11 additions & 0 deletions examples/bazel_env/env/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
},
)
18 changes: 18 additions & 0 deletions examples/bazel_env/src/main.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
6 changes: 3 additions & 3 deletions tools/rustfmt/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)

Expand Down Expand Up @@ -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"],
)
4 changes: 2 additions & 2 deletions tools/upstream_wrapper/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down

0 comments on commit 4ef91dd

Please sign in to comment.