Ejemplo n.º 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;
  }
Ejemplo n.º 2
0
 /**
  * {@code CompileEnvironment#compileModules} wrapper.
  *
  * @param module compilation module file
  * @param jar compilation destination jar
  * @param includeRuntime whether Kotlin runtime library is included in destination jar
  * @param stdlib "kotlin-runtime.jar" path
  * @param classpath compilation classpath, can be <code>null</code> or empty
  * @param enableInline
  * @param enableOptimization
  */
 public void moduleToJar(
     @NotNull String module,
     @NotNull String jar,
     boolean includeRuntime,
     @Nullable String stdlib,
     @Nullable String[] classpath,
     @Nullable String[] externalAnnotationsPath,
     boolean enableInline,
     boolean enableOptimization) {
   try {
     ModuleScriptData moduleScriptData =
         loadModuleDescriptions(
             getKotlinPathsForAntTask(),
             module,
             MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR);
     List<Module> modules = moduleScriptData.getModules();
     List<String> sourcesRoots = new ArrayList<String>();
     for (Module m : modules) {
       sourcesRoots.addAll(m.getSourceFiles());
     }
     CompilerConfiguration configuration =
         createConfiguration(
             stdlib,
             classpath,
             externalAnnotationsPath,
             sourcesRoots.toArray(new String[0]),
             enableInline,
             enableOptimization);
     File directory = new File(module).getParentFile();
     boolean success =
         KotlinToJVMBytecodeCompiler.compileModules(
             configuration, modules, directory, new File(jar), includeRuntime);
     if (!success) {
       throw new CompileEnvironmentException(errorMessage(new String[] {module}, false));
     }
   } catch (Exception e) {
     throw new CompileEnvironmentException(errorMessage(new String[] {module}, true), e);
   }
 }
Ejemplo n.º 3
0
  /**
   * {@code CompileEnvironment#compileBunchOfSources} wrapper.
   *
   * @param src compilation source (directories or files)
   * @param output compilation destination directory
   * @param stdlib "kotlin-runtime.jar" path
   * @param classpath compilation classpath, can be <code>null</code> or empty
   */
  public void sourcesToDir(
      @NotNull String[] src,
      @NotNull String output,
      @Nullable String stdlib,
      @Nullable String[] classpath,
      @Nullable String[] externalAnnotationsPath,
      boolean enableInline,
      boolean enableOptimization) {
    try {
      JetCoreEnvironment environment =
          env(stdlib, classpath, externalAnnotationsPath, src, enableInline, enableOptimization);

      boolean success =
          KotlinToJVMBytecodeCompiler.compileBunchOfSources(
              environment, null, new File(output), true);
      if (!success) {
        throw new CompileEnvironmentException(errorMessage(src, false));
      }
    } catch (Exception e) {
      throw new CompileEnvironmentException(errorMessage(src, true), e);
    }
  }