Search in sources :

Example 41 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 42 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 43 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 44 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)

Example 45 with ExecutionContext

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

the class WriteStringTemplateRule method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    buildableContext.recordArtifact(output);
    ImmutableList.Builder<Step> steps = ImmutableList.builder();
    steps.add(new MkdirStep(getProjectFilesystem(), output.getParent()));
    steps.add(new StringTemplateStep(context.getSourcePathResolver().getAbsolutePath(template), getProjectFilesystem(), output, st -> {
        for (Map.Entry<String, String> ent : values.entrySet()) {
            st = st.add(ent.getKey(), ent.getValue());
        }
        return st;
    }));
    if (executable) {
        steps.add(new AbstractExecutionStep("chmod +x") {

            @Override
            public StepExecutionResult execute(ExecutionContext context) throws IOException {
                MoreFiles.makeExecutable(getProjectFilesystem().resolve(output));
                return StepExecutionResult.of(0, Optional.empty());
            }
        });
    }
    return steps.build();
}
Also used : ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Step(com.facebook.buck.step.Step) ImmutableMap(com.google.common.collect.ImmutableMap) IOException(java.io.IOException) BuildTarget(com.facebook.buck.model.BuildTarget) MkdirStep(com.facebook.buck.step.fs.MkdirStep) StringTemplateStep(com.facebook.buck.step.fs.StringTemplateStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Suppliers(com.google.common.base.Suppliers) Optional(java.util.Optional) MoreFiles(com.facebook.buck.io.MoreFiles) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) Path(java.nio.file.Path) ExecutionContext(com.facebook.buck.step.ExecutionContext) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) ImmutableList(com.google.common.collect.ImmutableList) MkdirStep(com.facebook.buck.step.fs.MkdirStep) Step(com.facebook.buck.step.Step) MkdirStep(com.facebook.buck.step.fs.MkdirStep) StringTemplateStep(com.facebook.buck.step.fs.StringTemplateStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) IOException(java.io.IOException) StringTemplateStep(com.facebook.buck.step.fs.StringTemplateStep)

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