blob: ff0f74e9990eddd9143448edbfcef9044ca87044 [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
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 Zhangea568a42017-11-08 21:20:04 -080048 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -070049
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 Zhangea568a42017-11-08 21:20:04 -080052 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -080053}
54
55type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070056 ModuleBase
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000057 DefaultableModuleBase
Colin Cross068e0fe2016-12-13 15:23:47 -080058 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070059 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -080060}
61
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070062var _ SourceFileProducer = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -080063
Patrice Arruda8958a942019-03-12 10:06:00 -070064// 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 Nainar955dc492018-04-17 14:58:42 -070067func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -080068 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -070069 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070070 InitAndroidModule(module)
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000071 InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -070072 return module
Colin Cross068e0fe2016-12-13 15:23:47 -080073}
74
Liz Kammer5edc1412022-05-25 11:12:44 -040075var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
76
77func (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 Kammer5bde22f2021-04-19 14:04:14 -040092func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Cole Faustfdec8722024-05-22 11:38:29 -070093 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs.GetOrDefault(ctx, nil), fg.properties.Exclude_srcs.GetOrDefault(ctx, nil))
Colin Cross2fafa3e2019-03-05 12:39:51 -080094 if fg.properties.Path != nil {
95 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
96 }
Colin Cross40213022023-12-13 15:19:49 -080097 SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: fg.srcs.Strings()})
Jihoon Kang705e63e2024-03-13 01:21:16 +000098
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 Cross068e0fe2016-12-13 15:23:47 -0800117}
118
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700119func (fg *fileGroup) Srcs() Paths {
120 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800121}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700122
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700123func (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 Crossd91d7ac2017-09-12 22:52:12 -0700126 }
127}
Chris Parsonsf874e462022-05-10 13:50:12 -0400128
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000129// Defaults
130type FileGroupDefaults struct {
131 ModuleBase
132 DefaultsModuleBase
133}
134
135func FileGroupDefaultsFactory() Module {
136 module := &FileGroupDefaults{}
137 module.AddProperties(&fileGroupProperties{})
138 InitDefaultsModule(module)
139
140 return module
141}