@NotNull
  public static CompilerConfiguration createCompilerConfiguration(
      @NotNull CompilerConfiguration base, @NotNull List<Module> chunk, @NotNull File directory) {
    CompilerConfiguration configuration = base.copy();

    for (Module module : chunk) {
      ContentRootsKt.addKotlinSourceRoots(configuration, getAbsolutePaths(directory, module));
    }

    for (Module module : chunk) {
      for (JavaRootPath javaRootPath : module.getJavaSourceRoots()) {
        JvmContentRootsKt.addJavaSourceRoot(
            configuration, new File(javaRootPath.getPath()), javaRootPath.getPackagePrefix());
      }
    }

    for (Module module : chunk) {
      for (String classpathRoot : module.getClasspathRoots()) {
        JvmContentRootsKt.addJvmClasspathRoot(configuration, new File(classpathRoot));
      }
    }

    for (Module module : chunk) {
      configuration.add(JVMConfigurationKeys.MODULES, module);
    }

    return configuration;
  }
Пример #2
0
  @NotNull
  public static CompilerConfiguration compilerConfigurationForTests(
      @NotNull ConfigurationKind configurationKind,
      @NotNull TestJdkKind jdkKind,
      @NotNull List<File> classpath,
      @NotNull List<File> javaSource) {
    CompilerConfiguration configuration = new CompilerConfiguration();
    JvmContentRootsKt.addJavaSourceRoots(configuration, javaSource);
    if (jdkKind == TestJdkKind.MOCK_JDK) {
      JvmContentRootsKt.addJvmClasspathRoot(configuration, findMockJdkRtJar());
    } else if (jdkKind == TestJdkKind.ANDROID_API) {
      JvmContentRootsKt.addJvmClasspathRoot(configuration, findAndroidApiJar());
    } else {
      JvmContentRootsKt.addJvmClasspathRoots(configuration, PathUtil.getJdkClassesRoots());
    }

    if (configurationKind.getWithRuntime()) {
      JvmContentRootsKt.addJvmClasspathRoot(
          configuration, ForTestCompileRuntime.runtimeJarForTests());
    }
    if (configurationKind.getWithReflection()) {
      JvmContentRootsKt.addJvmClasspathRoot(
          configuration, ForTestCompileRuntime.reflectJarForTests());
    }

    JvmContentRootsKt.addJvmClasspathRoots(configuration, classpath);

    configuration.put(MODULE_NAME, "compilerConfigurationForTests");

    return configuration;
  }
 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);
   }
 }
  @NotNull
  private static GenerationState generate(
      @NotNull KotlinCoreEnvironment environment,
      @NotNull AnalysisResult result,
      @NotNull List<KtFile> sourceFiles,
      @Nullable Module module,
      File outputDirectory,
      String moduleName) {
    CompilerConfiguration configuration = environment.getConfiguration();
    IncrementalCompilationComponents incrementalCompilationComponents =
        configuration.get(JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS);

    Collection<FqName> packagesWithObsoleteParts;
    List<FqName> obsoleteMultifileClasses;
    TargetId targetId = null;

    if (module == null || incrementalCompilationComponents == null) {
      packagesWithObsoleteParts = Collections.emptySet();
      obsoleteMultifileClasses = Collections.emptyList();
    } else {
      targetId = TargetIdKt.TargetId(module);
      IncrementalCache incrementalCache =
          incrementalCompilationComponents.getIncrementalCache(targetId);

      packagesWithObsoleteParts = new HashSet<FqName>();
      for (String internalName : incrementalCache.getObsoletePackageParts()) {
        packagesWithObsoleteParts.add(JvmClassName.byInternalName(internalName).getPackageFqName());
      }

      obsoleteMultifileClasses = new ArrayList<FqName>();
      for (String obsoleteFacadeInternalName : incrementalCache.getObsoleteMultifileClasses()) {
        obsoleteMultifileClasses.add(
            JvmClassName.byInternalName(obsoleteFacadeInternalName)
                .getFqNameForClassNameWithoutDollars());
      }
    }
    BindingTraceContext diagnosticHolder = new BindingTraceContext();
    GenerationState generationState =
        new GenerationState(
            environment.getProject(),
            ClassBuilderFactories.BINARIES,
            result.getModuleDescriptor(),
            result.getBindingContext(),
            sourceFiles,
            configuration.get(JVMConfigurationKeys.DISABLE_CALL_ASSERTIONS, false),
            configuration.get(JVMConfigurationKeys.DISABLE_PARAM_ASSERTIONS, false),
            GenerationState.GenerateClassFilter.GENERATE_ALL,
            configuration.get(JVMConfigurationKeys.DISABLE_INLINE, false),
            configuration.get(JVMConfigurationKeys.DISABLE_OPTIMIZATION, false),
            /* useTypeTableInSerializer = */ false,
            diagnosticHolder,
            packagesWithObsoleteParts,
            obsoleteMultifileClasses,
            targetId,
            moduleName,
            outputDirectory,
            incrementalCompilationComponents);
    ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();

    long generationStart = PerformanceCounter.Companion.currentTime();

    KotlinCodegenFacade.compileCorrectFiles(
        generationState, CompilationErrorHandler.THROW_EXCEPTION);

    long generationNanos = PerformanceCounter.Companion.currentTime() - generationStart;
    String desc =
        module != null
            ? "target " + module.getModuleName() + "-" + module.getModuleType() + " "
            : "";
    String message =
        "GENERATE: "
            + sourceFiles.size()
            + " files ("
            + environment.countLinesOfCode(sourceFiles)
            + " lines) "
            + desc
            + "in "
            + TimeUnit.NANOSECONDS.toMillis(generationNanos)
            + " ms";
    K2JVMCompiler.Companion.reportPerf(environment.getConfiguration(), message);
    ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();

    AnalyzerWithCompilerReport.reportDiagnostics(
        new FilteredJvmDiagnostics(
            diagnosticHolder.getBindingContext().getDiagnostics(),
            result.getBindingContext().getDiagnostics()),
        environment.getConfiguration().get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY));
    ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
    return generationState;
  }