blob: 51f544898c38350dd1595e4200d6f4ba928c47f4 [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 Cross0c461f12016-10-20 16:11:43 -070018 "github.com/google/blueprint"
19
20 "android/soong/android"
21)
22
23func 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 Description: "protoc $out",
33 }, "protoFlags", "outDir")
34)
35
36// TODO(ccross): protos are often used to communicate between multiple modules. If the only
37// way to convert a proto to source is to reference it as a source file, and external modules cannot
38// reference source files in other modules, then every module that owns a proto file will need to
39// export a library for every type of external user (lite vs. full, c vs. c++ vs. java). It would
40// be better to support a proto module type that exported a proto file along with some include dirs,
41// and then external modules could depend on the proto module but use their own settings to
42// generate the source.
43
44func genProto(ctx android.ModuleContext, protoFile android.Path,
45 protoFlags string) (android.ModuleGenPath, android.ModuleGenPath) {
46
Dan Willemsen21ec4902016-11-02 20:43:13 -070047 outFile := android.GenPathWithExt(ctx, "proto", protoFile, "pb.cc")
48 headerFile := android.GenPathWithExt(ctx, "proto", protoFile, "pb.h")
Colin Cross0c461f12016-10-20 16:11:43 -070049 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
50 Rule: proto,
51 Outputs: android.WritablePaths{outFile, headerFile},
52 Input: protoFile,
53 Args: map[string]string{
Dan Willemsen21ec4902016-11-02 20:43:13 -070054 "outDir": protoDir(ctx).String(),
Colin Cross0c461f12016-10-20 16:11:43 -070055 "protoFlags": protoFlags,
56 },
57 })
58
59 return outFile, headerFile
60}
61
62// protoDir returns the module's "gen/proto" directory
63func protoDir(ctx android.ModuleContext) android.ModuleGenPath {
64 return android.PathForModuleGen(ctx, "proto")
65}
66
67// protoSubDir returns the module's "gen/proto/path/to/module" directory
68func protoSubDir(ctx android.ModuleContext) android.ModuleGenPath {
69 return android.PathForModuleGen(ctx, "proto", ctx.ModuleDir())
70}
71
72type ProtoProperties struct {
73 Proto struct {
74 // Proto generator type (full, lite)
75 Type string
76 // Link statically against the protobuf runtime
77 Static bool
78 }
79}
80
81func protoDeps(ctx BaseModuleContext, deps Deps, p *ProtoProperties) Deps {
82 var lib string
83 var static bool
84
85 switch p.Proto.Type {
86 case "full":
87 if ctx.sdk() {
88 lib = "libprotobuf-cpp-full-ndk"
89 static = true
90 } else {
91 lib = "libprotobuf-cpp-full"
92 }
93 case "lite", "":
94 if ctx.sdk() {
95 lib = "libprotobuf-cpp-lite-ndk"
96 static = true
97 } else {
98 lib = "libprotobuf-cpp-lite"
99 if p.Proto.Static {
100 static = true
101 }
102 }
103 default:
104 ctx.PropertyErrorf("proto.type", "unknown proto type %q", p.Proto.Type)
105 }
106
107 if static {
108 deps.StaticLibs = append(deps.StaticLibs, lib)
109 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib)
110 } else {
111 deps.SharedLibs = append(deps.SharedLibs, lib)
112 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib)
113 }
114
115 return deps
116}
117
118func protoFlags(ctx ModuleContext, flags Flags, p *ProtoProperties) Flags {
119 flags.CFlags = append(flags.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI")
120 flags.GlobalFlags = append(flags.GlobalFlags,
121 "-I"+protoSubDir(ctx).String(),
122 "-I"+protoDir(ctx).String(),
123 )
124
125 return flags
126}