aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/python/PyRuleClasses.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/PyRuleClasses.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/PyRuleClasses.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/python/PyRuleClasses.java18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PyRuleClasses.java b/src/main/java/com/google/devtools/build/lib/rules/python/PyRuleClasses.java
index 84f2372bbf..f24255b9c3 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/python/PyRuleClasses.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/python/PyRuleClasses.java
@@ -14,6 +14,10 @@
package com.google.devtools.build.lib.rules.python;
import com.google.devtools.build.lib.analysis.LanguageDependentFragment.LibraryLanguage;
+import com.google.devtools.build.lib.packages.RawAttributeMapper;
+import com.google.devtools.build.lib.packages.RuleClass;
+import com.google.devtools.build.lib.packages.RuleTransitionFactory;
+import com.google.devtools.build.lib.syntax.Type;
import com.google.devtools.build.lib.util.FileType;
/**
@@ -23,4 +27,18 @@ public class PyRuleClasses {
public static final FileType PYTHON_SOURCE = FileType.of(".py");
public static final LibraryLanguage LANGUAGE = new LibraryLanguage("Python");
+
+ /**
+ * Input for {@link RuleClass.Builder#cfg(RuleTransitionFactory)}: if
+ * {@link PythonOptions#forcePython} is unset, sets the Python version according to the rule's
+ * default Python version. Assumes the rule has the expected attribute for this setting.
+ *
+ * <p>Since this is a configuration transition, this propagates to the rules' transitive deps.
+ */
+ public static final RuleTransitionFactory DEFAULT_PYTHON_VERSION_TRANSITION =
+ (rule) ->
+ new PythonVersionTransition(
+ PythonVersion.parse(
+ RawAttributeMapper.of(rule).get("default_python_version", Type.STRING),
+ PythonVersion.ALL_VALUES));
}