Skip to content
Draft
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
4 changes: 4 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ common --incompatible_python_disallow_native_rules
common --incompatible_no_implicit_file_export

build --lockfile_mode=update

# See issue 3567. Disable implicit python zip creation.
common --build_python_zip=false
common --@rules_python//python/config_settings:build_python_zip=false
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ filegroup(
"internal_dev_deps.bzl",
"internal_dev_setup.bzl",
"version.bzl",
"//command_line_option:distribution",
"//python:distribution",
"//tools:distribution",
],
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ END_UNRELEASED_TEMPLATE
in order to make `pytorch` and friends easier to patch.
* (wheel) `py_wheel` no longer expands the input depset during analysis,
improving analysis performance for targets with large dependency trees.
* (binaries/tests) (Windows) `--enable_runfiles=true` is the default for
py_binary/py_test.

{#v0-0-0-fixed}
### Fixed
Expand Down
28 changes: 28 additions & 0 deletions command_line_option/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Aliases for Bazel builtin //command_line_option psuedo-targets
#
# These are alias to use with `py_binary.config_settings` that are treated
# as aliases for `//command_line_option:XXX` psuedo-targets.

package(
default_visibility = ["//visibility:public"],
)

# todo: add docs, xref with config_settings
# rules_python target, config_setting processing code converts it to the
# Bazel-builtin //command_line_label:build_runfile_links psuedo-target
alias(
name = "build_runfile_links",
actual = "//python:none",
)

# todo: add docs
alias(
name = "enable_runfiles",
actual = "//python:none",
)
Comment on lines +10 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The new aliases build_runfile_links and enable_runfiles have todo: add docs comments. It would be beneficial to add this documentation as part of this PR to make the new feature easier for users to understand. The documentation could explain what these aliases are for, how they relate to the Bazel built-in command line options, and include a cross-reference to config_settings as noted in the comment.


filegroup(
name = "distribution",
srcs = glob(["**"]),
visibility = ["//:__subpackages__"],
)
41 changes: 41 additions & 0 deletions docs/api/rules_python/command_line_option/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
:::{default-domain} bzl
:::
:::{bzl:currentfile} //command_line_option:BUILD.bazel
:::

# //command_line_option
Comment on lines +1 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This documentation file is currently a stub. It would be beneficial to expand it to document the new //command_line_option feature, including the available aliases and how to define custom ones. This would make the feature more discoverable and usable.


This package provides special targets that correspond to the Bazel-builtin
`//command_line_option` psuedo-targets. These can be used with the {obj}`config_settings`
attribute on Python rules to transition specific command line flags for a target.

:::{note}
These targets are not actual `alias()` targets, nor are they the actual builtin
command line flags. They are regular targets that the `config_settings` transition
logic specially recognizes and handles as if they were the builtin `//command_line_option`
psuedo-targets.
:::

:::{seealso}
The `config_settings` attribute documentation on:
* {obj}`py_binary.config_settings`
* {obj}`py_test.config_settings`
:::

## build_runfile_links

:::{bzl:target} build_runfile_links

Special target for the Bazel-builtin `//command_line_option:build_runfile_links` flag.

See the [Bazel documentation for --build_runfile_links](https://bazel.build/reference/command-line-reference#flag--build_runfile_links).
:::

## enable_runfiles

:::{bzl:target} enable_runfiles

Special target for the Bazel-builtin `//command_line_option:enable_runfiles` flag.

See the [Bazel documentation for --enable_runfiles](https://bazel.build/reference/command-line-reference#flag--enable_runfiles).
:::
4 changes: 4 additions & 0 deletions examples/build_file_generation/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ build --enable_runfiles
common --noenable_bzlmod
common --enable_workspace
common --incompatible_python_disallow_native_rules

# See issue 3567. Disable implicit python zip creation.
common --build_python_zip=false
common --@rules_python//python/config_settings:build_python_zip=false
4 changes: 4 additions & 0 deletions examples/bzlmod/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ test --test_output=errors --enable_runfiles
# Windows requires these for multi-python support:
build --enable_runfiles
common:bazel7.x --incompatible_python_disallow_native_rules

# See issue 3567. Disable implicit python zip creation.
common --build_python_zip=false
common --@rules_python//python/config_settings:build_python_zip=false
4 changes: 4 additions & 0 deletions examples/multi_python_versions/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ build --enable_runfiles

coverage --java_runtime_version=remotejdk_11
common:bazel7.x --incompatible_python_disallow_native_rules

# See issue 3567. Disable implicit python zip creation.
common --build_python_zip=false
common --@rules_python//python/config_settings:build_python_zip=false
7 changes: 7 additions & 0 deletions examples/pip_parse/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# https://docs.bazel.build/versions/main/best-practices.html#using-the-bazelrc-file
try-import %workspace%/user.bazelrc
common --incompatible_python_disallow_native_rules

# See issue 3567. Disable implicit python zip creation.
common --build_python_zip=false
common --@rules_python//python/config_settings:build_python_zip=false

# Windows makes use of runfiles for some rules
##common --enable_runfiles
4 changes: 4 additions & 0 deletions examples/pip_parse_vendored/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ build --enable_runfiles
common --noenable_bzlmod
common --enable_workspace
common --incompatible_python_disallow_native_rules

# See issue 3567. Disable implicit python zip creation.
common --build_python_zip=false
common --@rules_python//python/config_settings:build_python_zip=false
4 changes: 4 additions & 0 deletions examples/pip_repository_annotations/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ common --noenable_bzlmod
common --enable_workspace
common --legacy_external_runfiles=false
common --incompatible_python_disallow_native_rules

# See issue 3567. Disable implicit python zip creation.
common --build_python_zip=false
common --@rules_python//python/config_settings:build_python_zip=false
4 changes: 4 additions & 0 deletions gazelle/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ build --@rules_python//python/config_settings:incompatible_default_to_explicit_i
build --enable_runfiles

common:bazel7.x --incompatible_python_disallow_native_rules

# See issue 3567. Disable implicit python zip creation.
common --build_python_zip=false
common --@rules_python//python/config_settings:build_python_zip=false
4 changes: 4 additions & 0 deletions gazelle/examples/bzlmod_build_file_generation/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ common:bazel7.x --incompatible_python_disallow_native_rules
# rules_python code. In the BCR presubmits, this override is removed
# and the bazel_dep version of rules_python is used.
common --override_module=rules_python=../../../

# See issue 3567. Disable implicit python zip creation.
common --build_python_zip=false
common --@rules_python//python/config_settings:build_python_zip=false
23 changes: 22 additions & 1 deletion python/private/attributes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,23 @@ particular CPU, or defining a custom setting that `select()` uses elsewhere
to pick between `pip.parse` hubs. See the [How to guide on multiple
versions of a library] for a more concrete example.

:::{important}
Labels with package `command_line_option` are handled specially: they are treated
as the Bazel-builtin `//command_line_option:<name>` psuedo-targets.

e.g. `@foo//command_line_option:NAME` will attempt to transition
the Bazel-builtin `//command_line_option:NAME` setting.

See the {obj}`@rules_python//command_line_option` package for some predefined
special targets, or define your own by putting them in your own `command_line_option`
directory.
:::

:::{seealso}
* {obj}`//command_line_option:build_runfile_links`
* {obj}`//command_line_option:enable_runfiles`
:::

:::{note}
These values are transitioned on, so will affect the analysis graph and the
associated memory overhead. The more unique configurations in your overall
Expand All @@ -426,7 +443,11 @@ def apply_config_settings_attr(settings, attr):
{type}`dict[str, object]` the input `settings` value.
"""
for key, value in attr.config_settings.items():
settings[str(key)] = value
if key.package == "command_line_option":
str_key = "//command_line_option:" + key.name
else:
str_key = str(key)
settings[str_key] = value
return settings

AGNOSTIC_EXECUTABLE_ATTRS = dicts.add(
Expand Down
7 changes: 6 additions & 1 deletion python/private/common_labels.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ labels = struct(
ADD_SRCS_TO_RUNFILES = str(Label("//python/config_settings:add_srcs_to_runfiles")),
BOOTSTRAP_IMPL = str(Label("//python/config_settings:bootstrap_impl")),
BUILD_PYTHON_ZIP = str(Label("//python/config_settings:build_python_zip")),
# NOTE: Special target; see definition for details.
BUILD_RUNFILE_LINKS = str(Label("//command_line_option:build_runfile_links")),
DEBUGGER = str(Label("//python/config_settings:debugger")),
# NOTE: Special target; see definition for details.
ENABLE_RUNFILES = str(Label("//command_line_option:enable_runfiles")),
EXEC_TOOLS_TOOLCHAIN = str(Label("//python/config_settings:exec_tools_toolchain")),
PIP_ENV_MARKER_CONFIG = str(Label("//python/config_settings:pip_env_marker_config")),
NONE = str(Label("//python:none")),
PIP_ENV_MARKER_CONFIG = str(Label("//python/config_settings:pip_env_marker_config")),
PIP_WHL = str(Label("//python/config_settings:pip_whl")),
PIP_WHL_GLIBC_VERSION = str(Label("//python/config_settings:pip_whl_glibc_version")),
PIP_WHL_MUSLC_VERSION = str(Label("//python/config_settings:pip_whl_muslc_version")),
PIP_WHL_OSX_ARCH = str(Label("//python/config_settings:pip_whl_osx_arch")),
PIP_WHL_OSX_VERSION = str(Label("//python/config_settings:pip_whl_osx_version")),
PLATFORMS_OS_WINDOWS = str(Label("@platforms//os:windows")),
PRECOMPILE = str(Label("//python/config_settings:precompile")),
PRECOMPILE_SOURCE_RETENTION = str(Label("//python/config_settings:precompile_source_retention")),
PYC_COLLECTION = str(Label("//python/config_settings:pyc_collection")),
Expand Down
4 changes: 2 additions & 2 deletions python/private/py_binary_macro.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"""Implementation of macro-half of py_binary rule."""

load(":py_binary_rule.bzl", py_binary_rule = "py_binary")
load(":py_executable.bzl", "convert_legacy_create_init_to_int")
load(":py_executable.bzl", "common_executable_macro_kwargs_setup")

def py_binary(**kwargs):
py_binary_macro(py_binary_rule, **kwargs)

def py_binary_macro(py_rule, **kwargs):
convert_legacy_create_init_to_int(kwargs)
common_executable_macro_kwargs_setup(kwargs)
py_rule(**kwargs)
36 changes: 32 additions & 4 deletions python/private/py_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -768,10 +768,7 @@ def _create_stage1_bootstrap(
}

if stage2_bootstrap:
subs["%stage2_bootstrap%"] = "{}/{}".format(
ctx.workspace_name,
stage2_bootstrap.short_path,
)
subs["%stage2_bootstrap%"] = runfiles_root_path(ctx, stage2_bootstrap.short_path)
template = runtime.bootstrap_template
subs["%shebang%"] = runtime.stub_shebang
elif not ctx.files.srcs:
Expand Down Expand Up @@ -1771,6 +1768,32 @@ def _create_run_environment_info(ctx, inherited_environment):
inherited_environment = inherited_environment,
)

def add_config_setting_defaults(kwargs):
config_settings = kwargs.get("config_settings", None)
if config_settings == None:
config_settings = {}

# NOTE: This code runs in loading phase within the context of the caller.
# Label() must be used to resolve repo names within rules_python's
# context to avoid unknown repo name errors.
default = select({
labels.PLATFORMS_OS_WINDOWS: {
##labels.BUILD_RUNFILE_LINKS: "true",
labels.ENABLE_RUNFILES: "true",
},
"//conditions:default": {
##labels.BUILD_RUNFILE_LINKS: "true",
},
})

# Let user-provided settings have precedence
config_settings = default | config_settings
kwargs["config_settings"] = config_settings

def common_executable_macro_kwargs_setup(kwargs):
convert_legacy_create_init_to_int(kwargs)
add_config_setting_defaults(kwargs)

def _transition_executable_impl(settings, attr):
settings = dict(settings)
apply_config_settings_attr(settings, attr)
Expand All @@ -1780,6 +1803,7 @@ def _transition_executable_impl(settings, attr):

if attr.stamp != -1:
settings["//command_line_option:stamp"] = str(attr.stamp)

return settings

def create_executable_rule(*, attrs, **kwargs):
Expand Down Expand Up @@ -1833,10 +1857,14 @@ def create_executable_rule_builder(implementation, **kwargs):
inputs = TRANSITION_LABELS + [
labels.PYTHON_VERSION,
"//command_line_option:stamp",
"//command_line_option:build_runfile_links",
"//command_line_option:enable_runfiles",
],
outputs = TRANSITION_LABELS + [
labels.PYTHON_VERSION,
"//command_line_option:stamp",
"//command_line_option:build_runfile_links",
"//command_line_option:enable_runfiles",
],
),
**kwargs
Expand Down
4 changes: 2 additions & 2 deletions python/private/py_test_macro.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
# limitations under the License.
"""Implementation of macro-half of py_test rule."""

load(":py_executable.bzl", "convert_legacy_create_init_to_int")
load(":py_executable.bzl", "common_executable_macro_kwargs_setup")
load(":py_test_rule.bzl", py_test_rule = "py_test")

def py_test(**kwargs):
py_test_macro(py_test_rule, **kwargs)

def py_test_macro(py_rule, **kwargs):
convert_legacy_create_init_to_int(kwargs)
common_executable_macro_kwargs_setup(kwargs)
py_rule(**kwargs)
Loading