use of com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder 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();
}
use of com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder in project bazel by bazelbuild.
the class Filegroup method create.
@Override
public ConfiguredTarget create(RuleContext ruleContext) throws RuleErrorException {
String outputGroupName = ruleContext.attributes().get("output_group", Type.STRING);
if (outputGroupName.endsWith(INTERNAL_SUFFIX)) {
ruleContext.throwWithAttributeError("output_group", String.format(ILLEGAL_OUTPUT_GROUP_ERROR, outputGroupName));
}
NestedSet<Artifact> filesToBuild = outputGroupName.isEmpty() ? PrerequisiteArtifacts.nestedSet(ruleContext, "srcs", Mode.TARGET) : getArtifactsForOutputGroup(outputGroupName, ruleContext.getPrerequisites("srcs", Mode.TARGET));
NestedSet<Artifact> middleman = CompilationHelper.getAggregatingMiddleman(ruleContext, Actions.escapeLabel(ruleContext.getLabel()), filesToBuild);
InstrumentedFilesProvider instrumentedFilesProvider = InstrumentedFilesCollector.collect(ruleContext, // what do *we* know about whether this is a source file or not
new InstrumentationSpec(FileTypeSet.ANY_FILE, "srcs", "deps", "data"), InstrumentedFilesCollector.NO_METADATA_COLLECTOR, filesToBuild);
RunfilesProvider runfilesProvider = RunfilesProvider.withData(new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES).build(), // If you're visiting a filegroup as data, then we also visit its data as data.
new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addTransitiveArtifacts(filesToBuild).addDataDeps(ruleContext).build());
return new RuleConfiguredTargetBuilder(ruleContext).add(RunfilesProvider.class, runfilesProvider).setFilesToBuild(filesToBuild).setRunfilesSupport(null, getExecutable(filesToBuild)).add(InstrumentedFilesProvider.class, instrumentedFilesProvider).add(MiddlemanProvider.class, new MiddlemanProvider(middleman)).add(FilegroupPathProvider.class, new FilegroupPathProvider(getFilegroupPath(ruleContext))).build();
}
use of com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder in project bazel by bazelbuild.
the class GenQuery method create.
@Override
@Nullable
public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException {
Artifact outputArtifact = ruleContext.createOutputArtifact();
// The query string
final String query = ruleContext.attributes().get("expression", Type.STRING);
OptionsParser optionsParser = OptionsParser.newOptionsParser(QueryOptions.class);
optionsParser.setAllowResidue(false);
try {
optionsParser.parse(ruleContext.attributes().get("opts", Type.STRING_LIST));
} catch (OptionsParsingException e) {
ruleContext.attributeError("opts", "error while parsing query options: " + e.getMessage());
return null;
}
// Parsed query options
QueryOptions queryOptions = optionsParser.getOptions(QueryOptions.class);
if (queryOptions.keepGoing) {
ruleContext.attributeError("opts", "option --keep_going is not allowed");
return null;
}
if (!queryOptions.universeScope.isEmpty()) {
ruleContext.attributeError("opts", "option --universe_scope is not allowed");
return null;
}
if (optionsParser.containsExplicitOption("order_results")) {
ruleContext.attributeError("opts", "option --order_results is not allowed");
return null;
}
if (optionsParser.containsExplicitOption("noorder_results")) {
ruleContext.attributeError("opts", "option --noorder_results is not allowed");
return null;
}
if (optionsParser.containsExplicitOption("order_output")) {
ruleContext.attributeError("opts", "option --order_output is not allowed");
return null;
}
// Force results to be deterministic.
queryOptions.orderOutput = OrderOutput.FULL;
// force relative_locations to true so it has a deterministic output across machines.
queryOptions.relativeLocations = true;
final byte[] result = executeQuery(ruleContext, queryOptions, getScope(ruleContext), query);
if (result == null || ruleContext.hasErrors()) {
return null;
}
ruleContext.registerAction(new QueryResultAction(ruleContext.getActionOwner(), outputArtifact, result));
NestedSet<Artifact> filesToBuild = NestedSetBuilder.create(Order.STABLE_ORDER, outputArtifact);
return new RuleConfiguredTargetBuilder(ruleContext).setFilesToBuild(filesToBuild).add(RunfilesProvider.class, RunfilesProvider.simple(new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addTransitiveArtifacts(filesToBuild).build())).build();
}
use of com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder 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();
}
use of com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder in project bazel by bazelbuild.
the class IosDevice method create.
@Override
public ConfiguredTarget create(RuleContext context) throws InterruptedException, RuleErrorException {
AppleConfiguration appleConfiguration = context.getFragment(AppleConfiguration.class);
String iosVersionAttribute = context.attributes().get(IosDeviceRule.IOS_VERSION_ATTR_NAME, STRING);
XcodeVersionProperties xcodeVersionProperties = (XcodeVersionProperties) context.getPrerequisite(IosDeviceRule.XCODE_ATTR_NAME, Mode.TARGET, XcodeVersionProperties.SKYLARK_CONSTRUCTOR.getKey());
DottedVersion xcodeVersion = null;
if (xcodeVersionProperties != null && xcodeVersionProperties.getXcodeVersion().isPresent()) {
xcodeVersion = xcodeVersionProperties.getXcodeVersion().get();
} else if (appleConfiguration.getXcodeVersion() != null) {
xcodeVersion = appleConfiguration.getXcodeVersion();
}
DottedVersion iosVersion;
if (!Strings.isNullOrEmpty(iosVersionAttribute)) {
iosVersion = DottedVersion.fromString(iosVersionAttribute);
} else if (xcodeVersionProperties != null) {
iosVersion = xcodeVersionProperties.getDefaultIosSdkVersion();
} else {
iosVersion = appleConfiguration.getSdkVersionForPlatform(Platform.IOS_SIMULATOR);
}
IosDeviceProvider provider = new IosDeviceProvider.Builder().setType(context.attributes().get(IosDeviceRule.TYPE_ATTR_NAME, STRING)).setIosVersion(iosVersion).setLocale(context.attributes().get(IosDeviceRule.LOCALE_ATTR_NAME, STRING)).setXcodeVersion(xcodeVersion).build();
return new RuleConfiguredTargetBuilder(context).add(RunfilesProvider.class, RunfilesProvider.EMPTY).addNativeDeclaredProvider(provider).add(IosTestSubstitutionProvider.class, provider.iosTestSubstitutionProvider()).build();
}
Aggregations