fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
941c7cca
rust-analyzer
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 , 13crate_features = [], 14version = None , 15deps = [], 16build_script_env = {}, 17data = [], 18tools = [], 19links = None , 20rustc_env = {}, 21rustc_flags = [], 22visibility = None , 23tags = None , 24** kwargs ): 25"""Compile and execute a rust build script to generate build attributes 26 27This rules take the same arguments as rust_binary. 28 29Example: 30 31Suppose you have a crate with a cargo build script `build.rs`: 32 33```output 34[workspace]/ 35hello_lib/ 36BUILD 37build.rs 38src/ 39lib.rs 40``` 41 42Then you want to use the build script in the following: 43 44`hello_lib/BUILD`: 45```python 46package(default_visibility = ["//visibility:public"]) 47 48load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library") 49load("@rules_rust//cargo:defs.bzl", "cargo_build_script") 50 51# This will run the build script from the root of the workspace, and 52# collect the outputs. 53cargo_build_script( 54name = "build_script", 55srcs = ["build.rs"], 56# Optional environment variables passed during build.rs compilation 57rustc_env = { 58"CARGO_PKG_VERSION": "0.1.2", 59}, 60# Optional environment variables passed during build.rs execution. 61# Note that as the build script's working directory is not execroot, 62# execpath/location will return an absolute path, instead of a relative 63# one. 64build_script_env = { 65"SOME_TOOL_OR_FILE": "$(execpath @tool//:binary)" 66} 67# Optional data/tool dependencies 68data = ["@tool//:binary"], 69) 70 71rust_library( 72name = "hello_lib", 73srcs = [ 74"src/lib.rs", 75], 76deps = [":build_script"], 77) 78``` 79 80The `hello_lib` target will be build with the flags and the environment variables declared by the \ 81build script in addition to the file generated by it. 82 83Args: 84name (str): The name for the underlying rule. This should be the name of the package 85being compiled, optionally with a suffix of `_build_script`. 86crate_features (list, optional): A list of features to enable for the build script. 87version (str, optional): The semantic version (semver) of the crate. 88deps (list, optional): The dependencies of the crate. 89build_script_env (dict, optional): Environment variables for build scripts. 90data (list, optional): Files needed by the build script. 91tools (list, optional): Tools (executables) needed by the build script. 92links (str, optional): Name of the native library this crate links against. 93rustc_env (dict, optional): Environment variables to set in rustc when compiling the build script. 94rustc_flags (list, optional): List of compiler flags passed to `rustc`. 95visibility (list of label, optional): Visibility to apply to the generated build script output. 96tags: (list of str, optional): Tags to apply to the generated build script output. 97**kwargs: Forwards to the underlying `rust_binary` rule. An exception is the `compatible_with` 98attribute, which shouldn't be forwarded to the `rust_binary`, as the `rust_binary` is only 99built and used in `exec` mode. We propagate the `compatible_with` attribute to the `_build_scirpt_run` 100target. 101""" 102 103# This duplicates the code in _cargo_build_script_impl because we need to make these 104# available both when we invoke rustc (this code) and when we run the compiled build 105# script (_cargo_build_script_impl). https://github.com/bazelbuild/rules_rust/issues/661 106# will hopefully remove this duplication. 107rustc_env = dict ( rustc_env ) 108if "CARGO_PKG_NAME" not in rustc_env : 109rustc_env [ "CARGO_PKG_NAME" ] = name_to_pkg_name ( name ) 110if "CARGO_CRATE_NAME" not in rustc_env : 111rustc_env [ "CARGO_CRATE_NAME" ] = name_to_crate_name ( name_to_pkg_name ( name )) 112 113binary_tags = [ tag for tag in tags or []] 114if "manual" not in binary_tags : 115binary_tags . append ( "manual" ) 116build_script_kwargs = {} 117binary_kwargs = kwargs 118if "compatible_with" in kwargs : 119build_script_kwargs [ "compatible_with" ] = kwargs [ "compatible_with" ] 120binary_kwargs . pop ( "compatible_with" ) 121 122rust_binary ( 123name = name + "_" , 124crate_features = crate_features , 125version = version , 126deps = deps , 127data = data , 128rustc_env = rustc_env , 129rustc_flags = rustc_flags , 130tags = binary_tags , 131** binary_kwargs 132) 133_build_script_run ( 134name = name , 135script = ":{}_" . format ( name ), 136crate_features = crate_features , 137version = version , 138build_script_env = build_script_env , 139links = links , 140deps = deps , 141data = data , 142tools = tools , 143rustc_flags = rustc_flags , 144visibility = visibility , 145tags = tags , 146** build_script_kwargs 147)