blob: aad521ba03c9d93918ddfcd4ad97139bdcb9eab2 [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 {
Yu Liu2aa806b2022-09-01 11:54:47 -0700159 Type *string
Yu Liu2aa806b2022-09-01 11:54:47 -0700160 Proto_libs bazel.LabelList
Liz Kammer12615db2021-09-28 09:19:17 -0400161}
162
Yu Liu2aa806b2022-09-01 11:54:47 -0700163type ProtoAttrs struct {
Liz Kammer12615db2021-09-28 09:19:17 -0400164 Srcs bazel.LabelListAttribute
Spandan Dasc53767e2023-08-03 23:02:26 +0000165 Import_prefix *string
Liz Kammer12615db2021-09-28 09:19:17 -0400166 Strip_import_prefix *string
Yu Liu2d136142022-08-18 14:46:13 -0700167 Deps bazel.LabelListAttribute
168}
169
170// For each package in the include_dirs property a proto_library target should
171// be added to the BUILD file in that package and a mapping should be added here
172var includeDirsToProtoDeps = map[string]string{
173 "external/protobuf/src": "//external/protobuf:libprotobuf-proto",
Liz Kammer12615db2021-09-28 09:19:17 -0400174}
175
Spandan Dasc53767e2023-08-03 23:02:26 +0000176// Partitions srcs by the pkg it is in
177// srcs has been created using `TransformSubpackagePaths`
178// This function uses existence of Android.bp/BUILD files to create a label that is compatible with the package structure of bp2build workspace
179func partitionSrcsByPackage(currentDir string, srcs bazel.LabelList) map[string]bazel.LabelList {
180 getPackageFromLabel := func(label string) string {
181 // Remove any preceding //
182 label = strings.TrimPrefix(label, "//")
183 split := strings.Split(label, ":")
184 if len(split) == 1 {
185 // e.g. foo.proto
186 return currentDir
187 } else if split[0] == "" {
188 // e.g. :foo.proto
189 return currentDir
190 } else {
191 return split[0]
192 }
193 }
194
195 pkgToSrcs := map[string]bazel.LabelList{}
196 for _, src := range srcs.Includes {
197 pkg := getPackageFromLabel(src.Label)
198 list := pkgToSrcs[pkg]
199 list.Add(&src)
200 pkgToSrcs[pkg] = list
201 }
202 return pkgToSrcs
203}
204
Liz Kammer12615db2021-09-28 09:19:17 -0400205// Bp2buildProtoProperties converts proto properties, creating a proto_library and returning the
206// information necessary for language-specific handling.
Sam Delmericoc7681022022-02-04 21:01:20 +0000207func Bp2buildProtoProperties(ctx Bp2buildMutatorContext, m *ModuleBase, srcs bazel.LabelListAttribute) (Bp2buildProtoInfo, bool) {
Liz Kammer12615db2021-09-28 09:19:17 -0400208 var info Bp2buildProtoInfo
209 if srcs.IsEmpty() {
210 return info, false
211 }
Liz Kammer12615db2021-09-28 09:19:17 -0400212
Yu Liu2aa806b2022-09-01 11:54:47 -0700213 var protoLibraries bazel.LabelList
214 var directProtoSrcs bazel.LabelList
Liz Kammer12615db2021-09-28 09:19:17 -0400215
Yu Liu2aa806b2022-09-01 11:54:47 -0700216 // For filegroups that should be converted to proto_library just collect the
217 // labels of converted proto_library targets.
218 for _, protoSrc := range srcs.Value.Includes {
219 src := protoSrc.OriginalModuleName
220 if fg, ok := ToFileGroupAsLibrary(ctx, src); ok &&
221 fg.ShouldConvertToProtoLibrary(ctx) {
222 protoLibraries.Add(&bazel.Label{
223 Label: fg.GetProtoLibraryLabel(ctx),
224 })
225 } else {
226 directProtoSrcs.Add(&protoSrc)
Liz Kammer12615db2021-09-28 09:19:17 -0400227 }
228 }
229
Spandan Dasc53767e2023-08-03 23:02:26 +0000230 name := m.Name() + "_proto"
231
232 depsFromFilegroup := protoLibraries
Yu Liu2aa806b2022-09-01 11:54:47 -0700233
234 if len(directProtoSrcs.Includes) > 0 {
Spandan Dasc53767e2023-08-03 23:02:26 +0000235 pkgToSrcs := partitionSrcsByPackage(ctx.ModuleDir(), directProtoSrcs)
236 for _, pkg := range SortedStringKeys(pkgToSrcs) {
237 srcs := pkgToSrcs[pkg]
238 attrs := ProtoAttrs{
239 Srcs: bazel.MakeLabelListAttribute(srcs),
240 }
241 attrs.Deps.Append(bazel.MakeLabelListAttribute(depsFromFilegroup))
Yu Liu2aa806b2022-09-01 11:54:47 -0700242
Spandan Dasc53767e2023-08-03 23:02:26 +0000243 for axis, configToProps := range m.GetArchVariantProperties(ctx, &ProtoProperties{}) {
244 for _, rawProps := range configToProps {
245 var props *ProtoProperties
246 var ok bool
247 if props, ok = rawProps.(*ProtoProperties); !ok {
248 ctx.ModuleErrorf("Could not cast ProtoProperties to expected type")
Yu Liu2aa806b2022-09-01 11:54:47 -0700249 }
Spandan Dasc53767e2023-08-03 23:02:26 +0000250 if axis == bazel.NoConfigAxis {
251 info.Type = props.Proto.Type
Yu Liu2aa806b2022-09-01 11:54:47 -0700252
Spandan Dasc53767e2023-08-03 23:02:26 +0000253 if !proptools.BoolDefault(props.Proto.Canonical_path_from_root, canonicalPathFromRootDefault) {
254 // an empty string indicates to strips the package path
255 path := ""
256 attrs.Strip_import_prefix = &path
Yu Liu2aa806b2022-09-01 11:54:47 -0700257 }
Spandan Dasc53767e2023-08-03 23:02:26 +0000258
259 for _, dir := range props.Proto.Include_dirs {
260 if dep, ok := includeDirsToProtoDeps[dir]; ok {
261 attrs.Deps.Add(bazel.MakeLabelAttribute(dep))
262 } else {
263 ctx.PropertyErrorf("Could not find the proto_library target for include dir", dir)
264 }
265 }
266 } else if props.Proto.Type != info.Type && props.Proto.Type != nil {
267 ctx.ModuleErrorf("Cannot handle arch-variant types for protos at this time.")
Yu Liu2aa806b2022-09-01 11:54:47 -0700268 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700269 }
270 }
Spandan Dasc53767e2023-08-03 23:02:26 +0000271
272 tags := ApexAvailableTagsWithoutTestApexes(ctx.(TopDownMutatorContext), ctx.Module())
273
274 // Since we are creating the proto_library in a subpackage, create an import_prefix relative to the current package
275 if rel, err := filepath.Rel(ctx.ModuleDir(), pkg); err != nil {
276 ctx.ModuleErrorf("Could not get relative path for %v %v", pkg, err)
277 } else if rel != "." {
278 attrs.Import_prefix = &rel
279 }
280
281 ctx.CreateBazelTargetModule(
282 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
283 CommonAttributes{Name: name, Dir: proptools.StringPtr(pkg), Tags: tags},
284 &attrs,
285 )
286
287 l := ""
288 if pkg == ctx.ModuleDir() { // same package that the original module lives in
289 l = ":" + name
290 } else {
291 l = "//" + pkg + ":" + name
292 }
293 protoLibraries.Add(&bazel.Label{
294 Label: l,
295 })
Yu Liu2aa806b2022-09-01 11:54:47 -0700296 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700297 }
298
299 info.Proto_libs = protoLibraries
Liz Kammer12615db2021-09-28 09:19:17 -0400300
301 return info, true
302}