diff --git a/cargo/BUILD.bazel b/cargo/BUILD.bazel index 6c21ee13f9..8f5718204d 100644 --- a/cargo/BUILD.bazel +++ b/cargo/BUILD.bazel @@ -1,9 +1,13 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") -package(default_visibility = ["//visibility:public"]) +exports_files(["defs.bzl"]) bzl_library( name = "bzl_lib", - srcs = glob(["**/*.bzl"]), - deps = ["//cargo/private:bzl_lib"], + srcs = glob(["*.bzl"]), + visibility = ["//visibility:public"], + deps = [ + "//cargo/private:bzl_lib", + "//cargo/settings:bzl_lib", + ], ) diff --git a/cargo/defs.bzl b/cargo/defs.bzl index 1588a6c7c4..15c199c0ad 100644 --- a/cargo/defs.bzl +++ b/cargo/defs.bzl @@ -1,4 +1,7 @@ -"""Common definitions for the `@rules_rust//cargo` package""" +"""# Cargo + +Common definitions for the `@rules_rust//cargo` package +""" load( "//cargo/private:cargo_bootstrap.bzl", diff --git a/cargo/settings/BUILD.bazel b/cargo/settings/BUILD.bazel index 63ca4b5dc2..21d6cb7ef4 100644 --- a/cargo/settings/BUILD.bazel +++ b/cargo/settings/BUILD.bazel @@ -1,43 +1,32 @@ -load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_list_flag") +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") +load( + ":settings.bzl", + "cargo_manifest_dir_filename_suffixes_to_retain", + "debug_std_streams_output_group", + "experimental_symlink_execroot", + "incompatible_runfiles_cargo_manifest_dir", + "use_default_shell_env", +) package(default_visibility = ["//visibility:public"]) -# A flag for which causes `cargo_build_script` to symlink the execroot of the action to -# the `CARGO_MANIFEST_DIR` where the scripts are run. -bool_flag( - name = "experimental_symlink_execroot", - build_setting_default = False, -) +exports_files(["settings.bzl"]) -# A flag which causes `cargo_build_script` to write an explicit `CARGO_MANFIEST_DIR` -# directory from an action instead of using runfiles directories which cannot be -# passed to downstream actions. -# https://github.com/bazelbuild/bazel/issues/15486 -bool_flag( - name = "incompatible_runfiles_cargo_manifest_dir", - build_setting_default = True, -) - -# A flag which determines what files are retained in `CARGO_MANIFEST_DIR` directories -# that are created in `CargoBuildScriptRun` actions. -string_list_flag( - name = "cargo_manifest_dir_filename_suffixes_to_retain", - build_setting_default = [ - ".lib", - ".so", +bzl_library( + name = "bzl_lib", + srcs = glob(["*.bzl"]), + visibility = ["//cargo:__pkg__"], + deps = [ + "@bazel_skylib//rules:common_settings", ], ) -# A flag which adds a `streams` output group to `cargo_build_script` targets that contain -# the raw `stderr` and `stdout` streams from the build script. -bool_flag( - name = "debug_std_streams_output_group", - build_setting_default = False, -) +cargo_manifest_dir_filename_suffixes_to_retain() -# A flag which controls the global default of `ctx.actions.run.use_default_shell_env` -# for `cargo_build_script` targets. -bool_flag( - name = "use_default_shell_env", - build_setting_default = True, -) +debug_std_streams_output_group() + +experimental_symlink_execroot() + +incompatible_runfiles_cargo_manifest_dir() + +use_default_shell_env() diff --git a/cargo/settings/settings.bzl b/cargo/settings/settings.bzl new file mode 100644 index 0000000000..c0bbf6180e --- /dev/null +++ b/cargo/settings/settings.bzl @@ -0,0 +1,56 @@ +"""# Cargo settings + +Definitions for all `@rules_rust//cargo` settings +""" + +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_list_flag") + +def experimental_symlink_execroot(): + """A flag for which causes `cargo_build_script` to symlink the execroot of the action to \ + the `CARGO_MANIFEST_DIR` where the scripts are run. + """ + bool_flag( + name = "experimental_symlink_execroot", + build_setting_default = False, + ) + +def incompatible_runfiles_cargo_manifest_dir(): + """A flag which causes `cargo_build_script` to write an explicit `CARGO_MANFIEST_DIR` \ + directory from an action instead of using runfiles directories which cannot be \ + passed to downstream actions. + + https://github.com/bazelbuild/bazel/issues/15486 + """ + bool_flag( + name = "incompatible_runfiles_cargo_manifest_dir", + build_setting_default = True, + ) + +def cargo_manifest_dir_filename_suffixes_to_retain(): + """A flag which determines what files are retained in `CARGO_MANIFEST_DIR` directories \ + that are created in `CargoBuildScriptRun` actions. + """ + string_list_flag( + name = "cargo_manifest_dir_filename_suffixes_to_retain", + build_setting_default = [ + ".lib", + ".so", + ], + ) + +def debug_std_streams_output_group(): + """A flag which adds a `streams` output group to `cargo_build_script` targets that contain \ + the raw `stderr` and `stdout` streams from the build script. + """ + bool_flag( + name = "debug_std_streams_output_group", + build_setting_default = False, + ) + +def use_default_shell_env(): + """A flag which controls the global default of `ctx.actions.run.use_default_shell_env` for `cargo_build_script` targets. + """ + bool_flag( + name = "use_default_shell_env", + build_setting_default = True, + ) diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index 77b3a9a560..337945f875 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -2,7 +2,6 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load("@rules_rust_mdbook//:defs.bzl", "mdbook", "mdbook_server") load("@rules_shell//shell:sh_binary.bzl", "sh_binary") load("@stardoc//stardoc:stardoc.bzl", "stardoc") -load("//:page.bzl", "gen_header", "page") package(default_visibility = ["//visibility:private"]) @@ -33,127 +32,131 @@ bzl_library( ], ) -PAGES = dict([ - page( - name = "cargo", - symbols = [ - "cargo_bootstrap_repository", - "cargo_build_script", - "cargo_dep_env", - "cargo_env", - ], - ), - page( - name = "rust", - symbols = [ - "rust_binary", - "rust_library", - "rust_library_group", - "rust_static_library", - "rust_shared_library", - "rust_proc_macro", - "rust_test", - "rust_test_suite", - "error_format", - "extra_rustc_flag", - "extra_rustc_flags", - "capture_clippy_output", - ], - ), - page( - name = "providers", - symbols = [ - "CrateInfo", - "DepInfo", - "StdLibInfo", - ], - ), - page( - name = "rust_analyzer", - header_template = ":rust_analyzer.vm", - symbols = [ - "rust_analyzer_aspect", - "rust_analyzer_toolchain", - ], - ), - page( - name = "rust_clippy", - header_template = ":rust_clippy.vm", - symbols = [ - "rust_clippy", - "rust_clippy_aspect", - ], - ), - page( - name = "rust_doc", - symbols = [ - "rust_doc", - "rust_doc_test", - ], - ), - page( - name = "rust_fmt", - header_template = ":rust_fmt.vm", - symbols = [ - "rustfmt_aspect", - "rustfmt_test", - "rustfmt_toolchain", - ], - ), - page( - name = "rust_repositories", - symbols = [ - "rules_rust_dependencies", - "rust_analyzer_toolchain_repository", - "rust_register_toolchains", - "rust_repositories", - "rust_repository_set", - "rust_stdlib_filegroup", - "rust_toolchain_repository_proxy", - "rust_toolchain_repository", - "rust_toolchain_tools_repository", - "rust_toolchain", - ], - ), - page( - name = "settings", - symbols = [ - "incompatible_flag", - "fail_when_enabled", - ], - ), -]) +stardoc( + name = "cargo", + out = "cargo.md", + input = "@rules_rust//cargo:defs.bzl", + symbol_names = [ + "cargo_bootstrap_repository", + "cargo_build_script", + "cargo_dep_env", + "cargo_env", + ], + table_of_contents_template = "@stardoc//stardoc:templates/markdown_tables/table_of_contents.vm", + deps = [":all_docs"], +) + +stardoc( + name = "rust", + out = "rust.md", + input = "@rules_rust//rust:defs.bzl", + symbol_names = [ + "rust_binary", + "rust_library", + "rust_library_group", + "rust_static_library", + "rust_shared_library", + "rust_proc_macro", + "rust_test", + "rust_test_suite", + ], + table_of_contents_template = "@stardoc//stardoc:templates/markdown_tables/table_of_contents.vm", + deps = [":all_docs"], +) + +stardoc( + name = "providers", + out = "providers.md", + input = "@rules_rust//rust/private:providers.bzl", + symbol_names = [ + "CrateInfo", + "DepInfo", + "StdLibInfo", + ], + table_of_contents_template = "@stardoc//stardoc:templates/markdown_tables/table_of_contents.vm", + deps = [":all_docs"], +) + +stardoc( + name = "rust_analyzer", + out = "rust_analyzer.md", + header_template = ":rust_analyzer.vm", + input = "@rules_rust//rust:defs.bzl", + symbol_names = [ + "rust_analyzer_aspect", + ], + table_of_contents_template = "@stardoc//stardoc:templates/markdown_tables/table_of_contents.vm", + deps = [":all_docs"], +) + +stardoc( + name = "rust_clippy", + out = "rust_clippy.md", + header_template = ":rust_clippy.vm", + input = "@rules_rust//rust:defs.bzl", + symbol_names = [ + "rust_clippy", + "rust_clippy_aspect", + ], + table_of_contents_template = "@stardoc//stardoc:templates/markdown_tables/table_of_contents.vm", + deps = [":all_docs"], +) -# Generate headers for each page -[gen_header(page = p) for p in PAGES.values()] +stardoc( + name = "rust_doc", + out = "rust_doc.md", + input = "@rules_rust//rust:defs.bzl", + symbol_names = [ + "rust_doc", + "rust_doc_test", + ], + table_of_contents_template = "@stardoc//stardoc:templates/markdown_tables/table_of_contents.vm", + deps = [":all_docs"], +) -[ - stardoc( - name = "%s_md" % k, - out = "%s.md" % k, - header_template = ":%s_gen_header_vm" % k, - input = ":symbols.bzl", - symbol_names = PAGES[k].symbols, - deps = [":all_docs"], - ) - for k in PAGES.keys() -] +stardoc( + name = "rust_fmt", + out = "rust_fmt.md", + header_template = ":rust_fmt.vm", + input = "@rules_rust//rust:defs.bzl", + symbol_names = [ + "rustfmt_aspect", + "rustfmt_test", + ], + table_of_contents_template = "@stardoc//stardoc:templates/markdown_tables/table_of_contents.vm", + deps = [":all_docs"], +) -genrule( - name = "flatten_header_vm", - outs = ["flatten_header.vm"], - cmd = ">$@ echo '# Rust rules\n\n%s\n'" % "\n".join( - sorted(["* [{rule}](#{rule})".format(rule = v) for k in PAGES.keys() for v in PAGES[k].symbols]), - ), - output_to_bindir = True, +stardoc( + name = "rust_repositories", + out = "rust_repositories.md", + input = "@rules_rust//rust:repositories.bzl", + symbol_names = [ + "rules_rust_dependencies", + "rust_analyzer_toolchain_repository", + "rust_register_toolchains", + "rust_repositories", + "rust_repository_set", + "rust_stdlib_filegroup", + "rust_toolchain_repository_proxy", + "rust_toolchain_repository", + "rust_toolchain_tools_repository", + "rust_toolchain", + ], + table_of_contents_template = "@stardoc//stardoc:templates/markdown_tables/table_of_contents.vm", + deps = [":all_docs"], ) stardoc( - name = "flatten_md", - out = "flatten.md", - header_template = ":flatten_header_vm", - input = ":symbols.bzl", - symbol_names = sorted([symbol for k in PAGES.keys() for symbol in PAGES[k].symbols]), + name = "rust_toolchains", + out = "rust_toolchains.md", + input = "@rules_rust//rust:toolchain.bzl", + symbol_names = [ + "rust_toolchain", + "rustfmt_toolchain", + "rust_analyzer_toolchain", + ], + table_of_contents_template = "@stardoc//stardoc:templates/markdown_tables/table_of_contents.vm", deps = [":all_docs"], ) @@ -229,6 +232,22 @@ stardoc( deps = ["@rules_rust_wasm_bindgen//rules_js:bzl_lib"], ) +stardoc( + name = "rust_settings", + out = "rust_settings.md", + func_template = "rust_settings_func.vm", + input = "@rules_rust//rust/settings:settings.bzl", + deps = ["@rules_rust//rust:bzl_lib"], +) + +stardoc( + name = "cargo_settings", + out = "cargo_settings.md", + func_template = "cargo_settings_func.vm", + input = "@rules_rust//cargo/settings:settings.bzl", + deps = ["@rules_rust//cargo:bzl_lib"], +) + mdbook( name = "book", srcs = glob(["src/**/*.md"]), diff --git a/docs/MODULE.bazel b/docs/MODULE.bazel index 8ece1a2b53..3a72a8bb45 100644 --- a/docs/MODULE.bazel +++ b/docs/MODULE.bazel @@ -47,11 +47,11 @@ bazel_dep( ) bazel_dep( name = "stardoc", - version = "0.6.2", + version = "0.7.2", ) bazel_dep( name = "protobuf", - version = "27.0", + version = "29.0", repo_name = "com_google_protobuf", ) bazel_dep( diff --git a/docs/cargo_settings_func.vm b/docs/cargo_settings_func.vm new file mode 100644 index 0000000000..8154985e5a --- /dev/null +++ b/docs/cargo_settings_func.vm @@ -0,0 +1,22 @@ + + +#[[##]]# ${funcInfo.functionName} + +
+--@rules_rust//cargo/settings:${funcInfo.functionName} ++ +${funcInfo.docString} + +#if (!$funcInfo.getParameterList().isEmpty()) +**PARAMETERS** + + +| Name | Description | Options | +| :------------- | :------------- | :------------- | +#foreach ($param in $funcInfo.getParameterList()) +| $param.name | #if(!$param.docString.isEmpty()) ${util.markdownCellFormat($param.docString)} #else
-
#end | #if(!$param.getDefaultValue().isEmpty()) ${util.markdownCodeSpan($param.defaultValue)} #else none #end| +#end +#end +#if (!$funcInfo.getReturn().docString.isEmpty()) +#end diff --git a/docs/page.bzl b/docs/page.bzl deleted file mode 100644 index b20db65944..0000000000 --- a/docs/page.bzl +++ /dev/null @@ -1,59 +0,0 @@ -"""A helper module for generating documentation for rules_rust""" - -def page(name, symbols, header_template = None): - """Define a collection of attributes used to generate a page of documentation - - Note, all templates are Velocity files: https://velocity.apache.org/engine/devel/user-guide.html - - Args: - name (str): The name of the page - symbols (list): A list of symbol names - header_template (Label, optional): The label of a `header_template` stardoc attribute - - Returns: - tuple: The name of the page with the page content - """ - return (name, struct( - name = name, - header_template = header_template, - symbols = symbols, - )) - -# buildifier: disable=unnamed-macro -def gen_header(page): - """Generate a header with a table of contents - - Args: - page (struct): A `page` struct - """ - name = "%s_gen_header_vm" % page.name - outs = ["%s_gen_header.vm" % page.name] - - # Set the top level header - page_names = [w.capitalize() for w in page.name.split("_")] - cmd = [ - "echo ''", - "echo '# {}'".format(" ".join(page_names)), - "echo ''", - ] - - # Add table of contents - cmd.extend(["echo '* [{rule}](#{rule})'".format(rule = s) for s in page.symbols]) - - # Render an optional header - if page.header_template: - cmd.extend([ - "echo ''", - "cat $(execpath {})".format(page.header_template), - ]) - srcs = [page.header_template] - else: - srcs = [] - - native.genrule( - name = name, - outs = outs, - cmd = "{\n" + "\n".join(cmd) + "\n} > $@", - srcs = srcs, - output_to_bindir = True, - ) diff --git a/docs/rust_settings_func.vm b/docs/rust_settings_func.vm new file mode 100644 index 0000000000..876ca2a0d1 --- /dev/null +++ b/docs/rust_settings_func.vm @@ -0,0 +1,22 @@ + + +#[[##]]# ${funcInfo.functionName} + ++--@rules_rust//rust/settings:${funcInfo.functionName} ++ +${funcInfo.docString} + +#if (!$funcInfo.getParameterList().isEmpty()) +**PARAMETERS** + + +| Name | Description | Options | +| :------------- | :------------- | :------------- | +#foreach ($param in $funcInfo.getParameterList()) +| $param.name | #if(!$param.docString.isEmpty()) ${util.markdownCellFormat($param.docString)} #else
-
#end | #if(!$param.getDefaultValue().isEmpty()) ${util.markdownCodeSpan($param.defaultValue)} #else none #end| +#end +#end +#if (!$funcInfo.getReturn().docString.isEmpty()) +#end diff --git a/docs/settings_table_of_contents.vm b/docs/settings_table_of_contents.vm new file mode 100644 index 0000000000..e5cdb41826 --- /dev/null +++ b/docs/settings_table_of_contents.vm @@ -0,0 +1,7 @@ + +#if (!$functionInfos.isEmpty()) +#[[##]]# Settings + +#foreach ($functionInfo in $functionInfos) +- [$functionInfo.functionName](#$functionInfo.functionName) +#end diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 42f3358be2..9ad6a0259a 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -6,12 +6,16 @@ - [Rules](./rules.md) - [rust](./rust.md) - - [rustdoc](./rust_doc.md) - [clippy](./rust_clippy.md) - [rustfmt](./rust_fmt.md) + - [rustdoc](./rust_doc.md) - [cargo](./cargo.md) - [rust_analyzer](./rust_analyzer.md) - - [all symbols](./flatten.md) +- [Settings](./settings.md) + - [Rust Settings](./rust_settings.md) + - [Cargo Settings](./cargo_settings.md) +- [Toolchains](./rust_toolchains.md) +- [Repositories](./rust_repositories.md) - [External Crates](./external_crates.md) - [crate_universe (bzlmod)](crate_universe_bzlmod.md) - [crate_universe (WORKSPACE)](crate_universe.md) diff --git a/docs/src/cargo.md b/docs/src/cargo.md index cd55727f2d..d68fdee6ee 100644 --- a/docs/src/cargo.md +++ b/docs/src/cargo.md @@ -1,16 +1,31 @@ + # Cargo -* [cargo_bootstrap_repository](#cargo_bootstrap_repository) -* [cargo_build_script](#cargo_build_script) -* [cargo_dep_env](#cargo_dep_env) -* [cargo_env](#cargo_env) +Common definitions for the `@rules_rust//cargo` package + + +## Rules + +- [cargo_dep_env](#cargo_dep_env) + +## Functions + +- [cargo_build_script](#cargo_build_script) +- [cargo_env](#cargo_env) + +## Repository Rules + +- [cargo_bootstrap_repository](#cargo_bootstrap_repository) + ## cargo_dep_env+load("@rules_rust//cargo:defs.bzl", "cargo_dep_env") + cargo_dep_env(name, src, out_dir)@@ -31,6 +46,8 @@ A rule for generating variables for dependent `cargo_build_script`s without a bu ## cargo_build_script
+load("@rules_rust//cargo:defs.bzl", "cargo_build_script") + cargo_build_script(name, edition, crate_name, crate_root, srcs, crate_features, version, deps, link_deps, proc_macro_deps, build_script_env, use_default_shell_env, data, compile_data, tools, links, rundir, rustc_env, rustc_env_files, rustc_flags, @@ -132,6 +149,8 @@ The `hello_lib` target will be build with the flags and the environment variable ## cargo_env+load("@rules_rust//cargo:defs.bzl", "cargo_env") + cargo_env(env)@@ -179,6 +198,8 @@ str: A json encoded string of the environment variables ## cargo_bootstrap_repository+load("@rules_rust//cargo:defs.bzl", "cargo_bootstrap_repository") + cargo_bootstrap_repository(name, srcs, binary, build_mode, cargo_config, cargo_lockfile, cargo_toml, compressed_windows_toolchain_names, env, env_label, repo_mapping, rust_toolchain_cargo_template, rust_toolchain_rustc_template, timeout, diff --git a/docs/src/cargo_settings.md b/docs/src/cargo_settings.md new file mode 100644 index 0000000000..0f9160f390 --- /dev/null +++ b/docs/src/cargo_settings.md @@ -0,0 +1,68 @@ + + +# Cargo settings + +Definitions for all `@rules_rust//cargo` settings + + + +## cargo_manifest_dir_filename_suffixes_to_retain + ++--@rules_rust//cargo/settings:cargo_manifest_dir_filename_suffixes_to_retain ++ +A flag which determines what files are retained in `CARGO_MANIFEST_DIR` directories that are created in `CargoBuildScriptRun` actions. + + + + + +## debug_std_streams_output_group + ++--@rules_rust//cargo/settings:debug_std_streams_output_group ++ +A flag which adds a `streams` output group to `cargo_build_script` targets that contain the raw `stderr` and `stdout` streams from the build script. + + + + + +## experimental_symlink_execroot + ++--@rules_rust//cargo/settings:experimental_symlink_execroot ++ +A flag for which causes `cargo_build_script` to symlink the execroot of the action to the `CARGO_MANIFEST_DIR` where the scripts are run. + + + + + +## incompatible_runfiles_cargo_manifest_dir + ++--@rules_rust//cargo/settings:incompatible_runfiles_cargo_manifest_dir ++ +A flag which causes `cargo_build_script` to write an explicit `CARGO_MANFIEST_DIR` directory from an action instead of using runfiles directories which cannot be passed to downstream actions. + +https://github.com/bazelbuild/bazel/issues/15486 + + + + + +## use_default_shell_env + ++--@rules_rust//cargo/settings:use_default_shell_env ++ +A flag which controls the global default of `ctx.actions.run.use_default_shell_env` for `cargo_build_script` targets. + + + diff --git a/docs/src/crate_universe.md b/docs/src/crate_universe.md index c83078dddc..a0c5774b61 100644 --- a/docs/src/crate_universe.md +++ b/docs/src/crate_universe.md @@ -299,6 +299,8 @@ There is an example of this in the "complicated dependencies" section of https:/ ## crates_vendor+load("@rules_rust//crate_universe:docs_workspace.bzl", "crates_vendor") + crates_vendor(name, annotations, bazel, buildifier, cargo_bazel, cargo_config, cargo_lockfile, generate_binaries, generate_build_scripts, generate_target_compatible_with, manifests, mode, packages, render_config, repository_name, splicing_config, @@ -405,6 +407,8 @@ call against the generated workspace. The following table describes how to contr ## aliases+load("@rules_rust//crate_universe:docs_workspace.bzl", "aliases") + aliases(normal, normal_dev, proc_macro, proc_macro_dev, build, build_proc_macro, package_name)@@ -437,6 +441,8 @@ dict: The aliases of all associated packages ## all_crate_deps+load("@rules_rust//crate_universe:docs_workspace.bzl", "all_crate_deps") + all_crate_deps(normal, normal_dev, proc_macro, proc_macro_dev, build, build_proc_macro, package_name)@@ -470,6 +476,8 @@ list: A list of labels to generated rust targets (str) ## crate.annotation+load("@rules_rust//crate_universe:docs_workspace.bzl", "crate") + crate.annotation(version, additive_build_file, additive_build_file_content, alias_rule, build_script_compile_data, build_script_data, build_script_tools, build_script_data_glob, build_script_deps, build_script_env, @@ -533,6 +541,8 @@ string: A json encoded string containing the specified version and separately al ## crate.select+load("@rules_rust//crate_universe:docs_workspace.bzl", "crate") + crate.select(common, selects)@@ -556,6 +566,8 @@ struct: A struct representing the Starlark Select. ## crate.spec+load("@rules_rust//crate_universe:docs_workspace.bzl", "crate") + crate.spec(package, version, artifact, lib, default_features, features, git, branch, tag, rev)@@ -592,6 +604,8 @@ string: A json encoded string of all inputs ## crate.workspace_member+load("@rules_rust//crate_universe:docs_workspace.bzl", "crate") + crate.workspace_member(version, sha256)@@ -615,6 +629,8 @@ string: A json encoded string of all inputs ## crate_deps+load("@rules_rust//crate_universe:docs_workspace.bzl", "crate_deps") + crate_deps(deps, package_name)@@ -638,6 +654,8 @@ list: A list of labels to generated rust targets (str) ## crate_repositories+load("@rules_rust//crate_universe:docs_workspace.bzl", "crate_repositories") + crate_repositories()@@ -654,6 +672,8 @@ A list of repos visible to the module through the module extension. ## crate_universe_dependencies+load("@rules_rust//crate_universe:docs_workspace.bzl", "crate_universe_dependencies") + crate_universe_dependencies(rust_version, bootstrap, kwargs)@@ -679,6 +699,8 @@ list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories ## render_config+load("@rules_rust//crate_universe:docs_workspace.bzl", "render_config") + render_config(build_file_template, crate_label_template, crate_repository_template, crates_module_template, default_alias_rule, default_package_name, generate_target_compatible_with, platforms_template, regen_command, vendor_mode, @@ -727,6 +749,8 @@ string: A json encoded struct to match the Rust `config::RenderConfig` struct ## splicing_config+load("@rules_rust//crate_universe:docs_workspace.bzl", "splicing_config") + splicing_config(resolver_version)@@ -752,6 +776,8 @@ str: A json encoded string of the parameters provided ## crates_repository+load("@rules_rust//crate_universe:docs_workspace.bzl", "crates_repository") + crates_repository(name, annotations, cargo_config, cargo_lockfile, compressed_windows_toolchain_names, generate_binaries, generate_build_scripts, generate_target_compatible_with, generator, generator_sha256s, generator_urls, @@ -882,6 +908,7 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i **ENVIRONMENT VARIABLES** This repository rule depends on the following environment variables: + * `CARGO_BAZEL_GENERATOR_URL` * `CARGO_BAZEL_GENERATOR_SHA256` * `CARGO_BAZEL_REPIN` diff --git a/docs/src/flatten.md b/docs/src/flatten.md deleted file mode 100644 index 4632cb0feb..0000000000 --- a/docs/src/flatten.md +++ /dev/null @@ -1,1804 +0,0 @@ -# Rust rules - -* [CrateInfo](#CrateInfo) -* [DepInfo](#DepInfo) -* [StdLibInfo](#StdLibInfo) -* [capture_clippy_output](#capture_clippy_output) -* [cargo_bootstrap_repository](#cargo_bootstrap_repository) -* [cargo_build_script](#cargo_build_script) -* [cargo_dep_env](#cargo_dep_env) -* [cargo_env](#cargo_env) -* [error_format](#error_format) -* [extra_rustc_flag](#extra_rustc_flag) -* [extra_rustc_flags](#extra_rustc_flags) -* [fail_when_enabled](#fail_when_enabled) -* [incompatible_flag](#incompatible_flag) -* [rules_rust_dependencies](#rules_rust_dependencies) -* [rust_analyzer_aspect](#rust_analyzer_aspect) -* [rust_analyzer_toolchain](#rust_analyzer_toolchain) -* [rust_analyzer_toolchain_repository](#rust_analyzer_toolchain_repository) -* [rust_binary](#rust_binary) -* [rust_clippy](#rust_clippy) -* [rust_clippy_aspect](#rust_clippy_aspect) -* [rust_doc](#rust_doc) -* [rust_doc_test](#rust_doc_test) -* [rust_library](#rust_library) -* [rust_library_group](#rust_library_group) -* [rust_proc_macro](#rust_proc_macro) -* [rust_register_toolchains](#rust_register_toolchains) -* [rust_repositories](#rust_repositories) -* [rust_repository_set](#rust_repository_set) -* [rust_shared_library](#rust_shared_library) -* [rust_static_library](#rust_static_library) -* [rust_stdlib_filegroup](#rust_stdlib_filegroup) -* [rust_test](#rust_test) -* [rust_test_suite](#rust_test_suite) -* [rust_toolchain](#rust_toolchain) -* [rust_toolchain_repository](#rust_toolchain_repository) -* [rust_toolchain_repository_proxy](#rust_toolchain_repository_proxy) -* [rust_toolchain_tools_repository](#rust_toolchain_tools_repository) -* [rustfmt_aspect](#rustfmt_aspect) -* [rustfmt_test](#rustfmt_test) -* [rustfmt_toolchain](#rustfmt_toolchain) - - - - -## capture_clippy_output - --capture_clippy_output(name) -- -Control whether to print clippy output or store it to a file, using the configured error_format. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | - - - - -## cargo_dep_env - --cargo_dep_env(name, src, out_dir) -- -A rule for generating variables for dependent `cargo_build_script`s without a build script. This is useful for using Bazel rules instead of a build script, while also generating configuration information for build scripts which depend on this crate. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| src | File containing additional environment variables to set for build scripts of direct dependencies.
This has the same effect as a `cargo_build_script` which prints `cargo:VAR=VALUE` lines, but without requiring a build script.
This files should contain a single variable per line, of format `NAME=value`, and newlines may be included in a value by ending a line with a trailing back-slash (`\\`). | Label | required | | -| out_dir | Folder containing additional inputs when building all direct dependencies.
This has the same effect as a `cargo_build_script` which prints puts files into `$OUT_DIR`, but without requiring a build script. | Label | optional | `None` | - - - - -## error_format - --error_format(name) -- -Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) flag from the command line with `--@rules_rust//rust/settings:error_format`. See rustc documentation for valid values. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | - - - - -## extra_rustc_flag - --extra_rustc_flag(name) -- -Add additional rustc_flag from the command line with `--@rules_rust//rust/settings:extra_rustc_flag`. Multiple uses are accumulated and appended after the extra_rustc_flags. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | - - - - -## extra_rustc_flags - --extra_rustc_flags(name) -- -Add additional rustc_flags from the command line with `--@rules_rust//rust/settings:extra_rustc_flags`. This flag should only be used for flags that need to be applied across the entire build. For options that apply to individual crates, use the rustc_flags attribute on the individual crate's rule instead. NOTE: These flags not applied to the exec configuration (proc-macros, cargo_build_script, etc); use `--@rules_rust//rust/settings:extra_exec_rustc_flags` to apply flags to the exec configuration. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | - - - - -## incompatible_flag - --incompatible_flag(name, issue) -- -A rule defining an incompatible flag. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| issue | The link to the github issue associated with this flag | String | required | | - - - - -## rust_analyzer_toolchain - --rust_analyzer_toolchain(name, proc_macro_srv, rustc, rustc_srcs) -- -A toolchain for [rust-analyzer](https://rust-analyzer.github.io/). - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| proc_macro_srv | The path to a `rust_analyzer_proc_macro_srv` binary. | Label | optional | `None` | -| rustc | The path to a `rustc` binary. | Label | required | | -| rustc_srcs | The source code of rustc. | Label | required | | - - - - -## rust_binary - --rust_binary(name, deps, srcs, data, aliases, alwayslink, binary_name, compile_data, crate_features, - crate_name, crate_root, crate_type, edition, env, experimental_use_cc_common_link, - linker_script, malloc, out_binary, platform, proc_macro_deps, rustc_env, rustc_env_files, - rustc_flags, stamp, version) -- -Builds a Rust binary crate. - -Example: - -Suppose you have the following directory structure for a Rust project with a -library crate, `hello_lib`, and a binary crate, `hello_world` that uses the -`hello_lib` library: - -```output -[workspace]/ - WORKSPACE - hello_lib/ - BUILD - src/ - lib.rs - hello_world/ - BUILD - src/ - main.rs -``` - -`hello_lib/src/lib.rs`: -```rust -pub struct Greeter { - greeting: String, -} - -impl Greeter { - pub fn new(greeting: &str) -> Greeter { - Greeter { greeting: greeting.to_string(), } - } - - pub fn greet(&self, thing: &str) { - println!("{} {}", &self.greeting, thing); - } -} -``` - -`hello_lib/BUILD`: -```python -package(default_visibility = ["//visibility:public"]) - -load("@rules_rust//rust:defs.bzl", "rust_library") - -rust_library( - name = "hello_lib", - srcs = ["src/lib.rs"], -) -``` - -`hello_world/src/main.rs`: -```rust -extern crate hello_lib; - -fn main() { - let hello = hello_lib::Greeter::new("Hello"); - hello.greet("world"); -} -``` - -`hello_world/BUILD`: -```python -load("@rules_rust//rust:defs.bzl", "rust_binary") - -rust_binary( - name = "hello_world", - srcs = ["src/main.rs"], - deps = ["//hello_lib"], -) -``` - -Build and run `hello_world`: -``` -$ bazel run //hello_world -INFO: Found 1 target... -Target //examples/rust/hello_world:hello_world up-to-date: -bazel-bin/examples/rust/hello_world/hello_world -INFO: Elapsed time: 1.308s, Critical Path: 1.22s - -INFO: Running command line: bazel-bin/examples/rust/hello_world/hello_world -Hello world -``` - -On Windows, a PDB file containing debugging information is available under -the key `pdb_file` in `OutputGroupInfo`. Similarly on macOS, a dSYM folder -is available under the key `dsym_folder` in `OutputGroupInfo`. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| deps | List of other libraries to be linked to this library target.
These can be either other `rust_library` targets or `cc_library` targets if linking a native library. | List of labels | optional | `[]` | -| srcs | List of Rust `.rs` source files used to build the library.
If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | -| data | List of files used by this rule at compile time and runtime.
If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | -| aliases | Remap crates to a new name or moniker for linkage to this target
These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | -| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.
This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | -| binary_name | Override the resulting binary file name. By default, the binary file will be named using the `name` attribute on this rule, however sometimes that is not deseriable. | String | optional | `""` | -| compile_data | List of files used by this rule at compile time.
This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | -| crate_features | List of features to enable for this crate.
Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | -| crate_name | Crate name to use for this target.
This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | -| crate_root | The file that will be passed to `rustc` to be used for building this crate.
If `crate_root` is not set, then this rule will look for a `lib.rs` file (or `main.rs` for rust_binary) or the single file in `srcs` if `srcs` contains only one file. | Label | optional | `None` | -| crate_type | Crate type that will be passed to `rustc` to be used for building this crate.
This option is a temporary workaround and should be used only when building for WebAssembly targets (//rust/platform:wasi and //rust/platform:wasm). | String | optional | `"bin"` | -| edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | `""` | -| env | Specifies additional environment variables to set when the target is executed by bazel run. Values are subject to `$(rootpath)`, `$(execpath)`, location, and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution.
Execpath returns absolute path, and in order to be able to construct the absolute path we need to wrap the test binary in a launcher. Using a launcher comes with complications, such as more complicated debugger attachment. | Dictionary: String -> String | optional | `{}` | -| experimental_use_cc_common_link | Whether to use cc_common.link to link rust binaries. Possible values: [-1, 0, 1]. -1 means use the value of the toolchain.experimental_use_cc_common_link boolean build setting to determine. 0 means do not use cc_common.link (use rustc instead). 1 means use cc_common.link. | Integer | optional | `-1` | -| linker_script | Link script to forward into linker via rustc options. | Label | optional | `None` | -| malloc | Override the default dependency on `malloc`.
By default, Rust binaries linked with cc_common.link are linked against `@bazel_tools//tools/cpp:malloc"`, which is an empty library and the resulting binary will use libc's `malloc`. This label must refer to a `cc_library` rule. | Label | optional | `"@bazel_tools//tools/cpp:malloc"` | -| out_binary | Force a target, regardless of it's `crate_type`, to always mark the file as executable. This attribute is only used to support wasm targets but is expected to be removed following a resolution to https://github.com/bazelbuild/rules_rust/issues/771. | Boolean | optional | `False` | -| platform | Optional platform to transition the binary to. | Label | optional | `None` | -| proc_macro_deps | List of `rust_proc_macro` targets used to help build this library target. | List of labels | optional | `[]` | -| rustc_env | Dictionary of additional `"key": "value"` environment variables to set for rustc.
rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | Dictionary: String -> String | optional | `{}` | -| rustc_env_files | Files containing additional environment variables to set for rustc.
These files should contain a single variable per line, of format `NAME=value`, and newlines may be included in a value by ending a line with a trailing back-slash (`\\`).
The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged.
Note that the variables here are subject to [workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status) stamping should the `stamp` attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. `NAME={WORKSPACE_STATUS_VARIABLE}`. | List of labels | optional | `[]` | -| rustc_flags | List of compiler flags passed to `rustc`.
These strings are subject to Make variable expansion for predefined source/output path variables like `$location`, `$execpath`, and `$rootpath`. This expansion is useful if you wish to pass a generated file of arguments to rustc: `@$(location //package:target)`. | List of strings | optional | `[]` | -| stamp | Whether to encode build information into the `Rustc` action. Possible values:
- `stamp = 1`: Always stamp the build information into the `Rustc` action, even in [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds. This setting should be avoided, since it potentially kills remote caching for the target and any downstream actions that depend on it.
- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.
- `stamp = -1`: Embedding of build information is controlled by the [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.
Stamped targets are not rebuilt unless their dependencies change.
For example if a `rust_library` is stamped, and a `rust_binary` depends on that library, the stamped library won't be rebuilt when we change sources of the `rust_binary`. This is different from how [`cc_library.linkstamps`](https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library.linkstamp) behaves. | Integer | optional | `-1` | -| version | A version to inject in the cargo environment variable. | String | optional | `"0.0.0"` | - - - - -## rust_clippy - --rust_clippy(name, deps) -- -Executes the clippy checker on a specific target. - -Similar to `rust_clippy_aspect`, but allows specifying a list of dependencies within the build system. - -For example, given the following example targets: - -```python -load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") - -rust_library( - name = "hello_lib", - srcs = ["src/lib.rs"], -) - -rust_test( - name = "greeting_test", - srcs = ["tests/greeting.rs"], - deps = [":hello_lib"], -) -``` - -Rust clippy can be set as a build target with the following: - -```python -load("@rules_rust//rust:defs.bzl", "rust_clippy") - -rust_clippy( - name = "hello_library_clippy", - testonly = True, - deps = [ - ":hello_lib", - ":greeting_test", - ], -) -``` - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| deps | Rust targets to run clippy on. | List of labels | optional | `[]` | - - - - -## rust_doc - --rust_doc(name, crate, html_after_content, html_before_content, html_in_header, markdown_css, - rustc_flags, rustdoc_flags) -- -Generates code documentation. - -Example: -Suppose you have the following directory structure for a Rust library crate: - -``` -[workspace]/ - WORKSPACE - hello_lib/ - BUILD - src/ - lib.rs -``` - -To build [`rustdoc`][rustdoc] documentation for the `hello_lib` crate, define a `rust_doc` rule that depends on the the `hello_lib` `rust_library` target: - -[rustdoc]: https://doc.rust-lang.org/book/documentation.html - -```python -package(default_visibility = ["//visibility:public"]) - -load("@rules_rust//rust:defs.bzl", "rust_library", "rust_doc") - -rust_library( - name = "hello_lib", - srcs = ["src/lib.rs"], -) - -rust_doc( - name = "hello_lib_doc", - crate = ":hello_lib", -) -``` - -Running `bazel build //hello_lib:hello_lib_doc` will build a zip file containing the documentation for the `hello_lib` library crate generated by `rustdoc`. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| crate | The label of the target to generate code documentation for.
`rust_doc` can generate HTML code documentation for the source files of `rust_library` or `rust_binary` targets. | Label | required | | -| html_after_content | File to add in ``, after content. | Label | optional | `None` | -| html_before_content | File to add in ``, before content. | Label | optional | `None` | -| html_in_header | File to add to ``. | Label | optional | `None` | -| markdown_css | CSS files to include via `` in a rendered Markdown file. | List of labels | optional | `[]` | -| rustc_flags | **Deprecated**: use `rustdoc_flags` instead | List of strings | optional | `[]` | -| rustdoc_flags | List of flags passed to `rustdoc`.
These strings are subject to Make variable expansion for predefined source/output path variables like `$location`, `$execpath`, and `$rootpath`. This expansion is useful if you wish to pass a generated file of arguments to rustc: `@$(location //package:target)`. | List of strings | optional | `[]` | - - - - -## rust_doc_test - --rust_doc_test(name, deps, crate, proc_macro_deps) -- -Runs Rust documentation tests. - -Example: - -Suppose you have the following directory structure for a Rust library crate: - -```output -[workspace]/ -WORKSPACE -hello_lib/ - BUILD - src/ - lib.rs -``` - -To run [documentation tests][doc-test] for the `hello_lib` crate, define a `rust_doc_test` target that depends on the `hello_lib` `rust_library` target: - -[doc-test]: https://doc.rust-lang.org/book/documentation.html#documentation-as-tests - -```python -package(default_visibility = ["//visibility:public"]) - -load("@rules_rust//rust:defs.bzl", "rust_library", "rust_doc_test") - -rust_library( - name = "hello_lib", - srcs = ["src/lib.rs"], -) - -rust_doc_test( - name = "hello_lib_doc_test", - crate = ":hello_lib", -) -``` - -Running `bazel test //hello_lib:hello_lib_doc_test` will run all documentation tests for the `hello_lib` library crate. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| deps | List of other libraries to be linked to this library target.
These can be either other `rust_library` targets or `cc_library` targets if linking a native library. | List of labels | optional | `[]` | -| crate | The label of the target to generate code documentation for. `rust_doc_test` can generate HTML code documentation for the source files of `rust_library` or `rust_binary` targets. | Label | required | | -| proc_macro_deps | List of `rust_proc_macro` targets used to help build this library target. | List of labels | optional | `[]` | - - - - -## rust_library - --rust_library(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features, crate_name, - crate_root, disable_pipelining, edition, proc_macro_deps, rustc_env, rustc_env_files, - rustc_flags, stamp, version) -- -Builds a Rust library crate. - -Example: - -Suppose you have the following directory structure for a simple Rust library crate: - -```output -[workspace]/ - WORKSPACE - hello_lib/ - BUILD - src/ - greeter.rs - lib.rs -``` - -`hello_lib/src/greeter.rs`: -```rust -pub struct Greeter { - greeting: String, -} - -impl Greeter { - pub fn new(greeting: &str) -> Greeter { - Greeter { greeting: greeting.to_string(), } - } - - pub fn greet(&self, thing: &str) { - println!("{} {}", &self.greeting, thing); - } -} -``` - -`hello_lib/src/lib.rs`: - -```rust -pub mod greeter; -``` - -`hello_lib/BUILD`: -```python -package(default_visibility = ["//visibility:public"]) - -load("@rules_rust//rust:defs.bzl", "rust_library") - -rust_library( - name = "hello_lib", - srcs = [ - "src/greeter.rs", - "src/lib.rs", - ], -) -``` - -Build the library: -```output -$ bazel build //hello_lib -INFO: Found 1 target... -Target //examples/rust/hello_lib:hello_lib up-to-date: -bazel-bin/examples/rust/hello_lib/libhello_lib.rlib -INFO: Elapsed time: 1.245s, Critical Path: 1.01s -``` - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| deps | List of other libraries to be linked to this library target.
These can be either other `rust_library` targets or `cc_library` targets if linking a native library. | List of labels | optional | `[]` | -| srcs | List of Rust `.rs` source files used to build the library.
If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | -| data | List of files used by this rule at compile time and runtime.
If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | -| aliases | Remap crates to a new name or moniker for linkage to this target
These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | -| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.
This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | -| compile_data | List of files used by this rule at compile time.
This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | -| crate_features | List of features to enable for this crate.
Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | -| crate_name | Crate name to use for this target.
This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | -| crate_root | The file that will be passed to `rustc` to be used for building this crate.
If `crate_root` is not set, then this rule will look for a `lib.rs` file (or `main.rs` for rust_binary) or the single file in `srcs` if `srcs` contains only one file. | Label | optional | `None` | -| disable_pipelining | Disables pipelining for this rule if it is globally enabled. This will cause this rule to not produce a `.rmeta` file and all the dependent crates will instead use the `.rlib` file. | Boolean | optional | `False` | -| edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | `""` | -| proc_macro_deps | List of `rust_proc_macro` targets used to help build this library target. | List of labels | optional | `[]` | -| rustc_env | Dictionary of additional `"key": "value"` environment variables to set for rustc.
rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | Dictionary: String -> String | optional | `{}` | -| rustc_env_files | Files containing additional environment variables to set for rustc.
These files should contain a single variable per line, of format `NAME=value`, and newlines may be included in a value by ending a line with a trailing back-slash (`\\`).
The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged.
Note that the variables here are subject to [workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status) stamping should the `stamp` attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. `NAME={WORKSPACE_STATUS_VARIABLE}`. | List of labels | optional | `[]` | -| rustc_flags | List of compiler flags passed to `rustc`.
These strings are subject to Make variable expansion for predefined source/output path variables like `$location`, `$execpath`, and `$rootpath`. This expansion is useful if you wish to pass a generated file of arguments to rustc: `@$(location //package:target)`. | List of strings | optional | `[]` | -| stamp | Whether to encode build information into the `Rustc` action. Possible values:
- `stamp = 1`: Always stamp the build information into the `Rustc` action, even in [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds. This setting should be avoided, since it potentially kills remote caching for the target and any downstream actions that depend on it.
- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.
- `stamp = -1`: Embedding of build information is controlled by the [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.
Stamped targets are not rebuilt unless their dependencies change.
For example if a `rust_library` is stamped, and a `rust_binary` depends on that library, the stamped library won't be rebuilt when we change sources of the `rust_binary`. This is different from how [`cc_library.linkstamps`](https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library.linkstamp) behaves. | Integer | optional | `0` | -| version | A version to inject in the cargo environment variable. | String | optional | `"0.0.0"` | - - - - -## rust_library_group - --rust_library_group(name, deps) -- -Functions as an alias for a set of dependencies. - -Specifically, the following are equivalent: - -```starlark -rust_library_group( - name = "crate_group", - deps = [ - ":crate1", - ":crate2", - ], -) - -rust_library( - name = "foobar", - deps = [":crate_group"], - ... -) -``` - -and - -```starlark -rust_library( - name = "foobar", - deps = [ - ":crate1", - ":crate2", - ], - ... -) -``` - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| deps | Other dependencies to forward through this crate group. | List of labels | optional | `[]` | - - - - -## rust_proc_macro - --rust_proc_macro(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features, - crate_name, crate_root, edition, proc_macro_deps, rustc_env, rustc_env_files, - rustc_flags, stamp, version) -- -Builds a Rust proc-macro crate. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| deps | List of other libraries to be linked to this library target.
These can be either other `rust_library` targets or `cc_library` targets if linking a native library. | List of labels | optional | `[]` | -| srcs | List of Rust `.rs` source files used to build the library.
If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | -| data | List of files used by this rule at compile time and runtime.
If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | -| aliases | Remap crates to a new name or moniker for linkage to this target
These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | -| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.
This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | -| compile_data | List of files used by this rule at compile time.
This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | -| crate_features | List of features to enable for this crate.
Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | -| crate_name | Crate name to use for this target.
This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | -| crate_root | The file that will be passed to `rustc` to be used for building this crate.
If `crate_root` is not set, then this rule will look for a `lib.rs` file (or `main.rs` for rust_binary) or the single file in `srcs` if `srcs` contains only one file. | Label | optional | `None` | -| edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | `""` | -| proc_macro_deps | List of `rust_proc_macro` targets used to help build this library target. | List of labels | optional | `[]` | -| rustc_env | Dictionary of additional `"key": "value"` environment variables to set for rustc.
rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | Dictionary: String -> String | optional | `{}` | -| rustc_env_files | Files containing additional environment variables to set for rustc.
These files should contain a single variable per line, of format `NAME=value`, and newlines may be included in a value by ending a line with a trailing back-slash (`\\`).
The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged.
Note that the variables here are subject to [workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status) stamping should the `stamp` attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. `NAME={WORKSPACE_STATUS_VARIABLE}`. | List of labels | optional | `[]` | -| rustc_flags | List of compiler flags passed to `rustc`.
These strings are subject to Make variable expansion for predefined source/output path variables like `$location`, `$execpath`, and `$rootpath`. This expansion is useful if you wish to pass a generated file of arguments to rustc: `@$(location //package:target)`. | List of strings | optional | `[]` | -| stamp | Whether to encode build information into the `Rustc` action. Possible values:
- `stamp = 1`: Always stamp the build information into the `Rustc` action, even in [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds. This setting should be avoided, since it potentially kills remote caching for the target and any downstream actions that depend on it.
- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.
- `stamp = -1`: Embedding of build information is controlled by the [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.
Stamped targets are not rebuilt unless their dependencies change.
For example if a `rust_library` is stamped, and a `rust_binary` depends on that library, the stamped library won't be rebuilt when we change sources of the `rust_binary`. This is different from how [`cc_library.linkstamps`](https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library.linkstamp) behaves. | Integer | optional | `0` | -| version | A version to inject in the cargo environment variable. | String | optional | `"0.0.0"` | - - - - -## rust_shared_library - --rust_shared_library(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features, - crate_name, crate_root, edition, experimental_use_cc_common_link, malloc, - platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp, - version) -- -Builds a Rust shared library. - -This shared library will contain all transitively reachable crates and native objects. -It is meant to be used when producing an artifact that is then consumed by some other build system -(for example to produce a shared library that Python program links against). - -This rule provides CcInfo, so it can be used everywhere Bazel expects `rules_cc`. - -When building the whole binary in Bazel, use `rust_library` instead. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| deps | List of other libraries to be linked to this library target.
These can be either other `rust_library` targets or `cc_library` targets if linking a native library. | List of labels | optional | `[]` | -| srcs | List of Rust `.rs` source files used to build the library.
If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | -| data | List of files used by this rule at compile time and runtime.
If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | -| aliases | Remap crates to a new name or moniker for linkage to this target
These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | -| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.
This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | -| compile_data | List of files used by this rule at compile time.
This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | -| crate_features | List of features to enable for this crate.
Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | -| crate_name | Crate name to use for this target.
This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | -| crate_root | The file that will be passed to `rustc` to be used for building this crate.
If `crate_root` is not set, then this rule will look for a `lib.rs` file (or `main.rs` for rust_binary) or the single file in `srcs` if `srcs` contains only one file. | Label | optional | `None` | -| edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | `""` | -| experimental_use_cc_common_link | Whether to use cc_common.link to link rust binaries. Possible values: [-1, 0, 1]. -1 means use the value of the toolchain.experimental_use_cc_common_link boolean build setting to determine. 0 means do not use cc_common.link (use rustc instead). 1 means use cc_common.link. | Integer | optional | `-1` | -| malloc | Override the default dependency on `malloc`.
By default, Rust binaries linked with cc_common.link are linked against `@bazel_tools//tools/cpp:malloc"`, which is an empty library and the resulting binary will use libc's `malloc`. This label must refer to a `cc_library` rule. | Label | optional | `"@bazel_tools//tools/cpp:malloc"` | -| platform | Optional platform to transition the shared library to. | Label | optional | `None` | -| proc_macro_deps | List of `rust_proc_macro` targets used to help build this library target. | List of labels | optional | `[]` | -| rustc_env | Dictionary of additional `"key": "value"` environment variables to set for rustc.
rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | Dictionary: String -> String | optional | `{}` | -| rustc_env_files | Files containing additional environment variables to set for rustc.
These files should contain a single variable per line, of format `NAME=value`, and newlines may be included in a value by ending a line with a trailing back-slash (`\\`).
The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged.
Note that the variables here are subject to [workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status) stamping should the `stamp` attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. `NAME={WORKSPACE_STATUS_VARIABLE}`. | List of labels | optional | `[]` | -| rustc_flags | List of compiler flags passed to `rustc`.
These strings are subject to Make variable expansion for predefined source/output path variables like `$location`, `$execpath`, and `$rootpath`. This expansion is useful if you wish to pass a generated file of arguments to rustc: `@$(location //package:target)`. | List of strings | optional | `[]` | -| stamp | Whether to encode build information into the `Rustc` action. Possible values:
- `stamp = 1`: Always stamp the build information into the `Rustc` action, even in [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds. This setting should be avoided, since it potentially kills remote caching for the target and any downstream actions that depend on it.
- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.
- `stamp = -1`: Embedding of build information is controlled by the [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.
Stamped targets are not rebuilt unless their dependencies change.
For example if a `rust_library` is stamped, and a `rust_binary` depends on that library, the stamped library won't be rebuilt when we change sources of the `rust_binary`. This is different from how [`cc_library.linkstamps`](https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library.linkstamp) behaves. | Integer | optional | `0` | -| version | A version to inject in the cargo environment variable. | String | optional | `"0.0.0"` | - - - - -## rust_static_library - --rust_static_library(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features, - crate_name, crate_root, edition, platform, proc_macro_deps, rustc_env, - rustc_env_files, rustc_flags, stamp, version) -- -Builds a Rust static library. - -This static library will contain all transitively reachable crates and native objects. -It is meant to be used when producing an artifact that is then consumed by some other build system -(for example to produce an archive that Python program links against). - -This rule provides CcInfo, so it can be used everywhere Bazel expects `rules_cc`. - -When building the whole binary in Bazel, use `rust_library` instead. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| deps | List of other libraries to be linked to this library target.
These can be either other `rust_library` targets or `cc_library` targets if linking a native library. | List of labels | optional | `[]` | -| srcs | List of Rust `.rs` source files used to build the library.
If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | -| data | List of files used by this rule at compile time and runtime.
If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | -| aliases | Remap crates to a new name or moniker for linkage to this target
These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | -| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.
This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | -| compile_data | List of files used by this rule at compile time.
This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | -| crate_features | List of features to enable for this crate.
Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | -| crate_name | Crate name to use for this target.
This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | -| crate_root | The file that will be passed to `rustc` to be used for building this crate.
If `crate_root` is not set, then this rule will look for a `lib.rs` file (or `main.rs` for rust_binary) or the single file in `srcs` if `srcs` contains only one file. | Label | optional | `None` | -| edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | `""` | -| platform | Optional platform to transition the static library to. | Label | optional | `None` | -| proc_macro_deps | List of `rust_proc_macro` targets used to help build this library target. | List of labels | optional | `[]` | -| rustc_env | Dictionary of additional `"key": "value"` environment variables to set for rustc.
rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | Dictionary: String -> String | optional | `{}` | -| rustc_env_files | Files containing additional environment variables to set for rustc.
These files should contain a single variable per line, of format `NAME=value`, and newlines may be included in a value by ending a line with a trailing back-slash (`\\`).
The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged.
Note that the variables here are subject to [workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status) stamping should the `stamp` attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. `NAME={WORKSPACE_STATUS_VARIABLE}`. | List of labels | optional | `[]` | -| rustc_flags | List of compiler flags passed to `rustc`.
These strings are subject to Make variable expansion for predefined source/output path variables like `$location`, `$execpath`, and `$rootpath`. This expansion is useful if you wish to pass a generated file of arguments to rustc: `@$(location //package:target)`. | List of strings | optional | `[]` | -| stamp | Whether to encode build information into the `Rustc` action. Possible values:
- `stamp = 1`: Always stamp the build information into the `Rustc` action, even in [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds. This setting should be avoided, since it potentially kills remote caching for the target and any downstream actions that depend on it.
- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.
- `stamp = -1`: Embedding of build information is controlled by the [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.
Stamped targets are not rebuilt unless their dependencies change.
For example if a `rust_library` is stamped, and a `rust_binary` depends on that library, the stamped library won't be rebuilt when we change sources of the `rust_binary`. This is different from how [`cc_library.linkstamps`](https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library.linkstamp) behaves. | Integer | optional | `0` | -| version | A version to inject in the cargo environment variable. | String | optional | `"0.0.0"` | - - - - -## rust_stdlib_filegroup - --rust_stdlib_filegroup(name, srcs) -- -A dedicated filegroup-like rule for Rust stdlib artifacts. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| srcs | The list of targets/files that are components of the rust-stdlib file group | List of labels | required | | - - - - -## rust_test - --rust_test(name, deps, srcs, data, aliases, alwayslink, compile_data, crate, crate_features, - crate_name, crate_root, edition, env, env_inherit, experimental_use_cc_common_link, malloc, - platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp, - use_libtest_harness, version) -- -Builds a Rust test crate. - -Examples: - -Suppose you have the following directory structure for a Rust library crate with unit test code in the library sources: - -```output -[workspace]/ - WORKSPACE - hello_lib/ - BUILD - src/ - lib.rs -``` - -`hello_lib/src/lib.rs`: -```rust -pub struct Greeter { - greeting: String, -} - -impl Greeter { - pub fn new(greeting: &str) -> Greeter { - Greeter { greeting: greeting.to_string(), } - } - - pub fn greet(&self, thing: &str) -> String { - format!("{} {}", &self.greeting, thing) - } -} - -#[cfg(test)] -mod test { - use super::Greeter; - - #[test] - fn test_greeting() { - let hello = Greeter::new("Hi"); - assert_eq!("Hi Rust", hello.greet("Rust")); - } -} -``` - -To build and run the tests, simply add a `rust_test` rule with no `srcs` -and only depends on the `hello_lib` `rust_library` target via the -`crate` attribute: - -`hello_lib/BUILD`: -```python -load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") - -rust_library( - name = "hello_lib", - srcs = ["src/lib.rs"], -) - -rust_test( - name = "hello_lib_test", - crate = ":hello_lib", - # You may add other deps that are specific to the test configuration - deps = ["//some/dev/dep"], -) -``` - -Run the test with `bazel test //hello_lib:hello_lib_test`. - -### Example: `test` directory - -Integration tests that live in the [`tests` directory][int-tests], they are essentially built as separate crates. Suppose you have the following directory structure where `greeting.rs` is an integration test for the `hello_lib` library crate: - -[int-tests]: http://doc.rust-lang.org/book/testing.html#the-tests-directory - -```output -[workspace]/ - WORKSPACE - hello_lib/ - BUILD - src/ - lib.rs - tests/ - greeting.rs -``` - -`hello_lib/tests/greeting.rs`: -```rust -extern crate hello_lib; - -use hello_lib; - -#[test] -fn test_greeting() { - let hello = greeter::Greeter::new("Hello"); - assert_eq!("Hello world", hello.greeting("world")); -} -``` - -To build the `greeting.rs` integration test, simply add a `rust_test` target -with `greeting.rs` in `srcs` and a dependency on the `hello_lib` target: - -`hello_lib/BUILD`: -```python -load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") - -rust_library( - name = "hello_lib", - srcs = ["src/lib.rs"], -) - -rust_test( - name = "greeting_test", - srcs = ["tests/greeting.rs"], - deps = [":hello_lib"], -) -``` - -Run the test with `bazel test //hello_lib:greeting_test`. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| deps | List of other libraries to be linked to this library target.
These can be either other `rust_library` targets or `cc_library` targets if linking a native library. | List of labels | optional | `[]` | -| srcs | List of Rust `.rs` source files used to build the library.
If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | -| data | List of files used by this rule at compile time and runtime.
If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | -| aliases | Remap crates to a new name or moniker for linkage to this target
These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | -| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.
This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | -| compile_data | List of files used by this rule at compile time.
This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | -| crate | Target inline tests declared in the given crate
These tests are typically those that would be held out under `#[cfg(test)]` declarations. | Label | optional | `None` | -| crate_features | List of features to enable for this crate.
Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | -| crate_name | Crate name to use for this target.
This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | -| crate_root | The file that will be passed to `rustc` to be used for building this crate.
If `crate_root` is not set, then this rule will look for a `lib.rs` file (or `main.rs` for rust_binary) or the single file in `srcs` if `srcs` contains only one file. | Label | optional | `None` | -| edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | `""` | -| env | Specifies additional environment variables to set when the test is executed by bazel test. Values are subject to `$(rootpath)`, `$(execpath)`, location, and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. | Dictionary: String -> String | optional | `{}` | -| env_inherit | Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test. | List of strings | optional | `[]` | -| experimental_use_cc_common_link | Whether to use cc_common.link to link rust binaries. Possible values: [-1, 0, 1]. -1 means use the value of the toolchain.experimental_use_cc_common_link boolean build setting to determine. 0 means do not use cc_common.link (use rustc instead). 1 means use cc_common.link. | Integer | optional | `-1` | -| malloc | Override the default dependency on `malloc`.
By default, Rust binaries linked with cc_common.link are linked against `@bazel_tools//tools/cpp:malloc"`, which is an empty library and the resulting binary will use libc's `malloc`. This label must refer to a `cc_library` rule. | Label | optional | `"@bazel_tools//tools/cpp:malloc"` | -| platform | Optional platform to transition the test to. | Label | optional | `None` | -| proc_macro_deps | List of `rust_proc_macro` targets used to help build this library target. | List of labels | optional | `[]` | -| rustc_env | Dictionary of additional `"key": "value"` environment variables to set for rustc.
rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | Dictionary: String -> String | optional | `{}` | -| rustc_env_files | Files containing additional environment variables to set for rustc.
These files should contain a single variable per line, of format `NAME=value`, and newlines may be included in a value by ending a line with a trailing back-slash (`\\`).
The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged.
Note that the variables here are subject to [workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status) stamping should the `stamp` attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. `NAME={WORKSPACE_STATUS_VARIABLE}`. | List of labels | optional | `[]` | -| rustc_flags | List of compiler flags passed to `rustc`.
These strings are subject to Make variable expansion for predefined source/output path variables like `$location`, `$execpath`, and `$rootpath`. This expansion is useful if you wish to pass a generated file of arguments to rustc: `@$(location //package:target)`. | List of strings | optional | `[]` | -| stamp | Whether to encode build information into the `Rustc` action. Possible values:
- `stamp = 1`: Always stamp the build information into the `Rustc` action, even in [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds. This setting should be avoided, since it potentially kills remote caching for the target and any downstream actions that depend on it.
- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.
- `stamp = -1`: Embedding of build information is controlled by the [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.
Stamped targets are not rebuilt unless their dependencies change.
For example if a `rust_library` is stamped, and a `rust_binary` depends on that library, the stamped library won't be rebuilt when we change sources of the `rust_binary`. This is different from how [`cc_library.linkstamps`](https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library.linkstamp) behaves. | Integer | optional | `0` | -| use_libtest_harness | Whether to use `libtest`. For targets using this flag, individual tests can be run by using the [--test_arg](https://docs.bazel.build/versions/4.0.0/command-line-reference.html#flag--test_arg) flag. E.g. `bazel test //src:rust_test --test_arg=foo::test::test_fn`. | Boolean | optional | `True` | -| version | A version to inject in the cargo environment variable. | String | optional | `"0.0.0"` | - - - - -## rust_toolchain - --rust_toolchain(name, allocator_library, binary_ext, cargo, cargo_clippy, clippy_driver, debug_info, - default_edition, dylib_ext, env, exec_triple, experimental_link_std_dylib, - experimental_use_cc_common_link, extra_exec_rustc_flags, extra_rustc_flags, - extra_rustc_flags_for_crate_types, global_allocator_library, llvm_cov, llvm_profdata, - llvm_tools, opt_level, per_crate_rustc_flags, rust_doc, rust_std, rustc, rustc_lib, - rustfmt, staticlib_ext, stdlib_linkflags, strip_level, target_json, target_triple) -- -Declares a Rust toolchain for use. - -This is for declaring a custom toolchain, eg. for configuring a particular version of rust or supporting a new platform. - -Example: - -Suppose the core rust team has ported the compiler to a new target CPU, called `cpuX`. This support can be used in Bazel by defining a new toolchain definition and declaration: - -```python -load('@rules_rust//rust:toolchain.bzl', 'rust_toolchain') - -rust_toolchain( - name = "rust_cpuX_impl", - binary_ext = "", - dylib_ext = ".so", - exec_triple = "cpuX-unknown-linux-gnu", - rust_doc = "@rust_cpuX//:rustdoc", - rust_std = "@rust_cpuX//:rust_std", - rustc = "@rust_cpuX//:rustc", - rustc_lib = "@rust_cpuX//:rustc_lib", - staticlib_ext = ".a", - stdlib_linkflags = ["-lpthread", "-ldl"], - target_triple = "cpuX-unknown-linux-gnu", -) - -toolchain( - name = "rust_cpuX", - exec_compatible_with = [ - "@platforms//cpu:cpuX", - "@platforms//os:linux", - ], - target_compatible_with = [ - "@platforms//cpu:cpuX", - "@platforms//os:linux", - ], - toolchain = ":rust_cpuX_impl", -) -``` - -Then, either add the label of the toolchain rule to `register_toolchains` in the WORKSPACE, or pass it to the `"--extra_toolchains"` flag for Bazel, and it will be used. - -See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpuX` repository with the actual binaries and libraries. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | Label | optional | `"@rules_rust//ffi/cc/allocator_library"` | -| binary_ext | The extension for binaries created from rustc. | String | required | | -| cargo | The location of the `cargo` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | -| cargo_clippy | The location of the `cargo_clippy` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | -| clippy_driver | The location of the `clippy-driver` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | -| debug_info | Rustc debug info levels per opt level | Dictionary: String -> String | optional | `{"dbg": "2", "fastbuild": "0", "opt": "0"}` | -| default_edition | The edition to use for rust_* rules that don't specify an edition. If absent, every rule is required to specify its `edition` attribute. | String | optional | `""` | -| dylib_ext | The extension for dynamic libraries created from rustc. | String | required | | -| env | Environment variables to set in actions. | Dictionary: String -> String | optional | `{}` | -| exec_triple | The platform triple for the toolchains execution environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations | String | required | | -| experimental_link_std_dylib | Label to a boolean build setting that controls whether whether to link libstd dynamically. | Label | optional | `"@rules_rust//rust/settings:experimental_link_std_dylib"` | -| experimental_use_cc_common_link | Label to a boolean build setting that controls whether cc_common.link is used to link rust binaries. | Label | optional | `"@rules_rust//rust/settings:experimental_use_cc_common_link"` | -| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | `[]` | -| extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration. Subject to location expansion with respect to the srcs of the `rust_std` attribute. | List of strings | optional | `[]` | -| extra_rustc_flags_for_crate_types | Extra flags to pass to rustc based on crate type | Dictionary: String -> List of strings | optional | `{}` | -| global_allocator_library | Target that provides allocator functions for when a global allocator is present. | Label | optional | `"@rules_rust//ffi/cc/global_allocator_library"` | -| llvm_cov | The location of the `llvm-cov` binary. Can be a direct source or a filegroup containing one item. If None, rust code is not instrumented for coverage. | Label | optional | `None` | -| llvm_profdata | The location of the `llvm-profdata` binary. Can be a direct source or a filegroup containing one item. If `llvm_cov` is None, this can be None as well and rust code is not instrumented for coverage. | Label | optional | `None` | -| llvm_tools | LLVM tools that are shipped with the Rust toolchain. | Label | optional | `None` | -| opt_level | Rustc optimization levels. | Dictionary: String -> String | optional | `{"dbg": "0", "fastbuild": "0", "opt": "3"}` | -| per_crate_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | `[]` | -| rust_doc | The location of the `rustdoc` binary. Can be a direct source or a filegroup containing one item. | Label | required | | -| rust_std | The Rust standard library. | Label | required | | -| rustc | The location of the `rustc` binary. Can be a direct source or a filegroup containing one item. | Label | required | | -| rustc_lib | The libraries used by rustc during compilation. | Label | optional | `None` | -| rustfmt | **Deprecated**: Instead see [rustfmt_toolchain](#rustfmt_toolchain) | Label | optional | `None` | -| staticlib_ext | The extension for static libraries created from rustc. | String | required | | -| stdlib_linkflags | Additional linker flags to use when Rust standard library is linked by a C++ linker (rustc will deal with these automatically). Subject to location expansion with respect to the srcs of the `rust_std` attribute. | List of strings | required | | -| strip_level | Rustc strip levels. For all potential options, see https://doc.rust-lang.org/rustc/codegen-options/index.html#strip | Dictionary: String -> String | optional | `{"dbg": "none", "fastbuild": "none", "opt": "debuginfo"}` | -| target_json | Override the target_triple with a custom target specification. For more details see: https://doc.rust-lang.org/rustc/targets/custom.html | String | optional | `""` | -| target_triple | The platform triple for the toolchains target environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations | String | optional | `""` | - - - - -## rustfmt_test - --rustfmt_test(name, targets) -- -A test rule for performing `rustfmt --check` on a set of targets - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| targets | Rust targets to run `rustfmt --check` on. | List of labels | optional | `[]` | - - - - -## rustfmt_toolchain - --rustfmt_toolchain(name, rustc, rustc_lib, rustfmt) -- -A toolchain for [rustfmt](https://rust-lang.github.io/rustfmt/) - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| rustc | The location of the `rustc` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | -| rustc_lib | The libraries used by rustc during compilation. | Label | optional | `None` | -| rustfmt | The location of the `rustfmt` binary. Can be a direct source or a filegroup containing one item. | Label | required | | - - - - -## CrateInfo - --CrateInfo(aliases, compile_data, compile_data_targets, data, deps, edition, is_test, metadata, name, - output, owner, proc_macro_deps, root, rustc_env, rustc_env_files, rustc_output, - rustc_rmeta_output, srcs, std_dylib, type, wrapped_crate_type) -- -A provider containing general Crate information. - -**FIELDS** - - -| Name | Description | -| :------------- | :------------- | -| aliases | Dict[Label, String]: Renamed and aliased crates | -| compile_data | depset[File]: Compile data required by this crate. | -| compile_data_targets | depset[Label]: Compile data targets required by this crate. | -| data | depset[File]: Compile data required by crates that use the current crate as a proc-macro. | -| deps | depset[DepVariantInfo]: This crate's (rust or cc) dependencies' providers. | -| edition | str: The edition of this crate. | -| is_test | bool: If the crate is being compiled in a test context | -| metadata | File: The output from rustc from producing the output file. It is optional. | -| name | str: The name of this crate. | -| output | File: The output File that will be produced, depends on crate type. | -| owner | Label: The label of the target that produced this CrateInfo | -| proc_macro_deps | depset[DepVariantInfo]: This crate's rust proc_macro dependencies' providers. | -| root | File: The source File entrypoint to this crate, eg. lib.rs | -| rustc_env | Dict[String, String]: Additional `"key": "value"` environment variables to set for rustc. | -| rustc_env_files | [File]: Files containing additional environment variables to set for rustc. | -| rustc_output | File: The output from rustc from producing the output file. It is optional. | -| rustc_rmeta_output | File: The rmeta file produced for this crate. It is optional. | -| srcs | depset[File]: All source Files that are part of the crate. | -| std_dylib | File: libstd.so file | -| type | str: The type of this crate (see [rustc --crate-type](https://doc.rust-lang.org/rustc/command-line-arguments.html#--crate-type-a-list-of-types-of-crates-for-the-compiler-to-emit)). | -| wrapped_crate_type | str, optional: The original crate type for targets generated using a previously defined crate (typically tests using the `rust_test::crate` attribute) | - - - - -## DepInfo - --DepInfo(dep_env, direct_crates, link_search_path_files, transitive_build_infos, - transitive_crate_outputs, transitive_crates, transitive_data, transitive_metadata_outputs, - transitive_noncrates, transitive_proc_macro_data) -- -A provider containing information about a Crate's dependencies. - -**FIELDS** - - -| Name | Description | -| :------------- | :------------- | -| dep_env | File: File with environment variables direct dependencies build scripts rely upon. | -| direct_crates | depset[AliasableDepInfo] | -| link_search_path_files | depset[File]: All transitive files containing search paths to pass to the linker | -| transitive_build_infos | depset[BuildInfo] | -| transitive_crate_outputs | depset[File]: All transitive crate outputs. | -| transitive_crates | depset[CrateInfo] | -| transitive_data | depset[File]: Data of all transitive non-macro dependencies. | -| transitive_metadata_outputs | depset[File]: All transitive metadata dependencies (.rmeta, for crates that provide them) and all transitive object dependencies (.rlib) for crates that don't provide metadata. | -| transitive_noncrates | depset[LinkerInput]: All transitive dependencies that aren't crates. | -| transitive_proc_macro_data | depset[File]: Data of all transitive proc-macro dependencies, and non-macro dependencies of those macros. | - - - - -## StdLibInfo - --StdLibInfo(alloc_files, between_alloc_and_core_files, between_core_and_std_files, core_files, - dot_a_files, memchr_files, panic_files, self_contained_files, srcs, std_dylib, std_files, - std_rlibs, test_files) -- -A collection of files either found within the `rust-stdlib` artifact or generated based on existing files. - -**FIELDS** - - -| Name | Description | -| :------------- | :------------- | -| alloc_files | List[File]: `.a` files related to the `alloc` module. | -| between_alloc_and_core_files | List[File]: `.a` files related to the `compiler_builtins` module. | -| between_core_and_std_files | List[File]: `.a` files related to all modules except `adler`, `alloc`, `compiler_builtins`, `core`, and `std`. | -| core_files | List[File]: `.a` files related to the `core` and `adler` modules | -| dot_a_files | Depset[File]: Generated `.a` files | -| memchr_files | Depset[File]: `.a` files associated with the `memchr` module. | -| panic_files | Depset[File]: `.a` files associated with `panic_unwind` and `panic_abort`. | -| self_contained_files | List[File]: All `.o` files from the `self-contained` directory. | -| srcs | List[Target]: All targets from the original `srcs` attribute. | -| std_dylib | File: libstd.so file | -| std_files | Depset[File]: `.a` files associated with the `std` module. | -| std_rlibs | List[File]: All `.rlib` files | -| test_files | Depset[File]: `.a` files associated with the `test` module. | - - - - -## cargo_build_script - --cargo_build_script(name, edition, crate_name, crate_root, srcs, crate_features, version, deps, - link_deps, proc_macro_deps, build_script_env, use_default_shell_env, data, - compile_data, tools, links, rundir, rustc_env, rustc_env_files, rustc_flags, - visibility, tags, aliases, pkg_name, kwargs) -- -Compile and execute a rust build script to generate build attributes - -This rules take the same arguments as rust_binary. - -Example: - -Suppose you have a crate with a cargo build script `build.rs`: - -```output -[workspace]/ - hello_lib/ - BUILD - build.rs - src/ - lib.rs -``` - -Then you want to use the build script in the following: - -`hello_lib/BUILD`: -```python -package(default_visibility = ["//visibility:public"]) - -load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library") -load("@rules_rust//cargo:defs.bzl", "cargo_build_script") - -# This will run the build script from the root of the workspace, and -# collect the outputs. -cargo_build_script( - name = "build_script", - srcs = ["build.rs"], - # Optional environment variables passed during build.rs compilation - rustc_env = { - "CARGO_PKG_VERSION": "0.1.2", - }, - # Optional environment variables passed during build.rs execution. - # Note that as the build script's working directory is not execroot, - # execpath/location will return an absolute path, instead of a relative - # one. - build_script_env = { - "SOME_TOOL_OR_FILE": "$(execpath @tool//:binary)" - }, - # Optional data/tool dependencies - data = ["@tool//:binary"], -) - -rust_library( - name = "hello_lib", - srcs = [ - "src/lib.rs", - ], - deps = [":build_script"], -) -``` - -The `hello_lib` target will be build with the flags and the environment variables declared by the build script in addition to the file generated by it. - - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| name | The name for the underlying rule. This should be the name of the package being compiled, optionally with a suffix of `_bs`. Otherwise, you can set the package name via `pkg_name`. | none | -| edition | The rust edition to use for the internal binary crate. | `None` | -| crate_name | Crate name to use for build script. | `None` | -| crate_root | The file that will be passed to rustc to be used for building this crate. | `None` | -| srcs | Souce files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made. | `[]` | -| crate_features | A list of features to enable for the build script. | `[]` | -| version | The semantic version (semver) of the crate. | `None` | -| deps | The build-dependencies of the crate. | `[]` | -| link_deps | The subset of the (normal) dependencies of the crate that have the links attribute and therefore provide environment variables to this build script. | `[]` | -| proc_macro_deps | List of rust_proc_macro targets used to build the script. | `[]` | -| build_script_env | Environment variables for build scripts. | `{}` | -| use_default_shell_env | Whether or not to include the default shell environment for the build script action. If unset the global setting `@rules_rust//cargo/settings:use_default_shell_env` will be used to determine this value. | `None` | -| data | Files needed by the build script. | `[]` | -| compile_data | Files needed for the compilation of the build script. | `[]` | -| tools | Tools (executables) needed by the build script. | `[]` | -| links | Name of the native library this crate links against. | `None` | -| rundir | A directory to `cd` to before the cargo_build_script is run. This should be a path relative to the exec root.
The default behaviour (and the behaviour if rundir is set to the empty string) is to change to the relative path corresponding to the cargo manifest directory, which replicates the normal behaviour of cargo so it is easy to write compatible build scripts.
If set to `.`, the cargo build script will run in the exec root. | `None` | -| rustc_env | Environment variables to set in rustc when compiling the build script. | `{}` | -| rustc_env_files | Files containing additional environment variables to set for rustc when building the build script. | `[]` | -| rustc_flags | List of compiler flags passed to `rustc`. | `[]` | -| visibility | Visibility to apply to the generated build script output. | `None` | -| tags | (list of str, optional): Tags to apply to the generated build script output. | `None` | -| aliases | Remap crates to a new name or moniker for linkage to this target. These are other `rust_library` targets and will be presented as the new name given. | `None` | -| pkg_name | Override the package name used for the build script. This is useful if the build target name gets too long otherwise. | `None` | -| kwargs | Forwards to the underlying `rust_binary` rule. An exception is the `compatible_with` attribute, which shouldn't be forwarded to the `rust_binary`, as the `rust_binary` is only built and used in `exec` mode. We propagate the `compatible_with` attribute to the `_build_scirpt_run` target. | none | - - - - -## cargo_env - --cargo_env(env) -- -A helper for generating platform specific environment variables - -```python -load("@rules_rust//rust:defs.bzl", "rust_common") -load("@rules_rust//cargo:defs.bzl", "cargo_bootstrap_repository", "cargo_env") - -cargo_bootstrap_repository( - name = "bootstrapped_bin", - cargo_lockfile = "//:Cargo.lock", - cargo_toml = "//:Cargo.toml", - srcs = ["//:resolver_srcs"], - version = rust_common.default_version, - binary = "my-crate-binary", - env = { - "x86_64-unknown-linux-gnu": cargo_env({ - "FOO": "BAR", - }), - }, - env_label = { - "aarch64-unknown-linux-musl": cargo_env({ - "DOC": "//:README.md", - }), - } -) -``` - - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| env | A map of environment variables | none | - -**RETURNS** - -str: A json encoded string of the environment variables - - - - -## rules_rust_dependencies - --rules_rust_dependencies() -- -Dependencies used in the implementation of `rules_rust`. - - - - - -## rust_analyzer_toolchain_repository - --rust_analyzer_toolchain_repository(name, version, exec_compatible_with, target_compatible_with, - sha256s, urls, auth, netrc, auth_patterns) -- -Assemble a remote rust_analyzer_toolchain target based on the given params. - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| name | The name of the toolchain proxy repository contianing the registerable toolchain. | none | -| version | The version of the tool among "nightly", "beta', or an exact version. | none | -| exec_compatible_with | A list of constraints for the execution platform for this toolchain. | `[]` | -| target_compatible_with | A list of constraints for the target platform for this toolchain. | `[]` | -| sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_register_toolchains](#rust_register_toolchains) for more details. | `None` | -| urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.xz'] | `None` | -| auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | `None` | -| netrc | .netrc file to use for authentication; mirrors the eponymous attribute from http_archive | `None` | -| auth_patterns | Override mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archive | `None` | - -**RETURNS** - -str: The name of a registerable rust_analyzer_toolchain. - - - - -## rust_register_toolchains - --rust_register_toolchains(dev_components, edition, allocator_library, global_allocator_library, - register_toolchains, rustfmt_version, rust_analyzer_version, sha256s, - extra_target_triples, extra_rustc_flags, extra_exec_rustc_flags, urls, - versions, aliases, hub_name, compact_windows_names, toolchain_triples, - rustfmt_toolchain_triples, extra_toolchain_infos) -- -Emits a default set of toolchains for Linux, MacOS, and Freebsd - -Skip this macro and call the `rust_repository_set` macros directly if you need a compiler for other hosts or for additional target triples. - -The `sha256s` attribute represents a dict associating tool subdirectories to sha256 hashes. As an example: -```python -{ - "rust-1.46.0-x86_64-unknown-linux-gnu": "e3b98bc3440fe92817881933f9564389eccb396f5f431f33d48b979fa2fbdcf5", - "rustfmt-1.4.12-x86_64-unknown-linux-gnu": "1894e76913303d66bf40885a601462844eec15fca9e76a6d13c390d7000d64b0", - "rust-std-1.46.0-x86_64-unknown-linux-gnu": "ac04aef80423f612c0079829b504902de27a6997214eb58ab0765d02f7ec1dbc", -} -``` -This would match for `exec_triple = "x86_64-unknown-linux-gnu"`. If not specified, rules_rust pulls from a non-exhaustive list of known checksums.. - -See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more details. - - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | `False` | -| edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every target is required to specify its `edition` attribute. | `None` | -| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | `None` | -| global_allocator_library | Target that provides allocator functions when global allocator is used with cc_common.link. | `None` | -| register_toolchains | If true, repositories will be generated to produce and register `rust_toolchain` targets. | `True` | -| rustfmt_version | The version of rustfmt. If none is supplied and only a single version in `versions` is given, then this defaults to that version, otherwise will default to the default nightly version. | `None` | -| rust_analyzer_version | The version of Rustc to pair with rust-analyzer. | `None` | -| sha256s | A dict associating tool subdirectories to sha256 hashes. | `None` | -| extra_target_triples | Additional rust-style targets that rust toolchains should support. | `["wasm32-unknown-unknown", "wasm32-wasip1"]` | -| extra_rustc_flags | Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration. | `None` | -| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration. | `None` | -| urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). | `["https://static.rust-lang.org/dist/{}.tar.xz"]` | -| versions | A list of toolchain versions to download. This parameter only accepts one versions per channel. E.g. `["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]`. | `["1.83.0", "nightly/2024-11-28"]` | -| aliases | A mapping of "full" repository name to another name to use instead. | `{}` | -| hub_name | The name of the bzlmod hub repository for toolchains. | `None` | -| compact_windows_names | Whether or not to produce compact repository names for windows toolchains. This is to avoid MAX_PATH issues. | `True` | -| toolchain_triples | Mapping of rust target triple -> repository name to create. | `{"aarch64-apple-darwin": "rust_darwin_aarch64", "aarch64-pc-windows-msvc": "rust_windows_aarch64", "aarch64-unknown-linux-gnu": "rust_linux_aarch64", "s390x-unknown-linux-gnu": "rust_linux_s390x", "x86_64-apple-darwin": "rust_darwin_x86_64", "x86_64-pc-windows-msvc": "rust_windows_x86_64", "x86_64-unknown-freebsd": "rust_freebsd_x86_64", "x86_64-unknown-linux-gnu": "rust_linux_x86_64"}` | -| rustfmt_toolchain_triples | Like toolchain_triples, but for rustfmt toolchains. | `{"aarch64-apple-darwin": "rust_darwin_aarch64", "aarch64-pc-windows-msvc": "rust_windows_aarch64", "aarch64-unknown-linux-gnu": "rust_linux_aarch64", "s390x-unknown-linux-gnu": "rust_linux_s390x", "x86_64-apple-darwin": "rust_darwin_x86_64", "x86_64-pc-windows-msvc": "rust_windows_x86_64", "x86_64-unknown-freebsd": "rust_freebsd_x86_64", "x86_64-unknown-linux-gnu": "rust_linux_x86_64"}` | -| extra_toolchain_infos | (dict[str, dict], optional): Mapping of information about extra toolchains which were created outside of this call, which should be added to the hub repo. | `None` | - - - - -## rust_repositories - --rust_repositories(kwargs) -- -**Deprecated**: Use [rules_rust_dependencies](#rules_rust_dependencies) and [rust_register_toolchains](#rust_register_toolchains) directly. - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| kwargs | Keyword arguments for the `rust_register_toolchains` macro. | none | - - - - -## rust_repository_set - --rust_repository_set(name, versions, exec_triple, target_settings, allocator_library, - global_allocator_library, extra_target_triples, rustfmt_version, edition, - dev_components, extra_rustc_flags, extra_exec_rustc_flags, opt_level, sha256s, - urls, auth, netrc, auth_patterns, register_toolchain, exec_compatible_with, - default_target_compatible_with, aliases, compact_windows_names) -- -Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains. - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| name | The name of the generated repository | none | -| versions | A list of toolchain versions to download. This paramter only accepts one versions per channel. E.g. `["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]`. | none | -| exec_triple | The Rust-style target that this compiler runs on | none | -| target_settings | A list of config_settings that must be satisfied by the target configuration in order for this set of toolchains to be selected during toolchain resolution. | `[]` | -| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | `None` | -| global_allocator_library | Target that provides allocator functions a global allocator is used with cc_common.link. | `None` | -| extra_target_triples | Additional rust-style targets that this set of toolchains should support. If a map, values should be (optional) target_compatible_with lists for that particular target triple. | `{}` | -| rustfmt_version | The version of rustfmt to be associated with the toolchain. | `None` | -| edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its `edition` attribute. | `None` | -| dev_components | Whether to download the rustc-dev components. Requires version to be "nightly". | `False` | -| extra_rustc_flags | Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration. | `None` | -| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration. | `None` | -| opt_level | Dictionary of target triples to optimiztion config. | `None` | -| sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_register_toolchains](#rust_register_toolchains) for more details. | `None` | -| urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). | `["https://static.rust-lang.org/dist/{}.tar.xz"]` | -| auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | `None` | -| netrc | .netrc file to use for authentication; mirrors the eponymous attribute from http_archive | `None` | -| auth_patterns | Override mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archive | `None` | -| register_toolchain | If True, the generated `rust_toolchain` target will become a registered toolchain. | `True` | -| exec_compatible_with | A list of constraints for the execution platform for this toolchain. | `None` | -| default_target_compatible_with | A list of constraints for the target platform for this toolchain when the exec platform is the same as the target platform. | `None` | -| aliases | Replacement names to use for toolchains created by this macro. | `{}` | -| compact_windows_names | Whether or not to produce compact repository names for windows toolchains. This is to avoid MAX_PATH issues. | `True` | - -**RETURNS** - -dict[str, dict]: A dict of informations about all generated toolchains. - - - - -## rust_test_suite - --rust_test_suite(name, srcs, shared_srcs, kwargs) -- -A rule for creating a test suite for a set of `rust_test` targets. - -This rule can be used for setting up typical rust [integration tests][it]. Given the following -directory structure: - -```text -[crate]/ - BUILD.bazel - src/ - lib.rs - main.rs - tests/ - integrated_test_a.rs - integrated_test_b.rs - integrated_test_c.rs - patterns/ - fibonacci_test.rs - helpers/ - mod.rs -``` - -The rule can be used to generate [rust_test](#rust_test) targets for each source file under `tests` -and a [test_suite][ts] which encapsulates all tests. - -```python -load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test_suite") - -rust_library( - name = "math_lib", - srcs = ["src/lib.rs"], -) - -rust_binary( - name = "math_bin", - srcs = ["src/main.rs"], -) - -rust_test_suite( - name = "integrated_tests_suite", - srcs = glob(["tests/**"]), - shared_srcs=glob(["tests/helpers/**"]), - deps = [":math_lib"], -) -``` - -[it]: https://doc.rust-lang.org/rust-by-example/testing/integration_testing.html -[ts]: https://docs.bazel.build/versions/master/be/general.html#test_suite - - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| name | The name of the `test_suite`. | none | -| srcs | All test sources, typically `glob(["tests/**/*.rs"])`. | none | -| shared_srcs | Optional argument for sources shared among tests, typically helper functions. | `[]` | -| kwargs | Additional keyword arguments for the underyling [rust_test](#rust_test) targets. The `tags` argument is also passed to the generated `test_suite` target. | none | - - - - -## rust_toolchain_repository - --rust_toolchain_repository(name, version, exec_triple, target_triple, exec_compatible_with, - target_compatible_with, target_settings, channel, allocator_library, - global_allocator_library, rustfmt_version, edition, dev_components, - extra_rustc_flags, extra_exec_rustc_flags, opt_level, sha256s, urls, auth, - netrc, auth_patterns) -- -Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains. - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| name | The name of the generated repository | none | -| version | The version of the tool among "nightly", "beta", or an exact version. | none | -| exec_triple | The Rust-style target that this compiler runs on. | none | -| target_triple | The Rust-style target to build for. | none | -| exec_compatible_with | A list of constraints for the execution platform for this toolchain. | `None` | -| target_compatible_with | A list of constraints for the target platform for this toolchain. | `None` | -| target_settings | A list of config_settings that must be satisfied by the target configuration in order for this toolchain to be selected during toolchain resolution. | `[]` | -| channel | The channel of the Rust toolchain. | `None` | -| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | `None` | -| global_allocator_library | Target that provides allocator functions when a global allocator is used with cc_common.link. | `None` | -| rustfmt_version | The version of rustfmt to be associated with the toolchain. | `None` | -| edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its `edition` attribute. | `None` | -| dev_components | Whether to download the rustc-dev components. Requires version to be "nightly". Defaults to False. | `False` | -| extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration. | `None` | -| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration. | `None` | -| opt_level | Optimization level config for this toolchain. | `None` | -| sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_register_toolchains](#rust_register_toolchains) for more details. | `None` | -| urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.xz'] | `["https://static.rust-lang.org/dist/{}.tar.xz"]` | -| auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | `None` | -| netrc | .netrc file to use for authentication; mirrors the eponymous attribute from http_archive | `None` | -| auth_patterns | A list of patterns to match against urls for which the auth object should be used. | `None` | - -**RETURNS** - -dict[str, str]: Information about the registerable toolchain created by this rule. - - - - -## rust_analyzer_aspect - --rust_analyzer_aspect(name) -- -Annotates rust rules with RustAnalyzerInfo later used to build a rust-project.json - -**ASPECT ATTRIBUTES** - - -| Name | Type | -| :------------- | :------------- | -| deps| String | -| proc_macro_deps| String | -| crate| String | -| actual| String | -| proto| String | - - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | - - - - -## rust_clippy_aspect - --rust_clippy_aspect(name) -- -Executes the clippy checker on specified targets. - -This aspect applies to existing rust_library, rust_test, and rust_binary rules. - -As an example, if the following is defined in `examples/hello_lib/BUILD.bazel`: - -```python -load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") - -rust_library( - name = "hello_lib", - srcs = ["src/lib.rs"], -) - -rust_test( - name = "greeting_test", - srcs = ["tests/greeting.rs"], - deps = [":hello_lib"], -) -``` - -Then the targets can be analyzed with clippy using the following command: - -```output -$ bazel build --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect --output_groups=clippy_checks //hello_lib:all -``` - -**ASPECT ATTRIBUTES** - - - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | - - - - -## rustfmt_aspect - --rustfmt_aspect(name) -- -This aspect is used to gather information about a crate for use in rustfmt and perform rustfmt checks - -Output Groups: - -- `rustfmt_checks`: Executes `rustfmt --check` on the specified target. - -The build setting `@rules_rust//rust/settings:rustfmt.toml` is used to control the Rustfmt [configuration settings][cs] -used at runtime. - -[cs]: https://rust-lang.github.io/rustfmt/ - -This aspect is executed on any target which provides the `CrateInfo` provider. However -users may tag a target with `no-rustfmt` or `no-format` to have it skipped. Additionally, -generated source files are also ignored by this aspect. - -**ASPECT ATTRIBUTES** - - - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | - - - - -## cargo_bootstrap_repository - --cargo_bootstrap_repository(name, srcs, binary, build_mode, cargo_config, cargo_lockfile, cargo_toml, - compressed_windows_toolchain_names, env, env_label, repo_mapping, - rust_toolchain_cargo_template, rust_toolchain_rustc_template, timeout, - version) -- -A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/cargo/) - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this repository. | Name | required | | -| srcs | Souce files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made | List of labels | optional | `[]` | -| binary | The binary to build (the `--bin` parameter for Cargo). If left empty, the repository name will be used. | String | optional | `""` | -| build_mode | The build mode the binary should be built with | String | optional | `"release"` | -| cargo_config | The path of the Cargo configuration (`Config.toml`) file. | Label | optional | `None` | -| cargo_lockfile | The lockfile of the crate_universe resolver | Label | required | | -| cargo_toml | The path of the `Cargo.toml` file. | Label | required | | -| compressed_windows_toolchain_names | Wether or not the toolchain names of windows toolchains are expected to be in a `compressed` format. | Boolean | optional | `True` | -| env | A mapping of platform triple to a set of environment variables. See [cargo_env](#cargo_env) for usage details. Additionally, the platform triple `*` applies to all platforms. | Dictionary: String -> String | optional | `{}` | -| env_label | A mapping of platform triple to a set of environment variables. This attribute differs from `env` in that all variables passed here must be fully qualified labels of files. See [cargo_env](#cargo_env) for usage details. Additionally, the platform triple `*` applies to all platforms. | Dictionary: String -> String | optional | `{}` | -| repo_mapping | In `WORKSPACE` context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.
For example, an entry `"@foo": "@bar"` declares that, for any time this repository depends on `@foo` (such as a dependency on `@foo//some:target`, it should actually resolve that dependency within globally-declared `@bar` (`@bar//some:target`).
This attribute is _not_ supported in `MODULE.bazel` context (when invoking a repository rule inside a module extension's implementation function). | Dictionary: String -> String | optional | | -| rust_toolchain_cargo_template | The template to use for finding the host `cargo` binary. `{version}` (eg. '1.53.0'), `{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), `{system}` (eg. 'darwin'), `{channel}` (eg. 'stable'), and `{tool}` (eg. 'rustc.exe') will be replaced in the string if present. | String | optional | `"@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}"` | -| rust_toolchain_rustc_template | The template to use for finding the host `rustc` binary. `{version}` (eg. '1.53.0'), `{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), `{system}` (eg. 'darwin'), `{channel}` (eg. 'stable'), and `{tool}` (eg. 'rustc.exe') will be replaced in the string if present. | String | optional | `"@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}"` | -| timeout | Maximum duration of the Cargo build command in seconds | Integer | optional | `600` | -| version | The version of Rust the currently registered toolchain is using. Eg. `1.56.0`, or `nightly/2021-09-08` | String | optional | `"1.83.0"` | - - - - -## rust_toolchain_repository_proxy - --rust_toolchain_repository_proxy(name, exec_compatible_with, repo_mapping, target_compatible_with, - target_settings, toolchain, toolchain_type) -- -Generates a toolchain-bearing repository that declares the toolchains from some other rust_toolchain_repository. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this repository. | Name | required | | -| exec_compatible_with | A list of constraints for the execution platform for this toolchain. | List of strings | optional | `[]` | -| repo_mapping | In `WORKSPACE` context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.
For example, an entry `"@foo": "@bar"` declares that, for any time this repository depends on `@foo` (such as a dependency on `@foo//some:target`, it should actually resolve that dependency within globally-declared `@bar` (`@bar//some:target`).
This attribute is _not_ supported in `MODULE.bazel` context (when invoking a repository rule inside a module extension's implementation function). | Dictionary: String -> String | optional | | -| target_compatible_with | A list of constraints for the target platform for this toolchain. | List of strings | optional | `[]` | -| target_settings | A list of config_settings that must be satisfied by the target configuration in order for this toolchain to be selected during toolchain resolution. | List of strings | optional | `[]` | -| toolchain | The name of the toolchain implementation target. | String | required | | -| toolchain_type | The toolchain type of the toolchain to declare | String | required | | - - - - -## rust_toolchain_tools_repository - --rust_toolchain_tools_repository(name, allocator_library, auth, auth_patterns, dev_components, - edition, exec_triple, extra_exec_rustc_flags, extra_rustc_flags, - global_allocator_library, netrc, opt_level, repo_mapping, - rustfmt_version, sha256s, target_triple, urls, version) -- -Composes a single workspace containing the toolchain components for compiling on a given platform to a series of target platforms. - -A given instance of this rule should be accompanied by a toolchain_repository_proxy invocation to declare its toolchains to Bazel; the indirection allows separating toolchain selection from toolchain fetching. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this repository. | Name | required | | -| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | String | optional | `"@rules_rust//ffi/cc/allocator_library"` | -| auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | Dictionary: String -> String | optional | `{}` | -| auth_patterns | A list of patterns to match against urls for which the auth object should be used. | List of strings | optional | `[]` | -| dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | Boolean | optional | `False` | -| edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its `edition` attribute. | String | optional | `""` | -| exec_triple | The Rust-style target that this compiler runs on | String | required | | -| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | `[]` | -| extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | `[]` | -| global_allocator_library | Target that provides allocator functions when a global allocator is used with cc_common.link. | String | optional | `"@rules_rust//ffi/cc/global_allocator_library"` | -| netrc | .netrc file to use for authentication; mirrors the eponymous attribute from http_archive | String | optional | `""` | -| opt_level | Rustc optimization levels. For more details see the documentation for `rust_toolchain.opt_level`. | Dictionary: String -> String | optional | `{}` | -| repo_mapping | In `WORKSPACE` context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.
For example, an entry `"@foo": "@bar"` declares that, for any time this repository depends on `@foo` (such as a dependency on `@foo//some:target`, it should actually resolve that dependency within globally-declared `@bar` (`@bar//some:target`).
This attribute is _not_ supported in `MODULE.bazel` context (when invoking a repository rule inside a module extension's implementation function). | Dictionary: String -> String | optional | | -| rustfmt_version | The version of the tool among "nightly", "beta", or an exact version. | String | optional | `""` | -| sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_register_toolchains](#rust_register_toolchains) for more details. | Dictionary: String -> String | optional | `{}` | -| target_triple | The Rust-style target that this compiler builds for. | String | required | | -| urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). | List of strings | optional | `["https://static.rust-lang.org/dist/{}.tar.xz"]` | -| version | The version of the tool among "nightly", "beta", or an exact version. | String | required | | - - diff --git a/docs/src/providers.md b/docs/src/providers.md index 7e19b7a0f9..97edab2093 100644 --- a/docs/src/providers.md +++ b/docs/src/providers.md @@ -1,15 +1,22 @@ -# Providers -* [CrateInfo](#CrateInfo) -* [DepInfo](#DepInfo) -* [StdLibInfo](#StdLibInfo) +Module containing definitions of all Rust providers. + + +## Providers + +- [CrateInfo](#CrateInfo) +- [DepInfo](#DepInfo) +- [StdLibInfo](#StdLibInfo) + ## CrateInfo+load("@rules_rust//rust/private:providers.bzl", "CrateInfo") + CrateInfo(aliases, compile_data, compile_data_targets, data, deps, edition, is_test, metadata, name, output, owner, proc_macro_deps, root, rustc_env, rustc_env_files, rustc_output, rustc_rmeta_output, srcs, std_dylib, type, wrapped_crate_type) @@ -19,7 +26,6 @@ A provider containing general Crate information. **FIELDS** - | Name | Description | | :------------- | :------------- | | aliases | Dict[Label, String]: Renamed and aliased crates | @@ -50,6 +56,8 @@ A provider containing general Crate information. ## DepInfo+load("@rules_rust//rust/private:providers.bzl", "DepInfo") + DepInfo(dep_env, direct_crates, link_search_path_files, transitive_build_infos, transitive_crate_outputs, transitive_crates, transitive_data, transitive_metadata_outputs, transitive_noncrates, transitive_proc_macro_data) @@ -59,7 +67,6 @@ A provider containing information about a Crate's dependencies. **FIELDS** - | Name | Description | | :------------- | :------------- | | dep_env | File: File with environment variables direct dependencies build scripts rely upon. | @@ -79,6 +86,8 @@ A provider containing information about a Crate's dependencies. ## StdLibInfo+load("@rules_rust//rust/private:providers.bzl", "StdLibInfo") + StdLibInfo(alloc_files, between_alloc_and_core_files, between_core_and_std_files, core_files, dot_a_files, memchr_files, panic_files, self_contained_files, srcs, std_dylib, std_files, std_rlibs, test_files) @@ -88,7 +97,6 @@ A collection of files either found within the `rust-stdlib` artifact or generate **FIELDS** - | Name | Description | | :------------- | :------------- | | alloc_files | List[File]: `.a` files related to the `alloc` module. | diff --git a/docs/src/rust.md b/docs/src/rust.md index a537de7f0e..e9eebc989f 100644 --- a/docs/src/rust.md +++ b/docs/src/rust.md @@ -1,89 +1,21 @@ -# Rust -* [rust_binary](#rust_binary) -* [rust_library](#rust_library) -* [rust_library_group](#rust_library_group) -* [rust_static_library](#rust_static_library) -* [rust_shared_library](#rust_shared_library) -* [rust_proc_macro](#rust_proc_macro) -* [rust_test](#rust_test) -* [rust_test_suite](#rust_test_suite) -* [error_format](#error_format) -* [extra_rustc_flag](#extra_rustc_flag) -* [extra_rustc_flags](#extra_rustc_flags) -* [capture_clippy_output](#capture_clippy_output) +Public entry point to all Rust rules and supported APIs. - -## capture_clippy_output +## Rules --capture_clippy_output(name) -- -Control whether to print clippy output or store it to a file, using the configured error_format. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | - - - - -## error_format - --error_format(name) -- -Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) flag from the command line with `--@rules_rust//rust/settings:error_format`. See rustc documentation for valid values. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | - - - - -## extra_rustc_flag - --extra_rustc_flag(name) -+- [rust_binary](#rust_binary) +- [rust_library](#rust_library) +- [rust_library_group](#rust_library_group) +- [rust_proc_macro](#rust_proc_macro) +- [rust_shared_library](#rust_shared_library) +- [rust_static_library](#rust_static_library) +- [rust_test](#rust_test) -Add additional rustc_flag from the command line with `--@rules_rust//rust/settings:extra_rustc_flag`. Multiple uses are accumulated and appended after the extra_rustc_flags. +## Functions -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | - - - - -## extra_rustc_flags - --extra_rustc_flags(name) -- -Add additional rustc_flags from the command line with `--@rules_rust//rust/settings:extra_rustc_flags`. This flag should only be used for flags that need to be applied across the entire build. For options that apply to individual crates, use the rustc_flags attribute on the individual crate's rule instead. NOTE: These flags not applied to the exec configuration (proc-macros, cargo_build_script, etc); use `--@rules_rust//rust/settings:extra_exec_rustc_flags` to apply flags to the exec configuration. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | +- [rust_test_suite](#rust_test_suite) @@ -91,6 +23,8 @@ Add additional rustc_flags from the command line with `--@rules_rust//rust/setti ## rust_binary+load("@rules_rust//rust:defs.bzl", "rust_binary") + rust_binary(name, deps, srcs, data, aliases, alwayslink, binary_name, compile_data, crate_features, crate_name, crate_root, crate_type, edition, env, experimental_use_cc_common_link, linker_script, malloc, out_binary, platform, proc_macro_deps, rustc_env, rustc_env_files, @@ -221,6 +155,8 @@ is available under the key `dsym_folder` in `OutputGroupInfo`. ## rust_library+load("@rules_rust//rust:defs.bzl", "rust_library") + rust_library(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features, crate_name, crate_root, disable_pipelining, edition, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp, version) @@ -319,6 +255,8 @@ INFO: Elapsed time: 1.245s, Critical Path: 1.01s ## rust_library_group+load("@rules_rust//rust:defs.bzl", "rust_library_group") + rust_library_group(name, deps)@@ -369,6 +307,8 @@ rust_library( ## rust_proc_macro+load("@rules_rust//rust:defs.bzl", "rust_proc_macro") + rust_proc_macro(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features, crate_name, crate_root, edition, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp, version) @@ -405,6 +345,8 @@ Builds a Rust proc-macro crate. ## rust_shared_library+load("@rules_rust//rust:defs.bzl", "rust_shared_library") + rust_shared_library(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features, crate_name, crate_root, edition, experimental_use_cc_common_link, malloc, platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp, @@ -453,6 +395,8 @@ When building the whole binary in Bazel, use `rust_library` instead. ## rust_static_library+load("@rules_rust//rust:defs.bzl", "rust_static_library") + rust_static_library(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features, crate_name, crate_root, edition, platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp, version) @@ -498,6 +442,8 @@ When building the whole binary in Bazel, use `rust_library` instead. ## rust_test+load("@rules_rust//rust:defs.bzl", "rust_test") + rust_test(name, deps, srcs, data, aliases, alwayslink, compile_data, crate, crate_features, crate_name, crate_root, edition, env, env_inherit, experimental_use_cc_common_link, malloc, platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp, @@ -657,6 +603,8 @@ Run the test with `bazel test //hello_lib:greeting_test`. ## rust_test_suite+load("@rules_rust//rust:defs.bzl", "rust_test_suite") + rust_test_suite(name, srcs, shared_srcs, kwargs)diff --git a/docs/src/rust_analyzer.md b/docs/src/rust_analyzer.md index 59fc4bb655..4132986f5a 100644 --- a/docs/src/rust_analyzer.md +++ b/docs/src/rust_analyzer.md @@ -1,9 +1,3 @@ - -# Rust Analyzer - -* [rust_analyzer_aspect](#rust_analyzer_aspect) -* [rust_analyzer_toolchain](#rust_analyzer_toolchain) - ## Overview @@ -92,25 +86,10 @@ Then you can use a prototype [rust-analyzer plugin](https://marketplace.visualst - - -## rust_analyzer_toolchain - --rust_analyzer_toolchain(name, proc_macro_srv, rustc, rustc_srcs) -- -A toolchain for [rust-analyzer](https://rust-analyzer.github.io/). - -**ATTRIBUTES** +## Aspects -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| proc_macro_srv | The path to a `rust_analyzer_proc_macro_srv` binary. | Label | optional | `None` | -| rustc | The path to a `rustc` binary. | Label | required | | -| rustc_srcs | The source code of rustc. | Label | required | | +- [rust_analyzer_aspect](#rust_analyzer_aspect) @@ -118,6 +97,8 @@ A toolchain for [rust-analyzer](https://rust-analyzer.github.io/). ## rust_analyzer_aspect+load("@rules_rust//rust:defs.bzl", "rust_analyzer_aspect") + rust_analyzer_aspect(name)diff --git a/docs/src/rust_bindgen.md b/docs/src/rust_bindgen.md index f7bd8a6f45..a89c0e56c6 100644 --- a/docs/src/rust_bindgen.md +++ b/docs/src/rust_bindgen.md @@ -46,6 +46,8 @@ toolchains following the instructions for [rust_bindgen_toolchain](#rust_bindgen ## rust_bindgen+load("@rules_rust_bindgen//:defs.bzl", "rust_bindgen") + rust_bindgen(name, bindgen_flags, cc_lib, clang_flags, header, merge_cc_lib_objects_into_rlib, wrap_static_fns)@@ -71,6 +73,8 @@ Generates a rust source file from a cc_library and a header. ## rust_bindgen_toolchain+load("@rules_rust_bindgen//:defs.bzl", "rust_bindgen_toolchain") + rust_bindgen_toolchain(name, bindgen, clang, default_rustfmt, libclang, libstdcxx)@@ -119,6 +123,8 @@ For additional information, see the [Bazel toolchains documentation](https://doc ## rust_bindgen_library+load("@rules_rust_bindgen//:defs.bzl", "rust_bindgen_library") + rust_bindgen_library(name, header, cc_lib, bindgen_flags, bindgen_features, clang_flags, wrap_static_fns, kwargs)diff --git a/docs/src/rust_clippy.md b/docs/src/rust_clippy.md index 0e82494310..78cd9b5518 100644 --- a/docs/src/rust_clippy.md +++ b/docs/src/rust_clippy.md @@ -1,9 +1,3 @@ - -# Rust Clippy - -* [rust_clippy](#rust_clippy) -* [rust_clippy_aspect](#rust_clippy_aspect) - ## Overview @@ -37,11 +31,23 @@ the upstream implementation of clippy, this file must be named either `.clippy.t build --@rules_rust//rust/settings:clippy.toml=//:clippy.toml ``` + +## Rules + +- [rust_clippy](#rust_clippy) + +## Aspects + +- [rust_clippy_aspect](#rust_clippy_aspect) + + ## rust_clippy+load("@rules_rust//rust:defs.bzl", "rust_clippy") + rust_clippy(name, deps)@@ -95,6 +101,8 @@ rust_clippy( ## rust_clippy_aspect+load("@rules_rust//rust:defs.bzl", "rust_clippy_aspect") + rust_clippy_aspect(name)diff --git a/docs/src/rust_doc.md b/docs/src/rust_doc.md index b16f50d380..24c02a5d50 100644 --- a/docs/src/rust_doc.md +++ b/docs/src/rust_doc.md @@ -1,14 +1,21 @@ -# Rust Doc -* [rust_doc](#rust_doc) -* [rust_doc_test](#rust_doc_test) +Public entry point to all Rust rules and supported APIs. + + +## Rules + +- [rust_doc](#rust_doc) +- [rust_doc_test](#rust_doc_test) + ## rust_doc+load("@rules_rust//rust:defs.bzl", "rust_doc") + rust_doc(name, crate, html_after_content, html_before_content, html_in_header, markdown_css, rustc_flags, rustdoc_flags)@@ -69,6 +76,8 @@ Running `bazel build //hello_lib:hello_lib_doc` will build a zip file containing ## rust_doc_test+load("@rules_rust//rust:defs.bzl", "rust_doc_test") + rust_doc_test(name, deps, crate, proc_macro_deps)diff --git a/docs/src/rust_fmt.md b/docs/src/rust_fmt.md index 359d89be3b..263a36aeaf 100644 --- a/docs/src/rust_fmt.md +++ b/docs/src/rust_fmt.md @@ -1,10 +1,3 @@ - -# Rust Fmt - -* [rustfmt_aspect](#rustfmt_aspect) -* [rustfmt_test](#rustfmt_test) -* [rustfmt_toolchain](#rustfmt_toolchain) - ## Overview @@ -59,44 +52,35 @@ without failing. [rfcp]: https://github.com/rust-lang-nursery/fmt-rfcs [rgp]: https://rust-lang.github.io/rustfmt/ - -## rustfmt_test +## Rules --rustfmt_test(name, targets) -+- [rustfmt_test](#rustfmt_test) -A test rule for performing `rustfmt --check` on a set of targets +## Aspects -**ATTRIBUTES** +- [rustfmt_aspect](#rustfmt_aspect) -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| targets | Rust targets to run `rustfmt --check` on. | List of labels | optional | `[]` | - - - + -## rustfmt_toolchain +## rustfmt_test-rustfmt_toolchain(name, rustc, rustc_lib, rustfmt) +load("@rules_rust//rust:defs.bzl", "rustfmt_test") + +rustfmt_test(name, targets)-A toolchain for [rustfmt](https://rust-lang.github.io/rustfmt/) +A test rule for performing `rustfmt --check` on a set of targets **ATTRIBUTES** | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| rustc | The location of the `rustc` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | -| rustc_lib | The libraries used by rustc during compilation. | Label | optional | `None` | -| rustfmt | The location of the `rustfmt` binary. Can be a direct source or a filegroup containing one item. | Label | required | | +| name | A unique name for this target. | Name | required | | +| targets | Rust targets to run `rustfmt --check` on. | List of labels | optional | `[]` | @@ -104,6 +88,8 @@ A toolchain for [rustfmt](https://rust-lang.github.io/rustfmt/) ## rustfmt_aspect+load("@rules_rust//rust:defs.bzl", "rustfmt_aspect") + rustfmt_aspect(name)diff --git a/docs/src/rust_mdbook.md b/docs/src/rust_mdbook.md index 86efd45bb5..2b8057edda 100644 --- a/docs/src/rust_mdbook.md +++ b/docs/src/rust_mdbook.md @@ -40,6 +40,8 @@ rules_mdbook_transitive_deps() ## mdbook+load("@rules_rust_mdbook//:defs.bzl", "mdbook") + mdbook(name, srcs, book, plugins)@@ -61,6 +63,8 @@ Rules to create book from markdown files using `mdBook`. ## mdbook_server+load("@rules_rust_mdbook//:defs.bzl", "mdbook_server") + mdbook_server(name, book, hostname, port)@@ -82,6 +86,8 @@ Spawn an mdbook server for a given `mdbook` target. ## mdbook_toolchain+load("@rules_rust_mdbook//:defs.bzl", "mdbook_toolchain") + mdbook_toolchain(name, mdbook, plugins)diff --git a/docs/src/rust_prost.md b/docs/src/rust_prost.md index b6b816bc61..542bfe650c 100644 --- a/docs/src/rust_prost.md +++ b/docs/src/rust_prost.md @@ -147,6 +147,8 @@ register_toolchains("//toolchains:prost_toolchain") ## rust_prost_library+load("@rules_rust_prost//:defs.bzl", "rust_prost_library") + rust_prost_library(name, proto)@@ -166,6 +168,8 @@ A rule for generating a Rust library using Prost. ## rust_prost_toolchain+load("@rules_rust_prost//:defs.bzl", "rust_prost_toolchain") + rust_prost_toolchain(name, include_transitive_deps, prost_opts, prost_plugin, prost_plugin_flag, prost_runtime, prost_types, proto_compiler, tonic_opts, tonic_plugin, tonic_plugin_flag, tonic_runtime) @@ -197,6 +201,8 @@ Rust Prost toolchain rule. ## rust_prost_transform+load("@rules_rust_prost//:defs.bzl", "rust_prost_transform") + rust_prost_transform(name, deps, srcs, prost_opts, tonic_opts)diff --git a/docs/src/rust_protobuf.md b/docs/src/rust_protobuf.md index 035d5c8534..f3da7bd1d1 100644 --- a/docs/src/rust_protobuf.md +++ b/docs/src/rust_protobuf.md @@ -116,6 +116,8 @@ configuration. ## rust_grpc_library+load("@rules_rust_protobuf//:defs.bzl", "rust_grpc_library") + rust_grpc_library(name, deps, crate_name, rust_deps, rustc_flags)@@ -160,6 +162,8 @@ rust_binary( ## rust_proto_library+load("@rules_rust_protobuf//:defs.bzl", "rust_proto_library") + rust_proto_library(name, deps, crate_name, rust_deps, rustc_flags)diff --git a/docs/src/rust_repositories.md b/docs/src/rust_repositories.md index fa0d0318d4..294d40ce95 100644 --- a/docs/src/rust_repositories.md +++ b/docs/src/rust_repositories.md @@ -1,129 +1,23 @@ -# Rust Repositories - -* [rules_rust_dependencies](#rules_rust_dependencies) -* [rust_analyzer_toolchain_repository](#rust_analyzer_toolchain_repository) -* [rust_register_toolchains](#rust_register_toolchains) -* [rust_repositories](#rust_repositories) -* [rust_repository_set](#rust_repository_set) -* [rust_stdlib_filegroup](#rust_stdlib_filegroup) -* [rust_toolchain_repository_proxy](#rust_toolchain_repository_proxy) -* [rust_toolchain_repository](#rust_toolchain_repository) -* [rust_toolchain_tools_repository](#rust_toolchain_tools_repository) -* [rust_toolchain](#rust_toolchain) - - - -## rust_stdlib_filegroup - --rust_stdlib_filegroup(name, srcs) -- -A dedicated filegroup-like rule for Rust stdlib artifacts. - -**ATTRIBUTES** +# Rust Repositories -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| srcs | The list of targets/files that are components of the rust-stdlib file group | List of labels | required | | - - - - -## rust_toolchain - --rust_toolchain(name, allocator_library, binary_ext, cargo, cargo_clippy, clippy_driver, debug_info, - default_edition, dylib_ext, env, exec_triple, experimental_link_std_dylib, - experimental_use_cc_common_link, extra_exec_rustc_flags, extra_rustc_flags, - extra_rustc_flags_for_crate_types, global_allocator_library, llvm_cov, llvm_profdata, - llvm_tools, opt_level, per_crate_rustc_flags, rust_doc, rust_std, rustc, rustc_lib, - rustfmt, staticlib_ext, stdlib_linkflags, strip_level, target_json, target_triple) -- -Declares a Rust toolchain for use. - -This is for declaring a custom toolchain, eg. for configuring a particular version of rust or supporting a new platform. - -Example: +Repository rules for defining Rust dependencies and toolchains. -Suppose the core rust team has ported the compiler to a new target CPU, called `cpuX`. This support can be used in Bazel by defining a new toolchain definition and declaration: -```python -load('@rules_rust//rust:toolchain.bzl', 'rust_toolchain') - -rust_toolchain( - name = "rust_cpuX_impl", - binary_ext = "", - dylib_ext = ".so", - exec_triple = "cpuX-unknown-linux-gnu", - rust_doc = "@rust_cpuX//:rustdoc", - rust_std = "@rust_cpuX//:rust_std", - rustc = "@rust_cpuX//:rustc", - rustc_lib = "@rust_cpuX//:rustc_lib", - staticlib_ext = ".a", - stdlib_linkflags = ["-lpthread", "-ldl"], - target_triple = "cpuX-unknown-linux-gnu", -) - -toolchain( - name = "rust_cpuX", - exec_compatible_with = [ - "@platforms//cpu:cpuX", - "@platforms//os:linux", - ], - target_compatible_with = [ - "@platforms//cpu:cpuX", - "@platforms//os:linux", - ], - toolchain = ":rust_cpuX_impl", -) -``` +## Functions -Then, either add the label of the toolchain rule to `register_toolchains` in the WORKSPACE, or pass it to the `"--extra_toolchains"` flag for Bazel, and it will be used. +- [rules_rust_dependencies](#rules_rust_dependencies) +- [rust_analyzer_toolchain_repository](#rust_analyzer_toolchain_repository) +- [rust_register_toolchains](#rust_register_toolchains) +- [rust_repositories](#rust_repositories) +- [rust_repository_set](#rust_repository_set) +- [rust_toolchain_repository](#rust_toolchain_repository) -See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpuX` repository with the actual binaries and libraries. +## Repository Rules -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | Label | optional | `"@rules_rust//ffi/cc/allocator_library"` | -| binary_ext | The extension for binaries created from rustc. | String | required | | -| cargo | The location of the `cargo` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | -| cargo_clippy | The location of the `cargo_clippy` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | -| clippy_driver | The location of the `clippy-driver` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | -| debug_info | Rustc debug info levels per opt level | Dictionary: String -> String | optional | `{"dbg": "2", "fastbuild": "0", "opt": "0"}` | -| default_edition | The edition to use for rust_* rules that don't specify an edition. If absent, every rule is required to specify its `edition` attribute. | String | optional | `""` | -| dylib_ext | The extension for dynamic libraries created from rustc. | String | required | | -| env | Environment variables to set in actions. | Dictionary: String -> String | optional | `{}` | -| exec_triple | The platform triple for the toolchains execution environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations | String | required | | -| experimental_link_std_dylib | Label to a boolean build setting that controls whether whether to link libstd dynamically. | Label | optional | `"@rules_rust//rust/settings:experimental_link_std_dylib"` | -| experimental_use_cc_common_link | Label to a boolean build setting that controls whether cc_common.link is used to link rust binaries. | Label | optional | `"@rules_rust//rust/settings:experimental_use_cc_common_link"` | -| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | `[]` | -| extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration. Subject to location expansion with respect to the srcs of the `rust_std` attribute. | List of strings | optional | `[]` | -| extra_rustc_flags_for_crate_types | Extra flags to pass to rustc based on crate type | Dictionary: String -> List of strings | optional | `{}` | -| global_allocator_library | Target that provides allocator functions for when a global allocator is present. | Label | optional | `"@rules_rust//ffi/cc/global_allocator_library"` | -| llvm_cov | The location of the `llvm-cov` binary. Can be a direct source or a filegroup containing one item. If None, rust code is not instrumented for coverage. | Label | optional | `None` | -| llvm_profdata | The location of the `llvm-profdata` binary. Can be a direct source or a filegroup containing one item. If `llvm_cov` is None, this can be None as well and rust code is not instrumented for coverage. | Label | optional | `None` | -| llvm_tools | LLVM tools that are shipped with the Rust toolchain. | Label | optional | `None` | -| opt_level | Rustc optimization levels. | Dictionary: String -> String | optional | `{"dbg": "0", "fastbuild": "0", "opt": "3"}` | -| per_crate_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | `[]` | -| rust_doc | The location of the `rustdoc` binary. Can be a direct source or a filegroup containing one item. | Label | required | | -| rust_std | The Rust standard library. | Label | required | | -| rustc | The location of the `rustc` binary. Can be a direct source or a filegroup containing one item. | Label | required | | -| rustc_lib | The libraries used by rustc during compilation. | Label | optional | `None` | -| rustfmt | **Deprecated**: Instead see [rustfmt_toolchain](#rustfmt_toolchain) | Label | optional | `None` | -| staticlib_ext | The extension for static libraries created from rustc. | String | required | | -| stdlib_linkflags | Additional linker flags to use when Rust standard library is linked by a C++ linker (rustc will deal with these automatically). Subject to location expansion with respect to the srcs of the `rust_std` attribute. | List of strings | required | | -| strip_level | Rustc strip levels. For all potential options, see https://doc.rust-lang.org/rustc/codegen-options/index.html#strip | Dictionary: String -> String | optional | `{"dbg": "none", "fastbuild": "none", "opt": "debuginfo"}` | -| target_json | Override the target_triple with a custom target specification. For more details see: https://doc.rust-lang.org/rustc/targets/custom.html | String | optional | `""` | -| target_triple | The platform triple for the toolchains target environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations | String | optional | `""` | +- [rust_toolchain_repository_proxy](#rust_toolchain_repository_proxy) +- [rust_toolchain_tools_repository](#rust_toolchain_tools_repository) @@ -131,6 +25,8 @@ See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpu ## rules_rust_dependencies+load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies") + rules_rust_dependencies()@@ -143,6 +39,8 @@ Dependencies used in the implementation of `rules_rust`. ## rust_analyzer_toolchain_repository+load("@rules_rust//rust:repositories.bzl", "rust_analyzer_toolchain_repository") + rust_analyzer_toolchain_repository(name, version, exec_compatible_with, target_compatible_with, sha256s, urls, auth, netrc, auth_patterns)@@ -174,6 +72,8 @@ str: The name of a registerable rust_analyzer_toolchain. ## rust_register_toolchains+load("@rules_rust//rust:repositories.bzl", "rust_register_toolchains") + rust_register_toolchains(dev_components, edition, allocator_library, global_allocator_library, register_toolchains, rustfmt_version, rust_analyzer_version, sha256s, extra_target_triples, extra_rustc_flags, extra_exec_rustc_flags, urls, @@ -229,6 +129,8 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai ## rust_repositories+load("@rules_rust//rust:repositories.bzl", "rust_repositories") + rust_repositories(kwargs)@@ -247,6 +149,8 @@ rust_repositories(kwargs) ## rust_repository_set+load("@rules_rust//rust:repositories.bzl", "rust_repository_set") + rust_repository_set(name, versions, exec_triple, target_settings, allocator_library, global_allocator_library, extra_target_triples, rustfmt_version, edition, dev_components, extra_rustc_flags, extra_exec_rustc_flags, opt_level, sha256s, @@ -295,6 +199,8 @@ dict[str, dict]: A dict of informations about all generated toolchains. ## rust_toolchain_repository+load("@rules_rust//rust:repositories.bzl", "rust_toolchain_repository") + rust_toolchain_repository(name, version, exec_triple, target_triple, exec_compatible_with, target_compatible_with, target_settings, channel, allocator_library, global_allocator_library, rustfmt_version, edition, dev_components, @@ -341,6 +247,8 @@ dict[str, str]: Information about the registerable toolchain created by this rul ## rust_toolchain_repository_proxy+load("@rules_rust//rust:repositories.bzl", "rust_toolchain_repository_proxy") + rust_toolchain_repository_proxy(name, exec_compatible_with, repo_mapping, target_compatible_with, target_settings, toolchain, toolchain_type)@@ -366,6 +274,8 @@ Generates a toolchain-bearing repository that declares the toolchains from some ## rust_toolchain_tools_repository+load("@rules_rust//rust:repositories.bzl", "rust_toolchain_tools_repository") + rust_toolchain_tools_repository(name, allocator_library, auth, auth_patterns, dev_components, edition, exec_triple, extra_exec_rustc_flags, extra_rustc_flags, global_allocator_library, netrc, opt_level, repo_mapping, diff --git a/docs/src/rust_settings.md b/docs/src/rust_settings.md new file mode 100644 index 0000000000..941a819ac1 --- /dev/null +++ b/docs/src/rust_settings.md @@ -0,0 +1,368 @@ + + +# Rust settings + +Definitions for all `@rules_rust//rust` settings + + + +## capture_clippy_output + ++--@rules_rust//rust/settings:capture_clippy_output ++ +Control whether to print clippy output or store it to a file, using the configured error_format. + + + + + +## clippy_flag + ++--@rules_rust//rust/settings:clippy_flag ++ +Add a custom clippy flag from the command line with `--@rules_rust//rust/settings:clippy_flag`. + +Multiple uses are accumulated and appended after the `extra_rustc_flags`. + + + + + +## clippy_flags + ++--@rules_rust//rust/settings:clippy_flags ++ +This setting may be used to pass extra options to clippy from the command line. + +It applies across all targets. + + + + + +## clippy_toml + ++--@rules_rust//rust/settings:clippy_toml ++ +This setting is used by the clippy rules. See https://bazelbuild.github.io/rules_rust/rust_clippy.html + +Note that this setting is actually called `clippy.toml`. + + + + + +## error_format + ++--@rules_rust//rust/settings:error_format ++ +This setting may be changed from the command line to generate machine readable errors. + + + + + +## experimental_link_std_dylib + ++--@rules_rust//rust/settings:experimental_link_std_dylib ++ +A flag to control whether to link libstd dynamically. + + + + + +## experimental_per_crate_rustc_flag + ++--@rules_rust//rust/settings:experimental_per_crate_rustc_flag ++ +Add additional rustc_flag to matching crates from the command line with `--@rules_rust//rust/settings:experimental_per_crate_rustc_flag`. + +The expected flag format is prefix_filter@flag, where any crate with a label or execution path starting +with the prefix filter will be built with the given flag. The label matching uses the canonical form of +the label (i.e `//package:label_name`). The execution path is the relative path to your workspace directory +including the base name (including extension) of the crate root. This flag is only applied to the exec +configuration (proc-macros, cargo_build_script, etc). Multiple uses are accumulated. + + + + + +## experimental_use_cc_common_link + ++--@rules_rust//rust/settings:experimental_use_cc_common_link ++ +A flag to control whether to link rust_binary and rust_test targets using cc_common.link instead of rustc. + + + + + +## experimental_use_coverage_metadata_files + ++--@rules_rust//rust/settings:experimental_use_coverage_metadata_files ++ +A flag to have coverage tooling added as `coverage_common.instrumented_files_info.metadata_files` instead of reporting tools like `llvm-cov` and `llvm-profdata` as runfiles to each test. + + + + + +## experimental_use_global_allocator + ++--@rules_rust//rust/settings:experimental_use_global_allocator ++ +A flag to indicate that a global allocator is in use when using `--@rules_rust//rust/settings:experimental_use_cc_common_link` + +Users need to specify this flag because rustc generates different set of symbols at link time when a global allocator is in use. +When the linking is not done by rustc, the `rust_toolchain` itself provides the appropriate set of symbols. + + + + + +## experimental_use_sh_toolchain_for_bootstrap_process_wrapper + ++--@rules_rust//rust/settings:experimental_use_sh_toolchain_for_bootstrap_process_wrapper ++ +A flag to control whether the shell path from a shell toolchain (`@bazel_tools//tools/sh:toolchain_type`) is embedded into the bootstrap process wrapper for the `.sh` file. + + + + + +## extra_exec_rustc_flag + ++--@rules_rust//rust/settings:extra_exec_rustc_flag ++ +Add additional rustc_flags in the exec configuration from the command line with `--@rules_rust//rust/settings:extra_exec_rustc_flag`. + +Multiple uses are accumulated and appended after the extra_exec_rustc_flags. + + + + + +## extra_exec_rustc_flags + ++--@rules_rust//rust/settings:extra_exec_rustc_flags ++ +This setting may be used to pass extra options to rustc from the command line in exec configuration. + +It applies across all targets whereas the rustc_flags option on targets applies only +to that target. This can be useful for passing build-wide options such as LTO. + + + + + +## extra_rustc_flag + ++--@rules_rust//rust/settings:extra_rustc_flag ++ +Add additional rustc_flag from the command line with `--@rules_rust//rust/settings:extra_rustc_flag`. + +Multiple uses are accumulated and appended after the `extra_rustc_flags`. + + + + + +## extra_rustc_flags + ++--@rules_rust//rust/settings:extra_rustc_flags ++ +This setting may be used to pass extra options to rustc from the command line in non-exec configuration. + +It applies across all targets whereas the rustc_flags option on targets applies only +to that target. This can be useful for passing build-wide options such as LTO. + + + + + +## incompatible_change_rust_test_compilation_output_directory + ++--@rules_rust//rust/settings:incompatible_change_rust_test_compilation_output_directory ++ +A flag to put rust_test compilation outputs in the same directory as the rust_library compilation outputs. + + + + + +## incompatible_do_not_include_data_in_compile_data + ++--@rules_rust//rust/settings:incompatible_do_not_include_data_in_compile_data ++ +A flag to control whether to include data files in compile_data. + + + + + +## lto + ++--@rules_rust//rust/settings:lto ++ +A build setting which specifies the link time optimization mode used when building Rust code. + + + + + +## no_std + ++--@rules_rust//rust/settings:no_std ++ +This setting may be used to enable builds without the standard library. + +Currently only no_std + alloc is supported, which can be enabled with setting the value to "alloc". +In the future we could add support for additional modes, e.g "core", "alloc,collections". + + + + + +## pipelined_compilation + ++--@rules_rust//rust/settings:pipelined_compilation ++ +When set, this flag causes rustc to emit `*.rmeta` files and use them for `rlib -> rlib` dependencies. + +While this involves one extra (short) rustc invocation to build the rmeta file, +it allows library dependencies to be unlocked much sooner, increasing parallelism during compilation. + + + + + +## rename_first_party_crates + ++--@rules_rust//rust/settings:rename_first_party_crates ++ +A flag controlling whether to rename first-party crates such that their names encode the Bazel package and target name, instead of just the target name. + +First-party vs. third-party crates are identified using the value of +`@rules_rust//settings:third_party_dir`. + + + + + +## rustc_output_diagnostics + ++--@rules_rust//rust/settings:rustc_output_diagnostics ++ +This setting may be changed from the command line to generate rustc diagnostics. + + + + + +## rustfmt_toml + ++--@rules_rust//rust/settings:rustfmt_toml ++ +This setting is used by the rustfmt rules. See https://bazelbuild.github.io/rules_rust/rust_fmt.html + +Note that this setting is actually called `rustfmt.toml`. + + + + + +## third_party_dir + ++--@rules_rust//rust/settings:third_party_dir ++ +A flag specifying the location of vendored third-party rust crates within this repository that must not be renamed when `rename_first_party_crates` is enabled. + +Must be specified as a Bazel package, e.g. "//some/location/in/repo". + + + + + +## toolchain_generated_sysroot + ++--@rules_rust//rust/settings:toolchain_generated_sysroot ++ +A flag to set rustc --sysroot flag to the sysroot generated by rust_toolchain. + + + + + +## unpretty + ++--@rules_rust//rust/settings:unpretty ++ + + + + + + +## use_real_import_macro + ++--@rules_rust//rust/settings:use_real_import_macro ++ +A flag to control whether rust_library and rust_binary targets should implicitly depend on the *real* import macro, or on a no-op target. + + + diff --git a/docs/src/rust_toolchain.md b/docs/src/rust_toolchain.md new file mode 100644 index 0000000000..dfdd681d61 --- /dev/null +++ b/docs/src/rust_toolchain.md @@ -0,0 +1,107 @@ + + +The rust_toolchain rule definition and implementation. + + +## Rules + +- [rust_toolchain](#rust_toolchain) + + + + +## rust_toolchain + ++load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") + +rust_toolchain(name, allocator_library, binary_ext, cargo, cargo_clippy, clippy_driver, debug_info, + default_edition, dylib_ext, env, exec_triple, experimental_link_std_dylib, + experimental_use_cc_common_link, extra_exec_rustc_flags, extra_rustc_flags, + extra_rustc_flags_for_crate_types, global_allocator_library, llvm_cov, llvm_profdata, + llvm_tools, opt_level, per_crate_rustc_flags, rust_doc, rust_std, rustc, rustc_lib, + rustfmt, staticlib_ext, stdlib_linkflags, strip_level, target_json, target_triple) ++ +Declares a Rust toolchain for use. + +This is for declaring a custom toolchain, eg. for configuring a particular version of rust or supporting a new platform. + +Example: + +Suppose the core rust team has ported the compiler to a new target CPU, called `cpuX`. This support can be used in Bazel by defining a new toolchain definition and declaration: + +```python +load('@rules_rust//rust:toolchain.bzl', 'rust_toolchain') + +rust_toolchain( + name = "rust_cpuX_impl", + binary_ext = "", + dylib_ext = ".so", + exec_triple = "cpuX-unknown-linux-gnu", + rust_doc = "@rust_cpuX//:rustdoc", + rust_std = "@rust_cpuX//:rust_std", + rustc = "@rust_cpuX//:rustc", + rustc_lib = "@rust_cpuX//:rustc_lib", + staticlib_ext = ".a", + stdlib_linkflags = ["-lpthread", "-ldl"], + target_triple = "cpuX-unknown-linux-gnu", +) + +toolchain( + name = "rust_cpuX", + exec_compatible_with = [ + "@platforms//cpu:cpuX", + "@platforms//os:linux", + ], + target_compatible_with = [ + "@platforms//cpu:cpuX", + "@platforms//os:linux", + ], + toolchain = ":rust_cpuX_impl", +) +``` + +Then, either add the label of the toolchain rule to `register_toolchains` in the WORKSPACE, or pass it to the `"--extra_toolchains"` flag for Bazel, and it will be used. + +See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpuX` repository with the actual binaries and libraries. + +**ATTRIBUTES** + + +| Name | Description | Type | Mandatory | Default | +| :------------- | :------------- | :------------- | :------------- | :------------- | +| name | A unique name for this target. | Name | required | | +| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | Label | optional | `"@rules_rust//ffi/cc/allocator_library"` | +| binary_ext | The extension for binaries created from rustc. | String | required | | +| cargo | The location of the `cargo` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | +| cargo_clippy | The location of the `cargo_clippy` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | +| clippy_driver | The location of the `clippy-driver` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | +| debug_info | Rustc debug info levels per opt level | Dictionary: String -> String | optional | `{"dbg": "2", "fastbuild": "0", "opt": "0"}` | +| default_edition | The edition to use for rust_* rules that don't specify an edition. If absent, every rule is required to specify its `edition` attribute. | String | optional | `""` | +| dylib_ext | The extension for dynamic libraries created from rustc. | String | required | | +| env | Environment variables to set in actions. | Dictionary: String -> String | optional | `{}` | +| exec_triple | The platform triple for the toolchains execution environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations | String | required | | +| experimental_link_std_dylib | Label to a boolean build setting that controls whether whether to link libstd dynamically. | Label | optional | `"@rules_rust//rust/settings:experimental_link_std_dylib"` | +| experimental_use_cc_common_link | Label to a boolean build setting that controls whether cc_common.link is used to link rust binaries. | Label | optional | `"@rules_rust//rust/settings:experimental_use_cc_common_link"` | +| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | `[]` | +| extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration. Subject to location expansion with respect to the srcs of the `rust_std` attribute. | List of strings | optional | `[]` | +| extra_rustc_flags_for_crate_types | Extra flags to pass to rustc based on crate type | Dictionary: String -> List of strings | optional | `{}` | +| global_allocator_library | Target that provides allocator functions for when a global allocator is present. | Label | optional | `"@rules_rust//ffi/cc/global_allocator_library"` | +| llvm_cov | The location of the `llvm-cov` binary. Can be a direct source or a filegroup containing one item. If None, rust code is not instrumented for coverage. | Label | optional | `None` | +| llvm_profdata | The location of the `llvm-profdata` binary. Can be a direct source or a filegroup containing one item. If `llvm_cov` is None, this can be None as well and rust code is not instrumented for coverage. | Label | optional | `None` | +| llvm_tools | LLVM tools that are shipped with the Rust toolchain. | Label | optional | `None` | +| opt_level | Rustc optimization levels. | Dictionary: String -> String | optional | `{"dbg": "0", "fastbuild": "0", "opt": "3"}` | +| per_crate_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | `[]` | +| rust_doc | The location of the `rustdoc` binary. Can be a direct source or a filegroup containing one item. | Label | required | | +| rust_std | The Rust standard library. | Label | required | | +| rustc | The location of the `rustc` binary. Can be a direct source or a filegroup containing one item. | Label | required | | +| rustc_lib | The libraries used by rustc during compilation. | Label | optional | `None` | +| rustfmt | **Deprecated**: Instead see [rustfmt_toolchain](#rustfmt_toolchain) | Label | optional | `None` | +| staticlib_ext | The extension for static libraries created from rustc. | String | required | | +| stdlib_linkflags | Additional linker flags to use when Rust standard library is linked by a C++ linker (rustc will deal with these automatically). Subject to location expansion with respect to the srcs of the `rust_std` attribute. | List of strings | required | | +| strip_level | Rustc strip levels. For all potential options, see https://doc.rust-lang.org/rustc/codegen-options/index.html#strip | Dictionary: String -> String | optional | `{"dbg": "none", "fastbuild": "none", "opt": "debuginfo"}` | +| target_json | Override the target_triple with a custom target specification. For more details see: https://doc.rust-lang.org/rustc/targets/custom.html | String | optional | `""` | +| target_triple | The platform triple for the toolchains target environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations | String | optional | `""` | + + diff --git a/docs/src/rust_toolchains.md b/docs/src/rust_toolchains.md new file mode 100644 index 0000000000..368541ba73 --- /dev/null +++ b/docs/src/rust_toolchains.md @@ -0,0 +1,157 @@ + + +# Rust Toolchains + +Toolchain rules for Rust. + + +## Rules + +- [rust_analyzer_toolchain](#rust_analyzer_toolchain) +- [rust_toolchain](#rust_toolchain) +- [rustfmt_toolchain](#rustfmt_toolchain) + + + + +## rust_analyzer_toolchain + ++load("@rules_rust//rust:toolchain.bzl", "rust_analyzer_toolchain") + +rust_analyzer_toolchain(name, proc_macro_srv, rustc, rustc_srcs) ++ +A toolchain for [rust-analyzer](https://rust-analyzer.github.io/). + +**ATTRIBUTES** + + +| Name | Description | Type | Mandatory | Default | +| :------------- | :------------- | :------------- | :------------- | :------------- | +| name | A unique name for this target. | Name | required | | +| proc_macro_srv | The path to a `rust_analyzer_proc_macro_srv` binary. | Label | optional | `None` | +| rustc | The path to a `rustc` binary. | Label | required | | +| rustc_srcs | The source code of rustc. | Label | required | | + + + + +## rust_toolchain + ++load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") + +rust_toolchain(name, allocator_library, binary_ext, cargo, cargo_clippy, clippy_driver, debug_info, + default_edition, dylib_ext, env, exec_triple, experimental_link_std_dylib, + experimental_use_cc_common_link, extra_exec_rustc_flags, extra_rustc_flags, + extra_rustc_flags_for_crate_types, global_allocator_library, llvm_cov, llvm_profdata, + llvm_tools, opt_level, per_crate_rustc_flags, rust_doc, rust_std, rustc, rustc_lib, + rustfmt, staticlib_ext, stdlib_linkflags, strip_level, target_json, target_triple) ++ +Declares a Rust toolchain for use. + +This is for declaring a custom toolchain, eg. for configuring a particular version of rust or supporting a new platform. + +Example: + +Suppose the core rust team has ported the compiler to a new target CPU, called `cpuX`. This support can be used in Bazel by defining a new toolchain definition and declaration: + +```python +load('@rules_rust//rust:toolchain.bzl', 'rust_toolchain') + +rust_toolchain( + name = "rust_cpuX_impl", + binary_ext = "", + dylib_ext = ".so", + exec_triple = "cpuX-unknown-linux-gnu", + rust_doc = "@rust_cpuX//:rustdoc", + rust_std = "@rust_cpuX//:rust_std", + rustc = "@rust_cpuX//:rustc", + rustc_lib = "@rust_cpuX//:rustc_lib", + staticlib_ext = ".a", + stdlib_linkflags = ["-lpthread", "-ldl"], + target_triple = "cpuX-unknown-linux-gnu", +) + +toolchain( + name = "rust_cpuX", + exec_compatible_with = [ + "@platforms//cpu:cpuX", + "@platforms//os:linux", + ], + target_compatible_with = [ + "@platforms//cpu:cpuX", + "@platforms//os:linux", + ], + toolchain = ":rust_cpuX_impl", +) +``` + +Then, either add the label of the toolchain rule to `register_toolchains` in the WORKSPACE, or pass it to the `"--extra_toolchains"` flag for Bazel, and it will be used. + +See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpuX` repository with the actual binaries and libraries. + +**ATTRIBUTES** + + +| Name | Description | Type | Mandatory | Default | +| :------------- | :------------- | :------------- | :------------- | :------------- | +| name | A unique name for this target. | Name | required | | +| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | Label | optional | `"@rules_rust//ffi/cc/allocator_library"` | +| binary_ext | The extension for binaries created from rustc. | String | required | | +| cargo | The location of the `cargo` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | +| cargo_clippy | The location of the `cargo_clippy` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | +| clippy_driver | The location of the `clippy-driver` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | +| debug_info | Rustc debug info levels per opt level | Dictionary: String -> String | optional | `{"dbg": "2", "fastbuild": "0", "opt": "0"}` | +| default_edition | The edition to use for rust_* rules that don't specify an edition. If absent, every rule is required to specify its `edition` attribute. | String | optional | `""` | +| dylib_ext | The extension for dynamic libraries created from rustc. | String | required | | +| env | Environment variables to set in actions. | Dictionary: String -> String | optional | `{}` | +| exec_triple | The platform triple for the toolchains execution environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations | String | required | | +| experimental_link_std_dylib | Label to a boolean build setting that controls whether whether to link libstd dynamically. | Label | optional | `"@rules_rust//rust/settings:experimental_link_std_dylib"` | +| experimental_use_cc_common_link | Label to a boolean build setting that controls whether cc_common.link is used to link rust binaries. | Label | optional | `"@rules_rust//rust/settings:experimental_use_cc_common_link"` | +| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | `[]` | +| extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration. Subject to location expansion with respect to the srcs of the `rust_std` attribute. | List of strings | optional | `[]` | +| extra_rustc_flags_for_crate_types | Extra flags to pass to rustc based on crate type | Dictionary: String -> List of strings | optional | `{}` | +| global_allocator_library | Target that provides allocator functions for when a global allocator is present. | Label | optional | `"@rules_rust//ffi/cc/global_allocator_library"` | +| llvm_cov | The location of the `llvm-cov` binary. Can be a direct source or a filegroup containing one item. If None, rust code is not instrumented for coverage. | Label | optional | `None` | +| llvm_profdata | The location of the `llvm-profdata` binary. Can be a direct source or a filegroup containing one item. If `llvm_cov` is None, this can be None as well and rust code is not instrumented for coverage. | Label | optional | `None` | +| llvm_tools | LLVM tools that are shipped with the Rust toolchain. | Label | optional | `None` | +| opt_level | Rustc optimization levels. | Dictionary: String -> String | optional | `{"dbg": "0", "fastbuild": "0", "opt": "3"}` | +| per_crate_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | `[]` | +| rust_doc | The location of the `rustdoc` binary. Can be a direct source or a filegroup containing one item. | Label | required | | +| rust_std | The Rust standard library. | Label | required | | +| rustc | The location of the `rustc` binary. Can be a direct source or a filegroup containing one item. | Label | required | | +| rustc_lib | The libraries used by rustc during compilation. | Label | optional | `None` | +| rustfmt | **Deprecated**: Instead see [rustfmt_toolchain](#rustfmt_toolchain) | Label | optional | `None` | +| staticlib_ext | The extension for static libraries created from rustc. | String | required | | +| stdlib_linkflags | Additional linker flags to use when Rust standard library is linked by a C++ linker (rustc will deal with these automatically). Subject to location expansion with respect to the srcs of the `rust_std` attribute. | List of strings | required | | +| strip_level | Rustc strip levels. For all potential options, see https://doc.rust-lang.org/rustc/codegen-options/index.html#strip | Dictionary: String -> String | optional | `{"dbg": "none", "fastbuild": "none", "opt": "debuginfo"}` | +| target_json | Override the target_triple with a custom target specification. For more details see: https://doc.rust-lang.org/rustc/targets/custom.html | String | optional | `""` | +| target_triple | The platform triple for the toolchains target environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations | String | optional | `""` | + + + + +## rustfmt_toolchain + ++load("@rules_rust//rust:toolchain.bzl", "rustfmt_toolchain") + +rustfmt_toolchain(name, rustc, rustc_lib, rustfmt) ++ +A toolchain for [rustfmt](https://rust-lang.github.io/rustfmt/) + +**ATTRIBUTES** + + +| Name | Description | Type | Mandatory | Default | +| :------------- | :------------- | :------------- | :------------- | :------------- | +| name | A unique name for this target. | Name | required | | +| rustc | The location of the `rustc` binary. Can be a direct source or a filegroup containing one item. | Label | optional | `None` | +| rustc_lib | The libraries used by rustc during compilation. | Label | optional | `None` | +| rustfmt | The location of the `rustfmt` binary. Can be a direct source or a filegroup containing one item. | Label | required | | + + diff --git a/docs/src/rust_wasm_bindgen.md b/docs/src/rust_wasm_bindgen.md index b1dda7509b..9d3941e16f 100644 --- a/docs/src/rust_wasm_bindgen.md +++ b/docs/src/rust_wasm_bindgen.md @@ -47,6 +47,8 @@ for the `rules_js` submodule). ## rust_wasm_bindgen+load("@rules_rust_wasm_bindgen//:defs.bzl", "rust_wasm_bindgen") + rust_wasm_bindgen(name, bindgen_flags, out_name, target, target_arch, wasm_file)@@ -74,6 +76,8 @@ An example of this rule in use can be seen at [@rules_rust//examples/wasm](../ex ## rust_wasm_bindgen_toolchain+load("@rules_rust_wasm_bindgen//:defs.bzl", "rust_wasm_bindgen_toolchain") + rust_wasm_bindgen_toolchain(name, bindgen)@@ -121,6 +125,8 @@ For additional information, see the [Bazel toolchains documentation][toolchains] ## RustWasmBindgenInfo+load("@rules_rust_wasm_bindgen//:defs.bzl", "RustWasmBindgenInfo") + RustWasmBindgenInfo(js, root, snippets, ts, wasm)@@ -128,7 +134,6 @@ Info about wasm-bindgen outputs. **FIELDS** - | Name | Description | | :------------- | :------------- | | js | Depset[File]: The Javascript files produced by `wasm-bindgen`. | diff --git a/docs/src/rust_wasm_bindgen_rules_js.md b/docs/src/rust_wasm_bindgen_rules_js.md index ad42ae8c7f..ffcd131632 100644 --- a/docs/src/rust_wasm_bindgen_rules_js.md +++ b/docs/src/rust_wasm_bindgen_rules_js.md @@ -7,6 +7,8 @@ Rust WASM-bindgen rules for interfacing with aspect-build/rules_js ## js_rust_wasm_bindgen+load("@rules_rust_wasm_bindgen//rules_js:defs.bzl", "js_rust_wasm_bindgen") + js_rust_wasm_bindgen(name, bindgen_flags, out_name, target, target_arch, wasm_file)diff --git a/docs/src/settings.md b/docs/src/settings.md index d872c661cf..97597c44df 100644 --- a/docs/src/settings.md +++ b/docs/src/settings.md @@ -1,25 +1,3 @@ - # Settings -* [incompatible_flag](#incompatible_flag) -* [fail_when_enabled](#fail_when_enabled) - - - -## incompatible_flag - --incompatible_flag(name, issue) -- -A rule defining an incompatible flag. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| issue | The link to the github issue associated with this flag | String | required | | - - +Flags which control granular behavior of various Rust rules. diff --git a/docs/stardoc-attr-default-wrappers.patch b/docs/stardoc-attr-default-wrappers.patch deleted file mode 100644 index 46ce163ba8..0000000000 --- a/docs/stardoc-attr-default-wrappers.patch +++ /dev/null @@ -1,23 +0,0 @@ -commit 677356a2a00a2cb3b10e458c8586a2ea8bd562a4 -Author: Daniel Wagner-Hall-Date: Wed Nov 30 13:00:47 2022 +0000 - - Use backtick not ` ` for attr default values - - GitHub Pages doesn't recognise
blocks, so renders these - attributes with smart-quotes, which means you can't copy-paste them into - a starlark file. - -diff --git stardoc/templates/markdown_tables/func.vm stardoc/templates/markdown_tables/func.vm -index 5275d08..e0d2496 100644 ---- stardoc/templates/markdown_tables/func.vm -+++ stardoc/templates/markdown_tables/func.vm -@@ -15,7 +15,7 @@ ${util.htmlEscape($funcInfo.docString)} - | Name | Description | Default Value | - | :------------- | :------------- | :------------- | - #foreach ($param in $funcInfo.getParameterList()) --| $param.name | #if(!$param.docString.isEmpty()) ${util.markdownCellFormat($param.docString)} #else
-
#end | #if(!$param.getDefaultValue().isEmpty())${util.htmlEscape($param.getDefaultValue())}
#else none #end| -+| $param.name | #if(!$param.docString.isEmpty()) ${util.markdownCellFormat($param.docString)} #else-
#end | #if(!$param.getDefaultValue().isEmpty()) `${util.htmlEscape($param.getDefaultValue())}` #else none #end| - #end - #end - #if (!$funcInfo.getReturn().docString.isEmpty()) diff --git a/docs/symbols.bzl b/docs/symbols.bzl deleted file mode 100644 index 80f9d77b6a..0000000000 --- a/docs/symbols.bzl +++ /dev/null @@ -1,130 +0,0 @@ -"""This module provides a single place for all aspects, rules, and macros that are meant -to have stardoc generated documentation. -""" - -load( - "@rules_rust//cargo:defs.bzl", - _cargo_bootstrap_repository = "cargo_bootstrap_repository", - _cargo_build_script = "cargo_build_script", - _cargo_dep_env = "cargo_dep_env", - _cargo_env = "cargo_env", -) -load( - "@rules_rust//crate_universe:docs_workspace.bzl", - _crate = "crate", - _crate_universe_dependencies = "crate_universe_dependencies", - _crates_repository = "crates_repository", - _crates_vendor = "crates_vendor", -) -load( - "@rules_rust//rust:defs.bzl", - _capture_clippy_output = "capture_clippy_output", - _error_format = "error_format", - _extra_rustc_flag = "extra_rustc_flag", - _extra_rustc_flags = "extra_rustc_flags", - _rust_analyzer_aspect = "rust_analyzer_aspect", - _rust_binary = "rust_binary", - _rust_clippy = "rust_clippy", - _rust_clippy_aspect = "rust_clippy_aspect", - _rust_doc = "rust_doc", - _rust_doc_test = "rust_doc_test", - _rust_library = "rust_library", - _rust_library_group = "rust_library_group", - _rust_proc_macro = "rust_proc_macro", - _rust_shared_library = "rust_shared_library", - _rust_static_library = "rust_static_library", - _rust_test = "rust_test", - _rust_test_suite = "rust_test_suite", - _rustfmt_aspect = "rustfmt_aspect", - _rustfmt_test = "rustfmt_test", -) -load( - "@rules_rust//rust:repositories.bzl", - _rules_rust_dependencies = "rules_rust_dependencies", - _rust_analyzer_toolchain_repository = "rust_analyzer_toolchain_repository", - _rust_analyzer_toolchain_tools_repository = "rust_analyzer_toolchain_tools_repository", - _rust_register_toolchains = "rust_register_toolchains", - _rust_repositories = "rust_repositories", - _rust_repository_set = "rust_repository_set", - _rust_toolchain_repository = "rust_toolchain_repository", - _rust_toolchain_repository_proxy = "rust_toolchain_repository_proxy", - _rust_toolchain_tools_repository = "rust_toolchain_tools_repository", - _rustfmt_toolchain_repository = "rustfmt_toolchain_repository", - _rustfmt_toolchain_tools_repository = "rustfmt_toolchain_tools_repository", - _toolchain_repository_proxy = "toolchain_repository_proxy", -) -load( - "@rules_rust//rust:toolchain.bzl", - _rust_analyzer_toolchain = "rust_analyzer_toolchain", - _rust_stdlib_filegroup = "rust_stdlib_filegroup", - _rust_toolchain = "rust_toolchain", - _rustfmt_toolchain = "rustfmt_toolchain", -) - -# buildifier: disable=bzl-visibility -load( - "@rules_rust//rust/private:providers.bzl", - _CrateInfo = "CrateInfo", - _DepInfo = "DepInfo", - _StdLibInfo = "StdLibInfo", -) -load( - "@rules_rust//rust/settings:incompatible.bzl", - _incompatible_flag = "incompatible_flag", -) - -rust_binary = _rust_binary -rust_library = _rust_library -rust_library_group = _rust_library_group -rust_static_library = _rust_static_library -rust_shared_library = _rust_shared_library -rust_proc_macro = _rust_proc_macro -rust_test = _rust_test -rust_test_suite = _rust_test_suite -rust_doc = _rust_doc -rust_doc_test = _rust_doc_test - -rust_toolchain = _rust_toolchain -rust_stdlib_filegroup = _rust_stdlib_filegroup - -cargo_bootstrap_repository = _cargo_bootstrap_repository -cargo_build_script = _cargo_build_script -cargo_dep_env = _cargo_dep_env -cargo_env = _cargo_env - -rules_rust_dependencies = _rules_rust_dependencies -rust_register_toolchains = _rust_register_toolchains -rust_repositories = _rust_repositories -rust_repository_set = _rust_repository_set -rust_toolchain_repository = _rust_toolchain_repository -rust_toolchain_repository_proxy = _rust_toolchain_repository_proxy -rust_toolchain_tools_repository = _rust_toolchain_tools_repository -rustfmt_toolchain_tools_repository = _rustfmt_toolchain_tools_repository -rustfmt_toolchain_repository = _rustfmt_toolchain_repository -rust_analyzer_toolchain_repository = _rust_analyzer_toolchain_repository -rust_analyzer_toolchain_tools_repository = _rust_analyzer_toolchain_tools_repository -toolchain_repository_proxy = _toolchain_repository_proxy - -rust_clippy = _rust_clippy -rust_clippy_aspect = _rust_clippy_aspect -rust_analyzer_aspect = _rust_analyzer_aspect -rust_analyzer_toolchain = _rust_analyzer_toolchain - -crate = _crate -crates_repository = _crates_repository -crates_vendor = _crates_vendor -crate_universe_dependencies = _crate_universe_dependencies - -rustfmt_aspect = _rustfmt_aspect -rustfmt_test = _rustfmt_test -rustfmt_toolchain = _rustfmt_toolchain - -error_format = _error_format -extra_rustc_flag = _extra_rustc_flag -extra_rustc_flags = _extra_rustc_flags -incompatible_flag = _incompatible_flag -capture_clippy_output = _capture_clippy_output - -CrateInfo = _CrateInfo -DepInfo = _DepInfo -StdLibInfo = _StdLibInfo diff --git a/rust/private/BUILD.bazel b/rust/private/BUILD.bazel index 1dd47569e4..d639fe9650 100644 --- a/rust/private/BUILD.bazel +++ b/rust/private/BUILD.bazel @@ -3,6 +3,9 @@ load("//rust/private:rust_analyzer.bzl", "rust_analyzer_detect_sysroot") load("//rust/private:rustc.bzl", "is_proc_macro_dep", "is_proc_macro_dep_enabled") load("//rust/private:stamp.bzl", "stamp_build_setting") +# Exported for docs +exports_files(["providers.bzl"]) + bzl_library( name = "bazel_tools_bzl_lib", srcs = ["@bazel_tools//tools:bzl_srcs"], diff --git a/rust/repositories.bzl b/rust/repositories.bzl index 06de237d54..5e70b9bd7c 100644 --- a/rust/repositories.bzl +++ b/rust/repositories.bzl @@ -1,4 +1,7 @@ -"""Repository rules for defining Rust dependencies and toolchains""" +"""# Rust Repositories + +Repository rules for defining Rust dependencies and toolchains. +""" load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") diff --git a/rust/settings/BUILD.bazel b/rust/settings/BUILD.bazel index e4166e9a0e..3e5975cf66 100644 --- a/rust/settings/BUILD.bazel +++ b/rust/settings/BUILD.bazel @@ -1,270 +1,102 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") -load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag") load( - "//rust:defs.bzl", + ":settings.bzl", "capture_clippy_output", "clippy_flag", "clippy_flags", + "clippy_toml", "error_format", + "experimental_link_std_dylib", + "experimental_per_crate_rustc_flag", + "experimental_use_cc_common_link", + "experimental_use_coverage_metadata_files", + "experimental_use_global_allocator", + "experimental_use_sh_toolchain_for_bootstrap_process_wrapper", "extra_exec_rustc_flag", "extra_exec_rustc_flags", "extra_rustc_flag", "extra_rustc_flags", + "incompatible_change_rust_test_compilation_output_directory", + "incompatible_do_not_include_data_in_compile_data", + "lto", "no_std", - "per_crate_rustc_flag", + "pipelined_compilation", + "rename_first_party_crates", "rustc_output_diagnostics", + "rustfmt_toml", + "third_party_dir", + "toolchain_generated_sysroot", + "unpretty", + "use_real_import_macro", ) -load("//rust/private:lto.bzl", "rust_lto_flag") -load("//rust/private:unpretty.bzl", "rust_unpretty_flag") -load(":incompatible.bzl", "incompatible_flag") + +package(default_visibility = ["//visibility:public"]) exports_files([ ".clippy.toml", ".rustfmt.toml", + "settings.bzl", ]) bzl_library( name = "bzl_lib", - srcs = glob(["**/*.bzl"]), - visibility = ["//visibility:public"], -) - -rust_unpretty_flag( - name = "unpretty", - build_setting_default = [ - "ast-tree,expanded", - "ast-tree", - "expanded,hygiene", - "expanded,identified", - "expanded", - "hir-tree", - "hir,identified", - "hir,typed", - "hir", - "identified", - "mir-cfg", - "mir", - "normal", + srcs = glob(["*.bzl"]), + visibility = ["//rust:__pkg__"], + deps = [ + "@bazel_skylib//rules:common_settings", ], - visibility = ["//visibility:public"], ) -rust_lto_flag( - name = "lto", - build_setting_default = "unspecified", - visibility = ["//visibility:public"], -) +capture_clippy_output() -# A flag controlling whether to rename first-party crates such that their names -# encode the Bazel package and target name, instead of just the target name. -# -# First-party vs. third-party crates are identified using the value of -# //settings:third_party_dir. -bool_flag( - name = "rename_first_party_crates", - build_setting_default = False, - visibility = ["//visibility:public"], -) +clippy_flag() -# A flag specifying the location of vendored third-party rust crates within this -# repository that must not be renamed when `rename_first_party_crates` is enabled. -# -# Must be specified as a Bazel package, e.g. "//some/location/in/repo". -string_flag( - name = "third_party_dir", - build_setting_default = "//third_party/rust", - visibility = ["//visibility:public"], -) +clippy_flags() -# A flag to control whether rust_library and rust_binary targets should -# implicitly depend on the *real* import macro, or on a no-op target. -bool_flag( - name = "use_real_import_macro", - build_setting_default = False, - visibility = ["//visibility:public"], -) +clippy_toml() -# When set, this flag causes rustc to emit .rmeta files and use them for rlib -> rlib dependencies. -# While this involves one extra (short) rustc invocation to build the rmeta file, -# it allows library dependencies to be unlocked much sooner, increasing parallelism during compilation. -bool_flag( - name = "pipelined_compilation", - build_setting_default = False, - visibility = ["//visibility:public"], -) +error_format() -# A flag to control whether to link rust_binary and rust_test targets using -# cc_common.link instead of rustc. -bool_flag( - name = "experimental_use_cc_common_link", - build_setting_default = False, - visibility = ["//visibility:public"], -) +experimental_link_std_dylib() -# A flag to indicate that a global allocator is in use when using --@rules_rust//rust/settings:experimental_use_cc_common_link -# Users need to specify this flag because rustc generates different set of symbols at link time when a global allocator is in use. -# When the linking is not done by rustc, the `rust_toolchain` itself provides the appropriate set of symbols. -bool_flag( - name = "experimental_use_global_allocator", - build_setting_default = False, - visibility = ["//visibility:public"], -) +experimental_per_crate_rustc_flag() -# A flag to have coverage tooling added as `coverage_common.instrumented_files_info.metadata_files` instead of -# reporting tools like `llvm-cov` and `llvm-profdata` as runfiles to each test. -bool_flag( - name = "experimental_use_coverage_metadata_files", - build_setting_default = True, - visibility = ["//visibility:public"], -) +experimental_use_cc_common_link() -# A flag to set rustc --sysroot flag to the sysroot generated by rust_toolchain. -bool_flag( - name = "toolchain_generated_sysroot", - build_setting_default = True, - visibility = ["//visibility:public"], -) +experimental_use_coverage_metadata_files() -# A flag to put rust_test compilation outputs in the same directory as the rust_library compilation outputs. -incompatible_flag( - name = "incompatible_change_rust_test_compilation_output_directory", - build_setting_default = False, - issue = "https://github.com/bazelbuild/rules_rust/issues/2827", - visibility = ["//visibility:public"], -) +experimental_use_global_allocator() -# A flag to control whether to link libstd dynamically. -bool_flag( - name = "experimental_link_std_dylib", - build_setting_default = False, - visibility = ["//visibility:public"], -) +experimental_use_sh_toolchain_for_bootstrap_process_wrapper() -# A flag to control whether the shell path from a shell toolchain (`@bazel_tools//tools/sh:toolchain_type`) -# is embedded into the bootstrap process wrapper for the `.sh` file. -bool_flag( - name = "experimental_use_sh_toolchain_for_bootstrap_process_wrapper", - build_setting_default = False, - visibility = ["//visibility:public"], -) +extra_exec_rustc_flag() -# This setting is used by the clippy rules. See https://bazelbuild.github.io/rules_rust/rust_clippy.html -label_flag( - name = "clippy.toml", - build_setting_default = ".clippy.toml", - visibility = ["//visibility:public"], -) +extra_exec_rustc_flags() -# This setting is used by the rustfmt rules. See https://bazelbuild.github.io/rules_rust/rust_fmt.html -label_flag( - name = "rustfmt.toml", - build_setting_default = ".rustfmt.toml", - visibility = ["//visibility:public"], -) +extra_rustc_flag() -capture_clippy_output( - name = "capture_clippy_output", - build_setting_default = False, - visibility = ["//visibility:public"], -) - -# This setting may be used to enable builds without the standard library. -# Currently only no_std + alloc is supported, which can be enabled with setting the value to "alloc". -# In the future we could add support for additional modes, e.g "core", "alloc,collections". -string_flag( - name = "no_std", - build_setting_default = "off", - values = [ - "alloc", - "off", - ], - visibility = ["//visibility:public"], -) +extra_rustc_flags() -# A hack target to allow as to only apply the no_std mode in target config. -no_std( - name = "build_target_in_no_std", -) +incompatible_change_rust_test_compilation_output_directory() -# A config setting for setting conditional `cargo_features`, `deps`, based on the `:no_std` value. -config_setting( - name = "is_no_std", - flag_values = { - ":build_target_in_no_std": "alloc", - }, - visibility = ["//visibility:public"], -) +incompatible_do_not_include_data_in_compile_data() -# This setting may be changed from the command line to generate machine readable errors. -error_format( - name = "error_format", - build_setting_default = "human", - visibility = ["//visibility:public"], -) +lto() -# This setting may be changed from the command line to generate rustc diagnostics. -rustc_output_diagnostics( - name = "rustc_output_diagnostics", - build_setting_default = False, - visibility = ["//visibility:public"], -) +no_std() -# This setting may be used to pass extra options to clippy from the command line. -# It applies across all targets. -clippy_flags( - name = "clippy_flags", - build_setting_default = [], - visibility = ["//visibility:public"], -) +pipelined_compilation() -clippy_flag( - name = "clippy_flag", - build_setting_default = [], - visibility = ["//visibility:public"], -) +rename_first_party_crates() -# This setting may be used to pass extra options to rustc from the command line -# in non-exec configuration. -# It applies across all targets whereas the rustc_flags option on targets applies only -# to that target. This can be useful for passing build-wide options such as LTO. -extra_rustc_flags( - name = "extra_rustc_flags", - build_setting_default = [], - visibility = ["//visibility:public"], -) +rustc_output_diagnostics() -extra_rustc_flag( - name = "extra_rustc_flag", - build_setting_default = [], - visibility = ["//visibility:public"], -) +rustfmt_toml() -# This setting may be used to pass extra options to rustc from the command line -# in exec configuration. -# It applies across all targets whereas the rustc_flags option on targets applies only -# to that target. This can be useful for passing build-wide options such as LTO. -extra_exec_rustc_flags( - name = "extra_exec_rustc_flags", - build_setting_default = [], - visibility = ["//visibility:public"], -) +third_party_dir() -extra_exec_rustc_flag( - name = "extra_exec_rustc_flag", - build_setting_default = [], - visibility = ["//visibility:public"], -) +toolchain_generated_sysroot() -per_crate_rustc_flag( - name = "experimental_per_crate_rustc_flag", - build_setting_default = [], - visibility = ["//visibility:public"], -) +unpretty() -# A flag to control whether to include data files in compile_data. -incompatible_flag( - name = "incompatible_do_not_include_data_in_compile_data", - build_setting_default = True, - issue = "https://github.com/bazelbuild/rules_rust/issues/2977", - visibility = ["//visibility:public"], -) +use_real_import_macro() diff --git a/rust/settings/settings.bzl b/rust/settings/settings.bzl new file mode 100644 index 0000000000..fd1fffcf7b --- /dev/null +++ b/rust/settings/settings.bzl @@ -0,0 +1,330 @@ +"""# Rust settings + +Definitions for all `@rules_rust//rust` settings +""" + +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag") +load( + "//rust:defs.bzl", + _capture_clippy_output = "capture_clippy_output", + _clippy_flag = "clippy_flag", + _clippy_flags = "clippy_flags", + _error_format = "error_format", + _extra_exec_rustc_flag = "extra_exec_rustc_flag", + _extra_exec_rustc_flags = "extra_exec_rustc_flags", + _extra_rustc_flag = "extra_rustc_flag", + _extra_rustc_flags = "extra_rustc_flags", + _no_std = "no_std", + _per_crate_rustc_flag = "per_crate_rustc_flag", + _rustc_output_diagnostics = "rustc_output_diagnostics", +) +load("//rust/private:lto.bzl", "rust_lto_flag") +load("//rust/private:unpretty.bzl", "rust_unpretty_flag") +load(":incompatible.bzl", "incompatible_flag") + +# buildifier: disable=unnamed-macro +def unpretty(): + rust_unpretty_flag( + name = "unpretty", + build_setting_default = [ + "ast-tree,expanded", + "ast-tree", + "expanded,hygiene", + "expanded,identified", + "expanded", + "hir-tree", + "hir,identified", + "hir,typed", + "hir", + "identified", + "mir-cfg", + "mir", + "normal", + ], + ) + +# buildifier: disable=unnamed-macro +def lto(): + """A build setting which specifies the link time optimization mode used when building Rust code.""" + rust_lto_flag( + name = "lto", + build_setting_default = "unspecified", + ) + +def rename_first_party_crates(): + """A flag controlling whether to rename first-party crates such that their names \ + encode the Bazel package and target name, instead of just the target name. + + First-party vs. third-party crates are identified using the value of + `@rules_rust//settings:third_party_dir`. + """ + bool_flag( + name = "rename_first_party_crates", + build_setting_default = False, + ) + +def third_party_dir(): + """A flag specifying the location of vendored third-party rust crates within this \ + repository that must not be renamed when `rename_first_party_crates` is enabled. + + Must be specified as a Bazel package, e.g. "//some/location/in/repo". + """ + string_flag( + name = "third_party_dir", + build_setting_default = str(Label("//third_party/rust")), + ) + +def use_real_import_macro(): + """A flag to control whether rust_library and rust_binary targets should \ + implicitly depend on the *real* import macro, or on a no-op target. + """ + bool_flag( + name = "use_real_import_macro", + build_setting_default = False, + ) + +def pipelined_compilation(): + """When set, this flag causes rustc to emit `*.rmeta` files and use them for `rlib -> rlib` dependencies. + + While this involves one extra (short) rustc invocation to build the rmeta file, + it allows library dependencies to be unlocked much sooner, increasing parallelism during compilation. + """ + bool_flag( + name = "pipelined_compilation", + build_setting_default = False, + ) + +def experimental_use_cc_common_link(): + """A flag to control whether to link rust_binary and rust_test targets using \ + cc_common.link instead of rustc. + """ + bool_flag( + name = "experimental_use_cc_common_link", + build_setting_default = False, + ) + +def experimental_use_global_allocator(): + """A flag to indicate that a global allocator is in use when using `--@rules_rust//rust/settings:experimental_use_cc_common_link` + + Users need to specify this flag because rustc generates different set of symbols at link time when a global allocator is in use. + When the linking is not done by rustc, the `rust_toolchain` itself provides the appropriate set of symbols. + """ + bool_flag( + name = "experimental_use_global_allocator", + build_setting_default = False, + ) + +def experimental_use_coverage_metadata_files(): + """A flag to have coverage tooling added as `coverage_common.instrumented_files_info.metadata_files` instead of \ + reporting tools like `llvm-cov` and `llvm-profdata` as runfiles to each test. + """ + bool_flag( + name = "experimental_use_coverage_metadata_files", + build_setting_default = True, + ) + +def toolchain_generated_sysroot(): + """A flag to set rustc --sysroot flag to the sysroot generated by rust_toolchain.""" + bool_flag( + name = "toolchain_generated_sysroot", + build_setting_default = True, + visibility = ["//visibility:public"], + ) + +# buildifier: disable=unnamed-macro +def incompatible_change_rust_test_compilation_output_directory(): + """A flag to put rust_test compilation outputs in the same directory as the rust_library compilation outputs. + """ + incompatible_flag( + name = "incompatible_change_rust_test_compilation_output_directory", + build_setting_default = False, + issue = "https://github.com/bazelbuild/rules_rust/issues/2827", + ) + +def experimental_link_std_dylib(): + """A flag to control whether to link libstd dynamically.""" + bool_flag( + name = "experimental_link_std_dylib", + build_setting_default = False, + ) + +def experimental_use_sh_toolchain_for_bootstrap_process_wrapper(): + """A flag to control whether the shell path from a shell toolchain (`@bazel_tools//tools/sh:toolchain_type`) \ + is embedded into the bootstrap process wrapper for the `.sh` file. + """ + bool_flag( + name = "experimental_use_sh_toolchain_for_bootstrap_process_wrapper", + build_setting_default = False, + ) + +# buildifier: disable=unnamed-macro +def clippy_toml(): + """This setting is used by the clippy rules. See https://bazelbuild.github.io/rules_rust/rust_clippy.html + + Note that this setting is actually called `clippy.toml`. + """ + native.label_flag( + name = "clippy.toml", + build_setting_default = ".clippy.toml", + ) + +# buildifier: disable=unnamed-macro +def rustfmt_toml(): + """This setting is used by the rustfmt rules. See https://bazelbuild.github.io/rules_rust/rust_fmt.html + + Note that this setting is actually called `rustfmt.toml`. + """ + native.label_flag( + name = "rustfmt.toml", + build_setting_default = ".rustfmt.toml", + ) + +# buildifier: disable=unnamed-macro +def capture_clippy_output(): + """Control whether to print clippy output or store it to a file, using the configured error_format.""" + _capture_clippy_output( + name = "capture_clippy_output", + build_setting_default = False, + ) + +# buildifier: disable=unnamed-macro +def no_std(): + """This setting may be used to enable builds without the standard library. + + Currently only no_std + alloc is supported, which can be enabled with setting the value to "alloc". + In the future we could add support for additional modes, e.g "core", "alloc,collections". + """ + string_flag( + name = "no_std", + build_setting_default = "off", + values = [ + "alloc", + "off", + ], + ) + + # A hack target to allow as to only apply the no_std mode in target config. + _no_std( + name = "build_target_in_no_std", + visibility = ["//visibility:private"], + ) + + # A config setting for setting conditional `cargo_features`, `deps`, based on the `:no_std` value. + native.config_setting( + name = "is_no_std", + flag_values = { + ":build_target_in_no_std": "alloc", + }, + ) + +# buildifier: disable=unnamed-macro +def error_format(): + """This setting may be changed from the command line to generate machine readable errors. + """ + _error_format( + name = "error_format", + build_setting_default = "human", + ) + +# buildifier: disable=unnamed-macro +def rustc_output_diagnostics(): + """This setting may be changed from the command line to generate rustc diagnostics. + """ + _rustc_output_diagnostics( + name = "rustc_output_diagnostics", + build_setting_default = False, + visibility = ["//visibility:public"], + ) + +# buildifier: disable=unnamed-macro +def clippy_flags(): + """This setting may be used to pass extra options to clippy from the command line. + + It applies across all targets. + """ + _clippy_flags( + name = "clippy_flags", + build_setting_default = [], + ) + +# buildifier: disable=unnamed-macro +def clippy_flag(): + """Add a custom clippy flag from the command line with `--@rules_rust//rust/settings:clippy_flag`. + + Multiple uses are accumulated and appended after the `extra_rustc_flags`. + """ + _clippy_flag( + name = "clippy_flag", + build_setting_default = [], + ) + +# buildifier: disable=unnamed-macro +def extra_rustc_flags(): + """This setting may be used to pass extra options to rustc from the command line in non-exec configuration. + + It applies across all targets whereas the rustc_flags option on targets applies only + to that target. This can be useful for passing build-wide options such as LTO. + """ + _extra_rustc_flags( + name = "extra_rustc_flags", + build_setting_default = [], + ) + +# buildifier: disable=unnamed-macro +def extra_rustc_flag(): + """Add additional rustc_flag from the command line with `--@rules_rust//rust/settings:extra_rustc_flag`. + + Multiple uses are accumulated and appended after the `extra_rustc_flags`. + """ + _extra_rustc_flag( + name = "extra_rustc_flag", + build_setting_default = [], + ) + +# buildifier: disable=unnamed-macro +def extra_exec_rustc_flags(): + """This setting may be used to pass extra options to rustc from the command line in exec configuration. + + It applies across all targets whereas the rustc_flags option on targets applies only + to that target. This can be useful for passing build-wide options such as LTO. + """ + _extra_exec_rustc_flags( + name = "extra_exec_rustc_flags", + build_setting_default = [], + ) + +# buildifier: disable=unnamed-macro +def extra_exec_rustc_flag(): + """Add additional rustc_flags in the exec configuration from the command line with `--@rules_rust//rust/settings:extra_exec_rustc_flag`. + + Multiple uses are accumulated and appended after the extra_exec_rustc_flags. + """ + _extra_exec_rustc_flag( + name = "extra_exec_rustc_flag", + build_setting_default = [], + ) + +# buildifier: disable=unnamed-macro +def experimental_per_crate_rustc_flag(): + """Add additional rustc_flag to matching crates from the command line with `--@rules_rust//rust/settings:experimental_per_crate_rustc_flag`. + + The expected flag format is prefix_filter@flag, where any crate with a label or execution path starting + with the prefix filter will be built with the given flag. The label matching uses the canonical form of + the label (i.e `//package:label_name`). The execution path is the relative path to your workspace directory + including the base name (including extension) of the crate root. This flag is only applied to the exec + configuration (proc-macros, cargo_build_script, etc). Multiple uses are accumulated. + """ + _per_crate_rustc_flag( + name = "experimental_per_crate_rustc_flag", + build_setting_default = [], + ) + +# buildifier: disable=unnamed-macro +def incompatible_do_not_include_data_in_compile_data(): + """A flag to control whether to include data files in compile_data. + """ + incompatible_flag( + name = "incompatible_do_not_include_data_in_compile_data", + build_setting_default = True, + issue = "https://github.com/bazelbuild/rules_rust/issues/2977", + ) diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index abb93284e3..a1ed375743 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -1,4 +1,7 @@ -"""The rust_toolchain rule definition and implementation.""" +"""# Rust Toolchains + +Toolchain rules for Rust. +""" load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") load("//rust/platform:triple.bzl", "triple")