blob: 5a96ae78ba74ef165b91695b3371634a96c67b2d [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 "github.com/google/blueprint/pathtools"
Colin Cross0c461f12016-10-20 16:11:43 -070019
20 "android/soong/android"
21)
22
Colin Cross6af17aa2017-09-20 12:59:05 -070023// genProto creates a rule to convert a .proto file to generated .pb.cc and .pb.h files and returns
24// the paths to the generated files.
Colin Cross19878da2019-03-28 14:45:07 -070025func genProto(ctx android.ModuleContext, protoFile android.Path, flags builderFlags) (cc, header android.WritablePath) {
26 var ccFile, headerFile android.ModuleGenPath
Dan Willemsen60e62f02018-11-16 21:05:32 -080027
28 srcSuffix := ".cc"
29 if flags.protoC {
30 srcSuffix = ".c"
31 }
Colin Cross6af17aa2017-09-20 12:59:05 -070032
Colin Cross19878da2019-03-28 14:45:07 -070033 if flags.proto.CanonicalPathFromRoot {
Dan Willemsen60e62f02018-11-16 21:05:32 -080034 ccFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb"+srcSuffix)
Dan Willemsenab9f4262018-02-14 13:58:34 -080035 headerFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.h")
36 } else {
37 rel := protoFile.Rel()
Dan Willemsen60e62f02018-11-16 21:05:32 -080038 ccFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb"+srcSuffix))
Dan Willemsenab9f4262018-02-14 13:58:34 -080039 headerFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb.h"))
40 }
Colin Cross6af17aa2017-09-20 12:59:05 -070041
Dan Willemsen60e62f02018-11-16 21:05:32 -080042 protoDeps := flags.protoDeps
43 if flags.protoOptionsFile {
44 optionsFile := pathtools.ReplaceExtension(protoFile.String(), "options")
Colin Cross19878da2019-03-28 14:45:07 -070045 optionsPath := android.PathForSource(ctx, optionsFile)
46 protoDeps = append(android.Paths{optionsPath}, protoDeps...)
Dan Willemsen60e62f02018-11-16 21:05:32 -080047 }
48
Colin Cross19878da2019-03-28 14:45:07 -070049 outDir := flags.proto.Dir
50 depFile := ccFile.ReplaceExtension(ctx, "d")
51 outputs := android.WritablePaths{ccFile, headerFile}
52
53 rule := android.NewRuleBuilder()
54
55 android.ProtoRule(ctx, rule, protoFile, flags.proto, protoDeps, outDir, depFile, outputs)
56
57 rule.Build(pctx, ctx, "protoc_"+protoFile.Rel(), "protoc "+protoFile.Rel())
Colin Cross6af17aa2017-09-20 12:59:05 -070058
59 return ccFile, headerFile
60}
61
Colin Cross38f794e2017-09-07 10:53:07 -070062func protoDeps(ctx BaseModuleContext, deps Deps, p *android.ProtoProperties, static bool) Deps {
Colin Cross0c461f12016-10-20 16:11:43 -070063 var lib string
Colin Cross0c461f12016-10-20 16:11:43 -070064
Nan Zhang0007d812017-11-07 10:57:05 -080065 switch String(p.Proto.Type) {
Colin Cross0c461f12016-10-20 16:11:43 -070066 case "full":
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070067 if ctx.useSdk() {
Colin Cross0c461f12016-10-20 16:11:43 -070068 lib = "libprotobuf-cpp-full-ndk"
69 static = true
70 } else {
71 lib = "libprotobuf-cpp-full"
72 }
73 case "lite", "":
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070074 if ctx.useSdk() {
Colin Cross0c461f12016-10-20 16:11:43 -070075 lib = "libprotobuf-cpp-lite-ndk"
76 static = true
77 } else {
78 lib = "libprotobuf-cpp-lite"
Colin Cross0c461f12016-10-20 16:11:43 -070079 }
Dan Willemsen60e62f02018-11-16 21:05:32 -080080 case "nanopb-c":
81 lib = "libprotobuf-c-nano"
82 static = true
83 case "nanopb-c-enable_malloc":
84 lib = "libprotobuf-c-nano-enable_malloc"
85 static = true
Yu Shan76dd0052019-01-25 17:08:21 -080086 case "nanopb-c-16bit":
87 lib = "libprotobuf-c-nano-16bit"
88 static = true
89 case "nanopb-c-enable_malloc-16bit":
90 lib = "libprotobuf-c-nano-enable_malloc-16bit"
91 static = true
92 case "nanopb-c-32bit":
93 lib = "libprotobuf-c-nano-32bit"
94 static = true
95 case "nanopb-c-enable_malloc-32bit":
96 lib = "libprotobuf-c-nano-enable_malloc-32bit"
97 static = true
Colin Cross0c461f12016-10-20 16:11:43 -070098 default:
Colin Cross5ff51b52017-05-02 13:34:32 -070099 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
Nan Zhang0007d812017-11-07 10:57:05 -0800100 String(p.Proto.Type))
Colin Cross0c461f12016-10-20 16:11:43 -0700101 }
102
103 if static {
104 deps.StaticLibs = append(deps.StaticLibs, lib)
105 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib)
106 } else {
107 deps.SharedLibs = append(deps.SharedLibs, lib)
108 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib)
109 }
110
111 return deps
112}
113
Colin Cross38f794e2017-09-07 10:53:07 -0700114func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags {
Colin Cross0c461f12016-10-20 16:11:43 -0700115 flags.CFlags = append(flags.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI")
Dan Willemsenab9f4262018-02-14 13:58:34 -0800116
Colin Cross19878da2019-03-28 14:45:07 -0700117 flags.proto = android.GetProtoFlags(ctx, p)
118 if flags.proto.CanonicalPathFromRoot {
119 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+flags.proto.SubDir.String())
Dan Willemsenab9f4262018-02-14 13:58:34 -0800120 }
Colin Cross19878da2019-03-28 14:45:07 -0700121 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+flags.proto.Dir.String())
Colin Cross5ff51b52017-05-02 13:34:32 -0700122
Dan Willemsen60e62f02018-11-16 21:05:32 -0800123 var plugin string
124
125 switch String(p.Proto.Type) {
Yu Shan76dd0052019-01-25 17:08:21 -0800126 case "nanopb-c", "nanopb-c-enable_malloc", "nanopb-c-16bit", "nanopb-c-enable_malloc-16bit", "nanopb-c-32bit", "nanopb-c-enable_malloc-32bit":
Dan Willemsen60e62f02018-11-16 21:05:32 -0800127 flags.protoC = true
128 flags.protoOptionsFile = true
Colin Cross19878da2019-03-28 14:45:07 -0700129 flags.proto.OutTypeFlag = "--nanopb_out"
Dan Willemsen60e62f02018-11-16 21:05:32 -0800130 plugin = "protoc-gen-nanopb"
131 case "full":
Colin Cross19878da2019-03-28 14:45:07 -0700132 flags.proto.OutTypeFlag = "--cpp_out"
Dan Willemsen60e62f02018-11-16 21:05:32 -0800133 case "lite":
Colin Cross19878da2019-03-28 14:45:07 -0700134 flags.proto.OutTypeFlag = "--cpp_out"
135 flags.proto.OutParams = append(flags.proto.OutParams, "lite")
Dan Willemsen60e62f02018-11-16 21:05:32 -0800136 case "":
137 // TODO(b/119714316): this should be equivalent to "lite" in
138 // order to match protoDeps, but some modules are depending on
139 // this behavior
Colin Cross19878da2019-03-28 14:45:07 -0700140 flags.proto.OutTypeFlag = "--cpp_out"
Dan Willemsen60e62f02018-11-16 21:05:32 -0800141 default:
142 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
143 String(p.Proto.Type))
144 }
145
146 if plugin != "" {
147 path := ctx.Config().HostToolPath(ctx, plugin)
148 flags.protoDeps = append(flags.protoDeps, path)
Colin Cross19878da2019-03-28 14:45:07 -0700149 flags.proto.Flags = append(flags.proto.Flags, "--plugin="+path.String())
Joe Onorato09e94ab2017-11-18 18:23:14 -0800150 }
151
Colin Cross0c461f12016-10-20 16:11:43 -0700152 return flags
153}