use of com.google.devtools.build.lib.analysis.FilesToRunProvider 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.FilesToRunProvider in project bazel by bazelbuild.
the class JavaToolchain method create.
@Override
public ConfiguredTarget create(RuleContext ruleContext) throws RuleErrorException {
final String source = ruleContext.attributes().get("source_version", Type.STRING);
final String target = ruleContext.attributes().get("target_version", Type.STRING);
final NestedSet<Artifact> bootclasspath = getArtifactList("bootclasspath", ruleContext);
final NestedSet<Artifact> extclasspath = getArtifactList("extclasspath", ruleContext);
final String encoding = ruleContext.attributes().get("encoding", Type.STRING);
final List<String> xlint = ruleContext.attributes().get("xlint", Type.STRING_LIST);
final List<String> misc = ruleContext.getTokenizedStringListAttr("misc");
final List<String> jvmOpts = ruleContext.attributes().get("jvm_opts", Type.STRING_LIST);
final boolean javacSupportsWorkers = ruleContext.attributes().get("javac_supports_workers", Type.BOOLEAN);
Artifact javac = getArtifact("javac", ruleContext);
Artifact javabuilder = getArtifact("javabuilder", ruleContext);
Artifact headerCompiler = getArtifact("header_compiler", ruleContext);
boolean forciblyDisableHeaderCompilation = ruleContext.attributes().get("forcibly_disable_header_compilation", Type.BOOLEAN);
Artifact singleJar = getArtifact("singlejar", ruleContext);
Artifact oneVersion = getArtifact("oneversion", ruleContext);
Artifact oneVersionWhitelist = getArtifact("oneversion_whitelist", ruleContext);
Artifact genClass = getArtifact("genclass", ruleContext);
Artifact resourceJarBuilder = getArtifact("resourcejar", ruleContext);
Artifact timezoneData = getArtifact("timezone_data", ruleContext);
FilesToRunProvider ijar = ruleContext.getExecutablePrerequisite("ijar", Mode.HOST);
ImmutableListMultimap<String, String> compatibleJavacOptions = getCompatibleJavacOptions(ruleContext);
final JavaToolchainData toolchainData = new JavaToolchainData(source, target, Artifact.toExecPaths(bootclasspath), Artifact.toExecPaths(extclasspath), encoding, xlint, misc, jvmOpts, javacSupportsWorkers ? SupportsWorkers.YES : SupportsWorkers.NO);
final JavaConfiguration configuration = ruleContext.getFragment(JavaConfiguration.class);
JavaToolchainProvider provider = JavaToolchainProvider.create(ruleContext.getLabel(), toolchainData, bootclasspath, extclasspath, configuration.getDefaultJavacFlags(), javac, javabuilder, headerCompiler, forciblyDisableHeaderCompilation, singleJar, oneVersion, oneVersionWhitelist, genClass, resourceJarBuilder, timezoneData, ijar, compatibleJavacOptions);
RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext).addSkylarkTransitiveInfo(JavaToolchainSkylarkApiProvider.NAME, new JavaToolchainSkylarkApiProvider()).add(JavaToolchainProvider.class, provider).setFilesToBuild(new NestedSetBuilder<Artifact>(Order.STABLE_ORDER).build()).add(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY));
return builder.build();
}
use of com.google.devtools.build.lib.analysis.FilesToRunProvider in project bazel by bazelbuild.
the class CompilationSupport method registerHeaderScanningActions.
/**
* Creates and registers ObjcHeaderScanning {@link SpawnAction}. Groups all the actions by their
* compilation command line arguments and creates a ObjcHeaderScanning action for each unique one.
*/
protected void registerHeaderScanningActions(ImmutableList<ObjcHeaderThinningInfo> headerThinningInfo, ObjcProvider objcProvider, CompilationArtifacts compilationArtifacts) {
if (headerThinningInfo.isEmpty()) {
return;
}
FilesToRunProvider headerScannerTool = getHeaderThinningToolExecutable();
PrerequisiteArtifacts appleSdks = ruleContext.getPrerequisiteArtifacts(ObjcRuleClasses.APPLE_SDK_ATTRIBUTE, Mode.TARGET);
ListMultimap<ImmutableList<String>, ObjcHeaderThinningInfo> objcHeaderThinningInfoByCommandLine = groupActionsByCommandLine(headerThinningInfo);
// Register a header scanning spawn action for each unique set of command line arguments
for (ImmutableList<String> args : objcHeaderThinningInfoByCommandLine.keySet()) {
SpawnAction.Builder builder = new SpawnAction.Builder().setMnemonic("ObjcHeaderScanning").setExecutable(headerScannerTool).addInputs(appleSdks.list());
CustomCommandLine.Builder cmdLine = CustomCommandLine.builder().add("--arch").add(appleConfiguration.getSingleArchitecture().toLowerCase()).add("--platform").add(appleConfiguration.getSingleArchPlatform().getLowerCaseNameInPlist()).add("--sdk_version").add(appleConfiguration.getSdkVersionForPlatform(appleConfiguration.getSingleArchPlatform()).toStringWithMinimumComponents(2)).add("--xcode_version").add(appleConfiguration.getXcodeVersion().toStringWithMinimumComponents(2)).add("--");
for (ObjcHeaderThinningInfo info : objcHeaderThinningInfoByCommandLine.get(args)) {
cmdLine.addJoinPaths(":", Lists.newArrayList(info.sourceFile.getExecPath(), info.headersListFile.getExecPath()));
builder.addInput(info.sourceFile).addOutput(info.headersListFile);
}
ruleContext.registerAction(builder.setCommandLine(cmdLine.add("--").add(args).build()).addInputs(compilationArtifacts.getPrivateHdrs()).addTransitiveInputs(attributes.hdrs()).addTransitiveInputs(objcProvider.get(ObjcProvider.HEADER)).addInputs(compilationArtifacts.getPchFile().asSet()).addTransitiveInputs(objcProvider.get(ObjcProvider.STATIC_FRAMEWORK_FILE)).addTransitiveInputs(objcProvider.get(ObjcProvider.DYNAMIC_FRAMEWORK_FILE)).build(ruleContext));
}
}
use of com.google.devtools.build.lib.analysis.FilesToRunProvider in project bazel by bazelbuild.
the class JavaCompilationHelper method createIjarAction.
/**
* Creates the Action that creates ijars from Jar files.
*
* @param inputJar the Jar to create the ijar for
* @param addPrefix whether to prefix the path of the generated ijar with the package and
* name of the current rule
* @return the Artifact to create with the Action
*/
protected static Artifact createIjarAction(RuleContext ruleContext, JavaToolchainProvider javaToolchain, Artifact inputJar, boolean addPrefix) {
Artifact interfaceJar = getIjarArtifact(ruleContext, inputJar, addPrefix);
FilesToRunProvider ijarTarget = javaToolchain.getIjar();
if (!ruleContext.hasErrors()) {
ruleContext.registerAction(new SpawnAction.Builder().addInput(inputJar).addOutput(interfaceJar).setExecutable(ijarTarget).useDefaultShellEnvironment().addArgument(inputJar.getExecPathString()).addArgument(interfaceJar.getExecPathString()).setProgressMessage("Extracting interface " + ruleContext.getLabel()).setMnemonic("JavaIjar").build(ruleContext));
}
return interfaceJar;
}
use of com.google.devtools.build.lib.analysis.FilesToRunProvider in project bazel by bazelbuild.
the class PythonUtils method generate2to3Action.
private static Artifact generate2to3Action(RuleContext ruleContext, Artifact input) {
FilesToRunProvider py2to3converter = ruleContext.getExecutablePrerequisite("$python2to3", RuleConfiguredTarget.Mode.HOST);
Artifact output = get2to3OutputArtifact(ruleContext, input);
List<String> argv = new ArrayList<>();
argv.add("--no-diffs");
argv.add("--nobackups");
argv.add("--write");
argv.add("--output-dir");
argv.add(output.getExecPath().getParentDirectory().toString());
argv.add("--write-unchanged-files");
argv.add(input.getExecPathString());
ruleContext.registerAction(new SpawnAction.Builder().addInput(input).addOutput(output).setExecutable(py2to3converter).addArguments(argv).setProgressMessage("Converting to Python 3: " + input.prettyPrint()).setMnemonic("2to3").build(ruleContext));
return output;
}
Aggregations