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