diff options
author | Ivan Komissarov <ABBAPOH@gmail.com> | 2019-05-16 23:44:02 +0200 |
---|---|---|
committer | Ivan Komissarov <ABBAPOH@gmail.com> | 2019-06-27 10:18:09 +0000 |
commit | 0e5050b7f75c90d406c5292ddf8d43b6a5b555c8 (patch) | |
tree | ef6fa7afb3c6293ccf2919d0653d407df3afe3dd | |
parent | bb4622ac70ab43bb1e8f1930fc8430a3ec00eafb (diff) |
Add support for gRPC to the protobuf.cpp module
This implements support for the gRPC framework: https://www.grpc.io
Change-Id: Ia85461b9618e73827114c137fce8615e5a8139e3
Reviewed-by: Qbs CI Bot <travis-bot@weickelt.de>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
-rw-r--r-- | doc/reference/modules/protobufcpp-module.qdoc | 87 | ||||
-rw-r--r-- | examples/examples.qbs | 1 | ||||
-rw-r--r-- | examples/grpc/client.cpp | 95 | ||||
-rw-r--r-- | examples/grpc/ping-pong-grpc.proto | 15 | ||||
-rw-r--r-- | examples/grpc/ping-pong-grpc.qbs | 65 | ||||
-rw-r--r-- | examples/grpc/server.cpp | 78 | ||||
-rw-r--r-- | share/qbs/modules/protobuf/cpp/protobufcpp.qbs | 84 | ||||
-rw-r--r-- | share/qbs/modules/protobuf/protobuf.js | 25 | ||||
-rw-r--r-- | tests/auto/blackbox/testdata/grpc/grpc.cpp | 49 | ||||
-rw-r--r-- | tests/auto/blackbox/testdata/grpc/grpc.proto | 15 | ||||
-rw-r--r-- | tests/auto/blackbox/testdata/grpc/grpc_cpp.qbs | 26 | ||||
-rw-r--r-- | tests/auto/blackbox/tst_blackbox.cpp | 23 | ||||
-rw-r--r-- | tests/auto/blackbox/tst_blackbox.h | 2 |
13 files changed, 556 insertions, 9 deletions
diff --git a/doc/reference/modules/protobufcpp-module.qdoc b/doc/reference/modules/protobufcpp-module.qdoc index 63c56c6ca..b82ccc9f7 100644 --- a/doc/reference/modules/protobufcpp-module.qdoc +++ b/doc/reference/modules/protobufcpp-module.qdoc @@ -68,10 +68,33 @@ \li \c{*.proto} \li 1.13.0 \li Source files with this tag are considered inputs to the \c protoc compiler. + \row + \li \c{"protobuf.grpc"} + \li + \li 1.14.0 + \li Source files with this tag are considered as gRPC files. \endtable */ /*! + \qmlproperty string protobuf.cpp::grpcIncludePath + + The path where grpc++ headers are located. Set this property to override the + default location. + + \defaultvalue \c auto-detected +*/ + +/*! + \qmlproperty string protobuf.cpp::grpcLibraryPath + + The path where the grpc++ library is located. Set this property to override the + default location. + + \defaultvalue \c auto-detected +*/ + +/*! \qmlproperty string protobuf.cpp::protocBinary The command to invoke when compiling proto definition files. @@ -108,3 +131,67 @@ \defaultvalue \c auto-detected */ + +/*! + \qmlproperty bool protobuf.cpp::useGrpc + + Whether to use gRPC framework. + + Use this property to enable support for the modern open source high performance RPC + framework by Google, gRPC (\l{https://www.grpc.io}). + + A simple qbs file that uses grpc can be written as follows: + \code + CppApplication { + Depends { name: "protobuf.cpp" } + protobuf.cpp.useGrpc: true + files: ["main.cpp"] + Group { + files: "grpc.proto" + fileTags: "protobuf.grpc" + } + } + \endcode + + \note that \c protobuf.grpc tag should be assigned manually because gRPC uses same \c *.proto + files and \QBS can't detect whether to generate gRPC or \c protobuf. + + The following \c grpc.proto file... + \code + syntax = "proto3"; + + package Qbs; + + message Request { + string name = 1; + } + + message Response { + string name = 1; + } + + service Grpc { + rpc doWork(Request) returns (Response) {} + } + \endcode + + ...can be used in the C++ sources as follows: + \code + #include <grpc.grpc.pb.h> + + class Service final : public Qbs::Grpc::Service + { + grpc::Status doWork( + grpc::ServerContext* context, + const Qbs::Request* request, + Qbs::Response* reply) override + { + (void)context; + reply->set_name(request->name()); + return grpc::Status::OK; + } + }; + \endcode + + \defaultvalue \c false +*/ diff --git a/examples/examples.qbs b/examples/examples.qbs index c6c1f4ac3..abb6d5d9a 100644 --- a/examples/examples.qbs +++ b/examples/examples.qbs @@ -58,6 +58,7 @@ Project { "code-generator/code-generator.qbs", "collidingmice/collidingmice.qbs", "compiled-qml/myapp.qbs", + "grpc/ping-pong-grpc.qbs", "helloworld-complex/hello.qbs", "helloworld-minimal/hello.qbs", "helloworld-qt/hello.qbs", diff --git a/examples/grpc/client.cpp b/examples/grpc/client.cpp new file mode 100644 index 000000000..55bcd866d --- /dev/null +++ b/examples/grpc/client.cpp @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2019 Ivan Komissarov +** Contact: abbapoh@gmail.com +** +** This file is part of Qbs. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms and +** conditions see http://www.qt.io/terms-conditions. For further information +** use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#ifdef __GNUC__ + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wunused-parameter" +#endif // __GNUC__ + +#include <ping-pong-grpc.grpc.pb.h> + +#include <grpc/grpc.h> +#include <grpc++/channel.h> +#include <grpc++/client_context.h> +#include <grpc++/create_channel.h> + +#ifdef __GNUC__ + #pragma GCC diagnostic pop +#endif // __GNUC__ + +#include <iostream> +#include <memory> +#include <string> + +class Client +{ +public: + Client(std::shared_ptr<grpc::Channel> channel) + : m_stub(PP::MyApi::NewStub(channel)) {} + + int ping(int count) { + PP::Ping request; + request.set_count(count); + PP::Pong reply; + grpc::ClientContext context; + + const auto status = m_stub->pingPong(&context, request, &reply); + if (status.ok()) { + return reply.count(); + } else { + throw std::runtime_error("invalid status"); + } + } + +private: + std::unique_ptr<PP::MyApi::Stub> m_stub; +}; + +int main(int, char**) +{ + Client client( + grpc::CreateCustomChannel( + "localhost:50051", + grpc::InsecureChannelCredentials(), + grpc::ChannelArguments())); + + for (int i = 0; i < 1000; ++i) { + std::cout << "Sending ping " << i << "... "; + int result = client.ping(i); + if (result != i) { + std::cerr << "Invalid pong " << result << " for ping" << i << std::endl; + continue; + } + std::cout << "got pong " << result << std::endl; + } + + return 0; +} + diff --git a/examples/grpc/ping-pong-grpc.proto b/examples/grpc/ping-pong-grpc.proto new file mode 100644 index 000000000..da6d9490c --- /dev/null +++ b/examples/grpc/ping-pong-grpc.proto @@ -0,0 +1,15 @@ +syntax = "proto3"; + +package PP; + +message Ping { + int32 count = 1; +} + +message Pong { + int32 count = 1; +} + +service MyApi { + rpc pingPong(Ping) returns (Pong) {} +} diff --git a/examples/grpc/ping-pong-grpc.qbs b/examples/grpc/ping-pong-grpc.qbs new file mode 100644 index 000000000..8d8909350 --- /dev/null +++ b/examples/grpc/ping-pong-grpc.qbs @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2019 Ivan Komissarov +** Contact: abbapoh@gmail.com +** +** This file is part of Qbs. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms and +** conditions see http://www.qt.io/terms-conditions. For further information +** use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +import qbs.Utilities + +Project { + condition: Utilities.versionCompare(qbs.version, "1.14") >= 0 + + Application { + Depends { name: "cpp" } + Depends { name: "protobuf.cpp"; required: false } + condition: protobuf.cpp.present + protobuf.cpp.useGrpc: true + consoleApplication: true + cpp.cxxLanguageVersion: "c++17" + name: "client" + files: "client.cpp" + Group { + files: "ping-pong-grpc.proto" + fileTags: "protobuf.grpc" + } + } + + Application { + Depends { name: "cpp" } + Depends { name: "protobuf.cpp"; required: false } + condition: protobuf.cpp.present + protobuf.cpp.useGrpc: true + consoleApplication: true + cpp.cxxLanguageVersion: "c++17" + name: "server" + files: "client.cpp" + Group { + files: "ping-pong-grpc.proto" + fileTags: "protobuf.grpc" + } + } +} diff --git a/examples/grpc/server.cpp b/examples/grpc/server.cpp new file mode 100644 index 000000000..89d6f0096 --- /dev/null +++ b/examples/grpc/server.cpp @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2019 Ivan Komissarov +** Contact: abbapoh@gmail.com +** +** This file is part of Qbs. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms and +** conditions see http://www.qt.io/terms-conditions. For further information +** use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#ifdef __GNUC__ + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wunused-parameter" +#endif // __GNUC__ + +#include <ping-pong-grpc.grpc.pb.h> + +#include <grpc/grpc.h> +#include <grpc++/server.h> +#include <grpc++/server_builder.h> +#include <grpc++/server_context.h> +#include <grpc++/security/server_credentials.h> + +#ifdef __GNUC__ + #pragma GCC diagnostic pop +#endif // __GNUC__ + +#include <iostream> +#include <memory> +#include <string> + +class Service final : public PP::MyApi::Service +{ + grpc::Status pingPong( + grpc::ServerContext* context, + const PP::Ping* request, + PP::Pong* reply) override + { + (void)context; + reply->set_count(request->count()); + return grpc::Status::OK; + } +}; + +int main(int, char**) +{ + std::string server_address("0.0.0.0:50051"); + Service service; + + grpc::ServerBuilder builder; + builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); + builder.RegisterService(&service); + std::unique_ptr<grpc::Server> server(builder.BuildAndStart()); + std::cout << "Server listening on " << server_address << std::endl; + server->Wait(); + return 0; +} + diff --git a/share/qbs/modules/protobuf/cpp/protobufcpp.qbs b/share/qbs/modules/protobuf/cpp/protobufcpp.qbs index 2b6e94e83..0c511f2aa 100644 --- a/share/qbs/modules/protobuf/cpp/protobufcpp.qbs +++ b/share/qbs/modules/protobuf/cpp/protobufcpp.qbs @@ -2,6 +2,7 @@ import qbs import qbs.File import qbs.FileInfo import qbs.Probes +import qbs.ModUtils import "../protobufbase.qbs" as ProtobufBase import "../protobuf.js" as HelperFunctions @@ -9,23 +10,68 @@ ProtobufBase { property string includePath: includeProbe.path property string libraryPath: libraryProbe.path + property bool useGrpc: false + + property string grpcIncludePath: grpcIncludeProbe.path + property string grpcLibraryPath: grpcLibraryProbe.path + Depends { name: "cpp" } - cpp.libraryPaths: [libraryPath] - cpp.dynamicLibraries: qbs.targetOS.contains("unix") ? ["protobuf", "pthread"] : ["protobuf"] - cpp.includePaths: [outputDir, includePath] + property path grpcPluginPath: grpcPluginProbe.filePath + + Probes.BinaryProbe { + condition: useGrpc + id: grpcPluginProbe + names: "grpc_cpp_plugin" + } + + cpp.libraryPaths: { + var result = [libraryPath]; + if (useGrpc) + result.push(grpcLibraryPath); + return result; + } + cpp.dynamicLibraries: { + var result = ["protobuf"]; + if (qbs.targetOS.contains("unix")) + result.push("pthread"); + if (useGrpc) + result.push("grpc++"); + return result; + } + cpp.includePaths: { + var result = [outputDir, includePath]; + if (useGrpc) + result.push("grpcIncludePath"); + return result; + } Rule { - inputs: ["protobuf.input"] + inputs: ["protobuf.input", "protobuf.grpc"] outputFileTags: ["hpp", "cpp"] outputArtifacts: { - return [ - HelperFunctions.cppArtifact(input.protobuf.cpp, product, input, "hpp", ".pb.h"), - HelperFunctions.cppArtifact(input.protobuf.cpp, product, input, "cpp", ".pb.cc") - ]; + var result = [ + HelperFunctions.cppArtifact(input.protobuf.cpp, product, input, "hpp", ".pb.h"), + HelperFunctions.cppArtifact(input.protobuf.cpp, product, input, "cpp", ".pb.cc") + ]; + if (input.fileTags.contains("protobuf.grpc")) { + result.push( + HelperFunctions.cppArtifact(input.protobuf.cpp, product, input, "hpp", ".grpc.pb.h"), + HelperFunctions.cppArtifact(input.protobuf.cpp, product, input, "cpp", ".grpc.pb.cc")); + } + + return result; } - prepare: HelperFunctions.doPrepare(input.protobuf.cpp, product, input, outputs, "cpp") + prepare: { + var result = HelperFunctions.doPrepare( + input.protobuf.cpp, product, input, outputs, "cpp"); + if (input.fileTags.contains("protobuf.grpc")) { + result = ModUtils.concatAll(result, HelperFunctions.doPrepareGrpc( + input.protobuf.cpp, product, input, outputs, "cpp")); + } + return result; + } } validateFunc: { @@ -35,6 +81,15 @@ ProtobufBase { throw "Can't find cpp protobuf include files. Please set the includePath property."; if (!HelperFunctions.checkPath(libraryPath)) throw "Can't find cpp protobuf library. Please set the libraryPath property."; + + if (useGrpc) { + if (!File.exists(grpcPluginPath)) + throw "Can't find grpc_cpp_plugin plugin. Please set the grpcPluginPath property."; + if (!HelperFunctions.checkPath(grpcIncludePath)) + throw "Can't find grpc++ include files. Please set the grpcIncludePath property."; + if (!HelperFunctions.checkPath(grpcLibraryPath)) + throw "Can't find grpc++ library. Please set the grpcLibraryPath property."; + } } } @@ -47,4 +102,15 @@ ProtobufBase { id: libraryProbe names: "protobuf" } + + Probes.IncludeProbe { + id: grpcIncludeProbe + pathSuffixes: "grpc++" + names: "grpc++.h" + } + + Probes.LibraryProbe { + id: grpcLibraryProbe + names: "grpc++" + } } diff --git a/share/qbs/modules/protobuf/protobuf.js b/share/qbs/modules/protobuf/protobuf.js index 576a5ec07..511f5a6c6 100644 --- a/share/qbs/modules/protobuf/protobuf.js +++ b/share/qbs/modules/protobuf/protobuf.js @@ -109,3 +109,28 @@ function doPrepare(module, product, input, outputs, lang) cmd.description = "generating " + lang + " files for " + input.fileName; return [cmd]; } + +function doPrepareGrpc(module, product, input, outputs, lang) +{ + var outputDir = module.outputDir; + var args = []; + + args.push("--grpc_out", outputDir); + args.push("--plugin=protoc-gen-grpc=" + module.grpcPluginPath); + + var importPaths = module.importPaths; + if (importPaths.length === 0) + importPaths = [FileInfo.path(input.filePath)]; + importPaths.forEach(function(path) { + if (!FileInfo.isAbsolutePath(path)) + path = FileInfo.joinPaths(product.sourceDirectory, path); + args.push("--proto_path", path); + }); + + args.push(input.filePath); + + var cmd = new Command(module.protocBinary, args); + cmd.highlight = "codegen"; + cmd.description = "generating " + lang + " files for " + input.fileName; + return [cmd]; +} diff --git a/tests/auto/blackbox/testdata/grpc/grpc.cpp b/tests/auto/blackbox/testdata/grpc/grpc.cpp new file mode 100644 index 000000000..81995601d --- /dev/null +++ b/tests/auto/blackbox/testdata/grpc/grpc.cpp @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2019 Ivan Komissarov +** Contact: abbapoh@gmail.com +** +** This file is part of Qbs. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms and +** conditions see http://www.qt.io/terms-conditions. For further information +** use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#include <grpc.grpc.pb.h> + +class ServiceImpl final : public Qbs::Grpc::Service +{ + grpc::Status doWork( + grpc::ServerContext* context, + const Qbs::Request* request, + Qbs::Response* reply) override + { + (void)context; + reply->set_name(request->name()); + return grpc::Status::OK; + } +}; + +int main(int, char**) +{ + return 0; +} diff --git a/tests/auto/blackbox/testdata/grpc/grpc.proto b/tests/auto/blackbox/testdata/grpc/grpc.proto new file mode 100644 index 000000000..631006bad --- /dev/null +++ b/tests/auto/blackbox/testdata/grpc/grpc.proto @@ -0,0 +1,15 @@ +syntax = "proto3"; + +package Qbs; + +message Request { + string name = 1; +} + +message Response { + string name = 1; +} + +service Grpc { + rpc doWork(Request) returns (Response) {} +} diff --git a/tests/auto/blackbox/testdata/grpc/grpc_cpp.qbs b/tests/auto/blackbox/testdata/grpc/grpc_cpp.qbs new file mode 100644 index 000000000..8ee3dd9c9 --- /dev/null +++ b/tests/auto/blackbox/testdata/grpc/grpc_cpp.qbs @@ -0,0 +1,26 @@ +import qbs + +CppApplication { + name: "grpc_cpp" + consoleApplication: true + condition: hasDependencies + + Depends { name: "cpp" } + cpp.cxxLanguageVersion: "c++11" + cpp.warningLevel: "none" + + Depends { name: "protobuf.cpp"; required: false } + protobuf.cpp.useGrpc: true + + property bool hasDependencies: { + console.info("has grpc: " + protobuf.cpp.present); + return protobuf.cpp.present; + } + + files: "grpc.cpp" + + Group { + files: "grpc.proto" + fileTags: "protobuf.grpc" + } +} diff --git a/tests/auto/blackbox/tst_blackbox.cpp b/tests/auto/blackbox/tst_blackbox.cpp index 534922112..f87889155 100644 --- a/tests/auto/blackbox/tst_blackbox.cpp +++ b/tests/auto/blackbox/tst_blackbox.cpp @@ -6823,6 +6823,29 @@ void TestBlackbox::groupsInModules() QCOMPARE(output.readAll().trimmed(), QByteArray("diamond")); } +void TestBlackbox::grpc_data() +{ + QTest::addColumn<QString>("projectFile"); + QTest::newRow("cpp") << QString("grpc_cpp.qbs"); +} + +void TestBlackbox::grpc() +{ + QDir::setCurrent(testDataDir + "/grpc"); + QFETCH(QString, projectFile); + rmDirR(relativeBuildDir()); + QbsRunParameters resolveParams("resolve", QStringList{"-f", projectFile}); + QCOMPARE(runQbs(resolveParams), 0); + const bool withGrpc = m_qbsStdout.contains("has grpc: true"); + const bool withoutGrpc = m_qbsStdout.contains("has grpc: false"); + QVERIFY2(withGrpc || withoutGrpc, m_qbsStdout.constData()); + if (withoutGrpc) + QSKIP("grpc module not present"); + + QbsRunParameters runParams; + QCOMPARE(runQbs(runParams), 0); +} + void TestBlackbox::ico() { QDir::setCurrent(testDataDir + "/ico"); diff --git a/tests/auto/blackbox/tst_blackbox.h b/tests/auto/blackbox/tst_blackbox.h index e8c456b1e..0a14c418c 100644 --- a/tests/auto/blackbox/tst_blackbox.h +++ b/tests/auto/blackbox/tst_blackbox.h @@ -119,6 +119,8 @@ private slots: void generator(); void generator_data(); void groupsInModules(); + void grpc_data(); + void grpc(); void ico(); void importAssignment(); void importChangeTracking(); |