use of com.google.devtools.build.lib.analysis.Runfiles 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.Runfiles in project bazel by bazelbuild.
the class JavaBinary method collectDefaultRunfiles.
private void collectDefaultRunfiles(Runfiles.Builder builder, RuleContext ruleContext, JavaCommon common, JavaCompilationArtifacts javaArtifacts, NestedSet<Artifact> filesToBuild, Artifact launcher, Iterable<Artifact> dynamicRuntimeActionInputs) {
// Convert to iterable: filesToBuild has a different order.
builder.addArtifacts((Iterable<Artifact>) filesToBuild);
builder.addArtifacts(javaArtifacts.getRuntimeJars());
if (launcher != null) {
final TransitiveInfoCollection defaultLauncher = JavaHelper.launcherForTarget(semantics, ruleContext);
final Artifact defaultLauncherArtifact = JavaHelper.launcherArtifactForTarget(semantics, ruleContext);
if (!defaultLauncherArtifact.equals(launcher)) {
builder.addArtifact(launcher);
// N.B. The "default launcher" referred to here is the launcher target specified through
// an attribute or flag. We wish to retain the runfiles of the default launcher, *except*
// for the original cc_binary artifact, because we've swapped it out with our custom
// launcher. Hence, instead of calling builder.addTarget(), or adding an odd method
// to Runfiles.Builder, we "unravel" the call and manually add things to the builder.
// Because the NestedSet representing each target's launcher runfiles is re-built here,
// we may see increased memory consumption for representing the target's runfiles.
Runfiles runfiles = defaultLauncher.getProvider(RunfilesProvider.class).getDefaultRunfiles();
NestedSetBuilder<Artifact> unconditionalArtifacts = NestedSetBuilder.compileOrder();
for (Artifact a : runfiles.getUnconditionalArtifacts()) {
if (!a.equals(defaultLauncherArtifact)) {
unconditionalArtifacts.add(a);
}
}
builder.addTransitiveArtifacts(unconditionalArtifacts.build());
builder.addSymlinks(runfiles.getSymlinks());
builder.addRootSymlinks(runfiles.getRootSymlinks());
builder.addPruningManifests(runfiles.getPruningManifests());
} else {
builder.addTarget(defaultLauncher, RunfilesProvider.DEFAULT_RUNFILES);
}
}
semantics.addRunfilesForBinary(ruleContext, launcher, builder);
builder.addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES);
builder.add(ruleContext, JavaRunfilesProvider.TO_RUNFILES);
List<? extends TransitiveInfoCollection> runtimeDeps = ruleContext.getPrerequisites("runtime_deps", Mode.TARGET);
builder.addTargets(runtimeDeps, JavaRunfilesProvider.TO_RUNFILES);
builder.addTargets(runtimeDeps, RunfilesProvider.DEFAULT_RUNFILES);
semantics.addDependenciesForRunfiles(ruleContext, builder);
if (ruleContext.getConfiguration().isCodeCoverageEnabled()) {
Artifact instrumentedJar = javaArtifacts.getInstrumentedJar();
if (instrumentedJar != null) {
builder.addArtifact(instrumentedJar);
}
}
builder.addArtifacts((Iterable<Artifact>) common.getRuntimeClasspath());
// Add the JDK files if it comes from the source repository (see java_stub_template.txt).
TransitiveInfoCollection javabaseTarget = ruleContext.getPrerequisite(":jvm", Mode.TARGET);
if (javabaseTarget != null) {
builder.addArtifacts((Iterable<Artifact>) javabaseTarget.getProvider(FileProvider.class).getFilesToBuild());
// version information contained within the libraries' package paths.
for (Artifact lib : dynamicRuntimeActionInputs) {
PathFragment path = CPP_RUNTIMES.getRelative(lib.getExecPath().getBaseName());
builder.addSymlink(path, lib);
}
}
}
use of com.google.devtools.build.lib.analysis.Runfiles in project bazel by bazelbuild.
the class IosTest method create.
/**
* {@inheritDoc}
*
* Creates a target, including registering actions, just as {@link #create(RuleContext)} does.
* The difference between {@link #create(RuleContext)} and this method is that this method does
* only what is needed to support tests on the environment besides generate the Xcodeproj file
* and build the app and test {@code .ipa}s. The {@link #create(RuleContext)} method delegates
* to this method.
*/
@Override
public final ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException {
ProtobufSupport protoSupport = new ProtobufSupport(ruleContext).registerGenerationActions().registerCompilationActions();
Optional<ObjcProvider> protosObjcProvider = protoSupport.getObjcProvider();
Optional<XcodeProvider> protosXcodeProvider = protoSupport.getXcodeProvider();
ObjcCommon common = common(ruleContext, protosObjcProvider);
if (!common.getCompilationArtifacts().get().getArchive().isPresent()) {
ruleContext.ruleError(REQUIRES_SOURCE_ERROR);
}
if (!ruleContext.getFragment(AppleConfiguration.class).getIosMultiCpus().isEmpty()) {
ruleContext.ruleError(NO_MULTI_CPUS_ERROR);
}
XcodeProvider.Builder xcodeProviderBuilder = new XcodeProvider.Builder().addPropagatedDependencies(protosXcodeProvider.asSet());
NestedSetBuilder<Artifact> filesToBuild = NestedSetBuilder.stableOrder();
addResourceFilesToBuild(ruleContext, common.getObjcProvider(), filesToBuild);
XcodeProductType productType = getProductType(ruleContext);
ExtraLinkArgs extraLinkArgs;
Iterable<Artifact> extraLinkInputs;
String bundleFormat;
if (!isXcTest(ruleContext)) {
extraLinkArgs = new ExtraLinkArgs();
extraLinkInputs = ImmutableList.of();
bundleFormat = ReleaseBundlingSupport.APP_BUNDLE_DIR_FORMAT;
} else {
xcodeProviderBuilder.setProductType(productType);
XcodeProvider appIpaXcodeProvider = ruleContext.getPrerequisite(XCTEST_APP_ATTR, Mode.TARGET, XcodeProvider.class);
if (appIpaXcodeProvider != null) {
xcodeProviderBuilder.setTestHost(appIpaXcodeProvider);
}
XcTestAppProvider testApp = xcTestAppProvider(ruleContext);
Artifact bundleLoader = testApp.getBundleLoader();
// -bundle causes this binary to be linked as a bundle and not require an entry point
// (i.e. main())
// -bundle_loader causes the code in this test to have access to the symbols in the test rig,
// or more specifically, the flag causes ld to consider the given binary when checking for
// missing symbols.
// -rpath @loader_path/Frameworks allows test bundles to load dylibs from the app's
// Frameworks directory.
extraLinkArgs = new ExtraLinkArgs("-bundle", "-bundle_loader", bundleLoader.getExecPathString(), "-Xlinker", "-rpath", "-Xlinker", "@loader_path/Frameworks");
extraLinkInputs = ImmutableList.of(bundleLoader);
bundleFormat = ReleaseBundlingSupport.XCTEST_BUNDLE_DIR_FORMAT;
filesToBuild.add(testApp.getIpa());
}
J2ObjcMappingFileProvider j2ObjcMappingFileProvider = J2ObjcMappingFileProvider.union(ruleContext.getPrerequisites("deps", Mode.TARGET, J2ObjcMappingFileProvider.class));
J2ObjcEntryClassProvider j2ObjcEntryClassProvider = new J2ObjcEntryClassProvider.Builder().addTransitive(ruleContext.getPrerequisites("deps", Mode.TARGET, J2ObjcEntryClassProvider.class)).build();
CompilationSupport.create(ruleContext).registerLinkActions(common.getObjcProvider(), j2ObjcMappingFileProvider, j2ObjcEntryClassProvider, extraLinkArgs, extraLinkInputs, DsymOutputType.TEST).registerCompileAndArchiveActions(common).registerFullyLinkAction(common.getObjcProvider(), ruleContext.getImplicitOutputArtifact(CompilationSupport.FULLY_LINKED_LIB)).addXcodeSettings(xcodeProviderBuilder, common).validateAttributes();
AppleConfiguration appleConfiguration = ruleContext.getFragment(AppleConfiguration.class);
new ReleaseBundlingSupport(ruleContext, common.getObjcProvider(), LinkedBinary.LOCAL_AND_DEPENDENCIES, bundleFormat, appleConfiguration.getMinimumOsForPlatformType(PlatformType.IOS), appleConfiguration.getMultiArchPlatform(PlatformType.IOS)).registerActions(DsymOutputType.TEST).addXcodeSettings(xcodeProviderBuilder).addFilesToBuild(filesToBuild, Optional.of(DsymOutputType.TEST)).validateResources().validateAttributes();
new ResourceSupport(ruleContext).validateAttributes().addXcodeSettings(xcodeProviderBuilder);
new XcodeSupport(ruleContext).addXcodeSettings(xcodeProviderBuilder, common.getObjcProvider(), productType).addDependencies(xcodeProviderBuilder, new Attribute("bundles", Mode.TARGET)).addDependencies(xcodeProviderBuilder, new Attribute("deps", Mode.TARGET)).addNonPropagatedDependencies(xcodeProviderBuilder, new Attribute("non_propagated_deps", Mode.TARGET)).addFilesToBuild(filesToBuild).registerActions(xcodeProviderBuilder.build());
XcodeProvider xcodeProvider = xcodeProviderBuilder.build();
NestedSet<Artifact> filesToBuildSet = filesToBuild.build();
Runfiles.Builder runfilesBuilder = new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES);
NestedSetBuilder<Artifact> filesToBuildBuilder = NestedSetBuilder.<Artifact>stableOrder().addTransitive(filesToBuildSet);
InstrumentedFilesProvider instrumentedFilesProvider = CompilationSupport.create(ruleContext).getInstrumentedFilesProvider(common);
TestSupport testSupport = new TestSupport(ruleContext).registerTestRunnerActions().addRunfiles(runfilesBuilder, instrumentedFilesProvider).addFilesToBuild(filesToBuildBuilder);
Artifact executable = testSupport.generatedTestScript();
Runfiles runfiles = runfilesBuilder.build();
RunfilesSupport runfilesSupport = RunfilesSupport.withExecutable(ruleContext, runfiles, executable);
ImmutableMap.Builder<String, String> execInfoMapBuilder = new ImmutableMap.Builder<>();
execInfoMapBuilder.put(ExecutionRequirements.REQUIRES_DARWIN, "");
if (ruleContext.getFragment(ObjcConfiguration.class).runMemleaks()) {
execInfoMapBuilder.put("nosandbox", "");
}
return new RuleConfiguredTargetBuilder(ruleContext).setFilesToBuild(filesToBuildBuilder.build()).addProvider(xcodeProvider).addProvider(RunfilesProvider.simple(runfiles)).addNativeDeclaredProvider(new ExecutionInfoProvider(execInfoMapBuilder.build())).addNativeDeclaredProviders(testSupport.getExtraProviders()).addProvider(InstrumentedFilesProvider.class, instrumentedFilesProvider).setRunfilesSupport(runfilesSupport, executable).build();
}
use of com.google.devtools.build.lib.analysis.Runfiles in project bazel by bazelbuild.
the class JavaRuntime method create.
@Override
public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException {
NestedSet<Artifact> filesToBuild = PrerequisiteArtifacts.nestedSet(ruleContext, "srcs", Mode.TARGET);
NestedSet<Artifact> middleman = CompilationHelper.getAggregatingMiddleman(ruleContext, Actions.escapeLabel(ruleContext.getLabel()), filesToBuild);
// TODO(cushon): clean up uses of java_runtime in data deps and remove this
Runfiles runfiles = new Runfiles.Builder(ruleContext.getWorkspaceName()).addTransitiveArtifacts(filesToBuild).build();
return new RuleConfiguredTargetBuilder(ruleContext).addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles)).setFilesToBuild(filesToBuild).addProvider(JavaRuntimeProvider.class, JavaRuntimeProvider.create(filesToBuild)).addProvider(MiddlemanProvider.class, new MiddlemanProvider(middleman)).build();
}
use of com.google.devtools.build.lib.analysis.Runfiles in project bazel by bazelbuild.
the class JavaCommon method getRunfiles.
public static Runfiles getRunfiles(RuleContext ruleContext, JavaSemantics semantics, JavaCompilationArtifacts javaArtifacts, boolean neverLink) {
// runfiles from this target or its dependencies.
if (neverLink) {
return Runfiles.EMPTY;
}
Runfiles.Builder runfilesBuilder = new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addArtifacts(javaArtifacts.getRuntimeJars());
runfilesBuilder.addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES);
runfilesBuilder.add(ruleContext, JavaRunfilesProvider.TO_RUNFILES);
List<TransitiveInfoCollection> depsForRunfiles = new ArrayList<>();
if (ruleContext.getRule().isAttrDefined("runtime_deps", BuildType.LABEL_LIST)) {
depsForRunfiles.addAll(ruleContext.getPrerequisites("runtime_deps", Mode.TARGET));
}
if (ruleContext.getRule().isAttrDefined("exports", BuildType.LABEL_LIST)) {
depsForRunfiles.addAll(ruleContext.getPrerequisites("exports", Mode.TARGET));
}
runfilesBuilder.addTargets(depsForRunfiles, RunfilesProvider.DEFAULT_RUNFILES);
runfilesBuilder.addTargets(depsForRunfiles, JavaRunfilesProvider.TO_RUNFILES);
TransitiveInfoCollection launcher = JavaHelper.launcherForTarget(semantics, ruleContext);
if (launcher != null) {
runfilesBuilder.addTarget(launcher, RunfilesProvider.DATA_RUNFILES);
}
semantics.addRunfilesForLibrary(ruleContext, runfilesBuilder);
return runfilesBuilder.build();
}
Aggregations