Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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; |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 24 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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( |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 57 | env, actionOwner, ruleContext.getPackageDirectory(), purpose, filesToBuild, |
| 58 | ruleContext.getConfiguration().getMiddlemanDirectory( |
Kristina Chodorow | f8a1ae6 | 2016-08-11 14:44:40 +0000 | [diff] [blame] | 59 | ruleContext.getRule().getRepository()))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // TODO(bazel-team): remove this duplicated code after the ConfiguredTarget migration |
| 63 | /** |
| 64 | * Returns a middleman for all files to build for the given configured target. |
| 65 | * If multiple calls are made, then it returns the same artifact for |
| 66 | * configurations with the same internal directory. |
| 67 | * |
| 68 | * <p>The resulting middleman only aggregates the inputs and must be expanded |
| 69 | * before an action using it can be sent to a distributed execution-system. |
| 70 | */ |
| 71 | public static NestedSet<Artifact> getAggregatingMiddleman( |
| 72 | RuleContext ruleContext, String purpose, TransitiveInfoCollection dep) { |
| 73 | return NestedSetBuilder.wrap(Order.STABLE_ORDER, getMiddlemanInternal( |
| 74 | ruleContext.getAnalysisEnvironment(), ruleContext, ruleContext.getActionOwner(), purpose, |
| 75 | dep)); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Internal implementation for getAggregatingMiddleman / getAggregatingMiddlemanWithSolibSymlinks. |
| 80 | */ |
| 81 | private static List<Artifact> getMiddlemanInternal(AnalysisEnvironment env, |
| 82 | RuleContext ruleContext, ActionOwner actionOwner, String purpose, |
| 83 | TransitiveInfoCollection dep) { |
| 84 | if (dep == null) { |
| 85 | return ImmutableList.of(); |
| 86 | } |
| 87 | MiddlemanFactory factory = env.getMiddlemanFactory(); |
| 88 | Iterable<Artifact> artifacts = dep.getProvider(FileProvider.class).getFilesToBuild(); |
Damien Martin-Guillerez | d3a726c | 2015-07-29 17:51:37 +0000 | [diff] [blame] | 89 | return ImmutableList.of( |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 90 | factory.createMiddlemanAllowMultiple(env, actionOwner, ruleContext.getPackageDirectory(), |
Kristina Chodorow | f8a1ae6 | 2016-08-11 14:44:40 +0000 | [diff] [blame] | 91 | purpose, artifacts, ruleContext.getConfiguration().getMiddlemanDirectory( |
| 92 | ruleContext.getRule().getRepository()))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 93 | } |
| 94 | } |