Skip to content

Commit

Permalink
Merge pull request #1715 from lf-lang/file-access
Browse files Browse the repository at this point in the history
File access
  • Loading branch information
edwardalee authored Apr 28, 2023
2 parents bd3fe67 + 2b1b4c2 commit 922ed3f
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ public String generatePreamble(
includes.pr("#include \"core/federated/federate.h\"");
includes.pr("#include \"core/federated/net_common.h\"");
includes.pr("#include \"core/federated/net_util.h\"");
includes.pr("#include \"core/federated/clock-sync.h\"");
includes.pr("#include \"core/threaded/reactor_threaded.h\"");
includes.pr("#include \"core/utils/util.h\"");
includes.pr("extern federate_instance_t _fed;");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ public static void generateCMakeInclude(
CodeBuilder cmakeIncludeCode = new CodeBuilder();

cmakeIncludeCode.pr(generateSerializationCMakeExtension(federate));
cmakeIncludeCode.pr(
"add_compile_definitions(LF_SOURCE_DIRECTORY=\""
+ fileConfig.srcPath
+ "\")"
);

try (var srcWriter = Files.newBufferedWriter(cmakeIncludePath)) {
srcWriter.write(cmakeIncludeCode.getCode());
Expand Down
19 changes: 18 additions & 1 deletion org.lflang/src/org/lflang/generator/c/CCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package org.lflang.generator.c;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -222,6 +223,14 @@ static Stream<String> cmakeCompileDefinitions(TargetConfig targetConfig) {
private static List<String> cmakeOptions(TargetConfig targetConfig, FileConfig fileConfig) {
List<String> arguments = new ArrayList<>();
cmakeCompileDefinitions(targetConfig).forEachOrdered(arguments::add);
String separator = File.separator;
String maybeQuote = ""; // Windows seems to require extra level of quoting.
String srcPath = fileConfig.srcPath.toString(); // Windows requires escaping the backslashes.
if (separator.equals("\\")) {
separator = "\\\\\\\\";
maybeQuote = "\\\"";
srcPath = srcPath.replaceAll("\\\\", "\\\\\\\\");
}
arguments.addAll(List.of(
"-DCMAKE_BUILD_TYPE=" + ((targetConfig.cmakeBuildType!=null) ? targetConfig.cmakeBuildType.toString() : "Release"),
"-DCMAKE_INSTALL_PREFIX=" + FileUtil.toUnixString(fileConfig.getOutPath()),
Expand All @@ -230,8 +239,16 @@ private static List<String> cmakeOptions(TargetConfig targetConfig, FileConfig f
fileConfig.binPath
)
),
FileUtil.toUnixString(fileConfig.getSrcGenPath())
"-DLF_FILE_SEPARATOR=\"" + maybeQuote + separator + maybeQuote + "\""
));
// Add #define for source file directory.
// Do not do this for federated programs because for those, the definition is put
// into the cmake file (and fileConfig.srcPath is the wrong directory anyway).
if (!fileConfig.srcPath.toString().contains("fed-gen")) {
// Do not convert to Unix path
arguments.add("-DLF_SOURCE_DIRECTORY=\"" + maybeQuote + srcPath + maybeQuote + "\"");
}
arguments.add(FileUtil.toUnixString(fileConfig.getSrcGenPath()));

if (GeneratorUtils.isHostWindows()) {
arguments.add("-DCMAKE_SYSTEM_VERSION=\"10.0\"");
Expand Down
46 changes: 46 additions & 0 deletions test/C/src/FileReader.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/** Test reading a file at a location relative to the source file. */
target C

reactor Source {
output out: char* // Use char*, not string, so memory is freed.

reaction(startup) -> out {=
char* file_path =
LF_SOURCE_DIRECTORY
LF_FILE_SEPARATOR "lib"
LF_FILE_SEPARATOR "FileReader.txt";

FILE* file = fopen(file_path, "rb");
if (file == NULL) lf_print_error_and_exit("Error opening file at path %s.", file_path);

// Determine the file size
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);

// Allocate memory for the buffer
char* buffer = (char *) malloc(file_size + 1);
if (buffer == NULL) lf_print_error_and_exit("Out of memory.");

// Read the file into the buffer
fread(buffer, file_size, 1, file);
buffer[file_size] = '\0';
fclose(file);

lf_set(out, buffer);
=}
}

main reactor {
preamble {=
#include <string.h>
=}
s = new Source()

reaction(s.out) {=
printf("Received: %s\n", s.out->value);
if (strcmp("Hello World", s.out->value) != 0) {
lf_print_error_and_exit("Expected 'Hello World'");
}
=}
}
57 changes: 57 additions & 0 deletions test/C/src/federated/FederatedFileReader.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/** Test reading a file at a location relative to the source file. */
target C {
timeout: 0 s
}

reactor Source {
output out: char* // Use char*, not string, so memory is freed.

reaction(startup) -> out {=
char* file_path =
LF_SOURCE_DIRECTORY
LF_FILE_SEPARATOR ".."
LF_FILE_SEPARATOR "lib"
LF_FILE_SEPARATOR "FileReader.txt";

FILE* file = fopen(file_path, "rb");
if (file == NULL) lf_print_error_and_exit("Error opening file at path %s.", file_path);

// Determine the file size
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);

// Allocate memory for the buffer
char* buffer = (char *) malloc(file_size + 1);
if (buffer == NULL) lf_print_error_and_exit("Out of memory.");

// Read the file into the buffer
fread(buffer, file_size, 1, file);
buffer[file_size] = '\0';
fclose(file);

// For federated version, have to use lf_set_array so array size is know
// to the serializer.
lf_set_array(out, buffer, file_size + 1);
=}
}

reactor Check {
preamble {=
#include <string.h>
=}
input in: char*

reaction(in) {=
printf("Received: %s\n", in->value);
if (strcmp("Hello World", in->value) != 0) {
lf_print_error_and_exit("Expected 'Hello World'");
}
=}
}

federated reactor {
s = new Source()
c = new Check()
s.out -> c.in
}
1 change: 1 addition & 0 deletions test/C/src/lib/FileReader.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
2 changes: 1 addition & 1 deletion util/RunZephyrTests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ num_failures=0
failed_tests=""

# Skip
skip=()
skip=("FileReader")

find_kconfig_folders() {
if [ -f "$folder/CMakeLists.txt" ]; then
Expand Down

0 comments on commit 922ed3f

Please sign in to comment.