From 3ce2b7143d6b5dc094939509bae8e9cc98739ce6 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Mon, 6 Mar 2023 11:04:08 -0800 Subject: [PATCH] python: Remove Java implementation of Python rules This is to make transitioning the providers to Starlark easier. Work towards #15897 PiperOrigin-RevId: 514463791 Change-Id: Iccd11f4750d3cf5ca5ed1b0ff8427d6580adcecc --- .../lib/bazel/rules/python/BazelPyBinary.java | 26 --- .../bazel/rules/python/BazelPyBinaryRule.java | 2 +- .../bazel/rules/python/BazelPyLibrary.java | 26 --- .../rules/python/BazelPyLibraryRule.java | 3 +- .../lib/bazel/rules/python/BazelPyTest.java | 26 --- .../bazel/rules/python/BazelPyTestRule.java | 2 +- .../build/lib/rules/python/PyExecutable.java | 176 ------------------ .../build/lib/rules/python/PyLibrary.java | 77 -------- 8 files changed, 4 insertions(+), 334 deletions(-) delete mode 100644 src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyBinary.java delete mode 100644 src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyLibrary.java delete mode 100644 src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyTest.java delete mode 100644 src/main/java/com/google/devtools/build/lib/rules/python/PyExecutable.java delete mode 100644 src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyBinary.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyBinary.java deleted file mode 100644 index 0776e945267712..00000000000000 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyBinary.java +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2014 The Bazel Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.google.devtools.build.lib.bazel.rules.python; - -import com.google.devtools.build.lib.rules.python.PyExecutable; -import com.google.devtools.build.lib.rules.python.PythonSemantics; - -/** Bazel-specific implementation of {@code py_binary}. */ -public class BazelPyBinary extends PyExecutable { - @Override - protected PythonSemantics createSemantics() { - return new BazelPythonSemantics(); - } -} diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyBinaryRule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyBinaryRule.java index da7a03bf1d0e7e..43f72fc5966686 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyBinaryRule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyBinaryRule.java @@ -58,7 +58,7 @@ public Metadata getMetadata() { return RuleDefinition.Metadata.builder() .name("py_binary") .ancestors(PyBinaryBaseRule.class, BaseRuleClasses.BinaryBaseRule.class) - .factoryClass(BazelPyBinary.class) + .factoryClass(BaseRuleClasses.EmptyRuleConfiguredTargetFactory.class) .build(); } } diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyLibrary.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyLibrary.java deleted file mode 100644 index 5d2cfe95521606..00000000000000 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyLibrary.java +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2014 The Bazel Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.google.devtools.build.lib.bazel.rules.python; - -import com.google.devtools.build.lib.rules.python.PyLibrary; -import com.google.devtools.build.lib.rules.python.PythonSemantics; - -/** Bazel-specific implementation of the {@code py_library}. */ -public class BazelPyLibrary extends PyLibrary { - @Override - protected PythonSemantics createSemantics() { - return new BazelPythonSemantics(); - } -} diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyLibraryRule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyLibraryRule.java index 5af1d94b68d0a2..832f6df39e021a 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyLibraryRule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyLibraryRule.java @@ -17,6 +17,7 @@ import static com.google.devtools.build.lib.packages.Attribute.attr; import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST; +import com.google.devtools.build.lib.analysis.BaseRuleClasses; import com.google.devtools.build.lib.analysis.RuleDefinition; import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment; import com.google.devtools.build.lib.bazel.rules.python.BazelPyRuleClasses.PyBaseRule; @@ -49,7 +50,7 @@ public Metadata getMetadata() { return RuleDefinition.Metadata.builder() .name("py_library") .ancestors(PyBaseRule.class) - .factoryClass(BazelPyLibrary.class) + .factoryClass(BaseRuleClasses.EmptyRuleConfiguredTargetFactory.class) .build(); } } diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyTest.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyTest.java deleted file mode 100644 index 051ad8bb61fcd8..00000000000000 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyTest.java +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.google.devtools.build.lib.bazel.rules.python; - -import com.google.devtools.build.lib.rules.python.PyExecutable; -import com.google.devtools.build.lib.rules.python.PythonSemantics; - -/** Bazel-specific implementation of {@code py_test}. */ -public class BazelPyTest extends PyExecutable { - @Override - protected PythonSemantics createSemantics() { - return new BazelPythonSemantics(); - } -} diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyTestRule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyTestRule.java index 0ebab840205e60..6075ece52fb33f 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyTestRule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyTestRule.java @@ -77,7 +77,7 @@ public Metadata getMetadata() { .name("py_test") .type(RuleClassType.TEST) .ancestors(PyBinaryBaseRule.class, BaseRuleClasses.TestBaseRule.class) - .factoryClass(BazelPyTest.class) + .factoryClass(BaseRuleClasses.EmptyRuleConfiguredTargetFactory.class) .build(); } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PyExecutable.java b/src/main/java/com/google/devtools/build/lib/rules/python/PyExecutable.java deleted file mode 100644 index 717b347d2a847e..00000000000000 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PyExecutable.java +++ /dev/null @@ -1,176 +0,0 @@ -// Copyright 2018 The Bazel Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.google.devtools.build.lib.rules.python; - -import com.google.common.collect.ImmutableMap; -import com.google.devtools.build.lib.actions.Artifact; -import com.google.devtools.build.lib.actions.ExecutionRequirements; -import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException; -import com.google.devtools.build.lib.analysis.ConfiguredTarget; -import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder; -import com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory; -import com.google.devtools.build.lib.analysis.RuleContext; -import com.google.devtools.build.lib.analysis.Runfiles; -import com.google.devtools.build.lib.analysis.RunfilesProvider; -import com.google.devtools.build.lib.analysis.RunfilesSupport; -import com.google.devtools.build.lib.analysis.test.ExecutionInfo; -import com.google.devtools.build.lib.packages.BuildType; -import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException; -import com.google.devtools.build.lib.packages.TargetUtils; -import com.google.devtools.build.lib.packages.TriState; -import com.google.devtools.build.lib.rules.apple.ApplePlatform; -import com.google.devtools.build.lib.rules.cpp.CcCommon.CcFlagsSupplier; -import com.google.devtools.build.lib.rules.cpp.CcInfo; -import java.util.ArrayList; -import java.util.List; -import javax.annotation.Nullable; - -/** Common implementation logic for {@code py_binary} and {@code py_test}. */ -public abstract class PyExecutable implements RuleConfiguredTargetFactory { - - /** - * Creates a pluggable semantics object to be used for the analysis of a target of this rule type. - */ - protected abstract PythonSemantics createSemantics(); - - @Override - @Nullable - public ConfiguredTarget create(RuleContext ruleContext) - throws InterruptedException, RuleErrorException, ActionConflictException { - // Init the make variable context first. Otherwise it may be incorrectly initialized by default - // inside semantics/common via {@link RuleContext#getExpander}. - ruleContext.initConfigurationMakeVariableContext(new CcFlagsSupplier(ruleContext)); - - PythonSemantics semantics = createSemantics(); - PyCommon common = - new PyCommon(ruleContext, semantics, /*validateSources=*/ true, /*requiresMainFile=*/ true); - - List srcs = common.getPythonSources(); - List allOutputs = - new ArrayList<>(semantics.precompiledPythonFiles(ruleContext, srcs, common)); - if (ruleContext.hasErrors()) { - return null; - } - - common.initBinary(allOutputs); - semantics.validate(ruleContext, common); - if (ruleContext.hasErrors()) { - return null; - } - - CcInfo ccInfo = - semantics.buildCcInfoProvider(ruleContext, ruleContext.getPrerequisites("deps")); - - Runfiles commonRunfiles = collectCommonRunfiles(ruleContext, common, semantics, ccInfo); - - Runfiles.Builder defaultRunfilesBuilder = - new Runfiles.Builder( - ruleContext.getWorkspaceName(), - ruleContext.getConfiguration().legacyExternalRunfiles()) - .merge(commonRunfiles); - semantics.collectDefaultRunfilesForBinary(ruleContext, common, defaultRunfilesBuilder); - - common.createExecutable(ccInfo, defaultRunfilesBuilder); - - Runfiles defaultRunfiles = defaultRunfilesBuilder.build(); - - RunfilesSupport runfilesSupport = - RunfilesSupport.withExecutable(ruleContext, defaultRunfiles, common.getExecutable()); - - if (ruleContext.hasErrors()) { - return null; - } - - Runfiles dataRunfiles; - if (ruleContext.getFragment(PythonConfiguration.class).buildTransitiveRunfilesTrees()) { - // Only include common runfiles and middleman. Default runfiles added by semantics are - // excluded. The middleman is necessary to ensure the runfiles trees are generated for all - // dependency binaries. - dataRunfiles = - new Runfiles.Builder( - ruleContext.getWorkspaceName(), - ruleContext.getConfiguration().legacyExternalRunfiles()) - .merge(commonRunfiles) - .addLegacyExtraMiddleman(runfilesSupport.getRunfilesMiddleman()) - .build(); - } else { - dataRunfiles = commonRunfiles; - } - - RunfilesProvider runfilesProvider = RunfilesProvider.withData(defaultRunfiles, dataRunfiles); - - RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext); - common.addCommonTransitiveInfoProviders(builder); - - semantics.postInitExecutable(ruleContext, runfilesSupport, common, builder); - - // Support test execution on darwin. - if (ApplePlatform.isApplePlatform(ruleContext.getConfiguration().getCpu()) - && TargetUtils.isTestRule(ruleContext.getRule())) { - builder.addNativeDeclaredProvider( - new ExecutionInfo(ImmutableMap.of(ExecutionRequirements.REQUIRES_DARWIN, ""))); - } - - return builder - .setFilesToBuild(common.getFilesToBuild()) - .add(RunfilesProvider.class, runfilesProvider) - .setRunfilesSupport(runfilesSupport, common.getExecutable()) - .addNativeDeclaredProvider(new PyCcLinkParamsProvider(ccInfo)) - .build(); - } - - /** - * If requested, creates empty __init__.py files for each manifest file. - * - *

We do this if the rule defines {@code legacy_create_init} and its value is true. Auto is - * treated as false iff {@code --incompatible_default_to_explicit_init_py} is given. - * - *

See {@link PythonUtils#getInitPyFiles} for details about how the files are created. - */ - private static void maybeCreateInitFiles( - RuleContext ruleContext, Runfiles.Builder builder, PythonSemantics semantics) { - boolean createFiles; - if (!ruleContext.attributes().has("legacy_create_init", BuildType.TRISTATE)) { - createFiles = true; - } else { - TriState legacy = ruleContext.attributes().get("legacy_create_init", BuildType.TRISTATE); - if (legacy == TriState.AUTO) { - createFiles = !ruleContext.getFragment(PythonConfiguration.class).defaultToExplicitInitPy(); - } else { - createFiles = legacy != TriState.NO; - } - } - if (createFiles) { - builder.setEmptyFilesSupplier(semantics.getEmptyRunfilesSupplier()); - } - } - - private static Runfiles collectCommonRunfiles( - RuleContext ruleContext, PyCommon common, PythonSemantics semantics, CcInfo ccInfo) - throws InterruptedException, RuleErrorException { - Runfiles.Builder builder = - new Runfiles.Builder( - ruleContext.getWorkspaceName(), - ruleContext.getConfiguration().legacyExternalRunfiles()); - builder.addArtifact(common.getExecutable()); - builder.addTransitiveArtifacts(common.getFilesToBuild()); - builder.addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES); - - maybeCreateInitFiles(ruleContext, builder, semantics); - - semantics.collectRunfilesForBinary(ruleContext, builder, common, ccInfo); - return builder.build(); - } -} diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java deleted file mode 100644 index 52847372b1824d..00000000000000 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright 2014 The Bazel Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package com.google.devtools.build.lib.rules.python; - -import com.google.devtools.build.lib.actions.Artifact; -import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException; -import com.google.devtools.build.lib.analysis.ConfiguredTarget; -import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder; -import com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory; -import com.google.devtools.build.lib.analysis.RuleContext; -import com.google.devtools.build.lib.analysis.Runfiles; -import com.google.devtools.build.lib.analysis.RunfilesProvider; -import com.google.devtools.build.lib.collect.nestedset.NestedSet; -import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; -import com.google.devtools.build.lib.collect.nestedset.Order; -import java.util.ArrayList; -import java.util.List; -import javax.annotation.Nullable; - -/** Base implementation of {@code py_library}. */ -public abstract class PyLibrary implements RuleConfiguredTargetFactory { - - /** - * Create a {@link PythonSemantics} object that governs - * the behavior of this rule. - */ - protected abstract PythonSemantics createSemantics(); - - @Override - @Nullable - public ConfiguredTarget create(RuleContext ruleContext) - throws InterruptedException, RuleErrorException, ActionConflictException { - PythonSemantics semantics = createSemantics(); - PyCommon common = - new PyCommon( - ruleContext, semantics, /*validateSources=*/ true, /*requiresMainFile=*/ false); - semantics.validate(ruleContext, common); - - List srcs = common.getPythonSources(); - List allOutputs = - new ArrayList<>(semantics.precompiledPythonFiles(ruleContext, srcs, common)); - if (ruleContext.hasErrors()) { - return null; - } - - NestedSet filesToBuild = - NestedSetBuilder.wrap(Order.STABLE_ORDER, allOutputs); - common.addPyExtraActionPseudoAction(); - - Runfiles.Builder runfilesBuilder = new Runfiles.Builder( - ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()); - runfilesBuilder.addTransitiveArtifacts(filesToBuild); - runfilesBuilder.addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES); - - RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext); - common.addCommonTransitiveInfoProviders(builder); - - return builder - .setFilesToBuild(filesToBuild) - .addNativeDeclaredProvider( - new PyCcLinkParamsProvider( - semantics.buildCcInfoProvider(ruleContext, ruleContext.getPrerequisites("deps")))) - .add(RunfilesProvider.class, RunfilesProvider.simple(runfilesBuilder.build())) - .build(); - } -}