Skip to content

Commit

Permalink
Merge pull request #1914 from lf-lang/python-launch
Browse files Browse the repository at this point in the history
Python launch scripts
  • Loading branch information
cmnrd authored Jul 26, 2023
2 parents 0864dd0 + c226c66 commit 7dd7a94
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
public class RunSingleTest {

private static final Pattern TEST_FILE_PATTERN =
Pattern.compile("(test/(\\w+))/src/([^/]++/)*(\\w+.lf)");
Pattern.compile("(test\\W(\\w+))\\Wsrc\\W(\\w++\\W)*(\\w+.lf)");

@Test
public void runSingleTest() throws FileNotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,12 @@ public PyBuildConfig(
}

@Override
public String localExecuteCommand() {
return "python3 "
+ fileConfig.getSrcGenPath()
+ "/"
+ federate.name
+ "/"
+ federate.name
+ ".py -i $FEDERATION_ID";
public String remoteExecuteCommand() {
return "bin/" + fileConfig.name + "_" + federate.name + " -i '$FEDERATION_ID'";
}

@Override
public String remoteExecuteCommand() {
return "python3 src-gen/"
+ fileConfig.name
+ "/"
+ federate.name
+ "/"
+ fileConfig.name
+ "_"
+ federate.name
+ " -i '$FEDERATION_ID'";
public String localExecuteCommand() {
return fileConfig.getFedBinPath().resolve(federate.name) + " -i $FEDERATION_ID";
}
}
16 changes: 2 additions & 14 deletions core/src/main/java/org/lflang/generator/python/PyFileConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,18 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import org.eclipse.emf.ecore.resource.Resource;
import org.lflang.generator.GeneratorUtils;
import org.lflang.generator.c.CFileConfig;
import org.lflang.util.LFCommand;

public class PyFileConfig extends CFileConfig {
public PyFileConfig(Resource resource, Path srcGenBasePath, boolean useHierarchicalBin)
throws IOException {
super(resource, srcGenBasePath, useHierarchicalBin);
}

@Override
public LFCommand getCommand() {
return LFCommand.get(
"python3", List.of(srcPkgPath.relativize(getExecutable()).toString()), true, srcPkgPath);
}

@Override
public Path getExecutable() {
return srcGenPath.resolve(name + getExecutableExtension());
}

@Override
protected String getExecutableExtension() {
return ".py";
return GeneratorUtils.isHostWindows() ? ".bat" : "";
}
}
38 changes: 28 additions & 10 deletions core/src/main/java/org/lflang/generator/python/PythonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.lflang.AttributeUtils;
import org.lflang.FileConfig;
import org.lflang.Target;
import org.lflang.TargetProperty;
import org.lflang.ast.ASTUtils;
Expand Down Expand Up @@ -100,8 +101,7 @@ public PythonGenerator(LFGeneratorContext context) {
"lib/python_time.c",
"lib/pythontarget.c"),
PythonGenerator::setUpMainTarget,
"install(TARGETS)" // No-op
));
generateCmakeInstall(context.getFileConfig())));
}

private PythonGenerator(
Expand Down Expand Up @@ -396,15 +396,10 @@ public void doGenerate(Resource resource, LFGeneratorContext context) {
codeMaps.putAll(codeMapsForFederate);
copyTargetFiles();
new PythonValidator(fileConfig, messageReporter, codeMaps, protoNames).doValidate(context);
if (targetConfig.noCompile) {
messageReporter.nowhere().info(PythonInfoGenerator.generateSetupInfo(fileConfig));
}
} catch (Exception e) {
//noinspection ConstantConditions
throw Exceptions.sneakyThrow(e);
}

messageReporter.nowhere().info(PythonInfoGenerator.generateRunInfo(fileConfig, lfModuleName));
}

if (messageReporter.getErrorsOccurred()) {
Expand Down Expand Up @@ -578,7 +573,7 @@ private static String setUpMainTarget(
add_subdirectory(core)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
set(LF_MAIN_TARGET <pyModuleName>)
find_package(Python 3.7.0...<3.11.0 COMPONENTS Interpreter Development)
find_package(Python 3.10.0...<3.11.0 REQUIRED COMPONENTS Interpreter Development)
Python_add_library(
${LF_MAIN_TARGET}
MODULE
Expand All @@ -598,11 +593,34 @@ 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("executableName", executableName);
.replace("<pyModuleName>", generatePythonModuleName(executableName));
// The use of fileConfig.name will break federated execution, but that's fine
}

private static String generateCmakeInstall(FileConfig fileConfig) {
final var pyMainPath =
fileConfig.getSrcGenPath().resolve(fileConfig.name + ".py").toAbsolutePath();
// need to replace '\' with '\\' on Windwos for proper escaping in cmake
final var pyMainName = pyMainPath.toString().replace("\\", "\\\\");
return """
if(WIN32)
file(GENERATE OUTPUT <fileName>.bat CONTENT
"@echo off\n\
${Python_EXECUTABLE} <pyMainName> %*"
)
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/<fileName>.bat DESTINATION ${CMAKE_INSTALL_BINDIR})
else()
file(GENERATE OUTPUT <fileName> CONTENT
"#!/bin/sh\\n\\
${Python_EXECUTABLE} <pyMainName> \\\"$@\\\""
)
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/<fileName> DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
"""
.replace("<fileName>", fileConfig.name)
.replace("<pyMainName>", pyMainName);
}

/**
* Generate a ({@code key}, {@code val}) tuple pair for the {@code define_macros} field of the
* Extension class constructor from setuptools.
Expand Down

This file was deleted.

0 comments on commit 7dd7a94

Please sign in to comment.