From 0e5050b7f75c90d406c5292ddf8d43b6a5b555c8 Mon Sep 17 00:00:00 2001 From: Ivan Komissarov Date: Thu, 16 May 2019 23:44:02 +0200 Subject: 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 Reviewed-by: Christian Kandeler --- share/qbs/modules/protobuf/cpp/protobufcpp.qbs | 84 +++++++++++++++++++++++--- share/qbs/modules/protobuf/protobuf.js | 25 ++++++++ 2 files changed, 100 insertions(+), 9 deletions(-) (limited to 'share/qbs') 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]; +} -- cgit v1.2.3