Skip to content

Commit

Permalink
Merge pull request #2327 from lf-lang/files-property-docker-runtime-i…
Browse files Browse the repository at this point in the history
…mage

Make files available in Docker runner image
  • Loading branch information
petervdonovan committed Jun 21, 2024
2 parents 66d7d59 + f8692c6 commit 3a6cc1b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.lflang.target.property.BuildCommandsProperty;
import org.lflang.target.property.DockerProperty;
import org.lflang.target.property.DockerProperty.DockerOptions;
import org.lflang.target.property.FilesProperty;
import org.lflang.util.StringUtil;

/**
Expand Down Expand Up @@ -49,6 +50,7 @@ protected String generateDockerFileContent() {
"WORKDIR /lingua-franca",
"RUN mkdir scripts",
generateCopyOfScript(),
generateCopyOfUserFiles(),
generateRunForMakingExecutableDir(),
generateCopyOfExecutable(),
generateEntryPoint(),
Expand Down Expand Up @@ -150,6 +152,30 @@ protected String generateCopyOfScript() {
return "# (No pre-run script provided.)";
}

/**
* Return zero or more COPY commands to copy files specified using the {@code files} target
* property from the builder to the runner.
*/
protected String generateCopyOfUserFiles() {
if (!context.getTargetConfig().isSupported(FilesProperty.INSTANCE)) {
return "";
}
var files = context.getTargetConfig().get(FilesProperty.INSTANCE);
if (files == null) {
return "# (No user-specified files to be copied.)";
}
var ret = new StringBuilder();
for (var file : files) {
var p = Path.of(file);
var name = p.getFileName().toString();
ret.append(
String.format(
"COPY --from=builder \"lingua-franca/%s/src-gen/%s\" \"./%s\"",
context.getFileConfig().name, name, name));
}
return ret.toString();
}

/**
* Return a list of strings used to construct and entrypoint. If this is done for a federate, then
* also include additional parameters to pass in the federation ID.
Expand Down
39 changes: 39 additions & 0 deletions test/C/src/docker/RuntimeFilesPropertyContainerized.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
target C {
files: "./RuntimeFilesPropertyContainerized.lf",
docker: true
}

preamble {=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
=}

main reactor {
reaction(startup) {=
FILE *f = fopen("RuntimeFilesPropertyContainerized.lf", "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);

char *string = (char*) malloc(fsize + 1);
fread(string, fsize, 1, f);
fclose(f);

string[fsize] = 0;

printf("file contents:\n%s\n", string);

// fail if the file contents are not the contents of this file
char* expected = "target C {\n files: \"./RuntimeFilesPropertyContainerized.lf\",\n docker: true\n}";
string[strlen(expected)] = 0;
if (strcmp(string, expected) != 0) {
printf("file contents do not match expected contents\n");
exit(1);
} else {
printf("file contents match expected contents\n");
}

free(string);
=}
}

0 comments on commit 3a6cc1b

Please sign in to comment.