| 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" | 
| Spandan Das | fc6e645 | 2023-08-29 23:55:49 +0000 | [diff] [blame] | 20 | "sync" | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 21 |  | 
| Yu Liu | 2d13614 | 2022-08-18 14:46:13 -0700 | [diff] [blame] | 22 | "android/soong/bazel" | 
|  | 23 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint" | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 25 | "github.com/google/blueprint/proptools" | 
|  | 26 | ) | 
|  | 27 |  | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 28 | const ( | 
|  | 29 | canonicalPathFromRootDefault = true | 
|  | 30 | ) | 
|  | 31 |  | 
| Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 32 | // TODO(ccross): protos are often used to communicate between multiple modules.  If the only | 
|  | 33 | // way to convert a proto to source is to reference it as a source file, and external modules cannot | 
|  | 34 | // reference source files in other modules, then every module that owns a proto file will need to | 
|  | 35 | // export a library for every type of external user (lite vs. full, c vs. c++ vs. java).  It would | 
|  | 36 | // be better to support a proto module type that exported a proto file along with some include dirs, | 
|  | 37 | // and then external modules could depend on the proto module but use their own settings to | 
|  | 38 | // generate the source. | 
|  | 39 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 40 | type ProtoFlags struct { | 
|  | 41 | Flags                 []string | 
|  | 42 | CanonicalPathFromRoot bool | 
|  | 43 | Dir                   ModuleGenPath | 
|  | 44 | SubDir                ModuleGenPath | 
|  | 45 | OutTypeFlag           string | 
|  | 46 | OutParams             []string | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 47 | Deps                  Paths | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | type protoDependencyTag struct { | 
|  | 51 | blueprint.BaseDependencyTag | 
|  | 52 | name string | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | var ProtoPluginDepTag = protoDependencyTag{name: "plugin"} | 
|  | 56 |  | 
|  | 57 | func ProtoDeps(ctx BottomUpMutatorContext, p *ProtoProperties) { | 
|  | 58 | if String(p.Proto.Plugin) != "" && String(p.Proto.Type) != "" { | 
|  | 59 | ctx.ModuleErrorf("only one of proto.type and proto.plugin can be specified.") | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | if plugin := String(p.Proto.Plugin); plugin != "" { | 
| Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 63 | ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), | 
|  | 64 | ProtoPluginDepTag, "protoc-gen-"+plugin) | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 65 | } | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 66 | } | 
| Colin Cross | a3b2500 | 2017-12-15 13:41:30 -0800 | [diff] [blame] | 67 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 68 | func GetProtoFlags(ctx ModuleContext, p *ProtoProperties) ProtoFlags { | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 69 | var flags []string | 
|  | 70 | var deps Paths | 
|  | 71 |  | 
| Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 72 | if len(p.Proto.Local_include_dirs) > 0 { | 
|  | 73 | localProtoIncludeDirs := PathsForModuleSrc(ctx, p.Proto.Local_include_dirs) | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 74 | flags = append(flags, JoinWithPrefix(localProtoIncludeDirs.Strings(), "-I")) | 
| Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 75 | } | 
|  | 76 | if len(p.Proto.Include_dirs) > 0 { | 
|  | 77 | rootProtoIncludeDirs := PathsForSource(ctx, p.Proto.Include_dirs) | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 78 | flags = append(flags, JoinWithPrefix(rootProtoIncludeDirs.Strings(), "-I")) | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | ctx.VisitDirectDepsWithTag(ProtoPluginDepTag, func(dep Module) { | 
|  | 82 | if hostTool, ok := dep.(HostToolProvider); !ok || !hostTool.HostToolPath().Valid() { | 
|  | 83 | ctx.PropertyErrorf("proto.plugin", "module %q is not a host tool provider", | 
|  | 84 | ctx.OtherModuleName(dep)) | 
|  | 85 | } else { | 
|  | 86 | plugin := String(p.Proto.Plugin) | 
|  | 87 | deps = append(deps, hostTool.HostToolPath().Path()) | 
|  | 88 | flags = append(flags, "--plugin=protoc-gen-"+plugin+"="+hostTool.HostToolPath().String()) | 
|  | 89 | } | 
|  | 90 | }) | 
|  | 91 |  | 
|  | 92 | var protoOutFlag string | 
|  | 93 | if plugin := String(p.Proto.Plugin); plugin != "" { | 
|  | 94 | protoOutFlag = "--" + plugin + "_out" | 
| Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 97 | return ProtoFlags{ | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 98 | Flags:                 flags, | 
|  | 99 | Deps:                  deps, | 
|  | 100 | OutTypeFlag:           protoOutFlag, | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 101 | CanonicalPathFromRoot: proptools.BoolDefault(p.Proto.Canonical_path_from_root, canonicalPathFromRootDefault), | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 102 | Dir:                   PathForModuleGen(ctx, "proto"), | 
|  | 103 | SubDir:                PathForModuleGen(ctx, "proto", ctx.ModuleDir()), | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 104 | } | 
| Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
|  | 107 | type ProtoProperties struct { | 
|  | 108 | Proto struct { | 
|  | 109 | // Proto generator type.  C++: full or lite.  Java: micro, nano, stream, or lite. | 
|  | 110 | Type *string `android:"arch_variant"` | 
|  | 111 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 112 | // Proto plugin to use as the generator.  Must be a cc_binary_host module. | 
|  | 113 | Plugin *string `android:"arch_variant"` | 
|  | 114 |  | 
| Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 115 | // list of directories that will be added to the protoc include paths. | 
|  | 116 | Include_dirs []string | 
|  | 117 |  | 
|  | 118 | // list of directories relative to the bp file that will | 
|  | 119 | // be added to the protoc include paths. | 
|  | 120 | Local_include_dirs []string | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 121 |  | 
|  | 122 | // whether to identify the proto files from the root of the | 
|  | 123 | // source tree (the original method in Android, useful for | 
|  | 124 | // android-specific protos), or relative from where they were | 
|  | 125 | // specified (useful for external/third party protos). | 
|  | 126 | // | 
|  | 127 | // This defaults to true today, but is expected to default to | 
|  | 128 | // false in the future. | 
|  | 129 | Canonical_path_from_root *bool | 
| Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 130 | } `android:"arch_variant"` | 
|  | 131 | } | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 132 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 133 | func ProtoRule(rule *RuleBuilder, protoFile Path, flags ProtoFlags, deps Paths, | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 134 | outDir WritablePath, depFile WritablePath, outputs WritablePaths) { | 
|  | 135 |  | 
|  | 136 | var protoBase string | 
|  | 137 | if flags.CanonicalPathFromRoot { | 
|  | 138 | protoBase = "." | 
|  | 139 | } else { | 
|  | 140 | rel := protoFile.Rel() | 
|  | 141 | protoBase = strings.TrimSuffix(protoFile.String(), rel) | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | rule.Command(). | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 145 | BuiltTool("aprotoc"). | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 146 | FlagWithArg(flags.OutTypeFlag+"=", strings.Join(flags.OutParams, ",")+":"+outDir.String()). | 
|  | 147 | FlagWithDepFile("--dependency_out=", depFile). | 
|  | 148 | FlagWithArg("-I ", protoBase). | 
|  | 149 | Flags(flags.Flags). | 
|  | 150 | Input(protoFile). | 
|  | 151 | Implicits(deps). | 
|  | 152 | ImplicitOutputs(outputs) | 
|  | 153 |  | 
|  | 154 | rule.Command(). | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 155 | BuiltTool("dep_fixer").Flag(depFile.String()) | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 156 | } | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 157 |  | 
|  | 158 | // Bp2buildProtoInfo contains information necessary to pass on to language specific conversion. | 
|  | 159 | type Bp2buildProtoInfo struct { | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 160 | Type                  *string | 
|  | 161 | Proto_libs            bazel.LabelList | 
|  | 162 | Transitive_proto_libs bazel.LabelList | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 165 | type ProtoAttrs struct { | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 166 | Srcs                bazel.LabelListAttribute | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 167 | Import_prefix       *string | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 168 | Strip_import_prefix *string | 
| Yu Liu | 2d13614 | 2022-08-18 14:46:13 -0700 | [diff] [blame] | 169 | Deps                bazel.LabelListAttribute | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | // For each package in the include_dirs property a proto_library target should | 
|  | 173 | // be added to the BUILD file in that package and a mapping should be added here | 
|  | 174 | var includeDirsToProtoDeps = map[string]string{ | 
|  | 175 | "external/protobuf/src": "//external/protobuf:libprotobuf-proto", | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 178 | // Partitions srcs by the pkg it is in | 
|  | 179 | // srcs has been created using `TransformSubpackagePaths` | 
|  | 180 | // This function uses existence of Android.bp/BUILD files to create a label that is compatible with the package structure of bp2build workspace | 
|  | 181 | func partitionSrcsByPackage(currentDir string, srcs bazel.LabelList) map[string]bazel.LabelList { | 
|  | 182 | getPackageFromLabel := func(label string) string { | 
|  | 183 | // Remove any preceding // | 
|  | 184 | label = strings.TrimPrefix(label, "//") | 
|  | 185 | split := strings.Split(label, ":") | 
|  | 186 | if len(split) == 1 { | 
|  | 187 | // e.g. foo.proto | 
|  | 188 | return currentDir | 
|  | 189 | } else if split[0] == "" { | 
|  | 190 | // e.g. :foo.proto | 
|  | 191 | return currentDir | 
|  | 192 | } else { | 
|  | 193 | return split[0] | 
|  | 194 | } | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | pkgToSrcs := map[string]bazel.LabelList{} | 
|  | 198 | for _, src := range srcs.Includes { | 
|  | 199 | pkg := getPackageFromLabel(src.Label) | 
|  | 200 | list := pkgToSrcs[pkg] | 
|  | 201 | list.Add(&src) | 
|  | 202 | pkgToSrcs[pkg] = list | 
|  | 203 | } | 
|  | 204 | return pkgToSrcs | 
|  | 205 | } | 
|  | 206 |  | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 207 | // Bp2buildProtoProperties converts proto properties, creating a proto_library and returning the | 
|  | 208 | // information necessary for language-specific handling. | 
| Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 209 | func Bp2buildProtoProperties(ctx Bp2buildMutatorContext, m *ModuleBase, srcs bazel.LabelListAttribute) (Bp2buildProtoInfo, bool) { | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 210 | var info Bp2buildProtoInfo | 
|  | 211 | if srcs.IsEmpty() { | 
|  | 212 | return info, false | 
|  | 213 | } | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 214 |  | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 215 | var protoLibraries bazel.LabelList | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 216 | var transitiveProtoLibraries bazel.LabelList | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 217 | var directProtoSrcs bazel.LabelList | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 218 |  | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 219 | // For filegroups that should be converted to proto_library just collect the | 
|  | 220 | // labels of converted proto_library targets. | 
|  | 221 | for _, protoSrc := range srcs.Value.Includes { | 
|  | 222 | src := protoSrc.OriginalModuleName | 
|  | 223 | if fg, ok := ToFileGroupAsLibrary(ctx, src); ok && | 
|  | 224 | fg.ShouldConvertToProtoLibrary(ctx) { | 
|  | 225 | protoLibraries.Add(&bazel.Label{ | 
|  | 226 | Label: fg.GetProtoLibraryLabel(ctx), | 
|  | 227 | }) | 
|  | 228 | } else { | 
|  | 229 | directProtoSrcs.Add(&protoSrc) | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 230 | } | 
|  | 231 | } | 
|  | 232 |  | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 233 | name := m.Name() + "_proto" | 
|  | 234 |  | 
|  | 235 | depsFromFilegroup := protoLibraries | 
| Liz Kammer | 7dc6bcb | 2023-08-10 12:51:59 -0400 | [diff] [blame] | 236 | var canonicalPathFromRoot bool | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 237 |  | 
|  | 238 | if len(directProtoSrcs.Includes) > 0 { | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 239 | pkgToSrcs := partitionSrcsByPackage(ctx.ModuleDir(), directProtoSrcs) | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 240 | protoIncludeDirs := []string{} | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 241 | for _, pkg := range SortedStringKeys(pkgToSrcs) { | 
|  | 242 | srcs := pkgToSrcs[pkg] | 
|  | 243 | attrs := ProtoAttrs{ | 
|  | 244 | Srcs: bazel.MakeLabelListAttribute(srcs), | 
|  | 245 | } | 
|  | 246 | attrs.Deps.Append(bazel.MakeLabelListAttribute(depsFromFilegroup)) | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 247 |  | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 248 | for axis, configToProps := range m.GetArchVariantProperties(ctx, &ProtoProperties{}) { | 
|  | 249 | for _, rawProps := range configToProps { | 
|  | 250 | var props *ProtoProperties | 
|  | 251 | var ok bool | 
|  | 252 | if props, ok = rawProps.(*ProtoProperties); !ok { | 
|  | 253 | ctx.ModuleErrorf("Could not cast ProtoProperties to expected type") | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 254 | } | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 255 | if axis == bazel.NoConfigAxis { | 
|  | 256 | info.Type = props.Proto.Type | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 257 |  | 
| Liz Kammer | 7dc6bcb | 2023-08-10 12:51:59 -0400 | [diff] [blame] | 258 | canonicalPathFromRoot = proptools.BoolDefault(props.Proto.Canonical_path_from_root, canonicalPathFromRootDefault) | 
|  | 259 | if !canonicalPathFromRoot { | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 260 | // an empty string indicates to strips the package path | 
|  | 261 | path := "" | 
|  | 262 | attrs.Strip_import_prefix = &path | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 263 | } | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 264 |  | 
|  | 265 | for _, dir := range props.Proto.Include_dirs { | 
|  | 266 | if dep, ok := includeDirsToProtoDeps[dir]; ok { | 
|  | 267 | attrs.Deps.Add(bazel.MakeLabelAttribute(dep)) | 
|  | 268 | } else { | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 269 | protoIncludeDirs = append(protoIncludeDirs, dir) | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 270 | } | 
|  | 271 | } | 
| Spandan Das | 4e5a194 | 2023-08-22 19:20:39 +0000 | [diff] [blame] | 272 |  | 
|  | 273 | // proto.local_include_dirs are similar to proto.include_dirs, except that it is relative to the module directory | 
|  | 274 | for _, dir := range props.Proto.Local_include_dirs { | 
|  | 275 | relativeToTop := pathForModuleSrc(ctx, dir).String() | 
|  | 276 | protoIncludeDirs = append(protoIncludeDirs, relativeToTop) | 
|  | 277 | } | 
|  | 278 |  | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 279 | } else if props.Proto.Type != info.Type && props.Proto.Type != nil { | 
|  | 280 | ctx.ModuleErrorf("Cannot handle arch-variant types for protos at this time.") | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 281 | } | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 282 | } | 
|  | 283 | } | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 284 |  | 
| Spandan Das | cb84763 | 2023-08-22 19:24:07 +0000 | [diff] [blame] | 285 | if p, ok := m.module.(PkgPathInterface); ok && p.PkgPath(ctx) != nil { | 
|  | 286 | // python_library with pkg_path | 
|  | 287 | // proto_library for this module should have the pkg_path as the import_prefix | 
|  | 288 | attrs.Import_prefix = p.PkgPath(ctx) | 
|  | 289 | attrs.Strip_import_prefix = proptools.StringPtr("") | 
|  | 290 | } | 
|  | 291 |  | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 292 | tags := ApexAvailableTagsWithoutTestApexes(ctx.(TopDownMutatorContext), ctx.Module()) | 
|  | 293 |  | 
| Liz Kammer | 7dc6bcb | 2023-08-10 12:51:59 -0400 | [diff] [blame] | 294 | moduleDir := ctx.ModuleDir() | 
|  | 295 | if !canonicalPathFromRoot { | 
|  | 296 | // Since we are creating the proto_library in a subpackage, set the import_prefix relative to the current package | 
|  | 297 | if rel, err := filepath.Rel(moduleDir, pkg); err != nil { | 
|  | 298 | ctx.ModuleErrorf("Could not get relative path for %v %v", pkg, err) | 
|  | 299 | } else if rel != "." { | 
|  | 300 | attrs.Import_prefix = &rel | 
|  | 301 | } | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 302 | } | 
|  | 303 |  | 
| Spandan Das | 215adb4 | 2023-08-14 16:52:24 +0000 | [diff] [blame] | 304 | // TODO - b/246997908: Handle potential orphaned proto_library targets | 
|  | 305 | // To create proto_library targets in the same package, we split the .proto files | 
|  | 306 | // This means that if a proto_library in a subpackage imports another proto_library from the parent package | 
|  | 307 | // (or a different subpackage), it will not find it. | 
|  | 308 | // The CcProtoGen action itself runs fine because we construct the correct ProtoInfo, | 
|  | 309 | // but the FileDescriptorSet of each proto_library might not be compile-able | 
| Spandan Das | e0f2ed5 | 2023-08-22 18:19:44 +0000 | [diff] [blame] | 310 | // | 
|  | 311 | // Add manual tag if either | 
|  | 312 | // 1. .proto files are in more than one package | 
|  | 313 | // 2. proto.include_dirs is not empty | 
|  | 314 | if len(SortedStringKeys(pkgToSrcs)) > 1 || len(protoIncludeDirs) > 0 { | 
| Spandan Das | 215adb4 | 2023-08-14 16:52:24 +0000 | [diff] [blame] | 315 | tags.Append(bazel.MakeStringListAttribute([]string{"manual"})) | 
|  | 316 | } | 
| Spandan Das | e0f2ed5 | 2023-08-22 18:19:44 +0000 | [diff] [blame] | 317 |  | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 318 | ctx.CreateBazelTargetModule( | 
|  | 319 | bazel.BazelTargetModuleProperties{Rule_class: "proto_library"}, | 
|  | 320 | CommonAttributes{Name: name, Dir: proptools.StringPtr(pkg), Tags: tags}, | 
|  | 321 | &attrs, | 
|  | 322 | ) | 
|  | 323 |  | 
|  | 324 | l := "" | 
| Liz Kammer | 7dc6bcb | 2023-08-10 12:51:59 -0400 | [diff] [blame] | 325 | if pkg == moduleDir { // same package that the original module lives in | 
| Spandan Das | c53767e | 2023-08-03 23:02:26 +0000 | [diff] [blame] | 326 | l = ":" + name | 
|  | 327 | } else { | 
|  | 328 | l = "//" + pkg + ":" + name | 
|  | 329 | } | 
|  | 330 | protoLibraries.Add(&bazel.Label{ | 
|  | 331 | Label: l, | 
|  | 332 | }) | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 333 | } | 
| Spandan Das | f26ee15 | 2023-08-24 23:21:04 +0000 | [diff] [blame] | 334 | // Partitioning by packages can create dupes of protoIncludeDirs, so dedupe it first. | 
|  | 335 | protoLibrariesInIncludeDir := createProtoLibraryTargetsForIncludeDirs(ctx, SortedUniqueStrings(protoIncludeDirs)) | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 336 | transitiveProtoLibraries.Append(protoLibrariesInIncludeDir) | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 337 | } | 
|  | 338 |  | 
|  | 339 | info.Proto_libs = protoLibraries | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 340 | info.Transitive_proto_libs = transitiveProtoLibraries | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 341 |  | 
|  | 342 | return info, true | 
|  | 343 | } | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 344 |  | 
| Spandan Das | cb84763 | 2023-08-22 19:24:07 +0000 | [diff] [blame] | 345 | // PkgPathInterface is used as a type assertion in bp2build to get pkg_path property of python_library_host | 
|  | 346 | type PkgPathInterface interface { | 
|  | 347 | PkgPath(ctx BazelConversionContext) *string | 
|  | 348 | } | 
|  | 349 |  | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 350 | var ( | 
|  | 351 | protoIncludeDirGeneratedSuffix = ".include_dir_bp2build_generated_proto" | 
|  | 352 | protoIncludeDirsBp2buildKey    = NewOnceKey("protoIncludeDirsBp2build") | 
|  | 353 | ) | 
|  | 354 |  | 
| Spandan Das | 43dfeb6 | 2023-08-30 22:20:54 +0000 | [diff] [blame] | 355 | func getProtoIncludeDirsBp2build(config Config) *sync.Map { | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 356 | return config.Once(protoIncludeDirsBp2buildKey, func() interface{} { | 
| Spandan Das | 43dfeb6 | 2023-08-30 22:20:54 +0000 | [diff] [blame] | 357 | return &sync.Map{} | 
|  | 358 | }).(*sync.Map) | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 359 | } | 
|  | 360 |  | 
|  | 361 | // key for dynamically creating proto_library per proto.include_dirs | 
|  | 362 | type protoIncludeDirKey struct { | 
|  | 363 | dir            string | 
|  | 364 | subpackgeInDir string | 
|  | 365 | } | 
|  | 366 |  | 
|  | 367 | // createProtoLibraryTargetsForIncludeDirs creates additional proto_library targets for .proto files in includeDirs | 
|  | 368 | // Since Bazel imposes a constratint that the proto_library must be in the same package as the .proto file, this function | 
|  | 369 | // might create the targets in a subdirectory of `includeDir` | 
|  | 370 | // Returns the labels of the proto_library targets | 
|  | 371 | func createProtoLibraryTargetsForIncludeDirs(ctx Bp2buildMutatorContext, includeDirs []string) bazel.LabelList { | 
|  | 372 | var ret bazel.LabelList | 
|  | 373 | for _, dir := range includeDirs { | 
|  | 374 | if exists, _, _ := ctx.Config().fs.Exists(filepath.Join(dir, "Android.bp")); !exists { | 
|  | 375 | ctx.ModuleErrorf("TODO: Add support for proto.include_dir: %v. This directory does not contain an Android.bp file", dir) | 
|  | 376 | } | 
|  | 377 | dirMap := getProtoIncludeDirsBp2build(ctx.Config()) | 
|  | 378 | // Find all proto file targets in this dir | 
|  | 379 | protoLabelsInDir := BazelLabelForSrcPatternExcludes(ctx, dir, "**/*.proto", []string{}) | 
|  | 380 | // Partition the labels by package and subpackage(s) | 
|  | 381 | protoLabelelsPartitionedByPkg := partitionSrcsByPackage(dir, protoLabelsInDir) | 
|  | 382 | for _, pkg := range SortedStringKeys(protoLabelelsPartitionedByPkg) { | 
|  | 383 | label := strings.ReplaceAll(dir, "/", ".") + protoIncludeDirGeneratedSuffix | 
|  | 384 | ret.Add(&bazel.Label{ | 
|  | 385 | Label: "//" + pkg + ":" + label, | 
|  | 386 | }) | 
|  | 387 | key := protoIncludeDirKey{dir: dir, subpackgeInDir: pkg} | 
| Spandan Das | 43dfeb6 | 2023-08-30 22:20:54 +0000 | [diff] [blame] | 388 | if _, exists := dirMap.LoadOrStore(key, true); exists { | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 389 | // A proto_library has already been created for this package relative to this include dir | 
|  | 390 | continue | 
|  | 391 | } | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 392 | srcs := protoLabelelsPartitionedByPkg[pkg] | 
|  | 393 | rel, err := filepath.Rel(dir, pkg) | 
|  | 394 | if err != nil { | 
|  | 395 | ctx.ModuleErrorf("Could not create a proto_library in pkg %v due to %v\n", pkg, err) | 
|  | 396 | } | 
|  | 397 | // Create proto_library | 
|  | 398 | attrs := ProtoAttrs{ | 
|  | 399 | Srcs:                bazel.MakeLabelListAttribute(srcs), | 
|  | 400 | Strip_import_prefix: proptools.StringPtr(""), | 
|  | 401 | } | 
|  | 402 | if rel != "." { | 
|  | 403 | attrs.Import_prefix = proptools.StringPtr(rel) | 
|  | 404 | } | 
| Spandan Das | f26ee15 | 2023-08-24 23:21:04 +0000 | [diff] [blame] | 405 |  | 
|  | 406 | // If a specific directory is listed in proto.include_dirs of two separate modules (one host-specific and another device-specific), | 
|  | 407 | // we do not want to create the proto_library with target_compatible_with of the first visited of these two modules | 
|  | 408 | // As a workarounds, delete `target_compatible_with` | 
|  | 409 | alwaysEnabled := bazel.BoolAttribute{} | 
|  | 410 | alwaysEnabled.Value = proptools.BoolPtr(true) | 
| Spandan Das | ab29f57 | 2023-09-01 19:43:56 +0000 | [diff] [blame] | 411 | // Add android and linux explicitly so that fillcommonbp2buildmoduleattrs can override these configs | 
|  | 412 | // When we extend b support for other os'es (darwin/windows), we should add those configs here as well | 
|  | 413 | alwaysEnabled.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid, proptools.BoolPtr(true)) | 
|  | 414 | alwaysEnabled.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsLinux, proptools.BoolPtr(true)) | 
|  | 415 |  | 
| Spandan Das | f26ee15 | 2023-08-24 23:21:04 +0000 | [diff] [blame] | 416 | ctx.CreateBazelTargetModuleWithRestrictions( | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 417 | bazel.BazelTargetModuleProperties{Rule_class: "proto_library"}, | 
|  | 418 | CommonAttributes{ | 
|  | 419 | Name: label, | 
|  | 420 | Dir:  proptools.StringPtr(pkg), | 
|  | 421 | // This proto_library is used to construct a ProtoInfo | 
|  | 422 | // But it might not be buildable on its own | 
|  | 423 | Tags: bazel.MakeStringListAttribute([]string{"manual"}), | 
|  | 424 | }, | 
|  | 425 | &attrs, | 
| Spandan Das | f26ee15 | 2023-08-24 23:21:04 +0000 | [diff] [blame] | 426 | alwaysEnabled, | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 427 | ) | 
|  | 428 | } | 
|  | 429 | } | 
|  | 430 | return ret | 
|  | 431 | } |