Search in sources :

Example 1 with CompositeRunfilesSupplier

use of com.google.devtools.build.lib.actions.CompositeRunfilesSupplier in project bazel by bazelbuild.

the class SkylarkRuleImplementationFunctionsTest method testResolveCommandInputs.

@Test
public void testResolveCommandInputs() throws Exception {
    evalRuleContextCode(createRuleContext("//foo:resolve_me"), "inputs, argv, input_manifests = ruleContext.resolve_command(", "   tools=ruleContext.attr.tools)");
    @SuppressWarnings("unchecked") List<Artifact> inputs = (List<Artifact>) (List<?>) (MutableList) lookup("inputs");
    assertArtifactFilenames(inputs, "mytool.sh", "mytool", "foo_Smytool-runfiles", "t.exe");
    @SuppressWarnings("unchecked") CompositeRunfilesSupplier runfilesSupplier = new CompositeRunfilesSupplier((List<RunfilesSupplier>) lookup("input_manifests"));
    assertThat(runfilesSupplier.getMappings()).hasSize(1);
}
Also used : CompositeRunfilesSupplier(com.google.devtools.build.lib.actions.CompositeRunfilesSupplier) MutableList(com.google.devtools.build.lib.syntax.SkylarkList.MutableList) MutableList(com.google.devtools.build.lib.syntax.SkylarkList.MutableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Artifact(com.google.devtools.build.lib.actions.Artifact) RunfilesSupplier(com.google.devtools.build.lib.actions.RunfilesSupplier) CompositeRunfilesSupplier(com.google.devtools.build.lib.actions.CompositeRunfilesSupplier) Test(org.junit.Test)

Example 2 with CompositeRunfilesSupplier

use of com.google.devtools.build.lib.actions.CompositeRunfilesSupplier in project bazel by bazelbuild.

the class ExtraActionFactory method create.

@Override
public ConfiguredTarget create(RuleContext context) throws RuleErrorException {
    // This rule doesn't produce any output when listed as a build target.
    // Only when used via the --experimental_action_listener flag,
    // this rule instructs the build system to add additional outputs.
    List<Artifact> resolvedData = Lists.newArrayList();
    Iterable<? extends TransitiveInfoCollection> tools = context.getPrerequisites("tools", Mode.HOST);
    CommandHelper commandHelper = new CommandHelper(context, tools, ImmutableMap.<Label, Iterable<Artifact>>of());
    resolvedData.addAll(context.getPrerequisiteArtifacts("data", Mode.DATA).list());
    List<String> outputTemplates = context.attributes().get("out_templates", Type.STRING_LIST);
    String command = commandHelper.resolveCommandAndExpandLabels(false, true);
    // This is a bit of a hack. We want to run the MakeVariableExpander first, so we expand $ on
    // variables that are expanded below with $$, which gets reverted to $ by the
    // MakeVariableExpander. This allows us to expand package-specific make variables in the
    // package where the extra action is defined, and then later replace the owner-specific make
    // variables when the extra action is instantiated.
    command = command.replace("$(EXTRA_ACTION_FILE)", "$$(EXTRA_ACTION_FILE)");
    command = command.replace("$(ACTION_ID)", "$$(ACTION_ID)");
    command = command.replace("$(OWNER_LABEL_DIGEST)", "$$(OWNER_LABEL_DIGEST)");
    command = command.replace("$(output ", "$$(output ");
    try {
        command = MakeVariableExpander.expand(command, new ConfigurationMakeVariableContext(context.getTarget().getPackage(), context.getConfiguration()));
    } catch (MakeVariableExpander.ExpansionException e) {
        context.ruleError(String.format("Unable to expand make variables: %s", e.getMessage()));
    }
    boolean requiresActionOutput = context.attributes().get("requires_action_output", Type.BOOLEAN);
    ExtraActionSpec spec = new ExtraActionSpec(commandHelper.getResolvedTools(), new CompositeRunfilesSupplier(commandHelper.getToolsRunfilesSuppliers()), resolvedData, outputTemplates, command, context.getLabel(), TargetUtils.getExecutionInfo(context.getRule()), requiresActionOutput);
    return new RuleConfiguredTargetBuilder(context).addProvider(ExtraActionSpec.class, spec).add(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)).build();
}
Also used : CompositeRunfilesSupplier(com.google.devtools.build.lib.actions.CompositeRunfilesSupplier) MakeVariableExpander(com.google.devtools.build.lib.analysis.MakeVariableExpander) CommandHelper(com.google.devtools.build.lib.analysis.CommandHelper) RunfilesProvider(com.google.devtools.build.lib.analysis.RunfilesProvider) Artifact(com.google.devtools.build.lib.actions.Artifact) ConfigurationMakeVariableContext(com.google.devtools.build.lib.analysis.ConfigurationMakeVariableContext) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)

Example 3 with CompositeRunfilesSupplier

use of com.google.devtools.build.lib.actions.CompositeRunfilesSupplier in project bazel by bazelbuild.

the class GenRuleBase method create.

@Override
public ConfiguredTarget create(RuleContext ruleContext) throws RuleErrorException, InterruptedException {
    NestedSet<Artifact> filesToBuild = NestedSetBuilder.wrap(Order.STABLE_ORDER, ruleContext.getOutputArtifacts());
    NestedSetBuilder<Artifact> resolvedSrcsBuilder = NestedSetBuilder.stableOrder();
    if (filesToBuild.isEmpty()) {
        ruleContext.attributeError("outs", "Genrules without outputs don't make sense");
    }
    if (ruleContext.attributes().get("executable", Type.BOOLEAN) && Iterables.size(filesToBuild) > 1) {
        ruleContext.attributeError("executable", "if genrules produce executables, they are allowed only one output. " + "If you need the executable=1 argument, then you should split this genrule into " + "genrules producing single outputs");
    }
    ImmutableMap.Builder<Label, NestedSet<Artifact>> labelMap = ImmutableMap.builder();
    for (TransitiveInfoCollection dep : ruleContext.getPrerequisites("srcs", Mode.TARGET)) {
        // This target provides specific types of files for genrules.
        GenRuleSourcesProvider provider = dep.getProvider(GenRuleSourcesProvider.class);
        NestedSet<Artifact> files = (provider != null) ? provider.getGenruleFiles() : dep.getProvider(FileProvider.class).getFilesToBuild();
        resolvedSrcsBuilder.addTransitive(files);
        labelMap.put(AliasProvider.getDependencyLabel(dep), files);
    }
    NestedSet<Artifact> resolvedSrcs = resolvedSrcsBuilder.build();
    CommandHelper commandHelper = new CommandHelper(ruleContext, ruleContext.getPrerequisites("tools", Mode.HOST), labelMap.build());
    if (ruleContext.hasErrors()) {
        return null;
    }
    String baseCommand = commandHelper.resolveCommandAndExpandLabels(ruleContext.attributes().get("heuristic_label_expansion", Type.BOOLEAN), false);
    // Adds the genrule environment setup script before the actual shell command
    String command = String.format("source %s; %s", ruleContext.getPrerequisiteArtifact("$genrule_setup", Mode.HOST).getExecPath(), baseCommand);
    command = resolveCommand(command, ruleContext, resolvedSrcs, filesToBuild);
    String message = ruleContext.attributes().get("message", Type.STRING);
    if (message.isEmpty()) {
        message = "Executing genrule";
    }
    ImmutableMap<String, String> env = ruleContext.getConfiguration().getLocalShellEnvironment();
    ImmutableSet<String> clientEnvVars = ruleContext.getConfiguration().getVariableShellEnvironment();
    Map<String, String> executionInfo = Maps.newLinkedHashMap();
    executionInfo.putAll(TargetUtils.getExecutionInfo(ruleContext.getRule()));
    if (ruleContext.attributes().get("local", Type.BOOLEAN)) {
        executionInfo.put("local", "");
    }
    executionInfo.putAll(getExtraExecutionInfo(ruleContext, baseCommand));
    NestedSetBuilder<Artifact> inputs = NestedSetBuilder.stableOrder();
    inputs.addAll(resolvedSrcs);
    inputs.addAll(commandHelper.getResolvedTools());
    FilesToRunProvider genruleSetup = ruleContext.getPrerequisite("$genrule_setup", Mode.HOST, FilesToRunProvider.class);
    inputs.addAll(genruleSetup.getFilesToRun());
    List<String> argv = commandHelper.buildCommandLine(command, inputs, ".genrule_script.sh", ImmutableMap.copyOf(executionInfo));
    // TODO(bazel-team): Make the make variable expander pass back a list of these.
    if (requiresCrosstool(baseCommand)) {
        // If cc is used, silently throw in the crosstool filegroup as a dependency.
        inputs.addTransitive(CppHelper.getToolchain(ruleContext, ":cc_toolchain").getCrosstoolMiddleman());
    }
    if (requiresJdk(baseCommand)) {
        // If javac is used, silently throw in the jdk filegroup as a dependency.
        // Note we expand Java-related variables with the *host* configuration.
        inputs.addTransitive(JavaHelper.getHostJavabaseInputs(ruleContext));
    }
    for (NestedSet<Artifact> extraInputs : getExtraInputArtifacts(ruleContext, baseCommand)) {
        inputs.addTransitive(extraInputs);
    }
    if (isStampingEnabled(ruleContext)) {
        inputs.add(ruleContext.getAnalysisEnvironment().getStableWorkspaceStatusArtifact());
        inputs.add(ruleContext.getAnalysisEnvironment().getVolatileWorkspaceStatusArtifact());
    }
    ruleContext.registerAction(new GenRuleAction(ruleContext.getActionOwner(), ImmutableList.copyOf(commandHelper.getResolvedTools()), inputs.build(), filesToBuild, argv, env, clientEnvVars, ImmutableMap.copyOf(executionInfo), new CompositeRunfilesSupplier(commandHelper.getToolsRunfilesSuppliers()), message + ' ' + ruleContext.getLabel()));
    RunfilesProvider runfilesProvider = RunfilesProvider.withData(// No runfiles provided if not a data dependency.
    Runfiles.EMPTY, // configuration.
    new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addTransitiveArtifacts(filesToBuild).build());
    RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext).setFilesToBuild(filesToBuild).setRunfilesSupport(null, getExecutable(ruleContext, filesToBuild)).addProvider(RunfilesProvider.class, runfilesProvider);
    builder = updateBuilder(builder, ruleContext, filesToBuild);
    return builder.build();
}
Also used : CompositeRunfilesSupplier(com.google.devtools.build.lib.actions.CompositeRunfilesSupplier) FilesToRunProvider(com.google.devtools.build.lib.analysis.FilesToRunProvider) NestedSet(com.google.devtools.build.lib.collect.nestedset.NestedSet) Label(com.google.devtools.build.lib.cmdline.Label) CommandHelper(com.google.devtools.build.lib.analysis.CommandHelper) RunfilesProvider(com.google.devtools.build.lib.analysis.RunfilesProvider) Artifact(com.google.devtools.build.lib.actions.Artifact) ImmutableMap(com.google.common.collect.ImmutableMap) Runfiles(com.google.devtools.build.lib.analysis.Runfiles) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection)

Aggregations

Artifact (com.google.devtools.build.lib.actions.Artifact)3 CompositeRunfilesSupplier (com.google.devtools.build.lib.actions.CompositeRunfilesSupplier)3 CommandHelper (com.google.devtools.build.lib.analysis.CommandHelper)2 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)2 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 RunfilesSupplier (com.google.devtools.build.lib.actions.RunfilesSupplier)1 ConfigurationMakeVariableContext (com.google.devtools.build.lib.analysis.ConfigurationMakeVariableContext)1 FilesToRunProvider (com.google.devtools.build.lib.analysis.FilesToRunProvider)1 MakeVariableExpander (com.google.devtools.build.lib.analysis.MakeVariableExpander)1 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)1 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 Label (com.google.devtools.build.lib.cmdline.Label)1 NestedSet (com.google.devtools.build.lib.collect.nestedset.NestedSet)1 MutableList (com.google.devtools.build.lib.syntax.SkylarkList.MutableList)1 List (java.util.List)1 Test (org.junit.Test)1