Search in sources :

Example 1 with CompileInput

use of mb.stratego.build.strincr.task.input.CompileInput in project spoofax by metaborg.

the class GenerateSourcesBuilder method buildStratego.

private void buildStratego(GenerateSourcesBuilder.Input input, Origin sdfOrigin) throws IOException, MetaborgException {
    final File targetMetaborgDir = toFile(paths.targetMetaborgDir());
    final File strFile = input.strFile;
    final Boolean strEnabled = input.strEnabled;
    if (strFile != null && strEnabled) {
        require(strFile, FileExistsStamper.instance);
        if (!strFile.exists()) {
            throw new IOException("Main Stratego file at " + strFile + " does not exist");
        }
        boolean buildStrJavaStrat = input.strJavaStratPackage != null && input.strJavaStratFile != null;
        if (buildStrJavaStrat) {
            require(input.strJavaStratFile, FileExistsStamper.instance);
            if (!input.strJavaStratFile.exists()) {
                throw new IOException("Main Stratego Java strategies file at " + input.strJavaStratFile + " does not exist");
            }
        }
        final Arguments extraArgs = new Arguments();
        extraArgs.addAll(input.strjArgs);
        final File outputFile;
        final File depPath;
        final File outputDir;
        if (input.strFormat == StrategoFormat.ctree) {
            outputFile = FileUtils.getFile(targetMetaborgDir, "stratego.ctree");
            depPath = outputFile;
            outputDir = depPath;
            extraArgs.add("-F");
        } else {
            depPath = toFile(paths.strSrcGenJavaTransDir(input.languageId.id));
            outputDir = toFile(paths.strSrcGenJavaDir());
            outputFile = toFile(paths.strSrcGenJavaTransDir(input.languageId.id).resolveFile("Main.java"));
            extraArgs.add("-la", "java-front");
            if (buildStrJavaStrat) {
                extraArgs.add("-la", input.strJavaStratPackage);
            }
        }
        if (input.strExternalJarFlags != null) {
            extraArgs.addLine(input.strExternalJarFlags);
        }
        final File cacheDir = toFile(paths.strCacheDir());
        if (input.strategoVersion == StrategoVersion.v1) {
            final Strj.Input strjInput = new Strj.Input(context, strFile, outputFile, depPath, input.strJavaPackage, true, true, input.strjIncludeDirs, input.strjIncludeFiles, Lists.newArrayList(), cacheDir, extraArgs, sdfOrigin);
            final Origin strjOrigin = Strj.origin(strjInput);
            requireBuild(strjOrigin);
            return;
        }
        /*
             * Make sure to require all the sdf stuff before running the stratego compiler which will search for the
             * generated stratego files.
             */
        requireBuild(sdfOrigin);
        logger.info("> Compile Stratego code using the incremental compiler");
        final File projectLocation = context.resourceService().localPath(paths.root());
        assert projectLocation != null;
        /*
             * Make sure Pluto also understands which files Pie will require.
             */
        final Set<Path> changedFiles = getChangedFiles(projectLocation);
        final Set<ResourceKey> changedResources = new HashSet<>(changedFiles.size() * 2);
        for (Path changedFile : changedFiles) {
            require(changedFile.toFile(), FileHashStamper.instance);
            changedResources.add(new FSPath(changedFile));
        }
        final ArrayList<IModuleImportService.ModuleIdentifier> linkedLibraries = new ArrayList<>();
        final ArrayList<ResourcePath> strjIncludeDirs = new ArrayList<>();
        for (File strjIncludeDir : input.strjIncludeDirs) {
            FSPath fsPath = new FSPath(strjIncludeDir);
            strjIncludeDirs.add(fsPath);
        }
        final Arguments newArgs = GenerateSourcesBuilder.splitOffLinkedLibrariesIncludeDirs(extraArgs, linkedLibraries, strjIncludeDirs, projectLocation.getPath());
        final String strFileName = strFile.getName();
        final String mainModuleName = strFileName.substring(0, strFileName.length() - ".str2".length());
        final boolean legacyStratego = false;
        final boolean isLibrary = false;
        final ModuleIdentifier mainModuleIdentifier = new ModuleIdentifier(legacyStratego, isLibrary, mainModuleName, new FSPath(strFile));
        final ResourcePath projectPath = new FSPath(projectLocation);
        final String packageName = NameUtil.toJavaId(input.languageId.id) + ".trans";
        final ResourcePath str2libReplicateDir = new FSPath(context.resourceService().localPath(paths.targetClassesDir()));
        final ArrayList<Supplier<Stratego2LibInfo>> str2libraries = new ArrayList<>(input.str2libraries);
        final boolean library = true;
        final boolean autoImportStd = false;
        final CompileInput compileInput = new CompileInput(mainModuleIdentifier, projectPath, new FSPath(outputDir), str2libReplicateDir, packageName, new FSPath(cacheDir), new ArrayList<>(0), strjIncludeDirs, linkedLibraries, newArgs, new ArrayList<>(0), library, autoImportStd, input.strategoShadowJar, input.languageId.id, str2libraries);
        final Task<CompileOutput> compileTask = context.getCompileTask().createTask(compileInput);
        final IPieProvider pieProvider = context.pieProvider();
        final Pie pie = pieProvider.pie();
        synchronized (pie) {
            initCompiler(pieProvider, compileTask, depPath);
            try (final MixedSession session = pie.newSession()) {
                TopDownSession tdSession = session.updateAffectedBy(changedResources);
                session.deleteUnobservedTasks(t -> true, (t, r) -> {
                    if (r != null && Objects.equals(r.getLeafExtension(), "java")) {
                        logger.debug("Deleting garbage from previous build: " + r);
                        return true;
                    }
                    return false;
                });
                final CompileOutput compileOutput = tdSession.getOutput(compileTask);
                if (compileOutput instanceof CompileOutput.Failure) {
                    logger.info("> Incremental compilation of Stratego failed:");
                    final CompileOutput.Failure failure = (CompileOutput.Failure) compileOutput;
                    final ArrayList<String> notes = new ArrayList<>();
                    final ArrayList<String> warnings = new ArrayList<>();
                    final ArrayList<String> errors = new ArrayList<>();
                    for (Message message : failure.messages) {
                        switch(message.severity) {
                            case NOTE:
                                if (message.filename != null && Paths.get(new URI(message.filename)).startsWith(projectLocation.toPath())) {
                                    if (!(message.filename.endsWith(".str") && message instanceof TypeMessage)) {
                                        notes.add(message.toString());
                                    }
                                }
                                break;
                            case WARNING:
                                if (message.filename != null) {
                                    final URI uri = new URI(message.filename);
                                    if (uri.getScheme() != null) {
                                        if (Paths.get(uri).startsWith(projectLocation.toPath())) {
                                            if (!(message.filename.endsWith(".str") && message instanceof TypeMessage)) {
                                                warnings.add(message.toString());
                                            }
                                        }
                                    } else {
                                        logger.error("no uri scheme on filename of message: " + message);
                                    }
                                }
                                break;
                            case ERROR:
                                errors.add(message.toString());
                                break;
                        }
                    }
                    for (String note : notes) {
                        logger.info(note);
                    }
                    for (String warning : warnings) {
                        logger.warn(warning);
                    }
                    for (String error : errors) {
                        logger.error(error);
                    }
                    throw MetaborgException.withoutStackTrace("Incremental Stratego Compilation failed with " + errors.size() + " errors.", null);
                } else {
                    assert compileOutput instanceof CompileOutput.Success;
                    final CompileOutput.Success success = (CompileOutput.Success) compileOutput;
                    for (ResourcePath resultFile : success.resultFiles) {
                        final File file = context.getMbResourceService().toLocalFile(resultFile);
                        provide(file);
                    }
                }
            } catch (ExecException e) {
                throw MetaborgException.withoutStackTrace("Incremental Stratego build failed: " + e.getMessage(), e);
            } catch (InterruptedException e) {
            // Ignore
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : Origin(build.pluto.dependency.Origin) TypeMessage(mb.stratego.build.strincr.message.type.TypeMessage) Message(mb.stratego.build.strincr.message.Message) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) CompileInput(mb.stratego.build.strincr.task.input.CompileInput) SpoofaxInput(org.metaborg.spoofax.meta.core.pluto.SpoofaxInput) ResourcePath(mb.resource.hierarchical.ResourcePath) Supplier(mb.pie.api.Supplier) CompileOutput(mb.stratego.build.strincr.task.output.CompileOutput) ModuleIdentifier(mb.stratego.build.strincr.ModuleIdentifier) CompileInput(mb.stratego.build.strincr.task.input.CompileInput) MixedSession(mb.pie.api.MixedSession) Strj(org.metaborg.spoofax.meta.core.pluto.build.Strj) HashSet(java.util.HashSet) Path(java.nio.file.Path) ResourcePath(mb.resource.hierarchical.ResourcePath) FSPath(mb.resource.fs.FSPath) TopDownSession(mb.pie.api.TopDownSession) TypeMessage(mb.stratego.build.strincr.message.type.TypeMessage) ExecException(mb.pie.api.ExecException) Arguments(org.metaborg.util.cmd.Arguments) IOException(java.io.IOException) Pie(mb.pie.api.Pie) ResourceKey(mb.resource.ResourceKey) FSPath(mb.resource.fs.FSPath) File(java.io.File)

Aggregations

Origin (build.pluto.dependency.Origin)1 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ExecException (mb.pie.api.ExecException)1 MixedSession (mb.pie.api.MixedSession)1 Pie (mb.pie.api.Pie)1 Supplier (mb.pie.api.Supplier)1 TopDownSession (mb.pie.api.TopDownSession)1 ResourceKey (mb.resource.ResourceKey)1 FSPath (mb.resource.fs.FSPath)1 ResourcePath (mb.resource.hierarchical.ResourcePath)1 ModuleIdentifier (mb.stratego.build.strincr.ModuleIdentifier)1 Message (mb.stratego.build.strincr.message.Message)1 TypeMessage (mb.stratego.build.strincr.message.type.TypeMessage)1 CompileInput (mb.stratego.build.strincr.task.input.CompileInput)1