From dd3aaef5c1a8b43a10257d3f5b67d9f303991623 Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Sun, 29 Sep 2024 13:45:42 -0700 Subject: [PATCH 1/4] Prevent NPE and provide informative message about federated support --- .../lflang/federated/generator/FedGenerator.java | 13 +++++++++++-- .../lflang/target/property/PlatformProperty.java | 9 ++++++++- .../lflang/target/property/type/PlatformType.java | 8 ++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/lflang/federated/generator/FedGenerator.java b/core/src/main/java/org/lflang/federated/generator/FedGenerator.java index e1f36a9ad1..0d1d29a3cc 100644 --- a/core/src/main/java/org/lflang/federated/generator/FedGenerator.java +++ b/core/src/main/java/org/lflang/federated/generator/FedGenerator.java @@ -1,6 +1,7 @@ package org.lflang.federated.generator; import static org.lflang.generator.docker.DockerGenerator.dockerGeneratorFactory; +import static org.lflang.target.property.type.PlatformType.Platform.supportsFederated; import com.google.inject.Injector; import java.io.IOException; @@ -63,6 +64,7 @@ import org.lflang.target.property.DockerProperty.DockerOptions; import org.lflang.target.property.KeepaliveProperty; import org.lflang.target.property.NoCompileProperty; +import org.lflang.target.property.PlatformProperty; import org.lflang.target.property.type.CoordinationModeType.CoordinationMode; import org.lflang.util.Averager; import org.lflang.util.FileUtil; @@ -121,7 +123,7 @@ public FedGenerator(LFGeneratorContext context) { * @return False if no errors have occurred, true otherwise. */ public boolean doGenerate(Resource resource, LFGeneratorContext context) throws IOException { - if (!federatedExecutionIsSupported(resource)) return true; + if (!federatedExecutionIsSupported(resource, context)) return true; cleanIfNeeded(context); // In a federated execution, we need keepalive to be true, @@ -300,7 +302,7 @@ private void cleanIfNeeded(LFGeneratorContext context) { } /** Return whether federated execution is supported for {@code resource}. */ - private boolean federatedExecutionIsSupported(Resource resource) { + private boolean federatedExecutionIsSupported(Resource resource, LFGeneratorContext context) { TargetDecl targetDecl = GeneratorUtils.findTargetDecl(resource); var target = Target.fromDecl(targetDecl); var targetOK = @@ -316,6 +318,13 @@ private boolean federatedExecutionIsSupported(Resource resource) { .error("Federated LF programs with a C target are currently not supported on Windows."); targetOK = false; } + var platform = context.getTargetConfig().get(PlatformProperty.INSTANCE).platform(); + if (!supportsFederated(platform)) { + messageReporter + .at(targetDecl) + .error("Federations are not supported by the " + platform.getcMakeName() + " platform."); + targetOK = false; + } return targetOK; } diff --git a/core/src/main/java/org/lflang/target/property/PlatformProperty.java b/core/src/main/java/org/lflang/target/property/PlatformProperty.java index 12399319f7..9051a7c74a 100644 --- a/core/src/main/java/org/lflang/target/property/PlatformProperty.java +++ b/core/src/main/java/org/lflang/target/property/PlatformProperty.java @@ -2,6 +2,7 @@ import org.lflang.MessageReporter; import org.lflang.ast.ASTUtils; +import org.lflang.federated.generator.FederateTargetConfig; import org.lflang.lf.Element; import org.lflang.lf.KeyValuePair; import org.lflang.lf.KeyValuePairs; @@ -163,10 +164,16 @@ private void validateFlexPRET(TargetConfig config, MessageReporter reporter) { } } + private void federatedNotSupported(TargetConfig config, MessageReporter reporter) { + if (config instanceof FederateTargetConfig) { + reporter.nowhere().error("This platform does not support federations."); + } + } + private void validateZephyr(TargetConfig config, MessageReporter reporter) { var platform = config.get(PlatformProperty.INSTANCE); var singleThreaded = config.get(SingleThreadedProperty.INSTANCE); - + federatedNotSupported(config, reporter); if (singleThreaded) { if (platform.userThreads().value() > 0) { reporter diff --git a/core/src/main/java/org/lflang/target/property/type/PlatformType.java b/core/src/main/java/org/lflang/target/property/type/PlatformType.java index f861753bf8..4eb1103f30 100644 --- a/core/src/main/java/org/lflang/target/property/type/PlatformType.java +++ b/core/src/main/java/org/lflang/target/property/type/PlatformType.java @@ -53,5 +53,13 @@ public boolean isMultiThreaded() { public Platform getDefault() { return Platform.AUTO; } + + /** Return {@code true} if the given platform supports federated. */ + public static boolean supportsFederated(Platform platform) { + return switch (platform) { + case AUTO, LINUX, MAC -> true; + default -> false; + }; + } } } From 1406621978dcbb94fae3d0f696c66805340e9304 Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Sun, 29 Sep 2024 13:50:13 -0700 Subject: [PATCH 2/4] Dead code removal --- .../target/property/PlatformProperty.java | 8 ------ .../target/property/type/PlatformType.java | 26 +++++++------------ 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/org/lflang/target/property/PlatformProperty.java b/core/src/main/java/org/lflang/target/property/PlatformProperty.java index 9051a7c74a..ecd6574b8b 100644 --- a/core/src/main/java/org/lflang/target/property/PlatformProperty.java +++ b/core/src/main/java/org/lflang/target/property/PlatformProperty.java @@ -2,7 +2,6 @@ import org.lflang.MessageReporter; import org.lflang.ast.ASTUtils; -import org.lflang.federated.generator.FederateTargetConfig; import org.lflang.lf.Element; import org.lflang.lf.KeyValuePair; import org.lflang.lf.KeyValuePairs; @@ -164,16 +163,9 @@ private void validateFlexPRET(TargetConfig config, MessageReporter reporter) { } } - private void federatedNotSupported(TargetConfig config, MessageReporter reporter) { - if (config instanceof FederateTargetConfig) { - reporter.nowhere().error("This platform does not support federations."); - } - } - private void validateZephyr(TargetConfig config, MessageReporter reporter) { var platform = config.get(PlatformProperty.INSTANCE); var singleThreaded = config.get(SingleThreadedProperty.INSTANCE); - federatedNotSupported(config, reporter); if (singleThreaded) { if (platform.userThreads().value() > 0) { reporter diff --git a/core/src/main/java/org/lflang/target/property/type/PlatformType.java b/core/src/main/java/org/lflang/target/property/type/PlatformType.java index 4eb1103f30..4bfc726f9f 100644 --- a/core/src/main/java/org/lflang/target/property/type/PlatformType.java +++ b/core/src/main/java/org/lflang/target/property/type/PlatformType.java @@ -12,27 +12,23 @@ protected Class enumClass() { public enum Platform { AUTO, - ARDUINO, // FIXME: not multithreaded - NRF52("nRF52", false), - RP2040("Rp2040", true), - LINUX("Linux", true), - MAC("Darwin", true), - ZEPHYR("Zephyr", true), - FLEXPRET("FlexPRET", true), - WINDOWS("Windows", true); + ARDUINO, + NRF52("nRF52"), + RP2040("Rp2040"), + LINUX("Linux"), + MAC("Darwin"), + ZEPHYR("Zephyr"), + FLEXPRET("FlexPRET"), + WINDOWS("Windows"); final String cMakeName; - private final boolean multiThreaded; - Platform() { this.cMakeName = this.toString(); - this.multiThreaded = true; } - Platform(String cMakeName, boolean isMultiThreaded) { + Platform(String cMakeName) { this.cMakeName = cMakeName; - this.multiThreaded = isMultiThreaded; } /** Return the name in lower case. */ @@ -46,10 +42,6 @@ public String getcMakeName() { return this.cMakeName; } - public boolean isMultiThreaded() { - return this.multiThreaded; - } - public Platform getDefault() { return Platform.AUTO; } From 4fc3f5f21024ed044e7b403a0bb84ddf99b66446 Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Sun, 29 Sep 2024 14:37:08 -0700 Subject: [PATCH 3/4] Reduce scope of check --- .../lflang/federated/generator/FedGenerator.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/lflang/federated/generator/FedGenerator.java b/core/src/main/java/org/lflang/federated/generator/FedGenerator.java index 0d1d29a3cc..dd94a4354d 100644 --- a/core/src/main/java/org/lflang/federated/generator/FedGenerator.java +++ b/core/src/main/java/org/lflang/federated/generator/FedGenerator.java @@ -318,12 +318,15 @@ private boolean federatedExecutionIsSupported(Resource resource, LFGeneratorCont .error("Federated LF programs with a C target are currently not supported on Windows."); targetOK = false; } - var platform = context.getTargetConfig().get(PlatformProperty.INSTANCE).platform(); - if (!supportsFederated(platform)) { - messageReporter - .at(targetDecl) - .error("Federations are not supported by the " + platform.getcMakeName() + " platform."); - targetOK = false; + if (target.equals(Target.C) || target.equals(Target.CCPP)) { + // Currently, only the C runtime has a platform abstraction. + var platform = context.getTargetConfig().get(PlatformProperty.INSTANCE).platform(); + if (!supportsFederated(platform)) { + messageReporter + .at(targetDecl) + .error("Federations are not supported by the " + platform.getcMakeName() + " platform."); + targetOK = false; + } } return targetOK; From 805f95bf4b6a2d69bba8a9c32e928d29b84a2772 Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Sun, 29 Sep 2024 15:05:07 -0700 Subject: [PATCH 4/4] Formatting --- .../main/java/org/lflang/federated/generator/FedGenerator.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/lflang/federated/generator/FedGenerator.java b/core/src/main/java/org/lflang/federated/generator/FedGenerator.java index dd94a4354d..94df476594 100644 --- a/core/src/main/java/org/lflang/federated/generator/FedGenerator.java +++ b/core/src/main/java/org/lflang/federated/generator/FedGenerator.java @@ -324,7 +324,8 @@ private boolean federatedExecutionIsSupported(Resource resource, LFGeneratorCont if (!supportsFederated(platform)) { messageReporter .at(targetDecl) - .error("Federations are not supported by the " + platform.getcMakeName() + " platform."); + .error( + "Federations are not supported by the " + platform.getcMakeName() + " platform."); targetOK = false; } }