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
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
25 changes: 25 additions & 0 deletions command_line_option/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Special placeholders for Bazel builtin //command_line_option psuedo-targets
#
# These are special targets to use with `py_binary.config_settings` that are
# treated as aliases for `//command_line_option:XXX` psuedo-targets. They
# are not actual flags or have any value.

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

alias(
name = "build_runfile_links",
actual = "//python:none",
)

alias(
name = "enable_runfiles",
actual = "//python:none",
)

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

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).
:::
12 changes: 11 additions & 1 deletion examples/pip_repository_annotations/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@ py_test(
env = {
"REQUESTS_PKG_DIR": "pip_requests",
"WHEEL_PKG_DIR": "pip_wheel",
},
} | select({
":is_enable_runfiles_true": {"ENABLE_RUNFILES": "1"},
"//conditions:default": {"ENABLE_RUNFILES": "0"},
}),
main = "pip_repository_annotations_test.py",
deps = [
"@pip_requests//:pkg",
"@pip_wheel//:pkg",
"@rules_python//python/runfiles",
],
)

config_setting(
name = "is_enable_runfiles_true",
values = {
"enable_runfiles": True,
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_data_exclude_glob(self):
# Because windows does not have `--enable_runfiles` on by default, the
# `runfiles.Rlocation` results will be different on this platform vs
# unix platforms. See `@rules_python//python/runfiles` for more details.
if platform.system() == "Windows":
if os.environ["ENABLE_RUNFILES"] == "1":
self.assertIsNotNone(metadata_path)
self.assertIsNone(wheel_path)
else:
Expand Down
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)
31 changes: 31 additions & 0 deletions python/private/py_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,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 +1806,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 +1860,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)
2 changes: 2 additions & 0 deletions python/private/stage2_bootstrap_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ def find_runfiles_root(main_rel_path):
else:
stub_filename = os.path.join(os.path.dirname(stub_filename), target)

# The `--enable_runfiles=false` flag is likely set, which isn't fully
# supported.
raise AssertionError("Cannot find .runfiles directory for %s" % sys.argv[0])


Expand Down
4 changes: 2 additions & 2 deletions tests/support/py_reconfig.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ def _perform_transition_impl(input_settings, attr, base_impl):
settings[labels.VENVS_USE_DECLARE_SYMLINK] = attr.venvs_use_declare_symlink
if attr.venvs_site_packages:
settings[labels.VENVS_SITE_PACKAGES] = attr.venvs_site_packages
for key, value in attr.config_settings.items():
settings[str(key)] = value
return settings

_BUILTIN_BUILD_PYTHON_ZIP = [] if config.bazel_10_or_later else [
"//command_line_option:build_python_zip",
]

_RECONFIG_INPUTS = [
"//command_line_option:build_runfile_links",
"//command_line_option:extra_toolchains",
"//command_line_option:stamp",
CUSTOM_RUNTIME,
labels.BOOTSTRAP_IMPL,
labels.PYTHON_SRC,
Expand Down