Skip to content

Commit

Permalink
Fix protobufs path problem
Browse files Browse the repository at this point in the history
  • Loading branch information
lhstrh committed Nov 10, 2023
1 parent 6c3a1b8 commit 26efd80
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 41 deletions.
14 changes: 10 additions & 4 deletions core/src/main/java/org/lflang/federated/extensions/CExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,11 @@ protected void deserialize(
result.pr("lf_set(" + receiveRef + ", " + value + ");");
}
}
case PROTO -> throw new UnsupportedOperationException(
"Protobuf serialization is not supported yet.");
case PROTO -> {
return;
}
// throw new UnsupportedOperationException(
// "Protobuf serialization is not supported yet.");
case ROS2 -> {
var portType = ASTUtils.getInferredType(((Port) receivingPort.getVariable()));
var portTypeStr = types.getTargetType(portType);
Expand Down Expand Up @@ -408,8 +411,11 @@ protected void serializeAndSend(
result.pr(sendingFunction + "(" + commonArgs + ", " + pointerExpression + ");");
}
}
case PROTO -> throw new UnsupportedOperationException(
"Protobuf serialization is not supported yet.");
case PROTO -> {
return;
}
// throw new UnsupportedOperationException(
// "Protobuf serialization is not supported yet.");
case ROS2 -> {
var typeStr = types.getTargetType(type);
if (CUtil.isTokenType(type, types)) {
Expand Down
38 changes: 22 additions & 16 deletions core/src/main/java/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import static org.lflang.ast.ASTUtils.toText;
import static org.lflang.util.StringUtil.addDoubleQuotes;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import java.io.File;
import java.io.IOException;
Expand All @@ -45,6 +44,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -1452,7 +1452,7 @@ private void generateStartTimeStep(ReactorInstance instance) {

temp.pr("// Add port " + port.getFullName() + " to array of is_present fields.");

if (!Objects.equal(port.getParent(), instance)) {
if (!port.getParent().equals(instance)) {
// The port belongs to contained reactor, so we also have
// iterate over the instance bank members.
temp.startScopedBlock();
Expand Down Expand Up @@ -1486,7 +1486,7 @@ private void generateStartTimeStep(ReactorInstance instance) {

enclaveInfo.numIsPresentFields += port.getWidth() * port.getParent().getTotalWidth();

if (!Objects.equal(port.getParent(), instance)) {
if (!port.getParent().equals(instance)) {
temp.pr("count++;");
temp.endScopedBlock();
temp.endScopedBlock();
Expand Down Expand Up @@ -1614,25 +1614,31 @@ private void generateTimerInitializations(ReactorInstance instance) {
* <p>Run, if possible, the proto-c protocol buffer code generator to produce the required .h and
* .c files.
*
* @param filename Name of the file to process.
* @param file Path of the .proto file to process.
*/
public void processProtoFile(String filename) {
public void processProtoFile(Path file) {
var fileName = file.getFileName().toString();
var directory = Objects.requireNonNullElse(file.getParent(), "");
var protoc =
commandFactory.createCommand(
"protoc-c",
List.of("--c_out=" + this.fileConfig.getSrcGenPath(), filename),
List.of(
"--c_out=" + this.fileConfig.getSrcGenPath(),
"--proto_path=" + directory,
fileName),
fileConfig.srcPath);
if (protoc == null) {
messageReporter.nowhere().error("Processing .proto files requires protoc-c >= 1.3.3.");
return;
}
var returnCode = protoc.run();
if (returnCode == 0) {
var nameSansProto = filename.substring(0, filename.length() - 6);
targetConfig.compileAdditionalSources.add(
fileConfig.getSrcGenPath().resolve(nameSansProto + ".pb-c.c").toString());
} else {
messageReporter.nowhere().error("protoc-c returns error code " + returnCode);
var returnCode = protoc.run();
if (returnCode == 0) {
messageReporter.nowhere().info("Successfully compiled " + file);
var nameSansProto = fileName.substring(0, fileName.length() - 6);
targetConfig.compileAdditionalSources.add(
fileConfig.getSrcGenPath().resolve(nameSansProto + ".pb-c.c").toString());
} else {
messageReporter.nowhere().error("protoc-c failed:" + protoc.getErrors());
}
}
}

Expand Down Expand Up @@ -2013,10 +2019,10 @@ protected void setUpGeneralParameters() {
}
}

/** Iterate over the .proto files specified in the 'proto' target property and compile them. */
protected void handleProtoFiles() {
// Handle .proto files.
for (String file : targetConfig.get(ProtobufsProperty.INSTANCE)) {
this.processProtoFile(file);
this.processProtoFile(Path.of(file));
}
}

Expand Down
39 changes: 18 additions & 21 deletions core/src/main/java/org/lflang/generator/python/PythonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -63,7 +64,6 @@
import org.lflang.target.property.CompilerProperty;
import org.lflang.target.property.ProtobufsProperty;
import org.lflang.util.FileUtil;
import org.lflang.util.LFCommand;
import org.lflang.util.StringUtil;

/**
Expand Down Expand Up @@ -280,39 +280,36 @@ protected String generateTopLevelPreambles(Reactor ignored) {
@Override
protected void handleProtoFiles() {
for (String name : targetConfig.get(ProtobufsProperty.INSTANCE)) {
this.processProtoFile(name);
this.processProtoFile(Path.of(name));
int dotIndex = name.lastIndexOf(".");
String rootFilename = dotIndex > 0 ? name.substring(0, dotIndex) : name;
pythonPreamble.pr("import " + rootFilename + "_pb2 as " + rootFilename);
protoNames.add(rootFilename);
}
}

/**
* Process a given .proto file.
*
* <p>Run, if possible, the proto-c protocol buffer code generator to produce the required .h and
* .c files.
*
* @param filename Name of the file to process.
*/
@Override
public void processProtoFile(String filename) {
LFCommand protoc =
public void processProtoFile(Path file) {
var fileName = file.getFileName().toString();
var directory = Objects.requireNonNullElse(file.getParent(), "");
var protoc =
commandFactory.createCommand(
"protoc",
List.of("--python_out=" + fileConfig.getSrcGenPath(), filename),
List.of(
"--python_out=" + this.fileConfig.getSrcGenPath(),
"--proto_path=" + directory,
fileName),
fileConfig.srcPath);

if (protoc == null) {
messageReporter.nowhere().error("Processing .proto files requires libprotoc >= 3.6.1");
return;
}
int returnCode = protoc.run();
if (returnCode == 0) {
pythonRequiredModules.add("google-api-python-client");
messageReporter.nowhere().error("Processing .proto files requires protoc-c >= 1.3.3.");
} else {
messageReporter.nowhere().error("protoc returns error code " + returnCode);
var returnCode = protoc.run();
if (returnCode == 0) {
messageReporter.nowhere().info("Successfully compiled " + file);
pythonRequiredModules.add("google-api-python-client");
} else {
messageReporter.nowhere().error("protoc-c failed:" + protoc.getErrors());
}
}
}

Expand Down

0 comments on commit 26efd80

Please sign in to comment.