diff --git a/src/main/java/com/google/devtools/build/lib/analysis/BUILD b/src/main/java/com/google/devtools/build/lib/analysis/BUILD index 17d20b01915327..bc974ad230da5d 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/BUILD +++ b/src/main/java/com/google/devtools/build/lib/analysis/BUILD @@ -1822,6 +1822,15 @@ java_library( ], ) +java_library( + name = "config/toolchain_type_requirement", + srcs = ["config/ToolchainTypeRequirement.java"], + deps = [ + "//src/main/java/com/google/devtools/build/lib/cmdline", + "//third_party:auto_value", + ], +) + java_library( name = "config/transition_factories", srcs = ["config/TransitionFactories.java"], diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/ToolchainTypeRequirement.java b/src/main/java/com/google/devtools/build/lib/analysis/config/ToolchainTypeRequirement.java new file mode 100644 index 00000000000000..2f4a3f9fbe4019 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/analysis/config/ToolchainTypeRequirement.java @@ -0,0 +1,51 @@ +// Copyright 2022 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.analysis.config; + +import com.google.auto.value.AutoValue; +import com.google.devtools.build.lib.cmdline.Label; + +/** + * Describes a requirement on a specific toolchain type. + */ +@AutoValue +public abstract class ToolchainTypeRequirement { + /** Returns a builder for a new {@Link ToolchainTypeRequirement}. */ + public static Builder builder() { + return new AutoValue_ToolchainTypeRequirement.Builder() + .mandatory(true); + } + + /** Returns the label of the toolchain type that is requested. */ + public abstract Label toolchainType(); + + /** + * Returns whether the toolchain type is mandatory or optional. An optional toolchain type which + * cannot be found will be skipped, but a mandatory toolchain type which cannot be found will stop + * the build with an error. + */ + public abstract boolean mandatory(); + + /** A builder for a new {@link ToolchainTypeRequirement}. */ + @AutoValue.Builder + public interface Builder { + /** Sets the toolchain type. */ + Builder toolchainType(Label toolchainType); + /** Sets whether the toolchain type is mandatory. */ + Builder mandatory(boolean mandatory); + + /** Returns the newly built {@link ToolchainTypeRequirement}. */ + ToolchainTypeRequirement build(); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/analysis/testing/ToolchainTypeRequirementSubject.java b/src/test/java/com/google/devtools/build/lib/analysis/testing/ToolchainTypeRequirementSubject.java new file mode 100644 index 00000000000000..294d43bb3a8aec --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/analysis/testing/ToolchainTypeRequirementSubject.java @@ -0,0 +1,61 @@ +// Copyright 2022 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.analysis.testing; + +import static com.google.common.truth.Truth.assertAbout; + +import com.google.common.truth.ComparableSubject; +import com.google.common.truth.FailureMetadata; +import com.google.common.truth.Subject; +import com.google.devtools.build.lib.analysis.config.ToolchainTypeRequirement; +import com.google.devtools.build.lib.cmdline.Label; + +/** A Truth {@link Subject} for {@link ToolchainTypeRequirement}. */ +public class ToolchainTypeRequirementSubject extends Subject { + // Static data. + + /** Entry point for test assertions related to {@link ToolchainTypeRequirement}. */ + public static ToolchainTypeRequirementSubject assertThat( + ToolchainTypeRequirement toolchainTypeRequirement) { + return assertAbout(ToolchainTypeRequirementSubject::new).that(toolchainTypeRequirement); + } + + /** Static method for getting the subject factory (for use with assertAbout()). */ + public static Subject.Factory + toolchainTypeRequirements() { + return ToolchainTypeRequirementSubject::new; + } + + // Instance fields. + + private final ToolchainTypeRequirement actual; + + protected ToolchainTypeRequirementSubject( + FailureMetadata failureMetadata, ToolchainTypeRequirement subject) { + super(failureMetadata, subject); + this.actual = subject; + } + + public ComparableSubject