Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow detecting if --precompile_source_retention was specified on the command line #2192

Merged
merged 1 commit into from
Sep 6, 2024
Merged
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
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