blob: 38de8558f766cc0d255a5f3f9b36a7057c551b1c [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"
Sam Delmerico97bd1272022-08-25 14:45:31 -040019 "regexp"
Colin Crossd91d7ac2017-09-12 22:52:12 -070020 "strings"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000021
22 "android/soong/bazel"
Chris Parsonsf874e462022-05-10 13:50:12 -040023 "android/soong/bazel/cquery"
Sam Delmericoc7681022022-02-04 21:01:20 +000024
25 "github.com/google/blueprint"
Colin Cross068e0fe2016-12-13 15:23:47 -080026)
27
28func init() {
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000029 RegisterFilegroupBuildComponents(InitRegistrationContext)
Jingwen Chen32b4ece2021-01-21 03:20:18 -050030}
31
Paul Duffin35816122021-02-24 01:49:52 +000032var PrepareForTestWithFilegroup = FixtureRegisterWithContext(func(ctx RegistrationContext) {
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000033 RegisterFilegroupBuildComponents(ctx)
Paul Duffin35816122021-02-24 01:49:52 +000034})
35
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000036func RegisterFilegroupBuildComponents(ctx RegistrationContext) {
37 ctx.RegisterModuleType("filegroup", FileGroupFactory)
38 ctx.RegisterModuleType("filegroup_defaults", FileGroupDefaultsFactory)
39}
40
Yu Liu2aa806b2022-09-01 11:54:47 -070041var convertedProtoLibrarySuffix = "_bp2build_converted"
42
Sam Delmericoc7681022022-02-04 21:01:20 +000043// IsFilegroup checks that a module is a filegroup type
44func IsFilegroup(ctx bazel.OtherModuleContext, m blueprint.Module) bool {
45 return ctx.OtherModuleType(m) == "filegroup"
46}
47
Sam Delmerico97bd1272022-08-25 14:45:31 -040048var (
49 // ignoring case, checks for proto or protos as an independent word in the name, whether at the
50 // beginning, end, or middle. e.g. "proto.foo", "bar-protos", "baz_proto_srcs" would all match
51 filegroupLikelyProtoPattern = regexp.MustCompile("(?i)(^|[^a-z])proto(s)?([^a-z]|$)")
52 filegroupLikelyAidlPattern = regexp.MustCompile("(?i)(^|[^a-z])aidl([^a-z]|$)")
53
54 ProtoSrcLabelPartition = bazel.LabelPartition{
55 Extensions: []string{".proto"},
56 LabelMapper: isFilegroupWithPattern(filegroupLikelyProtoPattern),
57 }
58 AidlSrcLabelPartition = bazel.LabelPartition{
59 Extensions: []string{".aidl"},
60 LabelMapper: isFilegroupWithPattern(filegroupLikelyAidlPattern),
61 }
62)
63
64func isFilegroupWithPattern(pattern *regexp.Regexp) bazel.LabelMapper {
65 return func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) {
66 m, exists := ctx.ModuleFromName(label.OriginalModuleName)
67 labelStr := label.Label
68 if !exists || !IsFilegroup(ctx, m) {
69 return labelStr, false
70 }
71 likelyMatched := pattern.MatchString(label.OriginalModuleName)
72 return labelStr, likelyMatched
73 }
74}
75
Jingwen Chen32b4ece2021-01-21 03:20:18 -050076// https://docs.bazel.build/versions/master/be/general.html#filegroup
77type bazelFilegroupAttributes struct {
Jingwen Chen07027912021-03-15 06:02:43 -040078 Srcs bazel.LabelListAttribute
Jingwen Chen32b4ece2021-01-21 03:20:18 -050079}
80
Vinh Tran444154d2022-08-16 13:10:31 -040081type bazelAidlLibraryAttributes struct {
82 Srcs bazel.LabelListAttribute
83 Strip_import_prefix *string
84}
85
Spandan Dasbd52ea92023-03-09 23:03:07 +000086// api srcs can be contained in filegroups.
87// this should be generated in api_bp2build workspace as well.
88func (fg *fileGroup) ConvertWithApiBp2build(ctx TopDownMutatorContext) {
89 fg.ConvertWithBp2build(ctx)
90}
91
Liz Kammerbe46fcc2021-11-01 15:32:43 -040092// ConvertWithBp2build performs bp2build conversion of filegroup
93func (fg *fileGroup) ConvertWithBp2build(ctx TopDownMutatorContext) {
Jingwen Chen07027912021-03-15 06:02:43 -040094 srcs := bazel.MakeLabelListAttribute(
95 BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs))
Jingwen Chen5146ac02021-09-02 11:44:42 +000096
97 // For Bazel compatibility, don't generate the filegroup if there is only 1
98 // source file, and that the source file is named the same as the module
99 // itself. In Bazel, eponymous filegroups like this would be an error.
100 //
101 // Instead, dependents on this single-file filegroup can just depend
102 // on the file target, instead of rule target, directly.
103 //
104 // You may ask: what if a filegroup has multiple files, and one of them
105 // shares the name? The answer: we haven't seen that in the wild, and
106 // should lock Soong itself down to prevent the behavior. For now,
107 // we raise an error if bp2build sees this problem.
108 for _, f := range srcs.Value.Includes {
109 if f.Label == fg.Name() {
110 if len(srcs.Value.Includes) > 1 {
111 ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name())
112 }
113 return
114 }
115 }
116
Vinh Tran444154d2022-08-16 13:10:31 -0400117 // Convert module that has only AIDL files to aidl_library
118 // If the module has a mixed bag of AIDL and non-AIDL files, split the filegroup manually
119 // and then convert
120 if fg.ShouldConvertToAidlLibrary(ctx) {
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400121 tags := []string{"apex_available=//apex_available:anyapex"}
Vinh Tran444154d2022-08-16 13:10:31 -0400122 attrs := &bazelAidlLibraryAttributes{
123 Srcs: srcs,
124 Strip_import_prefix: fg.properties.Path,
125 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500126
Vinh Tran444154d2022-08-16 13:10:31 -0400127 props := bazel.BazelTargetModuleProperties{
128 Rule_class: "aidl_library",
129 Bzl_load_location: "//build/bazel/rules/aidl:library.bzl",
130 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500131
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400132 ctx.CreateBazelTargetModule(
133 props,
134 CommonAttributes{
135 Name: fg.Name(),
136 Tags: bazel.MakeStringListAttribute(tags),
137 },
138 attrs)
Vinh Tran444154d2022-08-16 13:10:31 -0400139 } else {
Yu Liu2aa806b2022-09-01 11:54:47 -0700140 if fg.ShouldConvertToProtoLibrary(ctx) {
141 attrs := &ProtoAttrs{
142 Srcs: srcs,
143 Strip_import_prefix: fg.properties.Path,
144 }
145
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400146 tags := []string{
147 "apex_available=//apex_available:anyapex",
148 // TODO(b/246997908): we can remove this tag if we could figure out a solution for this bug.
149 "manual",
150 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700151 ctx.CreateBazelTargetModule(
152 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
Sam Delmericoe9b33f72022-11-21 15:38:54 -0500153 CommonAttributes{
154 Name: fg.Name() + convertedProtoLibrarySuffix,
155 Tags: bazel.MakeStringListAttribute(tags),
156 },
Yu Liu2aa806b2022-09-01 11:54:47 -0700157 attrs)
158 }
159
160 // TODO(b/242847534): Still convert to a filegroup because other unconverted
161 // modules may depend on the filegroup
Vinh Tran444154d2022-08-16 13:10:31 -0400162 attrs := &bazelFilegroupAttributes{
163 Srcs: srcs,
164 }
165
166 props := bazel.BazelTargetModuleProperties{
167 Rule_class: "filegroup",
168 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
169 }
170
171 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
172 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800173}
174
175type fileGroupProperties struct {
176 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -0800177 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -0800178
Colin Cross27b922f2019-03-04 22:35:41 -0800179 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800180
181 // The base path to the files. May be used by other modules to determine which portion
182 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
183 // the base path is stripped off the path and the remaining path is used as the
184 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -0800185 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -0700186
187 // Create a make variable with the specified name that contains the list of files in the
188 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -0800189 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -0800190}
191
192type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700193 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500194 BazelModuleBase
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000195 DefaultableModuleBase
Yu Liu2aa806b2022-09-01 11:54:47 -0700196 FileGroupAsLibrary
Colin Cross068e0fe2016-12-13 15:23:47 -0800197 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700198 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -0800199}
200
Chris Parsonsf874e462022-05-10 13:50:12 -0400201var _ MixedBuildBuildable = (*fileGroup)(nil)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700202var _ SourceFileProducer = (*fileGroup)(nil)
Yu Liu2aa806b2022-09-01 11:54:47 -0700203var _ FileGroupAsLibrary = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -0800204
Patrice Arruda8958a942019-03-12 10:06:00 -0700205// filegroup contains a list of files that are referenced by other modules
206// properties (such as "srcs") using the syntax ":<name>". filegroup are
207// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700208func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -0800209 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -0700210 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700211 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500212 InitBazelModule(module)
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000213 InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700214 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800215}
216
Liz Kammer5edc1412022-05-25 11:12:44 -0400217var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
218
219func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
220 ins := make([]string, 0, len(fg.srcs))
221 outs := make([]string, 0, len(fg.srcs))
222 for _, p := range fg.srcs {
223 ins = append(ins, p.String())
224 outs = append(outs, p.Rel())
225 }
226 return []blueprint.JSONAction{
227 blueprint.JSONAction{
228 Inputs: ins,
229 Outputs: outs,
230 },
231 }
232}
233
Liz Kammer5bde22f2021-04-19 14:04:14 -0400234func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400235 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800236 if fg.properties.Path != nil {
237 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
238 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800239}
240
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700241func (fg *fileGroup) Srcs() Paths {
242 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800243}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700244
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700245func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
246 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
247 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700248 }
249}
Chris Parsonsf874e462022-05-10 13:50:12 -0400250
251func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) {
252 bazelCtx := ctx.Config().BazelContext
253
254 bazelCtx.QueueBazelRequest(
255 fg.GetBazelLabel(ctx, fg),
256 cquery.GetOutputFiles,
Yu Liue4312402023-01-18 09:15:31 -0800257 configKey{arch: Common.String(), osType: CommonOS})
Chris Parsonsf874e462022-05-10 13:50:12 -0400258}
259
260func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool {
Liz Kammer748209c2022-10-24 10:43:27 -0400261 // TODO(b/247782695), TODO(b/242847534) Fix mixed builds for filegroups
262 return false
Chris Parsonsf874e462022-05-10 13:50:12 -0400263}
264
265func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400266 bazelCtx := ctx.Config().BazelContext
267 // This is a short-term solution because we rely on info from Android.bp to handle
268 // a converted module. This will block when we want to remove Android.bp for all
269 // converted modules at some point.
270 // TODO(b/242847534): Implement a long-term solution in which we don't need to rely
271 // on info form Android.bp for modules that are already converted to Bazel
272 relativeRoot := ctx.ModuleDir()
Chris Parsonsf874e462022-05-10 13:50:12 -0400273 if fg.properties.Path != nil {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400274 relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path)
Chris Parsonsf874e462022-05-10 13:50:12 -0400275 }
276
Yu Liue4312402023-01-18 09:15:31 -0800277 filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{arch: Common.String(), osType: CommonOS})
Chris Parsonsf874e462022-05-10 13:50:12 -0400278 if err != nil {
279 ctx.ModuleErrorf(err.Error())
280 return
281 }
282
283 bazelOuts := make(Paths, 0, len(filePaths))
284 for _, p := range filePaths {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400285 bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p))
Chris Parsonsf874e462022-05-10 13:50:12 -0400286 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400287 fg.srcs = bazelOuts
288}
Vinh Tran444154d2022-08-16 13:10:31 -0400289
290func (fg *fileGroup) ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700291 return fg.shouldConvertToLibrary(ctx, ".aidl")
292}
293
294func (fg *fileGroup) ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool {
295 return fg.shouldConvertToLibrary(ctx, ".proto")
296}
297
298func (fg *fileGroup) shouldConvertToLibrary(ctx BazelConversionPathContext, suffix string) bool {
Vinh Tran444154d2022-08-16 13:10:31 -0400299 if len(fg.properties.Srcs) == 0 || !fg.ShouldConvertWithBp2build(ctx) {
300 return false
301 }
302 for _, src := range fg.properties.Srcs {
Yu Liu2aa806b2022-09-01 11:54:47 -0700303 if !strings.HasSuffix(src, suffix) {
Vinh Tran444154d2022-08-16 13:10:31 -0400304 return false
305 }
306 }
307 return true
308}
309
310func (fg *fileGroup) GetAidlLibraryLabel(ctx BazelConversionPathContext) string {
Yu Liu2aa806b2022-09-01 11:54:47 -0700311 return fg.getFileGroupAsLibraryLabel(ctx)
312}
313
314func (fg *fileGroup) GetProtoLibraryLabel(ctx BazelConversionPathContext) string {
315 return fg.getFileGroupAsLibraryLabel(ctx) + convertedProtoLibrarySuffix
316}
317
318func (fg *fileGroup) getFileGroupAsLibraryLabel(ctx BazelConversionPathContext) string {
Vinh Tran444154d2022-08-16 13:10:31 -0400319 if ctx.OtherModuleDir(fg.module) == ctx.ModuleDir() {
320 return ":" + fg.Name()
321 } else {
322 return fg.GetBazelLabel(ctx, fg)
323 }
324}
Sam Delmerico97bd1272022-08-25 14:45:31 -0400325
326// Given a name in srcs prop, check to see if the name references a filegroup
327// and the filegroup is converted to aidl_library
328func IsConvertedToAidlLibrary(ctx BazelConversionPathContext, name string) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700329 if fg, ok := ToFileGroupAsLibrary(ctx, name); ok {
330 return fg.ShouldConvertToAidlLibrary(ctx)
331 }
332 return false
333}
334
335func ToFileGroupAsLibrary(ctx BazelConversionPathContext, name string) (FileGroupAsLibrary, bool) {
Sam Delmerico97bd1272022-08-25 14:45:31 -0400336 if module, ok := ctx.ModuleFromName(name); ok {
337 if IsFilegroup(ctx, module) {
Yu Liu2aa806b2022-09-01 11:54:47 -0700338 if fg, ok := module.(FileGroupAsLibrary); ok {
339 return fg, true
Sam Delmerico97bd1272022-08-25 14:45:31 -0400340 }
341 }
342 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700343 return nil, false
Sam Delmerico97bd1272022-08-25 14:45:31 -0400344}
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000345
346// Defaults
347type FileGroupDefaults struct {
348 ModuleBase
349 DefaultsModuleBase
350}
351
352func FileGroupDefaultsFactory() Module {
353 module := &FileGroupDefaults{}
354 module.AddProperties(&fileGroupProperties{})
355 InitDefaultsModule(module)
356
357 return module
358}