private CompileContextImpl startup()

in src/main/java/com/intellij/compiler/impl/InternalCompileDriver.java [357:450]


    private CompileContextImpl startup(final CompileScope scope,
                                       final boolean isRebuild,
                                       final boolean forceCompile,
                                       boolean withModalProgress, final CompileStatusNotification callback,
                                       final CompilerMessage message) {
        ApplicationManager.getApplication().assertIsDispatchThread();

        final boolean isUnitTestMode = ApplicationManager.getApplication().isUnitTestMode();
        final String name = CompilerBundle.message(isRebuild ? "compiler.content.name.rebuild" : forceCompile ? "compiler.content.name.recompile" : "compiler.content.name.make");
        final CompilerTask compileTask = new CompilerTask(
                myProject, name, isUnitTestMode, !withModalProgress, true, isCompilationStartedAutomatically(scope), withModalProgress
        );

        StatusBar.Info.set("", myProject, "Compiler");
        // ensure the project model seen by build process is up-to-date
        myProject.save();
        if (!isUnitTestMode) {
            ApplicationManager.getApplication().saveSettings();
        }
        PsiDocumentManager.getInstance(myProject).commitAllDocuments();
        FileDocumentManager.getInstance().saveAllDocuments();

        final CompileContextImpl compileContext = new CompileContextImpl(myProject, compileTask, scope, !isRebuild && !forceCompile, isRebuild);

        final Runnable compileWork = () -> {
            final ProgressIndicator indicator = compileContext.getProgressIndicator();
            if (indicator.isCanceled() || myProject.isDisposed()) {
                if (callback != null) {
                    callback.finished(true, 0, 0, compileContext);
                }
                return;
            }
            CompilerCacheManager compilerCacheManager = CompilerCacheManager.getInstance(myProject);
            try {
                LOG.info("COMPILATION STARTED (BUILD PROCESS)");
                if (message != null) {
                    compileContext.addMessage(message);
                }
                if (isRebuild) {
                    CompilerUtil.runInContext(compileContext, "Clearing build system data...",
                            (ThrowableRunnable<Throwable>)() -> compilerCacheManager
                                    .clearCaches(compileContext));
                }
                final boolean beforeTasksOk = executeCompileTasks(compileContext, true);

                final int errorCount = compileContext.getMessageCount(CompilerMessageCategory.ERROR);
                if (!beforeTasksOk || errorCount > 0) {
                    COMPILE_SERVER_BUILD_STATUS.set(compileContext, errorCount > 0 ? ExitStatus.ERRORS : ExitStatus.CANCELLED);
                    return;
                }

                final TaskFuture future = compileInExternalProcess(compileContext, false);
                if (future != null) {
                    while (!future.waitFor(200L, TimeUnit.MILLISECONDS)) {
                        if (indicator.isCanceled()) {
                            future.cancel(false);
                        }
                    }
                    if (!executeCompileTasks(compileContext, false)) {
                        COMPILE_SERVER_BUILD_STATUS.set(compileContext, ExitStatus.CANCELLED);
                    }
                    if (compileContext.getMessageCount(CompilerMessageCategory.ERROR) > 0) {
                        COMPILE_SERVER_BUILD_STATUS.set(compileContext, ExitStatus.ERRORS);
                    }
                }
            }
            catch (ProcessCanceledException ignored) {
                compileContext.putUserDataIfAbsent(COMPILE_SERVER_BUILD_STATUS, ExitStatus.CANCELLED);
            }
            catch (Throwable e) {
                LOG.error(e); // todo
            }
            finally {
                // flushCaches does not work in headless mode
                //compilerCacheManager.flushCaches();

                final long duration = notifyCompilationCompleted(compileContext, callback, COMPILE_SERVER_BUILD_STATUS.get(compileContext));
                String logMessage =
                        "\tCOMPILATION FINISHED (BUILD PROCESS); Errors: " +
                                compileContext.getMessageCount(CompilerMessageCategory.ERROR) +
                                "; warnings: " +
                                compileContext.getMessageCount(CompilerMessageCategory.WARNING);
                CompilerUtil.logDuration(logMessage, duration);
            }
        };

        compileTask.start(compileWork, () -> {
            if (isRebuild) {
                startup(scope, false, false, callback, null);
            }
            startup(scope, isRebuild, forceCompile, callback, message);
        });
        return compileContext;
    }