Skip to content

Commit a495bae

Browse files
laszlocsomorCopybara-Service
authored and
Copybara-Service
committed
Extract LocalEnvProvider members to classes.
Create a PosixLocalEnvProvider and WindowsLocalEnvProvider class, with singleton instances for now. This refactoring should not change functionality, it's just a requirement for an upcoming change. That upcoming change is for these classes to respect the client environment's TMPDIR or TMP/TEMP envvars. See bazelbuild#4376 Change-Id: I032bb6f18adf8af9e43e6bc543c09c58adae3863 PiperOrigin-RevId: 180799936
1 parent f475101 commit a495bae

9 files changed

+102
-41
lines changed

src/main/java/com/google/devtools/build/lib/exec/local/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ java_library(
1111
srcs = [
1212
"LocalEnvProvider.java",
1313
"LocalSpawnRunner.java",
14+
"PosixLocalEnvProvider.java",
15+
"WindowsLocalEnvProvider.java",
1416
],
1517
data = [
1618
"//src/main/tools:process-wrapper",

src/main/java/com/google/devtools/build/lib/exec/local/LocalEnvProvider.java

-30
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
// limitations under the License.
1414
package com.google.devtools.build.lib.exec.local;
1515

16-
import com.google.common.collect.ImmutableMap;
17-
import com.google.common.collect.Maps;
1816
import com.google.devtools.build.lib.vfs.Path;
1917
import java.io.IOException;
2018
import java.util.Map;
@@ -35,34 +33,6 @@ public Map<String, String> rewriteLocalEnv(
3533
}
3634
};
3735

38-
public static final LocalEnvProvider ADD_TEMP_POSIX =
39-
new LocalEnvProvider() {
40-
@Override
41-
public Map<String, String> rewriteLocalEnv(
42-
Map<String, String> env, Path execRoot, Path tmpDir, String productName)
43-
throws IOException {
44-
ImmutableMap.Builder<String, String> result = ImmutableMap.builder();
45-
result.putAll(Maps.filterKeys(env, k -> !k.equals("TMPDIR")));
46-
result.put("TMPDIR", tmpDir.getPathString());
47-
return result.build();
48-
}
49-
};
50-
51-
public static final LocalEnvProvider ADD_TEMP_WINDOWS =
52-
new LocalEnvProvider() {
53-
@Override
54-
public Map<String, String> rewriteLocalEnv(
55-
Map<String, String> env, Path execRoot, Path tmpDir, String productName)
56-
throws IOException {
57-
ImmutableMap.Builder<String, String> result = ImmutableMap.builder();
58-
result.putAll(Maps.filterKeys(env, k -> !k.equals("TMP") && !k.equals("TEMP")));
59-
String tmpPath = tmpDir.getPathString().replace('/', '\\');
60-
result.put("TMP", tmpPath);
61-
result.put("TEMP", tmpPath);
62-
return result.build();
63-
}
64-
};
65-
6636
/** Rewrites the environment if necessary. */
6737
Map<String, String> rewriteLocalEnv(
6838
Map<String, String> env, Path execRoot, Path tmpDir, String productName) throws IOException;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2018 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.google.devtools.build.lib.exec.local;
15+
16+
import com.google.common.collect.ImmutableMap;
17+
import com.google.common.collect.Maps;
18+
import com.google.devtools.build.lib.vfs.Path;
19+
import java.io.IOException;
20+
import java.util.Map;
21+
22+
/** {@link LocalEnvProvider} implementation for actions running on Unix-like platforms. */
23+
public final class PosixLocalEnvProvider implements LocalEnvProvider {
24+
25+
public static final PosixLocalEnvProvider INSTANCE = new PosixLocalEnvProvider();
26+
27+
/**
28+
* Compute an environment map for local actions on Unix-like platforms (e.g. Linux, macOS).
29+
*
30+
* <p>Returns a map with the same keys and values as {@code env}. Overrides the value of TMPDIR
31+
* (or adds it if not present in {@code env}) by {@code tmpDir}.
32+
*/
33+
@Override
34+
public Map<String, String> rewriteLocalEnv(
35+
Map<String, String> env, Path execRoot, Path tmpDir, String productName) throws IOException {
36+
ImmutableMap.Builder<String, String> result = ImmutableMap.builder();
37+
result.putAll(Maps.filterKeys(env, k -> !k.equals("TMPDIR")));
38+
result.put("TMPDIR", tmpDir.getPathString());
39+
return result.build();
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2018 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.google.devtools.build.lib.exec.local;
15+
16+
import com.google.common.collect.ImmutableMap;
17+
import com.google.common.collect.Maps;
18+
import com.google.devtools.build.lib.vfs.Path;
19+
import java.io.IOException;
20+
import java.util.Map;
21+
22+
/** {@link LocalEnvProvider} implementation for actions running on Windows. */
23+
public final class WindowsLocalEnvProvider implements LocalEnvProvider {
24+
25+
public static final WindowsLocalEnvProvider INSTANCE = new WindowsLocalEnvProvider();
26+
27+
/**
28+
* Compute an environment map for local actions on Windows.
29+
*
30+
* <p>Returns a map with the same keys and values as {@code env}. Overrides the value of TMP and
31+
* TEMP (or adds them if not present in {@code env}) by {@code tmpDir}.
32+
*
33+
* <p>The values for TMP and TEMP will use backslashes as directory separators.
34+
*/
35+
@Override
36+
public Map<String, String> rewriteLocalEnv(
37+
Map<String, String> env, Path execRoot, Path tmpDir, String productName) throws IOException {
38+
ImmutableMap.Builder<String, String> result = ImmutableMap.builder();
39+
result.putAll(Maps.filterKeys(env, k -> !k.equals("TMP") && !k.equals("TEMP")));
40+
String tmpPath = tmpDir.getPathString().replace('/', '\\');
41+
result.put("TMP", tmpPath);
42+
result.put("TEMP", tmpPath);
43+
return result.build();
44+
}
45+
}

src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedSpawnRunner.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.devtools.build.lib.actions.UserExecException;
2828
import com.google.devtools.build.lib.analysis.BlazeDirectories;
2929
import com.google.devtools.build.lib.exec.local.LocalEnvProvider;
30+
import com.google.devtools.build.lib.exec.local.PosixLocalEnvProvider;
3031
import com.google.devtools.build.lib.runtime.CommandEnvironment;
3132
import com.google.devtools.build.lib.runtime.LinuxSandboxUtil;
3233
import com.google.devtools.build.lib.shell.Command;
@@ -166,7 +167,7 @@ public static boolean isSupported(CommandEnvironment cmdEnv) {
166167
this.inaccessibleHelperFile = inaccessibleHelperFile;
167168
this.inaccessibleHelperDir = inaccessibleHelperDir;
168169
this.timeoutKillDelay = timeoutKillDelay;
169-
this.localEnvProvider = LocalEnvProvider.ADD_TEMP_POSIX;
170+
this.localEnvProvider = PosixLocalEnvProvider.INSTANCE;
170171
}
171172

172173
@Override

src/main/java/com/google/devtools/build/lib/sandbox/ProcessWrapperSandboxedSpawnRunner.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.devtools.build.lib.actions.SpawnResult;
2020
import com.google.devtools.build.lib.exec.apple.XCodeLocalEnvProvider;
2121
import com.google.devtools.build.lib.exec.local.LocalEnvProvider;
22+
import com.google.devtools.build.lib.exec.local.PosixLocalEnvProvider;
2223
import com.google.devtools.build.lib.runtime.CommandEnvironment;
2324
import com.google.devtools.build.lib.runtime.ProcessWrapperUtil;
2425
import com.google.devtools.build.lib.util.OS;
@@ -88,9 +89,7 @@ public static boolean isSupported(CommandEnvironment cmdEnv) {
8889
this.timeoutKillDelay = timeoutKillDelay;
8990
this.processWrapper = ProcessWrapperUtil.getProcessWrapper(cmdEnv);
9091
this.localEnvProvider =
91-
OS.getCurrent() == OS.DARWIN
92-
? new XCodeLocalEnvProvider()
93-
: LocalEnvProvider.ADD_TEMP_POSIX;
92+
OS.getCurrent() == OS.DARWIN ? new XCodeLocalEnvProvider() : PosixLocalEnvProvider.INSTANCE;
9493
}
9594

9695
@Override

src/main/java/com/google/devtools/build/lib/sandbox/SandboxActionContextProvider.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.devtools.build.lib.exec.local.LocalEnvProvider;
2828
import com.google.devtools.build.lib.exec.local.LocalExecutionOptions;
2929
import com.google.devtools.build.lib.exec.local.LocalSpawnRunner;
30+
import com.google.devtools.build.lib.exec.local.PosixLocalEnvProvider;
3031
import com.google.devtools.build.lib.runtime.CommandEnvironment;
3132
import com.google.devtools.build.lib.util.OS;
3233
import com.google.devtools.build.lib.vfs.Path;
@@ -98,9 +99,7 @@ private static SpawnRunner createFallbackRunner(CommandEnvironment env) {
9899
LocalExecutionOptions localExecutionOptions =
99100
env.getOptions().getOptions(LocalExecutionOptions.class);
100101
LocalEnvProvider localEnvProvider =
101-
OS.getCurrent() == OS.DARWIN
102-
? new XCodeLocalEnvProvider()
103-
: LocalEnvProvider.ADD_TEMP_POSIX;
102+
OS.getCurrent() == OS.DARWIN ? new XCodeLocalEnvProvider() : PosixLocalEnvProvider.INSTANCE;
104103
return
105104
new LocalSpawnRunner(
106105
env.getExecRoot(),

src/main/java/com/google/devtools/build/lib/standalone/StandaloneActionContextProvider.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import com.google.devtools.build.lib.exec.local.LocalEnvProvider;
3333
import com.google.devtools.build.lib.exec.local.LocalExecutionOptions;
3434
import com.google.devtools.build.lib.exec.local.LocalSpawnRunner;
35+
import com.google.devtools.build.lib.exec.local.PosixLocalEnvProvider;
36+
import com.google.devtools.build.lib.exec.local.WindowsLocalEnvProvider;
3537
import com.google.devtools.build.lib.rules.cpp.IncludeScanningContext;
3638
import com.google.devtools.build.lib.rules.cpp.SpawnGccStrategy;
3739
import com.google.devtools.build.lib.rules.test.ExclusiveTestStrategy;
@@ -104,8 +106,8 @@ private static SpawnRunner createLocalRunner(CommandEnvironment env) {
104106
OS.getCurrent() == OS.DARWIN
105107
? new XCodeLocalEnvProvider()
106108
: (OS.getCurrent() == OS.WINDOWS
107-
? LocalEnvProvider.ADD_TEMP_WINDOWS
108-
: LocalEnvProvider.ADD_TEMP_POSIX);
109+
? WindowsLocalEnvProvider.INSTANCE
110+
: PosixLocalEnvProvider.INSTANCE);
109111
return
110112
new LocalSpawnRunner(
111113
env.getExecRoot(),

src/main/java/com/google/devtools/build/lib/worker/WorkerActionContextProvider.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import com.google.devtools.build.lib.exec.local.LocalEnvProvider;
2525
import com.google.devtools.build.lib.exec.local.LocalExecutionOptions;
2626
import com.google.devtools.build.lib.exec.local.LocalSpawnRunner;
27+
import com.google.devtools.build.lib.exec.local.PosixLocalEnvProvider;
28+
import com.google.devtools.build.lib.exec.local.WindowsLocalEnvProvider;
2729
import com.google.devtools.build.lib.runtime.CommandEnvironment;
2830
import com.google.devtools.build.lib.util.OS;
2931

@@ -58,8 +60,8 @@ private static SpawnRunner createFallbackRunner(CommandEnvironment env) {
5860
OS.getCurrent() == OS.DARWIN
5961
? new XCodeLocalEnvProvider()
6062
: (OS.getCurrent() == OS.WINDOWS
61-
? LocalEnvProvider.ADD_TEMP_WINDOWS
62-
: LocalEnvProvider.ADD_TEMP_POSIX);
63+
? WindowsLocalEnvProvider.INSTANCE
64+
: PosixLocalEnvProvider.INSTANCE);
6365
return new LocalSpawnRunner(
6466
env.getExecRoot(),
6567
localExecutionOptions,

0 commit comments

Comments
 (0)