Search in sources :

Example 1 with ExternalJavacManager

use of org.jetbrains.jps.javac.ExternalJavacManager in project intellij-community by JetBrains.

the class CompilerManagerImpl method getJavacManager.

@Nullable
private ExternalJavacManager getJavacManager() throws IOException {
    ExternalJavacManager manager = myExternalJavacManager;
    if (manager == null) {
        synchronized (this) {
            manager = myExternalJavacManager;
            if (manager == null) {
                final File compilerWorkingDir = getJavacCompilerWorkingDir();
                if (compilerWorkingDir == null) {
                    // should not happen for real projects
                    return null;
                }
                final int listenPort = NetUtils.findAvailableSocketPort();
                manager = new ExternalJavacManager(compilerWorkingDir);
                manager.start(listenPort);
                myExternalJavacManager = manager;
            }
        }
    }
    return manager;
}
Also used : ExternalJavacManager(org.jetbrains.jps.javac.ExternalJavacManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ExternalJavacManager

use of org.jetbrains.jps.javac.ExternalJavacManager in project intellij-community by JetBrains.

the class CompilerManagerImpl method compileJavaCode.

@Override
public Collection<ClassObject> compileJavaCode(List<String> options, Collection<File> platformCp, Collection<File> classpath, Collection<File> modulePath, Collection<File> sourcePath, Collection<File> files, File outputDir) throws IOException, CompilationException {
    final Pair<Sdk, JavaSdkVersion> runtime = BuildManager.getJavacRuntimeSdk(myProject);
    final Sdk sdk = runtime.getFirst();
    final SdkTypeId type = sdk.getSdkType();
    String javaHome = null;
    if (type instanceof JavaSdkType) {
        javaHome = sdk.getHomePath();
        if (!isJdkOrJre(javaHome)) {
            // this can be a java-dependent SDK, implementing JavaSdkType
            // hack, because there is no direct way to obtain the java sdk, this sdk depends on
            final String binPath = ((JavaSdkType) type).getBinPath(sdk);
            javaHome = binPath != null ? new File(binPath).getParent() : null;
            if (!isJdkOrJre(javaHome)) {
                javaHome = null;
            }
        }
    }
    if (javaHome == null) {
        throw new IOException("Was not able to determine JDK for project " + myProject.getName());
    }
    final OutputCollector outputCollector = new OutputCollector();
    DiagnosticCollector diagnostic = new DiagnosticCollector();
    final Set<File> sourceRoots = new THashSet<>(FileUtil.FILE_HASHING_STRATEGY);
    if (!sourcePath.isEmpty()) {
        for (File file : sourcePath) {
            sourceRoots.add(file);
        }
    } else {
        for (File file : files) {
            final File parentFile = file.getParentFile();
            if (parentFile != null) {
                sourceRoots.add(parentFile);
            }
        }
    }
    final Map<File, Set<File>> outs = Collections.singletonMap(outputDir, sourceRoots);
    final ExternalJavacManager javacManager = getJavacManager();
    boolean compiledOk = javacManager != null && javacManager.forkJavac(javaHome, -1, Collections.emptyList(), options, platformCp, classpath, modulePath, sourcePath, files, outs, diagnostic, outputCollector, new JavacCompilerTool(), CanceledStatus.NULL);
    if (!compiledOk) {
        final List<CompilationException.Message> messages = new SmartList<>();
        for (Diagnostic<? extends JavaFileObject> d : diagnostic.getDiagnostics()) {
            final JavaFileObject source = d.getSource();
            final URI uri = source != null ? source.toUri() : null;
            messages.add(new CompilationException.Message(kindToCategory(d.getKind()), d.getMessage(Locale.US), uri != null ? uri.toURL().toString() : null, (int) d.getLineNumber(), (int) d.getColumnNumber()));
        }
        throw new CompilationException("Compilation failed", messages);
    }
    final List<ClassObject> result = new ArrayList<>();
    for (OutputFileObject fileObject : outputCollector.getCompiledClasses()) {
        final BinaryContent content = fileObject.getContent();
        result.add(new CompiledClass(fileObject.getName(), fileObject.getClassName(), content != null ? content.toByteArray() : null));
    }
    return result;
}
Also used : JavacCompilerTool(org.jetbrains.jps.builders.impl.java.JavacCompilerTool) THashSet(gnu.trove.THashSet) BinaryContent(org.jetbrains.jps.incremental.BinaryContent) URI(java.net.URI) JavaFileObject(javax.tools.JavaFileObject) OutputFileObject(org.jetbrains.jps.javac.OutputFileObject) IOException(java.io.IOException) THashSet(gnu.trove.THashSet) ExternalJavacManager(org.jetbrains.jps.javac.ExternalJavacManager) SmartList(com.intellij.util.SmartList) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 3 with ExternalJavacManager

use of org.jetbrains.jps.javac.ExternalJavacManager in project intellij-community by JetBrains.

the class IncProjectBuilder method flushContext.

private static void flushContext(CompileContext context) {
    if (context != null) {
        final ProjectDescriptor pd = context.getProjectDescriptor();
        pd.timestamps.getStorage().force();
        pd.dataManager.flush(false);
    }
    final ExternalJavacManager server = ExternalJavacManager.KEY.get(context);
    if (server != null) {
        server.stop();
        ExternalJavacManager.KEY.set(context, null);
    }
}
Also used : ExternalJavacManager(org.jetbrains.jps.javac.ExternalJavacManager) ProjectDescriptor(org.jetbrains.jps.cmdline.ProjectDescriptor)

Aggregations

ExternalJavacManager (org.jetbrains.jps.javac.ExternalJavacManager)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 File (java.io.File)2 SmartList (com.intellij.util.SmartList)1 THashSet (gnu.trove.THashSet)1 IOException (java.io.IOException)1 URI (java.net.URI)1 JavaFileObject (javax.tools.JavaFileObject)1 Nullable (org.jetbrains.annotations.Nullable)1 JavacCompilerTool (org.jetbrains.jps.builders.impl.java.JavacCompilerTool)1 ProjectDescriptor (org.jetbrains.jps.cmdline.ProjectDescriptor)1 BinaryContent (org.jetbrains.jps.incremental.BinaryContent)1 OutputFileObject (org.jetbrains.jps.javac.OutputFileObject)1