public static boolean compileBunchOfSources(
      @NotNull KotlinCoreEnvironment environment,
      @Nullable File jar,
      @Nullable File outputDir,
      @NotNull List<String> friendPaths,
      boolean includeRuntime) {

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

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

    GenerationState generationState = analyzeAndGenerate(environment);
    if (generationState == null) {
      return false;
    }

    FqName mainClass = findMainClass(generationState, environment.getSourceFiles());

    try {
      writeOutput(
          environment.getConfiguration(),
          generationState.getFactory(),
          outputDir,
          jar,
          includeRuntime,
          mainClass);
      return true;
    } finally {
      generationState.destroy();
    }
  }
  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;
  }