diff options
author | 2015-10-26 14:42:41 +0000 | |
---|---|---|
committer | 2015-10-27 11:47:09 +0000 | |
commit | ee51bdd08ab3cc883f5e5e90c6f8ecadf8ba1ebb (patch) | |
tree | fc1c98d3a6e96ba31dde5257665b47e7ac8073fa /src/main/java/com | |
parent | e42275c03a1978f4eb5aa97e6a4929606e97bed8 (diff) |
Allow C++ compile actions to run in a sandbox by replacing the LocalGccStrategy / LocalLinkStrategy with SpawnGccStrategy / SpawnLinkStrategy.
RELNOTES: C++ compile actions run in a sandbox now on systems that support sandboxed execution.
--
MOS_MIGRATED_REVID=106299043
Diffstat (limited to 'src/main/java/com')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkAction.java | 4 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionContext.java | 2 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/cpp/Link.java | 2 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/cpp/LinkStrategy.java | 35 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/cpp/SpawnGccStrategy.java (renamed from src/main/java/com/google/devtools/build/lib/rules/cpp/LocalGccStrategy.java) | 61 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/cpp/SpawnLinkStrategy.java (renamed from src/main/java/com/google/devtools/build/lib/rules/cpp/LocalLinkStrategy.java) | 41 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/standalone/StandaloneActionContextProvider.java | 8 |
7 files changed, 55 insertions, 98 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkAction.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkAction.java index 36c197bda6..ccfb2e1ad5 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkAction.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkAction.java @@ -289,9 +289,7 @@ public final class CppLinkAction extends AbstractAction { @Override public String describeStrategy(Executor executor) { - return fake - ? "fake,local" - : executor.getContext(CppLinkActionContext.class).strategyLocality(this); + return fake ? "fake,local" : executor.getContext(CppLinkActionContext.class).strategyLocality(); } // Don't forget to update FAKE_LINK_GUID if you modify this method. diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionContext.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionContext.java index 4f61cff2fd..6e97cfc12a 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionContext.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionContext.java @@ -28,7 +28,7 @@ public interface CppLinkActionContext extends ActionContext { /** * Returns where the action actually runs. */ - String strategyLocality(CppLinkAction action); + String strategyLocality(); /** * Returns the estimated resource consumption of the action. diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/Link.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/Link.java index 7fbcf30ac0..c51a06e533 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/Link.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/Link.java @@ -32,7 +32,7 @@ import java.util.Iterator; * they may mutate the output file rather than overwriting it. * To avoid this, we need to delete the output file before invoking the * command. But that is not done by this class; deleting the output - * file is the responsibility of the classes derived from LinkStrategy. + * file is the responsibility of the classes implementing CppLinkActionContext. */ public abstract class Link { diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkStrategy.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkStrategy.java deleted file mode 100644 index 4bc2719c06..0000000000 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkStrategy.java +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2014 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.rules.cpp; - -/** - * A strategy for executing {@link CppLinkAction}s. - * - * <p>The linker commands, e.g. "ar", are not necessary functional, i.e. - * they may mutate the output file rather than overwriting it. - * To avoid this, we need to delete the output file before invoking the - * command. That must be done by the classes that extend this class. - */ -public abstract class LinkStrategy implements CppLinkActionContext { - public LinkStrategy() { - } - - /** The strategy name, preferably suitable for passing to --link_strategy. */ - public abstract String linkStrategyName(); - - @Override - public String strategyLocality(CppLinkAction execOwner) { - return linkStrategyName(); - } -} diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/LocalGccStrategy.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/SpawnGccStrategy.java index c2edfb293a..83cd41a6a5 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/LocalGccStrategy.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/SpawnGccStrategy.java @@ -1,4 +1,4 @@ -// Copyright 2014 The Bazel Authors. All rights reserved. +// Copyright 2015 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. @@ -11,46 +11,34 @@ // 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.rules.cpp; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.devtools.build.lib.actions.ActionExecutionContext; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.actions.BaseSpawn; import com.google.devtools.build.lib.actions.ExecException; import com.google.devtools.build.lib.actions.ExecutionStrategy; +import com.google.devtools.build.lib.actions.Executor; import com.google.devtools.build.lib.actions.ResourceSet; +import com.google.devtools.build.lib.actions.Spawn; +import com.google.devtools.build.lib.actions.SpawnActionContext; import java.util.Collection; -import java.util.HashMap; -import java.util.Map; /** - * Run gcc locally by delegating to spawn. + * A cpp strategy that simply passes everything through to the default spawn action strategy. */ -@ExecutionStrategy(name = { "local" }, - contextType = CppCompileActionContext.class) -public class LocalGccStrategy implements CppCompileActionContext { - private static final Reply CANNED_REPLY = new Reply() { - @Override - public byte[] getContents() { - throw new IllegalStateException("Remotely computed data requested for local action"); - } - }; - +@ExecutionStrategy( + contextType = CppCompileActionContext.class, + name = {"spawn"} +) +public class SpawnGccStrategy implements CppCompileActionContext { @Override public String strategyLocality() { - return "local"; - } - - public static void updateEnv(CppCompileAction action, Map<String, String> env) { - // We cannot locally execute an action that does not expect to output a .d file, since we would - // have no way to tell what files that it included were used during compilation. - // The exception to this is that if no .d file can be produced (as indicated by - // dotdfile == null), then the assumption is that there are truly no depencies, - // and therefore we don't care whether the step executes locally or remotely. - env.put("INTERCEPT_LOCALLY_EXECUTABLE", - (action.getDotdFile() != null && action.getDotdFile().artifact() == null) ? "0" : "1"); + return "spawn"; } @Override @@ -59,8 +47,9 @@ public class LocalGccStrategy implements CppCompileActionContext { } @Override - public Collection<Artifact> findAdditionalInputs(CppCompileAction action, - ActionExecutionContext actionExecutionContext) throws ExecException, InterruptedException { + public Collection<Artifact> findAdditionalInputs( + CppCompileAction action, ActionExecutionContext actionExecutionContext) + throws ExecException, InterruptedException { return null; } @@ -68,12 +57,16 @@ public class LocalGccStrategy implements CppCompileActionContext { public CppCompileActionContext.Reply execWithReply( CppCompileAction action, ActionExecutionContext actionExecutionContext) throws ExecException, InterruptedException { - Map<String, String> env = new HashMap<>(); - env.putAll(action.getEnvironment()); - updateEnv(action, env); - actionExecutionContext.getExecutor().getSpawnActionContext(action.getMnemonic()) - .exec(new BaseSpawn.Local(action.getArgv(), env, action), - actionExecutionContext); + Executor executor = actionExecutionContext.getExecutor(); + SpawnActionContext spawnActionContext = executor.getSpawnActionContext(action.getMnemonic()); + Spawn spawn = + new BaseSpawn( + action.getArgv(), + action.getEnvironment(), + ImmutableMap.<String, String>of(), + action, + estimateResourceConsumption(action)); + spawnActionContext.exec(spawn, actionExecutionContext); return null; } @@ -90,6 +83,6 @@ public class LocalGccStrategy implements CppCompileActionContext { @Override public Reply getReplyFromException(ExecException e, CppCompileAction action) { - return CANNED_REPLY; + return null; } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/LocalLinkStrategy.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/SpawnLinkStrategy.java index 3bc7d237cc..b64c5a7b9c 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/LocalLinkStrategy.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/SpawnLinkStrategy.java @@ -1,4 +1,4 @@ -// Copyright 2014 The Bazel Authors. All rights reserved. +// Copyright 2015 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. @@ -14,6 +14,7 @@ package com.google.devtools.build.lib.rules.cpp; +import com.google.common.collect.ImmutableMap; import com.google.devtools.build.lib.actions.ActionExecutionContext; import com.google.devtools.build.lib.actions.ActionExecutionException; import com.google.devtools.build.lib.actions.BaseSpawn; @@ -21,36 +22,36 @@ import com.google.devtools.build.lib.actions.ExecException; import com.google.devtools.build.lib.actions.ExecutionStrategy; import com.google.devtools.build.lib.actions.Executor; import com.google.devtools.build.lib.actions.ResourceSet; - -import java.util.List; +import com.google.devtools.build.lib.actions.Spawn; +import com.google.devtools.build.lib.actions.SpawnActionContext; /** - * A link strategy that runs the linking step on the local host. - * - * <p>The set of input files necessary to successfully complete the link is the middleman-expanded - * set of the action's dependency inputs (which includes crosstool and libc dependencies, as - * defined by {@link com.google.devtools.build.lib.rules.cpp.CppHelper#getCrosstoolInputsForLink - * CppHelper.getCrosstoolInputsForLink}). + * A link strategy that simply passes the everything through to the default spawn action strategy. */ -@ExecutionStrategy(contextType = CppLinkActionContext.class, name = { "local" }) -public final class LocalLinkStrategy extends LinkStrategy { - - public LocalLinkStrategy() { - } +@ExecutionStrategy( + contextType = CppLinkActionContext.class, + name = {"spawn"} +) +public final class SpawnLinkStrategy implements CppLinkActionContext { @Override public void exec(CppLinkAction action, ActionExecutionContext actionExecutionContext) throws ExecException, ActionExecutionException, InterruptedException { Executor executor = actionExecutionContext.getExecutor(); - List<String> argv = action.getCommandLine(); - executor.getSpawnActionContext(action.getMnemonic()).exec( - new BaseSpawn.Local(argv, action.getEnvironment(), action), - actionExecutionContext); + SpawnActionContext spawnActionContext = executor.getSpawnActionContext(action.getMnemonic()); + Spawn spawn = + new BaseSpawn( + action.getCommandLine(), + action.getEnvironment(), + ImmutableMap.<String, String>of(), + action, + estimateResourceConsumption(action)); + spawnActionContext.exec(spawn, actionExecutionContext); } @Override - public String linkStrategyName() { - return "local"; + public String strategyLocality() { + return "spawn"; } @Override diff --git a/src/main/java/com/google/devtools/build/lib/standalone/StandaloneActionContextProvider.java b/src/main/java/com/google/devtools/build/lib/standalone/StandaloneActionContextProvider.java index 43bdc5b43a..876ff0e7a9 100644 --- a/src/main/java/com/google/devtools/build/lib/standalone/StandaloneActionContextProvider.java +++ b/src/main/java/com/google/devtools/build/lib/standalone/StandaloneActionContextProvider.java @@ -26,8 +26,8 @@ import com.google.devtools.build.lib.buildtool.BuildRequest; import com.google.devtools.build.lib.exec.ExecutionOptions; import com.google.devtools.build.lib.exec.FileWriteStrategy; import com.google.devtools.build.lib.rules.cpp.IncludeScanningContext; -import com.google.devtools.build.lib.rules.cpp.LocalGccStrategy; -import com.google.devtools.build.lib.rules.cpp.LocalLinkStrategy; +import com.google.devtools.build.lib.rules.cpp.SpawnGccStrategy; +import com.google.devtools.build.lib.rules.cpp.SpawnLinkStrategy; import com.google.devtools.build.lib.rules.test.ExclusiveTestStrategy; import com.google.devtools.build.lib.rules.test.StandaloneTestStrategy; import com.google.devtools.build.lib.rules.test.TestActionContext; @@ -84,10 +84,10 @@ public class StandaloneActionContextProvider extends ActionContextProvider { strategiesBuilder.add( new StandaloneSpawnStrategy(runtime.getExecRoot(), verboseFailures), new DummyIncludeScanningContext(), - new LocalLinkStrategy(), + new SpawnLinkStrategy(), + new SpawnGccStrategy(), testStrategy, new ExclusiveTestStrategy(testStrategy), - new LocalGccStrategy(), new FileWriteStrategy()); this.strategies = strategiesBuilder.build(); |