fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
3d65214d
0.9.0
1"""Common utilities useful for unifying the behavior of different parts of `cargo-bazel`.""" 2 3# buildifier: disable=bzl-visibility 4load ( "//cargo/private:cargo_utils.bzl" , _rust_get_rust_tools = "get_rust_tools" ) 5load ( "//rust/platform:triple.bzl" , _get_host_triple = "get_host_triple" ) 6 7get_host_triple = _get_host_triple 8 9CARGO_BAZEL_ISOLATED = "CARGO_BAZEL_ISOLATED" 10CARGO_BAZEL_REPIN = "CARGO_BAZEL_REPIN" 11REPIN = "REPIN" 12 13REPIN_ENV_VARS = [ 14CARGO_BAZEL_REPIN , 15REPIN , 16] 17 18_EXECUTE_ERROR_MESSAGE = """\ 19Command {args} failed with exit code {exit_code}. 20STDOUT ------------------------------------------------------------------------ 21{stdout} 22STDERR ------------------------------------------------------------------------ 23{stderr} 24""" 25 26def execute ( repository_ctx , args , env = {}): 27"""A heler macro for executing some arguments and displaying nicely formatted errors 28 29Args: 30repository_ctx (repository_ctx): The rule's context object. 31args (list): A list of strings which act as `argv` for execution. 32env (dict, optional): Environment variables to set in the execution environment. 33 34Returns: 35struct: The results of `repository_ctx.execute` 36""" 37result = repository_ctx . execute ( 38args , 39environment = env , 40quiet = repository_ctx . attr . quiet , 41) 42 43if result . return_code : 44fail ( _EXECUTE_ERROR_MESSAGE . format ( 45args = args , 46exit_code = result . return_code , 47stdout = result . stdout , 48stderr = result . stderr , 49)) 50 51return result 52 53def get_rust_tools ( repository_ctx , host_triple ): 54"""Retrieve a cargo and rustc binary based on the host triple. 55 56Args: 57repository_ctx (repository_ctx): The rule's context object. 58host_triple (struct): A `@rules_rust//rust:triple.bzl%triple` object. 59 60Returns: 61struct: A struct containing the expected rust tools 62""" 63 64# This is a bit hidden but to ensure Cargo behaves consistently based 65# on the user provided config file, the config file is installed into 66# the `CARGO_HOME` path. This is done so here since fetching tools 67# is expected to always occur before any subcommands are run. 68if repository_ctx . attr . isolated and repository_ctx . attr . cargo_config : 69cargo_home = _cargo_home_path ( repository_ctx ) 70cargo_home_config = repository_ctx . path ( "{}/config.toml" . format ( cargo_home )) 71cargo_config = repository_ctx . path ( repository_ctx . attr . cargo_config ) 72repository_ctx . symlink ( cargo_config , cargo_home_config ) 73 74return _rust_get_rust_tools ( 75cargo_template = repository_ctx . attr . rust_toolchain_cargo_template , 76rustc_template = repository_ctx . attr . rust_toolchain_rustc_template , 77host_triple = host_triple , 78version = repository_ctx . attr . rust_version , 79) 80 81def _cargo_home_path ( repository_ctx ): 82"""Define a path within the repository to use in place of `CARGO_HOME` 83 84Args: 85repository_ctx (repository_ctx): The rules context object 86 87Returns: 88path: The path to a directory to use as `CARGO_HOME` 89""" 90return repository_ctx . path ( ".cargo_home" ) 91 92def cargo_environ ( repository_ctx ): 93"""Define Cargo environment varables for use with `cargo-bazel` 94 95Args: 96repository_ctx (repository_ctx): The rules context object 97 98Returns: 99dict: A set of environment variables for `cargo-bazel` executions 100""" 101env = dict () 102 103if CARGO_BAZEL_ISOLATED in repository_ctx . os . environ : 104if repository_ctx . os . environ [ CARGO_BAZEL_ISOLATED ]. lower () in [ "true" , "1" , "yes" , "on" ]: 105env . update ({ 106"CARGO_HOME" : str ( _cargo_home_path ( repository_ctx )), 107}) 108elif repository_ctx . attr . isolated : 109env . update ({ 110"CARGO_HOME" : str ( _cargo_home_path ( repository_ctx )), 111}) 112 113return env