Skip to content

Commit c1fc66f

Browse files
committed
feat(node-gateway): Add installation of gRPC for Nix
1 parent 9cc575b commit c1fc66f

File tree

6 files changed

+58
-12
lines changed

6 files changed

+58
-12
lines changed

CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ find_package(Criterion)
8080
find_package(OpalOrchestra)
8181
find_package(LibXml2)
8282
find_package(OpalAsyncApi)
83-
find_package(Protobuf)
84-
find_package(gRPC)
8583

8684
# Check for tools
8785
find_program(PROTOBUFC_COMPILER NAMES protoc-c)
@@ -120,7 +118,10 @@ pkg_check_modules(NANOMSG IMPORTED_TARGET nanomsg)
120118
if(NOT NANOMSG_FOUND)
121119
pkg_check_modules(NANOMSG IMPORTED_TARGET libnanomsg>=1.0.0)
122120
endif()
123-
121+
pkg_check_modules(GRPC IMPORTED_TARGET grpc grpc++)
122+
if (TARGET PkgConfig::GRPC)
123+
set_property(TARGET PkgConfig::GRPC PROPERTY INTERFACE_COMPILE_OPTIONS "")
124+
endif()
124125

125126
if (REDISPP_FOUND)
126127
file(READ "${REDISPP_INCLUDEDIR}/sw/redis++/tls.h" CONTENTS)
@@ -182,6 +183,7 @@ cmake_dependent_option(WITH_SRC "Build executables"
182183
cmake_dependent_option(WITH_TESTS "Run tests" "${WITH_DEFAULTS}" "TOPLEVEL_PROJECT" OFF)
183184
cmake_dependent_option(WITH_TOOLS "Build auxilary tools" "${WITH_DEFAULTS}" "TOPLEVEL_PROJECT" OFF)
184185
cmake_dependent_option(WITH_WEB "Build with internal webserver" "${WITH_DEFAULTS}" "LIBWEBSOCKETS_FOUND" OFF)
186+
cmake_dependent_option(WITH_GRPC "Build with grpc api" "${WITH_DEFAULTS}" "GRPC_FOUND" OFF)
185187

186188
cmake_dependent_option(WITH_NODE_AMQP "Build with amqp node-type" "${WITH_DEFAULTS}" "RABBITMQ_C_FOUND" OFF)
187189
cmake_dependent_option(WITH_NODE_CAN "Build with can node-type" "${WITH_DEFAULTS}" "" OFF)
@@ -216,7 +218,6 @@ cmake_dependent_option(WITH_NODE_ULDAQ "Build with uldaq node-type"
216218
cmake_dependent_option(WITH_NODE_WEBRTC "Build with webrtc node-type" "${WITH_DEFAULTS}" "WITH_WEB; LibDataChannel_FOUND" OFF)
217219
cmake_dependent_option(WITH_NODE_WEBSOCKET "Build with websocket node-type" "${WITH_DEFAULTS}" "WITH_WEB" OFF)
218220
cmake_dependent_option(WITH_NODE_ZEROMQ "Build with zeromq node-type" "${WITH_DEFAULTS}" "LIBZMQ_FOUND; NOT WITHOUT_GPL" OFF)
219-
cmake_dependent_option(WITH_GRPC "Build with grpc api" "${WITH_DEFAULTS}" "gRPC_FOUND" OFF)
220221

221222
# Set a default for the build type
222223
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
@@ -293,6 +294,7 @@ add_feature_info(SRC WITH_SRC "Build execu
293294
add_feature_info(TESTS WITH_TESTS "Run tests")
294295
add_feature_info(TOOLS WITH_TOOLS "Build auxilary tools")
295296
add_feature_info(WEB WITH_WEB "Build with internal webserver")
297+
add_feature_info(GRPC_API WITH_GRPC "Build with gRPC API")
296298

297299
add_feature_info(NODE_AMQP WITH_NODE_AMQP "Build with amqp node-type")
298300
add_feature_info(NODE_CAN WITH_NODE_CAN "Build with can node-type")

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116

117117
opendssc = pkgs.callPackage (nixDir + "/opendssc.nix") { };
118118
orchestra = pkgs.callPackage (nixDir + "/orchestra.nix") { };
119+
grpc-server-reflection = pkgs.callPackage (nixDir + "/opendssc.nix") { };
119120
};
120121
in
121122
{

lib/api/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ if (WITH_GRPC)
5252
${REFLECTION_INCLUDE_DIR}/grpc/reflection/v1alpha/reflection.pb.cc
5353
${REFLECTION_INCLUDE_DIR}/grpc/reflection/v1alpha/reflection.grpc.pb.cc
5454
)
55-
list(APPEND LIBRARIES protobuf::libprotobuf gRPC::grpc++)
55+
list(APPEND LIBRARIES PkgConfig::PROTOBUF PkgConfig::GRPC)
5656
endif()
5757
endif()
5858

lib/api/requests/gateway/grpc.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ class grpcRequest : public NodeRequest {
249249
int field_count = req_desc->field_count();
250250
for (int i = 0; i < field_count; i++) {
251251
if (const FieldDescriptor *f = req_desc->field(i)) {
252-
json_t *json_val = json_object_get(body, f->name().c_str());
252+
json_t *json_val = json_object_get(body, f->name().data());
253253
if (!json_val) {
254254
if (f->is_required()) {
255255
std::cerr << "Missing required field: " << f->name() << std::endl;
@@ -432,32 +432,32 @@ class grpcRequest : public NodeRequest {
432432
break;
433433
}
434434
}
435-
json_object_set_new(root, f->name().c_str(), json_arr);
435+
json_object_set_new(root, f->name().data(), json_arr);
436436
} else {
437437
switch (f->type()) {
438438
case grpc::protobuf::FieldDescriptor::Type::TYPE_STRING:
439439
json_object_set_new(
440-
root, f->name().c_str(),
440+
root, f->name().data(),
441441
json_string(resp_refl->GetString(*response, f).c_str()));
442442
break;
443443
case grpc::protobuf::FieldDescriptor::Type::TYPE_INT32:
444444
json_object_set_new(
445-
root, f->name().c_str(),
445+
root, f->name().data(),
446446
json_integer(resp_refl->GetInt32(*response, f)));
447447
break;
448448
case grpc::protobuf::FieldDescriptor::Type::TYPE_INT64:
449449
json_object_set_new(
450-
root, f->name().c_str(),
450+
root, f->name().data(),
451451
json_integer(resp_refl->GetInt64(*response, f)));
452452
break;
453453
case grpc::protobuf::FieldDescriptor::Type::TYPE_DOUBLE:
454454
json_object_set_new(
455-
root, f->name().c_str(),
455+
root, f->name().data(),
456456
json_real(resp_refl->GetDouble(*response, f)));
457457
break;
458458
case grpc::protobuf::FieldDescriptor::Type::TYPE_BOOL:
459459
json_object_set_new(
460-
root, f->name().c_str(),
460+
root, f->name().data(),
461461
json_boolean(resp_refl->GetBool(*response, f)));
462462
break;
463463
default:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
stdenv,
3+
fetchurl,
4+
protobuf,
5+
grpc
6+
}:
7+
8+
stdenv.mkDerivation {
9+
name = "grpc-server-reflection";
10+
11+
dontUnpack = true;
12+
nativeBuildInputs = [ protobuf grpc ];
13+
14+
src = fetchurl {
15+
url = "https://raw.githubusercontent.com/grpc/grpc/refs/heads/master/src/proto/grpc/reflection/v1alpha/reflection.proto";
16+
sha256 = "sha256-c1kQAgFlRRDNFZy7XEaKi+wUtUmXPG+P967poSN67Ec=";
17+
};
18+
19+
buildPhase = ''
20+
# Create a temporary build directory
21+
mkdir -p tmp
22+
ln -s $src tmp/reflection.proto
23+
24+
# Generate code into build_tmp
25+
protoc --cpp_out=tmp \
26+
--grpc_out=tmp \
27+
--plugin=protoc-gen-grpc=${grpc}/bin/grpc_cpp_plugin \
28+
-I tmp \
29+
tmp/reflection.proto
30+
'';
31+
32+
# Install into standard 'include' path so CMake finds it
33+
installPhase = ''
34+
mkdir -p $out/include/grpc/reflection/v1alpha
35+
cp tmp/*.pb.h $out/include/grpc/reflection/v1alpha/
36+
cp tmp/*.pb.cc $out/include/grpc/reflection/v1alpha/
37+
'';
38+
}

packaging/nix/villas.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
withNodeAmqp ? withAllNodes,
2222
withNodeComedi ? withAllNodes,
2323
withNodeEthercat ? (withAllNodes && system == "x86_64-linux"),
24+
withNodeGateway ? withAllNodes,
2425
withNodeIec60870 ? withAllNodes,
2526
withNodeIec61850 ? withAllNodes,
2627
withNodeInfiniband ? withAllNodes,
@@ -57,6 +58,8 @@
5758
cyrus_sasl,
5859
ethercat,
5960
gnugrep,
61+
grpc,
62+
grpc-server-reflection,
6063
jansson,
6164
lib60870,
6265
libconfig,
@@ -164,6 +167,8 @@ stdenv.mkDerivation {
164167
++ lib.optionals withNodeAmqp [ rabbitmq-c ]
165168
++ lib.optionals withNodeComedi [ comedilib ]
166169
++ lib.optionals withNodeEthercat [ ethercat ]
170+
++ lib.optionals withNodeGateway [ grpc ]
171+
++ lib.optionals withNodeGateway [ grpc-server-reflection ]
167172
++ lib.optionals withNodeIec60870 [ lib60870 ]
168173
++ lib.optionals withNodeIec61850 [ libiec61850 ]
169174
++ lib.optionals withNodeKafka [

0 commit comments

Comments
 (0)