diff options
author | Lukacs Berki <lberki@google.com> | 2015-03-20 15:08:24 +0000 |
---|---|---|
committer | Damien Martin-Guillerez <dmarting@google.com> | 2015-03-23 11:51:25 +0000 |
commit | f02655fec3fdfe9a784d287fff53b886d91bae1c (patch) | |
tree | 7d6daa1bd5ac8b414692896ce553f0d073a6d923 /src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java | |
parent | c4cf9130e83b731becb287e898faf758c753f40a (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/PyLibrary.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java | 82 |
1 files changed, 82 insertions, 0 deletions
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 new file mode 100644 index 0000000000..89b4fc8c19 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java @@ -0,0 +1,82 @@ +// 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.devtools.build.lib.actions.Artifact; +import com.google.devtools.build.lib.analysis.ConfiguredTarget; +import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode; +import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder; +import com.google.devtools.build.lib.analysis.RuleContext; +import com.google.devtools.build.lib.analysis.Runfiles; +import com.google.devtools.build.lib.analysis.RunfilesProvider; +import com.google.devtools.build.lib.collect.nestedset.NestedSet; +import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; +import com.google.devtools.build.lib.collect.nestedset.Order; +import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory; +import com.google.devtools.build.lib.rules.cpp.CcLinkParams; +import com.google.devtools.build.lib.rules.cpp.CcLinkParamsProvider; +import com.google.devtools.build.lib.rules.cpp.CcLinkParamsStore; + +/** + * An implementation for the {@code py_library} rule. + */ +public abstract class PyLibrary implements RuleConfiguredTargetFactory { + + /** + * Create a {@link PythonSemantics} object that governs + * the behavior of this rule. + */ + protected abstract PythonSemantics createSemantics(); + + @Override + public ConfiguredTarget create(final RuleContext ruleContext) { + PythonSemantics semantics = createSemantics(); + PyCommon common = new PyCommon(ruleContext); + common.initCommon(common.getDefaultPythonVersion()); + common.validatePackageName(); + NestedSet<Artifact> filesToBuild = + NestedSetBuilder.wrap(Order.STABLE_ORDER, common.validateSrcs()); + common.addPyExtraActionPseudoAction(); + + CcLinkParamsStore ccLinkParamsStore = new CcLinkParamsStore() { + @Override + protected void collect(CcLinkParams.Builder builder, boolean linkingStatically, + boolean linkShared) { + builder.addTransitiveTargets(ruleContext.getPrerequisites("deps", Mode.TARGET)); + builder.addTransitiveLangTargets( + ruleContext.getPrerequisites("deps", Mode.TARGET), + PyCcLinkParamsProvider.TO_LINK_PARAMS); + } + }; + + Runfiles.Builder runfilesBuilder = new Runfiles.Builder(); + if (common.getConvertedFiles() != null) { + runfilesBuilder.addSymlinks(common.getConvertedFiles()); + } else { + runfilesBuilder.addTransitiveArtifacts(filesToBuild); + } + runfilesBuilder.setManifestExpander(PythonUtils.GET_INIT_PY_FILES); + runfilesBuilder.add(ruleContext, PythonRunfilesProvider.TO_RUNFILES); + runfilesBuilder.addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES); + + RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext); + common.addCommonTransitiveInfoProviders(builder, semantics, filesToBuild); + return builder + .setFilesToBuild(filesToBuild) + .add(RunfilesProvider.class, RunfilesProvider.simple(runfilesBuilder.build())) + .add(CcLinkParamsProvider.class, new CcLinkParamsProvider(ccLinkParamsStore)) + .build(); + } +} + |