Skip to content

Commit 3b7c956

Browse files
committed
Starlarkify python flags
1 parent ec1df01 commit 3b7c956

File tree

12 files changed

+59
-11
lines changed

12 files changed

+59
-11
lines changed

.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ test --test_output=errors
1616
# creating (possibly empty) __init__.py files and adding them to the srcs of
1717
# Python targets as required.
1818
build --incompatible_default_to_explicit_init_py
19+
build --//python/config_settings:incompatible_default_to_explicit_init_py=True
1920

2021
# Ensure ongoing compatibility with this flag.
2122
common --incompatible_disallow_struct_provider_syntax

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ bazel_dep(name = "bazel_features", version = "1.21.0")
88
bazel_dep(name = "bazel_skylib", version = "1.8.1")
99
bazel_dep(name = "rules_cc", version = "0.1.5")
1010
bazel_dep(name = "platforms", version = "0.0.11")
11+
bazel_dep(name = "abseil-cpp", version = "20250814.1")
1112

1213
# Those are loaded only when using py_proto_library
1314
# Use py_proto_library directly from protobuf repository

gazelle/.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ test --test_output=errors
77
# creating (possibly empty) __init__.py files and adding them to the srcs of
88
# Python targets as required.
99
build --incompatible_default_to_explicit_init_py
10+
build --//python/config_settings:incompatible_default_to_explicit_init_py=True
1011

1112
# Windows makes use of runfiles for some rules
1213
build --enable_runfiles

python/config_settings/BUILD.bazel

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")
22
load("@pythons_hub//:versions.bzl", "DEFAULT_PYTHON_VERSION", "MINOR_MAPPING", "PYTHON_VERSIONS")
3+
load("@rules_python_internal//:rules_python_config.bzl", "config")
34
load(
45
"//python/private:flags.bzl",
56
"AddSrcsToRunfilesFlag",
@@ -244,5 +245,28 @@ label_flag(
244245
bool_flag(
245246
name = "experimental_python_import_all_repositories",
246247
build_setting_default = True,
248+
scope = "universal",
249+
visibility = ["//visibility:public"],
250+
)
251+
252+
bool_flag(
253+
name = "build_python_zip",
254+
build_setting_default = config.build_python_zip_default,
255+
help = "Build python executable zip. Defaults to on on Windows, off on other platforms",
256+
scope = "universal",
257+
visibility = ["//visibility:public"],
258+
)
259+
260+
bool_flag(
261+
name = "incompatible_default_to_explicit_init_py",
262+
build_setting_default = False,
263+
scope = "universal",
264+
visibility = ["//visibility:public"],
265+
)
266+
267+
string_flag(
268+
name = "python_path",
269+
build_setting_default = "python",
270+
scope = "universal",
247271
visibility = ["//visibility:public"],
248272
)

python/private/attributes.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ COMMON_ATTRS = dicts.add(
179179
# buildifier: disable=attr-license
180180
"licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
181181
},
182+
# starlark flags attributes
183+
{
184+
"_python_path": attr.label(default = "//python/config_settings:python_path"),
185+
"_default_to_explicit_init_py": attr.label(default = "//python/config_settings:incompatible_default_to_explicit_init_py"),
186+
"_python_import_all_repositories": attr.label(default = "//python/config_settings:experimental_python_import_all_repositories"),
187+
"_build_python_zip": attr.label(default = "//python/config_settings:build_python_zip"),
188+
},
182189
)
183190

184191
IMPORTS_ATTRS = {

python/private/common_labels.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ labels = struct(
77
# keep sorted
88
ADD_SRCS_TO_RUNFILES = str(Label("//python/config_settings:add_srcs_to_runfiles")),
99
BOOTSTRAP_IMPL = str(Label("//python/config_settings:bootstrap_impl")),
10+
BUILD_PYTHON_ZIP = str(Label("//python/config_settings:build_python_zip")),
1011
EXEC_TOOLS_TOOLCHAIN = str(Label("//python/config_settings:exec_tools_toolchain")),
1112
PIP_ENV_MARKER_CONFIG = str(Label("//python/config_settings:pip_env_marker_config")),
1213
NONE = str(Label("//python:none")),

python/private/internal_config_repo.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ _ENABLE_DEPRECATION_WARNINGS_DEFAULT = "0"
2828

2929
_CONFIG_TEMPLATE = """
3030
config = struct(
31+
build_python_zip_default = {build_python_zip_default},
3132
enable_pystar = True,
3233
enable_pipstar = {enable_pipstar},
3334
enable_deprecation_warnings = {enable_deprecation_warnings},
@@ -96,6 +97,7 @@ def _internal_config_repo_impl(rctx):
9697
builtin_py_cc_link_params_provider = "PyCcLinkParamsProvider"
9798

9899
rctx.file("rules_python_config.bzl", _CONFIG_TEMPLATE.format(
100+
build_python_zip_default = repo_utils.get_platforms_os_name(rctx) == "windows",
99101
enable_pipstar = _bool_from_environ(rctx, _ENABLE_PIPSTAR_ENVVAR_NAME, _ENABLE_PIPSTAR_DEFAULT),
100102
enable_deprecation_warnings = _bool_from_environ(rctx, _ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME, _ENABLE_DEPRECATION_WARNINGS_DEFAULT),
101103
builtin_py_info_symbol = builtin_py_info_symbol,

tests/base_rules/py_executable_base_tests.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ def _test_basic_windows(name, config):
4444
# the target platform. For windows, it defaults to true, so force
4545
# it to that to match behavior when this test runs on other
4646
# platforms.
47+
# Pass value to both native and starlark versions of the flag until
48+
# the native one is removed.
4749
"//command_line_option:build_python_zip": "true",
50+
labels.BUILD_PYTHON_ZIP: True,
4851
"//command_line_option:cpu": "windows_x86_64",
4952
"//command_line_option:crosstool_top": CROSSTOOL_TOP,
5053
"//command_line_option:extra_execution_platforms": [platform_targets.WINDOWS_X86_64],
@@ -87,7 +90,10 @@ def _test_basic_zip(name, config):
8790
# the target platform. For windows, it defaults to true, so force
8891
# it to that to match behavior when this test runs on other
8992
# platforms.
93+
# Pass value to both native and starlark versions of the flag until
94+
# the native one is removed.
9095
"//command_line_option:build_python_zip": "true",
96+
labels.BUILD_PYTHON_ZIP: True,
9197
"//command_line_option:cpu": "linux_x86_64",
9298
"//command_line_option:crosstool_top": CROSSTOOL_TOP,
9399
"//command_line_option:extra_execution_platforms": [platform_targets.LINUX_X86_64],

tests/bootstrap_impls/BUILD.bazel

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ py_reconfig_binary(
2323
srcs = ["bin.py"],
2424
bootstrap_impl = "script",
2525
# Force it to not be self-executable
26-
build_python_zip = "no",
26+
build_python_zip = False,
2727
main = "bin.py",
2828
target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT,
2929
)
@@ -50,14 +50,14 @@ sh_test(
5050

5151
sh_py_run_test(
5252
name = "run_binary_zip_no_test",
53-
build_python_zip = "no",
53+
build_python_zip = False,
5454
py_src = "bin.py",
5555
sh_src = "run_binary_zip_no_test.sh",
5656
)
5757

5858
sh_py_run_test(
5959
name = "run_binary_zip_yes_test",
60-
build_python_zip = "yes",
60+
build_python_zip = True,
6161
py_src = "bin.py",
6262
sh_src = "run_binary_zip_yes_test.sh",
6363
)
@@ -81,7 +81,7 @@ sh_py_run_test(
8181
sh_py_run_test(
8282
name = "run_binary_bootstrap_script_zip_yes_test",
8383
bootstrap_impl = "script",
84-
build_python_zip = "yes",
84+
build_python_zip = True,
8585
py_src = "bin.py",
8686
sh_src = "run_binary_zip_yes_test.sh",
8787
target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT,
@@ -90,7 +90,7 @@ sh_py_run_test(
9090
sh_py_run_test(
9191
name = "run_binary_bootstrap_script_zip_no_test",
9292
bootstrap_impl = "script",
93-
build_python_zip = "no",
93+
build_python_zip = False,
9494
py_src = "bin.py",
9595
sh_src = "run_binary_zip_no_test.sh",
9696
target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT,

tests/config_settings/transition/multi_version_tests.bzl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ load("@rules_testing//lib:util.bzl", rt_util = "util")
2020
load("//python:py_binary.bzl", "py_binary")
2121
load("//python:py_info.bzl", "PyInfo")
2222
load("//python:py_test.bzl", "py_test")
23+
load("//python/private:common_labels.bzl", "labels") # buildifier: disable=bzl-visibility
2324
load("//python/private:reexports.bzl", "BuiltinPyInfo") # buildifier: disable=bzl-visibility
2425
load("//tests/support:support.bzl", "CC_TOOLCHAIN")
2526
load("//tests/support/platforms:platforms.bzl", "platform_targets")
@@ -91,7 +92,8 @@ def _setup_py_binary_windows(name, *, impl, build_python_zip):
9192
target = name + "_subject",
9293
impl = impl,
9394
config_settings = {
94-
"//command_line_option:build_python_zip": build_python_zip,
95+
"//command_line_option:build_python_zip": str(build_python_zip),
96+
labels.BUILD_PYTHON_ZIP: build_python_zip,
9597
"//command_line_option:extra_toolchains": CC_TOOLCHAIN,
9698
"//command_line_option:platforms": str(platform_targets.WINDOWS_X86_64),
9799
},
@@ -100,7 +102,7 @@ def _setup_py_binary_windows(name, *, impl, build_python_zip):
100102
def _test_py_binary_windows_build_python_zip_false(name):
101103
_setup_py_binary_windows(
102104
name,
103-
build_python_zip = "false",
105+
build_python_zip = False,
104106
impl = _test_py_binary_windows_build_python_zip_false_impl,
105107
)
106108

@@ -121,7 +123,7 @@ _tests.append(_test_py_binary_windows_build_python_zip_false)
121123
def _test_py_binary_windows_build_python_zip_true(name):
122124
_setup_py_binary_windows(
123125
name,
124-
build_python_zip = "true",
126+
build_python_zip = True,
125127
impl = _test_py_binary_windows_build_python_zip_true_impl,
126128
)
127129

0 commit comments

Comments
 (0)