Then you want to use the build script in the following:
hello_lib/BUILD:
package(default_visibility = ["//visibility:public"])
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
# This will run the build script from the root of the workspace, and
# collect the outputs.
cargo_build_script(
name = "build_script",
srcs = ["build.rs"],
# Optional environment variables passed during build.rs compilation
rustc_env = {
"CARGO_PKG_VERSION": "0.1.2",
},
# Optional environment variables passed during build.rs execution.
# Note that as the build script's working directory is not execroot,
# execpath/location will return an absolute path, instead of a relative
# one.
build_script_env = {
"SOME_TOOL_OR_FILE": "$(execpath @tool//:binary)"
},
# Optional data/tool dependencies
data = ["@tool//:binary"],
)
rust_library(
name = "hello_lib",
srcs = [
"src/lib.rs",
],
deps = [":build_script"],
)
The hello_lib target will be build with the flags and the environment variables declared by the build script in addition to the file generated by it.
PARAMETERS
Name
Description
Default Value
name
The name for the underlying rule. This should be the name of the package being compiled, optionally with a suffix of _bs. Otherwise, you can set the package name via pkg_name.
none
edition
The rust edition to use for the internal binary crate.
None
crate_name
Crate name to use for build script.
None
crate_root
The file that will be passed to rustc to be used for building this crate.
None
srcs
Source files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made.
[]
crate_features
A list of features to enable for the build script.
[]
version
The semantic version (semver) of the crate.
None
deps
The build-dependencies of the crate.
[]
link_deps
The subset of the (normal) dependencies of the crate that have the links attribute and therefore provide environment variables to this build script.
[]
proc_macro_deps
List of rust_proc_macro targets used to build the script.
[]
build_script_env
Environment variables for build scripts.
{}
build_script_env_files
Files containing additional environment variables to set when running the build script.
[]
emit_warnings
Whether to forward cargo::warning= lines from the build script to stderr. Honored only when @rules_rust//cargo/settings:emit_build_script_warnings is auto (the default); set the flag to on or off to override every target.
True
use_cc_toolchain
Whether or not to pull in the resolved cc_toolchain when running the build script.
When enabled, the resolved cc_toolchain's all_files are added to the action inputs and the CC, CXX, AR, CFLAGS, CXXFLAGS, LDFLAGS, and INCLUDE environment variables are populated from that toolchain (matching Cargo's normal behavior). Disabling this can significantly shrink build script action input trees (particularly with hermetic sysroots) but breaks any build script that needs to compile C/C++ code. If unset the global setting @rules_rust//cargo/settings:use_cc_toolchain will be used to determine this value.
None
use_default_shell_env
Whether or not to include the default shell environment for the build script action. If unset the global setting @rules_rust//cargo/settings:use_default_shell_env will be used to determine this value.
None
data
Files needed by the build script.
[]
compile_data
Files needed for the compilation of the build script.
[]
tools
Tools (executables) needed by the build script.
[]
links
Name of the native library this crate links against.
None
rundir
A directory to cd to before the cargo_build_script is run. This should be a path relative to the exec root.
The default behaviour (and the behaviour if rundir is set to the empty string) is to change to the relative path corresponding to the cargo manifest directory, which replicates the normal behaviour of cargo so it is easy to write compatible build scripts.
If set to ., the cargo build script will run in the exec root.
None
rustc_env
Environment variables to set in rustc when compiling the build script.
{}
rustc_env_files
Files containing additional environment variables to set for rustc when building the build script.
[]
rustc_flags
List of compiler flags passed to rustc.
[]
visibility
Visibility to apply to the generated build script output.
None
tags
(list of str, optional): Tags to apply to the generated build script output.
None
aliases
Remap crates to a new name or moniker for linkage to this target. These are other rust_library targets and will be presented as the new name given.
None
pkg_name
Override the package name used for the build script. This is useful if the build target name gets too long otherwise.
None
kwargs
Forwards to the underlying rust_binary rule. An exception is the compatible_with attribute, which shouldn't be forwarded to the rust_binary, as the rust_binary is only built and used in exec mode. We propagate the compatible_with attribute to the _build_script_run target.