blob: 178d54f7fee8443d3167ed0373af97adc4e1a375 [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 }
271 } else if props.Proto.Type != info.Type && props.Proto.Type != nil {
272 ctx.ModuleErrorf("Cannot handle arch-variant types for protos at this time.")
Yu Liu2aa806b2022-09-01 11:54:47 -0700273 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700274 }
275 }
Spandan Dasc53767e2023-08-03 23:02:26 +0000276
277 tags := ApexAvailableTagsWithoutTestApexes(ctx.(TopDownMutatorContext), ctx.Module())
278
Liz Kammer7dc6bcb2023-08-10 12:51:59 -0400279 moduleDir := ctx.ModuleDir()
280 if !canonicalPathFromRoot {
281 // Since we are creating the proto_library in a subpackage, set the import_prefix relative to the current package
282 if rel, err := filepath.Rel(moduleDir, pkg); err != nil {
283 ctx.ModuleErrorf("Could not get relative path for %v %v", pkg, err)
284 } else if rel != "." {
285 attrs.Import_prefix = &rel
286 }
Spandan Dasc53767e2023-08-03 23:02:26 +0000287 }
288
Spandan Das215adb42023-08-14 16:52:24 +0000289 // TODO - b/246997908: Handle potential orphaned proto_library targets
290 // To create proto_library targets in the same package, we split the .proto files
291 // This means that if a proto_library in a subpackage imports another proto_library from the parent package
292 // (or a different subpackage), it will not find it.
293 // The CcProtoGen action itself runs fine because we construct the correct ProtoInfo,
294 // but the FileDescriptorSet of each proto_library might not be compile-able
Spandan Dase0f2ed52023-08-22 18:19:44 +0000295 //
296 // Add manual tag if either
297 // 1. .proto files are in more than one package
298 // 2. proto.include_dirs is not empty
299 if len(SortedStringKeys(pkgToSrcs)) > 1 || len(protoIncludeDirs) > 0 {
Spandan Das215adb42023-08-14 16:52:24 +0000300 tags.Append(bazel.MakeStringListAttribute([]string{"manual"}))
301 }
Spandan Dase0f2ed52023-08-22 18:19:44 +0000302
Spandan Dasc53767e2023-08-03 23:02:26 +0000303 ctx.CreateBazelTargetModule(
304 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
305 CommonAttributes{Name: name, Dir: proptools.StringPtr(pkg), Tags: tags},
306 &attrs,
307 )
308
309 l := ""
Liz Kammer7dc6bcb2023-08-10 12:51:59 -0400310 if pkg == moduleDir { // same package that the original module lives in
Spandan Dasc53767e2023-08-03 23:02:26 +0000311 l = ":" + name
312 } else {
313 l = "//" + pkg + ":" + name
314 }
315 protoLibraries.Add(&bazel.Label{
316 Label: l,
317 })
Yu Liu2aa806b2022-09-01 11:54:47 -0700318 }
Spandan Dasec39d512023-08-15 22:08:18 +0000319 protoLibrariesInIncludeDir := createProtoLibraryTargetsForIncludeDirs(ctx, protoIncludeDirs)
320 transitiveProtoLibraries.Append(protoLibrariesInIncludeDir)
Yu Liu2aa806b2022-09-01 11:54:47 -0700321 }
322
323 info.Proto_libs = protoLibraries
Spandan Dasec39d512023-08-15 22:08:18 +0000324 info.Transitive_proto_libs = transitiveProtoLibraries
Liz Kammer12615db2021-09-28 09:19:17 -0400325
326 return info, true
327}
Spandan Dasec39d512023-08-15 22:08:18 +0000328
329var (
330 protoIncludeDirGeneratedSuffix = ".include_dir_bp2build_generated_proto"
331 protoIncludeDirsBp2buildKey = NewOnceKey("protoIncludeDirsBp2build")
332)
333
334func getProtoIncludeDirsBp2build(config Config) *map[protoIncludeDirKey]bool {
335 return config.Once(protoIncludeDirsBp2buildKey, func() interface{} {
336 return &map[protoIncludeDirKey]bool{}
337 }).(*map[protoIncludeDirKey]bool)
338}
339
340// key for dynamically creating proto_library per proto.include_dirs
341type protoIncludeDirKey struct {
342 dir string
343 subpackgeInDir string
344}
345
346// createProtoLibraryTargetsForIncludeDirs creates additional proto_library targets for .proto files in includeDirs
347// Since Bazel imposes a constratint that the proto_library must be in the same package as the .proto file, this function
348// might create the targets in a subdirectory of `includeDir`
349// Returns the labels of the proto_library targets
350func createProtoLibraryTargetsForIncludeDirs(ctx Bp2buildMutatorContext, includeDirs []string) bazel.LabelList {
351 var ret bazel.LabelList
352 for _, dir := range includeDirs {
353 if exists, _, _ := ctx.Config().fs.Exists(filepath.Join(dir, "Android.bp")); !exists {
354 ctx.ModuleErrorf("TODO: Add support for proto.include_dir: %v. This directory does not contain an Android.bp file", dir)
355 }
356 dirMap := getProtoIncludeDirsBp2build(ctx.Config())
357 // Find all proto file targets in this dir
358 protoLabelsInDir := BazelLabelForSrcPatternExcludes(ctx, dir, "**/*.proto", []string{})
359 // Partition the labels by package and subpackage(s)
360 protoLabelelsPartitionedByPkg := partitionSrcsByPackage(dir, protoLabelsInDir)
361 for _, pkg := range SortedStringKeys(protoLabelelsPartitionedByPkg) {
362 label := strings.ReplaceAll(dir, "/", ".") + protoIncludeDirGeneratedSuffix
363 ret.Add(&bazel.Label{
364 Label: "//" + pkg + ":" + label,
365 })
366 key := protoIncludeDirKey{dir: dir, subpackgeInDir: pkg}
367 if _, exists := (*dirMap)[key]; exists {
368 // A proto_library has already been created for this package relative to this include dir
369 continue
370 }
371 (*dirMap)[key] = true
372 srcs := protoLabelelsPartitionedByPkg[pkg]
373 rel, err := filepath.Rel(dir, pkg)
374 if err != nil {
375 ctx.ModuleErrorf("Could not create a proto_library in pkg %v due to %v\n", pkg, err)
376 }
377 // Create proto_library
378 attrs := ProtoAttrs{
379 Srcs: bazel.MakeLabelListAttribute(srcs),
380 Strip_import_prefix: proptools.StringPtr(""),
381 }
382 if rel != "." {
383 attrs.Import_prefix = proptools.StringPtr(rel)
384 }
385 ctx.CreateBazelTargetModule(
386 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
387 CommonAttributes{
388 Name: label,
389 Dir: proptools.StringPtr(pkg),
390 // This proto_library is used to construct a ProtoInfo
391 // But it might not be buildable on its own
392 Tags: bazel.MakeStringListAttribute([]string{"manual"}),
393 },
394 &attrs,
395 )
396 }
397 }
398 return ret
399}