Search in sources :

Example 6 with FileConfig

use of org.lflang.FileConfig in project lingua-franca by lf-lang.

the class PythonGenerator method doGenerate.

/**
 * Generate C code from the Lingua Franca model contained by the
 *  specified resource. This is the main entry point for code
 *  generation.
 *  @param resource The resource containing the source code.
 *  @param context Context relating to invocation of the code generator.
 */
@Override
public void doGenerate(Resource resource, LFGeneratorContext context) {
    // If there are federates, assign the number of threads in the CGenerator to 1
    if (isFederated) {
        targetConfig.threads = 1;
    }
    // Prevent the CGenerator from compiling the C code.
    // The PythonGenerator will compiler it.
    boolean compileStatus = targetConfig.noCompile;
    targetConfig.noCompile = true;
    // Force disable the CMake because
    targetConfig.useCmake = false;
    // it interferes with the Python target functionality
    int cGeneratedPercentProgress = (IntegratedBuilder.VALIDATED_PERCENT_PROGRESS + 100) / 2;
    super.doGenerate(resource, new SubContext(context, IntegratedBuilder.VALIDATED_PERCENT_PROGRESS, cGeneratedPercentProgress));
    SubContext compilingFederatesContext = new SubContext(context, cGeneratedPercentProgress, 100);
    targetConfig.noCompile = compileStatus;
    if (errorsOccurred()) {
        context.unsuccessfulFinish();
        return;
    }
    String baseFileName = topLevelName;
    // Keep a separate file config for each federate
    FileConfig oldFileConfig = fileConfig;
    var federateCount = 0;
    Map<Path, CodeMap> codeMaps = new HashMap<>();
    for (FederateInstance federate : federates) {
        federateCount++;
        if (isFederated) {
            topLevelName = baseFileName + '_' + federate.name;
            try {
                fileConfig = new FedFileConfig(fileConfig, federate.name);
            } catch (IOException e) {
                throw Exceptions.sneakyThrow(e);
            }
        }
        // Don't generate code if there is no main reactor
        if (this.main != null) {
            try {
                Map<Path, CodeMap> codeMapsForFederate = generatePythonFiles(federate);
                codeMaps.putAll(codeMapsForFederate);
                PyUtil.copyTargetFiles(fileConfig);
                if (!targetConfig.noCompile) {
                    compilingFederatesContext.reportProgress(String.format("Validating %d/%d sets of generated files...", federateCount, federates.size()), 100 * federateCount / federates.size());
                    // If there are no federates, compile and install the generated code
                    new PythonValidator(fileConfig, errorReporter, codeMaps, protoNames).doValidate(context);
                    if (!errorsOccurred() && !Objects.equal(context.getMode(), LFGeneratorContext.Mode.LSP_MEDIUM)) {
                        compilingFederatesContext.reportProgress(String.format("Validation complete. Compiling and installing %d/%d Python modules...", federateCount, federates.size()), 100 * federateCount / federates.size());
                        // Why is this invoked here if the current federate is not a parameter?
                        pythonCompileCode(context);
                    }
                } else {
                    System.out.println(PythonInfoGenerator.generateSetupInfo(fileConfig));
                }
            } catch (Exception e) {
                throw Exceptions.sneakyThrow(e);
            }
            if (!isFederated) {
                System.out.println(PythonInfoGenerator.generateRunInfo(fileConfig, topLevelName));
            }
        }
        fileConfig = oldFileConfig;
    }
    if (isFederated) {
        System.out.println(PythonInfoGenerator.generateFedRunInfo(fileConfig));
    }
    // Restore filename
    topLevelName = baseFileName;
    if (errorReporter.getErrorsOccurred()) {
        context.unsuccessfulFinish();
    } else if (!isFederated) {
        context.finish(GeneratorResult.Status.COMPILED, topLevelName + ".py", fileConfig.getSrcGenPath(), fileConfig, codeMaps, "python3");
    } else {
        context.finish(GeneratorResult.Status.COMPILED, fileConfig.name, fileConfig.binPath, fileConfig, codeMaps, "bash");
    }
}
Also used : Path(java.nio.file.Path) FedFileConfig(org.lflang.federated.FedFileConfig) FileConfig(org.lflang.FileConfig) HashMap(java.util.HashMap) IOException(java.io.IOException) IOException(java.io.IOException) CodeMap(org.lflang.generator.CodeMap) SubContext(org.lflang.generator.SubContext) FederateInstance(org.lflang.federated.FederateInstance) FedFileConfig(org.lflang.federated.FedFileConfig)

Aggregations

FileConfig (org.lflang.FileConfig)6 IOException (java.io.IOException)5 Path (java.nio.file.Path)4 List (java.util.List)3 CancelIndicator (org.eclipse.xtext.util.CancelIndicator)3 ErrorReporter (org.lflang.ErrorReporter)3 Inject (com.google.inject.Inject)2 Provider (com.google.inject.Provider)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Properties (java.util.Properties)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 URI (org.eclipse.emf.common.util.URI)2 EObject (org.eclipse.emf.ecore.EObject)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)2 Severity (org.eclipse.xtext.diagnostics.Severity)2