aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/python/PythonVersionTransition.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/PythonVersionTransition.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/PythonVersionTransition.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/python/PythonVersionTransition.java60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PythonVersionTransition.java b/src/main/java/com/google/devtools/build/lib/rules/python/PythonVersionTransition.java
new file mode 100644
index 0000000000..b9ce9e22ee
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/python/PythonVersionTransition.java
@@ -0,0 +1,60 @@
+// Copyright 2017 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.analysis.config.BuildOptions;
+import com.google.devtools.build.lib.analysis.config.PatchTransition;
+
+/**
+ * A configuration transition that sets the Python version by setting
+ * {@link PythonOptions#forcePython}.
+ */
+public class PythonVersionTransition implements PatchTransition {
+ private final PythonVersion defaultVersion;
+
+ /**
+ * Creates a new transition that sets the given version if not already specified by
+ * {@link PythonOptions#forcePython}.
+ */
+ PythonVersionTransition(PythonVersion defaultVersion) {
+ this.defaultVersion = defaultVersion;
+ }
+
+ @Override
+ public BuildOptions apply(BuildOptions options) {
+ PythonOptions pyOptions = options.get(PythonOptions.class);
+ // The current Python version is either explicitly set by --force_python or a
+ // build-wide default.
+ PythonVersion currentVersion = pyOptions.getPythonVersion();
+ // The new Python version is either explicitly set by --force_python or this transition's
+ // default.
+ PythonVersion newVersion = pyOptions.getPythonVersion(defaultVersion);
+ if (currentVersion == newVersion) {
+ return options;
+ }
+
+ // forcePython must be one of PY2 or PY3 because these are the only values Blaze's output
+ // directories can safely distinguish. In other words, a configuration with forcePython=PY2
+ // would have the same output directory prefix as another with forcePython=PY2AND3, which is a
+ // major correctness failure.
+ //
+ // Even though this transition doesn't enforce the above, it only gets called on
+ // "default_python_version" attribute values, which happen to honor this. Proper enforcement is
+ // done in PythonConfiguration#getOutputDirectoryName.
+ BuildOptions newOptions = options.clone();
+ newOptions.get(PythonOptions.class).forcePython = newVersion;
+ return newOptions;
+ }
+}