blob: a7457f8861de6015e5f2706fb84eb3eebefd5f5f [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;
juliexxiab5f9d742020-04-09 13:07:13 -070019import com.google.common.annotations.VisibleForTesting;
juliexxia71a80dc2020-04-09 09:07:09 -070020import com.google.common.base.Preconditions;
21import com.google.common.collect.ImmutableMap;
22import com.google.common.collect.ImmutableSet;
juliexxiacede76b2020-09-23 13:49:34 -070023import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
juliexxia71a80dc2020-04-09 09:07:09 -070024import com.google.devtools.build.lib.cmdline.Label;
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
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";
juliexxia71a80dc2020-04-09 09:07:09 -070040
41 /** A map of execution group names to toolchain contexts. */
jcater50375372020-06-02 08:51:33 -070042 public abstract ImmutableMap<String, T> getContextMap();
juliexxia71a80dc2020-04-09 09:07:09 -070043
jcater7052f3c2020-06-02 15:43:14 -070044 public T getDefaultToolchainContext() {
jcater50375372020-06-02 08:51:33 -070045 return getContextMap().get(DEFAULT_EXEC_GROUP_NAME);
juliexxia71a80dc2020-04-09 09:07:09 -070046 }
47
jcater7052f3c2020-06-02 15:43:14 -070048 public boolean hasToolchainContext(String execGroup) {
jcater50375372020-06-02 08:51:33 -070049 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
jcater7052f3c2020-06-02 15:43:14 -070062 public ImmutableSet<String> getExecGroups() {
jcater50375372020-06-02 08:51:33 -070063 return getContextMap().keySet();
64 }
65
juliexxiacede76b2020-09-23 13:49:34 -070066 /**
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
jcater50375372020-06-02 08:51:33 -070074 @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>();
juliexxia0a0a13b2020-04-21 15:55:00 -070082 }
83
juliexxia71a80dc2020-04-09 09:07:09 -070084 /** Builder for ToolchainCollection. */
jcater50375372020-06-02 08:51:33 -070085 public static final class Builder<T extends ToolchainContext> {
86 // This is not immutable so that we can check for duplicate keys easily.
juliexxia71a80dc2020-04-09 09:07:09 -070087 private final Map<String, T> toolchainContexts = new HashMap<>();
88
89 public ToolchainCollection<T> build() {
jcater50375372020-06-02 08:51:33 -070090 Preconditions.checkArgument(toolchainContexts.containsKey(DEFAULT_EXEC_GROUP_NAME));
91 return new AutoValue_ToolchainCollection<T>(ImmutableMap.copyOf(toolchainContexts));
juliexxia71a80dc2020-04-09 09:07:09 -070092 }
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 }
juliexxia71a80dc2020-04-09 09:07:09 -0700107}