Skip to content

Commit

Permalink
Generate preambles only once in Python file
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardalee committed Dec 7, 2024
1 parent afd045c commit 252a2ae
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public class PythonGenerator extends CGenerator {
// Used to add module requirements to setup.py (delimited with ,)
private final List<String> pythonRequiredModules = new ArrayList<>();

/** Indicator that we have already generated top-level preambles. */
private Set<Model> generatedTopLevelPreambles = new HashSet<Model>();

private final PythonTypes types;

public PythonGenerator(LFGeneratorContext context) {
Expand Down Expand Up @@ -271,7 +274,12 @@ protected String generateTopLevelPreambles(Reactor ignored) {
models.add((Model) ASTUtils.toDefinition(this.mainDef.getReactorClass()).eContainer());
}
for (Model m : models) {
pythonPreamble.pr(PythonPreambleGenerator.generatePythonPreambles(m.getPreambles()));
// In the generated Python code, unlike C, all reactors go into the same file.
// Therefore, we do not need to generate this if it has already been generated.
if (!generatedTopLevelPreambles.contains(m)) {
generatedTopLevelPreambles.add(m);
pythonPreamble.pr(PythonPreambleGenerator.generatePythonPreambles(m.getPreambles()));
}
}
return PythonPreambleGenerator.generateCIncludeStatements(
targetConfig, targetLanguageIsCpp(), hasModalReactors);
Expand Down

0 comments on commit 252a2ae

Please sign in to comment.