blob: 0465daf8e55995a9eb80ecb0eab278dfcb7da6e6 [file] [log] [blame]
juliexxia71a80dc2020-04-09 09:07:09 -07001// 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.
14package com.google.devtools.build.lib.analysis;
15
juliexxiae7e1c262020-04-09 14:02:16 -070016import static com.google.common.collect.ImmutableSet.toImmutableSet;
17
jcater50375372020-06-02 08:51:33 -070018import com.google.auto.value.AutoValue;
juliexxia71a80dc2020-04-09 09:07:09 -070019import com.google.common.base.Preconditions;
20import com.google.common.collect.ImmutableMap;
21import com.google.common.collect.ImmutableSet;
juliexxiacede76b2020-09-23 13:49:34 -070022import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
juliexxia71a80dc2020-04-09 09:07:09 -070023import com.google.devtools.build.lib.cmdline.Label;
jcater64534e22021-05-06 07:56:27 -070024import com.google.devtools.build.lib.packages.ExecGroup;
juliexxia71a80dc2020-04-09 09:07:09 -070025import java.util.HashMap;
juliexxia71a80dc2020-04-09 09:07:09 -070026import java.util.Map;
juliexxia71a80dc2020-04-09 09:07:09 -070027
28/**
juliexxiaed5d5042020-04-24 11:23:10 -070029 * A wrapper class for a map of exec_group names to their relevant ToolchainContext.
juliexxia71a80dc2020-04-09 09:07:09 -070030 *
juliexxia0a0a13b2020-04-21 15:55:00 -070031 * @param <T> any class that extends ToolchainContext. This generic allows ToolchainCollection to be
32 * used, e.g., both before and after toolchain resolution.
juliexxia71a80dc2020-04-09 09:07:09 -070033 */
jcater50375372020-06-02 08:51:33 -070034@AutoValue
35public abstract class ToolchainCollection<T extends ToolchainContext> {
juliexxia0a0a13b2020-04-21 15:55:00 -070036
juliexxia71a80dc2020-04-09 09:07:09 -070037 /** A map of execution group names to toolchain contexts. */
jcater50375372020-06-02 08:51:33 -070038 public abstract ImmutableMap<String, T> getContextMap();
juliexxia71a80dc2020-04-09 09:07:09 -070039
jcater7052f3c2020-06-02 15:43:14 -070040 public T getDefaultToolchainContext() {
jcater64534e22021-05-06 07:56:27 -070041 return getContextMap().get(ExecGroup.DEFAULT_EXEC_GROUP_NAME);
juliexxia71a80dc2020-04-09 09:07:09 -070042 }
43
jcater7052f3c2020-06-02 15:43:14 -070044 public boolean hasToolchainContext(String execGroup) {
jcater50375372020-06-02 08:51:33 -070045 return getContextMap().containsKey(execGroup);
46 }
47
48 public T getToolchainContext(String execGroup) {
49 return getContextMap().get(execGroup);
50 }
51
52 public ImmutableSet<Label> getResolvedToolchains() {
53 return getContextMap().values().stream()
54 .flatMap(c -> c.resolvedToolchainLabels().stream())
55 .collect(toImmutableSet());
56 }
57
jcater9b18d952021-05-06 07:59:26 -070058 public ImmutableSet<String> getExecGroupNames() {
jcater50375372020-06-02 08:51:33 -070059 return getContextMap().keySet();
60 }
61
juliexxiacede76b2020-09-23 13:49:34 -070062 /**
63 * This is safe because all toolchain context in a toolchain collection should have the same
64 * target platform
65 */
66 public PlatformInfo getTargetPlatform() {
67 return getDefaultToolchainContext().targetPlatform();
68 }
69
jcater50375372020-06-02 08:51:33 -070070 @SuppressWarnings("unchecked")
71 public ToolchainCollection<ToolchainContext> asToolchainContexts() {
72 return (ToolchainCollection<ToolchainContext>) this;
73 }
74
75 /** Returns a new builder for {@link ToolchainCollection} instances. */
76 public static <T extends ToolchainContext> Builder<T> builder() {
77 return new Builder<T>();
juliexxia0a0a13b2020-04-21 15:55:00 -070078 }
79
juliexxia71a80dc2020-04-09 09:07:09 -070080 /** Builder for ToolchainCollection. */
jcater50375372020-06-02 08:51:33 -070081 public static final class Builder<T extends ToolchainContext> {
82 // This is not immutable so that we can check for duplicate keys easily.
juliexxia71a80dc2020-04-09 09:07:09 -070083 private final Map<String, T> toolchainContexts = new HashMap<>();
84
85 public ToolchainCollection<T> build() {
jcater64534e22021-05-06 07:56:27 -070086 Preconditions.checkArgument(toolchainContexts.containsKey(ExecGroup.DEFAULT_EXEC_GROUP_NAME));
jcater50375372020-06-02 08:51:33 -070087 return new AutoValue_ToolchainCollection<T>(ImmutableMap.copyOf(toolchainContexts));
juliexxia71a80dc2020-04-09 09:07:09 -070088 }
89
90 public void addContext(String execGroup, T context) {
91 Preconditions.checkArgument(
92 !toolchainContexts.containsKey(execGroup),
93 "Duplicate add of '%s' exec group to toolchain collection.",
94 execGroup);
95 toolchainContexts.put(execGroup, context);
96 }
97
98 public Builder<T> addDefaultContext(T context) {
jcater64534e22021-05-06 07:56:27 -070099 addContext(ExecGroup.DEFAULT_EXEC_GROUP_NAME, context);
juliexxia71a80dc2020-04-09 09:07:09 -0700100 return this;
101 }
102 }
juliexxia71a80dc2020-04-09 09:07:09 -0700103}