Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions rust/private/debug_info.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""A module defining Rust debug-info rules"""

load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")

RustDebugInfoInfo = provider(
doc = "A provider describing the debug-info settings per compilation mode.",
fields = {
"levels": "dict[str, str]: Mapping of compilation mode to debug-info string.",
},
)

def _rust_debug_info_flag_impl(ctx):
levels = {}
for mode in ("dbg", "fastbuild", "opt"):
levels[mode] = getattr(ctx.attr, mode)[BuildSettingInfo].value
return [RustDebugInfoInfo(levels = levels)]

rust_debug_info_flag = rule(
doc = "Aggregates the three per-mode debug-info flags into a single RustDebugInfoInfo provider. Valid values are defined by rustc: https://doc.rust-lang.org/rustc/codegen-options/index.html#debuginfo.",
implementation = _rust_debug_info_flag_impl,
attrs = {
"dbg": attr.label(default = "//rust/settings:debug_info_dbg"),
"fastbuild": attr.label(default = "//rust/settings:debug_info_fastbuild"),
"opt": attr.label(default = "//rust/settings:debug_info_opt"),
},
)
26 changes: 26 additions & 0 deletions rust/private/opt_level.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""A module defining Rust opt-level rules"""

load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")

RustOptLevelInfo = provider(
doc = "A provider describing the opt-level settings per compilation mode.",
fields = {
"levels": "dict[str, str]: Mapping of compilation mode to opt-level string.",
},
)

def _rust_opt_level_flag_impl(ctx):
levels = {}
for mode in ("dbg", "fastbuild", "opt"):
levels[mode] = getattr(ctx.attr, mode)[BuildSettingInfo].value
return [RustOptLevelInfo(levels = levels)]

rust_opt_level_flag = rule(
doc = "Aggregates the three per-mode opt-level flags into a single RustOptLevelInfo provider. Valid values are defined by rustc: https://doc.rust-lang.org/rustc/codegen-options/index.html#opt-level",
implementation = _rust_opt_level_flag_impl,
attrs = {
"dbg": attr.label(default = "//rust/settings:opt_level_dbg"),
"fastbuild": attr.label(default = "//rust/settings:opt_level_fastbuild"),
"opt": attr.label(default = "//rust/settings:opt_level_opt"),
},
)
13 changes: 13 additions & 0 deletions rust/private/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def rust_register_toolchains(
extra_rustc_flags = None,
extra_exec_rustc_flags = None,
opt_level = None,
debug_info = None,
strip_level = None,
urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
versions = _RUST_TOOLCHAIN_VERSIONS,
Expand Down Expand Up @@ -103,6 +104,7 @@ def rust_register_toolchains(
extra_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration.
extra_exec_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in exec configuration.
opt_level (dict, optional): Rustc optimization levels. For more details see the documentation for `rust_toolchain.opt_level`.
debug_info (dict, optional): Dictionary of target triples to debug info config.
strip_level (dict, dict, optional): Dictionary of target triples to strip config.
urls (list, optional): 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).
versions (list, optional): A list of toolchain versions to download. This parameter only accepts one versions
Expand Down Expand Up @@ -187,6 +189,7 @@ def rust_register_toolchains(
extra_rustc_flags = extra_rustc_flags,
extra_exec_rustc_flags = extra_exec_rustc_flags,
opt_level = opt_level_by_triple,
debug_info = debug_info,
strip_level = strip_level,
sha256s = sha256s,
urls = urls,
Expand Down Expand Up @@ -275,6 +278,9 @@ _RUST_TOOLCHAIN_REPOSITORY_ATTRS = {
"auth_patterns": attr.string_list(
doc = "A list of patterns to match against urls for which the auth object should be used.",
),
"debug_info": attr.string_dict(
doc = "Rustc debug info levels. For more details see the documentation for `rust_toolchain.debug_info`.",
),
"dev_components": attr.bool(
doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\".",
default = False,
Expand Down Expand Up @@ -483,6 +489,7 @@ def _rust_toolchain_tools_repository_impl(ctx):
extra_rustc_flags = ctx.attr.extra_rustc_flags,
extra_exec_rustc_flags = ctx.attr.extra_exec_rustc_flags,
opt_level = ctx.attr.opt_level if ctx.attr.opt_level else None,
debug_info = ctx.attr.debug_info if ctx.attr.debug_info else None,
strip_level = ctx.attr.strip_level if ctx.attr.strip_level else None,
version = toolchain_version,
channel = channel,
Expand Down Expand Up @@ -595,6 +602,7 @@ def rust_toolchain_repository(
extra_rustc_flags = None,
extra_exec_rustc_flags = None,
opt_level = None,
debug_info = None,
strip_level = None,
sha256s = None,
urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
Expand Down Expand Up @@ -627,6 +635,7 @@ def rust_toolchain_repository(
Subject to Make variable expansion with respect to RUST_SYSROOT,
RUST_SYSROOT_SHORT, RUSTC, etc.
opt_level (dict, optional): Optimization level config for this toolchain.
debug_info (dict, optional): Debug info level config for this toolchain.
strip_level (dict, optional): Strip level config for this toolchain.
sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
[rust_register_toolchains](#rust_register_toolchains) for more details.
Expand Down Expand Up @@ -660,6 +669,7 @@ def rust_toolchain_repository(
extra_rustc_flags = extra_rustc_flags,
extra_exec_rustc_flags = extra_exec_rustc_flags,
opt_level = opt_level,
debug_info = debug_info,
strip_level = strip_level,
sha256s = sha256s,
urls = urls,
Expand Down Expand Up @@ -993,6 +1003,7 @@ def rust_repository_set(
extra_rustc_flags = None,
extra_exec_rustc_flags = None,
opt_level = None,
debug_info = None,
strip_level = None,
sha256s = None,
urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
Expand Down Expand Up @@ -1026,6 +1037,7 @@ def rust_repository_set(
extra_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration.
extra_exec_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in exec configuration.
opt_level (dict, dict, optional): Dictionary of target triples to optimization config.
debug_info (dict, dict, optional): Dictionary of target triples to debug info config.
strip_level (dict, dict, optional): Dictionary of target triples to strip config.
sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
[rust_register_toolchains](#rust_register_toolchains) for more details.
Expand Down Expand Up @@ -1083,6 +1095,7 @@ def rust_repository_set(
extra_rustc_flags = toolchain_extra_rustc_flags,
extra_exec_rustc_flags = toolchain_extra_exec_rustc_flags,
opt_level = opt_level.get(toolchain.target_triple) if opt_level != None else None,
debug_info = debug_info.get(toolchain.target_triple) if debug_info != None else None,
strip_level = strip_level.get(toolchain.target_triple) if strip_level != None else None,
sha256s = sha256s,
urls = urls,
Expand Down
4 changes: 4 additions & 0 deletions rust/private/repository_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ rust_toolchain(
extra_rustc_flags = {extra_rustc_flags},
extra_exec_rustc_flags = {extra_exec_rustc_flags},
opt_level = {opt_level},
debug_info = {debug_info},
strip_level = {strip_level},
version = "{version}",
channel = "{channel}",
Expand All @@ -393,6 +394,7 @@ def BUILD_for_rust_toolchain(
extra_rustc_flags = None,
extra_exec_rustc_flags = None,
opt_level = None,
debug_info = None,
strip_level = None):
"""Emits a toolchain declaration to match an existing compiler and stdlib.

Expand All @@ -418,6 +420,7 @@ def BUILD_for_rust_toolchain(
extra_rustc_flags (list, optional): Extra flags to pass to rustc in non-exec configuration.
extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration.
opt_level (dict, optional): Optimization level config for this toolchain.
debug_info (dict, optional): Debug info level config for this toolchain.
strip_level (dict, optional): Strip level config for this toolchain.

Returns:
Expand Down Expand Up @@ -480,6 +483,7 @@ def BUILD_for_rust_toolchain(
extra_rustc_flags = extra_rustc_flags,
extra_exec_rustc_flags = extra_exec_rustc_flags,
opt_level = opt_level,
debug_info = debug_info,
strip_level = strip_level,
version = version,
channel = channel,
Expand Down
26 changes: 26 additions & 0 deletions rust/private/strip_level.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""A module defining Rust strip-level rules"""

load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")

RustStripLevelInfo = provider(
doc = "A provider describing the strip-level settings per compilation mode.",
fields = {
"levels": "dict[str, str]: Mapping of compilation mode to strip-level string.",
},
)

def _rust_strip_level_flag_impl(ctx):
levels = {}
for mode in ("dbg", "fastbuild", "opt"):
levels[mode] = getattr(ctx.attr, mode)[BuildSettingInfo].value
return [RustStripLevelInfo(levels = levels)]

rust_strip_level_flag = rule(
doc = "Aggregates the three per-mode strip-level flags into a single RustStripLevelInfo provider. Valid values are defined by rustc: https://doc.rust-lang.org/rustc/codegen-options/index.html#strip",
implementation = _rust_strip_level_flag_impl,
attrs = {
"dbg": attr.label(default = "//rust/settings:strip_level_dbg"),
"fastbuild": attr.label(default = "//rust/settings:strip_level_fastbuild"),
"opt": attr.label(default = "//rust/settings:strip_level_opt"),
},
)
65 changes: 35 additions & 30 deletions rust/private/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("//rust/platform:triple.bzl", "triple")
load("//rust/private:common.bzl", "rust_common")
load("//rust/private:debug_info.bzl", "RustDebugInfoInfo")
load("//rust/private:lto.bzl", "RustLtoInfo")
load("//rust/private:opt_level.bzl", "RustOptLevelInfo")
load(
"//rust/private:rust_allocator_libraries.bzl",
"make_libstd_and_allocator_ccinfo",
)
load("//rust/private:semver.bzl", "semver")
load("//rust/private:strip_level.bzl", "RustStripLevelInfo")
load(
"//rust/private:utils.bzl",
"deduplicate",
Expand Down Expand Up @@ -393,16 +396,18 @@ def _rust_toolchain_impl(ctx):
Returns:
list: A list containing the target's toolchain Provider info
"""
effective_opt_levels = dict(ctx.attr._opt_level[RustOptLevelInfo].levels)
effective_opt_levels.update(ctx.attr.opt_level)

effective_debug_info = dict(ctx.attr._debug_info[RustDebugInfoInfo].levels)
effective_debug_info.update(ctx.attr.debug_info)

effective_strip_level = dict(ctx.attr._strip_level[RustStripLevelInfo].levels)
effective_strip_level.update(ctx.attr.strip_level)

compilation_mode_opts = {}
for k, opt_level in ctx.attr.opt_level.items():
if not k in ctx.attr.debug_info:
fail("Compilation mode {} is not defined in debug_info but is defined opt_level".format(k))
if not k in ctx.attr.strip_level:
fail("Compilation mode {} is not defined in strip_level but is defined opt_level".format(k))
compilation_mode_opts[k] = struct(debug_info = ctx.attr.debug_info[k], opt_level = opt_level, strip_level = ctx.attr.strip_level[k])
for k in ctx.attr.debug_info.keys():
if not k in ctx.attr.opt_level:
fail("Compilation mode {} is not defined in opt_level but is defined debug_info".format(k))
for k, opt_level in effective_opt_levels.items():
compilation_mode_opts[k] = struct(debug_info = effective_debug_info[k], opt_level = opt_level, strip_level = effective_strip_level[k])

rename_first_party_crates = ctx.attr._rename_first_party_crates[BuildSettingInfo].value
third_party_dir = ctx.attr._third_party_dir[BuildSettingInfo].value
Expand Down Expand Up @@ -693,12 +698,13 @@ rust_toolchain = rule(
cfg = "exec",
),
"debug_info": attr.string_dict(
doc = "Rustc debug info levels per opt level",
default = {
"dbg": "2",
"fastbuild": "0",
"opt": "0",
},
doc = "Per-toolchain debug-info overrides per compilation mode. Any mode set here takes precedence over the global `//rust/settings:debug_info_*` flags. Omit a mode (or leave empty) to use the global flag value.",
default = {},
),
"_debug_info": attr.label(
default = Label("//rust/settings:debug_info"),
providers = [RustDebugInfoInfo],
doc = "Global debug-info defaults, read from the `//rust/settings:debug_info_*` flags.",
),
"default_edition": attr.string(
doc = (
Expand Down Expand Up @@ -796,12 +802,13 @@ rust_toolchain = rule(
doc = "Label to an LTO setting whether which can enable custom LTO settings",
),
"opt_level": attr.string_dict(
doc = "Rustc optimization levels.",
default = {
"dbg": "0",
"fastbuild": "0",
"opt": "3",
},
doc = "Per-toolchain opt-level overrides per compilation mode. Any mode set here takes precedence over the global `//rust/settings:opt_level_*` flags. Omit a mode (or leave empty) to use the global flag value.",
default = {},
),
"_opt_level": attr.label(
default = Label("//rust/settings:opt_level"),
providers = [RustOptLevelInfo],
doc = "Global opt-level defaults, read from the `//rust/settings:opt_level_*` flags.",
),
"per_crate_rustc_flags": attr.string_list(
doc = "Extra flags to pass to rustc in non-exec configuration",
Expand Down Expand Up @@ -857,15 +864,13 @@ rust_toolchain = rule(
mandatory = True,
),
"strip_level": attr.string_dict(
doc = (
"Rustc strip levels. For all potential options, see " +
"https://doc.rust-lang.org/rustc/codegen-options/index.html#strip"
),
default = {
"dbg": "none",
"fastbuild": "none",
"opt": "debuginfo",
},
doc = "Per-toolchain strip-level overrides per compilation mode. Any mode set here takes precedence over the global `//rust/settings:strip_level_*` flags. Omit a mode (or leave empty) to use the global flag value.",
default = {},
),
"_strip_level": attr.label(
default = Label("//rust/settings:strip_level"),
providers = [RustStripLevelInfo],
doc = "Global strip-level defaults, read from the `//rust/settings:strip_level_*` flags.",
),
"target_json": attr.string(
doc = ("Override the target_triple with a custom target specification. " +
Expand Down
9 changes: 9 additions & 0 deletions rust/settings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ load(
"clippy_toml",
"codegen_units",
"collect_cfgs",
"debug_info",
"default_allocator_library",
"error_format",
"experimental_compile_rustdoc_tests",
Expand All @@ -31,11 +32,13 @@ load(
"incompatible_do_not_include_transitive_data_in_compile_inputs",
"lto",
"no_std",
"opt_level",
"pipelined_compilation",
"rename_first_party_crates",
"require_explicit_unstable_features",
"rustc_output_diagnostics",
"rustfmt_toml",
"strip_level",
"third_party_dir",
"toolchain_generated_sysroot",
"toolchain_linker_preference",
Expand Down Expand Up @@ -117,10 +120,14 @@ incompatible_do_not_include_data_in_compile_data()

incompatible_do_not_include_transitive_data_in_compile_inputs()

debug_info()

lto()

no_std()

opt_level()

pipelined_compilation()

rename_first_party_crates()
Expand All @@ -131,6 +138,8 @@ rustc_output_diagnostics()

rustfmt_toml()

strip_level()

third_party_dir()

toolchain_generated_sysroot()
Expand Down
Loading