Skip to content

Commit

Permalink
Added target property for Python version
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardalee committed Dec 7, 2024
1 parent a9b6d78 commit 23b5922
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 41 deletions.
10 changes: 9 additions & 1 deletion core/src/main/java/org/lflang/generator/c/CCmakeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class CCmakeGenerator {

private final FileConfig fileConfig;
private final List<String> additionalSources;
private final SetUpMainTarget setUpMainTarget;
private SetUpMainTarget setUpMainTarget;
private final String installCode;

public CCmakeGenerator(FileConfig fileConfig, List<String> additionalSources) {
Expand All @@ -90,6 +90,14 @@ public CCmakeGenerator(
this.installCode = installCode;
}

/**
* Set the code generator for the CMake main target.
* @param setUpMainTarget
*/
public void setCmakeGenerator(SetUpMainTarget setUpMainTarget) {
this.setUpMainTarget = setUpMainTarget;
}

/**
* Generate the contents of a CMakeLists.txt that builds the provided LF C 'sources'. Any error
* will be reported in the 'errorReporter'.
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public class CGenerator extends GeneratorBase {

private final CTypes types;

private final CCmakeGenerator cmakeGenerator;
protected CCmakeGenerator cmakeGenerator;

protected CGenerator(
LFGeneratorContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.lflang.target.Target;
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;

Expand All @@ -82,7 +83,7 @@
*
* @author Soroush Bateni
*/
public class PythonGenerator extends CGenerator {
public class PythonGenerator extends CGenerator implements CCmakeGenerator.SetUpMainTarget {

// Used to add statements that come before reactor classes and user code
private final CodeBuilder pythonPreamble = new CodeBuilder();
Expand All @@ -107,8 +108,9 @@ public PythonGenerator(LFGeneratorContext context) {
"lib/python_tag.c",
"lib/python_time.c",
"lib/pythontarget.c"),
PythonGenerator::setUpMainTarget,
null, // Temporarily, because can't pass this.
generateCmakeInstall(context.getFileConfig())));
cmakeGenerator.setCmakeGenerator(this);
}

private PythonGenerator(
Expand Down Expand Up @@ -645,12 +647,16 @@ protected void additionalPostProcessingForModes() {
PythonModeGenerator.generateResetReactionsIfNeeded(reactors);
}

private static String setUpMainTarget(
public String getCmakeCode(
boolean hasMain, String executableName, Stream<String> cSources) {
// According to https://cmake.org/cmake/help/latest/module/FindPython.html#hints, the following
// should work to select the version of Python given in your virtual environment.
// However, this does not work for me (macOS Sequoia 15.0.1).
// FIXME: Define a target parameter to specify the exact Python version.
// As a consequence, the python-version target property can be used to specify the exact Python version.
var pythonVersion = "3.10.0"; // Allows 3.10 or later. Change to "3.10.0...<3.11.0" to require 3.10 by default.
if (targetConfig.isSet(PythonVersionProperty.INSTANCE)) {
pythonVersion = targetConfig.get(PythonVersionProperty.INSTANCE) + " EXACT";
}
return ("""
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_compile_definitions(_PYTHON_TARGET_ENABLED)
Expand All @@ -660,7 +666,7 @@ private static String setUpMainTarget(
set(Python_FIND_VIRTUALENV FIRST)
set(Python_FIND_STRATEGY LOCATION)
set(Python_FIND_FRAMEWORK LAST)
find_package(Python 3.10.0 REQUIRED COMPONENTS Interpreter Development)
find_package(Python <pyVersion> REQUIRED COMPONENTS Interpreter Development)
Python_add_library(
${LF_MAIN_TARGET}
MODULE
Expand All @@ -680,7 +686,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(executableName))
.replace("<pyVersion>", pythonVersion);
// The use of fileConfig.name will break federated execution, but that's fine
}

Expand Down
35 changes: 2 additions & 33 deletions core/src/main/java/org/lflang/target/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,7 @@
import java.util.Set;
import net.jcip.annotations.Immutable;
import org.lflang.lf.TargetDecl;
import org.lflang.target.property.AuthProperty;
import org.lflang.target.property.BuildCommandsProperty;
import org.lflang.target.property.BuildTypeProperty;
import org.lflang.target.property.CargoDependenciesProperty;
import org.lflang.target.property.CargoFeaturesProperty;
import org.lflang.target.property.ClockSyncModeProperty;
import org.lflang.target.property.ClockSyncOptionsProperty;
import org.lflang.target.property.CmakeIncludeProperty;
import org.lflang.target.property.CompileDefinitionsProperty;
import org.lflang.target.property.CompilerProperty;
import org.lflang.target.property.CoordinationOptionsProperty;
import org.lflang.target.property.CoordinationProperty;
import org.lflang.target.property.DockerProperty;
import org.lflang.target.property.ExportDependencyGraphProperty;
import org.lflang.target.property.ExternalRuntimePathProperty;
import org.lflang.target.property.FilesProperty;
import org.lflang.target.property.KeepaliveProperty;
import org.lflang.target.property.NoRuntimeValidationProperty;
import org.lflang.target.property.NoSourceMappingProperty;
import org.lflang.target.property.PlatformProperty;
import org.lflang.target.property.PrintStatisticsProperty;
import org.lflang.target.property.ProtobufsProperty;
import org.lflang.target.property.Ros2DependenciesProperty;
import org.lflang.target.property.Ros2Property;
import org.lflang.target.property.RuntimeVersionProperty;
import org.lflang.target.property.RustIncludeProperty;
import org.lflang.target.property.SchedulerProperty;
import org.lflang.target.property.SingleFileProjectProperty;
import org.lflang.target.property.SingleThreadedProperty;
import org.lflang.target.property.TracePluginProperty;
import org.lflang.target.property.TracingProperty;
import org.lflang.target.property.VerifyProperty;
import org.lflang.target.property.WorkersProperty;
import org.lflang.target.property.*;

/**
* Enumeration of targets and their associated properties.
Expand Down Expand Up @@ -634,6 +602,7 @@ public void initialize(TargetConfig config) {
KeepaliveProperty.INSTANCE,
NoSourceMappingProperty.INSTANCE,
ProtobufsProperty.INSTANCE,
PythonVersionProperty.INSTANCE,
SchedulerProperty.INSTANCE,
SingleThreadedProperty.INSTANCE,
TracingProperty.INSTANCE,
Expand Down

0 comments on commit 23b5922

Please sign in to comment.