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