private static GenerationItem[] doGenerate(
      @NotNull final CompileContext context,
      @NotNull final GenerationItem[] items,
      VirtualFile outputRootDirectory) {
    if (context.getProject().isDisposed()) {
      return EMPTY_GENERATION_ITEM_ARRAY;
    }

    // we have one item per module there, so clear output directory
    final String genRootPath = FileUtil.toSystemDependentName(outputRootDirectory.getPath());
    final File genRootDir = new File(genRootPath);
    if (genRootDir.exists()) {
      if (!FileUtil.delete(genRootDir)) {
        context.addMessage(
            CompilerMessageCategory.ERROR, "Cannot delete directory " + genRootPath, null, -1, -1);
        return EMPTY_GENERATION_ITEM_ARRAY;
      }
      if (!genRootDir.mkdir()) {
        context.addMessage(
            CompilerMessageCategory.ERROR, "Cannot create directory " + genRootPath, null, -1, -1);
        return EMPTY_GENERATION_ITEM_ARRAY;
      }
    }

    final List<GenerationItem> results = new ArrayList<GenerationItem>(items.length);
    for (final GenerationItem item : items) {
      if (item instanceof MyGenerationItem) {
        final MyGenerationItem genItem = (MyGenerationItem) item;

        if (!AndroidCompileUtil.isModuleAffected(context, genItem.myModule)) {
          continue;
        }

        boolean success = true;

        for (final VirtualFile sourceFile : genItem.myFiles) {
          final String depFolderOsPath =
              getDependencyFolder(context.getProject(), sourceFile, outputRootDirectory);

          try {
            final Map<CompilerMessageCategory, List<String>> messages =
                AndroidCompileUtil.toCompilerMessageCategoryKeys(
                    AndroidRenderscript.execute(
                        genItem.mySdkLocation,
                        genItem.myAndroidTarget,
                        sourceFile.getPath(),
                        genRootPath,
                        depFolderOsPath,
                        genItem.myRawDirPath));

            ApplicationManager.getApplication()
                .runReadAction(
                    new Runnable() {
                      public void run() {
                        if (context.getProject().isDisposed()) {
                          return;
                        }
                        addMessages(context, messages, sourceFile.getUrl());
                      }
                    });

            if (messages.get(CompilerMessageCategory.ERROR).size() > 0) {
              success = false;
            }
          } catch (final IOException e) {
            ApplicationManager.getApplication()
                .runReadAction(
                    new Runnable() {
                      public void run() {
                        if (context.getProject().isDisposed()) return;
                        context.addMessage(
                            CompilerMessageCategory.ERROR,
                            e.getMessage(),
                            sourceFile.getUrl(),
                            -1,
                            -1);
                      }
                    });
            success = false;
          }
        }

        if (success) {
          results.add(genItem);
        }
      }
    }
    outputRootDirectory.refresh(false, true);
    return results.toArray(new GenerationItem[results.size()]);
  }
  public static void generate(GeneratingCompiler compiler, final CompileContext context) {
    if (context == null) {
      return;
    }

    final Set<Module> affectedModules = new HashSet<Module>();
    Collections.addAll(affectedModules, context.getCompileScope().getAffectedModules());

    ApplicationManager.getApplication()
        .invokeAndWait(
            new Runnable() {
              @Override
              public void run() {
                for (Module module : affectedModules) {
                  if (module.isDisposed() || module.getProject().isDisposed()) {
                    continue;
                  }

                  final AndroidFacet facet = AndroidFacet.getInstance(module);
                  if (facet != null) {
                    AndroidCompileUtil.createGenModulesAndSourceRoots(facet);
                  }
                }
              }
            },
            ModalityState.defaultModalityState());

    List<GeneratingCompiler.GenerationItem> itemsToGenerate =
        new ArrayList<GeneratingCompiler.GenerationItem>();
    for (GeneratingCompiler.GenerationItem item : compiler.getGenerationItems(context)) {
      if (affectedModules.contains(item.getModule())) {
        itemsToGenerate.add(item);
      }
    }

    GeneratingCompiler.GenerationItem[] items =
        itemsToGenerate.toArray(new GeneratingCompiler.GenerationItem[itemsToGenerate.size()]);

    final boolean[] run = {true};
    final VirtualFile[] files = getFilesToCheckReadonlyStatus(items);
    if (files.length > 0) {
      ApplicationManager.getApplication()
          .invokeAndWait(
              new Runnable() {
                @Override
                public void run() {
                  ApplicationManager.getApplication()
                      .runReadAction(
                          new Runnable() {
                            @Override
                            public void run() {
                              final Project project = context.getProject();
                              run[0] =
                                  !project.isDisposed()
                                      && ReadonlyStatusHandler.ensureFilesWritable(project, files);
                            }
                          });
                }
              },
              ModalityState.defaultModalityState());
    }

    if (run[0]) {
      compiler.generate(context, items, null);
    }
  }