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; |
Googler | 25c6525 | 2020-06-12 07:02:33 -0700 | [diff] [blame] | 17 | import com.google.common.flogger.GoogleLogger; |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 18 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 19 | import com.google.devtools.build.lib.buildeventstream.BuildEvent; |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 20 | import com.google.devtools.build.lib.buildeventstream.BuildEventContext; |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.buildeventstream.BuildEventIdUtil; |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 22 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos; |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId; |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 24 | import com.google.devtools.build.lib.buildeventstream.BuildEventWithConfiguration; |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 25 | import com.google.devtools.build.lib.buildeventstream.GenericBuildEvent; |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 26 | import com.google.devtools.build.lib.buildeventstream.NullConfiguration; |
Klaus Aehlig | 284bdec | 2017-08-11 19:48:46 +0200 | [diff] [blame] | 27 | import com.google.devtools.build.lib.packages.RawAttributeMapper; |
| 28 | import com.google.devtools.build.lib.packages.Rule; |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 29 | import com.google.devtools.build.lib.packages.Target; |
| 30 | import com.google.devtools.build.lib.packages.TargetUtils; |
| 31 | import com.google.devtools.build.lib.packages.TestSize; |
Googler | c5fcc86 | 2019-09-06 16:17:47 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.packages.Type; |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 33 | import java.util.Collection; |
| 34 | |
| 35 | /** Event reporting about the configurations associated with a given target */ |
| 36 | public class TargetConfiguredEvent implements BuildEventWithConfiguration { |
Googler | 25c6525 | 2020-06-12 07:02:33 -0700 | [diff] [blame] | 37 | private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 38 | private final Target target; |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 39 | private final Collection<BuildConfiguration> configurations; |
| 40 | |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 41 | TargetConfiguredEvent(Target target, Collection<BuildConfiguration> configurations) { |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 42 | this.configurations = configurations; |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 43 | this.target = target; |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Override |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 47 | public Collection<BuildEvent> getConfigurations() { |
| 48 | ImmutableList.Builder<BuildEvent> builder = new ImmutableList.Builder<>(); |
| 49 | for (BuildConfiguration config : configurations) { |
| 50 | if (config != null) { |
shahan | 50f99d5 | 2018-03-10 05:14:09 -0800 | [diff] [blame] | 51 | builder.add(config.toBuildEvent()); |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 52 | } else { |
| 53 | builder.add(new NullConfiguration()); |
| 54 | } |
| 55 | } |
| 56 | return builder.build(); |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public BuildEventId getEventId() { |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 61 | return BuildEventIdUtil.targetConfigured(target.getLabel()); |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public Collection<BuildEventId> getChildrenEvents() { |
ulfjack | 904a8d6 | 2018-05-29 05:17:35 -0700 | [diff] [blame] | 66 | ImmutableList.Builder<BuildEventId> childrenBuilder = ImmutableList.builder(); |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 67 | for (BuildConfiguration config : configurations) { |
Klaus Aehlig | ebec925 | 2017-06-27 17:29:50 +0200 | [diff] [blame] | 68 | if (config != null) { |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 69 | childrenBuilder.add( |
| 70 | BuildEventIdUtil.targetCompleted(target.getLabel(), config.getEventId())); |
Klaus Aehlig | ebec925 | 2017-06-27 17:29:50 +0200 | [diff] [blame] | 71 | } else { |
| 72 | childrenBuilder.add( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 73 | BuildEventIdUtil.targetCompleted( |
| 74 | target.getLabel(), BuildEventIdUtil.nullConfigurationId())); |
Klaus Aehlig | ebec925 | 2017-06-27 17:29:50 +0200 | [diff] [blame] | 75 | } |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 76 | } |
| 77 | return childrenBuilder.build(); |
| 78 | } |
| 79 | |
Googler | 25c6525 | 2020-06-12 07:02:33 -0700 | [diff] [blame] | 80 | 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 Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 92 | } |
Googler | 25c6525 | 2020-06-12 07:02:33 -0700 | [diff] [blame] | 93 | logger.atInfo().log("Target %s has a test size of: %s", targetName, size); |
| 94 | return BuildEventStreamProtos.TestSize.UNKNOWN; |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 97 | @Override |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 98 | public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) { |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 99 | BuildEventStreamProtos.TargetConfigured.Builder builder = |
| 100 | BuildEventStreamProtos.TargetConfigured.newBuilder().setTargetKind(target.getTargetKind()); |
Klaus Aehlig | 284bdec | 2017-08-11 19:48:46 +0200 | [diff] [blame] | 101 | Rule rule = target.getAssociatedRule(); |
Klaus Aehlig | a529774 | 2018-12-07 08:35:23 -0800 | [diff] [blame] | 102 | 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 Aehlig | 284bdec | 2017-08-11 19:48:46 +0200 | [diff] [blame] | 105 | builder.addAllTag(RawAttributeMapper.of(rule).getMergedValues("tags", Type.STRING_LIST)); |
| 106 | } |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 107 | if (TargetUtils.isTestRule(target)) { |
Googler | 25c6525 | 2020-06-12 07:02:33 -0700 | [diff] [blame] | 108 | builder.setTestSize( |
| 109 | bepTestSize(target.getName(), TestSize.getTestSize(target.getAssociatedRule()))); |
Klaus Aehlig | 2a92c90 | 2017-08-11 14:03:31 +0200 | [diff] [blame] | 110 | } |
| 111 | return GenericBuildEvent.protoChaining(this).setConfigured(builder.build()).build(); |
Klaus Aehlig | 71c993b | 2017-05-09 07:49:46 -0400 | [diff] [blame] | 112 | } |
| 113 | } |