Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.analysis; |
| 15 | |
| 16 | import com.google.common.collect.ImmutableList; |
| 17 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 18 | import com.google.devtools.build.lib.buildeventstream.BuildEvent; |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 19 | import com.google.devtools.build.lib.buildeventstream.BuildEventContext; |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 20 | import com.google.devtools.build.lib.buildeventstream.BuildEventId; |
| 21 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos; |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 22 | import com.google.devtools.build.lib.buildeventstream.BuildEventWithConfiguration; |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 23 | import com.google.devtools.build.lib.buildeventstream.GenericBuildEvent; |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 24 | import com.google.devtools.build.lib.buildeventstream.NullConfiguration; |
Klaus Aehlig | 284bdec | 2017-08-11 19:48:46 +0200 | [diff] [blame] | 25 | import com.google.devtools.build.lib.packages.RawAttributeMapper; |
| 26 | import com.google.devtools.build.lib.packages.Rule; |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 27 | import com.google.devtools.build.lib.packages.Target; |
| 28 | import com.google.devtools.build.lib.packages.TargetUtils; |
| 29 | import com.google.devtools.build.lib.packages.TestSize; |
Klaus Aehlig | 284bdec | 2017-08-11 19:48:46 +0200 | [diff] [blame] | 30 | import com.google.devtools.build.lib.syntax.Type; |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 31 | import java.util.Collection; |
| 32 | |
| 33 | /** Event reporting about the configurations associated with a given target */ |
| 34 | public class TargetConfiguredEvent implements BuildEventWithConfiguration { |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 35 | private final Target target; |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 36 | private final Collection<BuildConfiguration> configurations; |
| 37 | |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 38 | TargetConfiguredEvent(Target target, Collection<BuildConfiguration> configurations) { |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 39 | this.configurations = configurations; |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 40 | this.target = target; |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Override |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 44 | public Collection<BuildEvent> getConfigurations() { |
| 45 | ImmutableList.Builder<BuildEvent> builder = new ImmutableList.Builder<>(); |
| 46 | for (BuildConfiguration config : configurations) { |
| 47 | if (config != null) { |
shahan | 50f99d5 | 2018-03-10 05:14:09 -0800 | [diff] [blame] | 48 | builder.add(config.toBuildEvent()); |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 49 | } else { |
| 50 | builder.add(new NullConfiguration()); |
| 51 | } |
| 52 | } |
| 53 | return builder.build(); |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public BuildEventId getEventId() { |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 58 | return BuildEventId.targetConfigured(target.getLabel()); |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public Collection<BuildEventId> getChildrenEvents() { |
ulfjack | 904a8d6 | 2018-05-29 05:17:35 -0700 | [diff] [blame] | 63 | ImmutableList.Builder<BuildEventId> childrenBuilder = ImmutableList.builder(); |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 64 | for (BuildConfiguration config : configurations) { |
Klaus Aehlig | ebec925 | 2017-06-27 17:29:50 +0200 | [diff] [blame] | 65 | if (config != null) { |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 66 | childrenBuilder.add(BuildEventId.targetCompleted(target.getLabel(), config.getEventId())); |
Klaus Aehlig | ebec925 | 2017-06-27 17:29:50 +0200 | [diff] [blame] | 67 | } else { |
| 68 | childrenBuilder.add( |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 69 | BuildEventId.targetCompleted(target.getLabel(), BuildEventId.nullConfigurationId())); |
Klaus Aehlig | ebec925 | 2017-06-27 17:29:50 +0200 | [diff] [blame] | 70 | } |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 71 | } |
| 72 | return childrenBuilder.build(); |
| 73 | } |
| 74 | |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 75 | static BuildEventStreamProtos.TestSize bepTestSize(TestSize size) { |
| 76 | switch (size) { |
| 77 | case SMALL: |
| 78 | return BuildEventStreamProtos.TestSize.SMALL; |
| 79 | case MEDIUM: |
| 80 | return BuildEventStreamProtos.TestSize.MEDIUM; |
| 81 | case LARGE: |
| 82 | return BuildEventStreamProtos.TestSize.LARGE; |
| 83 | case ENORMOUS: |
| 84 | return BuildEventStreamProtos.TestSize.ENORMOUS; |
| 85 | default: |
| 86 | return BuildEventStreamProtos.TestSize.UNKNOWN; |
| 87 | } |
| 88 | } |
| 89 | |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 90 | @Override |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 91 | public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) { |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 92 | BuildEventStreamProtos.TargetConfigured.Builder builder = |
| 93 | BuildEventStreamProtos.TargetConfigured.newBuilder().setTargetKind(target.getTargetKind()); |
Klaus Aehlig | 284bdec | 2017-08-11 19:48:46 +0200 | [diff] [blame] | 94 | Rule rule = target.getAssociatedRule(); |
Klaus Aehlig | a529774 | 2018-12-07 08:35:23 -0800 | [diff] [blame] | 95 | if (rule != null && RawAttributeMapper.of(rule).has("tags")) { |
| 96 | // Not every rule has tags, as, due to the "external" package we also have to expect |
| 97 | // repository rules at this place. |
Klaus Aehlig | 284bdec | 2017-08-11 19:48:46 +0200 | [diff] [blame] | 98 | builder.addAllTag(RawAttributeMapper.of(rule).getMergedValues("tags", Type.STRING_LIST)); |
| 99 | } |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 100 | if (TargetUtils.isTestRule(target)) { |
| 101 | builder.setTestSize(bepTestSize(TestSize.getTestSize(target.getAssociatedRule()))); |
| 102 | } |
| 103 | return GenericBuildEvent.protoChaining(this).setConfigured(builder.build()).build(); |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 104 | } |
| 105 | } |