private static void writeOutput(
     @NotNull CompilerConfiguration configuration,
     @NotNull ClassFileFactory outputFiles,
     @Nullable File outputDir,
     @Nullable File jarPath,
     boolean jarRuntime,
     @Nullable FqName mainClass) {
   if (jarPath != null) {
     CompileEnvironmentUtil.writeToJar(jarPath, jarRuntime, mainClass, outputFiles);
   } else {
     MessageCollector messageCollector =
         configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE);
     OutputUtilsKt.writeAll(
         outputFiles, outputDir == null ? new File(".") : outputDir, messageCollector);
   }
 }
  public static boolean compileModules(
      @NotNull KotlinCoreEnvironment environment,
      @NotNull CompilerConfiguration configuration,
      @NotNull List<Module> chunk,
      @NotNull File directory,
      @Nullable File jarPath,
      @NotNull List<String> friendPaths,
      boolean jarRuntime) {
    Map<Module, ClassFileFactory> outputFiles = Maps.newHashMap();

    ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();

    ModuleVisibilityManager moduleVisibilityManager =
        ModuleVisibilityManager.SERVICE.getInstance(environment.getProject());

    for (Module module : chunk) {
      moduleVisibilityManager.addModule(module);
    }

    for (String path : friendPaths) {
      moduleVisibilityManager.addFriendPath(path);
    }

    String targetDescription =
        "in targets ["
            + Joiner.on(", ")
                .join(
                    Collections2.transform(
                        chunk,
                        new Function<Module, String>() {
                          @Override
                          public String apply(@Nullable Module input) {
                            return input != null
                                ? input.getModuleName() + "-" + input.getModuleType()
                                : "<null>";
                          }
                        }))
            + "] ";
    AnalysisResult result = analyze(environment, targetDescription);
    if (result == null) {
      return false;
    }

    ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();

    result.throwIfError();

    for (Module module : chunk) {
      ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
      List<KtFile> jetFiles =
          CompileEnvironmentUtil.getKtFiles(
              environment.getProject(),
              getAbsolutePaths(directory, module),
              new Function1<String, Unit>() {
                @Override
                public Unit invoke(String s) {
                  throw new IllegalStateException("Should have been checked before: " + s);
                }
              });
      File moduleOutputDirectory = new File(module.getOutputDirectory());
      GenerationState generationState =
          generate(
              environment, result, jetFiles, module, moduleOutputDirectory, module.getModuleName());
      outputFiles.put(module, generationState.getFactory());
    }

    for (Module module : chunk) {
      ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
      writeOutput(
          configuration,
          outputFiles.get(module),
          new File(module.getOutputDirectory()),
          jarPath,
          jarRuntime,
          null);
    }
    return true;
  }