Search in sources :

Example 1 with TranslatingJavacPhaseTracer

use of com.facebook.buck.jvm.java.tracing.TranslatingJavacPhaseTracer in project buck by facebook.

the class Jsr199Javac method buildWithClasspath.

private int buildWithClasspath(JavacExecutionContext context, BuildTarget invokingRule, ImmutableList<String> options, ImmutableList<ResolvedJavacPluginProperties> annotationProcessors, ImmutableSortedSet<Path> javaSourceFilePaths, Path pathToSrcsList, JavaCompiler compiler, StandardJavaFileManager fileManager, Iterable<? extends JavaFileObject> compilationUnits, JavacOptions.AbiGenerationMode abiGenerationMode) {
    // since we do not print them out to console in case of error
    try {
        context.getProjectFilesystem().writeLinesToPath(FluentIterable.from(javaSourceFilePaths).transform(Object::toString).transform(ARGFILES_ESCAPER), pathToSrcsList);
    } catch (IOException e) {
        context.getEventSink().reportThrowable(e, "Cannot write list of .java files to compile to %s file! Terminating compilation.", pathToSrcsList);
        return 1;
    }
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    List<String> classNamesForAnnotationProcessing = ImmutableList.of();
    Writer compilerOutputWriter = new PrintWriter(context.getStdErr());
    JavaCompiler.CompilationTask compilationTask = compiler.getTask(compilerOutputWriter, context.getUsedClassesFileWriter().wrapFileManager(fileManager), diagnostics, options, classNamesForAnnotationProcessing, compilationUnits);
    boolean isSuccess = false;
    BuckTracing.setCurrentThreadTracingInterfaceFromJsr199Javac(new Jsr199TracingBridge(context.getEventSink(), invokingRule));
    Object abiValidatingTaskListener = null;
    if (abiGenerationMode != JavacOptions.AbiGenerationMode.CLASS) {
        abiValidatingTaskListener = SourceBasedAbiStubber.newValidatingTaskListener(context.getClassLoaderCache(), compilationTask, new FileManagerBootClasspathOracle(fileManager), abiGenerationMode == JavacOptions.AbiGenerationMode.SOURCE ? Diagnostic.Kind.ERROR : Diagnostic.Kind.WARNING);
    }
    try {
        try (// in some unusual situations
        TranslatingJavacPhaseTracer tracer = TranslatingJavacPhaseTracer.setupTracing(invokingRule, context.getClassLoaderCache(), context.getEventSink(), compilationTask, abiValidatingTaskListener);
            // may choke with novel errors that don't occur on the command line.
            AnnotationProcessorFactory processorFactory = new AnnotationProcessorFactory(context.getEventSink(), compiler.getClass().getClassLoader(), context.getClassLoaderCache(), invokingRule)) {
            compilationTask.setProcessors(processorFactory.createProcessors(annotationProcessors));
            // Invoke the compilation and inspect the result.
            isSuccess = compilationTask.call();
        } catch (IOException e) {
            LOG.warn(e, "Unable to close annotation processor class loader. We may be leaking memory.");
        }
    } finally {
        // Clear the tracing interface so we have no chance of leaking it to code that shouldn't
        // be using it.
        BuckTracing.clearCurrentThreadTracingInterfaceFromJsr199Javac();
    }
    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
        LOG.debug("javac: %s", DiagnosticPrettyPrinter.format(diagnostic));
    }
    List<Diagnostic<? extends JavaFileObject>> cleanDiagnostics = DiagnosticCleaner.clean(diagnostics.getDiagnostics());
    if (isSuccess) {
        context.getUsedClassesFileWriter().writeFile(context.getProjectFilesystem(), context.getObjectMapper());
        return 0;
    } else {
        if (context.getVerbosity().shouldPrintStandardInformation()) {
            int numErrors = 0;
            int numWarnings = 0;
            for (Diagnostic<? extends JavaFileObject> diagnostic : cleanDiagnostics) {
                Diagnostic.Kind kind = diagnostic.getKind();
                if (kind == Diagnostic.Kind.ERROR) {
                    ++numErrors;
                    handleMissingSymbolError(invokingRule, diagnostic, context);
                } else if (kind == Diagnostic.Kind.WARNING || kind == Diagnostic.Kind.MANDATORY_WARNING) {
                    ++numWarnings;
                }
                context.getStdErr().println(DiagnosticPrettyPrinter.format(diagnostic));
            }
            if (numErrors > 0 || numWarnings > 0) {
                context.getStdErr().printf("Errors: %d. Warnings: %d.\n", numErrors, numWarnings);
            }
        }
        return 1;
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) Diagnostic(javax.tools.Diagnostic) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) TranslatingJavacPhaseTracer(com.facebook.buck.jvm.java.tracing.TranslatingJavacPhaseTracer) JavaFileObject(javax.tools.JavaFileObject) DiagnosticCollector(javax.tools.DiagnosticCollector) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Aggregations

TranslatingJavacPhaseTracer (com.facebook.buck.jvm.java.tracing.TranslatingJavacPhaseTracer)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 Diagnostic (javax.tools.Diagnostic)1 DiagnosticCollector (javax.tools.DiagnosticCollector)1 JavaCompiler (javax.tools.JavaCompiler)1 JavaFileObject (javax.tools.JavaFileObject)1