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