Search in sources :

Example 1 with JavaSourceRootDescriptor

use of org.jetbrains.jps.builders.java.JavaSourceRootDescriptor in project intellij-community by JetBrains.

the class JavaBuilder method compile.

private ExitCode compile(CompileContext context, ModuleChunk chunk, DirtyFilesHolder<JavaSourceRootDescriptor, ModuleBuildTarget> dirtyFilesHolder, Collection<File> files, OutputConsumer outputConsumer, JavaCompilingTool compilingTool) throws Exception {
    ExitCode exitCode = ExitCode.NOTHING_DONE;
    final boolean hasSourcesToCompile = !files.isEmpty();
    if (!hasSourcesToCompile && !dirtyFilesHolder.hasRemovedFiles()) {
        return exitCode;
    }
    final ProjectDescriptor pd = context.getProjectDescriptor();
    JavaBuilderUtil.ensureModuleHasJdk(chunk.representativeTarget().getModule(), context, BUILDER_NAME);
    final Collection<File> classpath = ProjectPaths.getCompilationClasspath(chunk, false);
    final Collection<File> platformCp = ProjectPaths.getPlatformCompilationClasspath(chunk, false);
    // begin compilation round
    final OutputFilesSink outputSink = new OutputFilesSink(context, outputConsumer, JavaBuilderUtil.getDependenciesRegistrar(context), chunk.getPresentableShortName());
    Collection<File> filesWithErrors = null;
    try {
        if (hasSourcesToCompile) {
            exitCode = ExitCode.OK;
            final Set<File> srcPath = new HashSet<>();
            final BuildRootIndex index = pd.getBuildRootIndex();
            for (ModuleBuildTarget target : chunk.getTargets()) {
                for (JavaSourceRootDescriptor rd : index.getTempTargetRoots(target, context)) {
                    srcPath.add(rd.root);
                }
            }
            final DiagnosticSink diagnosticSink = new DiagnosticSink(context);
            final String chunkName = chunk.getName();
            context.processMessage(new ProgressMessage("Parsing java... [" + chunk.getPresentableShortName() + "]"));
            final int filesCount = files.size();
            boolean compiledOk = true;
            if (filesCount > 0) {
                LOG.info("Compiling " + filesCount + " java files; module: " + chunkName + (chunk.containsTests() ? " (tests)" : ""));
                if (LOG.isDebugEnabled()) {
                    for (File file : files) {
                        LOG.debug("Compiling " + file.getPath());
                    }
                    LOG.debug(" classpath for " + chunkName + ":");
                    for (File file : classpath) {
                        LOG.debug("  " + file.getAbsolutePath());
                    }
                    LOG.debug(" platform classpath for " + chunkName + ":");
                    for (File file : platformCp) {
                        LOG.debug("  " + file.getAbsolutePath());
                    }
                }
                try {
                    compiledOk = compileJava(context, chunk, files, classpath, platformCp, srcPath, diagnosticSink, outputSink, compilingTool);
                } finally {
                    // heuristic: incorrect paths data recovery, so that the next make should not contain non-existing sources in 'recompile' list
                    filesWithErrors = diagnosticSink.getFilesWithErrors();
                    for (File file : filesWithErrors) {
                        if (!file.exists()) {
                            FSOperations.markDeleted(context, file);
                        }
                    }
                }
            }
            context.checkCanceled();
            if (!compiledOk && diagnosticSink.getErrorCount() == 0) {
                diagnosticSink.report(new PlainMessageDiagnostic(Diagnostic.Kind.ERROR, "Compilation failed: internal java compiler error"));
            }
            if (!Utils.PROCEED_ON_ERROR_KEY.get(context, Boolean.FALSE) && diagnosticSink.getErrorCount() > 0) {
                if (!compiledOk) {
                    diagnosticSink.report(new JpsInfoDiagnostic("Errors occurred while compiling module '" + chunkName + "'"));
                }
                throw new StopBuildException("Compilation failed: errors: " + diagnosticSink.getErrorCount() + "; warnings: " + diagnosticSink.getWarningCount());
            }
        }
    } finally {
        JavaBuilderUtil.registerFilesToCompile(context, files);
        if (filesWithErrors != null) {
            JavaBuilderUtil.registerFilesWithErrors(context, filesWithErrors);
        }
        JavaBuilderUtil.registerSuccessfullyCompiled(context, outputSink.getSuccessfullyCompiled());
    }
    return exitCode;
}
Also used : ProgressMessage(org.jetbrains.jps.incremental.messages.ProgressMessage) BuildRootIndex(org.jetbrains.jps.builders.BuildRootIndex) ProjectDescriptor(org.jetbrains.jps.cmdline.ProjectDescriptor) JavaSourceRootDescriptor(org.jetbrains.jps.builders.java.JavaSourceRootDescriptor) THashSet(gnu.trove.THashSet)

Example 2 with JavaSourceRootDescriptor

use of org.jetbrains.jps.builders.java.JavaSourceRootDescriptor in project intellij-community by JetBrains.

the class OutputFilesSink method save.

public void save(@NotNull final OutputFileObject fileObject) {
    final BinaryContent content = fileObject.getContent();
    final File srcFile = fileObject.getSourceFile();
    boolean isTemp = false;
    final JavaFileObject.Kind outKind = fileObject.getKind();
    if (srcFile != null && content != null) {
        final String sourcePath = FileUtil.toSystemIndependentName(srcFile.getPath());
        final JavaSourceRootDescriptor rootDescriptor = myContext.getProjectDescriptor().getBuildRootIndex().findJavaRootDescriptor(myContext, srcFile);
        try {
            if (rootDescriptor != null) {
                isTemp = rootDescriptor.isTemp;
                if (!isTemp) {
                    // first, handle [src->output] mapping and register paths for files_generated event
                    if (outKind == JavaFileObject.Kind.CLASS) {
                        // todo: avoid array copying?
                        myOutputConsumer.registerCompiledClass(rootDescriptor.target, new CompiledClass(fileObject.getFile(), srcFile, fileObject.getClassName(), content));
                    } else {
                        myOutputConsumer.registerOutputFile(rootDescriptor.target, fileObject.getFile(), Collections.<String>singleton(sourcePath));
                    }
                }
            } else {
                // was not able to determine the source root descriptor or the source root is excluded from compilation (e.g. for annotation processors)
                if (outKind == JavaFileObject.Kind.CLASS) {
                    myOutputConsumer.registerCompiledClass(null, new CompiledClass(fileObject.getFile(), srcFile, fileObject.getClassName(), content));
                }
            }
        } catch (IOException e) {
            myContext.processMessage(new CompilerMessage(JavaBuilder.BUILDER_NAME, e));
        }
        if (!isTemp && outKind == JavaFileObject.Kind.CLASS) {
            // register in mappings any non-temp class file
            try {
                final ClassReader reader = new FailSafeClassReader(content.getBuffer(), content.getOffset(), content.getLength());
                myMappingsCallback.associate(FileUtil.toSystemIndependentName(fileObject.getFile().getPath()), sourcePath, reader);
            } catch (Throwable e) {
                // need this to make sure that unexpected errors in, for example, ASM will not ruin the compilation  
                final String message = "Class dependency information may be incomplete! Error parsing generated class " + fileObject.getFile().getPath();
                LOG.info(message, e);
                myContext.processMessage(new CompilerMessage(JavaBuilder.BUILDER_NAME, BuildMessage.Kind.WARNING, message + "\n" + CompilerMessage.getTextFromThrowable(e), sourcePath));
            }
        }
    }
    if (outKind == JavaFileObject.Kind.CLASS) {
        myContext.processMessage(new ProgressMessage("Writing classes... " + myChunkName));
        if (!isTemp && srcFile != null) {
            mySuccessfullyCompiled.add(srcFile);
        }
    }
}
Also used : ProgressMessage(org.jetbrains.jps.incremental.messages.ProgressMessage) CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) CompiledClass(org.jetbrains.jps.incremental.CompiledClass) IOException(java.io.IOException) BinaryContent(org.jetbrains.jps.incremental.BinaryContent) FailSafeClassReader(com.intellij.compiler.instrumentation.FailSafeClassReader) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) JavaSourceRootDescriptor(org.jetbrains.jps.builders.java.JavaSourceRootDescriptor) File(java.io.File) FailSafeClassReader(com.intellij.compiler.instrumentation.FailSafeClassReader)

Example 3 with JavaSourceRootDescriptor

use of org.jetbrains.jps.builders.java.JavaSourceRootDescriptor in project intellij-community by JetBrains.

the class ModuleBuildTarget method writeConfiguration.

@Override
public void writeConfiguration(ProjectDescriptor pd, PrintWriter out) {
    final JpsModule module = getModule();
    int fingerprint = getDependenciesFingerprint();
    for (JavaSourceRootDescriptor root : pd.getBuildRootIndex().getTargetRoots(this, null)) {
        fingerprint += FileUtil.fileHashCode(root.getRootFile());
    }
    final LanguageLevel level = JpsJavaExtensionService.getInstance().getLanguageLevel(module);
    if (level != null) {
        fingerprint += level.name().hashCode();
    }
    final JpsJavaCompilerConfiguration config = JpsJavaExtensionService.getInstance().getOrCreateCompilerConfiguration(module.getProject());
    final String bytecodeTarget = config.getByteCodeTargetLevel(module.getName());
    if (bytecodeTarget != null) {
        fingerprint += bytecodeTarget.hashCode();
    }
    final CompilerEncodingConfiguration encodingConfig = pd.getEncodingConfiguration();
    final String encoding = encodingConfig.getPreferredModuleEncoding(module);
    if (encoding != null) {
        fingerprint += encoding.hashCode();
    }
    out.write(Integer.toHexString(fingerprint));
}
Also used : JpsModule(org.jetbrains.jps.model.module.JpsModule) JpsJavaCompilerConfiguration(org.jetbrains.jps.model.java.compiler.JpsJavaCompilerConfiguration) JavaSourceRootDescriptor(org.jetbrains.jps.builders.java.JavaSourceRootDescriptor)

Example 4 with JavaSourceRootDescriptor

use of org.jetbrains.jps.builders.java.JavaSourceRootDescriptor in project intellij-community by JetBrains.

the class ModuleBuildTarget method computeRootDescriptors.

@NotNull
@Override
public List<JavaSourceRootDescriptor> computeRootDescriptors(JpsModel model, ModuleExcludeIndex index, IgnoredFileIndex ignoredFileIndex, BuildDataPaths dataPaths) {
    List<JavaSourceRootDescriptor> roots = new ArrayList<>();
    JavaSourceRootType type = isTests() ? JavaSourceRootType.TEST_SOURCE : JavaSourceRootType.SOURCE;
    Iterable<ExcludedJavaSourceRootProvider> excludedRootProviders = JpsServiceManager.getInstance().getExtensions(ExcludedJavaSourceRootProvider.class);
    final JpsJavaCompilerConfiguration compilerConfig = JpsJavaExtensionService.getInstance().getOrCreateCompilerConfiguration(myModule.getProject());
    roots_loop: for (JpsTypedModuleSourceRoot<JavaSourceRootProperties> sourceRoot : myModule.getSourceRoots(type)) {
        if (index.isExcludedFromModule(sourceRoot.getFile(), myModule)) {
            continue;
        }
        for (ExcludedJavaSourceRootProvider provider : excludedRootProviders) {
            if (provider.isExcludedFromCompilation(myModule, sourceRoot)) {
                continue roots_loop;
            }
        }
        final String packagePrefix = sourceRoot.getProperties().getPackagePrefix();
        // consider annotation processors output for generated sources, if contained under some source root
        Set<File> excludes = computeRootExcludes(sourceRoot.getFile(), index);
        final ProcessorConfigProfile profile = compilerConfig.getAnnotationProcessingProfile(myModule);
        if (profile.isEnabled()) {
            final File outputDir = ProjectPaths.getAnnotationProcessorGeneratedSourcesOutputDir(myModule, JavaSourceRootType.TEST_SOURCE == sourceRoot.getRootType(), profile);
            if (outputDir != null && FileUtil.isAncestor(sourceRoot.getFile(), outputDir, true)) {
                excludes = ContainerUtil.newTroveSet(FileUtil.FILE_HASHING_STRATEGY, excludes);
                excludes.add(outputDir);
            }
        }
        roots.add(new JavaSourceRootDescriptor(sourceRoot.getFile(), this, false, false, packagePrefix, excludes));
    }
    return roots;
}
Also used : JpsJavaCompilerConfiguration(org.jetbrains.jps.model.java.compiler.JpsJavaCompilerConfiguration) THashSet(gnu.trove.THashSet) Set(java.util.Set) ExcludedJavaSourceRootProvider(org.jetbrains.jps.builders.java.ExcludedJavaSourceRootProvider) JpsTypedModuleSourceRoot(org.jetbrains.jps.model.module.JpsTypedModuleSourceRoot) ArrayList(java.util.ArrayList) ProcessorConfigProfile(org.jetbrains.jps.model.java.compiler.ProcessorConfigProfile) JavaSourceRootDescriptor(org.jetbrains.jps.builders.java.JavaSourceRootDescriptor) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with JavaSourceRootDescriptor

use of org.jetbrains.jps.builders.java.JavaSourceRootDescriptor in project intellij-community by JetBrains.

the class FSOperations method markDeleted.

public static void markDeleted(CompileContext context, File file) throws IOException {
    final JavaSourceRootDescriptor rd = context.getProjectDescriptor().getBuildRootIndex().findJavaRootDescriptor(context, file);
    if (rd != null) {
        final ProjectDescriptor pd = context.getProjectDescriptor();
        pd.fsState.registerDeleted(context, rd.target, file, pd.timestamps.getStorage());
    }
}
Also used : ProjectDescriptor(org.jetbrains.jps.cmdline.ProjectDescriptor) JavaSourceRootDescriptor(org.jetbrains.jps.builders.java.JavaSourceRootDescriptor)

Aggregations

JavaSourceRootDescriptor (org.jetbrains.jps.builders.java.JavaSourceRootDescriptor)20 File (java.io.File)9 THashSet (gnu.trove.THashSet)7 JpsModule (org.jetbrains.jps.model.module.JpsModule)5 THashMap (gnu.trove.THashMap)4 IOException (java.io.IOException)4 NotNull (org.jetbrains.annotations.NotNull)4 ProjectDescriptor (org.jetbrains.jps.cmdline.ProjectDescriptor)4 ProgressMessage (org.jetbrains.jps.incremental.messages.ProgressMessage)3 JpsJavaCompilerConfiguration (org.jetbrains.jps.model.java.compiler.JpsJavaCompilerConfiguration)3 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 BuildRootIndex (org.jetbrains.jps.builders.BuildRootIndex)2 ProjectBuilderLogger (org.jetbrains.jps.builders.logging.ProjectBuilderLogger)2 BuildDataCorruptedException (org.jetbrains.jps.builders.storage.BuildDataCorruptedException)2 ModuleBuildTarget (org.jetbrains.jps.incremental.ModuleBuildTarget)2 CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)2 EnhancerRunner (com.intellij.appengine.rt.EnhancerRunner)1 FailSafeClassReader (com.intellij.compiler.instrumentation.FailSafeClassReader)1 SmartList (com.intellij.util.SmartList)1