blob: a01951fe613ee584a44a8bfaf449d563dd60ec4c [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 Cross5ff51b52017-05-02 13:34:32 -070019 "github.com/google/blueprint/proptools"
Colin Cross0c461f12016-10-20 16:11:43 -070020
21 "android/soong/android"
22)
23
Colin Cross6af17aa2017-09-20 12:59:05 -070024func init() {
25 pctx.HostBinToolVariable("protocCmd", "aprotoc")
26}
27
28var (
29 proto = pctx.AndroidStaticRule("protoc",
30 blueprint.RuleParams{
31 Command: "$protocCmd --cpp_out=$outDir $protoFlags $in",
32 CommandDeps: []string{"$protocCmd"},
33 }, "protoFlags", "outDir")
34)
35
36// genProto creates a rule to convert a .proto file to generated .pb.cc and .pb.h files and returns
37// the paths to the generated files.
38func genProto(ctx android.ModuleContext, protoFile android.Path,
39 protoFlags string) (ccFile, headerFile android.WritablePath) {
40
41 ccFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.cc")
42 headerFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.h")
43
44 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
45 Rule: proto,
46 Description: "protoc " + protoFile.Rel(),
47 Outputs: android.WritablePaths{ccFile, headerFile},
48 Input: protoFile,
49 Args: map[string]string{
50 "outDir": android.ProtoDir(ctx).String(),
51 "protoFlags": protoFlags,
52 },
53 })
54
55 return ccFile, headerFile
56}
57
Colin Cross38f794e2017-09-07 10:53:07 -070058func protoDeps(ctx BaseModuleContext, deps Deps, p *android.ProtoProperties, static bool) Deps {
Colin Cross0c461f12016-10-20 16:11:43 -070059 var lib string
Colin Cross0c461f12016-10-20 16:11:43 -070060
Colin Cross5ff51b52017-05-02 13:34:32 -070061 switch proptools.String(p.Proto.Type) {
Colin Cross0c461f12016-10-20 16:11:43 -070062 case "full":
63 if ctx.sdk() {
64 lib = "libprotobuf-cpp-full-ndk"
65 static = true
66 } else {
67 lib = "libprotobuf-cpp-full"
68 }
69 case "lite", "":
70 if ctx.sdk() {
71 lib = "libprotobuf-cpp-lite-ndk"
72 static = true
73 } else {
74 lib = "libprotobuf-cpp-lite"
Colin Cross0c461f12016-10-20 16:11:43 -070075 }
76 default:
Colin Cross5ff51b52017-05-02 13:34:32 -070077 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
78 proptools.String(p.Proto.Type))
Colin Cross0c461f12016-10-20 16:11:43 -070079 }
80
81 if static {
82 deps.StaticLibs = append(deps.StaticLibs, lib)
83 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib)
84 } else {
85 deps.SharedLibs = append(deps.SharedLibs, lib)
86 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib)
87 }
88
89 return deps
90}
91
Colin Cross38f794e2017-09-07 10:53:07 -070092func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags {
Colin Cross0c461f12016-10-20 16:11:43 -070093 flags.CFlags = append(flags.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI")
94 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross38f794e2017-09-07 10:53:07 -070095 "-I"+android.ProtoSubDir(ctx).String(),
96 "-I"+android.ProtoDir(ctx).String(),
Colin Cross0c461f12016-10-20 16:11:43 -070097 )
98
Colin Cross38f794e2017-09-07 10:53:07 -070099 flags.protoFlags = android.ProtoFlags(ctx, p)
Colin Cross5ff51b52017-05-02 13:34:32 -0700100
Colin Cross0c461f12016-10-20 16:11:43 -0700101 return flags
102}