blob: 86d7b4bbd5d167444f41ff78f5e021728be97373 [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"
Colin Cross068e0fe2016-12-13 15:23:47 -080022)
23
24func init() {
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000025 RegisterFilegroupBuildComponents(InitRegistrationContext)
Jingwen Chen32b4ece2021-01-21 03:20:18 -050026}
27
Paul Duffin35816122021-02-24 01:49:52 +000028var PrepareForTestWithFilegroup = FixtureRegisterWithContext(func(ctx RegistrationContext) {
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000029 RegisterFilegroupBuildComponents(ctx)
Paul Duffin35816122021-02-24 01:49:52 +000030})
31
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000032func RegisterFilegroupBuildComponents(ctx RegistrationContext) {
33 ctx.RegisterModuleType("filegroup", FileGroupFactory)
34 ctx.RegisterModuleType("filegroup_defaults", FileGroupDefaultsFactory)
35}
36
Colin Cross068e0fe2016-12-13 15:23:47 -080037type fileGroupProperties struct {
38 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -080039 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -080040
Colin Cross27b922f2019-03-04 22:35:41 -080041 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -080042
43 // The base path to the files. May be used by other modules to determine which portion
44 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
45 // the base path is stripped off the path and the remaining path is used as the
46 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -080047 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -070048
49 // Create a make variable with the specified name that contains the list of files in the
50 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -080051 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -080052}
53
54type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070055 ModuleBase
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000056 DefaultableModuleBase
Colin Cross068e0fe2016-12-13 15:23:47 -080057 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070058 srcs Paths
LaMont Jonesafe7baf2024-01-09 22:47:39 +000059
60 // Aconfig files for all transitive deps. Also exposed via TransitiveDeclarationsInfo
61 mergedAconfigFiles map[string]Paths
Colin Cross068e0fe2016-12-13 15:23:47 -080062}
63
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070064var _ SourceFileProducer = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -080065
Patrice Arruda8958a942019-03-12 10:06:00 -070066// filegroup contains a list of files that are referenced by other modules
67// properties (such as "srcs") using the syntax ":<name>". filegroup are
68// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070069func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -080070 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -070071 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070072 InitAndroidModule(module)
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000073 InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -070074 return module
Colin Cross068e0fe2016-12-13 15:23:47 -080075}
76
Liz Kammer5edc1412022-05-25 11:12:44 -040077var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
78
79func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
80 ins := make([]string, 0, len(fg.srcs))
81 outs := make([]string, 0, len(fg.srcs))
82 for _, p := range fg.srcs {
83 ins = append(ins, p.String())
84 outs = append(outs, p.Rel())
85 }
86 return []blueprint.JSONAction{
87 blueprint.JSONAction{
88 Inputs: ins,
89 Outputs: outs,
90 },
91 }
92}
93
Liz Kammer5bde22f2021-04-19 14:04:14 -040094func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -040095 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -080096 if fg.properties.Path != nil {
97 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
98 }
Colin Cross40213022023-12-13 15:19:49 -080099 SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: fg.srcs.Strings()})
LaMont Jonesafe7baf2024-01-09 22:47:39 +0000100 CollectDependencyAconfigFiles(ctx, &fg.mergedAconfigFiles)
Jihoon Kang705e63e2024-03-13 01:21:16 +0000101
102 var aconfigDeclarations []string
103 var intermediateCacheOutputPaths Paths
104 var srcjars Paths
105 modeInfos := make(map[string]ModeInfo)
106 ctx.VisitDirectDeps(func(module Module) {
107 if dep, ok := OtherModuleProvider(ctx, module, CodegenInfoProvider); ok {
108 aconfigDeclarations = append(aconfigDeclarations, dep.AconfigDeclarations...)
109 intermediateCacheOutputPaths = append(intermediateCacheOutputPaths, dep.IntermediateCacheOutputPaths...)
110 srcjars = append(srcjars, dep.Srcjars...)
111 maps.Copy(modeInfos, dep.ModeInfos)
112 }
113 })
114 SetProvider(ctx, CodegenInfoProvider, CodegenInfo{
115 AconfigDeclarations: aconfigDeclarations,
116 IntermediateCacheOutputPaths: intermediateCacheOutputPaths,
117 Srcjars: srcjars,
118 ModeInfos: modeInfos,
119 })
Colin Cross068e0fe2016-12-13 15:23:47 -0800120}
121
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700122func (fg *fileGroup) Srcs() Paths {
123 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800124}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700125
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700126func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
127 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
128 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700129 }
130}
Chris Parsonsf874e462022-05-10 13:50:12 -0400131
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000132// Defaults
133type FileGroupDefaults struct {
134 ModuleBase
135 DefaultsModuleBase
136}
137
138func FileGroupDefaultsFactory() Module {
139 module := &FileGroupDefaults{}
140 module.AddProperties(&fileGroupProperties{})
141 InitDefaultsModule(module)
142
143 return module
144}