Skip to content

Commit

Permalink
Make PlatformMappingApplier its own class and rename it.
Browse files Browse the repository at this point in the history
Work towards platform-based flags: #19409.

PiperOrigin-RevId: 570779049
Change-Id: I142c44db575bd3f4b77d369285a9e97f57227271
  • Loading branch information
katre authored and copybara-github committed Oct 4, 2023
1 parent 09d3f06 commit c24b847
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>This includes merging in platform mappings.
*
* <p>The output preserves the iteration order of the input.
*/
class BuildConfigurationKeyProducer implements StateMachine {
interface ResultSink {

void acceptTransitionError(OptionsParsingException e);

void acceptTransitionedConfigurations(
ImmutableMap<String, BuildConfigurationKey> transitionedOptions);
}

// -------------------- Input --------------------
private final ResultSink sink;
private final StateMachine runAfter;
private final Map<String, BuildOptions> options;

// -------------------- Internal State --------------------
private final Map<String, PlatformMappingValue> platformMappingValues = new HashMap<>();

BuildConfigurationKeyProducer(
ResultSink sink, StateMachine runAfter, Map<String, BuildOptions> 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<Optional<PathFragment>, String> index =
Multimaps.index(
options.keySet(), transitionKey -> getPlatformMappingsPath(options.get(transitionKey)));
for (Map.Entry<Optional<PathFragment>, Collection<String>> entry : index.asMap().entrySet()) {
Collection<String> 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.<String, BuildConfigurationKey>builderWithExpectedSize(options.size());
for (Map.Entry<String, BuildOptions> 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<PathFragment> getPlatformMappingsPath(BuildOptions fromOptions) {
return fromOptions.hasNoConfig()
? Optional.empty()
: Optional.ofNullable(fromOptions.get(PlatformOptions.class).platformMappings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -47,13 +37,8 @@
*/
final class TransitionApplier
implements StateMachine, StateMachine.ValueOrExceptionSink<TransitionException> {
interface ResultSink {
void acceptTransitionedConfigurations(
ImmutableMap<String, BuildConfigurationKey> transitionedOptions);

interface ResultSink extends BuildConfigurationKeyProducer.ResultSink {
void acceptTransitionError(TransitionException e);

void acceptTransitionError(OptionsParsingException e);
}

// -------------------- Input --------------------
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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.
*
* <p>The output preserves the iteration order of the input.
*/
private class PlatformMappingApplier implements StateMachine {
// -------------------- Input --------------------
private final Map<String, BuildOptions> options;

// -------------------- Internal State --------------------
private final Map<String, PlatformMappingValue> platformMappingValues = new HashMap<>();

private PlatformMappingApplier(Map<String, BuildOptions> options) {
this.options = options;
}

@Override
public StateMachine step(Tasks tasks) {
// Deduplicates the platform mapping paths and collates the transition keys.
ImmutableListMultimap<Optional<PathFragment>, String> index =
Multimaps.index(
options.keySet(),
transitionKey ->
Optional.ofNullable(getPlatformMappingsPath(options.get(transitionKey))));
for (Map.Entry<Optional<PathFragment>, Collection<String>> entry : index.asMap().entrySet()) {
Collection<String> 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.<String, BuildConfigurationKey>builderWithExpectedSize(options.size());
for (Map.Entry<String, BuildOptions> 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);
}
}

0 comments on commit c24b847

Please sign in to comment.