blob: 6e6f95ee930eb6745adea1cb671fc1fc138a9888 [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 (
Dan Willemsenab9f4262018-02-14 13:58:34 -080018 "strings"
19
Colin Cross6af17aa2017-09-20 12:59:05 -070020 "github.com/google/blueprint"
Dan Willemsenab9f4262018-02-14 13:58:34 -080021 "github.com/google/blueprint/pathtools"
Colin Cross0c461f12016-10-20 16:11:43 -070022
23 "android/soong/android"
24)
25
Colin Cross6af17aa2017-09-20 12:59:05 -070026func init() {
27 pctx.HostBinToolVariable("protocCmd", "aprotoc")
Dan Willemsen43398532018-02-21 02:10:29 -080028 pctx.HostBinToolVariable("depFixCmd", "dep_fixer")
Colin Cross6af17aa2017-09-20 12:59:05 -070029}
30
31var (
32 proto = pctx.AndroidStaticRule("protoc",
33 blueprint.RuleParams{
Dan Willemsen43398532018-02-21 02:10:29 -080034 Command: "$protocCmd --cpp_out=$protoOutParams:$outDir --dependency_out=$out.d -I $protoBase $protoFlags $in && " +
35 `$depFixCmd $out.d`,
36 CommandDeps: []string{"$protocCmd", "$depFixCmd"},
37 Depfile: "${out}.d",
38 Deps: blueprint.DepsGCC,
Dan Willemsenab9f4262018-02-14 13:58:34 -080039 }, "protoFlags", "protoOutParams", "protoBase", "outDir")
Colin Cross6af17aa2017-09-20 12:59:05 -070040)
41
42// genProto creates a rule to convert a .proto file to generated .pb.cc and .pb.h files and returns
43// the paths to the generated files.
44func genProto(ctx android.ModuleContext, protoFile android.Path,
Dan Willemsenab9f4262018-02-14 13:58:34 -080045 protoFlags, protoOutParams string, root bool) (ccFile, headerFile android.WritablePath) {
Colin Cross6af17aa2017-09-20 12:59:05 -070046
Dan Willemsenab9f4262018-02-14 13:58:34 -080047 var protoBase string
48 if root {
49 protoBase = "."
50 ccFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.cc")
51 headerFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.h")
52 } else {
53 rel := protoFile.Rel()
54 protoBase = strings.TrimSuffix(protoFile.String(), rel)
55 ccFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb.cc"))
56 headerFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb.h"))
57 }
Colin Cross6af17aa2017-09-20 12:59:05 -070058
Colin Crossae887032017-10-23 17:16:14 -070059 ctx.Build(pctx, android.BuildParams{
Dan Willemsen43398532018-02-21 02:10:29 -080060 Rule: proto,
61 Description: "protoc " + protoFile.Rel(),
62 Output: ccFile,
63 ImplicitOutput: headerFile,
64 Input: protoFile,
Colin Cross6af17aa2017-09-20 12:59:05 -070065 Args: map[string]string{
Joe Onorato09e94ab2017-11-18 18:23:14 -080066 "outDir": android.ProtoDir(ctx).String(),
67 "protoFlags": protoFlags,
68 "protoOutParams": protoOutParams,
Dan Willemsenab9f4262018-02-14 13:58:34 -080069 "protoBase": protoBase,
Colin Cross6af17aa2017-09-20 12:59:05 -070070 },
71 })
72
73 return ccFile, headerFile
74}
75
Colin Cross38f794e2017-09-07 10:53:07 -070076func protoDeps(ctx BaseModuleContext, deps Deps, p *android.ProtoProperties, static bool) Deps {
Colin Cross0c461f12016-10-20 16:11:43 -070077 var lib string
Colin Cross0c461f12016-10-20 16:11:43 -070078
Nan Zhang0007d812017-11-07 10:57:05 -080079 switch String(p.Proto.Type) {
Colin Cross0c461f12016-10-20 16:11:43 -070080 case "full":
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070081 if ctx.useSdk() {
Colin Cross0c461f12016-10-20 16:11:43 -070082 lib = "libprotobuf-cpp-full-ndk"
83 static = true
84 } else {
85 lib = "libprotobuf-cpp-full"
86 }
87 case "lite", "":
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070088 if ctx.useSdk() {
Colin Cross0c461f12016-10-20 16:11:43 -070089 lib = "libprotobuf-cpp-lite-ndk"
90 static = true
91 } else {
92 lib = "libprotobuf-cpp-lite"
Colin Cross0c461f12016-10-20 16:11:43 -070093 }
94 default:
Colin Cross5ff51b52017-05-02 13:34:32 -070095 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
Nan Zhang0007d812017-11-07 10:57:05 -080096 String(p.Proto.Type))
Colin Cross0c461f12016-10-20 16:11:43 -070097 }
98
99 if static {
100 deps.StaticLibs = append(deps.StaticLibs, lib)
101 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib)
102 } else {
103 deps.SharedLibs = append(deps.SharedLibs, lib)
104 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib)
105 }
106
107 return deps
108}
109
Colin Cross38f794e2017-09-07 10:53:07 -0700110func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags {
Colin Cross0c461f12016-10-20 16:11:43 -0700111 flags.CFlags = append(flags.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI")
Dan Willemsenab9f4262018-02-14 13:58:34 -0800112
113 flags.ProtoRoot = android.ProtoCanonicalPathFromRoot(ctx, p)
114 if flags.ProtoRoot {
115 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.ProtoSubDir(ctx).String())
116 }
117 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.ProtoDir(ctx).String())
Colin Cross0c461f12016-10-20 16:11:43 -0700118
Colin Cross38f794e2017-09-07 10:53:07 -0700119 flags.protoFlags = android.ProtoFlags(ctx, p)
Colin Cross5ff51b52017-05-02 13:34:32 -0700120
Colin Crossff3ae9d2018-04-10 16:15:18 -0700121 if String(p.Proto.Type) == "lite" {
Kweku Adamsfb5b31c2018-04-05 17:48:32 -0700122 flags.protoOutParams = append(flags.protoOutParams, "lite")
Joe Onorato09e94ab2017-11-18 18:23:14 -0800123 }
124
Colin Cross0c461f12016-10-20 16:11:43 -0700125 return flags
126}