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