blob: 7d39238144f011dc888c4d2ffaa66d9b4cebaa28 [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 (
Vinh Tran16fe8e12022-08-16 16:45:44 -040018 "path/filepath"
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
21 "android/soong/bazel"
Chris Parsonsf874e462022-05-10 13:50:12 -040022 "android/soong/bazel/cquery"
Sam Delmericoc7681022022-02-04 21:01:20 +000023
24 "github.com/google/blueprint"
Colin Cross068e0fe2016-12-13 15:23:47 -080025)
26
27func init() {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070028 RegisterModuleType("filegroup", FileGroupFactory)
Jingwen Chen32b4ece2021-01-21 03:20:18 -050029}
30
Paul Duffin35816122021-02-24 01:49:52 +000031var PrepareForTestWithFilegroup = FixtureRegisterWithContext(func(ctx RegistrationContext) {
32 ctx.RegisterModuleType("filegroup", FileGroupFactory)
33})
34
Sam Delmericoc7681022022-02-04 21:01:20 +000035// IsFilegroup checks that a module is a filegroup type
36func IsFilegroup(ctx bazel.OtherModuleContext, m blueprint.Module) bool {
37 return ctx.OtherModuleType(m) == "filegroup"
38}
39
Jingwen Chen32b4ece2021-01-21 03:20:18 -050040// https://docs.bazel.build/versions/master/be/general.html#filegroup
41type bazelFilegroupAttributes struct {
Jingwen Chen07027912021-03-15 06:02:43 -040042 Srcs bazel.LabelListAttribute
Jingwen Chen32b4ece2021-01-21 03:20:18 -050043}
44
Vinh Tran444154d2022-08-16 13:10:31 -040045type bazelAidlLibraryAttributes struct {
46 Srcs bazel.LabelListAttribute
47 Strip_import_prefix *string
48}
49
Liz Kammerbe46fcc2021-11-01 15:32:43 -040050// ConvertWithBp2build performs bp2build conversion of filegroup
51func (fg *fileGroup) ConvertWithBp2build(ctx TopDownMutatorContext) {
Jingwen Chen07027912021-03-15 06:02:43 -040052 srcs := bazel.MakeLabelListAttribute(
53 BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs))
Jingwen Chen5146ac02021-09-02 11:44:42 +000054
55 // For Bazel compatibility, don't generate the filegroup if there is only 1
56 // source file, and that the source file is named the same as the module
57 // itself. In Bazel, eponymous filegroups like this would be an error.
58 //
59 // Instead, dependents on this single-file filegroup can just depend
60 // on the file target, instead of rule target, directly.
61 //
62 // You may ask: what if a filegroup has multiple files, and one of them
63 // shares the name? The answer: we haven't seen that in the wild, and
64 // should lock Soong itself down to prevent the behavior. For now,
65 // we raise an error if bp2build sees this problem.
66 for _, f := range srcs.Value.Includes {
67 if f.Label == fg.Name() {
68 if len(srcs.Value.Includes) > 1 {
69 ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name())
70 }
71 return
72 }
73 }
74
Vinh Tran444154d2022-08-16 13:10:31 -040075 // Convert module that has only AIDL files to aidl_library
76 // If the module has a mixed bag of AIDL and non-AIDL files, split the filegroup manually
77 // and then convert
78 if fg.ShouldConvertToAidlLibrary(ctx) {
79 attrs := &bazelAidlLibraryAttributes{
80 Srcs: srcs,
81 Strip_import_prefix: fg.properties.Path,
82 }
Jingwen Chen1fd14692021-02-05 03:01:50 -050083
Vinh Tran444154d2022-08-16 13:10:31 -040084 props := bazel.BazelTargetModuleProperties{
85 Rule_class: "aidl_library",
86 Bzl_load_location: "//build/bazel/rules/aidl:library.bzl",
87 }
Jingwen Chen1fd14692021-02-05 03:01:50 -050088
Vinh Tran444154d2022-08-16 13:10:31 -040089 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
90 } else {
91 attrs := &bazelFilegroupAttributes{
92 Srcs: srcs,
93 }
94
95 props := bazel.BazelTargetModuleProperties{
96 Rule_class: "filegroup",
97 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
98 }
99
100 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
101 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800102}
103
104type fileGroupProperties struct {
105 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -0800106 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -0800107
Colin Cross27b922f2019-03-04 22:35:41 -0800108 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800109
110 // The base path to the files. May be used by other modules to determine which portion
111 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
112 // the base path is stripped off the path and the remaining path is used as the
113 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -0800114 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -0700115
116 // Create a make variable with the specified name that contains the list of files in the
117 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -0800118 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -0800119}
120
121type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700122 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500123 BazelModuleBase
Vinh Tran444154d2022-08-16 13:10:31 -0400124 Bp2buildAidlLibrary
Colin Cross068e0fe2016-12-13 15:23:47 -0800125 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700126 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -0800127}
128
Chris Parsonsf874e462022-05-10 13:50:12 -0400129var _ MixedBuildBuildable = (*fileGroup)(nil)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700130var _ SourceFileProducer = (*fileGroup)(nil)
Vinh Tran444154d2022-08-16 13:10:31 -0400131var _ Bp2buildAidlLibrary = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -0800132
Patrice Arruda8958a942019-03-12 10:06:00 -0700133// filegroup contains a list of files that are referenced by other modules
134// properties (such as "srcs") using the syntax ":<name>". filegroup are
135// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700136func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -0800137 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -0700138 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700139 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500140 InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700141 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800142}
143
Liz Kammer5edc1412022-05-25 11:12:44 -0400144var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
145
146func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
147 ins := make([]string, 0, len(fg.srcs))
148 outs := make([]string, 0, len(fg.srcs))
149 for _, p := range fg.srcs {
150 ins = append(ins, p.String())
151 outs = append(outs, p.Rel())
152 }
153 return []blueprint.JSONAction{
154 blueprint.JSONAction{
155 Inputs: ins,
156 Outputs: outs,
157 },
158 }
159}
160
Liz Kammer5bde22f2021-04-19 14:04:14 -0400161func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400162 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800163 if fg.properties.Path != nil {
164 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
165 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800166}
167
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700168func (fg *fileGroup) Srcs() Paths {
169 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800170}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700171
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700172func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
173 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
174 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700175 }
176}
Chris Parsonsf874e462022-05-10 13:50:12 -0400177
178func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) {
179 bazelCtx := ctx.Config().BazelContext
180
181 bazelCtx.QueueBazelRequest(
182 fg.GetBazelLabel(ctx, fg),
183 cquery.GetOutputFiles,
184 configKey{Common.String(), CommonOS})
185}
186
187func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool {
188 return true
189}
190
191func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400192 bazelCtx := ctx.Config().BazelContext
193 // This is a short-term solution because we rely on info from Android.bp to handle
194 // a converted module. This will block when we want to remove Android.bp for all
195 // converted modules at some point.
196 // TODO(b/242847534): Implement a long-term solution in which we don't need to rely
197 // on info form Android.bp for modules that are already converted to Bazel
198 relativeRoot := ctx.ModuleDir()
Chris Parsonsf874e462022-05-10 13:50:12 -0400199 if fg.properties.Path != nil {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400200 relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path)
Chris Parsonsf874e462022-05-10 13:50:12 -0400201 }
202
Chris Parsonsf874e462022-05-10 13:50:12 -0400203 filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{Common.String(), CommonOS})
204 if err != nil {
205 ctx.ModuleErrorf(err.Error())
206 return
207 }
208
209 bazelOuts := make(Paths, 0, len(filePaths))
210 for _, p := range filePaths {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400211 bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p))
Chris Parsonsf874e462022-05-10 13:50:12 -0400212 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400213 fg.srcs = bazelOuts
214}
Vinh Tran444154d2022-08-16 13:10:31 -0400215
216func (fg *fileGroup) ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool {
217 if len(fg.properties.Srcs) == 0 || !fg.ShouldConvertWithBp2build(ctx) {
218 return false
219 }
220 for _, src := range fg.properties.Srcs {
221 if !strings.HasSuffix(src, ".aidl") {
222 return false
223 }
224 }
225 return true
226}
227
228func (fg *fileGroup) GetAidlLibraryLabel(ctx BazelConversionPathContext) string {
229 if ctx.OtherModuleDir(fg.module) == ctx.ModuleDir() {
230 return ":" + fg.Name()
231 } else {
232 return fg.GetBazelLabel(ctx, fg)
233 }
234}