blob: 26efabcc52dd659005bd80678b064e727fa3e8a8 [file] [log] [blame]
Klaus Aehlig71c993b2017-05-09 07:49:46 -04001// Copyright 2017 The Bazel Authors. 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.
14package com.google.devtools.build.lib.analysis;
15
16import com.google.common.collect.ImmutableList;
Googler25c65252020-06-12 07:02:33 -070017import com.google.common.flogger.GoogleLogger;
Klaus Aehlig71c993b2017-05-09 07:49:46 -040018import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
Klaus Aehliga708a022017-07-11 12:54:40 +020019import com.google.devtools.build.lib.buildeventstream.BuildEvent;
ulfjack26e586d2018-05-17 08:42:13 -070020import com.google.devtools.build.lib.buildeventstream.BuildEventContext;
janakr3ca24682020-04-01 09:12:03 -070021import com.google.devtools.build.lib.buildeventstream.BuildEventIdUtil;
Klaus Aehlig71c993b2017-05-09 07:49:46 -040022import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos;
janakr3ca24682020-04-01 09:12:03 -070023import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId;
Klaus Aehliga708a022017-07-11 12:54:40 +020024import com.google.devtools.build.lib.buildeventstream.BuildEventWithConfiguration;
Klaus Aehlig71c993b2017-05-09 07:49:46 -040025import com.google.devtools.build.lib.buildeventstream.GenericBuildEvent;
Klaus Aehliga708a022017-07-11 12:54:40 +020026import com.google.devtools.build.lib.buildeventstream.NullConfiguration;
Klaus Aehlig284bdec2017-08-11 19:48:46 +020027import com.google.devtools.build.lib.packages.RawAttributeMapper;
28import com.google.devtools.build.lib.packages.Rule;
Klaus Aehlig2a92c902017-08-11 14:03:31 +020029import com.google.devtools.build.lib.packages.Target;
30import com.google.devtools.build.lib.packages.TargetUtils;
31import com.google.devtools.build.lib.packages.TestSize;
Googlerc5fcc862019-09-06 16:17:47 -070032import com.google.devtools.build.lib.packages.Type;
Klaus Aehlig71c993b2017-05-09 07:49:46 -040033import java.util.Collection;
34
35/** Event reporting about the configurations associated with a given target */
36public class TargetConfiguredEvent implements BuildEventWithConfiguration {
Googler25c65252020-06-12 07:02:33 -070037 private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();
Klaus Aehlig2a92c902017-08-11 14:03:31 +020038 private final Target target;
Klaus Aehlig71c993b2017-05-09 07:49:46 -040039 private final Collection<BuildConfiguration> configurations;
40
Klaus Aehlig2a92c902017-08-11 14:03:31 +020041 TargetConfiguredEvent(Target target, Collection<BuildConfiguration> configurations) {
Klaus Aehlig71c993b2017-05-09 07:49:46 -040042 this.configurations = configurations;
Klaus Aehlig2a92c902017-08-11 14:03:31 +020043 this.target = target;
Klaus Aehlig71c993b2017-05-09 07:49:46 -040044 }
45
46 @Override
Klaus Aehliga708a022017-07-11 12:54:40 +020047 public Collection<BuildEvent> getConfigurations() {
48 ImmutableList.Builder<BuildEvent> builder = new ImmutableList.Builder<>();
49 for (BuildConfiguration config : configurations) {
50 if (config != null) {
shahan50f99d52018-03-10 05:14:09 -080051 builder.add(config.toBuildEvent());
Klaus Aehliga708a022017-07-11 12:54:40 +020052 } else {
53 builder.add(new NullConfiguration());
54 }
55 }
56 return builder.build();
Klaus Aehlig71c993b2017-05-09 07:49:46 -040057 }
58
59 @Override
60 public BuildEventId getEventId() {
janakr3ca24682020-04-01 09:12:03 -070061 return BuildEventIdUtil.targetConfigured(target.getLabel());
Klaus Aehlig71c993b2017-05-09 07:49:46 -040062 }
63
64 @Override
65 public Collection<BuildEventId> getChildrenEvents() {
ulfjack904a8d62018-05-29 05:17:35 -070066 ImmutableList.Builder<BuildEventId> childrenBuilder = ImmutableList.builder();
Klaus Aehlig71c993b2017-05-09 07:49:46 -040067 for (BuildConfiguration config : configurations) {
Klaus Aehligebec9252017-06-27 17:29:50 +020068 if (config != null) {
janakr3ca24682020-04-01 09:12:03 -070069 childrenBuilder.add(
70 BuildEventIdUtil.targetCompleted(target.getLabel(), config.getEventId()));
Klaus Aehligebec9252017-06-27 17:29:50 +020071 } else {
72 childrenBuilder.add(
janakr3ca24682020-04-01 09:12:03 -070073 BuildEventIdUtil.targetCompleted(
74 target.getLabel(), BuildEventIdUtil.nullConfigurationId()));
Klaus Aehligebec9252017-06-27 17:29:50 +020075 }
Klaus Aehlig71c993b2017-05-09 07:49:46 -040076 }
77 return childrenBuilder.build();
78 }
79
Googler25c65252020-06-12 07:02:33 -070080 private static BuildEventStreamProtos.TestSize bepTestSize(String targetName, TestSize size) {
81 if (size != null) {
82 switch (size) {
83 case SMALL:
84 return BuildEventStreamProtos.TestSize.SMALL;
85 case MEDIUM:
86 return BuildEventStreamProtos.TestSize.MEDIUM;
87 case LARGE:
88 return BuildEventStreamProtos.TestSize.LARGE;
89 case ENORMOUS:
90 return BuildEventStreamProtos.TestSize.ENORMOUS;
91 }
Klaus Aehlig2a92c902017-08-11 14:03:31 +020092 }
Googler25c65252020-06-12 07:02:33 -070093 logger.atInfo().log("Target %s has a test size of: %s", targetName, size);
94 return BuildEventStreamProtos.TestSize.UNKNOWN;
Klaus Aehlig2a92c902017-08-11 14:03:31 +020095 }
96
Klaus Aehlig71c993b2017-05-09 07:49:46 -040097 @Override
ulfjack26e586d2018-05-17 08:42:13 -070098 public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) {
Klaus Aehlig2a92c902017-08-11 14:03:31 +020099 BuildEventStreamProtos.TargetConfigured.Builder builder =
100 BuildEventStreamProtos.TargetConfigured.newBuilder().setTargetKind(target.getTargetKind());
Klaus Aehlig284bdec2017-08-11 19:48:46 +0200101 Rule rule = target.getAssociatedRule();
Klaus Aehliga5297742018-12-07 08:35:23 -0800102 if (rule != null && RawAttributeMapper.of(rule).has("tags")) {
103 // Not every rule has tags, as, due to the "external" package we also have to expect
104 // repository rules at this place.
Klaus Aehlig284bdec2017-08-11 19:48:46 +0200105 builder.addAllTag(RawAttributeMapper.of(rule).getMergedValues("tags", Type.STRING_LIST));
106 }
Klaus Aehlig2a92c902017-08-11 14:03:31 +0200107 if (TargetUtils.isTestRule(target)) {
Googler25c65252020-06-12 07:02:33 -0700108 builder.setTestSize(
109 bepTestSize(target.getName(), TestSize.getTestSize(target.getAssociatedRule())));
Klaus Aehlig2a92c902017-08-11 14:03:31 +0200110 }
111 return GenericBuildEvent.protoChaining(this).setConfigured(builder.build()).build();
Klaus Aehlig71c993b2017-05-09 07:49:46 -0400112 }
113}