blob: 8a1fddc6ff544adabffc9b98da9a1beb7162d95f [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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
15package com.google.devtools.build.lib.analysis;
16
17import com.google.common.collect.ImmutableList;
18import com.google.devtools.build.lib.actions.ActionOwner;
19import com.google.devtools.build.lib.actions.Artifact;
20import com.google.devtools.build.lib.actions.MiddlemanFactory;
21import com.google.devtools.build.lib.collect.nestedset.NestedSet;
22import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
23import com.google.devtools.build.lib.collect.nestedset.Order;
Dmitry Lomove36a66c2017-02-17 14:48:48 +000024
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010025import java.util.List;
26
27/**
28 * A helper class for compilation helpers.
29 */
30public 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 Lomove36a66c2017-02-17 14:48:48 +000057 env, actionOwner, ruleContext.getPackageDirectory(), purpose, filesToBuild,
58 ruleContext.getConfiguration().getMiddlemanDirectory(
Kristina Chodorowf8a1ae62016-08-11 14:44:40 +000059 ruleContext.getRule().getRepository())));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010060 }
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-Guillerezd3a726c2015-07-29 17:51:37 +000089 return ImmutableList.of(
Dmitry Lomove36a66c2017-02-17 14:48:48 +000090 factory.createMiddlemanAllowMultiple(env, actionOwner, ruleContext.getPackageDirectory(),
Kristina Chodorowf8a1ae62016-08-11 14:44:40 +000091 purpose, artifacts, ruleContext.getConfiguration().getMiddlemanDirectory(
92 ruleContext.getRule().getRepository())));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010093 }
94}