// Copyright 2017 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.rules.config; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSortedMap; import com.google.common.collect.ImmutableSortedSet; import com.google.common.hash.Hasher; import com.google.common.hash.Hashing; import com.google.devtools.build.lib.actions.ArtifactOwner; import com.google.devtools.build.lib.analysis.config.BuildConfiguration; import com.google.devtools.build.lib.analysis.config.BuildOptions; import com.google.devtools.build.lib.analysis.config.ConfigurationEnvironment; import com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory; import com.google.devtools.build.lib.analysis.config.FragmentOptions; import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; import java.util.Map; import java.util.Optional; import java.util.SortedMap; import javax.annotation.Nullable; /** * Configuration fragment for Android's config_feature_flag, flags which can be defined in BUILD * files. */ @AutoCodec public final class ConfigFeatureFlagConfiguration extends BuildConfiguration.Fragment { /** * A configuration fragment loader able to create instances of {@link * ConfigFeatureFlagConfiguration} from {@link ConfigFeatureFlagOptions}. */ public static final class Loader implements ConfigurationFragmentFactory { @Override public BuildConfiguration.Fragment create( ConfigurationEnvironment env, BuildOptions buildOptions) throws InvalidConfigurationException { ConfigFeatureFlagOptions options = buildOptions.get(ConfigFeatureFlagOptions.class); if (!options.unknownFlags.isEmpty()) { ImmutableList.Builder errorMessage = new ImmutableList.Builder<>(); for (Label missingLabel : options.unknownFlags) { errorMessage.add( String.format( "Feature flag %1$s was accessed in a configuration it is not present in. All " + "targets which depend on %1$s directly or indirectly must name it in their " + "transitive_configs attribute.", missingLabel)); } throw new InvalidConfigurationException( "Some feature flags were incorrectly specified:\n" + Joiner.on("\n").join(errorMessage.build())); } return new ConfigFeatureFlagConfiguration(options); } @Override public Class creates() { return ConfigFeatureFlagConfiguration.class; } @Override public ImmutableSet> requiredOptions() { return ImmutableSet.>of(ConfigFeatureFlagOptions.class); } } /** Exception thrown when a flag is accessed in a configuration it is not present in. */ public static final class MissingFlagException extends RuntimeException { public MissingFlagException(Label label) { super( String.format( "Feature flag %1$s was accessed in a configuration it was trimmed from.", label)); } } private final ImmutableSortedMap flagValues; private final ImmutableSortedSet