From 38b278944f7f0131d4eb2e1863009b7aabc39e2d Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Tue, 15 Nov 2022 09:49:20 -0800 Subject: [PATCH] Expose Python configuration fragments to Starlark for Bazel Work towards #15897 PiperOrigin-RevId: 488684553 Change-Id: I6b439cf5b8fd75170e6647e4c76fa4ac8f72bdec --- .../devtools/build/lib/bazel/rules/python/BUILD | 1 + .../bazel/rules/python/BazelPythonConfiguration.java | 12 ++++++++---- .../build/lib/rules/python/PythonConfiguration.java | 8 ++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BUILD b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BUILD index 26da7689d97889..7493ceffc59c67 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BUILD +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BUILD @@ -49,6 +49,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/util:os", "//src/main/java/com/google/devtools/build/lib/vfs:pathfragment", "//src/main/java/com/google/devtools/common/options", + "//src/main/java/net/starlark/java/annot", "//third_party:guava", "//third_party:jsr305", ], diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonConfiguration.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonConfiguration.java index d4e5d2a569a3fc..537a212e4f02af 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonConfiguration.java @@ -30,9 +30,12 @@ import com.google.devtools.common.options.OptionDocumentationCategory; import com.google.devtools.common.options.OptionEffectTag; import com.google.devtools.common.options.OptionMetadataTag; +import net.starlark.java.annot.StarlarkBuiltin; +import net.starlark.java.annot.StarlarkMethod; /** Bazel-specific Python configuration. */ @Immutable +@StarlarkBuiltin(name = "bazel_py") @RequiresOptions(options = {BazelPythonConfiguration.Options.class, PythonOptions.class}) public class BazelPythonConfiguration extends Fragment { @@ -92,9 +95,7 @@ public static final class Options extends FragmentOptions { + "is less likely to experience import name collisions.") public boolean experimentalPythonImportAllRepositories; - /** - * Make Python configuration options available for host configurations as well - */ + /** Make Python configuration options available for host configurations as well */ @Override public FragmentOptions getHost() { return clone(); // host options are the same as target options @@ -151,8 +152,11 @@ public String getPythonPath() { return options.pythonPath == null ? "python" : options.pythonPath; } + @StarlarkMethod( + name = "python_import_all_repositories", + structField = true, + doc = "The value of the --experimental_python_import_all_repositories flag.") public boolean getImportAllRepositories() { return options.experimentalPythonImportAllRepositories; } - } diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PythonConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/python/PythonConfiguration.java index 596cf386f0991d..c60884146512dc 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PythonConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PythonConfiguration.java @@ -126,6 +126,10 @@ public String getOutputDirectoryName() { } /** Returns whether to build the executable zip file for Python binaries. */ + @StarlarkMethod( + name = "build_python_zip", + structField = true, + doc = "The effective value of --build_python_zip") public boolean buildPythonZip() { switch (buildPythonZip) { case YES: @@ -157,6 +161,10 @@ public boolean useToolchains() { return useToolchains; } + @StarlarkMethod( + name = "default_to_explicit_init_py", + structField = true, + doc = "The value from the --incompatible_default_to_explicit_init_py flag") /** * Returns true if executable Python rules should only write out empty __init__ files to their * runfiles tree when explicitly requested via {@code legacy_create_init}.