blob: 6887900c4848e1e6755104926afb5cd5df102004 [file] [log] [blame]
Colin Cross38f794e2017-09-07 10:53:07 -07001// Copyright 2017 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 android
16
Colin Cross19878da2019-03-28 14:45:07 -070017import (
Spandan Dasc53767e2023-08-03 23:02:26 +000018 "path/filepath"
Colin Cross19878da2019-03-28 14:45:07 -070019 "strings"
20
Yu Liu2d136142022-08-18 14:46:13 -070021 "android/soong/bazel"
22
Colin Crossfe17f6f2019-03-28 19:30:56 -070023 "github.com/google/blueprint"
Colin Cross19878da2019-03-28 14:45:07 -070024 "github.com/google/blueprint/proptools"
25)
26
Liz Kammer12615db2021-09-28 09:19:17 -040027const (
28 canonicalPathFromRootDefault = true
29)
30
Colin Cross38f794e2017-09-07 10:53:07 -070031// TODO(ccross): protos are often used to communicate between multiple modules. If the only
32// way to convert a proto to source is to reference it as a source file, and external modules cannot
33// reference source files in other modules, then every module that owns a proto file will need to
34// export a library for every type of external user (lite vs. full, c vs. c++ vs. java). It would
35// be better to support a proto module type that exported a proto file along with some include dirs,
36// and then external modules could depend on the proto module but use their own settings to
37// generate the source.
38
Colin Cross19878da2019-03-28 14:45:07 -070039type ProtoFlags struct {
40 Flags []string
41 CanonicalPathFromRoot bool
42 Dir ModuleGenPath
43 SubDir ModuleGenPath
44 OutTypeFlag string
45 OutParams []string
Colin Crossfe17f6f2019-03-28 19:30:56 -070046 Deps Paths
47}
48
49type protoDependencyTag struct {
50 blueprint.BaseDependencyTag
51 name string
52}
53
54var ProtoPluginDepTag = protoDependencyTag{name: "plugin"}
55
56func ProtoDeps(ctx BottomUpMutatorContext, p *ProtoProperties) {
57 if String(p.Proto.Plugin) != "" && String(p.Proto.Type) != "" {
58 ctx.ModuleErrorf("only one of proto.type and proto.plugin can be specified.")
59 }
60
61 if plugin := String(p.Proto.Plugin); plugin != "" {
Colin Cross0f7d2ef2019-10-16 11:03:10 -070062 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(),
63 ProtoPluginDepTag, "protoc-gen-"+plugin)
Colin Crossfe17f6f2019-03-28 19:30:56 -070064 }
Colin Cross19878da2019-03-28 14:45:07 -070065}
Colin Crossa3b25002017-12-15 13:41:30 -080066
Colin Cross19878da2019-03-28 14:45:07 -070067func GetProtoFlags(ctx ModuleContext, p *ProtoProperties) ProtoFlags {
Colin Crossfe17f6f2019-03-28 19:30:56 -070068 var flags []string
69 var deps Paths
70
Colin Cross38f794e2017-09-07 10:53:07 -070071 if len(p.Proto.Local_include_dirs) > 0 {
72 localProtoIncludeDirs := PathsForModuleSrc(ctx, p.Proto.Local_include_dirs)
Colin Crossfe17f6f2019-03-28 19:30:56 -070073 flags = append(flags, JoinWithPrefix(localProtoIncludeDirs.Strings(), "-I"))
Colin Cross38f794e2017-09-07 10:53:07 -070074 }
75 if len(p.Proto.Include_dirs) > 0 {
76 rootProtoIncludeDirs := PathsForSource(ctx, p.Proto.Include_dirs)
Colin Crossfe17f6f2019-03-28 19:30:56 -070077 flags = append(flags, JoinWithPrefix(rootProtoIncludeDirs.Strings(), "-I"))
78 }
79
80 ctx.VisitDirectDepsWithTag(ProtoPluginDepTag, func(dep Module) {
81 if hostTool, ok := dep.(HostToolProvider); !ok || !hostTool.HostToolPath().Valid() {
82 ctx.PropertyErrorf("proto.plugin", "module %q is not a host tool provider",
83 ctx.OtherModuleName(dep))
84 } else {
85 plugin := String(p.Proto.Plugin)
86 deps = append(deps, hostTool.HostToolPath().Path())
87 flags = append(flags, "--plugin=protoc-gen-"+plugin+"="+hostTool.HostToolPath().String())
88 }
89 })
90
91 var protoOutFlag string
92 if plugin := String(p.Proto.Plugin); plugin != "" {
93 protoOutFlag = "--" + plugin + "_out"
Colin Cross38f794e2017-09-07 10:53:07 -070094 }
95
Colin Cross19878da2019-03-28 14:45:07 -070096 return ProtoFlags{
Colin Crossfe17f6f2019-03-28 19:30:56 -070097 Flags: flags,
98 Deps: deps,
99 OutTypeFlag: protoOutFlag,
Liz Kammer12615db2021-09-28 09:19:17 -0400100 CanonicalPathFromRoot: proptools.BoolDefault(p.Proto.Canonical_path_from_root, canonicalPathFromRootDefault),
Colin Cross19878da2019-03-28 14:45:07 -0700101 Dir: PathForModuleGen(ctx, "proto"),
102 SubDir: PathForModuleGen(ctx, "proto", ctx.ModuleDir()),
Dan Willemsenab9f4262018-02-14 13:58:34 -0800103 }
Colin Cross38f794e2017-09-07 10:53:07 -0700104}
105
106type ProtoProperties struct {
107 Proto struct {
108 // Proto generator type. C++: full or lite. Java: micro, nano, stream, or lite.
109 Type *string `android:"arch_variant"`
110
Colin Crossfe17f6f2019-03-28 19:30:56 -0700111 // Proto plugin to use as the generator. Must be a cc_binary_host module.
112 Plugin *string `android:"arch_variant"`
113
Colin Cross38f794e2017-09-07 10:53:07 -0700114 // list of directories that will be added to the protoc include paths.
115 Include_dirs []string
116
117 // list of directories relative to the bp file that will
118 // be added to the protoc include paths.
119 Local_include_dirs []string
Dan Willemsenab9f4262018-02-14 13:58:34 -0800120
121 // whether to identify the proto files from the root of the
122 // source tree (the original method in Android, useful for
123 // android-specific protos), or relative from where they were
124 // specified (useful for external/third party protos).
125 //
126 // This defaults to true today, but is expected to default to
127 // false in the future.
128 Canonical_path_from_root *bool
Colin Cross38f794e2017-09-07 10:53:07 -0700129 } `android:"arch_variant"`
130}
Colin Cross19878da2019-03-28 14:45:07 -0700131
Colin Crossf1a035e2020-11-16 17:32:30 -0800132func ProtoRule(rule *RuleBuilder, protoFile Path, flags ProtoFlags, deps Paths,
Colin Cross19878da2019-03-28 14:45:07 -0700133 outDir WritablePath, depFile WritablePath, outputs WritablePaths) {
134
135 var protoBase string
136 if flags.CanonicalPathFromRoot {
137 protoBase = "."
138 } else {
139 rel := protoFile.Rel()
140 protoBase = strings.TrimSuffix(protoFile.String(), rel)
141 }
142
143 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800144 BuiltTool("aprotoc").
Colin Cross19878da2019-03-28 14:45:07 -0700145 FlagWithArg(flags.OutTypeFlag+"=", strings.Join(flags.OutParams, ",")+":"+outDir.String()).
146 FlagWithDepFile("--dependency_out=", depFile).
147 FlagWithArg("-I ", protoBase).
148 Flags(flags.Flags).
149 Input(protoFile).
150 Implicits(deps).
151 ImplicitOutputs(outputs)
152
153 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800154 BuiltTool("dep_fixer").Flag(depFile.String())
Colin Cross19878da2019-03-28 14:45:07 -0700155}
Liz Kammer12615db2021-09-28 09:19:17 -0400156
157// Bp2buildProtoInfo contains information necessary to pass on to language specific conversion.
158type Bp2buildProtoInfo struct {
Spandan Dasec39d512023-08-15 22:08:18 +0000159 Type *string
160 Proto_libs bazel.LabelList
161 Transitive_proto_libs bazel.LabelList
Liz Kammer12615db2021-09-28 09:19:17 -0400162}
163
Yu Liu2aa806b2022-09-01 11:54:47 -0700164type ProtoAttrs struct {
Liz Kammer12615db2021-09-28 09:19:17 -0400165 Srcs bazel.LabelListAttribute
Spandan Dasc53767e2023-08-03 23:02:26 +0000166 Import_prefix *string
Liz Kammer12615db2021-09-28 09:19:17 -0400167 Strip_import_prefix *string
Yu Liu2d136142022-08-18 14:46:13 -0700168 Deps bazel.LabelListAttribute
169}
170
171// For each package in the include_dirs property a proto_library target should
172// be added to the BUILD file in that package and a mapping should be added here
173var includeDirsToProtoDeps = map[string]string{
174 "external/protobuf/src": "//external/protobuf:libprotobuf-proto",
Liz Kammer12615db2021-09-28 09:19:17 -0400175}
176
Spandan Dasc53767e2023-08-03 23:02:26 +0000177// Partitions srcs by the pkg it is in
178// srcs has been created using `TransformSubpackagePaths`
179// This function uses existence of Android.bp/BUILD files to create a label that is compatible with the package structure of bp2build workspace
180func partitionSrcsByPackage(currentDir string, srcs bazel.LabelList) map[string]bazel.LabelList {
181 getPackageFromLabel := func(label string) string {
182 // Remove any preceding //
183 label = strings.TrimPrefix(label, "//")
184 split := strings.Split(label, ":")
185 if len(split) == 1 {
186 // e.g. foo.proto
187 return currentDir
188 } else if split[0] == "" {
189 // e.g. :foo.proto
190 return currentDir
191 } else {
192 return split[0]
193 }
194 }
195
196 pkgToSrcs := map[string]bazel.LabelList{}
197 for _, src := range srcs.Includes {
198 pkg := getPackageFromLabel(src.Label)
199 list := pkgToSrcs[pkg]
200 list.Add(&src)
201 pkgToSrcs[pkg] = list
202 }
203 return pkgToSrcs
204}
205
Liz Kammer12615db2021-09-28 09:19:17 -0400206// Bp2buildProtoProperties converts proto properties, creating a proto_library and returning the
207// information necessary for language-specific handling.
Sam Delmericoc7681022022-02-04 21:01:20 +0000208func Bp2buildProtoProperties(ctx Bp2buildMutatorContext, m *ModuleBase, srcs bazel.LabelListAttribute) (Bp2buildProtoInfo, bool) {
Liz Kammer12615db2021-09-28 09:19:17 -0400209 var info Bp2buildProtoInfo
210 if srcs.IsEmpty() {
211 return info, false
212 }
Liz Kammer12615db2021-09-28 09:19:17 -0400213
Yu Liu2aa806b2022-09-01 11:54:47 -0700214 var protoLibraries bazel.LabelList
Spandan Dasec39d512023-08-15 22:08:18 +0000215 var transitiveProtoLibraries bazel.LabelList
Yu Liu2aa806b2022-09-01 11:54:47 -0700216 var directProtoSrcs bazel.LabelList
Liz Kammer12615db2021-09-28 09:19:17 -0400217
Yu Liu2aa806b2022-09-01 11:54:47 -0700218 // For filegroups that should be converted to proto_library just collect the
219 // labels of converted proto_library targets.
220 for _, protoSrc := range srcs.Value.Includes {
221 src := protoSrc.OriginalModuleName
222 if fg, ok := ToFileGroupAsLibrary(ctx, src); ok &&
223 fg.ShouldConvertToProtoLibrary(ctx) {
224 protoLibraries.Add(&bazel.Label{
225 Label: fg.GetProtoLibraryLabel(ctx),
226 })
227 } else {
228 directProtoSrcs.Add(&protoSrc)
Liz Kammer12615db2021-09-28 09:19:17 -0400229 }
230 }
231
Spandan Dasc53767e2023-08-03 23:02:26 +0000232 name := m.Name() + "_proto"
233
234 depsFromFilegroup := protoLibraries
Liz Kammer7dc6bcb2023-08-10 12:51:59 -0400235 var canonicalPathFromRoot bool
Yu Liu2aa806b2022-09-01 11:54:47 -0700236
237 if len(directProtoSrcs.Includes) > 0 {
Spandan Dasc53767e2023-08-03 23:02:26 +0000238 pkgToSrcs := partitionSrcsByPackage(ctx.ModuleDir(), directProtoSrcs)
Spandan Dasec39d512023-08-15 22:08:18 +0000239 protoIncludeDirs := []string{}
Spandan Dasc53767e2023-08-03 23:02:26 +0000240 for _, pkg := range SortedStringKeys(pkgToSrcs) {
241 srcs := pkgToSrcs[pkg]
242 attrs := ProtoAttrs{
243 Srcs: bazel.MakeLabelListAttribute(srcs),
244 }
245 attrs.Deps.Append(bazel.MakeLabelListAttribute(depsFromFilegroup))
Yu Liu2aa806b2022-09-01 11:54:47 -0700246
Spandan Dasc53767e2023-08-03 23:02:26 +0000247 for axis, configToProps := range m.GetArchVariantProperties(ctx, &ProtoProperties{}) {
248 for _, rawProps := range configToProps {
249 var props *ProtoProperties
250 var ok bool
251 if props, ok = rawProps.(*ProtoProperties); !ok {
252 ctx.ModuleErrorf("Could not cast ProtoProperties to expected type")
Yu Liu2aa806b2022-09-01 11:54:47 -0700253 }
Spandan Dasc53767e2023-08-03 23:02:26 +0000254 if axis == bazel.NoConfigAxis {
255 info.Type = props.Proto.Type
Yu Liu2aa806b2022-09-01 11:54:47 -0700256
Liz Kammer7dc6bcb2023-08-10 12:51:59 -0400257 canonicalPathFromRoot = proptools.BoolDefault(props.Proto.Canonical_path_from_root, canonicalPathFromRootDefault)
258 if !canonicalPathFromRoot {
Spandan Dasc53767e2023-08-03 23:02:26 +0000259 // an empty string indicates to strips the package path
260 path := ""
261 attrs.Strip_import_prefix = &path
Yu Liu2aa806b2022-09-01 11:54:47 -0700262 }
Spandan Dasc53767e2023-08-03 23:02:26 +0000263
264 for _, dir := range props.Proto.Include_dirs {
265 if dep, ok := includeDirsToProtoDeps[dir]; ok {
266 attrs.Deps.Add(bazel.MakeLabelAttribute(dep))
267 } else {
Spandan Dasec39d512023-08-15 22:08:18 +0000268 protoIncludeDirs = append(protoIncludeDirs, dir)
Spandan Dasc53767e2023-08-03 23:02:26 +0000269 }
270 }
Spandan Das4e5a1942023-08-22 19:20:39 +0000271
272 // proto.local_include_dirs are similar to proto.include_dirs, except that it is relative to the module directory
273 for _, dir := range props.Proto.Local_include_dirs {
274 relativeToTop := pathForModuleSrc(ctx, dir).String()
275 protoIncludeDirs = append(protoIncludeDirs, relativeToTop)
276 }
277
Spandan Dasc53767e2023-08-03 23:02:26 +0000278 } else if props.Proto.Type != info.Type && props.Proto.Type != nil {
279 ctx.ModuleErrorf("Cannot handle arch-variant types for protos at this time.")
Yu Liu2aa806b2022-09-01 11:54:47 -0700280 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700281 }
282 }
Spandan Dasc53767e2023-08-03 23:02:26 +0000283
284 tags := ApexAvailableTagsWithoutTestApexes(ctx.(TopDownMutatorContext), ctx.Module())
285
Liz Kammer7dc6bcb2023-08-10 12:51:59 -0400286 moduleDir := ctx.ModuleDir()
287 if !canonicalPathFromRoot {
288 // Since we are creating the proto_library in a subpackage, set the import_prefix relative to the current package
289 if rel, err := filepath.Rel(moduleDir, pkg); err != nil {
290 ctx.ModuleErrorf("Could not get relative path for %v %v", pkg, err)
291 } else if rel != "." {
292 attrs.Import_prefix = &rel
293 }
Spandan Dasc53767e2023-08-03 23:02:26 +0000294 }
295
Spandan Das215adb42023-08-14 16:52:24 +0000296 // TODO - b/246997908: Handle potential orphaned proto_library targets
297 // To create proto_library targets in the same package, we split the .proto files
298 // This means that if a proto_library in a subpackage imports another proto_library from the parent package
299 // (or a different subpackage), it will not find it.
300 // The CcProtoGen action itself runs fine because we construct the correct ProtoInfo,
301 // but the FileDescriptorSet of each proto_library might not be compile-able
Spandan Dase0f2ed52023-08-22 18:19:44 +0000302 //
303 // Add manual tag if either
304 // 1. .proto files are in more than one package
305 // 2. proto.include_dirs is not empty
306 if len(SortedStringKeys(pkgToSrcs)) > 1 || len(protoIncludeDirs) > 0 {
Spandan Das215adb42023-08-14 16:52:24 +0000307 tags.Append(bazel.MakeStringListAttribute([]string{"manual"}))
308 }
Spandan Dase0f2ed52023-08-22 18:19:44 +0000309
Spandan Dasc53767e2023-08-03 23:02:26 +0000310 ctx.CreateBazelTargetModule(
311 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
312 CommonAttributes{Name: name, Dir: proptools.StringPtr(pkg), Tags: tags},
313 &attrs,
314 )
315
316 l := ""
Liz Kammer7dc6bcb2023-08-10 12:51:59 -0400317 if pkg == moduleDir { // same package that the original module lives in
Spandan Dasc53767e2023-08-03 23:02:26 +0000318 l = ":" + name
319 } else {
320 l = "//" + pkg + ":" + name
321 }
322 protoLibraries.Add(&bazel.Label{
323 Label: l,
324 })
Yu Liu2aa806b2022-09-01 11:54:47 -0700325 }
Spandan Dasec39d512023-08-15 22:08:18 +0000326 protoLibrariesInIncludeDir := createProtoLibraryTargetsForIncludeDirs(ctx, protoIncludeDirs)
327 transitiveProtoLibraries.Append(protoLibrariesInIncludeDir)
Yu Liu2aa806b2022-09-01 11:54:47 -0700328 }
329
330 info.Proto_libs = protoLibraries
Spandan Dasec39d512023-08-15 22:08:18 +0000331 info.Transitive_proto_libs = transitiveProtoLibraries
Liz Kammer12615db2021-09-28 09:19:17 -0400332
333 return info, true
334}
Spandan Dasec39d512023-08-15 22:08:18 +0000335
336var (
337 protoIncludeDirGeneratedSuffix = ".include_dir_bp2build_generated_proto"
338 protoIncludeDirsBp2buildKey = NewOnceKey("protoIncludeDirsBp2build")
339)
340
341func getProtoIncludeDirsBp2build(config Config) *map[protoIncludeDirKey]bool {
342 return config.Once(protoIncludeDirsBp2buildKey, func() interface{} {
343 return &map[protoIncludeDirKey]bool{}
344 }).(*map[protoIncludeDirKey]bool)
345}
346
347// key for dynamically creating proto_library per proto.include_dirs
348type protoIncludeDirKey struct {
349 dir string
350 subpackgeInDir string
351}
352
353// createProtoLibraryTargetsForIncludeDirs creates additional proto_library targets for .proto files in includeDirs
354// Since Bazel imposes a constratint that the proto_library must be in the same package as the .proto file, this function
355// might create the targets in a subdirectory of `includeDir`
356// Returns the labels of the proto_library targets
357func createProtoLibraryTargetsForIncludeDirs(ctx Bp2buildMutatorContext, includeDirs []string) bazel.LabelList {
358 var ret bazel.LabelList
359 for _, dir := range includeDirs {
360 if exists, _, _ := ctx.Config().fs.Exists(filepath.Join(dir, "Android.bp")); !exists {
361 ctx.ModuleErrorf("TODO: Add support for proto.include_dir: %v. This directory does not contain an Android.bp file", dir)
362 }
363 dirMap := getProtoIncludeDirsBp2build(ctx.Config())
364 // Find all proto file targets in this dir
365 protoLabelsInDir := BazelLabelForSrcPatternExcludes(ctx, dir, "**/*.proto", []string{})
366 // Partition the labels by package and subpackage(s)
367 protoLabelelsPartitionedByPkg := partitionSrcsByPackage(dir, protoLabelsInDir)
368 for _, pkg := range SortedStringKeys(protoLabelelsPartitionedByPkg) {
369 label := strings.ReplaceAll(dir, "/", ".") + protoIncludeDirGeneratedSuffix
370 ret.Add(&bazel.Label{
371 Label: "//" + pkg + ":" + label,
372 })
373 key := protoIncludeDirKey{dir: dir, subpackgeInDir: pkg}
374 if _, exists := (*dirMap)[key]; exists {
375 // A proto_library has already been created for this package relative to this include dir
376 continue
377 }
378 (*dirMap)[key] = true
379 srcs := protoLabelelsPartitionedByPkg[pkg]
380 rel, err := filepath.Rel(dir, pkg)
381 if err != nil {
382 ctx.ModuleErrorf("Could not create a proto_library in pkg %v due to %v\n", pkg, err)
383 }
384 // Create proto_library
385 attrs := ProtoAttrs{
386 Srcs: bazel.MakeLabelListAttribute(srcs),
387 Strip_import_prefix: proptools.StringPtr(""),
388 }
389 if rel != "." {
390 attrs.Import_prefix = proptools.StringPtr(rel)
391 }
392 ctx.CreateBazelTargetModule(
393 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
394 CommonAttributes{
395 Name: label,
396 Dir: proptools.StringPtr(pkg),
397 // This proto_library is used to construct a ProtoInfo
398 // But it might not be buildable on its own
399 Tags: bazel.MakeStringListAttribute([]string{"manual"}),
400 },
401 &attrs,
402 )
403 }
404 }
405 return ret
406}