Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1 | // Copyright 2014 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.analysis; |
| 16 | |
| 17 | import com.google.common.collect.ImmutableList; |
| 18 | import com.google.devtools.build.lib.actions.ActionOwner; |
| 19 | import com.google.devtools.build.lib.actions.Artifact; |
| 20 | import com.google.devtools.build.lib.actions.MiddlemanFactory; |
| 21 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
| 22 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
| 23 | import com.google.devtools.build.lib.collect.nestedset.Order; |
| 24 | |
| 25 | import java.util.List; |
| 26 | |
| 27 | /** |
| 28 | * A helper class for compilation helpers. |
| 29 | */ |
| 30 | public final class CompilationHelper { |
| 31 | /** |
| 32 | * Returns a middleman for all files to build for the given configured target. |
| 33 | * If multiple calls are made, then it returns the same artifact for |
| 34 | * configurations with the same internal directory. |
| 35 | * |
| 36 | * <p>The resulting middleman only aggregates the inputs and must be expanded |
| 37 | * before an action using it can be sent to a distributed execution-system. |
| 38 | */ |
| 39 | public static NestedSet<Artifact> getAggregatingMiddleman( |
| 40 | RuleContext ruleContext, String purpose, NestedSet<Artifact> filesToBuild) { |
| 41 | return NestedSetBuilder.wrap(Order.STABLE_ORDER, getMiddlemanInternal( |
| 42 | ruleContext.getAnalysisEnvironment(), ruleContext, ruleContext.getActionOwner(), purpose, |
| 43 | filesToBuild)); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Internal implementation for getAggregatingMiddleman / getAggregatingMiddlemanWithSolibSymlinks. |
| 48 | */ |
| 49 | private static List<Artifact> getMiddlemanInternal(AnalysisEnvironment env, |
| 50 | RuleContext ruleContext, ActionOwner actionOwner, String purpose, |
| 51 | NestedSet<Artifact> filesToBuild) { |
| 52 | if (filesToBuild == null) { |
| 53 | return ImmutableList.of(); |
| 54 | } |
| 55 | MiddlemanFactory factory = env.getMiddlemanFactory(); |
| 56 | return ImmutableList.of(factory.createMiddlemanAllowMultiple( |
Damien Martin-Guillerez | d3a726c | 2015-07-29 17:51:37 +0000 | [diff] [blame] | 57 | env, actionOwner, ruleContext.getPackageDirectory(), purpose, filesToBuild, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 58 | ruleContext.getConfiguration().getMiddlemanDirectory())); |
| 59 | } |
| 60 | |
| 61 | // TODO(bazel-team): remove this duplicated code after the ConfiguredTarget migration |
| 62 | /** |
| 63 | * Returns a middleman for all files to build for the given configured target. |
| 64 | * If multiple calls are made, then it returns the same artifact for |
| 65 | * configurations with the same internal directory. |
| 66 | * |
| 67 | * <p>The resulting middleman only aggregates the inputs and must be expanded |
| 68 | * before an action using it can be sent to a distributed execution-system. |
| 69 | */ |
| 70 | public static NestedSet<Artifact> getAggregatingMiddleman( |
| 71 | RuleContext ruleContext, String purpose, TransitiveInfoCollection dep) { |
| 72 | return NestedSetBuilder.wrap(Order.STABLE_ORDER, getMiddlemanInternal( |
| 73 | ruleContext.getAnalysisEnvironment(), ruleContext, ruleContext.getActionOwner(), purpose, |
| 74 | dep)); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Internal implementation for getAggregatingMiddleman / getAggregatingMiddlemanWithSolibSymlinks. |
| 79 | */ |
| 80 | private static List<Artifact> getMiddlemanInternal(AnalysisEnvironment env, |
| 81 | RuleContext ruleContext, ActionOwner actionOwner, String purpose, |
| 82 | TransitiveInfoCollection dep) { |
| 83 | if (dep == null) { |
| 84 | return ImmutableList.of(); |
| 85 | } |
| 86 | MiddlemanFactory factory = env.getMiddlemanFactory(); |
| 87 | Iterable<Artifact> artifacts = dep.getProvider(FileProvider.class).getFilesToBuild(); |
Damien Martin-Guillerez | d3a726c | 2015-07-29 17:51:37 +0000 | [diff] [blame] | 88 | return ImmutableList.of( |
| 89 | factory.createMiddlemanAllowMultiple(env, actionOwner, ruleContext.getPackageDirectory(), |
| 90 | purpose, artifacts, ruleContext.getConfiguration().getMiddlemanDirectory())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 91 | } |
| 92 | } |