Skip to content

Commit

Permalink
Remove static variable
Browse files Browse the repository at this point in the history
  • Loading branch information
lhstrh committed Aug 4, 2024
1 parent a352794 commit d14ae87
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
34 changes: 13 additions & 21 deletions core/src/main/java/org/lflang/generator/c/CCmakeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,30 +310,22 @@ CodeBuilder generateCMakeCode(
case ZEPHYR:
cMakeCode.pr(
setUpMainTargetZephyr(
hasMain,
executableName,
Stream.concat(additionalSources.stream(), sources.stream())));
hasMain, context, Stream.concat(additionalSources.stream(), sources.stream())));
break;
case RP2040:
cMakeCode.pr(
setUpMainTargetRp2040(
hasMain,
executableName,
Stream.concat(additionalSources.stream(), sources.stream())));
hasMain, context, Stream.concat(additionalSources.stream(), sources.stream())));
break;
case FLEXPRET:
cMakeCode.pr(
setUpMainTargetFlexPRET(
hasMain,
executableName,
Stream.concat(additionalSources.stream(), sources.stream())));
hasMain, context, Stream.concat(additionalSources.stream(), sources.stream())));
break;
default:
cMakeCode.pr(
setUpMainTarget.getCmakeCode(
hasMain,
executableName,
Stream.concat(additionalSources.stream(), sources.stream())));
hasMain, context, Stream.concat(additionalSources.stream(), sources.stream())));
}

// Ensure that the math library is linked
Expand Down Expand Up @@ -495,16 +487,16 @@ CodeBuilder generateCMakeCode(
public interface SetUpMainTarget {
// Implementation note: This indirection is necessary because the Python
// target produces a shared object file, not an executable.
String getCmakeCode(boolean hasMain, String executableName, Stream<String> cSources);
String getCmakeCode(boolean hasMain, LFGeneratorContext context, Stream<String> cSources);
}

/** Generate the C-target-specific code for configuring the executable produced by the build. */
private static String setUpMainTarget(
boolean hasMain, String executableName, Stream<String> cSources) {
boolean hasMain, LFGeneratorContext context, Stream<String> cSources) {
var code = new CodeBuilder();
code.pr("add_subdirectory(core)");
code.newLine();
code.pr("set(LF_MAIN_TARGET " + executableName + ")");
code.pr("set(LF_MAIN_TARGET " + context.getFileConfig().name + ")");
code.newLine();

if (hasMain) {
Expand All @@ -524,7 +516,7 @@ private static String setUpMainTarget(
}

private static String setUpMainTargetZephyr(
boolean hasMain, String executableName, Stream<String> cSources) {
boolean hasMain, LFGeneratorContext context, Stream<String> cSources) {
var code = new CodeBuilder();
code.pr("add_subdirectory(core)");
code.newLine();
Expand All @@ -535,7 +527,7 @@ private static String setUpMainTargetZephyr(
code.pr("target_sources(");
} else {
code.pr("# Declare a new library target and list all its sources");
code.pr("set(LF_MAIN_TARGET" + executableName + ")");
code.pr("set(LF_MAIN_TARGET" + context.getFileConfig().name + ")");
code.pr("add_library(");
}
code.indent();
Expand All @@ -553,7 +545,7 @@ private static String setUpMainTargetZephyr(
}

private static String setUpMainTargetRp2040(
boolean hasMain, String executableName, Stream<String> cSources) {
boolean hasMain, LFGeneratorContext context, Stream<String> cSources) {
var code = new CodeBuilder();
// initialize sdk
code.pr("pico_sdk_init()");
Expand All @@ -563,7 +555,7 @@ private static String setUpMainTargetRp2040(
code.pr("target_link_libraries(reactor-c PUBLIC pico_multicore)");
code.pr("target_link_libraries(reactor-c PUBLIC pico_sync)");
code.newLine();
code.pr("set(LF_MAIN_TARGET " + executableName + ")");
code.pr("set(LF_MAIN_TARGET " + context.getFileConfig().name + ")");

if (hasMain) {
code.pr("# Declare a new executable target and list all its sources");
Expand All @@ -584,7 +576,7 @@ private static String setUpMainTargetRp2040(
}

private static String setUpMainTargetFlexPRET(
boolean hasMain, String executableName, Stream<String> cSources) {
boolean hasMain, LFGeneratorContext context, Stream<String> cSources) {
var code = new CodeBuilder();
code.pr("add_subdirectory(core)");
code.newLine();
Expand All @@ -593,7 +585,7 @@ private static String setUpMainTargetFlexPRET(
code.pr("add_subdirectory($ENV{FP_SDK_PATH} BINARY_DIR)");
code.newLine();

code.pr("set(LF_MAIN_TARGET " + executableName + ")");
code.pr("set(LF_MAIN_TARGET " + context.getFileConfig().name + ")");
code.newLine();

if (hasMain) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Files;setUpPythonVersion();

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -61,6 +62,7 @@
import org.lflang.lf.Reactor;
import org.lflang.target.Target;
import org.lflang.target.property.DockerProperty;
import org.lflang.target.TargetConfig;
import org.lflang.target.property.ProtobufsProperty;
import org.lflang.target.property.PythonVersionProperty;
import org.lflang.util.FileUtil;
Expand Down Expand Up @@ -90,7 +92,6 @@ public class PythonGenerator extends CGenerator {
private final List<String> pythonRequiredModules = new ArrayList<>();

private final PythonTypes types;
private static String pythonVersion;

public PythonGenerator(LFGeneratorContext context) {
this(
Expand Down Expand Up @@ -364,7 +365,6 @@ public boolean isOSCompatible() {
*/
@Override
public void doGenerate(Resource resource, LFGeneratorContext context) {
setUpPythonVersion();
int cGeneratedPercentProgress = (IntegratedBuilder.VALIDATED_PERCENT_PROGRESS + 100) / 2;
code.pr(
PythonPreambleGenerator.generateCIncludeStatements(
Expand Down Expand Up @@ -568,14 +568,13 @@ protected void additionalPostProcessingForModes() {
PythonModeGenerator.generateResetReactionsIfNeeded(reactors);
}

/** Initializes the Python version based on the user's configuration. */
protected void setUpPythonVersion() {
String property = targetConfig.get(PythonVersionProperty.INSTANCE);
pythonVersion = property.isEmpty() ? "3.10.0...<3.11.0" : property + " EXACT";
private static String getPythonVersion(TargetConfig config) {
String property = config.get(PythonVersionProperty.INSTANCE);
return property.isEmpty() ? "3.10.0...<3.11.0" : property + " EXACT";
}

private static String setUpMainTarget(
boolean hasMain, String executableName, Stream<String> cSources) {
boolean hasMain, LFGeneratorContext context, Stream<String> cSources) {
return ("""
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_compile_definitions(_PYTHON_TARGET_ENABLED)
Expand All @@ -602,8 +601,8 @@ private static String setUpMainTarget(
target_link_libraries(${LF_MAIN_TARGET} PRIVATE ${Python_LIBRARIES})
target_compile_definitions(${LF_MAIN_TARGET} PUBLIC MODULE_NAME=<pyModuleName>)
""")
.replace("<pyModuleName>", generatePythonModuleName(executableName))
.replace("<pyVersion>", pythonVersion);
.replace("<pyModuleName>", generatePythonModuleName(context.getFileConfig().name))
.replace("<pyVersion>", getPythonVersion(context.getTargetConfig()));
// The use of fileConfig.name will break federated execution, but that's fine
}

Expand Down

0 comments on commit d14ae87

Please sign in to comment.