Skip to content

Commit

Permalink
fix: allow detecting if --precompile_source_retention was specified…
Browse files Browse the repository at this point in the history
… on the command line (bazelbuild#2192)

This defaults the `--precompile_source_retention` flag to a new value,
`auto`. The effective default remains the same (`keep_source`).

Having a separate value as the default allows detecting if the value was
specified on the command line, which allows distinguishing between "pick
for me" vs "I specifically want this behavior".
  • Loading branch information
rickeylev authored Sep 6, 2024
1 parent 4248467 commit c46ee92
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
8 changes: 7 additions & 1 deletion docs/api/rules_python/python/config_settings/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ except for the case of `force_enabled` and `forced_disabled`.

Values:

* `auto`: Automatically decide the effective value based on environment,
* `auto`: (default) Automatically decide the effective value based on environment,
target platform, etc.
* `enabled`: Compile Python source files at build time. Note that
{bzl:obj}`--precompile_add_to_runfiles` affects how the compiled files are included into
Expand Down Expand Up @@ -65,12 +65,18 @@ attribute.

Values:

* `auto`: (default) Automatically decide the effective value based on environment,
target platform, etc.
* `keep_source`: Include the original Python source.
* `omit_source`: Don't include the orignal py source.
* `omit_if_generated_source`: Keep the original source if it's a regular source
file, but omit it if it's a generated file.

:::{versionadded} 0.33.0
:::
:::{versionadded} 0.36.0
The `auto` value
:::
::::

::::{bzl:flag} precompile_add_to_runfiles
Expand Down
2 changes: 1 addition & 1 deletion python/config_settings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ string_flag(

string_flag(
name = "precompile_source_retention",
build_setting_default = PrecompileSourceRetentionFlag.KEEP_SOURCE,
build_setting_default = PrecompileSourceRetentionFlag.AUTO,
values = sorted(PrecompileSourceRetentionFlag.__members__.values()),
# NOTE: Only public because it's an implicit dependency
visibility = ["//visibility:public"],
Expand Down
4 changes: 2 additions & 2 deletions python/private/common/attributes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@rules_cc//cc:defs.bzl", "CcInfo")
load("//python/private:enum.bzl", "enum")
load("//python/private:flags.bzl", "PrecompileFlag")
load("//python/private:flags.bzl", "PrecompileFlag", "PrecompileSourceRetentionFlag")
load("//python/private:reexports.bzl", "BuiltinPyInfo")
load(":common.bzl", "union_attrs")
load(":providers.bzl", "PyInfo")
Expand Down Expand Up @@ -85,7 +85,7 @@ PrecompileInvalidationModeAttr = enum(
def _precompile_source_retention_get_effective_value(ctx):
attr_value = ctx.attr.precompile_source_retention
if attr_value == PrecompileSourceRetentionAttr.INHERIT:
attr_value = ctx.attr._precompile_source_retention_flag[BuildSettingInfo].value
attr_value = PrecompileSourceRetentionFlag.get_effective_value(ctx)

if attr_value not in (
PrecompileSourceRetentionAttr.KEEP_SOURCE,
Expand Down
9 changes: 9 additions & 0 deletions python/private/flags.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,26 @@ PrecompileFlag = enum(
get_effective_value = _precompile_flag_get_effective_value,
)

def _precompile_source_retention_flag_get_effective_value(ctx):
value = ctx.attr._precompile_source_retention_flag[BuildSettingInfo].value
if value == PrecompileSourceRetentionFlag.AUTO:
value = PrecompileSourceRetentionFlag.KEEP_SOURCE
return value

# Determines if, when a source file is compiled, if the source file is kept
# in the resulting output or not.
# buildifier: disable=name-conventions
PrecompileSourceRetentionFlag = enum(
# Automatically decide the effective value based on environment, etc.
AUTO = "auto",
# Include the original py source in the output.
KEEP_SOURCE = "keep_source",
# Don't include the original py source.
OMIT_SOURCE = "omit_source",
# Keep the original py source if it's a regular source file, but omit it
# if it's a generated file.
OMIT_IF_GENERATED_SOURCE = "omit_if_generated_source",
get_effective_value = _precompile_source_retention_flag_get_effective_value,
)

# Determines if a target adds its compiled files to its runfiles. When a target
Expand Down

0 comments on commit c46ee92

Please sign in to comment.