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 index b24cc876fb0edc..0776e945267712 100644 --- 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 @@ -14,13 +14,11 @@ package com.google.devtools.build.lib.bazel.rules.python; -import com.google.devtools.build.lib.rules.python.PyBinary; +import com.google.devtools.build.lib.rules.python.PyExecutable; import com.google.devtools.build.lib.rules.python.PythonSemantics; -/** - * Implementation of the {@code py_binary} rule for Bazel. - */ -public class BazelPyBinary extends PyBinary { +/** 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/BazelPyLibrary.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyLibrary.java index 8d0660d2dde851..5d2cfe95521606 100644 --- 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 @@ -17,9 +17,7 @@ import com.google.devtools.build.lib.rules.python.PyLibrary; import com.google.devtools.build.lib.rules.python.PythonSemantics; -/** - * Implementation of the {@code py_library} rule for Bazel. - */ +/** Bazel-specific implementation of the {@code py_library}. */ public class BazelPyLibrary extends PyLibrary { @Override protected PythonSemantics createSemantics() { 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 index 776ceb13e494a6..051ad8bb61fcd8 100644 --- 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 @@ -14,13 +14,11 @@ package com.google.devtools.build.lib.bazel.rules.python; -import com.google.devtools.build.lib.rules.python.PyTest; +import com.google.devtools.build.lib.rules.python.PyExecutable; import com.google.devtools.build.lib.rules.python.PythonSemantics; -/** - * Implementation of the {@code py_test} rule for Bazel. - */ -public class BazelPyTest extends PyTest { +/** 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/BazelPythonSemantics.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java index d22d1d78ec309e..f960b25a773414 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java @@ -222,8 +222,8 @@ private static Artifact createWindowsExeLauncher( } @Override - public void postInitBinary(RuleContext ruleContext, RunfilesSupport runfilesSupport, - PyCommon common) throws InterruptedException { + public void postInitExecutable( + RuleContext ruleContext, RunfilesSupport runfilesSupport, PyCommon common) { if (ruleContext.getFragment(PythonConfiguration.class).buildPythonZip()) { FilesToRunProvider zipper = ruleContext.getExecutablePrerequisite("$zipper", Mode.HOST); Artifact executable = common.getExecutable(); diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PyBinary.java b/src/main/java/com/google/devtools/build/lib/rules/python/PyExecutable.java similarity index 89% rename from src/main/java/com/google/devtools/build/lib/rules/python/PyBinary.java rename to src/main/java/com/google/devtools/build/lib/rules/python/PyExecutable.java index 23cad73ee40557..a737e8710267b9 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PyBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PyExecutable.java @@ -1,4 +1,4 @@ -// Copyright 2014 The Bazel Authors. All rights reserved. +// 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. @@ -11,6 +11,7 @@ // 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; @@ -30,13 +31,11 @@ import java.util.ArrayList; import java.util.List; -/** - * An implementation for the {@code py_binary} rule. - */ -public abstract class PyBinary implements RuleConfiguredTargetFactory { +/** Common implementation logic for {@code py_binary} and {@code py_test}. */ +public abstract class PyExecutable implements RuleConfiguredTargetFactory { + /** - * Create a {@link PythonSemantics} object that governs - * the behavior of this rule. + * Creates a pluggable semantics object to be used for the analysis of a target of this rule type. */ protected abstract PythonSemantics createSemantics(); @@ -44,17 +43,8 @@ public abstract class PyBinary implements RuleConfiguredTargetFactory { public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException, ActionConflictException { PyCommon common = new PyCommon(ruleContext); + PythonSemantics semantics = createSemantics(); - RuleConfiguredTargetBuilder builder = init(ruleContext, createSemantics(), common); - if (builder == null) { - return null; - } - return builder.build(); - } - - static RuleConfiguredTargetBuilder init( - RuleContext ruleContext, PythonSemantics semantics, PyCommon common) - throws InterruptedException, RuleErrorException { ruleContext.initConfigurationMakeVariableContext(new CcFlagsSupplier(ruleContext)); List srcs = common.validateSrcs(); @@ -122,14 +112,15 @@ static RuleConfiguredTargetBuilder init( new RuleConfiguredTargetBuilder(ruleContext); common.addCommonTransitiveInfoProviders(builder, semantics, common.getFilesToBuild(), imports); - semantics.postInitBinary(ruleContext, runfilesSupport, common); + semantics.postInitExecutable(ruleContext, runfilesSupport, common); return builder .setFilesToBuild(common.getFilesToBuild()) .add(RunfilesProvider.class, runfilesProvider) .setRunfilesSupport(runfilesSupport, realExecutable) .addNativeDeclaredProvider(new PyCcLinkParamsProvider(ccInfo)) - .add(PythonImportsProvider.class, new PythonImportsProvider(imports)); + .add(PythonImportsProvider.class, new PythonImportsProvider(imports)) + .build(); } private static Runfiles collectCommonRunfiles( 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 index 8e229a7d15f428..b36bd736768276 100644 --- 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 @@ -28,9 +28,7 @@ import java.util.ArrayList; import java.util.List; -/** - * An implementation for the {@code py_library} rule. - */ +/** Base implementation of {@code py_library}. */ public abstract class PyLibrary implements RuleConfiguredTargetFactory { /** @@ -40,7 +38,7 @@ public abstract class PyLibrary implements RuleConfiguredTargetFactory { protected abstract PythonSemantics createSemantics(); @Override - public ConfiguredTarget create(final RuleContext ruleContext) + public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException, ActionConflictException { PythonSemantics semantics = createSemantics(); PyCommon common = new PyCommon(ruleContext); diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PyTest.java b/src/main/java/com/google/devtools/build/lib/rules/python/PyTest.java deleted file mode 100644 index b398976c5bce7a..00000000000000 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PyTest.java +++ /dev/null @@ -1,45 +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.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; - -/** - * An implementation for {@code py_test} rules. - */ -public abstract class PyTest implements RuleConfiguredTargetFactory { - /** - * Create a {@link PythonSemantics} object that governs - * the behavior of this rule. - */ - protected abstract PythonSemantics createSemantics(); - - @Override - public ConfiguredTarget create(RuleContext ruleContext) - throws InterruptedException, RuleErrorException, ActionConflictException { - PythonSemantics semantics = createSemantics(); - PyCommon common = new PyCommon(ruleContext); - - RuleConfiguredTargetBuilder builder = PyBinary.init(ruleContext, semantics, common); - if (builder == null) { - return null; - } - return builder.build(); - } -} - diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PythonSemantics.java b/src/main/java/com/google/devtools/build/lib/rules/python/PythonSemantics.java index 1092675ea719e8..2f37f56570ae8b 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PythonSemantics.java +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PythonSemantics.java @@ -33,16 +33,23 @@ */ public interface PythonSemantics { /** - * Called at the beginning of the analysis of {@code py_binary} rules to validate its attributes. + * Called at the beginning of the analysis of {@code py_binary}, {@code py_test}, and {@code + * py_library} targets to validate their attributes. */ void validate(RuleContext ruleContext, PyCommon common); - /** Extends for the default and data runfiles of {@code py_binary} rules with custom elements. */ + /** + * Extends for the default and data runfiles of {@code py_binary} and {@code py_test} rules with + * custom elements. + */ void collectRunfilesForBinary( RuleContext ruleContext, Runfiles.Builder builder, PyCommon common, CcInfo ccInfo) throws InterruptedException, RuleErrorException; - /** Extends the default runfiles of {@code py_binary} rules with custom elements. */ + /** + * Extends the default runfiles of {@code py_binary} and {@code py_test} rules with custom + * elements. + */ void collectDefaultRunfilesForBinary(RuleContext ruleContext, Runfiles.Builder builder) throws InterruptedException; @@ -77,11 +84,12 @@ Artifact createExecutable( throws InterruptedException, RuleErrorException; /** - * Called at the end of the analysis of {@code py_binary} rules. + * Called at the end of the analysis of {@code py_binary} and {@code py_test} targets. + * * @throws InterruptedException */ - void postInitBinary(RuleContext ruleContext, RunfilesSupport runfilesSupport, - PyCommon common) throws InterruptedException; + void postInitExecutable(RuleContext ruleContext, RunfilesSupport runfilesSupport, PyCommon common) + throws InterruptedException; CcInfo buildCcInfoProvider(Iterable deps); }