aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/python/PythonConfigurationLoader.java
diff options
context:
space:
mode:
authorGravatar Lukacs Berki <lberki@google.com>2015-03-20 15:08:24 +0000
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2015-03-23 11:51:25 +0000
commitf02655fec3fdfe9a784d287fff53b886d91bae1c (patch)
tree7d6daa1bd5ac8b414692896ce553f0d073a6d923 /src/main/java/com/google/devtools/build/lib/rules/python/PythonConfigurationLoader.java
parentc4cf9130e83b731becb287e898faf758c753f40a (diff)
Initial checkin of Python rules into Bazel.
-- MOS_MIGRATED_REVID=89123900
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/python/PythonConfigurationLoader.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/python/PythonConfigurationLoader.java79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PythonConfigurationLoader.java b/src/main/java/com/google/devtools/build/lib/rules/python/PythonConfigurationLoader.java
new file mode 100644
index 0000000000..2acfe17981
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/python/PythonConfigurationLoader.java
@@ -0,0 +1,79 @@
+// Copyright 2014 Google Inc. 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.base.Function;
+import com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment;
+import com.google.devtools.build.lib.analysis.config.BuildOptions;
+import com.google.devtools.build.lib.analysis.config.ConfigurationEnvironment;
+import com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory;
+import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException;
+import com.google.devtools.build.lib.rules.cpp.CppConfiguration;
+import com.google.devtools.build.lib.rules.cpp.CppOptions;
+import com.google.devtools.build.lib.rules.cpp.CrosstoolConfigurationLoader;
+import com.google.devtools.build.lib.syntax.Label;
+import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig;
+
+import javax.annotation.Nullable;
+
+/**
+ * A factory implementation for {@link PythonConfiguration} objects.
+ */
+public class PythonConfigurationLoader implements ConfigurationFragmentFactory {
+ private final Function<String, String> cpuTransformer;
+
+ public PythonConfigurationLoader(Function<String, String> cpuTransformer) {
+ this.cpuTransformer = cpuTransformer;
+ }
+
+ @Nullable
+ private CrosstoolConfig.CToolchain getToolchain(
+ ConfigurationEnvironment env, BuildOptions buildOptions, Label crosstoolTop)
+ throws InvalidConfigurationException {
+ CrosstoolConfigurationLoader.CrosstoolFile file =
+ CrosstoolConfigurationLoader.readCrosstool(env, crosstoolTop);
+ if (file == null) {
+ return null;
+ }
+ return CrosstoolConfigurationLoader.selectToolchain(
+ file.getProto(), buildOptions, cpuTransformer);
+ }
+
+ @Override
+ public PythonConfiguration create(ConfigurationEnvironment env, BuildOptions buildOptions)
+ throws InvalidConfigurationException {
+ PythonOptions pythonOptions = buildOptions.get(PythonOptions.class);
+ CppConfiguration cppConfiguration = env.getFragment(buildOptions, CppConfiguration.class);
+ if (cppConfiguration == null) {
+ return null;
+ }
+
+ CrosstoolConfig.CToolchain toolchain = getToolchain(
+ env, buildOptions, buildOptions.get(CppOptions.class).crosstoolTop);
+ if (toolchain == null) {
+ return null;
+ }
+
+ boolean ignorePythonVersionAttribute = pythonOptions.forcePython != null;
+ PythonVersion pythonVersion = pythonOptions.getPythonVersion();
+
+ return new PythonConfiguration(pythonVersion, ignorePythonVersionAttribute);
+ }
+
+ @Override
+ public Class<? extends Fragment> creates() {
+ return PythonConfiguration.class;
+ }
+}
+