Skip to content

Commit c46ee92

Browse files
authored
fix: allow detecting if --precompile_source_retention was specified on the command line (bazel-contrib#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".
1 parent 4248467 commit c46ee92

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

docs/api/rules_python/python/config_settings/index.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ except for the case of `force_enabled` and `forced_disabled`.
3636

3737
Values:
3838

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

6666
Values:
6767

68+
* `auto`: (default) Automatically decide the effective value based on environment,
69+
target platform, etc.
6870
* `keep_source`: Include the original Python source.
6971
* `omit_source`: Don't include the orignal py source.
7072
* `omit_if_generated_source`: Keep the original source if it's a regular source
7173
file, but omit it if it's a generated file.
74+
7275
:::{versionadded} 0.33.0
7376
:::
77+
:::{versionadded} 0.36.0
78+
The `auto` value
79+
:::
7480
::::
7581

7682
::::{bzl:flag} precompile_add_to_runfiles

python/config_settings/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ string_flag(
5858

5959
string_flag(
6060
name = "precompile_source_retention",
61-
build_setting_default = PrecompileSourceRetentionFlag.KEEP_SOURCE,
61+
build_setting_default = PrecompileSourceRetentionFlag.AUTO,
6262
values = sorted(PrecompileSourceRetentionFlag.__members__.values()),
6363
# NOTE: Only public because it's an implicit dependency
6464
visibility = ["//visibility:public"],

python/private/common/attributes.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
1717
load("@rules_cc//cc:defs.bzl", "CcInfo")
1818
load("//python/private:enum.bzl", "enum")
19-
load("//python/private:flags.bzl", "PrecompileFlag")
19+
load("//python/private:flags.bzl", "PrecompileFlag", "PrecompileSourceRetentionFlag")
2020
load("//python/private:reexports.bzl", "BuiltinPyInfo")
2121
load(":common.bzl", "union_attrs")
2222
load(":providers.bzl", "PyInfo")
@@ -85,7 +85,7 @@ PrecompileInvalidationModeAttr = enum(
8585
def _precompile_source_retention_get_effective_value(ctx):
8686
attr_value = ctx.attr.precompile_source_retention
8787
if attr_value == PrecompileSourceRetentionAttr.INHERIT:
88-
attr_value = ctx.attr._precompile_source_retention_flag[BuildSettingInfo].value
88+
attr_value = PrecompileSourceRetentionFlag.get_effective_value(ctx)
8989

9090
if attr_value not in (
9191
PrecompileSourceRetentionAttr.KEEP_SOURCE,

python/private/flags.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,26 @@ PrecompileFlag = enum(
7474
get_effective_value = _precompile_flag_get_effective_value,
7575
)
7676

77+
def _precompile_source_retention_flag_get_effective_value(ctx):
78+
value = ctx.attr._precompile_source_retention_flag[BuildSettingInfo].value
79+
if value == PrecompileSourceRetentionFlag.AUTO:
80+
value = PrecompileSourceRetentionFlag.KEEP_SOURCE
81+
return value
82+
7783
# Determines if, when a source file is compiled, if the source file is kept
7884
# in the resulting output or not.
7985
# buildifier: disable=name-conventions
8086
PrecompileSourceRetentionFlag = enum(
87+
# Automatically decide the effective value based on environment, etc.
88+
AUTO = "auto",
8189
# Include the original py source in the output.
8290
KEEP_SOURCE = "keep_source",
8391
# Don't include the original py source.
8492
OMIT_SOURCE = "omit_source",
8593
# Keep the original py source if it's a regular source file, but omit it
8694
# if it's a generated file.
8795
OMIT_IF_GENERATED_SOURCE = "omit_if_generated_source",
96+
get_effective_value = _precompile_source_retention_flag_get_effective_value,
8897
)
8998

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

0 commit comments

Comments
 (0)