blob: 0e08c4fa97b5e8296ce0dc9418e38b1527105369 [file] [log] [blame]
Colin Cross068e0fe2016-12-13 15:23:47 -08001// 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 Nainar955dc492018-04-17 14:58:42 -070015package android
Colin Cross068e0fe2016-12-13 15:23:47 -080016
17import (
Jihoon Kang705e63e2024-03-13 01:21:16 +000018 "maps"
Colin Crossd91d7ac2017-09-12 22:52:12 -070019 "strings"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000020
Sam Delmericoc7681022022-02-04 21:01:20 +000021 "github.com/google/blueprint"
Cole Faustfdec8722024-05-22 11:38:29 -070022 "github.com/google/blueprint/proptools"
Colin Cross068e0fe2016-12-13 15:23:47 -080023)
24
25func init() {
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000026 RegisterFilegroupBuildComponents(InitRegistrationContext)
Jingwen Chen32b4ece2021-01-21 03:20:18 -050027}
28
Paul Duffin35816122021-02-24 01:49:52 +000029var PrepareForTestWithFilegroup = FixtureRegisterWithContext(func(ctx RegistrationContext) {
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000030 RegisterFilegroupBuildComponents(ctx)
Paul Duffin35816122021-02-24 01:49:52 +000031})
32
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000033func RegisterFilegroupBuildComponents(ctx RegistrationContext) {
34 ctx.RegisterModuleType("filegroup", FileGroupFactory)
35 ctx.RegisterModuleType("filegroup_defaults", FileGroupDefaultsFactory)
36}
37
Colin Cross068e0fe2016-12-13 15:23:47 -080038type fileGroupProperties struct {
39 // srcs lists files that will be included in this filegroup
Cole Faustfdec8722024-05-22 11:38:29 -070040 Srcs proptools.Configurable[[]string] `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -080041
Cole Faustfdec8722024-05-22 11:38:29 -070042 Exclude_srcs proptools.Configurable[[]string] `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -080043
Cole Faust65cb40a2024-10-21 15:41:42 -070044 // Sources the will be included in the filegroup, but any module dependencies will be added
45 // using the device os and the device's first architecture's variant.
46 Device_first_srcs proptools.Configurable[[]string] `android:"path_device_first"`
47
48 // Sources the will be included in the filegroup, but any module dependencies will be added
49 // using the device os and the common architecture's variant.
50 Device_common_srcs proptools.Configurable[[]string] `android:"path_device_common"`
51
Colin Crossfaeb7aa2017-02-01 14:12:44 -080052 // The base path to the files. May be used by other modules to determine which portion
53 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
54 // the base path is stripped off the path and the remaining path is used as the
55 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -080056 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -070057
58 // Create a make variable with the specified name that contains the list of files in the
59 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -080060 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -080061}
62
63type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070064 ModuleBase
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000065 DefaultableModuleBase
Colin Cross068e0fe2016-12-13 15:23:47 -080066 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070067 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -080068}
69
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070070var _ SourceFileProducer = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -080071
Patrice Arruda8958a942019-03-12 10:06:00 -070072// filegroup contains a list of files that are referenced by other modules
73// properties (such as "srcs") using the syntax ":<name>". filegroup are
74// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070075func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -080076 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -070077 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070078 InitAndroidModule(module)
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000079 InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -070080 return module
Colin Cross068e0fe2016-12-13 15:23:47 -080081}
82
Liz Kammer5edc1412022-05-25 11:12:44 -040083var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
84
85func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
86 ins := make([]string, 0, len(fg.srcs))
87 outs := make([]string, 0, len(fg.srcs))
88 for _, p := range fg.srcs {
89 ins = append(ins, p.String())
90 outs = append(outs, p.Rel())
91 }
92 return []blueprint.JSONAction{
93 blueprint.JSONAction{
94 Inputs: ins,
95 Outputs: outs,
96 },
97 }
98}
99
Liz Kammer5bde22f2021-04-19 14:04:14 -0400100func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Cole Faust65cb40a2024-10-21 15:41:42 -0700101 srcs := PathsForModuleSrcExcludes(ctx, fg.properties.Srcs.GetOrDefault(ctx, nil), fg.properties.Exclude_srcs.GetOrDefault(ctx, nil))
102 srcs = append(srcs, PathsForModuleSrc(ctx, fg.properties.Device_first_srcs.GetOrDefault(ctx, nil))...)
103 srcs = append(srcs, PathsForModuleSrc(ctx, fg.properties.Device_common_srcs.GetOrDefault(ctx, nil))...)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800104 if fg.properties.Path != nil {
Cole Faust65cb40a2024-10-21 15:41:42 -0700105 srcs = PathsWithModuleSrcSubDir(ctx, srcs, String(fg.properties.Path))
Colin Cross2fafa3e2019-03-05 12:39:51 -0800106 }
Cole Faust65cb40a2024-10-21 15:41:42 -0700107 SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: srcs.Strings()})
Jihoon Kang705e63e2024-03-13 01:21:16 +0000108
109 var aconfigDeclarations []string
110 var intermediateCacheOutputPaths Paths
111 var srcjars Paths
112 modeInfos := make(map[string]ModeInfo)
113 ctx.VisitDirectDeps(func(module Module) {
114 if dep, ok := OtherModuleProvider(ctx, module, CodegenInfoProvider); ok {
115 aconfigDeclarations = append(aconfigDeclarations, dep.AconfigDeclarations...)
116 intermediateCacheOutputPaths = append(intermediateCacheOutputPaths, dep.IntermediateCacheOutputPaths...)
117 srcjars = append(srcjars, dep.Srcjars...)
118 maps.Copy(modeInfos, dep.ModeInfos)
119 }
120 })
Cole Faust65cb40a2024-10-21 15:41:42 -0700121
122 fg.srcs = srcs
Jihoon Kang705e63e2024-03-13 01:21:16 +0000123 SetProvider(ctx, CodegenInfoProvider, CodegenInfo{
124 AconfigDeclarations: aconfigDeclarations,
125 IntermediateCacheOutputPaths: intermediateCacheOutputPaths,
126 Srcjars: srcjars,
127 ModeInfos: modeInfos,
128 })
Colin Cross068e0fe2016-12-13 15:23:47 -0800129}
130
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700131func (fg *fileGroup) Srcs() Paths {
132 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800133}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700134
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700135func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
136 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
137 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700138 }
139}
Chris Parsonsf874e462022-05-10 13:50:12 -0400140
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000141// Defaults
142type FileGroupDefaults struct {
143 ModuleBase
144 DefaultsModuleBase
145}
146
147func FileGroupDefaultsFactory() Module {
148 module := &FileGroupDefaults{}
149 module.AddProperties(&fileGroupProperties{})
150 InitDefaultsModule(module)
151
152 return module
153}