blob: f818edc9582ff0a6b3d54721b06f829d8154a8d3 [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
Colin Crossfe17f6f2019-03-28 19:30:56 -070042 protoDeps := flags.proto.Deps
Dan Willemsen60e62f02018-11-16 21:05:32 -080043 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 Crossfe17f6f2019-03-28 19:30:56 -070062func protoDeps(ctx DepsContext, 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
Colin Crossfe17f6f2019-03-28 19:30:56 -070065 if String(p.Proto.Plugin) == "" {
66 switch String(p.Proto.Type) {
67 case "full":
68 if ctx.useSdk() {
69 lib = "libprotobuf-cpp-full-ndk"
70 static = true
71 } else {
72 lib = "libprotobuf-cpp-full"
73 }
74 case "lite", "":
75 if ctx.useSdk() {
76 lib = "libprotobuf-cpp-lite-ndk"
77 static = true
78 } else {
79 lib = "libprotobuf-cpp-lite"
80 }
81 case "nanopb-c":
82 lib = "libprotobuf-c-nano"
Colin Cross0c461f12016-10-20 16:11:43 -070083 static = true
Colin Crossfe17f6f2019-03-28 19:30:56 -070084 case "nanopb-c-enable_malloc":
85 lib = "libprotobuf-c-nano-enable_malloc"
Colin Cross0c461f12016-10-20 16:11:43 -070086 static = true
Colin Crossfe17f6f2019-03-28 19:30:56 -070087 case "nanopb-c-16bit":
88 lib = "libprotobuf-c-nano-16bit"
89 static = true
90 case "nanopb-c-enable_malloc-16bit":
91 lib = "libprotobuf-c-nano-enable_malloc-16bit"
92 static = true
93 case "nanopb-c-32bit":
94 lib = "libprotobuf-c-nano-32bit"
95 static = true
96 case "nanopb-c-enable_malloc-32bit":
97 lib = "libprotobuf-c-nano-enable_malloc-32bit"
98 static = true
99 default:
100 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
101 String(p.Proto.Type))
Colin Cross0c461f12016-10-20 16:11:43 -0700102 }
Colin Cross0c461f12016-10-20 16:11:43 -0700103
Colin Crossfe17f6f2019-03-28 19:30:56 -0700104 if static {
105 deps.StaticLibs = append(deps.StaticLibs, lib)
106 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib)
107 } else {
108 deps.SharedLibs = append(deps.SharedLibs, lib)
109 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib)
110 }
Colin Cross0c461f12016-10-20 16:11:43 -0700111 }
112
113 return deps
114}
115
Colin Cross38f794e2017-09-07 10:53:07 -0700116func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags {
Colin Cross0c461f12016-10-20 16:11:43 -0700117 flags.CFlags = append(flags.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI")
Dan Willemsenab9f4262018-02-14 13:58:34 -0800118
Colin Cross19878da2019-03-28 14:45:07 -0700119 flags.proto = android.GetProtoFlags(ctx, p)
120 if flags.proto.CanonicalPathFromRoot {
121 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+flags.proto.SubDir.String())
Dan Willemsenab9f4262018-02-14 13:58:34 -0800122 }
Colin Cross19878da2019-03-28 14:45:07 -0700123 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+flags.proto.Dir.String())
Colin Cross5ff51b52017-05-02 13:34:32 -0700124
Colin Crossfe17f6f2019-03-28 19:30:56 -0700125 if String(p.Proto.Plugin) == "" {
126 var plugin string
Dan Willemsen60e62f02018-11-16 21:05:32 -0800127
Colin Crossfe17f6f2019-03-28 19:30:56 -0700128 switch String(p.Proto.Type) {
129 case "nanopb-c", "nanopb-c-enable_malloc", "nanopb-c-16bit", "nanopb-c-enable_malloc-16bit", "nanopb-c-32bit", "nanopb-c-enable_malloc-32bit":
130 flags.protoC = true
131 flags.protoOptionsFile = true
132 flags.proto.OutTypeFlag = "--nanopb_out"
133 plugin = "protoc-gen-nanopb"
134 case "full":
135 flags.proto.OutTypeFlag = "--cpp_out"
136 case "lite":
137 flags.proto.OutTypeFlag = "--cpp_out"
138 flags.proto.OutParams = append(flags.proto.OutParams, "lite")
139 case "":
140 // TODO(b/119714316): this should be equivalent to "lite" in
141 // order to match protoDeps, but some modules are depending on
142 // this behavior
143 flags.proto.OutTypeFlag = "--cpp_out"
144 default:
145 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
146 String(p.Proto.Type))
147 }
Dan Willemsen60e62f02018-11-16 21:05:32 -0800148
Colin Crossfe17f6f2019-03-28 19:30:56 -0700149 if plugin != "" {
150 path := ctx.Config().HostToolPath(ctx, plugin)
151 flags.proto.Deps = append(flags.proto.Deps, path)
152 flags.proto.Flags = append(flags.proto.Flags, "--plugin="+path.String())
153 }
Joe Onorato09e94ab2017-11-18 18:23:14 -0800154 }
155
Colin Cross0c461f12016-10-20 16:11:43 -0700156 return flags
157}