Link flags are passed to transitive rdeps (#346)

And absolute paths are redacted into $EXEC_ROOT-relative paths

This mirrors Cargo's behaviour of accumulating link flags and passing
them to the linker - otherwise there's not much point in sys libraries
outputting them.

This currently over-adds out_dirs and link flags to all actions, rather
than just those that will invoke the linker - we can reduce that if we
have a clear indication of what targets will invoke a linker, but I
wanted to go with a safe-but-over-invalidating solution rather than
picking a bad heuristic.
15 files changed
tree: c1d10f209e4a527939e1d67eb642d3ade559eb95
  1. .bazelci/
  2. bindgen/
  3. cargo/
  4. docs/
  5. examples/
  6. proto/
  7. rust/
  8. test/
  9. tools/
  10. util/
  11. wasm_bindgen/
  12. .gitignore
  13. AUTHORS
  14. BUILD
  15. CODEOWNERS
  16. CONTRIBUTING.md
  17. CONTRIBUTORS
  18. libc.BUILD
  19. LICENSE.txt
  20. package-lock.json
  21. package.json
  22. README.md
  23. renovate.json
  24. WORKSPACE
  25. workspace.bzl
README.md

Rust Rules

  • Postsubmit Build status
  • Postsubmit + Current Bazel Incompatible Flags Build status

Overview

This repository provides rules for building Rust projects with Bazel.

Basics

WebAssembly

To build a rust_binary for wasm32-unknown-unknown add the --platforms=//rust/platform:wasm flag.

bazel build @examples//hello_world_wasm --platforms=//rust/platform:wasm

rust_wasm_bindgen will automatically transition to the wasm platform and can be used when building wasm code for the host target.

Protobuf

with an overview here.

Setup

To use the Rust rules, add the following to your WORKSPACE file to add the external repositories for the Rust toolchain:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "io_bazel_rules_rust",
    sha256 = "b6da34e057a31b8a85e343c732de4af92a762f804fc36b0baa6c001423a70ebc",
    strip_prefix = "rules_rust-55f77017a7f5b08e525ebeab6e11d8896a4499d2",
    urls = [
        # Master branch as of 2019-10-07
        "https://github.com/bazelbuild/rules_rust/archive/55f77017a7f5b08e525ebeab6e11d8896a4499d2.tar.gz",
    ],
)

http_archive(
    name = "bazel_skylib",
    sha256 = "9a737999532daca978a158f94e77e9af6a6a169709c0cee274f0a4c3359519bd",
    strip_prefix = "bazel-skylib-1.0.0",
    url = "https://github.com/bazelbuild/bazel-skylib/archive/1.0.0.tar.gz",
)

load("@io_bazel_rules_rust//rust:repositories.bzl", "rust_repositories")
rust_repositories()

load("@io_bazel_rules_rust//:workspace.bzl", "bazel_version")
bazel_version(name = "bazel_version")

The rules are under active development, as such the lastest commit on the master branch should be used. master currently requires Bazel >= 0.26.0.

Specifying Rust version

To build with a particular version of the Rust compiler, pass that version to rust_repositories:

rust_repositories(version = "1.42.0", edition="2018")

As well as an exact version, version can be set to "nightly" or "beta". If set to these values, iso_date must also be set:

rust_repositories(version = "nightly", iso_date = "2020-04-19", edition="2018")

Similarly, rustfmt_version may also be configured:

rust_repositories(rustfmt_version = "1.4.8")

External Dependencies

Currently the most common approach to managing external dependencies is using cargo-raze to generate BUILD files for Cargo crates.

Roadmap

  • Improve expressiveness of features and support for Cargo's feature groups.
  • Add cargo_crate workspace rule for pulling crates from Cargo.