blob: 66aa15446acbb7df48f58ee6aac4e477f48f5c97 [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 Lidff65b02023-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
85}
86
Spandan Dasbd52ea92023-03-09 23:03:07 +000087// api srcs can be contained in filegroups.
88// this should be generated in api_bp2build workspace as well.
89func (fg *fileGroup) ConvertWithApiBp2build(ctx TopDownMutatorContext) {
90 fg.ConvertWithBp2build(ctx)
91}
92
Liz Kammerbe46fcc2021-11-01 15:32:43 -040093// ConvertWithBp2build performs bp2build conversion of filegroup
94func (fg *fileGroup) ConvertWithBp2build(ctx TopDownMutatorContext) {
Jingwen Chen07027912021-03-15 06:02:43 -040095 srcs := bazel.MakeLabelListAttribute(
96 BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs))
Jingwen Chen5146ac02021-09-02 11:44:42 +000097
98 // For Bazel compatibility, don't generate the filegroup if there is only 1
99 // source file, and that the source file is named the same as the module
100 // itself. In Bazel, eponymous filegroups like this would be an error.
101 //
102 // Instead, dependents on this single-file filegroup can just depend
103 // on the file target, instead of rule target, directly.
104 //
105 // You may ask: what if a filegroup has multiple files, and one of them
106 // shares the name? The answer: we haven't seen that in the wild, and
107 // should lock Soong itself down to prevent the behavior. For now,
108 // we raise an error if bp2build sees this problem.
109 for _, f := range srcs.Value.Includes {
110 if f.Label == fg.Name() {
111 if len(srcs.Value.Includes) > 1 {
112 ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name())
113 }
114 return
115 }
116 }
117
Vinh Tran444154d2022-08-16 13:10:31 -0400118 // Convert module that has only AIDL files to aidl_library
119 // If the module has a mixed bag of AIDL and non-AIDL files, split the filegroup manually
120 // and then convert
121 if fg.ShouldConvertToAidlLibrary(ctx) {
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400122 tags := []string{"apex_available=//apex_available:anyapex"}
Vinh Tran444154d2022-08-16 13:10:31 -0400123 attrs := &bazelAidlLibraryAttributes{
124 Srcs: srcs,
125 Strip_import_prefix: fg.properties.Path,
126 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500127
Vinh Tran444154d2022-08-16 13:10:31 -0400128 props := bazel.BazelTargetModuleProperties{
129 Rule_class: "aidl_library",
Sam Delmericoe55bf082023-03-31 09:47:28 -0400130 Bzl_load_location: "//build/bazel/rules/aidl:aidl_library.bzl",
Vinh Tran444154d2022-08-16 13:10:31 -0400131 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500132
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400133 ctx.CreateBazelTargetModule(
134 props,
135 CommonAttributes{
136 Name: fg.Name(),
137 Tags: bazel.MakeStringListAttribute(tags),
138 },
139 attrs)
Vinh Tran444154d2022-08-16 13:10:31 -0400140 } else {
Yu Liu2aa806b2022-09-01 11:54:47 -0700141 if fg.ShouldConvertToProtoLibrary(ctx) {
142 attrs := &ProtoAttrs{
143 Srcs: srcs,
144 Strip_import_prefix: fg.properties.Path,
145 }
146
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400147 tags := []string{
148 "apex_available=//apex_available:anyapex",
149 // TODO(b/246997908): we can remove this tag if we could figure out a solution for this bug.
150 "manual",
151 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700152 ctx.CreateBazelTargetModule(
153 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
Sam Delmericoe9b33f72022-11-21 15:38:54 -0500154 CommonAttributes{
155 Name: fg.Name() + convertedProtoLibrarySuffix,
156 Tags: bazel.MakeStringListAttribute(tags),
157 },
Yu Liu2aa806b2022-09-01 11:54:47 -0700158 attrs)
159 }
160
161 // TODO(b/242847534): Still convert to a filegroup because other unconverted
162 // modules may depend on the filegroup
Vinh Tran444154d2022-08-16 13:10:31 -0400163 attrs := &bazelFilegroupAttributes{
164 Srcs: srcs,
165 }
166
167 props := bazel.BazelTargetModuleProperties{
168 Rule_class: "filegroup",
169 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
170 }
171
172 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
173 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800174}
175
176type fileGroupProperties struct {
177 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -0800178 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -0800179
Colin Cross27b922f2019-03-04 22:35:41 -0800180 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800181
182 // The base path to the files. May be used by other modules to determine which portion
183 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
184 // the base path is stripped off the path and the remaining path is used as the
185 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -0800186 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -0700187
188 // Create a make variable with the specified name that contains the list of files in the
189 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -0800190 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -0800191}
192
193type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700194 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500195 BazelModuleBase
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000196 DefaultableModuleBase
Yu Liu2aa806b2022-09-01 11:54:47 -0700197 FileGroupAsLibrary
Colin Cross068e0fe2016-12-13 15:23:47 -0800198 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700199 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -0800200}
201
Chris Parsonsf874e462022-05-10 13:50:12 -0400202var _ MixedBuildBuildable = (*fileGroup)(nil)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700203var _ SourceFileProducer = (*fileGroup)(nil)
Yu Liu2aa806b2022-09-01 11:54:47 -0700204var _ FileGroupAsLibrary = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -0800205
Patrice Arruda8958a942019-03-12 10:06:00 -0700206// filegroup contains a list of files that are referenced by other modules
207// properties (such as "srcs") using the syntax ":<name>". filegroup are
208// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700209func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -0800210 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -0700211 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700212 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500213 InitBazelModule(module)
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000214 InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700215 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800216}
217
Liz Kammer5edc1412022-05-25 11:12:44 -0400218var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
219
220func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
221 ins := make([]string, 0, len(fg.srcs))
222 outs := make([]string, 0, len(fg.srcs))
223 for _, p := range fg.srcs {
224 ins = append(ins, p.String())
225 outs = append(outs, p.Rel())
226 }
227 return []blueprint.JSONAction{
228 blueprint.JSONAction{
229 Inputs: ins,
230 Outputs: outs,
231 },
232 }
233}
234
Liz Kammer5bde22f2021-04-19 14:04:14 -0400235func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400236 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800237 if fg.properties.Path != nil {
238 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
239 }
Aditya Choudhary95e2a3c2023-11-29 16:42:42 +0000240 ctx.SetProvider(blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: fg.srcs.Strings()})
Colin Cross068e0fe2016-12-13 15:23:47 -0800241}
242
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700243func (fg *fileGroup) Srcs() Paths {
244 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800245}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700246
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700247func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
248 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
249 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700250 }
251}
Chris Parsonsf874e462022-05-10 13:50:12 -0400252
253func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) {
254 bazelCtx := ctx.Config().BazelContext
255
256 bazelCtx.QueueBazelRequest(
257 fg.GetBazelLabel(ctx, fg),
258 cquery.GetOutputFiles,
Yu Liue4312402023-01-18 09:15:31 -0800259 configKey{arch: Common.String(), osType: CommonOS})
Chris Parsonsf874e462022-05-10 13:50:12 -0400260}
261
262func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool {
Liz Kammer748209c2022-10-24 10:43:27 -0400263 // TODO(b/247782695), TODO(b/242847534) Fix mixed builds for filegroups
264 return false
Chris Parsonsf874e462022-05-10 13:50:12 -0400265}
266
267func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400268 bazelCtx := ctx.Config().BazelContext
269 // This is a short-term solution because we rely on info from Android.bp to handle
270 // a converted module. This will block when we want to remove Android.bp for all
271 // converted modules at some point.
272 // TODO(b/242847534): Implement a long-term solution in which we don't need to rely
273 // on info form Android.bp for modules that are already converted to Bazel
274 relativeRoot := ctx.ModuleDir()
Chris Parsonsf874e462022-05-10 13:50:12 -0400275 if fg.properties.Path != nil {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400276 relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path)
Chris Parsonsf874e462022-05-10 13:50:12 -0400277 }
278
Yu Liue4312402023-01-18 09:15:31 -0800279 filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{arch: Common.String(), osType: CommonOS})
Chris Parsonsf874e462022-05-10 13:50:12 -0400280 if err != nil {
281 ctx.ModuleErrorf(err.Error())
282 return
283 }
284
285 bazelOuts := make(Paths, 0, len(filePaths))
286 for _, p := range filePaths {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400287 bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p))
Chris Parsonsf874e462022-05-10 13:50:12 -0400288 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400289 fg.srcs = bazelOuts
290}
Vinh Tran444154d2022-08-16 13:10:31 -0400291
292func (fg *fileGroup) ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700293 return fg.shouldConvertToLibrary(ctx, ".aidl")
294}
295
296func (fg *fileGroup) ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool {
297 return fg.shouldConvertToLibrary(ctx, ".proto")
298}
299
300func (fg *fileGroup) shouldConvertToLibrary(ctx BazelConversionPathContext, suffix string) bool {
Vinh Tran444154d2022-08-16 13:10:31 -0400301 if len(fg.properties.Srcs) == 0 || !fg.ShouldConvertWithBp2build(ctx) {
302 return false
303 }
304 for _, src := range fg.properties.Srcs {
Yu Liu2aa806b2022-09-01 11:54:47 -0700305 if !strings.HasSuffix(src, suffix) {
Vinh Tran444154d2022-08-16 13:10:31 -0400306 return false
307 }
308 }
309 return true
310}
311
312func (fg *fileGroup) GetAidlLibraryLabel(ctx BazelConversionPathContext) string {
Yu Liu2aa806b2022-09-01 11:54:47 -0700313 return fg.getFileGroupAsLibraryLabel(ctx)
314}
315
316func (fg *fileGroup) GetProtoLibraryLabel(ctx BazelConversionPathContext) string {
317 return fg.getFileGroupAsLibraryLabel(ctx) + convertedProtoLibrarySuffix
318}
319
320func (fg *fileGroup) getFileGroupAsLibraryLabel(ctx BazelConversionPathContext) string {
Vinh Tran444154d2022-08-16 13:10:31 -0400321 if ctx.OtherModuleDir(fg.module) == ctx.ModuleDir() {
322 return ":" + fg.Name()
323 } else {
324 return fg.GetBazelLabel(ctx, fg)
325 }
326}
Sam Delmerico97bd1272022-08-25 14:45:31 -0400327
328// Given a name in srcs prop, check to see if the name references a filegroup
329// and the filegroup is converted to aidl_library
330func IsConvertedToAidlLibrary(ctx BazelConversionPathContext, name string) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700331 if fg, ok := ToFileGroupAsLibrary(ctx, name); ok {
332 return fg.ShouldConvertToAidlLibrary(ctx)
333 }
334 return false
335}
336
337func ToFileGroupAsLibrary(ctx BazelConversionPathContext, name string) (FileGroupAsLibrary, bool) {
Sam Delmerico97bd1272022-08-25 14:45:31 -0400338 if module, ok := ctx.ModuleFromName(name); ok {
339 if IsFilegroup(ctx, module) {
Yu Liu2aa806b2022-09-01 11:54:47 -0700340 if fg, ok := module.(FileGroupAsLibrary); ok {
341 return fg, true
Sam Delmerico97bd1272022-08-25 14:45:31 -0400342 }
343 }
344 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700345 return nil, false
Sam Delmerico97bd1272022-08-25 14:45:31 -0400346}
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000347
348// Defaults
349type FileGroupDefaults struct {
350 ModuleBase
351 DefaultsModuleBase
352}
353
354func FileGroupDefaultsFactory() Module {
355 module := &FileGroupDefaults{}
356 module.AddProperties(&fileGroupProperties{})
357 InitDefaultsModule(module)
358
359 return module
360}