Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package com.google.devtools.build.lib.analysis.config;

import static com.google.common.collect.ImmutableMap.toImmutableMap;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
Expand Down Expand Up @@ -941,11 +940,6 @@ public List<Label> getTargetEnvironments() {
return options.targetEnvironments;
}

public ImmutableMap<String, String> getCommandLineFlagAliases() {
return options.commandLineFlagAliases.stream()
.collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
}

@Nullable
public Class<? extends Fragment> getStarlarkFragmentByName(String name) {
return starlarkVisibleFragments.get(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static com.google.devtools.build.lib.packages.Type.STRING;
import static com.google.devtools.build.lib.packages.Types.STRING_LIST;
import static com.google.devtools.build.lib.packages.Types.STRING_SET;
import static com.google.devtools.common.options.OptionsParser.STARLARK_SKIPPED_PREFIXES;
import static java.util.stream.Collectors.joining;

import com.google.common.base.Preconditions;
Expand All @@ -47,6 +48,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import net.starlark.java.eval.StarlarkInt;
import net.starlark.java.eval.StarlarkValue;
Expand All @@ -57,6 +59,13 @@
*/
public class CoreOptionConverters {

/**
* The name of the flag used for shorthand aliasing in blaze. {@link
* com.google.devtools.build.lib.analysis.config.CoreOptions#commandLineFlagAliases} for the
* option definition.
*/
public static final String BLAZE_ALIASING_FLAG = "flag_alias";

// Not instantiable.
private CoreOptionConverters() {}

Expand Down Expand Up @@ -350,4 +359,49 @@ private static Label convertOptionsLabel(String input, @Nullable Object conversi
throw new OptionsParsingException(e.getMessage());
}
}

/**
* A converter for command line flag aliases. It does additional validation on the name and value
* of the assignment to ensure they conform to the naming limitations.
*/
public static class FlagAliasConverter implements Converter<Map.Entry<String, Label>> {

@Override
public Map.Entry<String, Label> convert(String input, @Nullable Object conversionContext)
throws OptionsParsingException {
int pos = input.indexOf("=");
if (pos <= 0) {
throw new OptionsParsingException(
"Flag alias definitions must be in the form of a 'name=label' assignment");
}
String shortForm = input.substring(0, pos);
String longForm = input.substring(pos + 1);

String cmdLineAlias = "--" + BLAZE_ALIASING_FLAG + "=" + input;

if (!Pattern.matches("\\w*", shortForm)) {
throw new OptionsParsingException(
shortForm + " should only consist of word characters to be a valid alias name.",
cmdLineAlias);
}
if (longForm.contains("=")) {
throw new OptionsParsingException(
"--" + BLAZE_ALIASING_FLAG + " does not support flag value assignment.", cmdLineAlias);
}

// Remove this check if native options are permitted to be aliased
String longFormWithDashes = "--" + longForm;
if (STARLARK_SKIPPED_PREFIXES.stream().noneMatch(longFormWithDashes::startsWith)) {
throw new OptionsParsingException(
"--" + BLAZE_ALIASING_FLAG + " only supports Starlark build settings.", cmdLineAlias);
}

return Maps.immutableEntry(shortForm, convertOptionsLabel(longForm, conversionContext));
}

@Override
public String getTypeDescription() {
return "a 'name=label' flag alias";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static java.util.Map.Entry.comparingByKey;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand All @@ -36,7 +37,6 @@
import com.google.devtools.common.options.OptionsParsingException;
import com.google.devtools.common.options.TriState;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -1059,7 +1059,7 @@ public OutputPathsConverter() {

@Option(
name = "flag_alias",
converter = Converters.FlagAliasConverter.class,
converter = CoreOptionConverters.FlagAliasConverter.class,
defaultValue = "null",
allowMultiple = true,
documentationCategory = OptionDocumentationCategory.GENERIC_INPUTS,
Expand All @@ -1070,7 +1070,7 @@ public OutputPathsConverter() {
Sets a shorthand name for a Starlark flag. It takes a single key-value pair in the form
`{key}={value}` as an argument.
""")
public List<Map.Entry<String, String>> commandLineFlagAliases;
public List<Map.Entry<String, Label>> commandLineFlagAliases;

@Option(
name = "archived_tree_artifact_mnemonics_filter",
Expand Down Expand Up @@ -1155,6 +1155,20 @@ public boolean usePlatformInOutputDir(Label platform) {
return false;
}

private volatile ImmutableMap<String, Label> commandLineFlagAliasesMap = null;

public ImmutableMap<String, Label> getCommandLineFlagAliases() {
// Force a single copy of the map to avoid repeated conversions.
if (commandLineFlagAliasesMap == null) {
synchronized (this) {
if (commandLineFlagAliasesMap == null) {
commandLineFlagAliasesMap = ImmutableMap.copyOf(commandLineFlagAliases);
}
}
}
return commandLineFlagAliasesMap;
}

/** Ways configured targets may provide the {@link Fragment}s they require. */
public enum IncludeConfigFragmentsEnum implements StarlarkValue {
/**
Expand Down Expand Up @@ -1183,10 +1197,9 @@ public ImmutableMap<String, String> getNormalizedCommandLineBuildVariables() {
}

// Sort the map entries by key.
private static List<Map.Entry<String, String>> sortEntries(
List<Map.Entry<String, String>> entries) {
ImmutableList<Map.Entry<String, String>> sortedEntries =
entries.stream().sorted(Comparator.comparing(Map.Entry::getKey)).collect(toImmutableList());
private static <V> List<Map.Entry<String, V>> sortEntries(List<Map.Entry<String, V>> entries) {
ImmutableList<Map.Entry<String, V>> sortedEntries =
entries.stream().sorted(comparingByKey()).collect(toImmutableList());
// If we made no changes, return the same instance we got to reduce churn.
if (sortedEntries.equals(entries)) {
return entries;
Expand Down Expand Up @@ -1243,4 +1256,13 @@ public CoreOptions getNormalized() {

return result;
}

@Override
public CoreOptions clone() {
CoreOptions base = (CoreOptions) super.clone();
// commandLineFlagAliasesMap is derived from commandLineFlagAliases, so it must be cleared
// here to ensure it is recomputed in case commandLineFlagAliases is mutated after cloning.
base.commandLineFlagAliasesMap = null;
return base;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ protected static ImmutableList<String> dedupAndSort(@Nullable List<String> value
* Helper method for subclasses to normalize list of map entries by keeping only the last entry
* for each key. The order of the entries is preserved.
*/
protected static List<Map.Entry<String, String>> normalizeEntries(
List<Map.Entry<String, String>> entries) {
LinkedHashMap<String, String> normalizedEntries = new LinkedHashMap<>();
for (Map.Entry<String, String> entry : entries) {
protected static <V> List<Map.Entry<String, V>> normalizeEntries(
List<Map.Entry<String, V>> entries) {
LinkedHashMap<String, V> normalizedEntries = new LinkedHashMap<>();
for (Map.Entry<String, V> entry : entries) {
normalizedEntries.put(entry.getKey(), entry.getValue());
}
// If we made no changes, return the same instance we got to reduce churn.
Expand Down
Loading