"""Utilities directly related to the `generate` step of `cargo-bazel`.""" load(":common_utils.bzl", "CARGO_BAZEL_DEBUG", "CARGO_BAZEL_ISOLATED", "REPIN_ALLOWLIST_ENV_VAR", "REPIN_ENV_VARS", "cargo_environ", "execute", "parse_alias_rule") CARGO_BAZEL_GENERATOR_SHA256 = "CARGO_BAZEL_GENERATOR_SHA256" CARGO_BAZEL_GENERATOR_URL = "CARGO_BAZEL_GENERATOR_URL" GENERATOR_ENV_VARS = [ CARGO_BAZEL_GENERATOR_URL, CARGO_BAZEL_GENERATOR_SHA256, ] CRATES_REPOSITORY_ENVIRON = GENERATOR_ENV_VARS + REPIN_ENV_VARS + [ REPIN_ALLOWLIST_ENV_VAR, CARGO_BAZEL_ISOLATED, CARGO_BAZEL_DEBUG, ] def get_generator(repository_ctx, host_triple): """Query network resources to locate a `cargo-bazel` binary Args: repository_ctx (repository_ctx): The rule's context object. host_triple (string): A string representing the host triple Returns: tuple(path, dict): The path to a `cargo-bazel` binary and the host sha256 pairing. The pairing (dict) may be `None` if there is no need to update the attribute """ use_environ = False for var in GENERATOR_ENV_VARS: if var in repository_ctx.os.environ: use_environ = True output = repository_ctx.path("cargo-bazel.exe" if "win" in repository_ctx.os.name else "cargo-bazel") # The `generator` attribute is the next highest priority behind # environment variables. We check those first before deciding to # use an explicitly provided variable. if not use_environ and repository_ctx.attr.generator: generator = repository_ctx.path(Label(repository_ctx.attr.generator)) # Resolve a few levels of symlinks to ensure we're accessing the direct binary for _ in range(1, 100): real_generator = generator.realpath if real_generator == generator: break generator = real_generator return generator, None # The environment variable will take precedence if set if use_environ: generator_sha256 = repository_ctx.os.environ.get(CARGO_BAZEL_GENERATOR_SHA256) generator_url = repository_ctx.os.environ.get(CARGO_BAZEL_GENERATOR_URL) else: generator_sha256 = repository_ctx.attr.generator_sha256s.get(host_triple) generator_url = repository_ctx.attr.generator_urls.get(host_triple) if not generator_url: fail(( "No generator URL was found either in the `CARGO_BAZEL_GENERATOR_URL` " + "environment variable or for the `{}` triple in the `generator_urls` attribute" ).format(host_triple)) # Download the file into place if generator_sha256: repository_ctx.download( output = output, url = generator_url, sha256 = generator_sha256, executable = True, ) return output, None result = repository_ctx.download( output = output, url = generator_url, executable = True, ) return output, {host_triple: result.sha256} def render_config( build_file_template = "//:BUILD.{name}-{version}.bazel", crate_label_template = "@{repository}__{name}-{version}//:{target}", crate_repository_template = "{repository}__{name}-{version}", crates_module_template = "//:{file}", default_alias_rule = "alias", default_package_name = None, generate_target_compatible_with = True, platforms_template = "@rules_rust//rust/platform:{triple}", regen_command = None, vendor_mode = None, generate_rules_license_metadata = False): """Various settings used to configure rendered outputs The template parameters each support a select number of format keys. A description of each key can be found below where the supported keys for each template can be found in the parameter docs | key | definition | | --- | --- | | `name` | The name of the crate. Eg `tokio` | | `repository` | The rendered repository name for the crate. Directly relates to `crate_repository_template`. | | `triple` | A platform triple. Eg `x86_64-unknown-linux-gnu` | | `version` | The crate version. Eg `1.2.3` | | `target` | The library or binary target of the crate | | `file` | The basename of a file | Args: build_file_template (str, optional): The base template to use for BUILD file names. The available format keys are [`{name}`, {version}`]. crate_label_template (str, optional): The base template to use for crate labels. The available format keys are [`{repository}`, `{name}`, `{version}`, `{target}`]. crate_repository_template (str, optional): The base template to use for Crate label repository names. The available format keys are [`{repository}`, `{name}`, `{version}`]. crates_module_template (str, optional): The pattern to use for the `defs.bzl` and `BUILD.bazel` file names used for the crates module. The available format keys are [`{file}`]. default_alias_rule (str, option): Alias rule to use when generating aliases for all crates. Acceptable values are 'alias', 'dbg'/'fastbuild'/'opt' (transitions each crate's `compilation_mode`) or a string representing a rule in the form '