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