Skip to content

Commit 8626623

Browse files
lberkiCopybara-Service
authored and
Copybara-Service
committed
Remove BuildConfiguration.Fragment#setupActionEnvironment().
This is accomplished by moving it to ConfiguredRuleClassProvider. This also suggests a neat way to get rid of logic in ShellConfiguration.Loader() by moving the determination of the shell executable, there, too, but not in this change. RELNOTES: None. PiperOrigin-RevId: 192411609
1 parent a65b419 commit 8626623

File tree

8 files changed

+97
-163
lines changed

8 files changed

+97
-163
lines changed

src/main/java/com/google/devtools/build/lib/analysis/ConfiguredRuleClassProvider.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.common.collect.ImmutableMap;
2828
import com.google.common.collect.ImmutableSet;
2929
import com.google.common.collect.ImmutableSortedSet;
30+
import com.google.devtools.build.lib.actions.ActionEnvironment;
3031
import com.google.devtools.build.lib.analysis.buildinfo.BuildInfoFactory;
3132
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
3233
import com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment;
@@ -237,6 +238,8 @@ public static class Builder implements RuleDefinitionEnvironment {
237238
ImmutableList.<Class<?>>builder().addAll(SkylarkModules.MODULES);
238239
private ImmutableList.Builder<NativeProvider> nativeProviders = ImmutableList.builder();
239240
private Set<String> reservedActionMnemonics = new TreeSet<>();
241+
private BuildConfiguration.ActionEnvironmentProvider actionEnvironmentProvider =
242+
(BuildOptions options) -> ActionEnvironment.EMPTY;
240243
private ImmutableBiMap.Builder<String, Class<? extends TransitiveInfoProvider>>
241244
registeredSkylarkProviders = ImmutableBiMap.builder();
242245

@@ -371,6 +374,12 @@ public Builder addReservedActionMnemonic(String mnemonic) {
371374
return this;
372375
}
373376

377+
public Builder setActionEnvironmentProvider(
378+
BuildConfiguration.ActionEnvironmentProvider actionEnvironmentProvider) {
379+
this.actionEnvironmentProvider = actionEnvironmentProvider;
380+
return this;
381+
}
382+
374383
/**
375384
* Sets the C++ LIPO data transition, as defined in {@link
376385
* com.google.devtools.build.lib.rules.cpp.transitions.DisableLipoTransition}.
@@ -471,6 +480,7 @@ public ConfiguredRuleClassProvider build() {
471480
skylarkAccessibleTopLevels.build(),
472481
skylarkModules.build(),
473482
ImmutableSet.copyOf(reservedActionMnemonics),
483+
actionEnvironmentProvider,
474484
nativeProviders.build());
475485
}
476486

@@ -579,6 +589,8 @@ public Label load(String from) {
579589

580590
private final ImmutableSet<String> reservedActionMnemonics;
581591

592+
private final BuildConfiguration.ActionEnvironmentProvider actionEnvironmentProvider;
593+
582594
private final ImmutableList<NativeProvider> nativeProviders;
583595

584596
private final ImmutableMap<String, Class<?>> configurationFragmentMap;
@@ -601,6 +613,7 @@ private ConfiguredRuleClassProvider(
601613
ImmutableMap<String, Object> skylarkAccessibleJavaClasses,
602614
ImmutableList<Class<?>> skylarkModules,
603615
ImmutableSet<String> reservedActionMnemonics,
616+
BuildConfiguration.ActionEnvironmentProvider actionEnvironmentProvider,
604617
ImmutableList<NativeProvider> nativeProviders) {
605618
this.preludeLabel = preludeLabel;
606619
this.runfilesPrefix = runfilesPrefix;
@@ -618,6 +631,7 @@ private ConfiguredRuleClassProvider(
618631
this.prerequisiteValidator = prerequisiteValidator;
619632
this.globals = createGlobals(skylarkAccessibleJavaClasses, skylarkModules);
620633
this.reservedActionMnemonics = reservedActionMnemonics;
634+
this.actionEnvironmentProvider = actionEnvironmentProvider;
621635
this.nativeProviders = nativeProviders;
622636
this.configurationFragmentMap = createFragmentMap(configurationFragmentFactories);
623637
}
@@ -832,6 +846,10 @@ public ImmutableSet<String> getReservedActionMnemonics() {
832846
return reservedActionMnemonics;
833847
}
834848

849+
public BuildConfiguration.ActionEnvironmentProvider getActionEnvironmentProvider() {
850+
return actionEnvironmentProvider;
851+
}
852+
835853
/**
836854
* Returns all registered {@link NativeProvider} instances, i.e. all built-in provider types that
837855
* are based on {@link Provider} rather than {@link TransitiveInfoProvider}.

src/main/java/com/google/devtools/build/lib/analysis/ShellConfiguration.java

Lines changed: 3 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -21,80 +21,34 @@
2121
import com.google.devtools.build.lib.analysis.config.ConfigurationEnvironment;
2222
import com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory;
2323
import com.google.devtools.build.lib.analysis.config.FragmentOptions;
24-
import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext;
25-
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
26-
import com.google.devtools.build.lib.skyframe.serialization.SerializationContext;
27-
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
2824
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
2925
import com.google.devtools.build.lib.util.OS;
3026
import com.google.devtools.build.lib.util.OptionsUtils.PathFragmentConverter;
3127
import com.google.devtools.build.lib.vfs.PathFragment;
3228
import com.google.devtools.common.options.Option;
3329
import com.google.devtools.common.options.OptionDocumentationCategory;
3430
import com.google.devtools.common.options.OptionEffectTag;
35-
import com.google.protobuf.CodedInputStream;
36-
import com.google.protobuf.CodedOutputStream;
37-
import java.io.IOException;
38-
import java.util.Map;
3931
import javax.annotation.Nullable;
4032

4133
/** A configuration fragment that tells where the shell is. */
34+
@AutoCodec
4235
public class ShellConfiguration extends BuildConfiguration.Fragment {
43-
44-
/**
45-
* A codec for {@link ShellConfiguration}.
46-
*
47-
* <p>It does not handle the Bazel version, but that's fine, because we don't want to serialize
48-
* anything in Bazel.
49-
*
50-
* <p>We cannot use {@code AutoCodec} because the field {@link #actionEnvironment} is a lambda.
51-
* That does not necessarily need to be the case, but it's there in support for
52-
* {@link BuildConfiguration.Fragment#setupActionEnvironment()}, which is slated to be removed.
53-
*/
54-
public static final class Codec implements ObjectCodec<ShellConfiguration> {
55-
@Override
56-
public Class<? extends ShellConfiguration> getEncodedClass() {
57-
return ShellConfiguration.class;
58-
}
59-
60-
@Override
61-
public void serialize(SerializationContext context, ShellConfiguration obj,
62-
CodedOutputStream codedOut) throws SerializationException, IOException {
63-
context.serialize(obj.shellExecutable, codedOut);
64-
}
65-
66-
@Override
67-
public ShellConfiguration deserialize(DeserializationContext context, CodedInputStream codedIn)
68-
throws SerializationException, IOException {
69-
PathFragment shellExecutable = context.deserialize(codedIn);
70-
return new ShellConfiguration(shellExecutable, NO_ACTION_ENV.fromOptions(null));
71-
}
72-
}
73-
7436
private static final ImmutableMap<OS, PathFragment> OS_SPECIFIC_SHELL =
7537
ImmutableMap.<OS, PathFragment>builder()
7638
.put(OS.WINDOWS, PathFragment.create("c:/tools/msys64/usr/bin/bash.exe"))
7739
.put(OS.FREEBSD, PathFragment.create("/usr/local/bin/bash"))
7840
.build();
7941

8042
private final PathFragment shellExecutable;
81-
private final ShellActionEnvironment actionEnvironment;
8243

83-
public ShellConfiguration(PathFragment shellExecutable,
84-
ShellActionEnvironment actionEnvironment) {
44+
public ShellConfiguration(PathFragment shellExecutable) {
8545
this.shellExecutable = shellExecutable;
86-
this.actionEnvironment = actionEnvironment;
8746
}
8847

8948
public PathFragment getShellExecutable() {
9049
return shellExecutable;
9150
}
9251

93-
@Override
94-
public void setupActionEnvironment(Map<String, String> builder) {
95-
actionEnvironment.setupActionEnvironment(this, builder);
96-
}
97-
9852
/** An option that tells Bazel where the shell is. */
9953
@AutoCodec(strategy = AutoCodec.Strategy.PUBLIC_FIELDS)
10054
public static class Options extends FragmentOptions {
@@ -123,33 +77,6 @@ public Options getHost() {
12377
}
12478
}
12579

126-
/**
127-
* Encapsulates the contributions of {@link ShellConfiguration} to the action environment.
128-
*
129-
* <p>This is done this way because we need the shell environment to be different between Bazel
130-
* and Blaze, but configuration fragments are handed out based on their classes, thus,
131-
* doing this with inheritance would be difficult. The good old "has-a instead of this-a" pattern.
132-
*/
133-
public interface ShellActionEnvironment {
134-
void setupActionEnvironment(ShellConfiguration configuration, Map<String, String> builder);
135-
}
136-
137-
/**
138-
* A factory for shell action environments.
139-
*
140-
* <p>This is necessary because the Bazel shell action environment depends on whether we use
141-
* strict action environments or not, but we cannot simply hardcode the dependency on that bit
142-
* here because it doesn't exist in Blaze. Thus, during configuration creation time, we call this
143-
* factory which returns an object, which, when called, updates the actual action environment.
144-
*/
145-
public interface ShellActionEnvironmentFactory {
146-
ShellActionEnvironment fromOptions(BuildOptions options);
147-
}
148-
149-
/** A {@link ShellConfiguration} that contributes nothing to the action environment. */
150-
public static final ShellActionEnvironmentFactory NO_ACTION_ENV =
151-
(BuildOptions options) -> (ShellConfiguration config, Map<String, String> builder) -> {};
152-
15380
/** the part of {@link ShellConfiguration} that determines where the shell is. */
15481
public interface ShellExecutableProvider {
15582
PathFragment getShellExecutable(BuildOptions options);
@@ -163,23 +90,18 @@ public static ShellExecutableProvider hardcodedShellExecutable(String shell) {
16390
/** The loader for {@link ShellConfiguration}. */
16491
public static class Loader implements ConfigurationFragmentFactory {
16592
private final ShellExecutableProvider shellExecutableProvider;
166-
private final ShellActionEnvironmentFactory actionEnvironmentFactory;
16793
private final ImmutableSet<Class<? extends FragmentOptions>> requiredOptions;
16894

16995
public Loader(ShellExecutableProvider shellExecutableProvider,
170-
ShellActionEnvironmentFactory actionEnvironmentFactory,
17196
Class<? extends FragmentOptions>... requiredOptions) {
17297
this.shellExecutableProvider = shellExecutableProvider;
173-
this.actionEnvironmentFactory = actionEnvironmentFactory;
17498
this.requiredOptions = ImmutableSet.copyOf(requiredOptions);
17599
}
176100

177101
@Nullable
178102
@Override
179103
public Fragment create(ConfigurationEnvironment env, BuildOptions buildOptions) {
180-
return new ShellConfiguration(
181-
shellExecutableProvider.getShellExecutable(buildOptions),
182-
actionEnvironmentFactory.fromOptions(buildOptions));
104+
return new ShellConfiguration(shellExecutableProvider.getShellExecutable(buildOptions));
183105
}
184106

185107
public static PathFragment determineShellExecutable(

src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ public class BuildConfiguration {
123123
private static final Interner<ImmutableSortedMap<Class<? extends Fragment>, Fragment>>
124124
fragmentsInterner = BlazeInterners.newWeakInterner();
125125

126+
/** Compute the default shell environment for actions from the command line options. */
127+
public interface ActionEnvironmentProvider {
128+
ActionEnvironment getActionEnvironment(BuildOptions options);
129+
}
130+
126131
/**
127132
* An interface for language-specific configurations.
128133
*
@@ -156,14 +161,6 @@ public String getOutputDirectoryName() {
156161
return null;
157162
}
158163

159-
/**
160-
* Add items to the action environment.
161-
*
162-
* @param builder the map to add environment variables to
163-
*/
164-
public void setupActionEnvironment(Map<String, String> builder) {
165-
}
166-
167164
/**
168165
* Returns { 'option name': 'alternative default' } entries for options where the
169166
* "real default" should be something besides the default specified in the {@link Option}
@@ -1218,27 +1215,6 @@ public boolean apply(@Nullable Fragment fragment) {
12181215
});
12191216
}
12201217

1221-
/**
1222-
* Compute the shell environment, which, at configuration level, is a pair consisting of the
1223-
* statically set environment variables with their values and the set of environment variables to
1224-
* be inherited from the client environment.
1225-
*/
1226-
private ActionEnvironment setupActionEnvironment() {
1227-
// We make a copy first to remove duplicate entries; last one wins.
1228-
Map<String, String> actionEnv = new HashMap<>();
1229-
// TODO(ulfjack): Remove all env variables from configuration fragments.
1230-
for (Fragment fragment : fragments.values()) {
1231-
fragment.setupActionEnvironment(actionEnv);
1232-
}
1233-
// Shell environment variables specified via options take precedence over the
1234-
// ones inherited from the fragments. In the long run, these fragments will
1235-
// be replaced by appropriate default rc files anyway.
1236-
for (Map.Entry<String, String> entry : options.actionEnvironment) {
1237-
actionEnv.put(entry.getKey(), entry.getValue());
1238-
}
1239-
return ActionEnvironment.split(actionEnv);
1240-
}
1241-
12421218
/**
12431219
* Compute the test environment, which, at configuration level, is a pair consisting of the
12441220
* statically set environment variables with their values and the set of environment variables to
@@ -1265,6 +1241,7 @@ public BuildConfiguration(
12651241
BuildOptions buildOptions,
12661242
BuildOptions.OptionsDiffForReconstruction buildOptionsDiff,
12671243
ImmutableSet<String> reservedActionMnemonics,
1244+
ActionEnvironment actionEnvironment,
12681245
String repositoryName) {
12691246
this.directories = directories;
12701247
this.fragments = makeFragmentsMap(fragmentsMap);
@@ -1314,7 +1291,7 @@ public BuildConfiguration(
13141291
OutputDirectory.MIDDLEMAN.getRoot(
13151292
RepositoryName.MAIN, outputDirName, directories, mainRepositoryName);
13161293

1317-
this.actionEnv = setupActionEnvironment();
1294+
this.actionEnv = actionEnvironment;
13181295

13191296
this.testEnv = setupTestEnvironment();
13201297

@@ -1367,6 +1344,7 @@ public BuildConfiguration clone(
13671344
options,
13681345
BuildOptions.diffForReconstruction(defaultBuildOptions, options),
13691346
reservedActionMnemonics,
1347+
actionEnv,
13701348
mainRepositoryName.strippedName());
13711349
return newConfig;
13721350
}

0 commit comments

Comments
 (0)