blob: 4d72f26655130715efa6b5b0b3320d2759df855a [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"
Liz Kammer12615db2021-09-28 09:19:17 -040019 "github.com/google/blueprint/proptools"
Colin Cross0c461f12016-10-20 16:11:43 -070020
21 "android/soong/android"
Liz Kammer12615db2021-09-28 09:19:17 -040022)
23
24const (
25 protoTypeDefault = "lite"
Colin Cross0c461f12016-10-20 16:11:43 -070026)
27
Colin Cross6af17aa2017-09-20 12:59:05 -070028// genProto creates a rule to convert a .proto file to generated .pb.cc and .pb.h files and returns
29// the paths to the generated files.
Colin Cross19878da2019-03-28 14:45:07 -070030func genProto(ctx android.ModuleContext, protoFile android.Path, flags builderFlags) (cc, header android.WritablePath) {
31 var ccFile, headerFile android.ModuleGenPath
Dan Willemsen60e62f02018-11-16 21:05:32 -080032
33 srcSuffix := ".cc"
34 if flags.protoC {
35 srcSuffix = ".c"
36 }
Colin Cross6af17aa2017-09-20 12:59:05 -070037
Colin Cross19878da2019-03-28 14:45:07 -070038 if flags.proto.CanonicalPathFromRoot {
Dan Willemsen60e62f02018-11-16 21:05:32 -080039 ccFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb"+srcSuffix)
Dan Willemsenab9f4262018-02-14 13:58:34 -080040 headerFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.h")
41 } else {
42 rel := protoFile.Rel()
Dan Willemsen60e62f02018-11-16 21:05:32 -080043 ccFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb"+srcSuffix))
Dan Willemsenab9f4262018-02-14 13:58:34 -080044 headerFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb.h"))
45 }
Colin Cross6af17aa2017-09-20 12:59:05 -070046
Colin Crossfe17f6f2019-03-28 19:30:56 -070047 protoDeps := flags.proto.Deps
Dan Willemsen60e62f02018-11-16 21:05:32 -080048 if flags.protoOptionsFile {
49 optionsFile := pathtools.ReplaceExtension(protoFile.String(), "options")
Colin Cross19878da2019-03-28 14:45:07 -070050 optionsPath := android.PathForSource(ctx, optionsFile)
51 protoDeps = append(android.Paths{optionsPath}, protoDeps...)
Dan Willemsen60e62f02018-11-16 21:05:32 -080052 }
53
Colin Cross19878da2019-03-28 14:45:07 -070054 outDir := flags.proto.Dir
55 depFile := ccFile.ReplaceExtension(ctx, "d")
56 outputs := android.WritablePaths{ccFile, headerFile}
57
Colin Crossf1a035e2020-11-16 17:32:30 -080058 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross19878da2019-03-28 14:45:07 -070059
Colin Crossf1a035e2020-11-16 17:32:30 -080060 android.ProtoRule(rule, protoFile, flags.proto, protoDeps, outDir, depFile, outputs)
Colin Cross19878da2019-03-28 14:45:07 -070061
Colin Crossf1a035e2020-11-16 17:32:30 -080062 rule.Build("protoc_"+protoFile.Rel(), "protoc "+protoFile.Rel())
Colin Cross6af17aa2017-09-20 12:59:05 -070063
64 return ccFile, headerFile
65}
66
Colin Crossfe17f6f2019-03-28 19:30:56 -070067func protoDeps(ctx DepsContext, deps Deps, p *android.ProtoProperties, static bool) Deps {
Colin Cross0c461f12016-10-20 16:11:43 -070068 var lib string
Colin Cross0c461f12016-10-20 16:11:43 -070069
Colin Crossfe17f6f2019-03-28 19:30:56 -070070 if String(p.Proto.Plugin) == "" {
Liz Kammer12615db2021-09-28 09:19:17 -040071 switch proptools.StringDefault(p.Proto.Type, protoTypeDefault) {
Colin Crossfe17f6f2019-03-28 19:30:56 -070072 case "full":
73 if ctx.useSdk() {
74 lib = "libprotobuf-cpp-full-ndk"
75 static = true
76 } else {
77 lib = "libprotobuf-cpp-full"
78 }
Liz Kammer12615db2021-09-28 09:19:17 -040079 case "lite":
Colin Crossfe17f6f2019-03-28 19:30:56 -070080 if ctx.useSdk() {
81 lib = "libprotobuf-cpp-lite-ndk"
82 static = true
83 } else {
84 lib = "libprotobuf-cpp-lite"
85 }
86 case "nanopb-c":
87 lib = "libprotobuf-c-nano"
Colin Cross0c461f12016-10-20 16:11:43 -070088 static = true
Colin Crossfe17f6f2019-03-28 19:30:56 -070089 case "nanopb-c-enable_malloc":
90 lib = "libprotobuf-c-nano-enable_malloc"
Colin Cross0c461f12016-10-20 16:11:43 -070091 static = true
Colin Crossfe17f6f2019-03-28 19:30:56 -070092 case "nanopb-c-16bit":
93 lib = "libprotobuf-c-nano-16bit"
94 static = true
95 case "nanopb-c-enable_malloc-16bit":
96 lib = "libprotobuf-c-nano-enable_malloc-16bit"
97 static = true
98 case "nanopb-c-32bit":
99 lib = "libprotobuf-c-nano-32bit"
100 static = true
101 case "nanopb-c-enable_malloc-32bit":
102 lib = "libprotobuf-c-nano-enable_malloc-32bit"
103 static = true
104 default:
105 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
106 String(p.Proto.Type))
Colin Cross0c461f12016-10-20 16:11:43 -0700107 }
Colin Cross0c461f12016-10-20 16:11:43 -0700108
Colin Crossfe17f6f2019-03-28 19:30:56 -0700109 if static {
110 deps.StaticLibs = append(deps.StaticLibs, lib)
111 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib)
112 } else {
113 deps.SharedLibs = append(deps.SharedLibs, lib)
114 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib)
115 }
Colin Cross0c461f12016-10-20 16:11:43 -0700116 }
117
118 return deps
119}
120
Colin Cross38f794e2017-09-07 10:53:07 -0700121func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags {
Colin Cross4af21ed2019-11-04 09:37:55 -0800122 flags.Local.CFlags = append(flags.Local.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI")
Dan Willemsenab9f4262018-02-14 13:58:34 -0800123
Colin Cross19878da2019-03-28 14:45:07 -0700124 flags.proto = android.GetProtoFlags(ctx, p)
125 if flags.proto.CanonicalPathFromRoot {
Colin Cross4af21ed2019-11-04 09:37:55 -0800126 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+flags.proto.SubDir.String())
Dan Willemsenab9f4262018-02-14 13:58:34 -0800127 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800128 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+flags.proto.Dir.String())
Colin Cross5ff51b52017-05-02 13:34:32 -0700129
Colin Crossfe17f6f2019-03-28 19:30:56 -0700130 if String(p.Proto.Plugin) == "" {
131 var plugin string
Dan Willemsen60e62f02018-11-16 21:05:32 -0800132
Colin Crossfe17f6f2019-03-28 19:30:56 -0700133 switch String(p.Proto.Type) {
134 case "nanopb-c", "nanopb-c-enable_malloc", "nanopb-c-16bit", "nanopb-c-enable_malloc-16bit", "nanopb-c-32bit", "nanopb-c-enable_malloc-32bit":
135 flags.protoC = true
136 flags.protoOptionsFile = true
137 flags.proto.OutTypeFlag = "--nanopb_out"
Ulf Adams82fd89b2020-11-25 22:37:22 +0100138 // Disable nanopb timestamps to support remote caching.
139 flags.proto.OutParams = append(flags.proto.OutParams, "-T")
Colin Crossfe17f6f2019-03-28 19:30:56 -0700140 plugin = "protoc-gen-nanopb"
141 case "full":
142 flags.proto.OutTypeFlag = "--cpp_out"
143 case "lite":
144 flags.proto.OutTypeFlag = "--cpp_out"
145 flags.proto.OutParams = append(flags.proto.OutParams, "lite")
146 case "":
147 // TODO(b/119714316): this should be equivalent to "lite" in
148 // order to match protoDeps, but some modules are depending on
149 // this behavior
150 flags.proto.OutTypeFlag = "--cpp_out"
151 default:
152 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
153 String(p.Proto.Type))
154 }
Dan Willemsen60e62f02018-11-16 21:05:32 -0800155
Colin Crossfe17f6f2019-03-28 19:30:56 -0700156 if plugin != "" {
157 path := ctx.Config().HostToolPath(ctx, plugin)
158 flags.proto.Deps = append(flags.proto.Deps, path)
159 flags.proto.Flags = append(flags.proto.Flags, "--plugin="+path.String())
160 }
Joe Onorato09e94ab2017-11-18 18:23:14 -0800161 }
162
Colin Cross0c461f12016-10-20 16:11:43 -0700163 return flags
164}