Skip to content

Commit

Permalink
Merge branch 'master' into nrf52
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardalee committed Jul 2, 2024
2 parents f56059b + 2b6ebad commit 9b8188e
Show file tree
Hide file tree
Showing 57 changed files with 1,133 additions and 659 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/lsp-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ jobs:
- name: Uninstall packages Ubuntu
run: sudo apt-get remove clang-*
if: ${{ runner.os == 'Linux' }}
- name: Uninstall packages MacOS
run: brew uninstall --ignore-dependencies gcc
if: ${{ runner.os == 'macOS' }}
- name: Uninstall packages Windows
shell: pwsh
run: |
Expand Down Expand Up @@ -68,7 +65,7 @@ jobs:
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: "3.10"
- name: Run language server Python tests without PyLint
run: ./gradlew core:integrationTest --tests org.lflang.tests.lsp.LspTests.pythonValidationTestSyntaxOnly core:integrationTestCodeCoverageReport
- name: Install pylint
Expand Down
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
implementation "com.fasterxml.jackson.core:jackson-core:$fasterxmlVersion"
implementation "com.fasterxml.jackson.core:jackson-annotations:$fasterxmlVersion"
implementation "com.fasterxml.jackson.core:jackson-databind:$fasterxmlVersion"
implementation "org.apache.commons:commons-text:$commonsTextVersion"

implementation ("de.cau.cs.kieler.klighd:de.cau.cs.kieler.klighd.lsp:$klighdVersion") {
exclude group: 'org.eclipse.platform', module: 'org.eclipse.swt.*'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ protected boolean supportsEnclaves() {
return true;
}

@Override
protected boolean supportsDockerOption() {
return true;
}

@Test
@Override
public void runBasicTests() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.lflang.generator.TargetTypes;
import org.lflang.generator.TimerInstance;
import org.lflang.generator.TriggerInstance;
import org.lflang.generator.docker.DockerGenerator;
import org.lflang.lf.AttrParm;
import org.lflang.lf.Attribute;
import org.lflang.lf.Connection;
Expand Down Expand Up @@ -1744,4 +1745,9 @@ public TargetTypes getTargetTypes() {
throw new UnsupportedOperationException(
"This method is not applicable for this generator since Uclid5 is not an LF target.");
}

@Override
protected DockerGenerator getDockerGenerator(LFGeneratorContext context) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,7 @@ private void buildUsingDocker(LFGeneratorContext context, List<SubContext> subCo
try {
var dockerGen = new FedDockerComposeGenerator(context, rtiConfig.getHost());
dockerGen.writeDockerComposeFile(createDockerFiles(context, subContexts));
if (dockerGen.build()) {
dockerGen.createLauncher();
} else {
context.getErrorReporter().nowhere().error("Docker build failed.");
}
dockerGen.buildIfRequested();
} catch (IOException e) {
context
.getErrorReporter()
Expand Down Expand Up @@ -279,6 +275,7 @@ private List<DockerData> createDockerFiles(
var dockerData = dockerGenerator.generateDockerData();
try {
dockerData.writeDockerFile();
dockerData.copyScripts(context);
} catch (IOException e) {
throw new RuntimeIOException(e);
}
Expand Down Expand Up @@ -366,9 +363,7 @@ time allowed for the test expires (currently two hours).
new TargetConfig(
subFileConfig.resource, GeneratorArguments.none(), subContextMessageReporter);

if (targetConfig.get(DockerProperty.INSTANCE).enabled()
&& targetConfig.target.buildsUsingDocker()
|| fed.isRemote) {
if (targetConfig.get(DockerProperty.INSTANCE).enabled() || fed.isRemote) {
NoCompileProperty.INSTANCE.override(subConfig, true);
}
// Disabled Docker for the federate and put federation in charge.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
package org.lflang.federated.generator;

import static org.lflang.ast.ASTUtils.convertToEmptyListIfNull;

import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import org.eclipse.emf.ecore.resource.Resource;
import org.lflang.MessageReporter;
import org.lflang.ast.ASTUtils;
import org.lflang.generator.GeneratorUtils;
import org.lflang.generator.LFGeneratorContext;
import org.lflang.lf.KeyValuePair;
import org.lflang.target.Target;
import org.lflang.target.TargetConfig;
import org.lflang.target.property.FileListProperty;
import org.lflang.util.FileUtil;

/**
* Subclass of TargetConfig with a specialized constructor for creating configurations for
Expand Down Expand Up @@ -43,7 +33,7 @@ public FederateTargetConfig(LFGeneratorContext context, Resource federateResourc
load(federationResource, reporter);

// Load properties from the federate file
mergeImportedConfig(federateResource, federationResource, reporter);
mergeImportedConfig(federateResource, federationResource, p -> p.loadFromFederate(), reporter);

// Load properties from the generator context
load(context.getArgs(), reporter);
Expand All @@ -52,66 +42,4 @@ public FederateTargetConfig(LFGeneratorContext context, Resource federateResourc

this.validate(reporter);
}

/**
* If the federate that target configuration applies to is imported, merge target properties
* declared in the file that it was imported from.
*
* @param federateResource The resource where the class of the federate is specified.
* @param mainResource The resource in which the federation (i.e., main reactor) is specified.
* @param messageReporter An error reporter to use when problems are encountered.
*/
private void mergeImportedConfig(
Resource federateResource, Resource mainResource, MessageReporter messageReporter) {
// If the federate is imported, then update the configuration based on target properties
// in the imported file.
if (!federateResource.equals(mainResource)) {
var importedTargetDecl = GeneratorUtils.findTargetDecl(federateResource);
var targetProperties = importedTargetDecl.getConfig();
if (targetProperties != null) {
// Merge properties
update(
this,
convertToEmptyListIfNull(targetProperties.getPairs()),
getRelativePath(mainResource, federateResource),
messageReporter);
}
}
}

private Path getRelativePath(Resource source, Resource target) {
return FileUtil.toPath(source.getURI())
.getParent()
.relativize(FileUtil.toPath(target.getURI()).getParent());
}

/**
* Update the given configuration using the given target properties.
*
* @param config The configuration object to update.
* @param pairs AST node that holds all the target properties.
* @param relativePath The path from the main resource to the resource from which the new
* properties originate.
*/
public void update(
TargetConfig config, List<KeyValuePair> pairs, Path relativePath, MessageReporter err) {
pairs.forEach(
pair -> {
var p = config.forName(pair.getName());
if (p.isPresent()) {
var value = pair.getValue();
var property = p.get();
if (property instanceof FileListProperty fileListProperty) {
var files =
ASTUtils.elementToListOfStrings(value).stream()
.map(relativePath::resolve) // assume all paths are relative
.map(Objects::toString)
.toList();
fileListProperty.update(config, files);
} else {
p.get().update(this, pair, err);
}
}
});
}
}
96 changes: 55 additions & 41 deletions core/src/main/java/org/lflang/generator/GeneratorBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -51,6 +50,8 @@
import org.lflang.analyses.uclid.UclidGenerator;
import org.lflang.ast.ASTUtils;
import org.lflang.ast.AstTransformation;
import org.lflang.generator.docker.DockerComposeGenerator;
import org.lflang.generator.docker.DockerGenerator;
import org.lflang.graph.InstantiationGraph;
import org.lflang.lf.Attribute;
import org.lflang.lf.Connection;
Expand Down Expand Up @@ -78,18 +79,12 @@
*/
public abstract class GeneratorBase extends AbstractLFValidator {

////////////////////////////////////////////
//// Public fields.

/** The main (top-level) reactor instance. */
public ReactorInstance main;

/** An error reporter for reporting any errors or warnings during the code generation */
public MessageReporter messageReporter;

////////////////////////////////////////////
//// Protected fields.

/** The current target configuration. */
protected final TargetConfig targetConfig;

Expand Down Expand Up @@ -123,9 +118,6 @@ public Instantiation getMainDef() {
*/
protected List<Reactor> reactors = new ArrayList<>();

/** The set of resources referenced reactor classes reside in. */
protected Set<LFResource> resources = new LinkedHashSet<>(); // FIXME: Why do we need this?

/**
* Graph that tracks dependencies between instantiations. This is a graph where each node is a
* Reactor (not a ReactorInstance) and an arc from Reactor A to Reactor B means that B contains an
Expand All @@ -145,9 +137,6 @@ public Instantiation getMainDef() {
/** Indicates whether the program has any watchdogs. This is used to check for support. */
public boolean hasWatchdogs = false;

// //////////////////////////////////////////
// // Private fields.

/** A list ot AST transformations to apply before code generation */
private final List<AstTransformation> astTransformations = new ArrayList<>();

Expand All @@ -168,8 +157,26 @@ protected void registerTransformation(AstTransformation transformation) {
astTransformations.add(transformation);
}

// //////////////////////////////////////////
// // Code generation functions to override for a concrete code generator.
/**
* If the given reactor is defined in another file, process its target properties so that they are
* reflected in the target configuration.
*/
private void loadTargetProperties(Resource resource) {
var mainFileConfig = this.context.getFileConfig();
if (resource != mainFileConfig.resource) {
this.context
.getTargetConfig()
.mergeImportedConfig(
LFGenerator.createFileConfig(
resource,
mainFileConfig.getSrcGenBasePath(),
mainFileConfig.useHierarchicalBin)
.resource,
mainFileConfig.resource,
p -> p.loadFromImport(),
this.messageReporter);
}
}

/**
* Generate code from the Lingua Franca model contained by the specified resource.
Expand Down Expand Up @@ -216,46 +223,29 @@ public void doGenerate(Resource resource, LFGeneratorContext context) {
}
}

// Process target files. Copy each of them into the src-gen dir.
// FIXME: Should we do this here? This doesn't make sense for federates the way it is
// done here.
copyUserFiles(this.targetConfig, context.getFileConfig());

// Collect reactors and create an instantiation graph.
// These are needed to figure out which resources we need
// to validate, which happens in setResources().
setReactorsAndInstantiationGraph(context.getMode());

List<Resource> allResources = GeneratorUtils.getResources(reactors);
resources.addAll(
allResources
.stream() // FIXME: This filter reproduces the behavior of the method it replaces. But
// why must it be so complicated? Why are we worried about weird corner cases
// like this?
.filter(
it ->
!Objects.equal(it, context.getFileConfig().resource)
|| mainDef != null && it == mainDef.getReactorClass().eResource())
.map(
it ->
GeneratorUtils.getLFResource(
it, context.getFileConfig().getSrcGenBasePath(), context, messageReporter))
.toList());
Set<Resource> allResources = GeneratorUtils.getResources(reactors);

GeneratorUtils.accommodatePhysicalActionsIfPresent(
allResources,
getTarget().setsKeepAliveOptionAutomatically(),
targetConfig,
messageReporter);
// FIXME: Should the GeneratorBase pull in {@code files} from imported
// resources?

// Load target properties for all resources.
allResources.forEach(r -> loadTargetProperties(r));

for (AstTransformation transformation : astTransformations) {
transformation.applyTransformation(reactors);
}

// Transform connections that reside in mutually exclusive modes and are otherwise conflicting
// This should be done before creating the instantiation graph
transformConflictingConnectionsInModalReactors();
transformConflictingConnectionsInModalReactors(allResources);

// Invoke these functions a second time because transformations
// may have introduced new reactors!
Expand Down Expand Up @@ -424,9 +414,9 @@ protected void checkWatchdogSupport(boolean isSupported) {
* Finds and transforms connections into forwarding reactions iff the connections have the same
* destination as other connections or reaction in mutually exclusive modes.
*/
private void transformConflictingConnectionsInModalReactors() {
for (LFResource r : resources) {
var transform = ASTUtils.findConflictingConnectionsInModalReactors(r.eResource);
private void transformConflictingConnectionsInModalReactors(Set<Resource> resources) {
for (Resource r : resources) {
var transform = ASTUtils.findConflictingConnectionsInModalReactors(r);
if (!transform.isEmpty()) {
var factory = LfFactory.eINSTANCE;
for (Connection connection : transform) {
Expand Down Expand Up @@ -627,6 +617,30 @@ protected void cleanIfNeeded(LFGeneratorContext context) {
}
}

/** Return a {@code DockerGenerator} instance suitable for the target. */
protected abstract DockerGenerator getDockerGenerator(LFGeneratorContext context);

/** Create Dockerfiles and docker-compose.yml, build, and create a launcher. */
protected boolean buildUsingDocker() {
// Create docker file.
var dockerCompose = new DockerComposeGenerator(context);
var dockerData = getDockerGenerator(context).generateDockerData();
try {
dockerData.writeDockerFile();
dockerData.copyScripts(context);
dockerCompose.writeDockerComposeFile(List.of(dockerData));
} catch (IOException e) {
context
.getErrorReporter()
.nowhere()
.error(
"Error while writing Docker files: "
+ (e.getMessage() == null ? "No cause given" : e.getMessage()));
return false;
}
return dockerCompose.buildIfRequested();
}

/**
* Check if @property is used. If so, instantiate a UclidGenerator. The verification model needs
* to be generated before the target code since code generation changes LF program (desugar
Expand Down
Loading

0 comments on commit 9b8188e

Please sign in to comment.