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