Search in sources :

Example 1 with StrictJavaDepsPlugin

use of com.google.devtools.build.buildjar.javac.plugins.dependency.StrictJavaDepsPlugin in project bazel by bazelbuild.

the class JavacTurbine method compile.

Result compile() throws IOException {
    ImmutableList.Builder<String> argbuilder = ImmutableList.builder();
    argbuilder.addAll(JavacOptions.removeBazelSpecificFlags(turbineOptions.javacOpts()));
    // Disable compilation of implicit source files.
    // This is insurance: the sourcepath is empty, so we don't expect implicit sources.
    argbuilder.add("-implicit:none");
    // Disable debug info
    argbuilder.add("-g:none");
    // Enable MethodParameters
    argbuilder.add("-parameters");
    // Compile-time jars always use Java 8
    argbuilder.add("-source");
    argbuilder.add("8");
    argbuilder.add("-target");
    argbuilder.add("8");
    ImmutableList<Path> processorpath;
    if (!turbineOptions.processors().isEmpty()) {
        argbuilder.add("-processor");
        argbuilder.add(Joiner.on(',').join(turbineOptions.processors()));
        processorpath = asPaths(turbineOptions.processorPath());
        // see b/31371210
        argbuilder.add("-Aexperimental_turbine_hjar");
    } else {
        processorpath = ImmutableList.of();
    }
    ImmutableList<Path> sources = ImmutableList.<Path>builder().addAll(asPaths(turbineOptions.sources())).addAll(getSourceJarEntries(turbineOptions)).build();
    JavacTurbineCompileRequest.Builder requestBuilder = JavacTurbineCompileRequest.builder().setSources(sources).setJavacOptions(argbuilder.build()).setBootClassPath(asPaths(turbineOptions.bootClassPath())).setProcessorClassPath(processorpath);
    // JavaBuilder exempts some annotation processors from Strict Java Deps enforcement.
    // To avoid having to apply the same exemptions here, we just ignore strict deps errors
    // and leave enforcement to JavaBuilder.
    DependencyModule dependencyModule = buildDependencyModule(turbineOptions, StrictJavaDeps.WARN);
    if (sources.isEmpty()) {
        // accept compilations with an empty source list for compatibility with JavaBuilder
        emitClassJar(Paths.get(turbineOptions.outputFile()), ImmutableMap.<String, OutputFileObject>of());
        dependencyModule.emitDependencyInformation(/*classpath=*/
        "", /*successful=*/
        true);
        return Result.OK_WITH_REDUCED_CLASSPATH;
    }
    Result result = Result.ERROR;
    JavacTurbineCompileResult compileResult;
    List<String> actualClasspath;
    List<String> originalClasspath = turbineOptions.classPath();
    List<String> compressedClasspath = dependencyModule.computeStrictClasspath(turbineOptions.classPath());
    requestBuilder.setStrictDepsPlugin(new StrictJavaDepsPlugin(dependencyModule));
    {
        // compile with reduced classpath
        actualClasspath = compressedClasspath;
        requestBuilder.setClassPath(asPaths(actualClasspath));
        compileResult = JavacTurbineCompiler.compile(requestBuilder.build());
        if (compileResult.success()) {
            result = Result.OK_WITH_REDUCED_CLASSPATH;
            context = compileResult.context();
        }
    }
    if (!compileResult.success() && hasRecognizedError(compileResult.output())) {
        // fall back to transitive classpath
        actualClasspath = originalClasspath;
        requestBuilder.setClassPath(asPaths(actualClasspath));
        compileResult = JavacTurbineCompiler.compile(requestBuilder.build());
        if (compileResult.success()) {
            result = Result.OK_WITH_FULL_CLASSPATH;
            context = compileResult.context();
        }
    }
    if (result.ok()) {
        emitClassJar(Paths.get(turbineOptions.outputFile()), compileResult.files());
        dependencyModule.emitDependencyInformation(CLASSPATH_JOINER.join(actualClasspath), compileResult.success());
    } else {
        out.print(compileResult.output());
    }
    return result;
}
Also used : Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) DependencyModule(com.google.devtools.build.buildjar.javac.plugins.dependency.DependencyModule) StrictJavaDepsPlugin(com.google.devtools.build.buildjar.javac.plugins.dependency.StrictJavaDepsPlugin) FileVisitResult(java.nio.file.FileVisitResult)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 DependencyModule (com.google.devtools.build.buildjar.javac.plugins.dependency.DependencyModule)1 StrictJavaDepsPlugin (com.google.devtools.build.buildjar.javac.plugins.dependency.StrictJavaDepsPlugin)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1