blob: 3b5fd3b63a046e57335c591e30f9e274e551d5d8 [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"
Joe Onorato09e94ab2017-11-18 18:23:14 -080019 "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{
Joe Onorato09e94ab2017-11-18 18:23:14 -080031 Command: "$protocCmd --cpp_out=$protoOutParams:$outDir $protoFlags $in",
Colin Cross6af17aa2017-09-20 12:59:05 -070032 CommandDeps: []string{"$protocCmd"},
Joe Onorato09e94ab2017-11-18 18:23:14 -080033 }, "protoFlags", "protoOutParams", "outDir")
Colin Cross6af17aa2017-09-20 12:59:05 -070034)
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,
Joe Onorato09e94ab2017-11-18 18:23:14 -080039 protoFlags string, protoOutParams string) (ccFile, headerFile android.WritablePath) {
Colin Cross6af17aa2017-09-20 12:59:05 -070040
41 ccFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.cc")
42 headerFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.h")
43
Colin Crossae887032017-10-23 17:16:14 -070044 ctx.Build(pctx, android.BuildParams{
Colin Cross6af17aa2017-09-20 12:59:05 -070045 Rule: proto,
46 Description: "protoc " + protoFile.Rel(),
47 Outputs: android.WritablePaths{ccFile, headerFile},
48 Input: protoFile,
49 Args: map[string]string{
Joe Onorato09e94ab2017-11-18 18:23:14 -080050 "outDir": android.ProtoDir(ctx).String(),
51 "protoFlags": protoFlags,
52 "protoOutParams": protoOutParams,
Colin Cross6af17aa2017-09-20 12:59:05 -070053 },
54 })
55
56 return ccFile, headerFile
57}
58
Colin Cross38f794e2017-09-07 10:53:07 -070059func protoDeps(ctx BaseModuleContext, deps Deps, p *android.ProtoProperties, static bool) Deps {
Colin Cross0c461f12016-10-20 16:11:43 -070060 var lib string
Colin Cross0c461f12016-10-20 16:11:43 -070061
Nan Zhang0007d812017-11-07 10:57:05 -080062 switch String(p.Proto.Type) {
Colin Cross0c461f12016-10-20 16:11:43 -070063 case "full":
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070064 if ctx.useSdk() {
Colin Cross0c461f12016-10-20 16:11:43 -070065 lib = "libprotobuf-cpp-full-ndk"
66 static = true
67 } else {
68 lib = "libprotobuf-cpp-full"
69 }
70 case "lite", "":
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070071 if ctx.useSdk() {
Colin Cross0c461f12016-10-20 16:11:43 -070072 lib = "libprotobuf-cpp-lite-ndk"
73 static = true
74 } else {
75 lib = "libprotobuf-cpp-lite"
Colin Cross0c461f12016-10-20 16:11:43 -070076 }
77 default:
Colin Cross5ff51b52017-05-02 13:34:32 -070078 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
Nan Zhang0007d812017-11-07 10:57:05 -080079 String(p.Proto.Type))
Colin Cross0c461f12016-10-20 16:11:43 -070080 }
81
82 if static {
83 deps.StaticLibs = append(deps.StaticLibs, lib)
84 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib)
85 } else {
86 deps.SharedLibs = append(deps.SharedLibs, lib)
87 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib)
88 }
89
90 return deps
91}
92
Colin Cross38f794e2017-09-07 10:53:07 -070093func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags {
Colin Cross0c461f12016-10-20 16:11:43 -070094 flags.CFlags = append(flags.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI")
95 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross38f794e2017-09-07 10:53:07 -070096 "-I"+android.ProtoSubDir(ctx).String(),
97 "-I"+android.ProtoDir(ctx).String(),
Colin Cross0c461f12016-10-20 16:11:43 -070098 )
99
Colin Cross38f794e2017-09-07 10:53:07 -0700100 flags.protoFlags = android.ProtoFlags(ctx, p)
Colin Cross5ff51b52017-05-02 13:34:32 -0700101
Joe Onorato09e94ab2017-11-18 18:23:14 -0800102 if proptools.String(p.Proto.Type) == "lite" {
103 flags.protoOutParams = []string{"lite"}
104 }
105
Colin Cross0c461f12016-10-20 16:11:43 -0700106 return flags
107}