blob: c042ff18eaa9e0a2dc0f2d196b2d00a3f54a1c05 [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 {
Wei Li2c9e8d62023-05-05 01:07:15 -070078 Srcs bazel.LabelListAttribute
79 Applicable_licenses bazel.LabelListAttribute
Jingwen Chen32b4ece2021-01-21 03:20:18 -050080}
81
Vinh Tran444154d2022-08-16 13:10:31 -040082type bazelAidlLibraryAttributes struct {
83 Srcs bazel.LabelListAttribute
84 Strip_import_prefix *string
Vinh Tran25625852023-04-14 18:45:20 -040085 Deps bazel.LabelListAttribute
Vinh Tran444154d2022-08-16 13:10:31 -040086}
87
Spandan Dasbd52ea92023-03-09 23:03:07 +000088// api srcs can be contained in filegroups.
89// this should be generated in api_bp2build workspace as well.
90func (fg *fileGroup) ConvertWithApiBp2build(ctx TopDownMutatorContext) {
91 fg.ConvertWithBp2build(ctx)
92}
93
Liz Kammerbe46fcc2021-11-01 15:32:43 -040094// ConvertWithBp2build performs bp2build conversion of filegroup
95func (fg *fileGroup) ConvertWithBp2build(ctx TopDownMutatorContext) {
Jingwen Chen07027912021-03-15 06:02:43 -040096 srcs := bazel.MakeLabelListAttribute(
97 BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs))
Jingwen Chen5146ac02021-09-02 11:44:42 +000098
99 // For Bazel compatibility, don't generate the filegroup if there is only 1
100 // source file, and that the source file is named the same as the module
101 // itself. In Bazel, eponymous filegroups like this would be an error.
102 //
103 // Instead, dependents on this single-file filegroup can just depend
104 // on the file target, instead of rule target, directly.
105 //
106 // You may ask: what if a filegroup has multiple files, and one of them
107 // shares the name? The answer: we haven't seen that in the wild, and
108 // should lock Soong itself down to prevent the behavior. For now,
109 // we raise an error if bp2build sees this problem.
110 for _, f := range srcs.Value.Includes {
111 if f.Label == fg.Name() {
112 if len(srcs.Value.Includes) > 1 {
113 ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name())
114 }
115 return
116 }
117 }
118
Vinh Tran444154d2022-08-16 13:10:31 -0400119 // Convert module that has only AIDL files to aidl_library
120 // If the module has a mixed bag of AIDL and non-AIDL files, split the filegroup manually
121 // and then convert
122 if fg.ShouldConvertToAidlLibrary(ctx) {
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400123 tags := []string{"apex_available=//apex_available:anyapex"}
Vinh Tran25625852023-04-14 18:45:20 -0400124 deps := bazel.MakeLabelListAttribute(BazelLabelForModuleDeps(ctx, fg.properties.Aidl.Deps))
125
Vinh Tran444154d2022-08-16 13:10:31 -0400126 attrs := &bazelAidlLibraryAttributes{
127 Srcs: srcs,
128 Strip_import_prefix: fg.properties.Path,
Vinh Tran25625852023-04-14 18:45:20 -0400129 Deps: deps,
Vinh Tran444154d2022-08-16 13:10:31 -0400130 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500131
Vinh Tran444154d2022-08-16 13:10:31 -0400132 props := bazel.BazelTargetModuleProperties{
133 Rule_class: "aidl_library",
Sam Delmericoe55bf082023-03-31 09:47:28 -0400134 Bzl_load_location: "//build/bazel/rules/aidl:aidl_library.bzl",
Vinh Tran444154d2022-08-16 13:10:31 -0400135 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500136
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400137 ctx.CreateBazelTargetModule(
138 props,
139 CommonAttributes{
140 Name: fg.Name(),
141 Tags: bazel.MakeStringListAttribute(tags),
142 },
143 attrs)
Vinh Tran444154d2022-08-16 13:10:31 -0400144 } else {
Yu Liu2aa806b2022-09-01 11:54:47 -0700145 if fg.ShouldConvertToProtoLibrary(ctx) {
146 attrs := &ProtoAttrs{
147 Srcs: srcs,
148 Strip_import_prefix: fg.properties.Path,
149 }
150
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400151 tags := []string{
152 "apex_available=//apex_available:anyapex",
153 // TODO(b/246997908): we can remove this tag if we could figure out a solution for this bug.
154 "manual",
155 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700156 ctx.CreateBazelTargetModule(
157 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
Sam Delmericoe9b33f72022-11-21 15:38:54 -0500158 CommonAttributes{
159 Name: fg.Name() + convertedProtoLibrarySuffix,
160 Tags: bazel.MakeStringListAttribute(tags),
161 },
Yu Liu2aa806b2022-09-01 11:54:47 -0700162 attrs)
163 }
164
165 // TODO(b/242847534): Still convert to a filegroup because other unconverted
166 // modules may depend on the filegroup
Vinh Tran444154d2022-08-16 13:10:31 -0400167 attrs := &bazelFilegroupAttributes{
168 Srcs: srcs,
169 }
170
171 props := bazel.BazelTargetModuleProperties{
172 Rule_class: "filegroup",
173 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
174 }
175
176 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
177 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800178}
179
Alixb29a3cd2023-05-09 20:37:49 +0000180type FileGroupPath interface {
181 GetPath(ctx TopDownMutatorContext) string
182}
183
184func (fg *fileGroup) GetPath(ctx TopDownMutatorContext) string {
185 if fg.properties.Path != nil {
186 return *fg.properties.Path
187 }
188 return ""
189}
190
Colin Cross068e0fe2016-12-13 15:23:47 -0800191type fileGroupProperties struct {
192 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -0800193 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -0800194
Colin Cross27b922f2019-03-04 22:35:41 -0800195 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800196
197 // The base path to the files. May be used by other modules to determine which portion
198 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
199 // the base path is stripped off the path and the remaining path is used as the
200 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -0800201 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -0700202
203 // Create a make variable with the specified name that contains the list of files in the
204 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -0800205 Export_to_make_var *string
Vinh Tran25625852023-04-14 18:45:20 -0400206
207 // aidl is explicitly provided for implicit aidl dependencies
208 // TODO(b/278298615): aidl prop is a no-op in Soong and is an escape hatch
209 // to include implicit aidl dependencies for bazel migration compatibility
210 Aidl struct {
211 // List of aidl files or filegroup depended on by srcs
212 Deps []string `android:"path"`
213 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800214}
215
216type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700217 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500218 BazelModuleBase
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000219 DefaultableModuleBase
Yu Liu2aa806b2022-09-01 11:54:47 -0700220 FileGroupAsLibrary
Alixb29a3cd2023-05-09 20:37:49 +0000221 FileGroupPath
Colin Cross068e0fe2016-12-13 15:23:47 -0800222 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700223 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -0800224}
225
Chris Parsonsf874e462022-05-10 13:50:12 -0400226var _ MixedBuildBuildable = (*fileGroup)(nil)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700227var _ SourceFileProducer = (*fileGroup)(nil)
Yu Liu2aa806b2022-09-01 11:54:47 -0700228var _ FileGroupAsLibrary = (*fileGroup)(nil)
Alixb29a3cd2023-05-09 20:37:49 +0000229var _ FileGroupPath = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -0800230
Patrice Arruda8958a942019-03-12 10:06:00 -0700231// filegroup contains a list of files that are referenced by other modules
232// properties (such as "srcs") using the syntax ":<name>". filegroup are
233// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700234func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -0800235 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -0700236 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700237 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500238 InitBazelModule(module)
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000239 InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700240 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800241}
242
Liz Kammer5edc1412022-05-25 11:12:44 -0400243var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
244
245func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
246 ins := make([]string, 0, len(fg.srcs))
247 outs := make([]string, 0, len(fg.srcs))
248 for _, p := range fg.srcs {
249 ins = append(ins, p.String())
250 outs = append(outs, p.Rel())
251 }
252 return []blueprint.JSONAction{
253 blueprint.JSONAction{
254 Inputs: ins,
255 Outputs: outs,
256 },
257 }
258}
259
Liz Kammer5bde22f2021-04-19 14:04:14 -0400260func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400261 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800262 if fg.properties.Path != nil {
263 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
264 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800265}
266
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700267func (fg *fileGroup) Srcs() Paths {
268 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800269}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700270
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700271func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
272 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
273 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700274 }
275}
Chris Parsonsf874e462022-05-10 13:50:12 -0400276
277func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) {
278 bazelCtx := ctx.Config().BazelContext
279
280 bazelCtx.QueueBazelRequest(
281 fg.GetBazelLabel(ctx, fg),
282 cquery.GetOutputFiles,
Yu Liue4312402023-01-18 09:15:31 -0800283 configKey{arch: Common.String(), osType: CommonOS})
Chris Parsonsf874e462022-05-10 13:50:12 -0400284}
285
286func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool {
Liz Kammer748209c2022-10-24 10:43:27 -0400287 // TODO(b/247782695), TODO(b/242847534) Fix mixed builds for filegroups
288 return false
Chris Parsonsf874e462022-05-10 13:50:12 -0400289}
290
291func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400292 bazelCtx := ctx.Config().BazelContext
293 // This is a short-term solution because we rely on info from Android.bp to handle
294 // a converted module. This will block when we want to remove Android.bp for all
295 // converted modules at some point.
296 // TODO(b/242847534): Implement a long-term solution in which we don't need to rely
297 // on info form Android.bp for modules that are already converted to Bazel
298 relativeRoot := ctx.ModuleDir()
Chris Parsonsf874e462022-05-10 13:50:12 -0400299 if fg.properties.Path != nil {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400300 relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path)
Chris Parsonsf874e462022-05-10 13:50:12 -0400301 }
302
Yu Liue4312402023-01-18 09:15:31 -0800303 filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{arch: Common.String(), osType: CommonOS})
Chris Parsonsf874e462022-05-10 13:50:12 -0400304 if err != nil {
305 ctx.ModuleErrorf(err.Error())
306 return
307 }
308
309 bazelOuts := make(Paths, 0, len(filePaths))
310 for _, p := range filePaths {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400311 bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p))
Chris Parsonsf874e462022-05-10 13:50:12 -0400312 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400313 fg.srcs = bazelOuts
314}
Vinh Tran444154d2022-08-16 13:10:31 -0400315
316func (fg *fileGroup) ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700317 return fg.shouldConvertToLibrary(ctx, ".aidl")
318}
319
320func (fg *fileGroup) ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool {
321 return fg.shouldConvertToLibrary(ctx, ".proto")
322}
323
324func (fg *fileGroup) shouldConvertToLibrary(ctx BazelConversionPathContext, suffix string) bool {
Vinh Tran444154d2022-08-16 13:10:31 -0400325 if len(fg.properties.Srcs) == 0 || !fg.ShouldConvertWithBp2build(ctx) {
326 return false
327 }
328 for _, src := range fg.properties.Srcs {
Yu Liu2aa806b2022-09-01 11:54:47 -0700329 if !strings.HasSuffix(src, suffix) {
Vinh Tran444154d2022-08-16 13:10:31 -0400330 return false
331 }
332 }
333 return true
334}
335
336func (fg *fileGroup) GetAidlLibraryLabel(ctx BazelConversionPathContext) string {
Yu Liu2aa806b2022-09-01 11:54:47 -0700337 return fg.getFileGroupAsLibraryLabel(ctx)
338}
339
340func (fg *fileGroup) GetProtoLibraryLabel(ctx BazelConversionPathContext) string {
341 return fg.getFileGroupAsLibraryLabel(ctx) + convertedProtoLibrarySuffix
342}
343
344func (fg *fileGroup) getFileGroupAsLibraryLabel(ctx BazelConversionPathContext) string {
Vinh Tran444154d2022-08-16 13:10:31 -0400345 if ctx.OtherModuleDir(fg.module) == ctx.ModuleDir() {
346 return ":" + fg.Name()
347 } else {
348 return fg.GetBazelLabel(ctx, fg)
349 }
350}
Sam Delmerico97bd1272022-08-25 14:45:31 -0400351
352// Given a name in srcs prop, check to see if the name references a filegroup
353// and the filegroup is converted to aidl_library
354func IsConvertedToAidlLibrary(ctx BazelConversionPathContext, name string) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700355 if fg, ok := ToFileGroupAsLibrary(ctx, name); ok {
356 return fg.ShouldConvertToAidlLibrary(ctx)
357 }
358 return false
359}
360
361func ToFileGroupAsLibrary(ctx BazelConversionPathContext, name string) (FileGroupAsLibrary, bool) {
Sam Delmerico97bd1272022-08-25 14:45:31 -0400362 if module, ok := ctx.ModuleFromName(name); ok {
363 if IsFilegroup(ctx, module) {
Yu Liu2aa806b2022-09-01 11:54:47 -0700364 if fg, ok := module.(FileGroupAsLibrary); ok {
365 return fg, true
Sam Delmerico97bd1272022-08-25 14:45:31 -0400366 }
367 }
368 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700369 return nil, false
Sam Delmerico97bd1272022-08-25 14:45:31 -0400370}
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000371
372// Defaults
373type FileGroupDefaults struct {
374 ModuleBase
375 DefaultsModuleBase
376}
377
378func FileGroupDefaultsFactory() Module {
379 module := &FileGroupDefaults{}
380 module.AddProperties(&fileGroupProperties{})
381 InitDefaultsModule(module)
382
383 return module
384}