blob: 14ed78312de570d0291db10c8d58f7ed744b37b9 [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
20 "android/soong/bazel"
Chris Parsonsf874e462022-05-10 13:50:12 -040021 "android/soong/bazel/cquery"
Sam Delmericoc7681022022-02-04 21:01:20 +000022
23 "github.com/google/blueprint"
Colin Cross068e0fe2016-12-13 15:23:47 -080024)
25
26func init() {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070027 RegisterModuleType("filegroup", FileGroupFactory)
Jingwen Chen32b4ece2021-01-21 03:20:18 -050028}
29
Paul Duffin35816122021-02-24 01:49:52 +000030var PrepareForTestWithFilegroup = FixtureRegisterWithContext(func(ctx RegistrationContext) {
31 ctx.RegisterModuleType("filegroup", FileGroupFactory)
32})
33
Sam Delmericoc7681022022-02-04 21:01:20 +000034// IsFilegroup checks that a module is a filegroup type
35func IsFilegroup(ctx bazel.OtherModuleContext, m blueprint.Module) bool {
36 return ctx.OtherModuleType(m) == "filegroup"
37}
38
Jingwen Chen32b4ece2021-01-21 03:20:18 -050039// https://docs.bazel.build/versions/master/be/general.html#filegroup
40type bazelFilegroupAttributes struct {
Jingwen Chen07027912021-03-15 06:02:43 -040041 Srcs bazel.LabelListAttribute
Jingwen Chen32b4ece2021-01-21 03:20:18 -050042}
43
Liz Kammerbe46fcc2021-11-01 15:32:43 -040044// ConvertWithBp2build performs bp2build conversion of filegroup
45func (fg *fileGroup) ConvertWithBp2build(ctx TopDownMutatorContext) {
Jingwen Chen07027912021-03-15 06:02:43 -040046 srcs := bazel.MakeLabelListAttribute(
47 BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs))
Jingwen Chen5146ac02021-09-02 11:44:42 +000048
49 // For Bazel compatibility, don't generate the filegroup if there is only 1
50 // source file, and that the source file is named the same as the module
51 // itself. In Bazel, eponymous filegroups like this would be an error.
52 //
53 // Instead, dependents on this single-file filegroup can just depend
54 // on the file target, instead of rule target, directly.
55 //
56 // You may ask: what if a filegroup has multiple files, and one of them
57 // shares the name? The answer: we haven't seen that in the wild, and
58 // should lock Soong itself down to prevent the behavior. For now,
59 // we raise an error if bp2build sees this problem.
60 for _, f := range srcs.Value.Includes {
61 if f.Label == fg.Name() {
62 if len(srcs.Value.Includes) > 1 {
63 ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name())
64 }
65 return
66 }
67 }
68
Jingwen Chen1fd14692021-02-05 03:01:50 -050069 attrs := &bazelFilegroupAttributes{
Jingwen Chen07027912021-03-15 06:02:43 -040070 Srcs: srcs,
Jingwen Chen1fd14692021-02-05 03:01:50 -050071 }
72
Jingwen Chen14a8bda2021-06-02 11:10:02 +000073 props := bazel.BazelTargetModuleProperties{
74 Rule_class: "filegroup",
75 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
76 }
Jingwen Chen1fd14692021-02-05 03:01:50 -050077
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000078 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
Colin Cross068e0fe2016-12-13 15:23:47 -080079}
80
81type fileGroupProperties struct {
82 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -080083 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -080084
Colin Cross27b922f2019-03-04 22:35:41 -080085 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -080086
87 // The base path to the files. May be used by other modules to determine which portion
88 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
89 // the base path is stripped off the path and the remaining path is used as the
90 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -080091 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -070092
93 // Create a make variable with the specified name that contains the list of files in the
94 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -080095 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -080096}
97
98type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070099 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500100 BazelModuleBase
Colin Cross068e0fe2016-12-13 15:23:47 -0800101 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700102 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -0800103}
104
Chris Parsonsf874e462022-05-10 13:50:12 -0400105var _ MixedBuildBuildable = (*fileGroup)(nil)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700106var _ SourceFileProducer = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -0800107
Patrice Arruda8958a942019-03-12 10:06:00 -0700108// filegroup contains a list of files that are referenced by other modules
109// properties (such as "srcs") using the syntax ":<name>". filegroup are
110// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700111func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -0800112 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -0700113 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700114 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500115 InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700116 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800117}
118
Liz Kammer5edc1412022-05-25 11:12:44 -0400119var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
120
121func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
122 ins := make([]string, 0, len(fg.srcs))
123 outs := make([]string, 0, len(fg.srcs))
124 for _, p := range fg.srcs {
125 ins = append(ins, p.String())
126 outs = append(outs, p.Rel())
127 }
128 return []blueprint.JSONAction{
129 blueprint.JSONAction{
130 Inputs: ins,
131 Outputs: outs,
132 },
133 }
134}
135
Liz Kammer5bde22f2021-04-19 14:04:14 -0400136func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400137 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800138 if fg.properties.Path != nil {
139 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
140 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800141}
142
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700143func (fg *fileGroup) Srcs() Paths {
144 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800145}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700146
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700147func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
148 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
149 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700150 }
151}
Chris Parsonsf874e462022-05-10 13:50:12 -0400152
153func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) {
154 bazelCtx := ctx.Config().BazelContext
155
156 bazelCtx.QueueBazelRequest(
157 fg.GetBazelLabel(ctx, fg),
158 cquery.GetOutputFiles,
159 configKey{Common.String(), CommonOS})
160}
161
162func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool {
163 return true
164}
165
166func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) {
167 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
168 if fg.properties.Path != nil {
169 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
170 }
171
172 bazelCtx := ctx.Config().BazelContext
173 filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{Common.String(), CommonOS})
174 if err != nil {
175 ctx.ModuleErrorf(err.Error())
176 return
177 }
178
179 bazelOuts := make(Paths, 0, len(filePaths))
180 for _, p := range filePaths {
181 src := PathForBazelOut(ctx, p)
182 bazelOuts = append(bazelOuts, src)
183 }
184
185 fg.srcs = bazelOuts
186}