Search in sources :

Example 1 with BinaryBuildRule

use of com.facebook.buck.rules.BinaryBuildRule in project buck by facebook.

the class GoDescriptors method getTestMainGenerator.

static Tool getTestMainGenerator(GoBuckConfig goBuckConfig, BuildRuleParams sourceParams, BuildRuleResolver resolver) throws NoSuchBuildTargetException {
    Optional<Tool> configTool = goBuckConfig.getGoTestMainGenerator(resolver);
    if (configTool.isPresent()) {
        return configTool.get();
    }
    // TODO(mikekap): Make a single test main gen, rather than one per test. The generator itself
    // doesn't vary per test.
    BuildTarget generatorTarget = sourceParams.getBuildTarget().withFlavors(InternalFlavor.of("make-test-main-gen"));
    Optional<BuildRule> generator = resolver.getRuleOptional(generatorTarget);
    if (generator.isPresent()) {
        return ((BinaryBuildRule) generator.get()).getExecutableCommand();
    }
    BuildTarget generatorSourceTarget = sourceParams.getBuildTarget().withAppendedFlavors(InternalFlavor.of("test-main-gen-source"));
    WriteFile writeFile = resolver.addToIndex(new WriteFile(sourceParams.withBuildTarget(generatorSourceTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of())), extractTestMainGenerator(), BuildTargets.getGenPath(sourceParams.getProjectFilesystem(), generatorSourceTarget, "%s/main.go"), /* executable */
    false));
    GoBinary binary = resolver.addToIndex(createGoBinaryRule(sourceParams.withBuildTarget(generatorTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of(writeFile))), resolver, goBuckConfig, ImmutableSet.of(writeFile.getSourcePathToOutput()), ImmutableList.of(), ImmutableList.of(), ImmutableList.of(), goBuckConfig.getDefaultPlatform()));
    return binary.getExecutableCommand();
}
Also used : WriteFile(com.facebook.buck.file.WriteFile) BuildTarget(com.facebook.buck.model.BuildTarget) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) Tool(com.facebook.buck.rules.Tool)

Example 2 with BinaryBuildRule

use of com.facebook.buck.rules.BinaryBuildRule in project buck by facebook.

the class AppleCxxPlatformsTest method buckTargetIsUsedWhenBuildTargetIsSpecified.

@Test
public void buckTargetIsUsedWhenBuildTargetIsSpecified() {
    AppleCxxPlatform appleCxxPlatform = buildAppleCxxPlatform(temp.getRoot(), FakeBuckConfig.builder().setSections("[apple]", "codesign = //foo:bar").build());
    BuildTarget buildTarget = BuildTargetFactory.newInstance("//foo:bar");
    BinaryBuildRule buildRule = EasyMock.createMock(BinaryBuildRule.class);
    Tool codesign = EasyMock.createMock(Tool.class);
    EasyMock.expect(buildRule.getExecutableCommand()).andReturn(codesign);
    BuildRuleResolver buildRuleResolver = EasyMock.createMock(BuildRuleResolver.class);
    EasyMock.expect(buildRuleResolver.getRuleOptional(buildTarget)).andReturn(Optional.of(buildRule));
    EasyMock.replay(buildRule, buildRuleResolver);
    assertThat(appleCxxPlatform.getCodesignProvider().resolve(buildRuleResolver), is(codesign));
}
Also used : BuildTarget(com.facebook.buck.model.BuildTarget) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) VersionedTool(com.facebook.buck.rules.VersionedTool) Tool(com.facebook.buck.rules.Tool) Test(org.junit.Test)

Example 3 with BinaryBuildRule

use of com.facebook.buck.rules.BinaryBuildRule in project buck by facebook.

the class RunCommand method runWithoutHelp.

@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
    if (!hasTargetSpecified()) {
        params.getBuckEventBus().post(ConsoleEvent.severe("No target given to run"));
        params.getBuckEventBus().post(ConsoleEvent.severe("buck run <target> <arg1> <arg2>..."));
        return 1;
    }
    // Make sure the target is built.
    BuildCommand buildCommand = new BuildCommand(ImmutableList.of(getTarget(params.getBuckConfig())));
    int exitCode = buildCommand.runWithoutHelp(params);
    if (exitCode != 0) {
        return exitCode;
    }
    String targetName = getTarget(params.getBuckConfig());
    BuildTarget target = Iterables.getOnlyElement(getBuildTargets(params.getCell().getCellPathResolver(), ImmutableSet.of(targetName)));
    Build build = buildCommand.getBuild();
    BuildRule targetRule;
    try {
        targetRule = build.getRuleResolver().requireRule(target);
    } catch (NoSuchBuildTargetException e) {
        throw new HumanReadableException(e.getHumanReadableErrorMessage());
    }
    BinaryBuildRule binaryBuildRule = null;
    if (targetRule instanceof BinaryBuildRule) {
        binaryBuildRule = (BinaryBuildRule) targetRule;
    }
    if (binaryBuildRule == null) {
        params.getBuckEventBus().post(ConsoleEvent.severe("target " + targetName + " is not a binary rule (only binary rules can be `run`)"));
        return 1;
    }
    // Ideally, we would take fullCommand, disconnect from NailGun, and run the command in the
    // user's shell. Currently, if you use `buck run` with buckd and ctrl-C to kill the command
    // being run, occasionally I get the following error when I try to run `buck run` again:
    //
    //   Daemon is busy, please wait or run "buck kill" to terminate it.
    //
    // Clearly something bad has happened here. If you are using `buck run` to start up a server
    // or some other process that is meant to "run forever," then it's pretty common to do:
    // `buck run`, test server, hit ctrl-C, edit server code, repeat. This should not wedge buckd.
    SourcePathResolver resolver = new SourcePathResolver(new SourcePathRuleFinder(build.getRuleResolver()));
    Tool executable = binaryBuildRule.getExecutableCommand();
    ListeningProcessExecutor processExecutor = new ListeningProcessExecutor();
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addAllCommand(executable.getCommandPrefix(resolver)).addAllCommand(getTargetArguments()).setEnvironment(ImmutableMap.<String, String>builder().putAll(params.getEnvironment()).putAll(executable.getEnvironment(resolver)).build()).setDirectory(params.getCell().getFilesystem().getRootPath()).build();
    ForwardingProcessListener processListener = new ForwardingProcessListener(Channels.newChannel(params.getConsole().getStdOut()), Channels.newChannel(params.getConsole().getStdErr()));
    ListeningProcessExecutor.LaunchedProcess process = processExecutor.launchProcess(processExecutorParams, processListener);
    try {
        return processExecutor.waitForProcess(process);
    } finally {
        processExecutor.destroyProcess(process, /* force */
        false);
        processExecutor.waitForProcess(process);
    }
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildTarget(com.facebook.buck.model.BuildTarget) Build(com.facebook.buck.command.Build) HumanReadableException(com.facebook.buck.util.HumanReadableException) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) ForwardingProcessListener(com.facebook.buck.util.ForwardingProcessListener) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) Tool(com.facebook.buck.rules.Tool)

Example 4 with BinaryBuildRule

use of com.facebook.buck.rules.BinaryBuildRule in project buck by facebook.

the class WorkerToolDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, final BuildRuleParams params, final BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
    BuildRule rule = resolver.requireRule(args.exe);
    if (!(rule instanceof BinaryBuildRule)) {
        throw new HumanReadableException("The 'exe' argument of %s, %s, needs to correspond to a " + "binary rule, such as sh_binary().", params.getBuildTarget(), args.exe.getFullyQualifiedName());
    }
    Function<String, com.facebook.buck.rules.args.Arg> toArg = MacroArg.toMacroArgFunction(MACRO_HANDLER, params.getBuildTarget(), params.getCellRoots(), resolver);
    final ImmutableList<com.facebook.buck.rules.args.Arg> workerToolArgs = args.getStartupArgs().stream().map(toArg::apply).collect(MoreCollectors.toImmutableList());
    ImmutableMap<String, String> expandedEnv = ImmutableMap.copyOf(FluentIterable.from(args.env.entrySet()).transform(input -> {
        try {
            return Maps.immutableEntry(input.getKey(), MACRO_HANDLER.expand(params.getBuildTarget(), params.getCellRoots(), resolver, input.getValue()));
        } catch (MacroException e) {
            throw new HumanReadableException(e, "%s: %s", params.getBuildTarget(), e.getMessage());
        }
    }));
    int maxWorkers;
    if (args.maxWorkers.isPresent()) {
        // negative or zero: unlimited number of worker processes
        maxWorkers = args.maxWorkers.get() < 1 ? Integer.MAX_VALUE : args.maxWorkers.get();
    } else {
        // default is 1 worker process (for backwards compatibility)
        maxWorkers = 1;
    }
    return new DefaultWorkerTool(params, (BinaryBuildRule) rule, workerToolArgs, expandedEnv, maxWorkers, args.persistent.orElse(buckConfig.getBooleanValue(CONFIG_SECTION, CONFIG_PERSISTENT_KEY, false)));
}
Also used : BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) CellPathResolver(com.facebook.buck.rules.CellPathResolver) Either(com.facebook.buck.model.Either) ClasspathMacroExpander(com.facebook.buck.rules.macros.ClasspathMacroExpander) BuildRule(com.facebook.buck.rules.BuildRule) BuckConfig(com.facebook.buck.cli.BuckConfig) ImmutableList(com.google.common.collect.ImmutableList) FluentIterable(com.google.common.collect.FluentIterable) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) ImplicitDepsInferringDescription(com.facebook.buck.rules.ImplicitDepsInferringDescription) Map(java.util.Map) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) MacroArg(com.facebook.buck.rules.args.MacroArg) Function(com.google.common.base.Function) ImmutableMap(com.google.common.collect.ImmutableMap) TargetGraph(com.facebook.buck.rules.TargetGraph) MacroException(com.facebook.buck.model.MacroException) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildTarget(com.facebook.buck.model.BuildTarget) SuppressFieldNotInitialized(com.facebook.infer.annotation.SuppressFieldNotInitialized) Maps(com.google.common.collect.Maps) ExecutableMacroExpander(com.facebook.buck.rules.macros.ExecutableMacroExpander) MacroExpander(com.facebook.buck.rules.macros.MacroExpander) AbstractDescriptionArg(com.facebook.buck.rules.AbstractDescriptionArg) Optional(java.util.Optional) LocationMacroExpander(com.facebook.buck.rules.macros.LocationMacroExpander) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) MacroHandler(com.facebook.buck.rules.macros.MacroHandler) Description(com.facebook.buck.rules.Description) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) HumanReadableException(com.facebook.buck.util.HumanReadableException) MacroArg(com.facebook.buck.rules.args.MacroArg) AbstractDescriptionArg(com.facebook.buck.rules.AbstractDescriptionArg) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) MacroException(com.facebook.buck.model.MacroException)

Aggregations

BuildTarget (com.facebook.buck.model.BuildTarget)4 BinaryBuildRule (com.facebook.buck.rules.BinaryBuildRule)4 BuildRule (com.facebook.buck.rules.BuildRule)3 Tool (com.facebook.buck.rules.Tool)3 NoSuchBuildTargetException (com.facebook.buck.parser.NoSuchBuildTargetException)2 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)2 HumanReadableException (com.facebook.buck.util.HumanReadableException)2 BuckConfig (com.facebook.buck.cli.BuckConfig)1 Build (com.facebook.buck.command.Build)1 WriteFile (com.facebook.buck.file.WriteFile)1 Either (com.facebook.buck.model.Either)1 MacroException (com.facebook.buck.model.MacroException)1 AbstractDescriptionArg (com.facebook.buck.rules.AbstractDescriptionArg)1 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)1 CellPathResolver (com.facebook.buck.rules.CellPathResolver)1 Description (com.facebook.buck.rules.Description)1 ImplicitDepsInferringDescription (com.facebook.buck.rules.ImplicitDepsInferringDescription)1 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)1 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)1 TargetGraph (com.facebook.buck.rules.TargetGraph)1