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" |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 19 | "strings" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0d99045 | 2021-08-11 16:46:13 +0000 | [diff] [blame] | 20 | |
| 21 | "android/soong/bazel" |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 22 | "android/soong/bazel/cquery" |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 23 | |
| 24 | "github.com/google/blueprint" |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func init() { |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 28 | RegisterModuleType("filegroup", FileGroupFactory) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 29 | } |
| 30 | |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 31 | var PrepareForTestWithFilegroup = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 32 | ctx.RegisterModuleType("filegroup", FileGroupFactory) |
| 33 | }) |
| 34 | |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 35 | // IsFilegroup checks that a module is a filegroup type |
| 36 | func IsFilegroup(ctx bazel.OtherModuleContext, m blueprint.Module) bool { |
| 37 | return ctx.OtherModuleType(m) == "filegroup" |
| 38 | } |
| 39 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 40 | // https://docs.bazel.build/versions/master/be/general.html#filegroup |
| 41 | type bazelFilegroupAttributes struct { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 42 | Srcs bazel.LabelListAttribute |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame^] | 45 | type bazelAidlLibraryAttributes struct { |
| 46 | Srcs bazel.LabelListAttribute |
| 47 | Strip_import_prefix *string |
| 48 | } |
| 49 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 50 | // ConvertWithBp2build performs bp2build conversion of filegroup |
| 51 | func (fg *fileGroup) ConvertWithBp2build(ctx TopDownMutatorContext) { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 52 | srcs := bazel.MakeLabelListAttribute( |
| 53 | BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 54 | |
| 55 | // For Bazel compatibility, don't generate the filegroup if there is only 1 |
| 56 | // source file, and that the source file is named the same as the module |
| 57 | // itself. In Bazel, eponymous filegroups like this would be an error. |
| 58 | // |
| 59 | // Instead, dependents on this single-file filegroup can just depend |
| 60 | // on the file target, instead of rule target, directly. |
| 61 | // |
| 62 | // You may ask: what if a filegroup has multiple files, and one of them |
| 63 | // shares the name? The answer: we haven't seen that in the wild, and |
| 64 | // should lock Soong itself down to prevent the behavior. For now, |
| 65 | // we raise an error if bp2build sees this problem. |
| 66 | for _, f := range srcs.Value.Includes { |
| 67 | if f.Label == fg.Name() { |
| 68 | if len(srcs.Value.Includes) > 1 { |
| 69 | ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name()) |
| 70 | } |
| 71 | return |
| 72 | } |
| 73 | } |
| 74 | |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame^] | 75 | // Convert module that has only AIDL files to aidl_library |
| 76 | // If the module has a mixed bag of AIDL and non-AIDL files, split the filegroup manually |
| 77 | // and then convert |
| 78 | if fg.ShouldConvertToAidlLibrary(ctx) { |
| 79 | attrs := &bazelAidlLibraryAttributes{ |
| 80 | Srcs: srcs, |
| 81 | Strip_import_prefix: fg.properties.Path, |
| 82 | } |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 83 | |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame^] | 84 | props := bazel.BazelTargetModuleProperties{ |
| 85 | Rule_class: "aidl_library", |
| 86 | Bzl_load_location: "//build/bazel/rules/aidl:library.bzl", |
| 87 | } |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 88 | |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame^] | 89 | ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs) |
| 90 | } else { |
| 91 | attrs := &bazelFilegroupAttributes{ |
| 92 | Srcs: srcs, |
| 93 | } |
| 94 | |
| 95 | props := bazel.BazelTargetModuleProperties{ |
| 96 | Rule_class: "filegroup", |
| 97 | Bzl_load_location: "//build/bazel/rules:filegroup.bzl", |
| 98 | } |
| 99 | |
| 100 | ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs) |
| 101 | } |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | type fileGroupProperties struct { |
| 105 | // srcs lists files that will be included in this filegroup |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 106 | Srcs []string `android:"path"` |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 107 | |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 108 | Exclude_srcs []string `android:"path"` |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 109 | |
| 110 | // The base path to the files. May be used by other modules to determine which portion |
| 111 | // of the path to use. For example, when a filegroup is used as data in a cc_test rule, |
| 112 | // the base path is stripped off the path and the remaining path is used as the |
| 113 | // installation directory. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 114 | Path *string |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 115 | |
| 116 | // Create a make variable with the specified name that contains the list of files in the |
| 117 | // filegroup, relative to the root of the source tree. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 118 | Export_to_make_var *string |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | type fileGroup struct { |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 122 | ModuleBase |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 123 | BazelModuleBase |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame^] | 124 | Bp2buildAidlLibrary |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 125 | properties fileGroupProperties |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 126 | srcs Paths |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 129 | var _ MixedBuildBuildable = (*fileGroup)(nil) |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 130 | var _ SourceFileProducer = (*fileGroup)(nil) |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame^] | 131 | var _ Bp2buildAidlLibrary = (*fileGroup)(nil) |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 132 | |
Patrice Arruda | 8958a94 | 2019-03-12 10:06:00 -0700 | [diff] [blame] | 133 | // filegroup contains a list of files that are referenced by other modules |
| 134 | // properties (such as "srcs") using the syntax ":<name>". filegroup are |
| 135 | // also be used to export files across package boundaries. |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 136 | func FileGroupFactory() Module { |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 137 | module := &fileGroup{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 138 | module.AddProperties(&module.properties) |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 139 | InitAndroidModule(module) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 140 | InitBazelModule(module) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 141 | return module |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Liz Kammer | 5edc141 | 2022-05-25 11:12:44 -0400 | [diff] [blame] | 144 | var _ blueprint.JSONActionSupplier = (*fileGroup)(nil) |
| 145 | |
| 146 | func (fg *fileGroup) JSONActions() []blueprint.JSONAction { |
| 147 | ins := make([]string, 0, len(fg.srcs)) |
| 148 | outs := make([]string, 0, len(fg.srcs)) |
| 149 | for _, p := range fg.srcs { |
| 150 | ins = append(ins, p.String()) |
| 151 | outs = append(outs, p.Rel()) |
| 152 | } |
| 153 | return []blueprint.JSONAction{ |
| 154 | blueprint.JSONAction{ |
| 155 | Inputs: ins, |
| 156 | Outputs: outs, |
| 157 | }, |
| 158 | } |
| 159 | } |
| 160 | |
Liz Kammer | 5bde22f | 2021-04-19 14:04:14 -0400 | [diff] [blame] | 161 | func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) { |
Liz Kammer | 5bde22f | 2021-04-19 14:04:14 -0400 | [diff] [blame] | 162 | fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs) |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 163 | if fg.properties.Path != nil { |
| 164 | fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path)) |
| 165 | } |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 168 | func (fg *fileGroup) Srcs() Paths { |
| 169 | return append(Paths{}, fg.srcs...) |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 170 | } |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 171 | |
Dan Willemsen | 6a6478d | 2020-07-17 19:28:53 -0700 | [diff] [blame] | 172 | func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) { |
| 173 | if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" { |
| 174 | ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " ")) |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 177 | |
| 178 | func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) { |
| 179 | bazelCtx := ctx.Config().BazelContext |
| 180 | |
| 181 | bazelCtx.QueueBazelRequest( |
| 182 | fg.GetBazelLabel(ctx, fg), |
| 183 | cquery.GetOutputFiles, |
| 184 | configKey{Common.String(), CommonOS}) |
| 185 | } |
| 186 | |
| 187 | func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool { |
| 188 | return true |
| 189 | } |
| 190 | |
| 191 | func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) { |
Vinh Tran | 16fe8e1 | 2022-08-16 16:45:44 -0400 | [diff] [blame] | 192 | bazelCtx := ctx.Config().BazelContext |
| 193 | // This is a short-term solution because we rely on info from Android.bp to handle |
| 194 | // a converted module. This will block when we want to remove Android.bp for all |
| 195 | // converted modules at some point. |
| 196 | // TODO(b/242847534): Implement a long-term solution in which we don't need to rely |
| 197 | // on info form Android.bp for modules that are already converted to Bazel |
| 198 | relativeRoot := ctx.ModuleDir() |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 199 | if fg.properties.Path != nil { |
Vinh Tran | 16fe8e1 | 2022-08-16 16:45:44 -0400 | [diff] [blame] | 200 | relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 203 | filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{Common.String(), CommonOS}) |
| 204 | if err != nil { |
| 205 | ctx.ModuleErrorf(err.Error()) |
| 206 | return |
| 207 | } |
| 208 | |
| 209 | bazelOuts := make(Paths, 0, len(filePaths)) |
| 210 | for _, p := range filePaths { |
Vinh Tran | 16fe8e1 | 2022-08-16 16:45:44 -0400 | [diff] [blame] | 211 | bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p)) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 212 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 213 | fg.srcs = bazelOuts |
| 214 | } |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame^] | 215 | |
| 216 | func (fg *fileGroup) ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool { |
| 217 | if len(fg.properties.Srcs) == 0 || !fg.ShouldConvertWithBp2build(ctx) { |
| 218 | return false |
| 219 | } |
| 220 | for _, src := range fg.properties.Srcs { |
| 221 | if !strings.HasSuffix(src, ".aidl") { |
| 222 | return false |
| 223 | } |
| 224 | } |
| 225 | return true |
| 226 | } |
| 227 | |
| 228 | func (fg *fileGroup) GetAidlLibraryLabel(ctx BazelConversionPathContext) string { |
| 229 | if ctx.OtherModuleDir(fg.module) == ctx.ModuleDir() { |
| 230 | return ":" + fg.Name() |
| 231 | } else { |
| 232 | return fg.GetBazelLabel(ctx, fg) |
| 233 | } |
| 234 | } |