Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 1 | // Copyright 2016 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 | |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 15 | package android |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 16 | |
| 17 | import ( |
Vinh Tran | 16fe8e1 | 2022-08-16 16:45:44 -0400 | [diff] [blame] | 18 | "path/filepath" |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 19 | "regexp" |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 20 | "strings" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0d99045 | 2021-08-11 16:46:13 +0000 | [diff] [blame] | 21 | |
| 22 | "android/soong/bazel" |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 23 | "android/soong/bazel/cquery" |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 24 | |
| 25 | "github.com/google/blueprint" |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func init() { |
Anton Hansson | 7d6dd8b | 2023-03-06 11:26:17 +0000 | [diff] [blame] | 29 | RegisterFilegroupBuildComponents(InitRegistrationContext) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 32 | var PrepareForTestWithFilegroup = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
Anton Hansson | 7d6dd8b | 2023-03-06 11:26:17 +0000 | [diff] [blame] | 33 | RegisterFilegroupBuildComponents(ctx) |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 34 | }) |
| 35 | |
Anton Hansson | 7d6dd8b | 2023-03-06 11:26:17 +0000 | [diff] [blame] | 36 | func RegisterFilegroupBuildComponents(ctx RegistrationContext) { |
| 37 | ctx.RegisterModuleType("filegroup", FileGroupFactory) |
| 38 | ctx.RegisterModuleType("filegroup_defaults", FileGroupDefaultsFactory) |
| 39 | } |
| 40 | |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 41 | var convertedProtoLibrarySuffix = "_bp2build_converted" |
| 42 | |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 43 | // IsFilegroup checks that a module is a filegroup type |
| 44 | func IsFilegroup(ctx bazel.OtherModuleContext, m blueprint.Module) bool { |
| 45 | return ctx.OtherModuleType(m) == "filegroup" |
| 46 | } |
| 47 | |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 48 | var ( |
| 49 | // ignoring case, checks for proto or protos as an independent word in the name, whether at the |
| 50 | // beginning, end, or middle. e.g. "proto.foo", "bar-protos", "baz_proto_srcs" would all match |
| 51 | filegroupLikelyProtoPattern = regexp.MustCompile("(?i)(^|[^a-z])proto(s)?([^a-z]|$)") |
| 52 | filegroupLikelyAidlPattern = regexp.MustCompile("(?i)(^|[^a-z])aidl([^a-z]|$)") |
| 53 | |
| 54 | ProtoSrcLabelPartition = bazel.LabelPartition{ |
| 55 | Extensions: []string{".proto"}, |
| 56 | LabelMapper: isFilegroupWithPattern(filegroupLikelyProtoPattern), |
| 57 | } |
| 58 | AidlSrcLabelPartition = bazel.LabelPartition{ |
| 59 | Extensions: []string{".aidl"}, |
| 60 | LabelMapper: isFilegroupWithPattern(filegroupLikelyAidlPattern), |
| 61 | } |
| 62 | ) |
| 63 | |
| 64 | func isFilegroupWithPattern(pattern *regexp.Regexp) bazel.LabelMapper { |
| 65 | return func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) { |
| 66 | m, exists := ctx.ModuleFromName(label.OriginalModuleName) |
| 67 | labelStr := label.Label |
| 68 | if !exists || !IsFilegroup(ctx, m) { |
| 69 | return labelStr, false |
| 70 | } |
| 71 | likelyMatched := pattern.MatchString(label.OriginalModuleName) |
| 72 | return labelStr, likelyMatched |
| 73 | } |
| 74 | } |
| 75 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 76 | // https://docs.bazel.build/versions/master/be/general.html#filegroup |
| 77 | type bazelFilegroupAttributes struct { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 78 | Srcs bazel.LabelListAttribute |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 81 | type bazelAidlLibraryAttributes struct { |
| 82 | Srcs bazel.LabelListAttribute |
| 83 | Strip_import_prefix *string |
Vinh Tran | 2562585 | 2023-04-14 18:45:20 -0400 | [diff] [blame] | 84 | Deps bazel.LabelListAttribute |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 85 | } |
| 86 | |
Spandan Das | bd52ea9 | 2023-03-09 23:03:07 +0000 | [diff] [blame] | 87 | // api srcs can be contained in filegroups. |
| 88 | // this should be generated in api_bp2build workspace as well. |
| 89 | func (fg *fileGroup) ConvertWithApiBp2build(ctx TopDownMutatorContext) { |
| 90 | fg.ConvertWithBp2build(ctx) |
| 91 | } |
| 92 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 93 | // ConvertWithBp2build performs bp2build conversion of filegroup |
| 94 | func (fg *fileGroup) ConvertWithBp2build(ctx TopDownMutatorContext) { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 95 | srcs := bazel.MakeLabelListAttribute( |
| 96 | BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 97 | |
| 98 | // For Bazel compatibility, don't generate the filegroup if there is only 1 |
| 99 | // source file, and that the source file is named the same as the module |
| 100 | // itself. In Bazel, eponymous filegroups like this would be an error. |
| 101 | // |
| 102 | // Instead, dependents on this single-file filegroup can just depend |
| 103 | // on the file target, instead of rule target, directly. |
| 104 | // |
| 105 | // You may ask: what if a filegroup has multiple files, and one of them |
| 106 | // shares the name? The answer: we haven't seen that in the wild, and |
| 107 | // should lock Soong itself down to prevent the behavior. For now, |
| 108 | // we raise an error if bp2build sees this problem. |
| 109 | for _, f := range srcs.Value.Includes { |
| 110 | if f.Label == fg.Name() { |
| 111 | if len(srcs.Value.Includes) > 1 { |
| 112 | ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name()) |
| 113 | } |
| 114 | return |
| 115 | } |
| 116 | } |
| 117 | |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 118 | // Convert module that has only AIDL files to aidl_library |
| 119 | // If the module has a mixed bag of AIDL and non-AIDL files, split the filegroup manually |
| 120 | // and then convert |
| 121 | if fg.ShouldConvertToAidlLibrary(ctx) { |
Liz Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame] | 122 | tags := []string{"apex_available=//apex_available:anyapex"} |
Vinh Tran | 2562585 | 2023-04-14 18:45:20 -0400 | [diff] [blame] | 123 | deps := bazel.MakeLabelListAttribute(BazelLabelForModuleDeps(ctx, fg.properties.Aidl.Deps)) |
| 124 | |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 125 | attrs := &bazelAidlLibraryAttributes{ |
| 126 | Srcs: srcs, |
| 127 | Strip_import_prefix: fg.properties.Path, |
Vinh Tran | 2562585 | 2023-04-14 18:45:20 -0400 | [diff] [blame] | 128 | Deps: deps, |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 129 | } |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 130 | |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 131 | props := bazel.BazelTargetModuleProperties{ |
| 132 | Rule_class: "aidl_library", |
Sam Delmerico | e55bf08 | 2023-03-31 09:47:28 -0400 | [diff] [blame] | 133 | Bzl_load_location: "//build/bazel/rules/aidl:aidl_library.bzl", |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 134 | } |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 135 | |
Liz Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame] | 136 | ctx.CreateBazelTargetModule( |
| 137 | props, |
| 138 | CommonAttributes{ |
| 139 | Name: fg.Name(), |
| 140 | Tags: bazel.MakeStringListAttribute(tags), |
| 141 | }, |
| 142 | attrs) |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 143 | } else { |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 144 | if fg.ShouldConvertToProtoLibrary(ctx) { |
| 145 | attrs := &ProtoAttrs{ |
| 146 | Srcs: srcs, |
| 147 | Strip_import_prefix: fg.properties.Path, |
| 148 | } |
| 149 | |
Liz Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame] | 150 | tags := []string{ |
| 151 | "apex_available=//apex_available:anyapex", |
| 152 | // TODO(b/246997908): we can remove this tag if we could figure out a solution for this bug. |
| 153 | "manual", |
| 154 | } |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 155 | ctx.CreateBazelTargetModule( |
| 156 | bazel.BazelTargetModuleProperties{Rule_class: "proto_library"}, |
Sam Delmerico | e9b33f7 | 2022-11-21 15:38:54 -0500 | [diff] [blame] | 157 | CommonAttributes{ |
| 158 | Name: fg.Name() + convertedProtoLibrarySuffix, |
| 159 | Tags: bazel.MakeStringListAttribute(tags), |
| 160 | }, |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 161 | attrs) |
| 162 | } |
| 163 | |
| 164 | // TODO(b/242847534): Still convert to a filegroup because other unconverted |
| 165 | // modules may depend on the filegroup |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 166 | attrs := &bazelFilegroupAttributes{ |
| 167 | Srcs: srcs, |
| 168 | } |
| 169 | |
| 170 | props := bazel.BazelTargetModuleProperties{ |
| 171 | Rule_class: "filegroup", |
| 172 | Bzl_load_location: "//build/bazel/rules:filegroup.bzl", |
| 173 | } |
| 174 | |
| 175 | ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs) |
| 176 | } |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | type fileGroupProperties struct { |
| 180 | // srcs lists files that will be included in this filegroup |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 181 | Srcs []string `android:"path"` |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 182 | |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 183 | Exclude_srcs []string `android:"path"` |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 184 | |
| 185 | // The base path to the files. May be used by other modules to determine which portion |
| 186 | // of the path to use. For example, when a filegroup is used as data in a cc_test rule, |
| 187 | // the base path is stripped off the path and the remaining path is used as the |
| 188 | // installation directory. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 189 | Path *string |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 190 | |
| 191 | // Create a make variable with the specified name that contains the list of files in the |
| 192 | // filegroup, relative to the root of the source tree. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 193 | Export_to_make_var *string |
Vinh Tran | 2562585 | 2023-04-14 18:45:20 -0400 | [diff] [blame] | 194 | |
| 195 | // aidl is explicitly provided for implicit aidl dependencies |
| 196 | // TODO(b/278298615): aidl prop is a no-op in Soong and is an escape hatch |
| 197 | // to include implicit aidl dependencies for bazel migration compatibility |
| 198 | Aidl struct { |
| 199 | // List of aidl files or filegroup depended on by srcs |
| 200 | Deps []string `android:"path"` |
| 201 | } |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | type fileGroup struct { |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 205 | ModuleBase |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 206 | BazelModuleBase |
Anton Hansson | 7d6dd8b | 2023-03-06 11:26:17 +0000 | [diff] [blame] | 207 | DefaultableModuleBase |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 208 | FileGroupAsLibrary |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 209 | properties fileGroupProperties |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 210 | srcs Paths |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 213 | var _ MixedBuildBuildable = (*fileGroup)(nil) |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 214 | var _ SourceFileProducer = (*fileGroup)(nil) |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 215 | var _ FileGroupAsLibrary = (*fileGroup)(nil) |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 216 | |
Patrice Arruda | 8958a94 | 2019-03-12 10:06:00 -0700 | [diff] [blame] | 217 | // filegroup contains a list of files that are referenced by other modules |
| 218 | // properties (such as "srcs") using the syntax ":<name>". filegroup are |
| 219 | // also be used to export files across package boundaries. |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 220 | func FileGroupFactory() Module { |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 221 | module := &fileGroup{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 222 | module.AddProperties(&module.properties) |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 223 | InitAndroidModule(module) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 224 | InitBazelModule(module) |
Anton Hansson | 7d6dd8b | 2023-03-06 11:26:17 +0000 | [diff] [blame] | 225 | InitDefaultableModule(module) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 226 | return module |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Liz Kammer | 5edc141 | 2022-05-25 11:12:44 -0400 | [diff] [blame] | 229 | var _ blueprint.JSONActionSupplier = (*fileGroup)(nil) |
| 230 | |
| 231 | func (fg *fileGroup) JSONActions() []blueprint.JSONAction { |
| 232 | ins := make([]string, 0, len(fg.srcs)) |
| 233 | outs := make([]string, 0, len(fg.srcs)) |
| 234 | for _, p := range fg.srcs { |
| 235 | ins = append(ins, p.String()) |
| 236 | outs = append(outs, p.Rel()) |
| 237 | } |
| 238 | return []blueprint.JSONAction{ |
| 239 | blueprint.JSONAction{ |
| 240 | Inputs: ins, |
| 241 | Outputs: outs, |
| 242 | }, |
| 243 | } |
| 244 | } |
| 245 | |
Liz Kammer | 5bde22f | 2021-04-19 14:04:14 -0400 | [diff] [blame] | 246 | func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) { |
Liz Kammer | 5bde22f | 2021-04-19 14:04:14 -0400 | [diff] [blame] | 247 | fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs) |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 248 | if fg.properties.Path != nil { |
| 249 | fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path)) |
| 250 | } |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 253 | func (fg *fileGroup) Srcs() Paths { |
| 254 | return append(Paths{}, fg.srcs...) |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 255 | } |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 256 | |
Dan Willemsen | 6a6478d | 2020-07-17 19:28:53 -0700 | [diff] [blame] | 257 | func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) { |
| 258 | if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" { |
| 259 | ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " ")) |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 262 | |
| 263 | func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) { |
| 264 | bazelCtx := ctx.Config().BazelContext |
| 265 | |
| 266 | bazelCtx.QueueBazelRequest( |
| 267 | fg.GetBazelLabel(ctx, fg), |
| 268 | cquery.GetOutputFiles, |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 269 | configKey{arch: Common.String(), osType: CommonOS}) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool { |
Liz Kammer | 748209c | 2022-10-24 10:43:27 -0400 | [diff] [blame] | 273 | // TODO(b/247782695), TODO(b/242847534) Fix mixed builds for filegroups |
| 274 | return false |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) { |
Vinh Tran | 16fe8e1 | 2022-08-16 16:45:44 -0400 | [diff] [blame] | 278 | bazelCtx := ctx.Config().BazelContext |
| 279 | // This is a short-term solution because we rely on info from Android.bp to handle |
| 280 | // a converted module. This will block when we want to remove Android.bp for all |
| 281 | // converted modules at some point. |
| 282 | // TODO(b/242847534): Implement a long-term solution in which we don't need to rely |
| 283 | // on info form Android.bp for modules that are already converted to Bazel |
| 284 | relativeRoot := ctx.ModuleDir() |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 285 | if fg.properties.Path != nil { |
Vinh Tran | 16fe8e1 | 2022-08-16 16:45:44 -0400 | [diff] [blame] | 286 | relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 287 | } |
| 288 | |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 289 | filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{arch: Common.String(), osType: CommonOS}) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 290 | if err != nil { |
| 291 | ctx.ModuleErrorf(err.Error()) |
| 292 | return |
| 293 | } |
| 294 | |
| 295 | bazelOuts := make(Paths, 0, len(filePaths)) |
| 296 | for _, p := range filePaths { |
Vinh Tran | 16fe8e1 | 2022-08-16 16:45:44 -0400 | [diff] [blame] | 297 | bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p)) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 298 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 299 | fg.srcs = bazelOuts |
| 300 | } |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 301 | |
| 302 | func (fg *fileGroup) ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool { |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 303 | return fg.shouldConvertToLibrary(ctx, ".aidl") |
| 304 | } |
| 305 | |
| 306 | func (fg *fileGroup) ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool { |
| 307 | return fg.shouldConvertToLibrary(ctx, ".proto") |
| 308 | } |
| 309 | |
| 310 | func (fg *fileGroup) shouldConvertToLibrary(ctx BazelConversionPathContext, suffix string) bool { |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 311 | if len(fg.properties.Srcs) == 0 || !fg.ShouldConvertWithBp2build(ctx) { |
| 312 | return false |
| 313 | } |
| 314 | for _, src := range fg.properties.Srcs { |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 315 | if !strings.HasSuffix(src, suffix) { |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 316 | return false |
| 317 | } |
| 318 | } |
| 319 | return true |
| 320 | } |
| 321 | |
| 322 | func (fg *fileGroup) GetAidlLibraryLabel(ctx BazelConversionPathContext) string { |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 323 | return fg.getFileGroupAsLibraryLabel(ctx) |
| 324 | } |
| 325 | |
| 326 | func (fg *fileGroup) GetProtoLibraryLabel(ctx BazelConversionPathContext) string { |
| 327 | return fg.getFileGroupAsLibraryLabel(ctx) + convertedProtoLibrarySuffix |
| 328 | } |
| 329 | |
| 330 | func (fg *fileGroup) getFileGroupAsLibraryLabel(ctx BazelConversionPathContext) string { |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 331 | if ctx.OtherModuleDir(fg.module) == ctx.ModuleDir() { |
| 332 | return ":" + fg.Name() |
| 333 | } else { |
| 334 | return fg.GetBazelLabel(ctx, fg) |
| 335 | } |
| 336 | } |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 337 | |
| 338 | // Given a name in srcs prop, check to see if the name references a filegroup |
| 339 | // and the filegroup is converted to aidl_library |
| 340 | func IsConvertedToAidlLibrary(ctx BazelConversionPathContext, name string) bool { |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 341 | if fg, ok := ToFileGroupAsLibrary(ctx, name); ok { |
| 342 | return fg.ShouldConvertToAidlLibrary(ctx) |
| 343 | } |
| 344 | return false |
| 345 | } |
| 346 | |
| 347 | func ToFileGroupAsLibrary(ctx BazelConversionPathContext, name string) (FileGroupAsLibrary, bool) { |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 348 | if module, ok := ctx.ModuleFromName(name); ok { |
| 349 | if IsFilegroup(ctx, module) { |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 350 | if fg, ok := module.(FileGroupAsLibrary); ok { |
| 351 | return fg, true |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | } |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 355 | return nil, false |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 356 | } |
Anton Hansson | 7d6dd8b | 2023-03-06 11:26:17 +0000 | [diff] [blame] | 357 | |
| 358 | // Defaults |
| 359 | type FileGroupDefaults struct { |
| 360 | ModuleBase |
| 361 | DefaultsModuleBase |
| 362 | } |
| 363 | |
| 364 | func FileGroupDefaultsFactory() Module { |
| 365 | module := &FileGroupDefaults{} |
| 366 | module.AddProperties(&fileGroupProperties{}) |
| 367 | InitDefaultsModule(module) |
| 368 | |
| 369 | return module |
| 370 | } |