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

New target property for specifying Python version #2332

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
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 @@ -60,8 +60,10 @@
import org.lflang.lf.Reaction;
import org.lflang.lf.Reactor;
import org.lflang.target.Target;
import org.lflang.target.TargetConfig;
import org.lflang.target.property.DockerProperty;
import org.lflang.target.property.ProtobufsProperty;
import org.lflang.target.property.PythonVersionProperty;
import org.lflang.util.FileUtil;
import org.lflang.util.LFCommand;
import org.lflang.util.StringUtil;
Expand Down Expand Up @@ -565,15 +567,20 @@ protected void additionalPostProcessingForModes() {
PythonModeGenerator.generateResetReactionsIfNeeded(reactors);
}

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)
add_subdirectory(core)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
set(LF_MAIN_TARGET <pyModuleName>)
find_package(Python 3.10.0...<3.11.0 REQUIRED COMPONENTS Interpreter Development)
find_package(Python <pyVersion> REQUIRED COMPONENTS Interpreter Development)
Python_add_library(
${LF_MAIN_TARGET}
MODULE
Expand All @@ -593,7 +600,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("<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
4 changes: 3 additions & 1 deletion core/src/main/java/org/lflang/target/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.lflang.target.property.PlatformProperty;
import org.lflang.target.property.PrintStatisticsProperty;
import org.lflang.target.property.ProtobufsProperty;
import org.lflang.target.property.PythonVersionProperty;
import org.lflang.target.property.Ros2DependenciesProperty;
import org.lflang.target.property.Ros2Property;
import org.lflang.target.property.RuntimeVersionProperty;
Expand Down Expand Up @@ -640,7 +641,8 @@ public void initialize(TargetConfig config) {
SingleThreadedProperty.INSTANCE,
TracingProperty.INSTANCE,
TracePluginProperty.INSTANCE,
WorkersProperty.INSTANCE);
WorkersProperty.INSTANCE,
PythonVersionProperty.INSTANCE);
case Rust ->
config.register(
BuildTypeProperty.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.lflang.target.property;

import org.lflang.MessageReporter;
import org.lflang.lf.LfPackage.Literals;
import org.lflang.target.TargetConfig;

/** Directive for specifying a specific version of the reactor runtime library. */
public final class PythonVersionProperty extends StringProperty {
jackyk02 marked this conversation as resolved.
Show resolved Hide resolved

/** Singleton target property instance. */
public static final PythonVersionProperty INSTANCE = new PythonVersionProperty();

private PythonVersionProperty() {
super();
}

@Override
public String name() {
return "python-version";
}

@Override
public void validate(TargetConfig config, MessageReporter reporter) {
String version = config.get(PythonVersionProperty.INSTANCE);
if (!version.isEmpty() && !version.contains("3.10")) {
reporter
.at(config.lookup(this), Literals.KEY_VALUE_PAIR__NAME)
.warning(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cmnrd: note that we issue a warning if anything other than 3.10 is selected.

"Python "
+ version
+ " is currently unsupported and untested. As such, it may fail in unexpected"
+ " ways.");
}
}
}
Loading