Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate default values for the user-configurable CMake flags #2036

Merged
merged 5 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions core/src/main/java/org/lflang/generator/c/CCmakeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,23 @@ CodeBuilder generateCMakeCode(
"set(CMAKE_SYSTEM_NAME " + targetConfig.platformOptions.platform.getcMakeName() + ")");
}
cMakeCode.newLine();
cMakeCode.pr("# Set default values for build parameters\n");
targetConfig.compileDefinitions.forEach(
(key, value) -> {
if (key.equals("LF_THREADED") || key.equals("LF_UNTHREADED")) {
cMakeCode.pr("if (NOT DEFINED LF_THREADED AND NOT DEFINED LF_UNTHREADED)\n");
} else {
cMakeCode.pr("if (NOT DEFINED " + key + ")\n");
}
cMakeCode.indent();
var v = "TRUE";
if (value != null && !value.isEmpty()) {
v = value;
}
cMakeCode.pr("set(" + key + " " + v + ")\n");
cMakeCode.unindent();
cMakeCode.pr("endif()\n");
});

// Setup main target for different platforms
switch (targetConfig.platformOptions.platform) {
Expand Down
11 changes: 0 additions & 11 deletions core/src/main/java/org/lflang/generator/c/CCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.lflang.FileConfig;
import org.lflang.MessageReporter;
import org.lflang.TargetConfig;
Expand Down Expand Up @@ -216,14 +215,8 @@ public LFCommand compileCmakeCommand() {
return command;
}

static Stream<String> cmakeCompileDefinitions(TargetConfig targetConfig) {
return targetConfig.compileDefinitions.entrySet().stream()
.map(entry -> "-D" + entry.getKey() + "=" + entry.getValue());
}

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.
Expand Down Expand Up @@ -403,10 +396,6 @@ public LFCommand compileCCommand(String fileToCompile, boolean noBinary) {
compileArgs.add(FileUtil.toUnixString(relativePath));
}

// Add compile definitions
targetConfig.compileDefinitions.forEach(
(key, value) -> compileArgs.add("-D" + key + "=" + value));

// Finally, add the compiler flags in target parameters (if any)
compileArgs.addAll(targetConfig.compilerFlags);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.lflang.generator.c;

import java.util.stream.Collectors;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.lflang.Target;
import org.lflang.generator.DockerGenerator;
Expand Down Expand Up @@ -68,10 +67,7 @@ protected String generateDefaultCompileCommand() {
"\n",
"RUN set -ex && \\",
"mkdir bin && \\",
"cmake "
+ CCompiler.cmakeCompileDefinitions(context.getTargetConfig())
.collect(Collectors.joining(" "))
+ " -S src-gen -B bin && \\",
"cmake -S src-gen -B bin && \\",
"cd bin && \\",
"make all");
}
Expand Down
39 changes: 0 additions & 39 deletions core/src/main/java/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.google.common.collect.Iterables;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -496,44 +495,6 @@ public void doGenerate(Resource resource, LFGeneratorContext context) {
return;
}

// Dump the additional compile definitions to a file to keep the generated project
// self-contained. In this way, third-party build tools like PlatformIO, west, arduino-cli can
// take over and do the rest of compilation.
try {
String compileDefs =
targetConfig.compileDefinitions.keySet().stream()
.map(key -> key + "=" + targetConfig.compileDefinitions.get(key))
.collect(Collectors.joining("\n"))
+ "\n";
FileUtil.writeToFile(
compileDefs,
Path.of(fileConfig.getSrcGenPath() + File.separator + "CompileDefinitions.txt"));
} catch (IOException e) {
Exceptions.sneakyThrow(e);
}

// Create a .vscode/settings.json file in the target directory so that VSCode can
// immediately compile the generated code.
try {
String compileDefs =
targetConfig.compileDefinitions.keySet().stream()
.map(key -> "\"-D" + key + "=" + targetConfig.compileDefinitions.get(key) + "\"")
.collect(Collectors.joining(",\n"));
String settings = "{\n" + "\"cmake.configureArgs\": [\n" + compileDefs + "\n]\n}\n";
Path vscodePath = fileConfig.getSrcGenPath().resolve(".vscode");
if (!Files.exists(vscodePath)) Files.createDirectory(vscodePath);
FileUtil.writeToFile(
settings,
Path.of(
fileConfig.getSrcGenPath()
+ File.separator
+ ".vscode"
+ File.separator
+ "settings.json"));
} catch (IOException e) {
Exceptions.sneakyThrow(e);
}

// 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.noCompile
Expand Down
Loading