juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 1 | // Copyright 2020 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 | |
juliexxia | e7e1c26 | 2020-04-09 14:02:16 -0700 | [diff] [blame] | 16 | import static com.google.common.collect.ImmutableSet.toImmutableSet; |
| 17 | |
jcater | 5037537 | 2020-06-02 08:51:33 -0700 | [diff] [blame] | 18 | import com.google.auto.value.AutoValue; |
juliexxia | b5f9d74 | 2020-04-09 13:07:13 -0700 | [diff] [blame] | 19 | import com.google.common.annotations.VisibleForTesting; |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 20 | import com.google.common.base.Preconditions; |
| 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import com.google.common.collect.ImmutableSet; |
juliexxia | cede76b | 2020-09-23 13:49:34 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.analysis.platform.PlatformInfo; |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.cmdline.Label; |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 25 | import java.util.HashMap; |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 26 | import java.util.Map; |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
juliexxia | ed5d504 | 2020-04-24 11:23:10 -0700 | [diff] [blame] | 29 | * A wrapper class for a map of exec_group names to their relevant ToolchainContext. |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 30 | * |
juliexxia | 0a0a13b | 2020-04-21 15:55:00 -0700 | [diff] [blame] | 31 | * @param <T> any class that extends ToolchainContext. This generic allows ToolchainCollection to be |
| 32 | * used, e.g., both before and after toolchain resolution. |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 33 | */ |
jcater | 5037537 | 2020-06-02 08:51:33 -0700 | [diff] [blame] | 34 | @AutoValue |
| 35 | public abstract class ToolchainCollection<T extends ToolchainContext> { |
juliexxia | 0a0a13b | 2020-04-21 15:55:00 -0700 | [diff] [blame] | 36 | |
| 37 | // This is intentionally a string that would fail {@code Identifier.isValid} so that |
| 38 | // users can't create a group with the same name. |
| 39 | @VisibleForTesting public static final String DEFAULT_EXEC_GROUP_NAME = "default-exec-group"; |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 40 | |
| 41 | /** A map of execution group names to toolchain contexts. */ |
jcater | 5037537 | 2020-06-02 08:51:33 -0700 | [diff] [blame] | 42 | public abstract ImmutableMap<String, T> getContextMap(); |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 43 | |
jcater | 7052f3c | 2020-06-02 15:43:14 -0700 | [diff] [blame] | 44 | public T getDefaultToolchainContext() { |
jcater | 5037537 | 2020-06-02 08:51:33 -0700 | [diff] [blame] | 45 | return getContextMap().get(DEFAULT_EXEC_GROUP_NAME); |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 46 | } |
| 47 | |
jcater | 7052f3c | 2020-06-02 15:43:14 -0700 | [diff] [blame] | 48 | public boolean hasToolchainContext(String execGroup) { |
jcater | 5037537 | 2020-06-02 08:51:33 -0700 | [diff] [blame] | 49 | return getContextMap().containsKey(execGroup); |
| 50 | } |
| 51 | |
| 52 | public T getToolchainContext(String execGroup) { |
| 53 | return getContextMap().get(execGroup); |
| 54 | } |
| 55 | |
| 56 | public ImmutableSet<Label> getResolvedToolchains() { |
| 57 | return getContextMap().values().stream() |
| 58 | .flatMap(c -> c.resolvedToolchainLabels().stream()) |
| 59 | .collect(toImmutableSet()); |
| 60 | } |
| 61 | |
jcater | 7052f3c | 2020-06-02 15:43:14 -0700 | [diff] [blame] | 62 | public ImmutableSet<String> getExecGroups() { |
jcater | 5037537 | 2020-06-02 08:51:33 -0700 | [diff] [blame] | 63 | return getContextMap().keySet(); |
| 64 | } |
| 65 | |
juliexxia | cede76b | 2020-09-23 13:49:34 -0700 | [diff] [blame] | 66 | /** |
| 67 | * This is safe because all toolchain context in a toolchain collection should have the same |
| 68 | * target platform |
| 69 | */ |
| 70 | public PlatformInfo getTargetPlatform() { |
| 71 | return getDefaultToolchainContext().targetPlatform(); |
| 72 | } |
| 73 | |
jcater | 5037537 | 2020-06-02 08:51:33 -0700 | [diff] [blame] | 74 | @SuppressWarnings("unchecked") |
| 75 | public ToolchainCollection<ToolchainContext> asToolchainContexts() { |
| 76 | return (ToolchainCollection<ToolchainContext>) this; |
| 77 | } |
| 78 | |
| 79 | /** Returns a new builder for {@link ToolchainCollection} instances. */ |
| 80 | public static <T extends ToolchainContext> Builder<T> builder() { |
| 81 | return new Builder<T>(); |
juliexxia | 0a0a13b | 2020-04-21 15:55:00 -0700 | [diff] [blame] | 82 | } |
| 83 | |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 84 | /** Builder for ToolchainCollection. */ |
jcater | 5037537 | 2020-06-02 08:51:33 -0700 | [diff] [blame] | 85 | public static final class Builder<T extends ToolchainContext> { |
| 86 | // This is not immutable so that we can check for duplicate keys easily. |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 87 | private final Map<String, T> toolchainContexts = new HashMap<>(); |
| 88 | |
| 89 | public ToolchainCollection<T> build() { |
jcater | 5037537 | 2020-06-02 08:51:33 -0700 | [diff] [blame] | 90 | Preconditions.checkArgument(toolchainContexts.containsKey(DEFAULT_EXEC_GROUP_NAME)); |
| 91 | return new AutoValue_ToolchainCollection<T>(ImmutableMap.copyOf(toolchainContexts)); |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | public void addContext(String execGroup, T context) { |
| 95 | Preconditions.checkArgument( |
| 96 | !toolchainContexts.containsKey(execGroup), |
| 97 | "Duplicate add of '%s' exec group to toolchain collection.", |
| 98 | execGroup); |
| 99 | toolchainContexts.put(execGroup, context); |
| 100 | } |
| 101 | |
| 102 | public Builder<T> addDefaultContext(T context) { |
| 103 | addContext(DEFAULT_EXEC_GROUP_NAME, context); |
| 104 | return this; |
| 105 | } |
| 106 | } |
juliexxia | 71a80dc | 2020-04-09 09:07:09 -0700 | [diff] [blame] | 107 | } |