public static final <T extends TransitiveInfoProvider> Iterable<T> getTransitivePrerequisites( RuleContext ruleContext, Mode mode, final Class<T> classType) { IterablesChain.Builder<T> builder = IterablesChain.builder(); for (String attr : TRANSITIVE_ATTRIBUTES) { if (ruleContext.getAttribute(attr) != null) { builder.add(ruleContext.getPrerequisites(attr, mode, classType)); } } return builder.build(); }
private static IterablesChain<Artifact> getArchiveInputs( JavaTargetAttributes attributes, Function<Artifact, Artifact> derivedJarFunction) { IterablesChain.Builder<Artifact> inputs = IterablesChain.builder(); inputs.add( ImmutableList.copyOf( Iterables.transform(attributes.getRuntimeClassPathForArchive(), derivedJarFunction))); // TODO(bazel-team): Remove? Resources not used as input to singlejar action inputs.add(ImmutableList.copyOf(attributes.getResources().values())); inputs.add(attributes.getClassPathResources()); return inputs.build(); }
/** Builds the action as configured. */ public void build() throws InterruptedException { ImmutableList<Artifact> classpathResources = attributes.getClassPathResources(); Set<String> classPathResourceNames = new HashSet<>(); for (Artifact artifact : classpathResources) { String name = artifact.getExecPath().getBaseName(); if (!classPathResourceNames.add(name)) { ruleContext.attributeError( "classpath_resources", "entries must have different file names (duplicate: " + name + ")"); return; } } IterablesChain<Artifact> runtimeJars = runtimeJarsBuilder.build(); // TODO(kmb): Consider not using getArchiveInputs, specifically because we don't want/need to // transform anything but the runtimeClasspath and b/c we currently do it twice here and below IterablesChain.Builder<Artifact> inputs = IterablesChain.builder(); inputs.add(getArchiveInputs(attributes, derivedJars)); inputs.add(ImmutableList.copyOf(Iterables.transform(runtimeJars, derivedJars))); if (runfilesMiddleman != null) { inputs.addElement(runfilesMiddleman); } ImmutableList<Artifact> buildInfoArtifacts = ruleContext.getBuildInfo(JavaBuildInfoFactory.KEY); inputs.add(buildInfoArtifacts); Iterable<Artifact> runtimeClasspath = Iterables.transform( Iterables.concat(runtimeJars, attributes.getRuntimeClassPathForArchive()), derivedJars); if (launcher != null) { inputs.addElement(launcher); } CommandLine commandLine = semantics.buildSingleJarCommandLine( ruleContext.getConfiguration(), outputJar, javaStartClass, deployManifestLines, buildInfoArtifacts, classpathResources, runtimeClasspath, includeBuildData, compression, launcher); List<String> jvmArgs = ImmutableList.of("-client", SINGLEJAR_MAX_MEMORY); ResourceSet resourceSet = ResourceSet.createWithRamCpuIo(/*memoryMb = */ 200.0, /*cpuUsage = */ .2, /*ioUsage=*/ .2); // If singlejar's name ends with .jar, it is Java application, otherwise it is native. // TODO(asmundak): once b/28640279 is fixed (that is, the native singlejar is released), // eliminate this check, allowing only native singlejar. Artifact singlejar = getSingleJar(ruleContext); if (singlejar.getFilename().endsWith(".jar")) { ruleContext.registerAction( new SpawnAction.Builder() .addInputs(inputs.build()) .addTransitiveInputs(JavaHelper.getHostJavabaseInputs(ruleContext)) .addOutput(outputJar) .setResources(resourceSet) .setJarExecutable( ruleContext.getHostConfiguration().getFragment(Jvm.class).getJavaExecutable(), singlejar, jvmArgs) .setCommandLine(commandLine) .alwaysUseParameterFile(ParameterFileType.SHELL_QUOTED) .setProgressMessage("Building deploy jar " + outputJar.prettyPrint()) .setMnemonic("JavaDeployJar") .setExecutionInfo(ImmutableMap.of("supports-workers", "1")) .build(ruleContext)); } else { ruleContext.registerAction( new SpawnAction.Builder() .addInputs(inputs.build()) .addTransitiveInputs(JavaHelper.getHostJavabaseInputs(ruleContext)) .addOutput(outputJar) .setResources(resourceSet) .setExecutable(singlejar) .setCommandLine(commandLine) .alwaysUseParameterFile(ParameterFileType.SHELL_QUOTED) .setProgressMessage("Building deploy jar " + outputJar.prettyPrint()) .setMnemonic("JavaDeployJar") .build(ruleContext)); } }