Пример #1
0
  @NotNull
  public static List<Module> loadModuleScript(
      String moduleScriptFile, MessageCollector messageCollector) {
    Disposable disposable =
        new Disposable() {
          @Override
          public void dispose() {}
        };
    CompilerConfiguration configuration = new CompilerConfiguration();
    File defaultRuntimePath = CompilerPathUtil.getRuntimePath();
    if (defaultRuntimePath != null) {
      configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, defaultRuntimePath);
    }
    configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, PathUtil.findRtJar());
    File jdkAnnotationsPath = CompilerPathUtil.getJdkAnnotationsPath();
    if (jdkAnnotationsPath != null) {
      configuration.add(JVMConfigurationKeys.ANNOTATIONS_PATH_KEY, jdkAnnotationsPath);
    }
    configuration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, moduleScriptFile);
    JetCoreEnvironment scriptEnvironment =
        JetCoreEnvironment.createCoreEnvironmentForJVM(disposable, configuration);

    GenerationState generationState =
        KotlinToJVMBytecodeCompiler.analyzeAndGenerate(
            new K2JVMCompileEnvironmentConfiguration(
                scriptEnvironment,
                messageCollector,
                false,
                BuiltinsScopeExtensionMode.ALL,
                false,
                BuiltinToJavaTypesMapping.ENABLED),
            false);
    if (generationState == null) {
      throw new CompileEnvironmentException(
          "Module script " + moduleScriptFile + " analyze failed");
    }

    List<Module> modules = runDefineModules(moduleScriptFile, generationState.getFactory());

    Disposer.dispose(disposable);

    if (modules == null) {
      throw new CompileEnvironmentException(
          "Module script " + moduleScriptFile + " compilation failed");
    }

    if (modules.isEmpty()) {
      throw new CompileEnvironmentException("No modules where defined by " + moduleScriptFile);
    }
    return modules;
  }
  private void blackBoxFileWithJavaByFullPath(@NotNull String directory) throws Exception {
    File dirFile = new File(directory);

    final List<String> javaFilePaths = new ArrayList<String>();
    final List<String> ktFilePaths = new ArrayList<String>();
    FileUtil.processFilesRecursively(
        dirFile,
        new Processor<File>() {
          @Override
          public boolean process(File file) {
            String path = relativePath(file);
            if (path.endsWith(".kt")) {
              ktFilePaths.add(path);
            } else if (path.endsWith(".java")) {
              javaFilePaths.add(path);
            }
            return true;
          }
        });

    CompilerConfiguration configuration =
        JetTestUtils.compilerConfigurationForTests(
            ConfigurationKind.ALL, TestJdkKind.FULL_JDK, JetTestUtils.getAnnotationsJar());
    configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, dirFile);
    myEnvironment = JetCoreEnvironment.createForTests(getTestRootDisposable(), configuration);
    loadFiles(ArrayUtil.toStringArray(ktFilePaths));
    classFileFactory =
        GenerationUtils.compileManyFilesGetGenerationStateForTest(
                myEnvironment.getProject(), myFiles.getPsiFiles())
            .getFactory();
    File kotlinOut = JetTestUtils.tmpDir(toString());
    OutputUtilsPackage.writeAllTo(classFileFactory, kotlinOut);

    // TODO: support several Java sources
    File javaOut = compileJava(KotlinPackage.single(javaFilePaths), kotlinOut.getPath());
    // Add javac output to classpath so that the created class loader can find generated Java
    // classes
    configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, javaOut);

    blackBox();
  }
Пример #3
0
  private CompilerConfiguration createConfiguration(
      @Nullable String stdlib,
      @Nullable String[] classpath,
      @Nullable String[] externalAnnotationsPath,
      @NotNull String[] sourceRoots,
      boolean enableInline,
      boolean enableOptimization) {
    KotlinPaths paths = getKotlinPathsForAntTask();
    CompilerConfiguration configuration = new CompilerConfiguration();
    configuration.addAll(CLASSPATH_KEY, PathUtil.getJdkClassesRoots());
    if ((stdlib != null) && (stdlib.trim().length() > 0)) {
      configuration.add(CLASSPATH_KEY, new File(stdlib));
    } else {
      File path = paths.getRuntimePath();
      if (path.exists()) {
        configuration.add(CLASSPATH_KEY, path);
      }
    }
    if ((classpath != null) && (classpath.length > 0)) {
      for (String path : classpath) {
        configuration.add(CLASSPATH_KEY, new File(path));
      }
    }
    if ((externalAnnotationsPath != null) && (externalAnnotationsPath.length > 0)) {
      for (String path : externalAnnotationsPath) {
        configuration.add(ANNOTATIONS_PATH_KEY, new File(path));
      }
    }
    File jdkAnnotationsPath = paths.getJdkAnnotationsPath();
    if (jdkAnnotationsPath.exists()) {
      configuration.add(ANNOTATIONS_PATH_KEY, jdkAnnotationsPath);
    }

    configuration.addAll(CommonConfigurationKeys.SOURCE_ROOTS_KEY, Arrays.asList(sourceRoots));
    for (String sourceRoot : sourceRoots) {
      File file = new File(sourceRoot);
      if (!file.isFile() || !"kt".equals(FileUtilRt.getExtension(file.getName()))) {
        configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, file);
      }
    }
    configuration.put(
        CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY,
        MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR);

    configuration.put(ENABLE_INLINE, enableInline);
    configuration.put(ENABLE_OPTIMIZATION, enableOptimization);

    // lets register any compiler plugins
    configuration.addAll(CLIConfigurationKeys.COMPILER_PLUGINS, getCompilerPlugins());
    return configuration;
  }