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