aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/python/PythonConfiguration.java
diff options
context:
space:
mode:
authorGravatar gregce <gregce@google.com>2017-10-19 20:37:40 +0200
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2017-10-20 14:04:05 +0200
commit8b5bf1f440ef8d140eb3843861fbce65709438ea (patch)
treeadd463ed197b886232382968101bef0e68cfa9ae /src/main/java/com/google/devtools/build/lib/rules/python/PythonConfiguration.java
parent70e0b7a6b567d5f6c189ad044e8965e17b729ad4 (diff)
Support Python 2 and 3 in the same build.
This is also an example of the versatility of dynamic configurations: this feature only requires changes to rules/python/... Issue #3871 PiperOrigin-RevId: 172775001
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/python/PythonConfiguration.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/python/PythonConfiguration.java30
1 files changed, 30 insertions, 0 deletions
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 1feb6ec040..bc0555790f 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
@@ -13,11 +13,19 @@
// limitations under the License.
package com.google.devtools.build.lib.rules.python;
+import com.google.common.base.Joiner;
+import com.google.common.base.Verify;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
+import com.google.devtools.build.lib.analysis.config.BuildOptions;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
+import com.google.devtools.build.lib.events.Event;
+import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.common.options.TriState;
+import java.util.Arrays;
+import java.util.List;
+
/**
* The configuration fragment containing information about the various pieces of infrastructure
* needed to run Python compilations.
@@ -52,9 +60,31 @@ public class PythonConfiguration extends BuildConfiguration.Fragment {
@Override
public String getOutputDirectoryName() {
+ List<PythonVersion> allowedVersions = Arrays.asList(PythonVersion.TARGET_PYTHON_VALUES);
+ Verify.verify(
+ allowedVersions.size() == 2, // If allowedVersions.size() == 1, we don't need this method.
+ ">2 possible defaultPythonVersion values makes output directory clashes possible");
+ // Skip this check if --force_python is set. That's because reportInvalidOptions reports
+ // bad --force_python settings with a clearer user error (and Bazel's configuration
+ // initialization logic calls reportInvalidOptions after this method).
+ if (!ignorePythonVersionAttribute && !allowedVersions.contains(defaultPythonVersion)) {
+ throw new IllegalStateException(
+ String.format("defaultPythonVersion=%s not allowed: must be in %s to prevent output "
+ + "directory clashes", defaultPythonVersion, Joiner.on(", ").join(allowedVersions)));
+ }
return (defaultPythonVersion == PythonVersion.PY3) ? "py3" : null;
}
+ @Override
+ public void reportInvalidOptions(EventHandler reporter, BuildOptions buildOptions) {
+ PythonOptions pythonOptions = buildOptions.get(PythonOptions.class);
+ if (pythonOptions.forcePython != null
+ && pythonOptions.forcePython != PythonVersion.PY2
+ && pythonOptions.forcePython != PythonVersion.PY3) {
+ reporter.handle(Event.error("'--force_python' argument must be 'PY2' or 'PY3'"));
+ }
+ }
+
/** Returns whether to build the executable zip file for Python binaries. */
public boolean buildPythonZip() {
switch (buildPythonZip) {