<commit_before>load("@io_bazel_rules_rust//rust:private/rustc.bzl", "BuildInfo", "rustc_compile_action")
load("@io_bazel_rules_rust//rust:private/utils.bzl", "find_toolchain")
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary")

def _cargo_build_script_run(ctx, script):
    toolchain = find_toolchain(ctx)
    out_dir = ctx.actions.declare_directory(ctx.label.name + ".out_dir")
    env_out = ctx.actions.declare_file(ctx.label.name + ".env")
    flags_out = ctx.actions.declare_file(ctx.label.name + ".flags")
    manifest_dir = "%s.runfiles/%s" % (script.path, ctx.label.workspace_name or ctx.workspace_name)
    env = {
        "CARGO_MANIFEST_DIR": manifest_dir,
        "RUSTC": toolchain.rustc.path,
        "TARGET": toolchain.target_triple,
        "CARGO_CFG_TARGET_ARCH": toolchain.target_arch,
        "OUT_DIR": out_dir.path,
        "RUST_BACKTRACE": "full",
    }

    for f in ctx.attr.crate_features:
        env["CARGO_FEATURE_" + f.upper().replace("-", "_")] = "1"

    ctx.actions.run(
        executable = ctx.executable._cargo_build_script_runner,
        arguments = [script.path, env_out.path, flags_out.path],
        outputs = [out_dir, env_out, flags_out],
        tools = [script, ctx.executable._cargo_build_script_runner],
        inputs = [script, toolchain.rustc],
        mnemonic = "CargoBuildScriptRun",
        env = env,
    )

    return [
        BuildInfo(
            out_dir = out_dir,
            rustc_env = env_out,
            flags = flags_out,
        ),
    ]

def _build_script_impl(ctx):
    return _cargo_build_script_run(ctx, ctx.executable.script)

_build_script_run = rule(
    _build_script_impl,
    attrs = {
        "script": attr.label(
            executable = True,
            allow_files = True,
            mandatory = True,
            cfg = "host",
            doc = "The binary script to run, generally a rust_binary target. ",
        ),
        "crate_features": attr.string_list(doc = "The list of rust features that the build script should consider activated."),<commit_msg>Include rustc deps when running build scripts (#333)<commit_after>load("@io_bazel_rules_rust//rust:private/rustc.bzl", "BuildInfo", "rustc_compile_action")
load("@io_bazel_rules_rust//rust:private/utils.bzl", "find_toolchain")
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary")

def _cargo_build_script_run(ctx, script):
    toolchain = find_toolchain(ctx)
    out_dir = ctx.actions.declare_directory(ctx.label.name + ".out_dir")
    env_out = ctx.actions.declare_file(ctx.label.name + ".env")
    flags_out = ctx.actions.declare_file(ctx.label.name + ".flags")
    manifest_dir = "%s.runfiles/%s" % (script.path, ctx.label.workspace_name or ctx.workspace_name)
    env = {
        "CARGO_MANIFEST_DIR": manifest_dir,
        "RUSTC": toolchain.rustc.path,
        "TARGET": toolchain.target_triple,
        "CARGO_CFG_TARGET_ARCH": toolchain.target_arch,
        "OUT_DIR": out_dir.path,
        "RUST_BACKTRACE": "full",
    }

    for f in ctx.attr.crate_features:
        env["CARGO_FEATURE_" + f.upper().replace("-", "_")] = "1"

    tools = depset(
        direct = [
            script,
            ctx.executable._cargo_build_script_runner,
            toolchain.rustc,
        ],
        transitive = [
            # Needed for rustc to function.
            toolchain.rustc_lib.files,
            toolchain.rust_lib.files,
        ],
    )

    ctx.actions.run(
        executable = ctx.executable._cargo_build_script_runner,
        arguments = [script.path, env_out.path, flags_out.path],
        outputs = [out_dir, env_out, flags_out],
        tools = tools,
        mnemonic = "CargoBuildScriptRun",
        env = env,
    )

    return [
        BuildInfo(
            out_dir = out_dir,
            rustc_env = env_out,
            flags = flags_out,
        ),
    ]

def _build_script_impl(ctx):
    return _cargo_build_script_run(ctx, ctx.executable.script)

_build_script_run = rule(
    _build_script_impl,
    attrs = {
        "script": attr.label(
            executable = True,
            allow_files = True,
            mandatory = True,
            cfg = "host",
            doc = "The binary script to run, generally a rust_binary target. ",
        ),
        "crate_features": attr.string_list(doc = "The list of rust features that the build script should consider activated."),
{'fraction_non_alphanumeric': 0.12102493708533517, 'fraction_numerical': 0.0011439029970258523, 'mean_word_length': 2.6555183946488294, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1655847', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1434, 'n_words': 254}