Search in sources :

Example 1 with CommandHelper

use of com.google.devtools.build.lib.analysis.CommandHelper 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 2 with CommandHelper

use of com.google.devtools.build.lib.analysis.CommandHelper in project bazel by bazelbuild.

the class ExtraActionSpec method addExtraAction.

/**
   * Adds an extra_action to the action graph based on the action to shadow.
   */
public Collection<Artifact> addExtraAction(RuleContext owningRule, Action actionToShadow) {
    Collection<Artifact> extraActionOutputs = new LinkedHashSet<>();
    Collection<Artifact> protoOutputs = new ArrayList<>();
    NestedSetBuilder<Artifact> extraActionInputs = NestedSetBuilder.stableOrder();
    Label ownerLabel = owningRule.getLabel();
    if (requiresActionOutput || actionToShadow.discoversInputs()) {
        extraActionInputs.addAll(actionToShadow.getOutputs());
    }
    extraActionInputs.addAll(resolvedTools);
    extraActionInputs.addAll(resolvedData);
    boolean createDummyOutput = false;
    for (String outputTemplate : outputTemplates) {
        // We create output for the extra_action based on the 'out_template' attribute.
        // See {link #getExtraActionOutputArtifact} for supported variables.
        extraActionOutputs.add(getExtraActionOutputArtifact(owningRule, actionToShadow, outputTemplate));
    }
    // extra_action has no output, we need to create some dummy output to keep the build up-to-date.
    if (extraActionOutputs.isEmpty()) {
        createDummyOutput = true;
        extraActionOutputs.add(getExtraActionOutputArtifact(owningRule, actionToShadow, "$(ACTION_ID).dummy"));
    }
    // We generate a file containing a protocol buffer describing the action that is being shadowed.
    // It is up to each action being shadowed to decide what contents to store here.
    Artifact extraActionInfoFile = getExtraActionOutputArtifact(owningRule, actionToShadow, "$(ACTION_ID).xa");
    owningRule.registerAction(new ExtraActionInfoFileWriteAction(actionToShadow.getOwner(), extraActionInfoFile, actionToShadow));
    extraActionInputs.add(extraActionInfoFile);
    protoOutputs.add(extraActionInfoFile);
    // Expand extra_action specific variables from the provided command-line.
    // See {@link #createExpandedCommand} for list of supported variables.
    String command = createExpandedCommand(owningRule, actionToShadow, extraActionInfoFile);
    Map<String, String> env = owningRule.getConfiguration().getLocalShellEnvironment();
    Set<String> clientEnvVars = owningRule.getConfiguration().getVariableShellEnvironment();
    CommandHelper commandHelper = new CommandHelper(owningRule, ImmutableList.<TransitiveInfoCollection>of(), ImmutableMap.<Label, Iterable<Artifact>>of());
    // Multiple actions in the same configured target need to have different names for the artifact
    // that might be created here, so we append something that should be unique for each action.
    String actionUniquifier = actionToShadow.getPrimaryOutput().getExecPath().getBaseName() + "." + actionToShadow.getKey();
    List<String> argv = commandHelper.buildCommandLine(command, extraActionInputs, "." + actionUniquifier + ".extra_action_script.sh", executionInfo);
    String commandMessage = String.format("Executing extra_action %s on %s", label, ownerLabel);
    owningRule.registerAction(new ExtraAction(ImmutableSet.copyOf(extraActionInputs.build()), runfilesSupplier, extraActionOutputs, actionToShadow, createDummyOutput, CommandLine.of(argv, false), env, clientEnvVars, executionInfo, commandMessage, label.getName()));
    return ImmutableSet.<Artifact>builder().addAll(extraActionOutputs).addAll(protoOutputs).build();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) Label(com.google.devtools.build.lib.cmdline.Label) CommandHelper(com.google.devtools.build.lib.analysis.CommandHelper) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 3 with CommandHelper

use of com.google.devtools.build.lib.analysis.CommandHelper 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 CommandHelper (com.google.devtools.build.lib.analysis.CommandHelper)3 CompositeRunfilesSupplier (com.google.devtools.build.lib.actions.CompositeRunfilesSupplier)2 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)2 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)2 Label (com.google.devtools.build.lib.cmdline.Label)2 ImmutableMap (com.google.common.collect.ImmutableMap)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 NestedSet (com.google.devtools.build.lib.collect.nestedset.NestedSet)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1