org.jetbrains.kotlin.cli.js.K2JSCompiler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.cli.js;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ExceptionUtil;
import com.intellij.util.SmartList;
import kotlin.collections.ArraysKt;
import kotlin.collections.CollectionsKt;
import kotlin.collections.SetsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.analyzer.AnalysisResult;
import org.jetbrains.kotlin.backend.common.output.OutputFileCollection;
import org.jetbrains.kotlin.cli.common.*;
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments;
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArgumentsKt;
import org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants;
import org.jetbrains.kotlin.cli.common.config.ContentRootsKt;
import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport;
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity;
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
import org.jetbrains.kotlin.cli.common.messages.MessageUtil;
import org.jetbrains.kotlin.cli.common.output.OutputUtilsKt;
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
import org.jetbrains.kotlin.config.*;
import org.jetbrains.kotlin.incremental.components.ExpectActualTracker;
import org.jetbrains.kotlin.incremental.components.LookupTracker;
import org.jetbrains.kotlin.incremental.js.IncrementalDataProvider;
import org.jetbrains.kotlin.incremental.js.IncrementalResultsConsumer;
import org.jetbrains.kotlin.incremental.js.TranslationResultValue;
import org.jetbrains.kotlin.js.analyze.TopDownAnalyzerFacadeForJS;
import org.jetbrains.kotlin.js.analyzer.JsAnalysisResult;
import org.jetbrains.kotlin.js.config.EcmaVersion;
import org.jetbrains.kotlin.js.config.JSConfigurationKeys;
import org.jetbrains.kotlin.js.config.JsConfig;
import org.jetbrains.kotlin.js.config.SourceMapSourceEmbedding;
import org.jetbrains.kotlin.js.facade.K2JSTranslator;
import org.jetbrains.kotlin.js.facade.MainCallParameters;
import org.jetbrains.kotlin.js.facade.TranslationResult;
import org.jetbrains.kotlin.js.facade.TranslationUnit;
import org.jetbrains.kotlin.js.facade.exceptions.TranslationException;
import org.jetbrains.kotlin.js.sourceMap.SourceFilePathResolver;
import org.jetbrains.kotlin.metadata.deserialization.BinaryVersion;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus;
import org.jetbrains.kotlin.psi.KtFile;
import org.jetbrains.kotlin.resolve.CompilerEnvironment;
import org.jetbrains.kotlin.serialization.js.ModuleKind;
import org.jetbrains.kotlin.utils.*;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import static org.jetbrains.kotlin.cli.common.ExitCode.COMPILATION_ERROR;
import static org.jetbrains.kotlin.cli.common.ExitCode.OK;
import static org.jetbrains.kotlin.cli.common.UtilsKt.getLibraryFromHome;
import static org.jetbrains.kotlin.cli.common.UtilsKt.incrementalCompilationIsEnabledForJs;
import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.*;
public class K2JSCompiler extends CLICompiler {
private K2JsIrCompiler irCompiler = null;
@NotNull
private K2JsIrCompiler getIrCompiler() {
if (irCompiler == null)
irCompiler = new K2JsIrCompiler();
return irCompiler;
}
@Override
protected void addPlatformOptions(@NotNull List $self, @NotNull K2JSCompilerArguments arguments) {}
public static void main(String... args) {
doMain(new K2JSCompiler(), args);
}
private final K2JSCompilerPerformanceManager performanceManager = new K2JSCompilerPerformanceManager();
@NotNull
@Override
public K2JSCompilerArguments createArguments() {
return new K2JSCompilerArguments();
}
@NotNull
@Override
protected ExitCode doExecute(
@NotNull K2JSCompilerArguments arguments,
@NotNull CompilerConfiguration configuration,
@NotNull Disposable rootDisposable,
@Nullable KotlinPaths paths
) {
MessageCollector messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY);
boolean useFir = Boolean.TRUE.equals(configuration.get(CommonConfigurationKeys.USE_FIR));
if (K2JSCompilerArgumentsKt.isIrBackendEnabled(arguments) || useFir) {
return getIrCompiler().doExecute(arguments, configuration.copy(), rootDisposable, paths);
}
messageCollector.report(ERROR, "Old Kotlin/JS compiler is no longer supported. Please migrate to the new JS IR backend", null);
return COMPILATION_ERROR;
}
@Override
protected void setupPlatformSpecificArgumentsAndServices(
@NotNull CompilerConfiguration configuration, @NotNull K2JSCompilerArguments arguments,
@NotNull Services services
) {
if (K2JSCompilerArgumentsKt.isIrBackendEnabled(arguments)) {
getIrCompiler().setupPlatformSpecificArgumentsAndServices(configuration, arguments, services);
}
}
@NotNull
static String calculateSourceMapSourceRoot(
@NotNull MessageCollector messageCollector,
@NotNull K2JSCompilerArguments arguments
) {
File commonPath = null;
List pathToRoot = new ArrayList<>();
Map pathToRootIndexes = new HashMap<>();
try {
for (String path : arguments.getFreeArgs()) {
File file = new File(path).getCanonicalFile();
if (commonPath == null) {
commonPath = file;
while (file != null) {
pathToRoot.add(file);
file = file.getParentFile();
}
Collections.reverse(pathToRoot);
for (int i = 0; i < pathToRoot.size(); ++i) {
pathToRootIndexes.put(pathToRoot.get(i), i);
}
}
else {
while (file != null) {
Integer existingIndex = pathToRootIndexes.get(file);
if (existingIndex != null) {
existingIndex = Math.min(existingIndex, pathToRoot.size() - 1);
pathToRoot.subList(existingIndex + 1, pathToRoot.size()).clear();
commonPath = pathToRoot.get(pathToRoot.size() - 1);
break;
}
file = file.getParentFile();
}
if (file == null) {
break;
}
}
}
}
catch (IOException e) {
String text = ExceptionUtil.getThrowableText(e);
messageCollector.report(CompilerMessageSeverity.ERROR, "IO error occurred calculating source root:\n" + text, null);
return ".";
}
return commonPath != null ? commonPath.getPath() : ".";
}
@NotNull
@Override
public CommonCompilerPerformanceManager getDefaultPerformanceManager() {
return performanceManager;
}
@NotNull
@Override
public String executableScriptFileName() {
return "kotlinc-js";
}
@NotNull
@Override
protected BinaryVersion createMetadataVersion(@NotNull int[] versionArray) {
return new JsMetadataVersion(versionArray);
}
private static final class K2JSCompilerPerformanceManager extends CommonCompilerPerformanceManager {
public K2JSCompilerPerformanceManager() {
super("Kotlin to JS Compiler");
}
}
}