blob: e7f1d414cb10ce399a71c92b0074fd7d48cac339 [file] [log] [blame]
Colin Cross0c461f12016-10-20 16:11:43 -07001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
Colin Cross6af17aa2017-09-20 12:59:05 -070018 "github.com/google/blueprint"
Colin Cross0c461f12016-10-20 16:11:43 -070019
20 "android/soong/android"
21)
22
Colin Cross6af17aa2017-09-20 12:59:05 -070023func init() {
24 pctx.HostBinToolVariable("protocCmd", "aprotoc")
25}
26
27var (
28 proto = pctx.AndroidStaticRule("protoc",
29 blueprint.RuleParams{
30 Command: "$protocCmd --cpp_out=$outDir $protoFlags $in",
31 CommandDeps: []string{"$protocCmd"},
32 }, "protoFlags", "outDir")
33)
34
35// genProto creates a rule to convert a .proto file to generated .pb.cc and .pb.h files and returns
36// the paths to the generated files.
37func genProto(ctx android.ModuleContext, protoFile android.Path,
38 protoFlags string) (ccFile, headerFile android.WritablePath) {
39
40 ccFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.cc")
41 headerFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.h")
42
Colin Crossae887032017-10-23 17:16:14 -070043 ctx.Build(pctx, android.BuildParams{
Colin Cross6af17aa2017-09-20 12:59:05 -070044 Rule: proto,
45 Description: "protoc " + protoFile.Rel(),
46 Outputs: android.WritablePaths{ccFile, headerFile},
47 Input: protoFile,
48 Args: map[string]string{
49 "outDir": android.ProtoDir(ctx).String(),
50 "protoFlags": protoFlags,
51 },
52 })
53
54 return ccFile, headerFile
55}
56
Colin Cross38f794e2017-09-07 10:53:07 -070057func protoDeps(ctx BaseModuleContext, deps Deps, p *android.ProtoProperties, static bool) Deps {
Colin Cross0c461f12016-10-20 16:11:43 -070058 var lib string
Colin Cross0c461f12016-10-20 16:11:43 -070059
Nan Zhang0007d812017-11-07 10:57:05 -080060 switch String(p.Proto.Type) {
Colin Cross0c461f12016-10-20 16:11:43 -070061 case "full":
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070062 if ctx.useSdk() {
Colin Cross0c461f12016-10-20 16:11:43 -070063 lib = "libprotobuf-cpp-full-ndk"
64 static = true
65 } else {
66 lib = "libprotobuf-cpp-full"
67 }
68 case "lite", "":
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070069 if ctx.useSdk() {
Colin Cross0c461f12016-10-20 16:11:43 -070070 lib = "libprotobuf-cpp-lite-ndk"
71 static = true
72 } else {
73 lib = "libprotobuf-cpp-lite"
Colin Cross0c461f12016-10-20 16:11:43 -070074 }
75 default:
Colin Cross5ff51b52017-05-02 13:34:32 -070076 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
Nan Zhang0007d812017-11-07 10:57:05 -080077 String(p.Proto.Type))
Colin Cross0c461f12016-10-20 16:11:43 -070078 }
79
80 if static {
81 deps.StaticLibs = append(deps.StaticLibs, lib)
82 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib)
83 } else {
84 deps.SharedLibs = append(deps.SharedLibs, lib)
85 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib)
86 }
87
88 return deps
89}
90
Colin Cross38f794e2017-09-07 10:53:07 -070091func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags {
Colin Cross0c461f12016-10-20 16:11:43 -070092 flags.CFlags = append(flags.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI")
93 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross38f794e2017-09-07 10:53:07 -070094 "-I"+android.ProtoSubDir(ctx).String(),
95 "-I"+android.ProtoDir(ctx).String(),
Colin Cross0c461f12016-10-20 16:11:43 -070096 )
97
Colin Cross38f794e2017-09-07 10:53:07 -070098 flags.protoFlags = android.ProtoFlags(ctx, p)
Colin Cross5ff51b52017-05-02 13:34:32 -070099
Colin Cross0c461f12016-10-20 16:11:43 -0700100 return flags
101}