Add tools.jar for JPS plugin
This commit is contained in:
committed by
Mikhail Glukhikh
parent
71ba8e3ad0
commit
91412b0ee9
@@ -324,7 +324,7 @@ class JavacWrapper(javaFiles: Collection<File>,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return symbol?.let { SymbolBasedClass(it, this@JavacWrapper, it.classfile) }
|
return symbol.let { SymbolBasedClass(it, this@JavacWrapper, it.classfile) }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -28,28 +28,30 @@ import java.io.IOException;
|
|||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.lang.ref.SoftReference;
|
import java.lang.ref.SoftReference;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Collections;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.ERROR;
|
import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.ERROR;
|
||||||
|
|
||||||
public class CompilerRunnerUtil {
|
public class CompilerRunnerUtil {
|
||||||
|
|
||||||
private static SoftReference<ClassLoader> ourClassLoaderRef = new SoftReference<ClassLoader>(null);
|
private static SoftReference<ClassLoader> ourClassLoaderRef = new SoftReference<>(null);
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static synchronized ClassLoader getOrCreateClassLoader(
|
private static synchronized ClassLoader getOrCreateClassLoader(
|
||||||
@NotNull JpsCompilerEnvironment environment,
|
@NotNull JpsCompilerEnvironment environment,
|
||||||
@NotNull File libPath
|
@NotNull List<File> paths
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
ClassLoader classLoader = ourClassLoaderRef.get();
|
ClassLoader classLoader = ourClassLoaderRef.get();
|
||||||
if (classLoader == null) {
|
if (classLoader == null) {
|
||||||
classLoader = ClassPreloadingUtils.preloadClasses(
|
classLoader = ClassPreloadingUtils.preloadClasses(
|
||||||
Collections.singletonList(new File(libPath, "kotlin-compiler.jar")),
|
paths,
|
||||||
Preloader.DEFAULT_CLASS_NUMBER_ESTIMATE,
|
Preloader.DEFAULT_CLASS_NUMBER_ESTIMATE,
|
||||||
CompilerRunnerUtil.class.getClassLoader(),
|
CompilerRunnerUtil.class.getClassLoader(),
|
||||||
environment.getClassesToLoadByParent()
|
environment.getClassesToLoadByParent()
|
||||||
);
|
);
|
||||||
ourClassLoaderRef = new SoftReference<ClassLoader>(classLoader);
|
ourClassLoaderRef = new SoftReference<>(classLoader);
|
||||||
}
|
}
|
||||||
return classLoader;
|
return classLoader;
|
||||||
}
|
}
|
||||||
@@ -78,7 +80,17 @@ public class CompilerRunnerUtil {
|
|||||||
File libPath = getLibPath(environment.getKotlinPaths(), environment.getMessageCollector());
|
File libPath = getLibPath(environment.getKotlinPaths(), environment.getMessageCollector());
|
||||||
if (libPath == null) return null;
|
if (libPath == null) return null;
|
||||||
|
|
||||||
ClassLoader classLoader = getOrCreateClassLoader(environment, libPath);
|
List<File> paths = new ArrayList<>();
|
||||||
|
paths.add(new File(libPath, "kotlin-compiler.jar"));
|
||||||
|
|
||||||
|
if (Arrays.asList(arguments).contains("-Xuse-javac")) {
|
||||||
|
File toolsJar = getJdkToolsJar();
|
||||||
|
if (toolsJar != null) {
|
||||||
|
paths.add(toolsJar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ClassLoader classLoader = getOrCreateClassLoader(environment, paths);
|
||||||
|
|
||||||
Class<?> kompiler = Class.forName(compilerClassName, true, classLoader);
|
Class<?> kompiler = Class.forName(compilerClassName, true, classLoader);
|
||||||
Method exec = kompiler.getMethod(
|
Method exec = kompiler.getMethod(
|
||||||
@@ -90,4 +102,28 @@ public class CompilerRunnerUtil {
|
|||||||
|
|
||||||
return exec.invoke(kompiler.newInstance(), out, environment.getServices(), arguments);
|
return exec.invoke(kompiler.newInstance(), out, environment.getServices(), arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
static File getJdkToolsJar() throws IOException {
|
||||||
|
String javaHomePath = System.getProperty("java.home");
|
||||||
|
if (javaHomePath == null || javaHomePath.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
File javaHome = new File(javaHomePath);
|
||||||
|
File toolsJar = new File(javaHome, "lib/tools.jar");
|
||||||
|
if (toolsJar.exists()) {
|
||||||
|
return toolsJar.getCanonicalFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
// We might be inside jre.
|
||||||
|
if (javaHome.getName().equals("jre")) {
|
||||||
|
toolsJar = new File(javaHome.getParent(), "lib/tools.jar");
|
||||||
|
if (toolsJar.exists()) {
|
||||||
|
return toolsJar.getCanonicalFile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -227,7 +227,8 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
|
|||||||
getOrCreateDaemonConnection {
|
getOrCreateDaemonConnection {
|
||||||
val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, environment.messageCollector)
|
val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, environment.messageCollector)
|
||||||
val compilerPath = File(libPath, "kotlin-compiler.jar")
|
val compilerPath = File(libPath, "kotlin-compiler.jar")
|
||||||
val compilerId = CompilerId.makeCompilerId(compilerPath)
|
val toolsJarPath = CompilerRunnerUtil.getJdkToolsJar()
|
||||||
|
val compilerId = CompilerId.makeCompilerId(listOfNotNull(compilerPath, toolsJarPath) )
|
||||||
val daemonOptions = configureDaemonOptions()
|
val daemonOptions = configureDaemonOptions()
|
||||||
|
|
||||||
val clientFlagFile = KotlinCompilerClient.getOrCreateClientFlagFile(daemonOptions)
|
val clientFlagFile = KotlinCompilerClient.getOrCreateClientFlagFile(daemonOptions)
|
||||||
|
|||||||
Reference in New Issue
Block a user