Search in sources :

Example 11 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class JUnitStep method getTimeoutHandler.

@Override
protected Optional<Consumer<Process>> getTimeoutHandler(final ExecutionContext context) {
    return Optional.of(process -> {
        Optional<Long> pid = Optional.empty();
        Platform platform = context.getPlatform();
        try {
            switch(platform) {
                case LINUX:
                case FREEBSD:
                case MACOS:
                    {
                        Field field = process.getClass().getDeclaredField("pid");
                        field.setAccessible(true);
                        try {
                            pid = Optional.of((long) field.getInt(process));
                        } catch (IllegalAccessException e) {
                            LOG.error(e, "Failed to access `pid`.");
                        }
                        break;
                    }
                case WINDOWS:
                    {
                        Field field = process.getClass().getDeclaredField("handle");
                        field.setAccessible(true);
                        try {
                            pid = Optional.of(field.getLong(process));
                        } catch (IllegalAccessException e) {
                            LOG.error(e, "Failed to access `handle`.");
                        }
                        break;
                    }
                case UNKNOWN:
                    LOG.info("Unknown platform; unable to obtain the process id!");
                    break;
            }
        } catch (NoSuchFieldException e) {
            LOG.error(e);
        }
        Optional<Path> jstack = new ExecutableFinder(context.getPlatform()).getOptionalExecutable(Paths.get("jstack"), context.getEnvironment());
        if (!pid.isPresent() || !jstack.isPresent()) {
            LOG.info("Unable to print a stack trace for timed out test!");
            return;
        }
        context.getStdErr().println("Test has timed out!  Here is a trace of what it is currently doing:");
        try {
            context.getProcessExecutor().launchAndExecute(ProcessExecutorParams.builder().addCommand(jstack.get().toString(), "-l", pid.get().toString()).setEnvironment(context.getEnvironment()).build(), ImmutableSet.<ProcessExecutor.Option>builder().add(ProcessExecutor.Option.PRINT_STD_OUT).add(ProcessExecutor.Option.PRINT_STD_ERR).build(), Optional.empty(), Optional.of(TimeUnit.SECONDS.toMillis(30)), Optional.of(input -> {
                context.getStdErr().print("Printing the stack took longer than 30 seconds. No longer trying.");
            }));
        } catch (Exception e) {
            LOG.error(e);
        }
    });
}
Also used : Path(java.nio.file.Path) Logger(com.facebook.buck.log.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Platform(com.facebook.buck.util.environment.Platform) ExecutableFinder(com.facebook.buck.io.ExecutableFinder) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) Field(java.lang.reflect.Field) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ExecutionContext(com.facebook.buck.step.ExecutionContext) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ImmutableList(com.google.common.collect.ImmutableList) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) Paths(java.nio.file.Paths) Map(java.util.Map) Optional(java.util.Optional) ShellStep(com.facebook.buck.shell.ShellStep) Path(java.nio.file.Path) Field(java.lang.reflect.Field) ExecutableFinder(com.facebook.buck.io.ExecutableFinder) Platform(com.facebook.buck.util.environment.Platform)

Example 12 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class BaseCompileToJarStepFactory method addPostprocessClassesCommands.

/**
   * Adds a BashStep for each postprocessClasses command that runs the command followed by the
   * outputDirectory of javac outputs.
   *
   * The expectation is that the command will inspect and update the directory by
   * modifying, adding, and deleting the .class files in the directory.
   *
   * The outputDirectory should be a valid java root.  I.e., if outputDirectory
   * is buck-out/bin/java/abc/lib__abc__classes/, then a contained class abc.AbcModule
   * should be at buck-out/bin/java/abc/lib__abc__classes/abc/AbcModule.class
   *
   * @param filesystem the project filesystem.
   * @param postprocessClassesCommands the list of commands to post-process .class files.
   * @param outputDirectory the directory that will contain all the javac output.
   * @param declaredClasspathEntries the list of classpath entries.
   * @param bootClasspath the compilation boot classpath.
   */
@VisibleForTesting
static ImmutableList<Step> addPostprocessClassesCommands(ProjectFilesystem filesystem, List<String> postprocessClassesCommands, Path outputDirectory, ImmutableSortedSet<Path> declaredClasspathEntries, Optional<String> bootClasspath) {
    if (postprocessClassesCommands.isEmpty()) {
        return ImmutableList.of();
    }
    ImmutableList.Builder<Step> commands = new ImmutableList.Builder<Step>();
    ImmutableMap.Builder<String, String> envVarBuilder = ImmutableMap.builder();
    envVarBuilder.put("COMPILATION_CLASSPATH", Joiner.on(':').join(Iterables.transform(declaredClasspathEntries, filesystem::resolve)));
    if (bootClasspath.isPresent()) {
        envVarBuilder.put("COMPILATION_BOOTCLASSPATH", bootClasspath.get());
    }
    ImmutableMap<String, String> envVars = envVarBuilder.build();
    for (final String postprocessClassesCommand : postprocessClassesCommands) {
        BashStep bashStep = new BashStep(filesystem.getRootPath(), postprocessClassesCommand + " " + outputDirectory) {

            @Override
            public ImmutableMap<String, String> getEnvironmentVariables(ExecutionContext context) {
                return envVars;
            }
        };
        commands.add(bashStep);
    }
    return commands.build();
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableList(com.google.common.collect.ImmutableList) BashStep(com.facebook.buck.shell.BashStep) Step(com.facebook.buck.step.Step) BashStep(com.facebook.buck.shell.BashStep) ImmutableMap(com.google.common.collect.ImmutableMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 13 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class LuaStandaloneBinary method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    ImmutableList.Builder<Step> steps = ImmutableList.builder();
    buildableContext.recordArtifact(output);
    // Make sure the parent directory exists.
    steps.add(new MkdirStep(getProjectFilesystem(), output.getParent()));
    // Delete any other pex that was there (when switching between pex styles).
    steps.add(new RmStep(getProjectFilesystem(), output, RmStep.Mode.RECURSIVE));
    SourcePathResolver resolver = context.getSourcePathResolver();
    steps.add(new ShellStep(getProjectFilesystem().getRootPath()) {

        @Override
        protected Optional<String> getStdin(ExecutionContext context) {
            try {
                return Optional.of(context.getObjectMapper().writeValueAsString(ImmutableMap.of("modules", Maps.transformValues(components.getModules(), Functions.compose(Object::toString, resolver::getAbsolutePath)), "pythonModules", Maps.transformValues(components.getPythonModules(), Functions.compose(Object::toString, resolver::getAbsolutePath)), "nativeLibraries", Maps.transformValues(components.getNativeLibraries(), Functions.compose(Object::toString, resolver::getAbsolutePath)))));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        protected ImmutableList<String> getShellCommandInternal(ExecutionContext context) {
            ImmutableList.Builder<String> command = ImmutableList.builder();
            command.addAll(builder.getCommandPrefix(resolver));
            command.addAll(builderArgs);
            command.add("--entry-point", mainModule);
            command.add("--interpreter");
            if (starter.isPresent()) {
                command.add(resolver.getAbsolutePath(starter.get()).toString());
            } else {
                command.add(lua.getCommandPrefix(resolver).get(0));
            }
            command.add(getProjectFilesystem().resolve(output).toString());
            return command.build();
        }

        @Override
        public String getShortName() {
            return "lua_package";
        }
    });
    return steps.build();
}
Also used : Optional(java.util.Optional) ImmutableList(com.google.common.collect.ImmutableList) MkdirStep(com.facebook.buck.step.fs.MkdirStep) RmStep(com.facebook.buck.step.fs.RmStep) Step(com.facebook.buck.step.Step) MkdirStep(com.facebook.buck.step.fs.MkdirStep) ShellStep(com.facebook.buck.shell.ShellStep) IOException(java.io.IOException) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) RmStep(com.facebook.buck.step.fs.RmStep) ExecutionContext(com.facebook.buck.step.ExecutionContext) ShellStep(com.facebook.buck.shell.ShellStep)

Example 14 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class RunShTestAndRecordResultStep method execute.

@Override
public StepExecutionResult execute(ExecutionContext context) throws IOException, InterruptedException {
    TestResultSummary summary;
    if (context.getPlatform() == Platform.WINDOWS) {
        // Ignore sh_test on Windows.
        summary = new TestResultSummary(getShortName(), "sh_test", /* type */
        ResultType.SUCCESS, /* duration*/
        0, /* message */
        "sh_test ignored on Windows", /* stacktrace */
        null, /* stdout */
        null, /* stderr */
        null);
    } else {
        ShellStep test = new ShellStep(filesystem.getRootPath()) {

            boolean timedOut = false;

            @Override
            public String getShortName() {
                return pathToShellScript.toString();
            }

            @Override
            protected ImmutableList<String> getShellCommandInternal(ExecutionContext context) {
                return ImmutableList.<String>builder().add(pathToShellScript.toString()).addAll(args).build();
            }

            @Override
            public ImmutableMap<String, String> getEnvironmentVariables(ExecutionContext context) {
                return ImmutableMap.<String, String>builder().put("NO_BUCKD", "1").putAll(env).build();
            }

            @Override
            protected boolean shouldPrintStderr(Verbosity verbosity) {
                // Do not stream this output because we want to capture it.
                return false;
            }

            @Override
            public StepExecutionResult execute(ExecutionContext context) throws IOException, InterruptedException {
                StepExecutionResult executionResult = super.execute(context);
                if (timedOut) {
                    throw new HumanReadableException("Timed out running test: " + testCaseName + ", with exitCode: " + executionResult.getExitCode());
                }
                return executionResult;
            }

            @Override
            protected Optional<Consumer<Process>> getTimeoutHandler(final ExecutionContext context) {
                return Optional.of(process -> timedOut = true);
            }

            @Override
            protected Optional<Long> getTimeout() {
                return testRuleTimeoutMs;
            }

            @Override
            protected boolean shouldPrintStdout(Verbosity verbosity) {
                // Do not stream this output because we want to capture it.
                return false;
            }
        };
        StepExecutionResult executionResult = test.execute(context);
        // Write test result.
        boolean isSuccess = executionResult.isSuccess();
        summary = new TestResultSummary(getShortName(), "sh_test", /* type */
        isSuccess ? ResultType.SUCCESS : ResultType.FAILURE, test.getDuration(), /* message */
        null, /* stacktrace */
        null, test.getStdout(), test.getStderr());
    }
    ObjectMapper mapper = context.getObjectMapper();
    try (OutputStream outputStream = filesystem.newFileOutputStream(pathToTestResultFile)) {
        mapper.writeValue(outputStream, summary);
    }
    // should be zero.
    return StepExecutionResult.SUCCESS;
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) Consumer(java.util.function.Consumer) HumanReadableException(com.facebook.buck.util.HumanReadableException) OutputStream(java.io.OutputStream) TestResultSummary(com.facebook.buck.test.TestResultSummary) Verbosity(com.facebook.buck.util.Verbosity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 15 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class RustCompileRule method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext buildContext, BuildableContext buildableContext) {
    Path output = getOutput();
    buildableContext.recordArtifact(output);
    SourcePathResolver resolver = buildContext.getSourcePathResolver();
    return ImmutableList.of(new MakeCleanDirectoryStep(getProjectFilesystem(), scratchDir), new SymlinkFilesIntoDirectoryStep(getProjectFilesystem(), getProjectFilesystem().getRootPath(), srcs.stream().map(resolver::getRelativePath).collect(MoreCollectors.toImmutableList()), scratchDir), new MakeCleanDirectoryStep(getProjectFilesystem(), getOutputDir(getBuildTarget(), getProjectFilesystem())), new ShellStep(getProjectFilesystem().getRootPath()) {

        @Override
        protected ImmutableList<String> getShellCommandInternal(ExecutionContext executionContext) {
            ImmutableList<String> linkerCmd = linker.getCommandPrefix(resolver);
            ImmutableList.Builder<String> cmd = ImmutableList.builder();
            Path src = scratchDir.resolve(resolver.getRelativePath(rootModule));
            cmd.addAll(compiler.getCommandPrefix(resolver)).addAll(executionContext.getAnsi().isAnsiTerminal() ? ImmutableList.of("--color=always") : ImmutableList.of()).add(String.format("-Clinker=%s", linkerCmd.get(0))).addAll(processLinkerArgs(linkerCmd.subList(1, linkerCmd.size()))).addAll(processLinkerArgs(Arg.stringify(linkerArgs, buildContext.getSourcePathResolver()))).addAll(Arg.stringify(args, buildContext.getSourcePathResolver())).add("-o", output.toString()).add(src.toString());
            return cmd.build();
        }

        /*
           * Make sure all stderr output from rustc is emitted, since its either a warning or an
           * error. In general Rust code should have zero warnings, or all warnings as errors.
           * Regardless, respect requests for silence.
           */
        @Override
        protected boolean shouldPrintStderr(Verbosity verbosity) {
            return !verbosity.isSilent();
        }

        @Override
        public ImmutableMap<String, String> getEnvironmentVariables(ExecutionContext context) {
            return compiler.getEnvironment(buildContext.getSourcePathResolver());
        }

        @Override
        public String getShortName() {
            return "rust-build";
        }
    });
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableList(com.google.common.collect.ImmutableList) ShellStep(com.facebook.buck.shell.ShellStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) Verbosity(com.facebook.buck.util.Verbosity) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SymlinkFilesIntoDirectoryStep(com.facebook.buck.shell.SymlinkFilesIntoDirectoryStep) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ExecutionContext (com.facebook.buck.step.ExecutionContext)176 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)140 Test (org.junit.Test)128 Path (java.nio.file.Path)79 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)67 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)66 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)55 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)50 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)45 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)44 Step (com.facebook.buck.step.Step)34 ImmutableList (com.google.common.collect.ImmutableList)32 BuildTarget (com.facebook.buck.model.BuildTarget)31 SourcePath (com.facebook.buck.rules.SourcePath)26 TestConsole (com.facebook.buck.testutil.TestConsole)25 FakeProcess (com.facebook.buck.util.FakeProcess)21 ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)20 ImmutableMap (com.google.common.collect.ImmutableMap)20 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)19 FakeProcessExecutor (com.facebook.buck.util.FakeProcessExecutor)19