diff options
author | 2017-06-13 12:56:07 +0200 | |
---|---|---|
committer | 2017-06-13 17:13:17 +0200 | |
commit | 1d62c67a2d6d6eccf415ad5647d860d08f4c5966 (patch) | |
tree | 23eeec1aa45038fc5693b09c8d2556df5f92aa59 /src/main/java/com/google/devtools/build/lib/standalone | |
parent | 3e87c626ed76536420aa06e4c258209b32bb76e0 (diff) |
Extract the MacOS/XCode env rewrite logic into lib.exec.apple
Also add an interface to allow injecting that logic into LocalSpawnRunner; this
is in preparation for rewriting StandaloneSpawnStrategy to use
LocalSpawnRunner.
At the same time, this reduces the dependencies from exec / standalone to
rules.apple, which is a prerequisite for micro-Bazel.
There's a small semantic change hidden here - we now only set the new
XCodeLocalEnvProvider if we're actually running on Darwin, so we no longer
fail execution on non-Darwin platforms if XCODE_VERSION_OVERRIDE or
APPLE_SDK_VERSION_OVERRIDE is set. As a result, I moved the corresponding test
from StandaloneSpawnStrategyTest to the new XCodeLocalEnvProviderTest.
While I'm at it, also open source DottedVersionTest and CacheManagerTest.
PiperOrigin-RevId: 158829077
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/standalone')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/standalone/BUILD | 3 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/standalone/StandaloneSpawnStrategy.java | 89 |
2 files changed, 16 insertions, 76 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/standalone/BUILD b/src/main/java/com/google/devtools/build/lib/standalone/BUILD index 0b2171e25e..64e70e52c7 100644 --- a/src/main/java/com/google/devtools/build/lib/standalone/BUILD +++ b/src/main/java/com/google/devtools/build/lib/standalone/BUILD @@ -12,12 +12,13 @@ java_library( "//src/main/java/com/google/devtools/build/lib:build-base", "//src/main/java/com/google/devtools/build/lib:io", "//src/main/java/com/google/devtools/build/lib:packages-internal", - "//src/main/java/com/google/devtools/build/lib:process_util", "//src/main/java/com/google/devtools/build/lib:runtime", "//src/main/java/com/google/devtools/build/lib:shell", "//src/main/java/com/google/devtools/build/lib:util", "//src/main/java/com/google/devtools/build/lib:vfs", "//src/main/java/com/google/devtools/build/lib/actions", + "//src/main/java/com/google/devtools/build/lib/exec/apple", + "//src/main/java/com/google/devtools/build/lib/exec/local", "//src/main/java/com/google/devtools/build/lib/rules/apple", "//src/main/java/com/google/devtools/build/lib/rules/cpp", "//third_party:guava", diff --git a/src/main/java/com/google/devtools/build/lib/standalone/StandaloneSpawnStrategy.java b/src/main/java/com/google/devtools/build/lib/standalone/StandaloneSpawnStrategy.java index fe9774d760..724542ff1b 100644 --- a/src/main/java/com/google/devtools/build/lib/standalone/StandaloneSpawnStrategy.java +++ b/src/main/java/com/google/devtools/build/lib/standalone/StandaloneSpawnStrategy.java @@ -13,7 +13,6 @@ // limitations under the License. package com.google.devtools.build.lib.standalone; -import com.google.common.collect.ImmutableMap; import com.google.common.eventbus.EventBus; import com.google.devtools.build.lib.actions.ActionExecutionContext; import com.google.devtools.build.lib.actions.ActionExecutionMetadata; @@ -27,9 +26,8 @@ import com.google.devtools.build.lib.actions.Spawn; import com.google.devtools.build.lib.actions.SpawnActionContext; import com.google.devtools.build.lib.actions.Spawns; import com.google.devtools.build.lib.actions.UserExecException; -import com.google.devtools.build.lib.rules.apple.AppleConfiguration; -import com.google.devtools.build.lib.rules.apple.AppleHostInfo; -import com.google.devtools.build.lib.rules.apple.DottedVersion; +import com.google.devtools.build.lib.exec.apple.XCodeLocalEnvProvider; +import com.google.devtools.build.lib.exec.local.LocalEnvProvider; import com.google.devtools.build.lib.shell.AbnormalTerminationException; import com.google.devtools.build.lib.shell.Command; import com.google.devtools.build.lib.shell.CommandException; @@ -40,6 +38,7 @@ import com.google.devtools.build.lib.util.OsUtils; import com.google.devtools.build.lib.util.io.FileOutErr; import com.google.devtools.build.lib.vfs.Path; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -53,6 +52,7 @@ public class StandaloneSpawnStrategy implements SpawnActionContext { private final Path execRoot; private final String productName; private final ResourceManager resourceManager; + private final LocalEnvProvider localEnvProvider; public StandaloneSpawnStrategy(Path execRoot, boolean verboseFailures, String productName) { this(execRoot, verboseFailures, productName, ResourceManager.instance()); @@ -66,6 +66,9 @@ public class StandaloneSpawnStrategy implements SpawnActionContext { "_bin/process-wrapper" + OsUtils.executableExtension()); this.productName = productName; this.resourceManager = resourceManager; + this.localEnvProvider = OS.getCurrent() == OS.DARWIN + ? new XCodeLocalEnvProvider() + : LocalEnvProvider.UNMODIFIED; } /** @@ -81,7 +84,11 @@ public class StandaloneSpawnStrategy implements SpawnActionContext { try (ResourceHandle handle = resourceManager.acquireResources(owner, spawn.getLocalResources())) { eventBus.post(ActionStatusMessage.runningStrategy(owner, "standalone")); - actuallyExec(spawn, actionExecutionContext); + try { + actuallyExec(spawn, actionExecutionContext); + } catch (IOException e) { + throw new UserExecException("I/O exception during local execution", e); + } } } @@ -90,7 +97,7 @@ public class StandaloneSpawnStrategy implements SpawnActionContext { */ private void actuallyExec(Spawn spawn, ActionExecutionContext actionExecutionContext) - throws ExecException { + throws ExecException, IOException { Executor executor = actionExecutionContext.getExecutor(); if (executor.reportsSubcommands()) { @@ -123,7 +130,7 @@ public class StandaloneSpawnStrategy implements SpawnActionContext { Command cmd = new Command( args.toArray(new String[] {}), - locallyDeterminedEnv(execRoot, productName, spawn.getEnvironment()), + localEnvProvider.rewriteLocalEnv(spawn.getEnvironment(), execRoot, productName), new File(cwd), OS.getCurrent() == OS.WINDOWS && timeoutSeconds >= 0 ? timeoutSeconds * 1000 : -1); @@ -155,74 +162,6 @@ public class StandaloneSpawnStrategy implements SpawnActionContext { return "standalone"; } - /** - * Adds to the given environment all variables that are dependent on system state of the host - * machine. - * - * <p>Admittedly, hermeticity is "best effort" in such cases; these environment values should be - * as tied to configuration parameters as possible. - * - * <p>For example, underlying iOS toolchains require that SDKROOT resolve to an absolute system - * path, but, when selecting which SDK to resolve, the version number comes from build - * configuration. - * - * @return the new environment, comprised of the old environment plus any new variables - * @throws UserExecException if any variables dependent on system state could not be resolved - */ - public static ImmutableMap<String, String> locallyDeterminedEnv( - Path execRoot, String productName, ImmutableMap<String, String> env) - throws UserExecException { - // TODO(bazel-team): Remove apple-specific logic from this class. - ImmutableMap.Builder<String, String> newEnvBuilder = ImmutableMap.builder(); - newEnvBuilder.putAll(env); - // Empty developer dir indicates to use the system default. - // TODO(bazel-team): Bazel's view of the xcode version and developer dir - // should be explicitly set for build hermiticity. - String developerDir = ""; - if (env.containsKey(AppleConfiguration.XCODE_VERSION_ENV_NAME)) { - developerDir = - getDeveloperDir( - execRoot, productName, env.get(AppleConfiguration.XCODE_VERSION_ENV_NAME)); - newEnvBuilder.put("DEVELOPER_DIR", developerDir); - } - if (env.containsKey(AppleConfiguration.APPLE_SDK_VERSION_ENV_NAME)) { - // The Apple platform is needed to select the appropriate SDK. - if (!env.containsKey(AppleConfiguration.APPLE_SDK_PLATFORM_ENV_NAME)) { - throw new UserExecException("Could not resolve apple platform for determining SDK"); - } - String iosSdkVersion = env.get(AppleConfiguration.APPLE_SDK_VERSION_ENV_NAME); - String appleSdkPlatform = env.get(AppleConfiguration.APPLE_SDK_PLATFORM_ENV_NAME); - newEnvBuilder.put( - "SDKROOT", - getSdkRootEnv(execRoot, productName, developerDir, iosSdkVersion, appleSdkPlatform)); - } - return newEnvBuilder.build(); - } - - private static String getDeveloperDir(Path execRoot, String productName, String xcodeVersion) - throws UserExecException { - if (OS.getCurrent() != OS.DARWIN) { - throw new UserExecException( - "Cannot locate xcode developer directory on non-darwin operating system"); - } - return AppleHostInfo.getDeveloperDir(execRoot, DottedVersion.fromString(xcodeVersion), - productName); - } - - private static String getSdkRootEnv( - Path execRoot, - String productName, - String developerDir, - String iosSdkVersion, - String appleSdkPlatform) - throws UserExecException { - if (OS.getCurrent() != OS.DARWIN) { - throw new UserExecException("Cannot locate iOS SDK on non-darwin operating system"); - } - return AppleHostInfo.getSdkRoot(execRoot, developerDir, iosSdkVersion, appleSdkPlatform, - productName); - } - @Override public boolean shouldPropagateExecException() { return false; |