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 ( |
Jihoon Kang | 705e63e | 2024-03-13 01:21:16 +0000 | [diff] [blame] | 18 | "maps" |
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 | |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Cole Faust | fdec872 | 2024-05-22 11:38:29 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | func init() { |
Anton Hansson | 7d6dd8b | 2023-03-06 11:26:17 +0000 | [diff] [blame] | 26 | RegisterFilegroupBuildComponents(InitRegistrationContext) |
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) { |
Anton Hansson | 7d6dd8b | 2023-03-06 11:26:17 +0000 | [diff] [blame] | 30 | RegisterFilegroupBuildComponents(ctx) |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 31 | }) |
| 32 | |
Anton Hansson | 7d6dd8b | 2023-03-06 11:26:17 +0000 | [diff] [blame] | 33 | func RegisterFilegroupBuildComponents(ctx RegistrationContext) { |
| 34 | ctx.RegisterModuleType("filegroup", FileGroupFactory) |
| 35 | ctx.RegisterModuleType("filegroup_defaults", FileGroupDefaultsFactory) |
| 36 | } |
| 37 | |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 38 | type fileGroupProperties struct { |
| 39 | // srcs lists files that will be included in this filegroup |
Cole Faust | fdec872 | 2024-05-22 11:38:29 -0700 | [diff] [blame] | 40 | Srcs proptools.Configurable[[]string] `android:"path"` |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 41 | |
Cole Faust | fdec872 | 2024-05-22 11:38:29 -0700 | [diff] [blame] | 42 | Exclude_srcs proptools.Configurable[[]string] `android:"path"` |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 43 | |
| 44 | // The base path to the files. May be used by other modules to determine which portion |
| 45 | // of the path to use. For example, when a filegroup is used as data in a cc_test rule, |
| 46 | // the base path is stripped off the path and the remaining path is used as the |
| 47 | // installation directory. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 48 | Path *string |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 49 | |
| 50 | // Create a make variable with the specified name that contains the list of files in the |
| 51 | // filegroup, relative to the root of the source tree. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 52 | Export_to_make_var *string |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | type fileGroup struct { |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 56 | ModuleBase |
Anton Hansson | 7d6dd8b | 2023-03-06 11:26:17 +0000 | [diff] [blame] | 57 | DefaultableModuleBase |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 58 | properties fileGroupProperties |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 59 | srcs Paths |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 62 | var _ SourceFileProducer = (*fileGroup)(nil) |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 63 | |
Patrice Arruda | 8958a94 | 2019-03-12 10:06:00 -0700 | [diff] [blame] | 64 | // filegroup contains a list of files that are referenced by other modules |
| 65 | // properties (such as "srcs") using the syntax ":<name>". filegroup are |
| 66 | // also be used to export files across package boundaries. |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 67 | func FileGroupFactory() Module { |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 68 | module := &fileGroup{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 69 | module.AddProperties(&module.properties) |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 70 | InitAndroidModule(module) |
Anton Hansson | 7d6dd8b | 2023-03-06 11:26:17 +0000 | [diff] [blame] | 71 | InitDefaultableModule(module) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 72 | return module |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Liz Kammer | 5edc141 | 2022-05-25 11:12:44 -0400 | [diff] [blame] | 75 | var _ blueprint.JSONActionSupplier = (*fileGroup)(nil) |
| 76 | |
| 77 | func (fg *fileGroup) JSONActions() []blueprint.JSONAction { |
| 78 | ins := make([]string, 0, len(fg.srcs)) |
| 79 | outs := make([]string, 0, len(fg.srcs)) |
| 80 | for _, p := range fg.srcs { |
| 81 | ins = append(ins, p.String()) |
| 82 | outs = append(outs, p.Rel()) |
| 83 | } |
| 84 | return []blueprint.JSONAction{ |
| 85 | blueprint.JSONAction{ |
| 86 | Inputs: ins, |
| 87 | Outputs: outs, |
| 88 | }, |
| 89 | } |
| 90 | } |
| 91 | |
Liz Kammer | 5bde22f | 2021-04-19 14:04:14 -0400 | [diff] [blame] | 92 | func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) { |
Cole Faust | fdec872 | 2024-05-22 11:38:29 -0700 | [diff] [blame] | 93 | fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs.GetOrDefault(ctx, nil), fg.properties.Exclude_srcs.GetOrDefault(ctx, nil)) |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 94 | if fg.properties.Path != nil { |
| 95 | fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path)) |
| 96 | } |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 97 | SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: fg.srcs.Strings()}) |
Jihoon Kang | 705e63e | 2024-03-13 01:21:16 +0000 | [diff] [blame] | 98 | |
| 99 | var aconfigDeclarations []string |
| 100 | var intermediateCacheOutputPaths Paths |
| 101 | var srcjars Paths |
| 102 | modeInfos := make(map[string]ModeInfo) |
| 103 | ctx.VisitDirectDeps(func(module Module) { |
| 104 | if dep, ok := OtherModuleProvider(ctx, module, CodegenInfoProvider); ok { |
| 105 | aconfigDeclarations = append(aconfigDeclarations, dep.AconfigDeclarations...) |
| 106 | intermediateCacheOutputPaths = append(intermediateCacheOutputPaths, dep.IntermediateCacheOutputPaths...) |
| 107 | srcjars = append(srcjars, dep.Srcjars...) |
| 108 | maps.Copy(modeInfos, dep.ModeInfos) |
| 109 | } |
| 110 | }) |
| 111 | SetProvider(ctx, CodegenInfoProvider, CodegenInfo{ |
| 112 | AconfigDeclarations: aconfigDeclarations, |
| 113 | IntermediateCacheOutputPaths: intermediateCacheOutputPaths, |
| 114 | Srcjars: srcjars, |
| 115 | ModeInfos: modeInfos, |
| 116 | }) |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 119 | func (fg *fileGroup) Srcs() Paths { |
| 120 | return append(Paths{}, fg.srcs...) |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 121 | } |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 122 | |
Dan Willemsen | 6a6478d | 2020-07-17 19:28:53 -0700 | [diff] [blame] | 123 | func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) { |
| 124 | if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" { |
| 125 | ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " ")) |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 128 | |
Anton Hansson | 7d6dd8b | 2023-03-06 11:26:17 +0000 | [diff] [blame] | 129 | // Defaults |
| 130 | type FileGroupDefaults struct { |
| 131 | ModuleBase |
| 132 | DefaultsModuleBase |
| 133 | } |
| 134 | |
| 135 | func FileGroupDefaultsFactory() Module { |
| 136 | module := &FileGroupDefaults{} |
| 137 | module.AddProperties(&fileGroupProperties{}) |
| 138 | InitDefaultsModule(module) |
| 139 | |
| 140 | return module |
| 141 | } |
Spandan Das | 5e63b25 | 2024-10-17 00:19:16 +0000 | [diff] [blame^] | 142 | |
| 143 | // Collect information for opening IDE project files in java/jdeps.go. |
| 144 | // Copied from build/soong/genrule/genrule.go |
| 145 | func (fg *fileGroup) IDEInfo(ctx BaseModuleContext, dpInfo *IdeInfo) { |
| 146 | dpInfo.Srcs = append(dpInfo.Srcs, fg.Srcs().Strings()...) |
| 147 | for _, src := range fg.properties.Srcs.GetOrDefault(ctx, nil) { |
| 148 | if mod, _ := SrcIsModuleWithTag(src); mod != "" { |
| 149 | // Register the module name without any tags in `Deps` |
| 150 | dpInfo.Deps = append(dpInfo.Deps, mod) |
| 151 | } |
| 152 | } |
| 153 | } |