blob: b9365a70fdf0b12343ec9899182fef8d4b2de3d1 [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
Spandan Dascb847632023-08-22 19:24:07 +0000284 if p, ok := m.module.(PkgPathInterface); ok && p.PkgPath(ctx) != nil {
285 // python_library with pkg_path
286 // proto_library for this module should have the pkg_path as the import_prefix
287 attrs.Import_prefix = p.PkgPath(ctx)
288 attrs.Strip_import_prefix = proptools.StringPtr("")
289 }
290
Spandan Dasc53767e2023-08-03 23:02:26 +0000291 tags := ApexAvailableTagsWithoutTestApexes(ctx.(TopDownMutatorContext), ctx.Module())
292
Liz Kammer7dc6bcb2023-08-10 12:51:59 -0400293 moduleDir := ctx.ModuleDir()
294 if !canonicalPathFromRoot {
295 // Since we are creating the proto_library in a subpackage, set the import_prefix relative to the current package
296 if rel, err := filepath.Rel(moduleDir, pkg); err != nil {
297 ctx.ModuleErrorf("Could not get relative path for %v %v", pkg, err)
298 } else if rel != "." {
299 attrs.Import_prefix = &rel
300 }
Spandan Dasc53767e2023-08-03 23:02:26 +0000301 }
302
Spandan Das215adb42023-08-14 16:52:24 +0000303 // TODO - b/246997908: Handle potential orphaned proto_library targets
304 // To create proto_library targets in the same package, we split the .proto files
305 // This means that if a proto_library in a subpackage imports another proto_library from the parent package
306 // (or a different subpackage), it will not find it.
307 // The CcProtoGen action itself runs fine because we construct the correct ProtoInfo,
308 // but the FileDescriptorSet of each proto_library might not be compile-able
Spandan Dase0f2ed52023-08-22 18:19:44 +0000309 //
310 // Add manual tag if either
311 // 1. .proto files are in more than one package
312 // 2. proto.include_dirs is not empty
313 if len(SortedStringKeys(pkgToSrcs)) > 1 || len(protoIncludeDirs) > 0 {
Spandan Das215adb42023-08-14 16:52:24 +0000314 tags.Append(bazel.MakeStringListAttribute([]string{"manual"}))
315 }
Spandan Dase0f2ed52023-08-22 18:19:44 +0000316
Spandan Dasc53767e2023-08-03 23:02:26 +0000317 ctx.CreateBazelTargetModule(
318 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
319 CommonAttributes{Name: name, Dir: proptools.StringPtr(pkg), Tags: tags},
320 &attrs,
321 )
322
323 l := ""
Liz Kammer7dc6bcb2023-08-10 12:51:59 -0400324 if pkg == moduleDir { // same package that the original module lives in
Spandan Dasc53767e2023-08-03 23:02:26 +0000325 l = ":" + name
326 } else {
327 l = "//" + pkg + ":" + name
328 }
329 protoLibraries.Add(&bazel.Label{
330 Label: l,
331 })
Yu Liu2aa806b2022-09-01 11:54:47 -0700332 }
Spandan Dasec39d512023-08-15 22:08:18 +0000333 protoLibrariesInIncludeDir := createProtoLibraryTargetsForIncludeDirs(ctx, protoIncludeDirs)
334 transitiveProtoLibraries.Append(protoLibrariesInIncludeDir)
Yu Liu2aa806b2022-09-01 11:54:47 -0700335 }
336
337 info.Proto_libs = protoLibraries
Spandan Dasec39d512023-08-15 22:08:18 +0000338 info.Transitive_proto_libs = transitiveProtoLibraries
Liz Kammer12615db2021-09-28 09:19:17 -0400339
340 return info, true
341}
Spandan Dasec39d512023-08-15 22:08:18 +0000342
Spandan Dascb847632023-08-22 19:24:07 +0000343// PkgPathInterface is used as a type assertion in bp2build to get pkg_path property of python_library_host
344type PkgPathInterface interface {
345 PkgPath(ctx BazelConversionContext) *string
346}
347
Spandan Dasec39d512023-08-15 22:08:18 +0000348var (
349 protoIncludeDirGeneratedSuffix = ".include_dir_bp2build_generated_proto"
350 protoIncludeDirsBp2buildKey = NewOnceKey("protoIncludeDirsBp2build")
351)
352
353func getProtoIncludeDirsBp2build(config Config) *map[protoIncludeDirKey]bool {
354 return config.Once(protoIncludeDirsBp2buildKey, func() interface{} {
355 return &map[protoIncludeDirKey]bool{}
356 }).(*map[protoIncludeDirKey]bool)
357}
358
359// key for dynamically creating proto_library per proto.include_dirs
360type protoIncludeDirKey struct {
361 dir string
362 subpackgeInDir string
363}
364
365// createProtoLibraryTargetsForIncludeDirs creates additional proto_library targets for .proto files in includeDirs
366// Since Bazel imposes a constratint that the proto_library must be in the same package as the .proto file, this function
367// might create the targets in a subdirectory of `includeDir`
368// Returns the labels of the proto_library targets
369func createProtoLibraryTargetsForIncludeDirs(ctx Bp2buildMutatorContext, includeDirs []string) bazel.LabelList {
370 var ret bazel.LabelList
371 for _, dir := range includeDirs {
372 if exists, _, _ := ctx.Config().fs.Exists(filepath.Join(dir, "Android.bp")); !exists {
373 ctx.ModuleErrorf("TODO: Add support for proto.include_dir: %v. This directory does not contain an Android.bp file", dir)
374 }
375 dirMap := getProtoIncludeDirsBp2build(ctx.Config())
376 // Find all proto file targets in this dir
377 protoLabelsInDir := BazelLabelForSrcPatternExcludes(ctx, dir, "**/*.proto", []string{})
378 // Partition the labels by package and subpackage(s)
379 protoLabelelsPartitionedByPkg := partitionSrcsByPackage(dir, protoLabelsInDir)
380 for _, pkg := range SortedStringKeys(protoLabelelsPartitionedByPkg) {
381 label := strings.ReplaceAll(dir, "/", ".") + protoIncludeDirGeneratedSuffix
382 ret.Add(&bazel.Label{
383 Label: "//" + pkg + ":" + label,
384 })
385 key := protoIncludeDirKey{dir: dir, subpackgeInDir: pkg}
386 if _, exists := (*dirMap)[key]; exists {
387 // A proto_library has already been created for this package relative to this include dir
388 continue
389 }
390 (*dirMap)[key] = true
391 srcs := protoLabelelsPartitionedByPkg[pkg]
392 rel, err := filepath.Rel(dir, pkg)
393 if err != nil {
394 ctx.ModuleErrorf("Could not create a proto_library in pkg %v due to %v\n", pkg, err)
395 }
396 // Create proto_library
397 attrs := ProtoAttrs{
398 Srcs: bazel.MakeLabelListAttribute(srcs),
399 Strip_import_prefix: proptools.StringPtr(""),
400 }
401 if rel != "." {
402 attrs.Import_prefix = proptools.StringPtr(rel)
403 }
404 ctx.CreateBazelTargetModule(
405 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
406 CommonAttributes{
407 Name: label,
408 Dir: proptools.StringPtr(pkg),
409 // This proto_library is used to construct a ProtoInfo
410 // But it might not be buildable on its own
411 Tags: bazel.MakeStringListAttribute([]string{"manual"}),
412 },
413 &attrs,
414 )
415 }
416 }
417 return ret
418}