aboutsummaryrefslogtreecommitdiffstats
path: root/share/qbs/modules/protobuf/cpp/protobufcpp.qbs
blob: 50caf058a4e5a021b901e2ccfe85e4176bcc5e82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import qbs
import qbs.File
import qbs.FileInfo
import qbs.Probes
import qbs.ModUtils
import "../protobufbase.qbs" as ProtobufBase
import "../protobuf.js" as HelperFunctions

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

    readonly property string _libraryName: {
        var libraryName;
        if (libraryProbe.found)
            libraryName = FileInfo.baseName(libraryProbe.fileName);
        if (libraryName.startsWith("lib"))
            libraryName = libraryName.substring(3);
        return libraryName;
    }

    Depends { name: "cpp" }

    property path grpcPluginPath: grpcPluginProbe.filePath

    Probes.BinaryProbe {
        condition: useGrpc
        id: grpcPluginProbe
        names: "grpc_cpp_plugin"
    }

    cpp.libraryPaths: {
        var result = [];
        if (libraryPath)
            result.push(libraryPath);
        if (useGrpc && grpcLibraryPath)
            result.push(grpcLibraryPath);
        return result;
    }
    cpp.dynamicLibraries: {
        var result = [];
        if (_libraryName)
            result.push(_libraryName)
        if (qbs.targetOS.contains("unix"))
            result.push("pthread");
        if (useGrpc)
            result.push("grpc++");
        return result;
    }
    cpp.includePaths: {
        var result = [outputDir];
        if (includePath)
            result.push(includePath);
        if (useGrpc && grpcIncludePath)
            result.push(grpcIncludePath);
        return result;
    }

    Rule {
        inputs: ["protobuf.input", "protobuf.grpc"]
        outputFileTags: ["hpp", "protobuf.hpp", "cpp"]
        outputArtifacts: {
            var outputDir = HelperFunctions.getOutputDir(input.protobuf.cpp, input);
            var result = [
                        HelperFunctions.cppArtifact(outputDir, input, ["hpp", "protobuf.hpp"],
                                                    ".pb.h"),
                        HelperFunctions.cppArtifact(outputDir, input, "cpp", ".pb.cc")
                    ];
            if (input.fileTags.contains("protobuf.grpc")) {
                result.push(
                        HelperFunctions.cppArtifact(outputDir, input, ["hpp", "protobuf.hpp"],
                                                    ".grpc.pb.h"),
                        HelperFunctions.cppArtifact(outputDir, input, ["cpp"], ".grpc.pb.cc"));
            }

            return result;
        }

        prepare: {
            var result = HelperFunctions.doPrepare(
                        input.protobuf.cpp, product, input, outputs, "cpp");
            if (input.fileTags.contains("protobuf.grpc")) {
                result = ModUtils.concatAll(result, HelperFunctions.doPrepare(
                                input.protobuf.cpp, product, input, outputs, "grpc",
                                "protoc-gen-grpc=" + input.protobuf.cpp.grpcPluginPath));
            }
            return result;
        }
    }

    Probes.IncludeProbe {
        id: includeProbe
        names: "google/protobuf/message.h"
    }

    Probes.LibraryProbe {
        id: libraryProbe
        names: [
            "protobuf",
            "protobufd",
        ]
    }

    Probes.IncludeProbe {
        id: grpcIncludeProbe
        pathSuffixes: "grpc++"
        names: "grpc++.h"
    }

    Probes.LibraryProbe {
        id: grpcLibraryProbe
        names: "grpc++"
    }

    validate: {
        HelperFunctions.validateCompiler(compilerName, compilerPath);

        if (!HelperFunctions.checkPath(includePath))
            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.";
        }
    }
}