From 5340db48a12cc3c17a664417318dddc0fda55cba Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Wed, 14 Feb 2024 22:11:36 -0800 Subject: [PATCH 01/10] Cleanup of build logic in CGenerator --- .../org/lflang/generator/c/CGenerator.java | 78 +++++++------------ 1 file changed, 26 insertions(+), 52 deletions(-) diff --git a/core/src/main/java/org/lflang/generator/c/CGenerator.java b/core/src/main/java/org/lflang/generator/c/CGenerator.java index a40eb0abde..0501e0dec0 100644 --- a/core/src/main/java/org/lflang/generator/c/CGenerator.java +++ b/core/src/main/java/org/lflang/generator/c/CGenerator.java @@ -519,54 +519,14 @@ public void doGenerate(Resource resource, LFGeneratorContext context) { return; } - // If this code generator is directly compiling the code, compile it now so that we - // clean it up after, removing the #line directives after errors have been reported. - if (!targetConfig.get(NoCompileProperty.INSTANCE) - && !targetConfig.get(DockerProperty.INSTANCE).enabled() - && IterableExtensions.isNullOrEmpty(targetConfig.get(BuildCommandsProperty.INSTANCE)) - // This code is unreachable in LSP_FAST mode, so that check is omitted. - && context.getMode() != LFGeneratorContext.Mode.LSP_MEDIUM) { - // FIXME: Currently, a lack of main is treated as a request to not produce - // a binary and produce a .o file instead. There should be a way to control - // this. - // Create an anonymous Runnable class and add it to the compileThreadPool - // so that compilation can happen in parallel. - var cleanCode = code.removeLines("#line"); - - var execName = lfModuleName; - var threadFileConfig = fileConfig; - var generator = - this; // FIXME: currently only passed to report errors with line numbers in the Eclipse - // IDE - var CppMode = cppMode; - // generatingContext.reportProgress( - // String.format("Generated code for %d/%d executables. Compiling...", federateCount, - // federates.size()), - // 100 * federateCount / federates.size() - // ); // FIXME: Move to FedGenerator - // Create the compiler to be used later - - var cCompiler = new CCompiler(targetConfig, threadFileConfig, messageReporter, CppMode); - try { - if (!cCompiler.runCCompiler(generator, context)) { - // If compilation failed, remove any bin files that may have been created. - CUtil.deleteBinFiles(threadFileConfig); - // If finish has already been called, it is illegal and makes no sense. However, - // if finish has already been called, then this must be a federated execution. - context.unsuccessfulFinish(); - } else { - context.finish(GeneratorResult.Status.COMPILED, null); - } - cleanCode.writeToFile(targetFile); - } catch (IOException e) { - Exceptions.sneakyThrow(e); - } - } + var customBuildCommands = targetConfig.get(BuildCommandsProperty.INSTANCE); + var dockerBuild = targetConfig.get(DockerProperty.INSTANCE); + - // If a build directive has been given, invoke it now. - // Note that the code does not get cleaned in this case. - if (!targetConfig.get(NoCompileProperty.INSTANCE)) { - if (!IterableExtensions.isNullOrEmpty(targetConfig.get(BuildCommandsProperty.INSTANCE))) { + if (targetConfig.get(NoCompileProperty.INSTANCE)) { + context.finish(GeneratorResult.GENERATED_NO_EXECUTABLE.apply(context, null)); + } else if (context.getMode() != LFGeneratorContext.Mode.LSP_MEDIUM) { + if (customBuildCommands != null && !customBuildCommands.isEmpty()) { CUtil.runBuildCommand( fileConfig, targetConfig, @@ -575,12 +535,26 @@ public void doGenerate(Resource resource, LFGeneratorContext context) { this::reportCommandErrors, context.getMode()); context.finish(GeneratorResult.Status.COMPILED, null); + } else if (dockerBuild.enabled()) { + // FIXME: build using Docker + } else { + var cleanCode = code.removeLines("#line"); + var cCompiler = new CCompiler(targetConfig, fileConfig, messageReporter, cppMode); + try { + if (!cCompiler.runCCompiler(this, context)) { + // If compilation failed, remove any bin files that may have been created. + CUtil.deleteBinFiles(fileConfig); + // If finish has already been called, it is illegal and makes no sense. However, + // if finish has already been called, then this must be a federated execution. + context.unsuccessfulFinish(); + } else { + context.finish(GeneratorResult.Status.COMPILED, null); + } + cleanCode.writeToFile(targetFile); + } catch (IOException e) { + messageReporter.nowhere().warning("Generated code may still contain line directives."); + } } - if (!errorsOccurred()) { - messageReporter.nowhere().info("Compiled binary is in " + fileConfig.binPath); - } - } else { - context.finish(GeneratorResult.GENERATED_NO_EXECUTABLE.apply(context, null)); } // In case we are in Eclipse, make sure the generated code is visible. From cea53686de3811e3dbe394db4460c9ee0ab06b6c Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Wed, 14 Feb 2024 22:23:03 -0800 Subject: [PATCH 02/10] Remove strange comment --- core/src/main/java/org/lflang/generator/c/CGenerator.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/src/main/java/org/lflang/generator/c/CGenerator.java b/core/src/main/java/org/lflang/generator/c/CGenerator.java index 0501e0dec0..295e3d0551 100644 --- a/core/src/main/java/org/lflang/generator/c/CGenerator.java +++ b/core/src/main/java/org/lflang/generator/c/CGenerator.java @@ -49,7 +49,6 @@ import java.util.stream.Stream; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.xtext.xbase.lib.Exceptions; -import org.eclipse.xtext.xbase.lib.IterableExtensions; import org.lflang.FileConfig; import org.lflang.ast.ASTUtils; import org.lflang.ast.DelayedConnectionTransformation; @@ -522,7 +521,6 @@ public void doGenerate(Resource resource, LFGeneratorContext context) { var customBuildCommands = targetConfig.get(BuildCommandsProperty.INSTANCE); var dockerBuild = targetConfig.get(DockerProperty.INSTANCE); - if (targetConfig.get(NoCompileProperty.INSTANCE)) { context.finish(GeneratorResult.GENERATED_NO_EXECUTABLE.apply(context, null)); } else if (context.getMode() != LFGeneratorContext.Mode.LSP_MEDIUM) { @@ -544,8 +542,6 @@ public void doGenerate(Resource resource, LFGeneratorContext context) { if (!cCompiler.runCCompiler(this, context)) { // If compilation failed, remove any bin files that may have been created. CUtil.deleteBinFiles(fileConfig); - // If finish has already been called, it is illegal and makes no sense. However, - // if finish has already been called, then this must be a federated execution. context.unsuccessfulFinish(); } else { context.finish(GeneratorResult.Status.COMPILED, null); From 3fbce5a746edfaf701de25e0a39ebec28e2020fc Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Wed, 14 Feb 2024 22:36:21 -0800 Subject: [PATCH 03/10] Only generate launch script once --- .../main/java/org/lflang/federated/generator/FedGenerator.java | 3 +-- 1 file changed, 1 insertion(+), 2 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 0acf495fc7..bb6355462d 100644 --- a/core/src/main/java/org/lflang/federated/generator/FedGenerator.java +++ b/core/src/main/java/org/lflang/federated/generator/FedGenerator.java @@ -184,7 +184,6 @@ public boolean doGenerate(Resource resource, LFGeneratorContext context) throws lf2lfCodeMapMap, subContexts -> { createDockerFiles(context, subContexts); - generateLaunchScript(); // If an error has occurred during codegen of any federate, report it. subContexts.forEach( c -> { @@ -196,7 +195,7 @@ public boolean doGenerate(Resource resource, LFGeneratorContext context) throws } }); }); - + generateLaunchScript(); context.finish(Status.COMPILED, codeMapMap); return false; } From 250f9f5fed8f9bce63f2d476c9b5a890023dd9da Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Thu, 15 Feb 2024 00:51:29 -0800 Subject: [PATCH 04/10] Generate docker compose script for unfederated programs that use docker --- .../federated/generator/FedGenerator.java | 1 + .../org/lflang/generator/c/CGenerator.java | 29 ++++---- .../docker/DockerComposeGenerator.java | 66 ++++++++++++------- 3 files changed, 61 insertions(+), 35 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 bb6355462d..ddb81dd06d 100644 --- a/core/src/main/java/org/lflang/federated/generator/FedGenerator.java +++ b/core/src/main/java/org/lflang/federated/generator/FedGenerator.java @@ -196,6 +196,7 @@ public boolean doGenerate(Resource resource, LFGeneratorContext context) throws }); }); generateLaunchScript(); + // FIXME: build if Docker context.finish(Status.COMPILED, codeMapMap); return false; } diff --git a/core/src/main/java/org/lflang/generator/c/CGenerator.java b/core/src/main/java/org/lflang/generator/c/CGenerator.java index 295e3d0551..ad0821181f 100644 --- a/core/src/main/java/org/lflang/generator/c/CGenerator.java +++ b/core/src/main/java/org/lflang/generator/c/CGenerator.java @@ -444,17 +444,6 @@ public void doGenerate(Resource resource, LFGeneratorContext context) { CompileDefinitionsProperty.INSTANCE.update( targetConfig, Map.of("NUMBER_OF_WATCHDOGS", String.valueOf(nWatchdogs))); - // Create docker file. - if (targetConfig.get(DockerProperty.INSTANCE).enabled() && mainDef != null) { - try { - var dockerData = getDockerGenerator(context).generateDockerData(); - dockerData.writeDockerFile(); - (new DockerComposeGenerator(context)).writeDockerComposeFile(List.of(dockerData)); - } catch (IOException e) { - throw new RuntimeException("Error while writing Docker files", e); - } - } - var isArduino = targetConfig.getOrDefault(PlatformProperty.INSTANCE).platform() == Platform.ARDUINO; @@ -534,7 +523,7 @@ public void doGenerate(Resource resource, LFGeneratorContext context) { context.getMode()); context.finish(GeneratorResult.Status.COMPILED, null); } else if (dockerBuild.enabled()) { - // FIXME: build using Docker + buildUsingDocker(); } else { var cleanCode = code.removeLines("#line"); var cCompiler = new CCompiler(targetConfig, fileConfig, messageReporter, cppMode); @@ -557,6 +546,22 @@ public void doGenerate(Resource resource, LFGeneratorContext context) { GeneratorUtils.refreshProject(resource, context.getMode()); } + private void buildUsingDocker() { + // Create docker file. + var dockerCompose = new DockerComposeGenerator(context); + var dockerData = getDockerGenerator(context).generateDockerData(); + try { + dockerData.writeDockerFile(); + dockerCompose.writeDockerComposeFile(List.of(dockerData)); + } catch (IOException e) { + throw new RuntimeException("Error while writing Docker files", e); + } + dockerCompose.build(); + if (mainDef != null) { + dockerCompose.createLauncher(); + } + } + private void generateCodeFor(String lfModuleName) throws IOException { code.pr(generateDirectives()); code.pr(new CMainFunctionGenerator(targetConfig).generateCode()); diff --git a/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java b/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java index 72528d0b89..cb895e5444 100644 --- a/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java +++ b/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java @@ -1,11 +1,14 @@ package org.lflang.generator.docker; +import java.io.BufferedWriter; +import java.io.FileWriter; import java.io.IOException; -import java.nio.file.Path; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; import org.lflang.generator.LFGeneratorContext; import org.lflang.util.FileUtil; +import org.lflang.util.LFCommand; /** * Code generator for docker-compose configurations. @@ -15,14 +18,10 @@ */ public class DockerComposeGenerator { - /** Path to the docker-compose.yml file. */ - protected final Path path; - /** Context of the code generator. */ protected final LFGeneratorContext context; public DockerComposeGenerator(LFGeneratorContext context) { - this.path = context.getFileConfig().getSrcGenPath().resolve("docker-compose.yml"); this.context = context; } @@ -52,22 +51,7 @@ protected String generateDockerServices(List services) { %s """ .formatted( - services.stream() - .map(data -> getServiceDescription(data)) - .collect(Collectors.joining("\n"))); - } - - /** Return the command to build and run using the docker-compose configuration. */ - public String getUsageInstructions() { - return """ - ##################################### - To build and run: - pushd %s && docker compose up --build - To return to the current working directory afterwards: - popd - ##################################### - """ - .formatted(path.getParent()); + services.stream().map(this::getServiceDescription).collect(Collectors.joining("\n"))); } /** Turn given docker data into a string. */ @@ -116,7 +100,43 @@ public void writeDockerComposeFile(List services, String networkName var contents = String.join( "\n", this.generateDockerServices(services), this.generateDockerNetwork(networkName)); - FileUtil.writeToFile(contents, path); - context.getErrorReporter().nowhere().info(getUsageInstructions()); + FileUtil.writeToFile( + contents, context.getFileConfig().getSrcGenPath().resolve("docker-compose.yml")); + } + + public void build() { + Objects.requireNonNull( + LFCommand.get( + "docker", + List.of("compose", "build"), + false, + context.getFileConfig().getSrcGenPath())) + .run(); + } + + public void createLauncher() { + var fileConfig = context.getFileConfig(); + var packageRoot = fileConfig.srcPkgPath; + var srcGenPath = fileConfig.getSrcGenPath(); + var file = fileConfig.binPath.resolve(fileConfig.name).toFile(); + var script = + """ + #!/bin/sh + cd %s + docker compose up + """ + .formatted(packageRoot.resolve(packageRoot.relativize(srcGenPath))); + var messageReporter = context.getErrorReporter(); + try { + var writer = new BufferedWriter(new FileWriter(file)); + writer.write(script); + writer.close(); + } catch (IOException e) { + messageReporter.nowhere().warning("Unable to write launcher to: " + file.getAbsolutePath()); + } + + if (!file.setExecutable(true, false)) { + messageReporter.nowhere().warning("Unable to make launcher script executable."); + } } } From 0b2baa7b7f11d45280fbce5389945b4a46121334 Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Wed, 6 Mar 2024 16:58:19 -0800 Subject: [PATCH 05/10] Use relative path in launcher for dockerized app --- .../lflang/generator/docker/DockerComposeGenerator.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java b/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java index cb895e5444..f2bc3dd8d1 100644 --- a/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java +++ b/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java @@ -122,10 +122,13 @@ public void createLauncher() { var script = """ #!/bin/sh - cd %s + set -euo pipefail + cd $(dirname "$0") + cd .. + cd "%s" docker compose up """ - .formatted(packageRoot.resolve(packageRoot.relativize(srcGenPath))); + .formatted(packageRoot.relativize(srcGenPath)); var messageReporter = context.getErrorReporter(); try { var writer = new BufferedWriter(new FileWriter(file)); From c2c96de2210aff4cd7cd9bacbeb37a3505fe1aa6 Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Wed, 6 Mar 2024 20:43:07 -0800 Subject: [PATCH 06/10] Build dockerized federations using Docker --- .../federated/generator/FedGenerator.java | 44 +++++++++++++------ .../docker/DockerComposeGenerator.java | 22 ++++++---- .../generator/docker/RtiDockerGenerator.java | 2 +- 3 files changed, 46 insertions(+), 22 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 ddb81dd06d..445b1dacfc 100644 --- a/core/src/main/java/org/lflang/federated/generator/FedGenerator.java +++ b/core/src/main/java/org/lflang/federated/generator/FedGenerator.java @@ -178,12 +178,13 @@ public boolean doGenerate(Resource resource, LFGeneratorContext context) throws // If the RTI is to be built locally, set up a build environment for it. prepareRtiBuildEnvironment(context); + var useDocker = context.getTargetConfig().get(DockerProperty.INSTANCE).enabled(); + Map codeMapMap = compileFederates( context, lf2lfCodeMapMap, subContexts -> { - createDockerFiles(context, subContexts); // If an error has occurred during codegen of any federate, report it. subContexts.forEach( c -> { @@ -194,13 +195,35 @@ public boolean doGenerate(Resource resource, LFGeneratorContext context) throws .error("Failure during code generation of " + c.getFileConfig().srcFile); } }); + if (useDocker) { + buildUsingDocker(context, subContexts); + } else { + generateLaunchScript(); + } }); - generateLaunchScript(); - // FIXME: build if Docker + context.finish(Status.COMPILED, codeMapMap); return false; } + /** + * Create Dockerfiles and docker-compose.yml, build, and create a launcher. + * + * @param context The main generator context. + * @param subContexts The context for the federates. + */ + private void buildUsingDocker(LFGeneratorContext context, List subContexts) { + try { + var dockerGen = new FedDockerComposeGenerator(context, rtiConfig.getHost()); + dockerGen.writeDockerComposeFile(createDockerFiles(context, subContexts)); + if (dockerGen.build()) { + dockerGen.createLauncher(); + } + } catch (IOException e) { + context.getErrorReporter().nowhere().error("Unsuccessful Docker build."); + } + } + /** * Prepare a build environment for the rti alongside the generated sources of the federates. * @@ -235,12 +258,12 @@ private void generateLaunchScript() { * @param context The main context in which the federation has been compiled. * @param subContexts The subcontexts in which the federates have been compiled. */ - private void createDockerFiles(LFGeneratorContext context, List subContexts) { - if (!context.getTargetConfig().get(DockerProperty.INSTANCE).enabled()) return; + private List createDockerFiles( + LFGeneratorContext context, List subContexts) { final List services = new ArrayList<>(); // 1. create a Dockerfile for each federate - for (SubContext subContext : subContexts) { // Inherit Docker options from main context - + for (SubContext subContext : subContexts) { + // Inherit Docker options from main context DockerProperty.INSTANCE.override( subContext.getTargetConfig(), context.getTargetConfig().get(DockerProperty.INSTANCE)); var dockerGenerator = dockerGeneratorFactory(subContext); @@ -252,12 +275,7 @@ private void createDockerFiles(LFGeneratorContext context, List subC } services.add(dockerData); } - // 2. create a docker-compose.yml for the federation - try { - new FedDockerComposeGenerator(context, rtiConfig.getHost()).writeDockerComposeFile(services); - } catch (IOException e) { - throw new RuntimeIOException(e); - } + return services; } /** diff --git a/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java b/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java index f2bc3dd8d1..ea891f3394 100644 --- a/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java +++ b/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java @@ -104,14 +104,20 @@ public void writeDockerComposeFile(List services, String networkName contents, context.getFileConfig().getSrcGenPath().resolve("docker-compose.yml")); } - public void build() { - Objects.requireNonNull( - LFCommand.get( - "docker", - List.of("compose", "build"), - false, - context.getFileConfig().getSrcGenPath())) - .run(); + /** + * Build using docker compose. + * + * @return {@code true} if successful,{@code false} otherwise. + */ + public boolean build() { + return Objects.requireNonNull( + LFCommand.get( + "docker", + List.of("compose", "build"), + false, + context.getFileConfig().getSrcGenPath())) + .run() + == 0; } public void createLauncher() { diff --git a/core/src/main/java/org/lflang/generator/docker/RtiDockerGenerator.java b/core/src/main/java/org/lflang/generator/docker/RtiDockerGenerator.java index ba285da991..65e75710b7 100644 --- a/core/src/main/java/org/lflang/generator/docker/RtiDockerGenerator.java +++ b/core/src/main/java/org/lflang/generator/docker/RtiDockerGenerator.java @@ -22,7 +22,7 @@ protected String generateDockerFileContent() { COPY include /reactor-c/include WORKDIR /reactor-c/core/federated/RTI %s - RUN mkdir build && \\ + RUN mkdir -p build && \\ cd build && \\ cmake ../ && \\ make && \\ From 35f4fd894915b8b8bef5075c9ee00af906ba3791 Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Wed, 6 Mar 2024 20:50:17 -0800 Subject: [PATCH 07/10] Mostly comments --- core/src/main/java/org/lflang/generator/c/CGenerator.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/lflang/generator/c/CGenerator.java b/core/src/main/java/org/lflang/generator/c/CGenerator.java index ad0821181f..afd2ce53da 100644 --- a/core/src/main/java/org/lflang/generator/c/CGenerator.java +++ b/core/src/main/java/org/lflang/generator/c/CGenerator.java @@ -546,6 +546,7 @@ public void doGenerate(Resource resource, LFGeneratorContext context) { GeneratorUtils.refreshProject(resource, context.getMode()); } + /** Create Dockerfiles and docker-compose.yml, build, and create a launcher. */ private void buildUsingDocker() { // Create docker file. var dockerCompose = new DockerComposeGenerator(context); @@ -556,8 +557,8 @@ private void buildUsingDocker() { } catch (IOException e) { throw new RuntimeException("Error while writing Docker files", e); } - dockerCompose.build(); - if (mainDef != null) { + var success = dockerCompose.build(); + if (success && mainDef != null) { dockerCompose.createLauncher(); } } From cb55ee009eec9560ac77f1300426526b1b4b8873 Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Wed, 6 Mar 2024 20:56:11 -0800 Subject: [PATCH 08/10] Add missing comment --- .../java/org/lflang/generator/docker/DockerComposeGenerator.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java b/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java index ea891f3394..063665868a 100644 --- a/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java +++ b/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java @@ -120,6 +120,7 @@ public boolean build() { == 0; } + /** Create a launcher script that invokes Docker. */ public void createLauncher() { var fileConfig = context.getFileConfig(); var packageRoot = fileConfig.srcPkgPath; From dd853415a35ef4cc28a76022ff657d3127c49035 Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Wed, 6 Mar 2024 22:53:10 -0800 Subject: [PATCH 09/10] Update core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java Co-authored-by: Peter Donovan <33707478+petervdonovan@users.noreply.github.com> --- .../org/lflang/generator/docker/DockerComposeGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java b/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java index 063665868a..52212d5cf1 100644 --- a/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java +++ b/core/src/main/java/org/lflang/generator/docker/DockerComposeGenerator.java @@ -128,7 +128,7 @@ public void createLauncher() { var file = fileConfig.binPath.resolve(fileConfig.name).toFile(); var script = """ - #!/bin/sh + #!/bin/bash set -euo pipefail cd $(dirname "$0") cd .. From 8b5e98ac3da1706e92a78f939a36edc30b9d43f4 Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Wed, 6 Mar 2024 23:19:28 -0800 Subject: [PATCH 10/10] Address feedback from code review --- .../org/lflang/generator/c/CGenerator.java | 18 ++++++++++++++---- .../generator/docker/RtiDockerGenerator.java | 3 ++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/lflang/generator/c/CGenerator.java b/core/src/main/java/org/lflang/generator/c/CGenerator.java index afd2ce53da..c1357b8f9a 100644 --- a/core/src/main/java/org/lflang/generator/c/CGenerator.java +++ b/core/src/main/java/org/lflang/generator/c/CGenerator.java @@ -527,17 +527,27 @@ public void doGenerate(Resource resource, LFGeneratorContext context) { } else { var cleanCode = code.removeLines("#line"); var cCompiler = new CCompiler(targetConfig, fileConfig, messageReporter, cppMode); + var success = false; try { - if (!cCompiler.runCCompiler(this, context)) { + success = cCompiler.runCCompiler(this, context); + } catch (IOException e) { + messageReporter.nowhere().error("Unexpected error during compilation."); + } finally { + if (!success) { // If compilation failed, remove any bin files that may have been created. + messageReporter.nowhere().error("Compilation was unsuccessful."); CUtil.deleteBinFiles(fileConfig); context.unsuccessfulFinish(); } else { + try { + cleanCode.writeToFile(targetFile); + } catch (IOException e) { + messageReporter + .nowhere() + .warning("Generated code may still contain line directives."); + } context.finish(GeneratorResult.Status.COMPILED, null); } - cleanCode.writeToFile(targetFile); - } catch (IOException e) { - messageReporter.nowhere().warning("Generated code may still contain line directives."); } } } diff --git a/core/src/main/java/org/lflang/generator/docker/RtiDockerGenerator.java b/core/src/main/java/org/lflang/generator/docker/RtiDockerGenerator.java index 65e75710b7..fb863eed4c 100644 --- a/core/src/main/java/org/lflang/generator/docker/RtiDockerGenerator.java +++ b/core/src/main/java/org/lflang/generator/docker/RtiDockerGenerator.java @@ -22,7 +22,8 @@ protected String generateDockerFileContent() { COPY include /reactor-c/include WORKDIR /reactor-c/core/federated/RTI %s - RUN mkdir -p build && \\ + RUN rm -rf build && \\ + mkdir build && \\ cd build && \\ cmake ../ && \\ make && \\