Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 17 | import ( |
Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 18 | "path/filepath" |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 19 | "strings" |
| 20 | |
Yu Liu | 2d13614 | 2022-08-18 14:46:13 -0700 | [diff] [blame] | 21 | "android/soong/bazel" |
| 22 | |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint" |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
| 25 | ) |
| 26 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 27 | const ( |
| 28 | canonicalPathFromRootDefault = true |
| 29 | ) |
| 30 | |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 31 | // 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 Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 39 | type ProtoFlags struct { |
| 40 | Flags []string |
| 41 | CanonicalPathFromRoot bool |
| 42 | Dir ModuleGenPath |
| 43 | SubDir ModuleGenPath |
| 44 | OutTypeFlag string |
| 45 | OutParams []string |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 46 | Deps Paths |
| 47 | } |
| 48 | |
| 49 | type protoDependencyTag struct { |
| 50 | blueprint.BaseDependencyTag |
| 51 | name string |
| 52 | } |
| 53 | |
| 54 | var ProtoPluginDepTag = protoDependencyTag{name: "plugin"} |
| 55 | |
| 56 | func 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 Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 62 | ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), |
| 63 | ProtoPluginDepTag, "protoc-gen-"+plugin) |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 64 | } |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 65 | } |
Colin Cross | a3b2500 | 2017-12-15 13:41:30 -0800 | [diff] [blame] | 66 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 67 | func GetProtoFlags(ctx ModuleContext, p *ProtoProperties) ProtoFlags { |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 68 | var flags []string |
| 69 | var deps Paths |
| 70 | |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 71 | if len(p.Proto.Local_include_dirs) > 0 { |
| 72 | localProtoIncludeDirs := PathsForModuleSrc(ctx, p.Proto.Local_include_dirs) |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 73 | flags = append(flags, JoinWithPrefix(localProtoIncludeDirs.Strings(), "-I")) |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 74 | } |
| 75 | if len(p.Proto.Include_dirs) > 0 { |
| 76 | rootProtoIncludeDirs := PathsForSource(ctx, p.Proto.Include_dirs) |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 77 | 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 Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 96 | return ProtoFlags{ |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 97 | Flags: flags, |
| 98 | Deps: deps, |
| 99 | OutTypeFlag: protoOutFlag, |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 100 | CanonicalPathFromRoot: proptools.BoolDefault(p.Proto.Canonical_path_from_root, canonicalPathFromRootDefault), |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 101 | Dir: PathForModuleGen(ctx, "proto"), |
| 102 | SubDir: PathForModuleGen(ctx, "proto", ctx.ModuleDir()), |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 103 | } |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | type 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 Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 111 | // Proto plugin to use as the generator. Must be a cc_binary_host module. |
| 112 | Plugin *string `android:"arch_variant"` |
| 113 | |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 114 | // 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 Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 120 | |
| 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 Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 129 | } `android:"arch_variant"` |
| 130 | } |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 131 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 132 | func ProtoRule(rule *RuleBuilder, protoFile Path, flags ProtoFlags, deps Paths, |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 133 | 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 Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 144 | BuiltTool("aprotoc"). |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 145 | 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 Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 154 | BuiltTool("dep_fixer").Flag(depFile.String()) |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 155 | } |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 156 | |
| 157 | // Bp2buildProtoInfo contains information necessary to pass on to language specific conversion. |
| 158 | type Bp2buildProtoInfo struct { |
Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 159 | Type *string |
| 160 | Proto_libs bazel.LabelList |
| 161 | Transitive_proto_libs bazel.LabelList |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 164 | type ProtoAttrs struct { |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 165 | Srcs bazel.LabelListAttribute |
Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 166 | Import_prefix *string |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 167 | Strip_import_prefix *string |
Yu Liu | 2d13614 | 2022-08-18 14:46:13 -0700 | [diff] [blame] | 168 | 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 |
| 173 | var includeDirsToProtoDeps = map[string]string{ |
| 174 | "external/protobuf/src": "//external/protobuf:libprotobuf-proto", |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 177 | // 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 |
| 180 | func 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 Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 206 | // Bp2buildProtoProperties converts proto properties, creating a proto_library and returning the |
| 207 | // information necessary for language-specific handling. |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 208 | func Bp2buildProtoProperties(ctx Bp2buildMutatorContext, m *ModuleBase, srcs bazel.LabelListAttribute) (Bp2buildProtoInfo, bool) { |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 209 | var info Bp2buildProtoInfo |
| 210 | if srcs.IsEmpty() { |
| 211 | return info, false |
| 212 | } |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 213 | |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 214 | var protoLibraries bazel.LabelList |
Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 215 | var transitiveProtoLibraries bazel.LabelList |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 216 | var directProtoSrcs bazel.LabelList |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 217 | |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 218 | // 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 Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 232 | name := m.Name() + "_proto" |
| 233 | |
| 234 | depsFromFilegroup := protoLibraries |
Liz Kammer | 7dc6bcb | 2023-08-10 12:51:59 -0400 | [diff] [blame] | 235 | var canonicalPathFromRoot bool |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 236 | |
| 237 | if len(directProtoSrcs.Includes) > 0 { |
Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 238 | pkgToSrcs := partitionSrcsByPackage(ctx.ModuleDir(), directProtoSrcs) |
Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 239 | protoIncludeDirs := []string{} |
Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 240 | 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 Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 246 | |
Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 247 | 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 Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 253 | } |
Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 254 | if axis == bazel.NoConfigAxis { |
| 255 | info.Type = props.Proto.Type |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 256 | |
Liz Kammer | 7dc6bcb | 2023-08-10 12:51:59 -0400 | [diff] [blame] | 257 | canonicalPathFromRoot = proptools.BoolDefault(props.Proto.Canonical_path_from_root, canonicalPathFromRootDefault) |
| 258 | if !canonicalPathFromRoot { |
Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 259 | // an empty string indicates to strips the package path |
| 260 | path := "" |
| 261 | attrs.Strip_import_prefix = &path |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 262 | } |
Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 263 | |
| 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 Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 268 | protoIncludeDirs = append(protoIncludeDirs, dir) |
Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 269 | } |
| 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 Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 273 | } |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 276 | |
| 277 | tags := ApexAvailableTagsWithoutTestApexes(ctx.(TopDownMutatorContext), ctx.Module()) |
| 278 | |
Liz Kammer | 7dc6bcb | 2023-08-10 12:51:59 -0400 | [diff] [blame] | 279 | 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 Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Spandan Das | 215adb4 | 2023-08-14 16:52:24 +0000 | [diff] [blame] | 289 | // 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 Das | e0f2ed5 | 2023-08-22 18:19:44 +0000 | [diff] [blame^] | 295 | // |
| 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 Das | 215adb4 | 2023-08-14 16:52:24 +0000 | [diff] [blame] | 300 | tags.Append(bazel.MakeStringListAttribute([]string{"manual"})) |
| 301 | } |
Spandan Das | e0f2ed5 | 2023-08-22 18:19:44 +0000 | [diff] [blame^] | 302 | |
Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 303 | 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 Kammer | 7dc6bcb | 2023-08-10 12:51:59 -0400 | [diff] [blame] | 310 | if pkg == moduleDir { // same package that the original module lives in |
Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 311 | l = ":" + name |
| 312 | } else { |
| 313 | l = "//" + pkg + ":" + name |
| 314 | } |
| 315 | protoLibraries.Add(&bazel.Label{ |
| 316 | Label: l, |
| 317 | }) |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 318 | } |
Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 319 | protoLibrariesInIncludeDir := createProtoLibraryTargetsForIncludeDirs(ctx, protoIncludeDirs) |
| 320 | transitiveProtoLibraries.Append(protoLibrariesInIncludeDir) |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | info.Proto_libs = protoLibraries |
Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 324 | info.Transitive_proto_libs = transitiveProtoLibraries |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 325 | |
| 326 | return info, true |
| 327 | } |
Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 328 | |
| 329 | var ( |
| 330 | protoIncludeDirGeneratedSuffix = ".include_dir_bp2build_generated_proto" |
| 331 | protoIncludeDirsBp2buildKey = NewOnceKey("protoIncludeDirsBp2build") |
| 332 | ) |
| 333 | |
| 334 | func 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 |
| 341 | type 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 |
| 350 | func 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 | } |