blob: bc881ed97487ffa15e8add28df8fe5a35b9f50e1 [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 (
Colin Crossd91d7ac2017-09-12 22:52:12 -070018 "strings"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000019
Sam Delmericoc7681022022-02-04 21:01:20 +000020 "github.com/google/blueprint"
Colin Cross068e0fe2016-12-13 15:23:47 -080021)
22
23func init() {
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000024 RegisterFilegroupBuildComponents(InitRegistrationContext)
Jingwen Chen32b4ece2021-01-21 03:20:18 -050025}
26
Paul Duffin35816122021-02-24 01:49:52 +000027var PrepareForTestWithFilegroup = FixtureRegisterWithContext(func(ctx RegistrationContext) {
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000028 RegisterFilegroupBuildComponents(ctx)
Paul Duffin35816122021-02-24 01:49:52 +000029})
30
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000031func RegisterFilegroupBuildComponents(ctx RegistrationContext) {
32 ctx.RegisterModuleType("filegroup", FileGroupFactory)
33 ctx.RegisterModuleType("filegroup_defaults", FileGroupDefaultsFactory)
34}
35
Colin Cross068e0fe2016-12-13 15:23:47 -080036type fileGroupProperties struct {
37 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -080038 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -080039
Colin Cross27b922f2019-03-04 22:35:41 -080040 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -080041
42 // The base path to the files. May be used by other modules to determine which portion
43 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
44 // the base path is stripped off the path and the remaining path is used as the
45 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -080046 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -070047
48 // Create a make variable with the specified name that contains the list of files in the
49 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -080050 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -080051}
52
53type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070054 ModuleBase
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000055 DefaultableModuleBase
Colin Cross068e0fe2016-12-13 15:23:47 -080056 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070057 srcs Paths
LaMont Jonesafe7baf2024-01-09 22:47:39 +000058
59 // Aconfig files for all transitive deps. Also exposed via TransitiveDeclarationsInfo
60 mergedAconfigFiles map[string]Paths
Colin Cross068e0fe2016-12-13 15:23:47 -080061}
62
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070063var _ SourceFileProducer = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -080064
Patrice Arruda8958a942019-03-12 10:06:00 -070065// filegroup contains a list of files that are referenced by other modules
66// properties (such as "srcs") using the syntax ":<name>". filegroup are
67// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070068func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -080069 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -070070 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070071 InitAndroidModule(module)
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000072 InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -070073 return module
Colin Cross068e0fe2016-12-13 15:23:47 -080074}
75
Liz Kammer5edc1412022-05-25 11:12:44 -040076var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
77
78func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
79 ins := make([]string, 0, len(fg.srcs))
80 outs := make([]string, 0, len(fg.srcs))
81 for _, p := range fg.srcs {
82 ins = append(ins, p.String())
83 outs = append(outs, p.Rel())
84 }
85 return []blueprint.JSONAction{
86 blueprint.JSONAction{
87 Inputs: ins,
88 Outputs: outs,
89 },
90 }
91}
92
Liz Kammer5bde22f2021-04-19 14:04:14 -040093func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -040094 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -080095 if fg.properties.Path != nil {
96 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
97 }
Colin Cross40213022023-12-13 15:19:49 -080098 SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: fg.srcs.Strings()})
LaMont Jonesafe7baf2024-01-09 22:47:39 +000099 CollectDependencyAconfigFiles(ctx, &fg.mergedAconfigFiles)
Colin Cross068e0fe2016-12-13 15:23:47 -0800100}
101
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700102func (fg *fileGroup) Srcs() Paths {
103 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800104}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700105
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700106func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
107 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
108 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700109 }
110}
Chris Parsonsf874e462022-05-10 13:50:12 -0400111
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000112// Defaults
113type FileGroupDefaults struct {
114 ModuleBase
115 DefaultsModuleBase
116}
117
118func FileGroupDefaultsFactory() Module {
119 module := &FileGroupDefaults{}
120 module.AddProperties(&fileGroupProperties{})
121 InitDefaultsModule(module)
122
123 return module
124}