From c24b847322e7e21de10a9031aaae19a691475909 Mon Sep 17 00:00:00 2001 From: Googler Date: Wed, 4 Oct 2023 12:56:41 -0700 Subject: [PATCH] Make PlatformMappingApplier its own class and rename it. Work towards platform-based flags: #19409. PiperOrigin-RevId: 570779049 Change-Id: I142c44db575bd3f4b77d369285a9e97f57227271 --- .../BuildConfigurationKeyProducer.java | 108 ++++++++++++++++++ .../analysis/producers/TransitionApplier.java | 90 +-------------- 2 files changed, 113 insertions(+), 85 deletions(-) create mode 100644 src/main/java/com/google/devtools/build/lib/analysis/producers/BuildConfigurationKeyProducer.java diff --git a/src/main/java/com/google/devtools/build/lib/analysis/producers/BuildConfigurationKeyProducer.java b/src/main/java/com/google/devtools/build/lib/analysis/producers/BuildConfigurationKeyProducer.java new file mode 100644 index 00000000000000..1e4ec3b457ace4 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/analysis/producers/BuildConfigurationKeyProducer.java @@ -0,0 +1,108 @@ +// Copyright 2023 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.producers; + +import com.google.common.collect.ImmutableListMultimap; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Multimaps; +import com.google.devtools.build.lib.analysis.PlatformOptions; +import com.google.devtools.build.lib.analysis.config.BuildOptions; +import com.google.devtools.build.lib.skyframe.config.BuildConfigurationKey; +import com.google.devtools.build.lib.skyframe.config.PlatformMappingValue; +import com.google.devtools.build.lib.vfs.PathFragment; +import com.google.devtools.build.skyframe.state.StateMachine; +import com.google.devtools.common.options.OptionsParsingException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +/** + * Creates the needed {@link BuildConfigurationKey} instances for the given options. + * + *

This includes merging in platform mappings. + * + *

The output preserves the iteration order of the input. + */ +class BuildConfigurationKeyProducer implements StateMachine { + interface ResultSink { + + void acceptTransitionError(OptionsParsingException e); + + void acceptTransitionedConfigurations( + ImmutableMap transitionedOptions); + } + + // -------------------- Input -------------------- + private final ResultSink sink; + private final StateMachine runAfter; + private final Map options; + + // -------------------- Internal State -------------------- + private final Map platformMappingValues = new HashMap<>(); + + BuildConfigurationKeyProducer( + ResultSink sink, StateMachine runAfter, Map options) { + this.sink = sink; + this.runAfter = runAfter; + this.options = options; + } + + @Override + public StateMachine step(Tasks tasks) { + // Deduplicates the platform mapping paths and collates the transition keys. + ImmutableListMultimap, String> index = + Multimaps.index( + options.keySet(), transitionKey -> getPlatformMappingsPath(options.get(transitionKey))); + for (Map.Entry, Collection> entry : index.asMap().entrySet()) { + Collection transitionKeys = entry.getValue(); + tasks.lookUp( + PlatformMappingValue.Key.create(entry.getKey().orElse(null)), + rawValue -> { + var value = (PlatformMappingValue) rawValue; + // Maps the value from all transition keys with the same platform mappings path. + for (String key : transitionKeys) { + platformMappingValues.put(key, value); + } + }); + } + return this::applyMappings; + } + + private StateMachine applyMappings(Tasks tasks) { + var result = + ImmutableMap.builderWithExpectedSize(options.size()); + for (Map.Entry entry : options.entrySet()) { + String transitionKey = entry.getKey(); + BuildConfigurationKey newConfigurationKey; + try { + newConfigurationKey = + BuildConfigurationKey.withPlatformMapping( + platformMappingValues.get(transitionKey), entry.getValue()); + } catch (OptionsParsingException e) { + sink.acceptTransitionError(e); + return runAfter; + } + result.put(transitionKey, newConfigurationKey); + } + sink.acceptTransitionedConfigurations(result.buildOrThrow()); + return runAfter; + } + + private static Optional getPlatformMappingsPath(BuildOptions fromOptions) { + return fromOptions.hasNoConfig() + ? Optional.empty() + : Optional.ofNullable(fromOptions.get(PlatformOptions.class).platformMappings); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/analysis/producers/TransitionApplier.java b/src/main/java/com/google/devtools/build/lib/analysis/producers/TransitionApplier.java index af37e30f5ee2a1..57d6c631cad2cd 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/producers/TransitionApplier.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/producers/TransitionApplier.java @@ -13,11 +13,7 @@ // limitations under the License. package com.google.devtools.build.lib.analysis.producers; -import com.google.common.collect.ImmutableListMultimap; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Multimaps; -import com.google.devtools.build.lib.analysis.PlatformOptions; import com.google.devtools.build.lib.analysis.config.BuildOptions; import com.google.devtools.build.lib.analysis.config.StarlarkTransitionCache; import com.google.devtools.build.lib.analysis.config.transitions.ConfigurationTransition; @@ -28,15 +24,9 @@ import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.events.ExtendedEventHandler; import com.google.devtools.build.lib.skyframe.config.BuildConfigurationKey; -import com.google.devtools.build.lib.skyframe.config.PlatformMappingValue; -import com.google.devtools.build.lib.vfs.PathFragment; import com.google.devtools.build.skyframe.SkyValue; import com.google.devtools.build.skyframe.state.StateMachine; -import com.google.devtools.common.options.OptionsParsingException; -import java.util.Collection; -import java.util.HashMap; import java.util.Map; -import java.util.Optional; import javax.annotation.Nullable; /** @@ -47,13 +37,8 @@ */ final class TransitionApplier implements StateMachine, StateMachine.ValueOrExceptionSink { - interface ResultSink { - void acceptTransitionedConfigurations( - ImmutableMap transitionedOptions); - + interface ResultSink extends BuildConfigurationKeyProducer.ResultSink { void acceptTransitionError(TransitionException e); - - void acceptTransitionError(OptionsParsingException e); } // -------------------- Input -------------------- @@ -96,7 +81,9 @@ public StateMachine step(Tasks tasks) throws InterruptedException { return runAfter; } if (!doesStarlarkTransition) { - return new PlatformMappingApplier( + return new BuildConfigurationKeyProducer( + this.sink, + this.runAfter, transition.apply( TransitionUtil.restrict(transition, fromConfiguration.getOptions()), eventHandler)); } @@ -142,73 +129,6 @@ private StateMachine applyStarlarkTransition(Tasks tasks) throws InterruptedExce sink.acceptTransitionError(e); return runAfter; } - return new PlatformMappingApplier(transitionedOptions); - } - - /** - * Applies the platform mapping to each option. - * - *

The output preserves the iteration order of the input. - */ - private class PlatformMappingApplier implements StateMachine { - // -------------------- Input -------------------- - private final Map options; - - // -------------------- Internal State -------------------- - private final Map platformMappingValues = new HashMap<>(); - - private PlatformMappingApplier(Map options) { - this.options = options; - } - - @Override - public StateMachine step(Tasks tasks) { - // Deduplicates the platform mapping paths and collates the transition keys. - ImmutableListMultimap, String> index = - Multimaps.index( - options.keySet(), - transitionKey -> - Optional.ofNullable(getPlatformMappingsPath(options.get(transitionKey)))); - for (Map.Entry, Collection> entry : index.asMap().entrySet()) { - Collection transitionKeys = entry.getValue(); - tasks.lookUp( - PlatformMappingValue.Key.create(entry.getKey().orElse(null)), - rawValue -> { - var value = (PlatformMappingValue) rawValue; - // Maps the value from all transition keys with the same platform mappings path. - for (String key : transitionKeys) { - platformMappingValues.put(key, value); - } - }); - } - return this::applyMappings; - } - - private StateMachine applyMappings(Tasks tasks) { - var result = - ImmutableMap.builderWithExpectedSize(options.size()); - for (Map.Entry entry : options.entrySet()) { - String transitionKey = entry.getKey(); - BuildConfigurationKey newConfigurationKey; - try { - newConfigurationKey = - BuildConfigurationKey.withPlatformMapping( - platformMappingValues.get(transitionKey), entry.getValue()); - } catch (OptionsParsingException e) { - sink.acceptTransitionError(e); - return runAfter; - } - result.put(transitionKey, newConfigurationKey); - } - sink.acceptTransitionedConfigurations(result.buildOrThrow()); - return runAfter; - } - } - - @Nullable - private static PathFragment getPlatformMappingsPath(BuildOptions fromOptions) { - return fromOptions.hasNoConfig() - ? null - : fromOptions.get(PlatformOptions.class).platformMappings; + return new BuildConfigurationKeyProducer(this.sink, this.runAfter, transitionedOptions); } }