Search in sources :

Example 1 with JavaCompilationArtifacts

use of com.google.devtools.build.lib.rules.java.JavaCompilationArtifacts in project bazel by bazelbuild.

the class AndroidCommon method initJava.

private void initJava(JavaSemantics javaSemantics, JavaCompilationHelper helper, JavaCompilationArtifacts.Builder javaArtifactsBuilder, boolean collectJavaCompilationArgs, NestedSetBuilder<Artifact> filesBuilder, boolean isBinary) throws InterruptedException {
    JavaTargetAttributes attributes = helper.getAttributes();
    if (ruleContext.hasErrors()) {
        // Avoid leaving filesToBuild set to null, otherwise we'll get a NullPointerException masking
        // the real error.
        filesToBuild = filesBuilder.build();
        return;
    }
    Artifact jar = null;
    if (attributes.hasSourceFiles() || attributes.hasSourceJars() || attributes.hasResources()) {
        // We only want to add a jar to the classpath of a dependent rule if it has content.
        javaArtifactsBuilder.addRuntimeJar(classJar);
        jar = classJar;
    }
    filesBuilder.add(classJar);
    manifestProtoOutput = helper.createManifestProtoOutput(classJar);
    // it is null, and the source jar action will not depend on the compile action.
    if (helper.usesAnnotationProcessing()) {
        genClassJar = helper.createGenJar(classJar);
        genSourceJar = helper.createGensrcJar(classJar);
        helper.createGenJarAction(classJar, manifestProtoOutput, genClassJar);
    }
    srcJar = ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_LIBRARY_SOURCE_JAR);
    javaSourceJarsProviderBuilder.addSourceJar(srcJar).addAllTransitiveSourceJars(javaCommon.collectTransitiveSourceJars(srcJar));
    helper.createSourceJarAction(srcJar, genSourceJar);
    outputDepsProto = helper.createOutputDepsProtoArtifact(classJar, javaArtifactsBuilder);
    helper.createCompileActionWithInstrumentation(classJar, manifestProtoOutput, genSourceJar, outputDepsProto, javaArtifactsBuilder);
    if (isBinary) {
        generatedExtensionRegistryProvider = javaSemantics.createGeneratedExtensionRegistry(ruleContext, javaCommon, filesBuilder, javaArtifactsBuilder, javaRuleOutputJarsProviderBuilder, javaSourceJarsProviderBuilder);
    }
    filesToBuild = filesBuilder.build();
    if ((attributes.hasSourceFiles() || attributes.hasSourceJars()) && jar != null) {
        iJar = helper.createCompileTimeJarAction(jar, javaArtifactsBuilder);
    }
    JavaCompilationArtifacts javaArtifacts = javaArtifactsBuilder.build();
    compileTimeDependencyArtifacts = javaCommon.collectCompileTimeDependencyArtifacts(javaArtifacts.getCompileTimeDependencyArtifact());
    javaCommon.setJavaCompilationArtifacts(javaArtifacts);
    javaCommon.setClassPathFragment(new ClasspathConfiguredFragment(javaCommon.getJavaCompilationArtifacts(), attributes, asNeverLink, helper.getBootclasspathOrDefault()));
    transitiveNeverlinkLibraries = collectTransitiveNeverlinkLibraries(ruleContext, javaCommon.getDependencies(), javaCommon.getJavaCompilationArtifacts().getRuntimeJars());
    if (collectJavaCompilationArgs) {
        boolean hasSources = attributes.hasSourceFiles() || attributes.hasSourceJars();
        this.javaCompilationArgs = collectJavaCompilationArgs(exportDeps, asNeverLink, hasSources);
        this.recursiveJavaCompilationArgs = collectJavaCompilationArgs(true, asNeverLink, /* hasSources */
        true);
    }
}
Also used : ClasspathConfiguredFragment(com.google.devtools.build.lib.rules.java.ClasspathConfiguredFragment) JavaCompilationArtifacts(com.google.devtools.build.lib.rules.java.JavaCompilationArtifacts) JavaTargetAttributes(com.google.devtools.build.lib.rules.java.JavaTargetAttributes) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 2 with JavaCompilationArtifacts

use of com.google.devtools.build.lib.rules.java.JavaCompilationArtifacts in project bazel by bazelbuild.

the class BazelJavaSemantics method createStubAction.

@Override
public Artifact createStubAction(RuleContext ruleContext, JavaCommon javaCommon, List<String> jvmFlags, Artifact executable, String javaStartClass, String javaExecutable) {
    Preconditions.checkState(ruleContext.getConfiguration().hasFragment(Jvm.class));
    Preconditions.checkNotNull(jvmFlags);
    Preconditions.checkNotNull(executable);
    Preconditions.checkNotNull(javaStartClass);
    Preconditions.checkNotNull(javaExecutable);
    List<Substitution> arguments = new ArrayList<>();
    String workspaceName = ruleContext.getWorkspaceName();
    final String workspacePrefix = workspaceName + (workspaceName.isEmpty() ? "" : "/");
    final boolean isRunfilesEnabled = ruleContext.getConfiguration().runfilesEnabled();
    arguments.add(Substitution.of("%runfiles_manifest_only%", isRunfilesEnabled ? "" : "1"));
    arguments.add(Substitution.of("%workspace_prefix%", workspacePrefix));
    arguments.add(Substitution.of("%javabin%", javaExecutable));
    arguments.add(Substitution.of("%needs_runfiles%", ruleContext.getFragment(Jvm.class).getJavaExecutable().isAbsolute() ? "0" : "1"));
    NestedSet<Artifact> classpath = javaCommon.getRuntimeClasspath();
    arguments.add(new ComputedClasspathSubstitution("%classpath%", classpath, workspacePrefix, isRunfilesEnabled));
    JavaCompilationArtifacts javaArtifacts = javaCommon.getJavaCompilationArtifacts();
    String path = javaArtifacts.getInstrumentedJar() != null ? "${JAVA_RUNFILES}/" + workspacePrefix + javaArtifacts.getInstrumentedJar().getRootRelativePath().getPathString() : "";
    arguments.add(Substitution.of("%set_jacoco_metadata%", ruleContext.getConfiguration().isCodeCoverageEnabled() ? "export JACOCO_METADATA_JAR=" + path : ""));
    arguments.add(Substitution.of("%java_start_class%", ShellEscaper.escapeString(javaStartClass)));
    arguments.add(Substitution.ofSpaceSeparatedList("%jvm_flags%", ImmutableList.copyOf(jvmFlags)));
    ruleContext.registerAction(new TemplateExpansionAction(ruleContext.getActionOwner(), executable, STUB_SCRIPT, arguments, true));
    if (OS.getCurrent() == OS.WINDOWS) {
        Artifact newExecutable = ruleContext.getImplicitOutputArtifact(ruleContext.getTarget().getName() + ".cmd");
        ruleContext.registerAction(new TemplateExpansionAction(ruleContext.getActionOwner(), newExecutable, STUB_SCRIPT_WINDOWS, ImmutableList.of(Substitution.of("%bash_exe_path%", ruleContext.getFragment(BazelConfiguration.class).getShellExecutable().getPathString()), Substitution.of("%cygpath_exe_path%", ruleContext.getFragment(BazelConfiguration.class).getShellExecutable().replaceName("cygpath.exe").getPathString())), true));
        return newExecutable;
    } else {
        return executable;
    }
}
Also used : Jvm(com.google.devtools.build.lib.rules.java.Jvm) TemplateExpansionAction(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction) Substitution(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution) ComputedSubstitution(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.ComputedSubstitution) JavaCompilationArtifacts(com.google.devtools.build.lib.rules.java.JavaCompilationArtifacts) BazelConfiguration(com.google.devtools.build.lib.bazel.rules.BazelConfiguration) ArrayList(java.util.ArrayList) Artifact(com.google.devtools.build.lib.actions.Artifact)

Aggregations

Artifact (com.google.devtools.build.lib.actions.Artifact)2 JavaCompilationArtifacts (com.google.devtools.build.lib.rules.java.JavaCompilationArtifacts)2 TemplateExpansionAction (com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction)1 ComputedSubstitution (com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.ComputedSubstitution)1 Substitution (com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution)1 BazelConfiguration (com.google.devtools.build.lib.bazel.rules.BazelConfiguration)1 ClasspathConfiguredFragment (com.google.devtools.build.lib.rules.java.ClasspathConfiguredFragment)1 JavaTargetAttributes (com.google.devtools.build.lib.rules.java.JavaTargetAttributes)1 Jvm (com.google.devtools.build.lib.rules.java.Jvm)1 ArrayList (java.util.ArrayList)1