fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
64502533
main
1"""Rules for Cargo build scripts (`build.rs` files)""" 2 3load ( "//rust:rust_common.bzl" , "BuildInfo" , "DepInfo" ) 4 5# buildifier: disable=bzl-visibility 6load ( "//rust/private:utils.bzl" , "dedent" ) 7 8def _cargo_dep_env_impl ( ctx ): 9empty_file = ctx . actions . declare_file ( ctx . label . name + ".empty_file" ) 10empty_dir = ctx . actions . declare_directory ( ctx . label . name + ".empty_dir" ) 11ctx . actions . write ( 12output = empty_file , 13content = "" , 14) 15ctx . actions . run ( 16outputs = [ empty_dir ], 17executable = "true" , 18) 19 20build_infos = [] 21out_dir = ctx . file . out_dir 22if out_dir : 23if not out_dir . is_directory : 24fail ( "out_dir must be a directory artifact" ) 25 26# BuildInfos in this list are collected up for all transitive cargo_build_script 27# dependencies. This is important for any flags set in `dep_env` which reference this 28# `out_dir`. 29# 30# TLDR: This BuildInfo propagates up build script dependencies. 31build_infos . append ( BuildInfo ( 32dep_env = empty_file , 33flags = empty_file , 34linker_flags = empty_file , 35link_search_paths = empty_file , 36out_dir = out_dir , 37rustc_env = empty_file , 38compile_data = depset ([]), 39)) 40return [ 41DefaultInfo ( files = depset ( ctx . files . src )), 42# Parts of this BuildInfo is used when building all transitive dependencies 43# (cargo_build_script and otherwise), alongside the DepInfo. This is how other rules 44# identify this one as a valid dependency, but we don't otherwise have a use for it. 45# 46# TLDR: This BuildInfo propagates up normal (non build script) depenencies. 47# 48# In the future, we could consider setting rustc_env here, and also propagating dep_dir 49# so files in it can be referenced there. 50BuildInfo ( 51dep_env = empty_file , 52flags = empty_file , 53linker_flags = empty_file , 54link_search_paths = empty_file , 55out_dir = None , 56rustc_env = empty_file , 57compile_data = depset ([]), 58), 59# Information here is used directly by dependencies, and it is an error to have more than 60# one dependency which sets this. This is the main way to specify information from build 61# scripts, which is what we're looking to do. 62DepInfo ( 63dep_env = ctx . file . src , 64direct_crates = depset (), 65link_search_path_files = depset (), 66transitive_build_infos = depset ( direct = build_infos ), 67transitive_crate_outputs = depset (), 68transitive_crates = depset (), 69transitive_noncrates = depset (), 70), 71] 72 73cargo_dep_env = rule ( 74implementation = _cargo_dep_env_impl , 75doc = ( 76"A rule for generating variables for dependent `cargo_build_script`s " + 77"without a build script. This is useful for using Bazel rules instead " + 78"of a build script, while also generating configuration information " + 79"for build scripts which depend on this crate." 80), 81attrs = { 82"out_dir" : attr . label ( 83doc = dedent ( """\ 84Folder containing additional inputs when building all direct dependencies. 85 86This has the same effect as a `cargo_build_script` which prints 87puts files into `$OUT_DIR`, but without requiring a build script. 88""" ), 89allow_single_file = True , 90mandatory = False , 91), 92"src" : attr . label ( 93doc = dedent ( """\ 94 File containing additional environment variables to set for build scripts of direct dependencies. 95 96 This has the same effect as a `cargo_build_script` which prints 97 `cargo:VAR=VALUE` lines, but without requiring a build script. 98 99 This files should contain a single variable per line, of format 100 `NAME=value`, and newlines may be included in a value by ending a 101 line with a trailing back-slash (`\\\\`). 102 """), 103 allow_single_file = True, 104 mandatory = True, 105 ), 106 }, 107)