fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
cbdf5400
main
1"""Rules for Cargo build scripts (`build.rs` files)""" 2 3load ( 4"//cargo/private:cargo_build_script.bzl" , 5"cargo_build_script_runfiles" , 6"name_to_crate_name" , 7"name_to_pkg_name" , 8_build_script_run = "cargo_build_script" , 9) 10load ( "//rust:defs.bzl" , "rust_binary" ) 11 12def cargo_build_script ( 13name , 14edition = None , 15crate_name = None , 16crate_root = None , 17srcs = [], 18crate_features = [], 19version = None , 20deps = [], 21link_deps = [], 22proc_macro_deps = [], 23build_script_env = {}, 24data = [], 25compile_data = [], 26tools = [], 27links = None , 28rundir = None , 29rustc_env = {}, 30rustc_env_files = [], 31rustc_flags = [], 32visibility = None , 33tags = None , 34aliases = None , 35pkg_name = None , 36** kwargs ): 37"""Compile and execute a rust build script to generate build attributes 38 39This rules take the same arguments as rust_binary. 40 41Example: 42 43Suppose you have a crate with a cargo build script `build.rs`: 44 45```output 46[workspace]/ 47hello_lib/ 48BUILD 49build.rs 50src/ 51lib.rs 52``` 53 54Then you want to use the build script in the following: 55 56`hello_lib/BUILD`: 57```python 58package(default_visibility = ["//visibility:public"]) 59 60load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library") 61load("@rules_rust//cargo:defs.bzl", "cargo_build_script") 62 63# This will run the build script from the root of the workspace, and 64# collect the outputs. 65cargo_build_script( 66name = "build_script", 67srcs = ["build.rs"], 68# Optional environment variables passed during build.rs compilation 69rustc_env = { 70"CARGO_PKG_VERSION": "0.1.2", 71}, 72# Optional environment variables passed during build.rs execution. 73# Note that as the build script's working directory is not execroot, 74# execpath/location will return an absolute path, instead of a relative 75# one. 76build_script_env = { 77"SOME_TOOL_OR_FILE": "$(execpath @tool//:binary)" 78}, 79# Optional data/tool dependencies 80data = ["@tool//:binary"], 81) 82 83rust_library( 84name = "hello_lib", 85srcs = [ 86"src/lib.rs", 87], 88deps = [":build_script"], 89) 90``` 91 92The `hello_lib` target will be build with the flags and the environment variables declared by the \ 93build script in addition to the file generated by it. 94 95Args: 96name (str): The name for the underlying rule. This should be the name of the package 97being compiled, optionally with a suffix of `_bs`. Otherwise, you can set the package name via `pkg_name`. 98edition (str): The rust edition to use for the internal binary crate. 99crate_name (str): Crate name to use for build script. 100crate_root (label): The file that will be passed to rustc to be used for building this crate. 101srcs (list of label): Souce files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made. 102crate_features (list, optional): A list of features to enable for the build script. 103version (str, optional): The semantic version (semver) of the crate. 104deps (list, optional): The build-dependencies of the crate. 105pkg_name (string, optional): Override the package name used for the build script. This is useful if the build target name gets too long otherwise. 106link_deps (list, optional): The subset of the (normal) dependencies of the crate that have the 107links attribute and therefore provide environment variables to this build script. 108proc_macro_deps (list of label, optional): List of rust_proc_macro targets used to build the script. 109build_script_env (dict, optional): Environment variables for build scripts. 110data (list, optional): Files needed by the build script. 111compile_data (list, optional): Files needed for the compilation of the build script. 112tools (list, optional): Tools (executables) needed by the build script. 113links (str, optional): Name of the native library this crate links against. 114rundir (str, optional): A directory to `cd` to before the cargo_build_script is run. This should be a path relative to the exec root. 115 116The 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. 117 118If set to `.`, the cargo build script will run in the exec root. 119rustc_env (dict, optional): Environment variables to set in rustc when compiling the build script. 120rustc_env_files (list of label, optional): Files containing additional environment variables to set for rustc 121when building the build script. 122rustc_flags (list, optional): List of compiler flags passed to `rustc`. 123visibility (list of label, optional): Visibility to apply to the generated build script output. 124tags: (list of str, optional): Tags to apply to the generated build script output. 125aliases (dict, optional): Remap crates to a new name or moniker for linkage to this target. \ 126These are other `rust_library` targets and will be presented as the new name given. 127**kwargs: Forwards to the underlying `rust_binary` rule. An exception is the `compatible_with` 128attribute, which shouldn't be forwarded to the `rust_binary`, as the `rust_binary` is only 129built and used in `exec` mode. We propagate the `compatible_with` attribute to the `_build_scirpt_run` 130target. 131""" 132 133# This duplicates the code in _cargo_build_script_impl because we need to make these 134# available both when we invoke rustc (this code) and when we run the compiled build 135# script (_cargo_build_script_impl). https://github.com/bazelbuild/rules_rust/issues/661 136# will hopefully remove this duplication. 137if pkg_name == None : 138pkg_name = name_to_pkg_name ( name ) 139 140rustc_env = dict ( rustc_env ) 141if "CARGO_PKG_NAME" not in rustc_env : 142rustc_env [ "CARGO_PKG_NAME" ] = pkg_name 143if "CARGO_CRATE_NAME" not in rustc_env : 144rustc_env [ "CARGO_CRATE_NAME" ] = name_to_crate_name ( name_to_pkg_name ( name )) 145 146script_kwargs = {} 147for arg in ( "exec_compatible_with" , "testonly" ): 148if arg in kwargs : 149script_kwargs [ arg ] = kwargs [ arg ] 150 151wrapper_kwargs = dict ( script_kwargs ) 152for arg in ( "compatible_with" , "target_compatible_with" ): 153if arg in kwargs : 154wrapper_kwargs [ arg ] = kwargs [ arg ] 155 156binary_tags = depset ( 157( tags if tags else []) + [ "manual" ], 158). to_list () 159 160# This target exists as the actual build script. 161rust_binary ( 162name = name + "_" , 163crate_name = crate_name , 164srcs = srcs , 165crate_root = crate_root , 166crate_features = crate_features , 167deps = deps , 168proc_macro_deps = proc_macro_deps , 169data = tools , 170compile_data = compile_data , 171rustc_env = rustc_env , 172rustc_env_files = rustc_env_files , 173rustc_flags = rustc_flags , 174edition = edition , 175tags = binary_tags , 176aliases = aliases , 177** script_kwargs 178) 179 180# Because the build script is expected to be run on the exec host, the 181# script above needs to be in the exec configuration but the script may 182# need data files that are in the target configuration. This rule wraps 183# the script above so the `cfg=exec` target can be run without issue in 184# a `cfg=target` environment. More details can be found on the rule. 185cargo_build_script_runfiles ( 186name = name + "-" , 187script = ":{}_" . format ( name ), 188data = data , 189tools = tools , 190tags = binary_tags , 191** wrapper_kwargs 192) 193 194# This target executes the build script. 195_build_script_run ( 196name = name , 197script = ":{}-" . format ( name ), 198crate_features = crate_features , 199version = version , 200build_script_env = build_script_env , 201links = links , 202deps = deps , 203link_deps = link_deps , 204rundir = rundir , 205rustc_flags = rustc_flags , 206visibility = visibility , 207tags = tags , 208pkg_name = pkg_name , 209** kwargs 210)