Skip to content

Commit 56282bb

Browse files
author
Luca Di Grazia
committed
Transformation for build configurations based on a platform/flags mapping.
Introduces a new SkyValue which stores the information obtained from a mapping file (parser yet to be written) and provides logic to transform a build configuration (key) based on that. Step 3/N towards the platforms mapping functionality for bazelbuild/bazel#6426 RELNOTES: None. PiperOrigin-RevId: 238298127
1 parent 4dcff09 commit 56282bb

File tree

6 files changed

+22
-108
lines changed

6 files changed

+22
-108
lines changed

dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/analysis/PlatformConfigurationLoader.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
import com.google.common.collect.ImmutableList;
1818
import com.google.common.collect.ImmutableSet;
19+
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
1920
import com.google.devtools.build.lib.analysis.config.BuildOptions;
2021
import com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory;
21-
import com.google.devtools.build.lib.analysis.config.Fragment;
2222
import com.google.devtools.build.lib.analysis.config.FragmentOptions;
2323
import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException;
2424

@@ -33,17 +33,16 @@ public ImmutableSet<Class<? extends FragmentOptions>> requiredOptions() {
3333
public PlatformConfiguration create(BuildOptions buildOptions)
3434
throws InvalidConfigurationException {
3535
PlatformOptions platformOptions = buildOptions.get(PlatformOptions.class);
36-
3736
return new PlatformConfiguration(
3837
platformOptions.computeHostPlatform(),
3938
ImmutableList.copyOf(platformOptions.extraExecutionPlatforms),
4039
platformOptions.computeTargetPlatform(),
4140
ImmutableList.copyOf(platformOptions.extraToolchains),
42-
platformOptions.targetFilterToAdditionalExecConstraints);
41+
ImmutableList.copyOf(platformOptions.enabledToolchainTypes));
4342
}
4443

4544
@Override
46-
public Class<? extends Fragment> creates() {
45+
public Class<? extends BuildConfiguration.Fragment> creates() {
4746
return PlatformConfiguration.class;
4847
}
4948
}

dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/analysis/PlatformOptions.java

-26
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import com.google.devtools.build.lib.analysis.config.BuildConfiguration.LabelListConverter;
2121
import com.google.devtools.build.lib.analysis.config.FragmentOptions;
2222
import com.google.devtools.build.lib.cmdline.Label;
23-
import com.google.devtools.build.lib.util.OptionsUtils;
24-
import com.google.devtools.build.lib.vfs.PathFragment;
2523
import com.google.devtools.common.options.Converters.CommaSeparatedOptionListConverter;
2624
import com.google.devtools.common.options.Option;
2725
import com.google.devtools.common.options.OptionDocumentationCategory;
@@ -41,13 +39,6 @@ public class PlatformOptions extends FragmentOptions {
4139
public static final Label LEGACY_DEFAULT_TARGET_PLATFORM =
4240
Label.parseAbsoluteUnchecked("@bazel_tools//platforms:target_platform");
4341

44-
/**
45-
* Main workspace-relative location to use when the user does not explicitly set {@code
46-
* --platform_mappings}.
47-
*/
48-
public static final PathFragment DEFAULT_PLATFORM_MAPPINGS =
49-
PathFragment.create("platform_mappings");
50-
5142
@Option(
5243
name = "host_platform",
5344
oldName = "experimental_host_platform",
@@ -178,23 +169,6 @@ public class PlatformOptions extends FragmentOptions {
178169
+ " java_runtime.")
179170
public boolean useToolchainResolutionForJavaRules;
180171

181-
@Option(
182-
name = "platform_mappings",
183-
converter = OptionsUtils.EmptyToNullRelativePathFragmentConverter.class,
184-
defaultValue = "",
185-
documentationCategory = OptionDocumentationCategory.TOOLCHAIN,
186-
effectTags = {
187-
OptionEffectTag.AFFECTS_OUTPUTS,
188-
OptionEffectTag.CHANGES_INPUTS,
189-
OptionEffectTag.LOADING_AND_ANALYSIS
190-
},
191-
help =
192-
"The location of a mapping file that describes which platform to use if none is set or "
193-
+ "which flags to set when a platform already exists. Must be relative to the main "
194-
+ "workspace root. Defaults to 'platform_mappings' (a file directly under the "
195-
+ "workspace root).")
196-
public PathFragment platformMappings;
197-
198172
@Override
199173
public PlatformOptions getHost() {
200174
PlatformOptions host = (PlatformOptions) getDefault();

dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/analysis/config/BuildOptions.java

+8-11
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,10 @@ public BuildOptions applyDiff(OptionsDiffForReconstruction optionsDiff) {
332332
*
333333
* @param parsingResult any options that are being modified
334334
* @return the new options after applying the parsing result to the original options
335+
* @throws OptionsParsingException if a value in the parsing result cannot in fact be parsed
335336
*/
336-
public BuildOptions applyParsingResult(OptionsParsingResult parsingResult) {
337+
public BuildOptions applyParsingResult(OptionsParsingResult parsingResult)
338+
throws OptionsParsingException {
337339
Map<Class<? extends FragmentOptions>, FragmentOptions> modifiedFragments =
338340
toModifiedFragments(parsingResult);
339341

@@ -359,7 +361,7 @@ public BuildOptions applyParsingResult(OptionsParsingResult parsingResult) {
359361
}
360362

361363
private Map<Class<? extends FragmentOptions>, FragmentOptions> toModifiedFragments(
362-
OptionsParsingResult parsingResult) {
364+
OptionsParsingResult parsingResult) throws OptionsParsingException {
363365
Map<Class<? extends FragmentOptions>, FragmentOptions> replacedOptions = new HashMap<>();
364366
for (ParsedOptionDescription parsedOption : parsingResult.asListOfExplicitOptions()) {
365367
OptionDefinition optionDefinition = parsedOption.getOptionDefinition();
@@ -380,9 +382,7 @@ private Map<Class<? extends FragmentOptions>, FragmentOptions> toModifiedFragmen
380382
fragmentOptionClass,
381383
(Class<? extends FragmentOptions> k) -> originalFragment.clone());
382384
try {
383-
Object value =
384-
parsingResult.getOptionValueDescription(optionDefinition.getOptionName()).getValue();
385-
optionDefinition.getField().set(newOptions, value);
385+
optionDefinition.getField().set(newOptions, parsedOption.getConvertedValue());
386386
} catch (IllegalAccessException e) {
387387
throw new IllegalStateException("Couldn't set " + optionDefinition.getField(), e);
388388
}
@@ -471,14 +471,11 @@ public Builder merge(BuildOptions options) {
471471
}
472472

473473
/**
474-
* Adds a new {@link FragmentOptions} instance to the builder.
475-
*
476-
* <p>Overrides previous instances of the exact same subclass of {@code FragmentOptions}.
477-
*
478-
* <p>The options get preprocessed with {@link FragmentOptions#getNormalized}.
474+
* Adds a new FragmentOptions instance to the builder. Overrides previous instances of the exact
475+
* same subclass of FragmentOptions.
479476
*/
480477
public <T extends FragmentOptions> Builder addFragmentOptions(T options) {
481-
fragmentOptions.put(options.getClass(), options.getNormalized());
478+
fragmentOptions.put(options.getClass(), options);
482479
return this;
483480
}
484481

dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/skyframe/PlatformMappingValue.java

+10-48
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import com.google.common.base.Preconditions;
1818
import com.google.common.collect.ImmutableList;
19-
import com.google.common.collect.ImmutableMap;
2019
import com.google.common.collect.Interner;
2120
import com.google.common.collect.Iterables;
2221
import com.google.devtools.build.lib.analysis.PlatformOptions;
@@ -25,7 +24,7 @@
2524
import com.google.devtools.build.lib.concurrent.BlazeInterners;
2625
import com.google.devtools.build.lib.concurrent.ThreadSafety;
2726
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
28-
import com.google.devtools.build.lib.vfs.PathFragment;
27+
import com.google.devtools.build.lib.vfs.RootedPath;
2928
import com.google.devtools.build.skyframe.SkyFunctionName;
3029
import com.google.devtools.build.skyframe.SkyKey;
3130
import com.google.devtools.build.skyframe.SkyValue;
@@ -36,7 +35,6 @@
3635
import java.util.List;
3736
import java.util.Map;
3837
import java.util.Objects;
39-
import javax.annotation.Nullable;
4038

4139
/**
4240
* Stores contents of a platforms/flags mapping file for transforming one {@link
@@ -48,54 +46,22 @@
4846
*/
4947
public final class PlatformMappingValue implements SkyValue {
5048

51-
public static final PlatformMappingValue EMPTY =
52-
new PlatformMappingValue(ImmutableMap.of(), ImmutableMap.of());
53-
5449
/** Key for {@link PlatformMappingValue} based on the location of the mapping file. */
5550
@ThreadSafety.Immutable
5651
@AutoCodec
5752
public static final class Key implements SkyKey {
5853
private static final Interner<Key> interner = BlazeInterners.newWeakInterner();
5954

60-
/**
61-
* Creates a new platform mappings key with the given, main workspace-relative path to the
62-
* mappings file, typically derived from the {@code --platform_mappings} flag.
63-
*
64-
* <p>If the path is {@code null} the {@link PlatformOptions#DEFAULT_PLATFORM_MAPPINGS default
65-
* path} will be used and the key marked as not having been set by a user.
66-
*
67-
* @param workspaceRelativeMappingPath main workspace relative path to the mappings file or
68-
* {@code null} if the default location should be used
69-
*/
70-
public static Key create(@Nullable PathFragment workspaceRelativeMappingPath) {
71-
if (workspaceRelativeMappingPath == null) {
72-
return create(PlatformOptions.DEFAULT_PLATFORM_MAPPINGS, false);
73-
} else {
74-
return create(workspaceRelativeMappingPath, true);
75-
}
76-
}
77-
78-
@AutoCodec.Instantiator
79-
@AutoCodec.VisibleForSerialization
80-
static Key create(PathFragment workspaceRelativeMappingPath, boolean wasExplicitlySetByUser) {
81-
return interner.intern(new Key(workspaceRelativeMappingPath, wasExplicitlySetByUser));
82-
}
83-
84-
private final PathFragment path;
85-
private final boolean wasExplicitlySetByUser;
55+
private final RootedPath path;
8656

87-
private Key(PathFragment path, boolean wasExplicitlySetByUser) {
57+
private Key(RootedPath path) {
8858
this.path = path;
89-
this.wasExplicitlySetByUser = wasExplicitlySetByUser;
90-
}
91-
92-
/** Returns the main-workspace relative path this mapping's mapping file can be found at. */
93-
public PathFragment getWorkspaceRelativeMappingPath() {
94-
return path;
9559
}
9660

97-
public boolean wasExplicitlySetByUser() {
98-
return wasExplicitlySetByUser;
61+
@AutoCodec.VisibleForSerialization
62+
@AutoCodec.Instantiator
63+
static Key create(RootedPath path) {
64+
return interner.intern(new Key(path));
9965
}
10066

10167
@Override
@@ -112,21 +78,17 @@ public boolean equals(Object o) {
11278
return false;
11379
}
11480
Key key = (Key) o;
115-
return Objects.equals(path, key.path) && wasExplicitlySetByUser == key.wasExplicitlySetByUser;
81+
return Objects.equals(path, key.path);
11682
}
11783

11884
@Override
11985
public int hashCode() {
120-
return Objects.hash(path, wasExplicitlySetByUser);
86+
return Objects.hash(path);
12187
}
12288

12389
@Override
12490
public String toString() {
125-
return "PlatformMappingValue.Key{path="
126-
+ path
127-
+ ", wasExplicitlySetByUser="
128-
+ wasExplicitlySetByUser
129-
+ "}";
91+
return "PlatformMappingValue.Key{" + "path=" + path + '}';
13092
}
13193
}
13294

dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/skyframe/SkyFunctions.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public final class SkyFunctions {
118118
public static final SkyFunctionName BUILD_INFO = SkyFunctionName.createHermetic("BUILD_INFO");
119119
public static final SkyFunctionName WORKSPACE_NAME =
120120
SkyFunctionName.createHermetic("WORKSPACE_NAME");
121-
public static final SkyFunctionName PLATFORM_MAPPING =
121+
static final SkyFunctionName PLATFORM_MAPPING =
122122
SkyFunctionName.createHermetic("PLATFORM_MAPPING");
123123
static final SkyFunctionName COVERAGE_REPORT = SkyFunctionName.createHermetic("COVERAGE_REPORT");
124124
public static final SkyFunctionName REPOSITORY = SkyFunctionName.createHermetic("REPOSITORY");

dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/skyframe/PlatformMappingValueTest.java

-18
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.google.devtools.build.lib.analysis.config.CompilationMode;
2828
import com.google.devtools.build.lib.analysis.config.FragmentOptions;
2929
import com.google.devtools.build.lib.cmdline.Label;
30-
import com.google.devtools.build.lib.vfs.PathFragment;
3130
import com.google.devtools.common.options.OptionsParsingException;
3231
import java.util.Collection;
3332
import java.util.Set;
@@ -204,23 +203,6 @@ public void testMapNoMappingIfPlatformIsSetAndNoPlatformMapping() throws Excepti
204203
assertThat(keyForOptions(modifiedOptions)).isEqualTo(mapped);
205204
}
206205

207-
@Test
208-
public void testDefaultKey() {
209-
PlatformMappingValue.Key key = PlatformMappingValue.Key.create(null);
210-
211-
assertThat(key.getWorkspaceRelativeMappingPath())
212-
.isEqualTo(PlatformOptions.DEFAULT_PLATFORM_MAPPINGS);
213-
assertThat(key.wasExplicitlySetByUser()).isFalse();
214-
}
215-
216-
@Test
217-
public void testCustomKey() {
218-
PlatformMappingValue.Key key = PlatformMappingValue.Key.create(PathFragment.create("my/path"));
219-
220-
assertThat(key.getWorkspaceRelativeMappingPath()).isEqualTo(PathFragment.create("my/path"));
221-
assertThat(key.wasExplicitlySetByUser()).isTrue();
222-
}
223-
224206
private BuildOptions toMappedOptions(BuildConfigurationValue.Key mapped) {
225207
return DEFAULT_BUILD_CONFIG_PLATFORM_OPTIONS.applyDiff(mapped.getOptionsDiff());
226208
}

0 commit comments

Comments
 (0)