Search in sources :

Example 1 with TerminationStatus

use of com.google.devtools.build.lib.shell.TerminationStatus in project bazel by bazelbuild.

the class AppleHostInfo method getSdkRoot.

/**
   * Returns the absolute root path of the target Apple SDK on the host system for a given
   * version of xcode (as defined by the given {@code developerDir}). This may spawn a
   * process and use the {@code /usr/bin/xcrun} binary to locate the target SDK. This uses a local
   * cache file under {@code bazel-out}, and will only spawn a new {@code xcrun} process in the case
   * of a cache miss.
   *
   * @param execRoot the execution root path, used to locate the cache file
   * @param developerDir the value of {@code DEVELOPER_DIR} for the target version of xcode
   * @param sdkVersion the sdk version, for example, "9.1"
   * @param appleSdkPlatform the sdk platform, for example, "iPhoneOS"
   * @param productName the product name
   * @throws UserExecException if there is an issue with obtaining the root from the spawned
   *     process, either because the SDK platform/version pair doesn't exist, or there was an
   *     unexpected issue finding or running the tool
   */
public static String getSdkRoot(Path execRoot, String developerDir, String sdkVersion, String appleSdkPlatform, String productName) throws UserExecException {
    try {
        CacheManager cacheManager = new CacheManager(execRoot.getRelative(BlazeDirectories.getRelativeOutputPath(productName)), XCRUN_CACHE_FILENAME);
        String sdkString = appleSdkPlatform.toLowerCase() + sdkVersion;
        String cacheResult = cacheManager.getValue(developerDir, sdkString);
        if (cacheResult != null) {
            return cacheResult;
        } else {
            Map<String, String> env = Strings.isNullOrEmpty(developerDir) ? ImmutableMap.<String, String>of() : ImmutableMap.of("DEVELOPER_DIR", developerDir);
            CommandResult xcrunResult = new Command(new String[] { "/usr/bin/xcrun", "--sdk", sdkString, "--show-sdk-path" }, env, null).execute();
            // calling xcrun via Command returns a value with a newline on the end.
            String sdkRoot = new String(xcrunResult.getStdout(), StandardCharsets.UTF_8).trim();
            cacheManager.writeEntry(ImmutableList.of(developerDir, sdkString), sdkRoot);
            return sdkRoot;
        }
    } catch (AbnormalTerminationException e) {
        TerminationStatus terminationStatus = e.getResult().getTerminationStatus();
        if (terminationStatus.exited()) {
            throw new UserExecException(String.format("xcrun failed with code %s.\n" + "This most likely indicates that SDK version [%s] for platform [%s] is " + "unsupported for the target version of xcode.\n" + "%s\n" + "Stderr: %s", terminationStatus.getExitCode(), sdkVersion, appleSdkPlatform, terminationStatus.toString(), new String(e.getResult().getStderr(), StandardCharsets.UTF_8)));
        }
        String message = String.format("xcrun failed.\n%s\n%s", e.getResult().getTerminationStatus(), new String(e.getResult().getStderr(), StandardCharsets.UTF_8));
        throw new UserExecException(message, e);
    } catch (CommandException | IOException e) {
        throw new UserExecException(e);
    }
}
Also used : Command(com.google.devtools.build.lib.shell.Command) TerminationStatus(com.google.devtools.build.lib.shell.TerminationStatus) UserExecException(com.google.devtools.build.lib.actions.UserExecException) AbnormalTerminationException(com.google.devtools.build.lib.shell.AbnormalTerminationException) CommandException(com.google.devtools.build.lib.shell.CommandException) IOException(java.io.IOException) CommandResult(com.google.devtools.build.lib.shell.CommandResult)

Example 2 with TerminationStatus

use of com.google.devtools.build.lib.shell.TerminationStatus in project bazel by bazelbuild.

the class AppleHostInfo method getDeveloperDir.

/**
   * Returns the absolute root path of the xcode developer directory on the host system for
   * the given xcode version. This may spawn a process and use the {@code xcode-locator} binary.
   * This uses a local cache file under {@code bazel-out}, and will only spawn a new process in the
   * case of a cache miss.
   *
   * @param execRoot the execution root path, used to locate the cache file
   * @param version the xcode version number to look up
   * @param productName the product name
   * @throws UserExecException if there is an issue with obtaining the path from the spawned
   *     process, either because there is no installed xcode with the given version, or
   *     there was an unexpected issue finding or running the tool
   */
public static String getDeveloperDir(Path execRoot, DottedVersion version, String productName) throws UserExecException {
    try {
        CacheManager cacheManager = new CacheManager(execRoot.getRelative(BlazeDirectories.getRelativeOutputPath(productName)), XCODE_LOCATOR_CACHE_FILENAME);
        String cacheResult = cacheManager.getValue(version.toString());
        if (cacheResult != null) {
            return cacheResult;
        } else {
            CommandResult xcodeLocatorResult = new Command(new String[] { execRoot.getRelative("_bin/xcode-locator").getPathString(), version.toString() }).execute();
            String developerDir = new String(xcodeLocatorResult.getStdout(), StandardCharsets.UTF_8).trim();
            cacheManager.writeEntry(ImmutableList.of(version.toString()), developerDir);
            return developerDir;
        }
    } catch (AbnormalTerminationException e) {
        TerminationStatus terminationStatus = e.getResult().getTerminationStatus();
        String message;
        if (e.getResult().getTerminationStatus().exited()) {
            message = String.format("xcode-locator failed with code %s.\n" + "This most likely indicates that xcode version %s is not available on the host " + "machine.\n" + "%s\n" + "stderr: %s", terminationStatus.getExitCode(), version, terminationStatus.toString(), new String(e.getResult().getStderr(), StandardCharsets.UTF_8));
        } else {
            message = String.format("xcode-locator failed. %s\nstderr: %s", e.getResult().getTerminationStatus(), new String(e.getResult().getStderr(), StandardCharsets.UTF_8));
        }
        throw new UserExecException(message, e);
    } catch (CommandException | IOException e) {
        throw new UserExecException(e);
    }
}
Also used : Command(com.google.devtools.build.lib.shell.Command) TerminationStatus(com.google.devtools.build.lib.shell.TerminationStatus) UserExecException(com.google.devtools.build.lib.actions.UserExecException) AbnormalTerminationException(com.google.devtools.build.lib.shell.AbnormalTerminationException) CommandException(com.google.devtools.build.lib.shell.CommandException) IOException(java.io.IOException) CommandResult(com.google.devtools.build.lib.shell.CommandResult)

Example 3 with TerminationStatus

use of com.google.devtools.build.lib.shell.TerminationStatus in project bazel by bazelbuild.

the class SandboxRunner method run.

/**
   * Runs the command specified via {@code arguments} and {@code env} inside the sandbox.
   *
   * @param arguments - arguments of spawn to run inside the sandbox.
   * @param environment - environment variables to pass to the spawn.
   * @param outErr - error output to capture sandbox's and command's stderr.
   * @param timeout - after how many seconds should the process be killed.
   * @param allowNetwork - whether networking should be allowed for the process.
   * @param sandboxDebug - whether debugging message should be printed.
   * @param useFakeHostname - whether the hostname should be set to 'localhost' inside the sandbox.
   */
void run(List<String> arguments, Map<String, String> environment, OutErr outErr, int timeout, boolean allowNetwork, boolean sandboxDebug, boolean useFakeHostname) throws ExecException {
    Command cmd;
    try {
        cmd = getCommand(arguments, environment, timeout, allowNetwork, useFakeHostname);
    } catch (IOException e) {
        throw new UserExecException("I/O error during sandboxed execution", e);
    }
    TerminationStatus status = null;
    try {
        cmd.execute(/* stdin */
        new byte[] {}, getCommandObserver(timeout), outErr.getOutputStream(), outErr.getErrorStream(), /* killSubprocessOnInterrupt */
        true);
    } catch (CommandException e) {
        boolean timedOut = false;
        if (e instanceof AbnormalTerminationException) {
            status = ((AbnormalTerminationException) e).getResult().getTerminationStatus();
            timedOut = !status.exited() && (status.getTerminatingSignal() == getSignalOnTimeout());
        }
        String statusMessage = status + " [sandboxed]";
        if (!verboseFailures) {
            // simplest error message
            throw new UserExecException(statusMessage, e, timedOut);
        }
        List<String> commandList;
        if (!sandboxDebug) {
            commandList = arguments;
        } else {
            commandList = Arrays.asList(cmd.getCommandLineElements());
        }
        String commandFailureMessage = CommandFailureUtils.describeCommandFailure(true, commandList, environment, null) + (sandboxDebug ? "" : SANDBOX_DEBUG_SUGGESTION);
        throw new UserExecException(commandFailureMessage, e, timedOut);
    }
}
Also used : Command(com.google.devtools.build.lib.shell.Command) TerminationStatus(com.google.devtools.build.lib.shell.TerminationStatus) UserExecException(com.google.devtools.build.lib.actions.UserExecException) AbnormalTerminationException(com.google.devtools.build.lib.shell.AbnormalTerminationException) List(java.util.List) IOException(java.io.IOException) CommandException(com.google.devtools.build.lib.shell.CommandException)

Example 4 with TerminationStatus

use of com.google.devtools.build.lib.shell.TerminationStatus in project bazel by bazelbuild.

the class StandaloneSpawnStrategy method actuallyExec.

/**
   * Executes the given {@code spawn}.
   */
private void actuallyExec(Spawn spawn, ActionExecutionContext actionExecutionContext) throws ExecException {
    Executor executor = actionExecutionContext.getExecutor();
    if (executor.reportsSubcommands()) {
        executor.reportSubcommand(spawn);
    }
    int timeoutSeconds = Spawns.getTimeoutSeconds(spawn);
    // We must wrap the subprocess with process-wrapper to kill the process tree.
    // All actions therefore depend on the process-wrapper file. Since it's embedded,
    // we don't bother with declaring it as an input.
    List<String> args = new ArrayList<>();
    if (OS.getCurrent() != OS.WINDOWS) {
        // TODO(bazel-team): process-wrapper seems to work on Windows, but requires
        // additional setup as it is an msys2 binary, so it needs msys2 DLLs on %PATH%.
        // Disable it for now to make the setup easier and to avoid further PATH hacks.
        // Ideally we should have a native implementation of process-wrapper for Windows.
        args.add(processWrapper.getPathString());
        args.add(Integer.toString(timeoutSeconds));
        args.add("5");
        /* kill delay: give some time to print stacktraces and whatnot. */
        // TODO(bazel-team): use process-wrapper redirection so we don't have to
        // pass test logs through the Java heap.
        args.add("-");
        /* stdout. */
        args.add("-");
    /* stderr. */
    }
    args.addAll(spawn.getArguments());
    String cwd = executor.getExecRoot().getPathString();
    Command cmd = new Command(args.toArray(new String[] {}), locallyDeterminedEnv(execRoot, productName, spawn.getEnvironment()), new File(cwd), OS.getCurrent() == OS.WINDOWS && timeoutSeconds >= 0 ? timeoutSeconds * 1000 : -1);
    FileOutErr outErr = actionExecutionContext.getFileOutErr();
    try {
        cmd.execute(/* stdin */
        new byte[] {}, Command.NO_OBSERVER, outErr.getOutputStream(), outErr.getErrorStream(), /*killSubprocessOnInterrupt*/
        true);
    } catch (AbnormalTerminationException e) {
        TerminationStatus status = e.getResult().getTerminationStatus();
        boolean timedOut = !status.exited() && (status.timedout() || status.getTerminatingSignal() == 14);
        String message = CommandFailureUtils.describeCommandFailure(verboseFailures, spawn.getArguments(), spawn.getEnvironment(), cwd);
        throw new UserExecException(String.format("%s: %s", message, e), timedOut);
    } catch (CommandException e) {
        String message = CommandFailureUtils.describeCommandFailure(verboseFailures, spawn.getArguments(), spawn.getEnvironment(), cwd);
        throw new UserExecException(message, e);
    }
}
Also used : Executor(com.google.devtools.build.lib.actions.Executor) FileOutErr(com.google.devtools.build.lib.util.io.FileOutErr) Command(com.google.devtools.build.lib.shell.Command) TerminationStatus(com.google.devtools.build.lib.shell.TerminationStatus) ArrayList(java.util.ArrayList) UserExecException(com.google.devtools.build.lib.actions.UserExecException) AbnormalTerminationException(com.google.devtools.build.lib.shell.AbnormalTerminationException) CommandException(com.google.devtools.build.lib.shell.CommandException) File(java.io.File)

Aggregations

UserExecException (com.google.devtools.build.lib.actions.UserExecException)4 AbnormalTerminationException (com.google.devtools.build.lib.shell.AbnormalTerminationException)4 Command (com.google.devtools.build.lib.shell.Command)4 CommandException (com.google.devtools.build.lib.shell.CommandException)4 TerminationStatus (com.google.devtools.build.lib.shell.TerminationStatus)4 IOException (java.io.IOException)3 CommandResult (com.google.devtools.build.lib.shell.CommandResult)2 Executor (com.google.devtools.build.lib.actions.Executor)1 FileOutErr (com.google.devtools.build.lib.util.io.FileOutErr)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1