blob: 5a8c4b9bcdcd8aabbc59cf73bda4a8466f1461eb [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"
Chris Parsons39a16972023-06-08 14:28:51 +000024 "android/soong/ui/metrics/bp2build_metrics_proto"
Sam Delmericoc7681022022-02-04 21:01:20 +000025
26 "github.com/google/blueprint"
Spandan Dasdf3ec822023-08-04 02:19:53 +000027 "github.com/google/blueprint/proptools"
Colin Cross068e0fe2016-12-13 15:23:47 -080028)
29
30func init() {
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000031 RegisterFilegroupBuildComponents(InitRegistrationContext)
Jingwen Chen32b4ece2021-01-21 03:20:18 -050032}
33
Paul Duffin35816122021-02-24 01:49:52 +000034var PrepareForTestWithFilegroup = FixtureRegisterWithContext(func(ctx RegistrationContext) {
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000035 RegisterFilegroupBuildComponents(ctx)
Paul Duffin35816122021-02-24 01:49:52 +000036})
37
Anton Hansson7d6dd8b2023-03-06 11:26:17 +000038func RegisterFilegroupBuildComponents(ctx RegistrationContext) {
39 ctx.RegisterModuleType("filegroup", FileGroupFactory)
40 ctx.RegisterModuleType("filegroup_defaults", FileGroupDefaultsFactory)
41}
42
Yu Liu2aa806b2022-09-01 11:54:47 -070043var convertedProtoLibrarySuffix = "_bp2build_converted"
44
Sam Delmericoc7681022022-02-04 21:01:20 +000045// IsFilegroup checks that a module is a filegroup type
46func IsFilegroup(ctx bazel.OtherModuleContext, m blueprint.Module) bool {
47 return ctx.OtherModuleType(m) == "filegroup"
48}
49
Sam Delmerico97bd1272022-08-25 14:45:31 -040050var (
51 // ignoring case, checks for proto or protos as an independent word in the name, whether at the
52 // beginning, end, or middle. e.g. "proto.foo", "bar-protos", "baz_proto_srcs" would all match
53 filegroupLikelyProtoPattern = regexp.MustCompile("(?i)(^|[^a-z])proto(s)?([^a-z]|$)")
54 filegroupLikelyAidlPattern = regexp.MustCompile("(?i)(^|[^a-z])aidl([^a-z]|$)")
55
56 ProtoSrcLabelPartition = bazel.LabelPartition{
57 Extensions: []string{".proto"},
58 LabelMapper: isFilegroupWithPattern(filegroupLikelyProtoPattern),
59 }
60 AidlSrcLabelPartition = bazel.LabelPartition{
61 Extensions: []string{".aidl"},
62 LabelMapper: isFilegroupWithPattern(filegroupLikelyAidlPattern),
63 }
64)
65
66func isFilegroupWithPattern(pattern *regexp.Regexp) bazel.LabelMapper {
67 return func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) {
68 m, exists := ctx.ModuleFromName(label.OriginalModuleName)
69 labelStr := label.Label
70 if !exists || !IsFilegroup(ctx, m) {
71 return labelStr, false
72 }
73 likelyMatched := pattern.MatchString(label.OriginalModuleName)
74 return labelStr, likelyMatched
75 }
76}
77
Jingwen Chen32b4ece2021-01-21 03:20:18 -050078// https://docs.bazel.build/versions/master/be/general.html#filegroup
79type bazelFilegroupAttributes struct {
Wei Li2c9e8d62023-05-05 01:07:15 -070080 Srcs bazel.LabelListAttribute
81 Applicable_licenses bazel.LabelListAttribute
Jingwen Chen32b4ece2021-01-21 03:20:18 -050082}
83
Vinh Tran444154d2022-08-16 13:10:31 -040084type bazelAidlLibraryAttributes struct {
85 Srcs bazel.LabelListAttribute
86 Strip_import_prefix *string
87}
88
Liz Kammerbe46fcc2021-11-01 15:32:43 -040089// ConvertWithBp2build performs bp2build conversion of filegroup
Chris Parsons637458d2023-09-19 20:09:00 +000090func (fg *fileGroup) ConvertWithBp2build(ctx Bp2buildMutatorContext) {
Jingwen Chen07027912021-03-15 06:02:43 -040091 srcs := bazel.MakeLabelListAttribute(
92 BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs))
Jingwen Chen5146ac02021-09-02 11:44:42 +000093
94 // For Bazel compatibility, don't generate the filegroup if there is only 1
95 // source file, and that the source file is named the same as the module
96 // itself. In Bazel, eponymous filegroups like this would be an error.
97 //
98 // Instead, dependents on this single-file filegroup can just depend
99 // on the file target, instead of rule target, directly.
100 //
101 // You may ask: what if a filegroup has multiple files, and one of them
102 // shares the name? The answer: we haven't seen that in the wild, and
103 // should lock Soong itself down to prevent the behavior. For now,
104 // we raise an error if bp2build sees this problem.
105 for _, f := range srcs.Value.Includes {
106 if f.Label == fg.Name() {
107 if len(srcs.Value.Includes) > 1 {
108 ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name())
109 }
Chris Parsons39a16972023-06-08 14:28:51 +0000110 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_SRC_NAME_COLLISION, "")
Jingwen Chen5146ac02021-09-02 11:44:42 +0000111 return
112 }
113 }
114
Vinh Tran444154d2022-08-16 13:10:31 -0400115 // Convert module that has only AIDL files to aidl_library
116 // If the module has a mixed bag of AIDL and non-AIDL files, split the filegroup manually
117 // and then convert
118 if fg.ShouldConvertToAidlLibrary(ctx) {
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400119 tags := []string{"apex_available=//apex_available:anyapex"}
Vinh Tran444154d2022-08-16 13:10:31 -0400120 attrs := &bazelAidlLibraryAttributes{
121 Srcs: srcs,
122 Strip_import_prefix: fg.properties.Path,
123 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500124
Vinh Tran444154d2022-08-16 13:10:31 -0400125 props := bazel.BazelTargetModuleProperties{
126 Rule_class: "aidl_library",
Sam Delmericoe55bf082023-03-31 09:47:28 -0400127 Bzl_load_location: "//build/bazel/rules/aidl:aidl_library.bzl",
Vinh Tran444154d2022-08-16 13:10:31 -0400128 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500129
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400130 ctx.CreateBazelTargetModule(
131 props,
132 CommonAttributes{
133 Name: fg.Name(),
134 Tags: bazel.MakeStringListAttribute(tags),
135 },
136 attrs)
Vinh Tran444154d2022-08-16 13:10:31 -0400137 } else {
Yu Liu2aa806b2022-09-01 11:54:47 -0700138 if fg.ShouldConvertToProtoLibrary(ctx) {
Spandan Dasdf3ec822023-08-04 02:19:53 +0000139 pkgToSrcs := partitionSrcsByPackage(ctx.ModuleDir(), bazel.MakeLabelList(srcs.Value.Includes))
140 if len(pkgToSrcs) > 1 {
141 ctx.ModuleErrorf("TODO: Add bp2build support for multiple package .protosrcs in filegroup")
142 return
143 }
144 pkg := SortedKeys(pkgToSrcs)[0]
Yu Liu2aa806b2022-09-01 11:54:47 -0700145 attrs := &ProtoAttrs{
Spandan Dasdf3ec822023-08-04 02:19:53 +0000146 Srcs: bazel.MakeLabelListAttribute(pkgToSrcs[pkg]),
Yu Liu2aa806b2022-09-01 11:54:47 -0700147 Strip_import_prefix: fg.properties.Path,
148 }
149
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400150 tags := []string{
151 "apex_available=//apex_available:anyapex",
152 // TODO(b/246997908): we can remove this tag if we could figure out a solution for this bug.
153 "manual",
154 }
Spandan Dasdf3ec822023-08-04 02:19:53 +0000155 if pkg != ctx.ModuleDir() {
156 // Since we are creating the proto_library in a subpackage, create an import_prefix relative to the current package
157 if rel, err := filepath.Rel(ctx.ModuleDir(), pkg); err != nil {
158 ctx.ModuleErrorf("Could not get relative path for %v %v", pkg, err)
159 } else if rel != "." {
160 attrs.Import_prefix = &rel
161 // Strip the package prefix
162 attrs.Strip_import_prefix = proptools.StringPtr("")
163 }
164 }
165
Yu Liu2aa806b2022-09-01 11:54:47 -0700166 ctx.CreateBazelTargetModule(
167 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
Sam Delmericoe9b33f72022-11-21 15:38:54 -0500168 CommonAttributes{
Spandan Dasdf3ec822023-08-04 02:19:53 +0000169 Name: fg.Name() + "_proto",
170 Dir: proptools.StringPtr(pkg),
Sam Delmericoe9b33f72022-11-21 15:38:54 -0500171 Tags: bazel.MakeStringListAttribute(tags),
172 },
Yu Liu2aa806b2022-09-01 11:54:47 -0700173 attrs)
Spandan Dasdf3ec822023-08-04 02:19:53 +0000174
175 // Create an alias in the current dir. The actual target might exist in a different package, but rdeps
176 // can reliabily use this alias
177 ctx.CreateBazelTargetModule(
178 bazel.BazelTargetModuleProperties{Rule_class: "alias"},
179 CommonAttributes{
180 Name: fg.Name() + convertedProtoLibrarySuffix,
181 // TODO(b/246997908): we can remove this tag if we could figure out a solution for this bug.
182 Tags: bazel.MakeStringListAttribute(tags),
183 },
184 &bazelAliasAttributes{
185 Actual: bazel.MakeLabelAttribute("//" + pkg + ":" + fg.Name() + "_proto"),
186 },
187 )
Yu Liu2aa806b2022-09-01 11:54:47 -0700188 }
189
190 // TODO(b/242847534): Still convert to a filegroup because other unconverted
191 // modules may depend on the filegroup
Vinh Tran444154d2022-08-16 13:10:31 -0400192 attrs := &bazelFilegroupAttributes{
193 Srcs: srcs,
194 }
195
196 props := bazel.BazelTargetModuleProperties{
197 Rule_class: "filegroup",
198 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
199 }
200
201 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
202 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800203}
204
Alixb29a3cd2023-05-09 20:37:49 +0000205type FileGroupPath interface {
Chris Parsons637458d2023-09-19 20:09:00 +0000206 GetPath(ctx Bp2buildMutatorContext) string
Alixb29a3cd2023-05-09 20:37:49 +0000207}
208
Chris Parsons637458d2023-09-19 20:09:00 +0000209func (fg *fileGroup) GetPath(ctx Bp2buildMutatorContext) string {
Alixb29a3cd2023-05-09 20:37:49 +0000210 if fg.properties.Path != nil {
211 return *fg.properties.Path
212 }
213 return ""
214}
215
Colin Cross068e0fe2016-12-13 15:23:47 -0800216type fileGroupProperties struct {
217 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -0800218 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -0800219
Colin Cross27b922f2019-03-04 22:35:41 -0800220 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800221
222 // The base path to the files. May be used by other modules to determine which portion
223 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
224 // the base path is stripped off the path and the remaining path is used as the
225 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -0800226 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -0700227
228 // Create a make variable with the specified name that contains the list of files in the
229 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -0800230 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -0800231}
232
233type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700234 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500235 BazelModuleBase
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000236 DefaultableModuleBase
Yu Liu2aa806b2022-09-01 11:54:47 -0700237 FileGroupAsLibrary
Alixb29a3cd2023-05-09 20:37:49 +0000238 FileGroupPath
Colin Cross068e0fe2016-12-13 15:23:47 -0800239 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700240 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -0800241}
242
Chris Parsonsf874e462022-05-10 13:50:12 -0400243var _ MixedBuildBuildable = (*fileGroup)(nil)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700244var _ SourceFileProducer = (*fileGroup)(nil)
Yu Liu2aa806b2022-09-01 11:54:47 -0700245var _ FileGroupAsLibrary = (*fileGroup)(nil)
Alixb29a3cd2023-05-09 20:37:49 +0000246var _ FileGroupPath = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -0800247
Patrice Arruda8958a942019-03-12 10:06:00 -0700248// filegroup contains a list of files that are referenced by other modules
249// properties (such as "srcs") using the syntax ":<name>". filegroup are
250// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700251func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -0800252 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -0700253 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700254 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500255 InitBazelModule(module)
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000256 InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700257 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800258}
259
Liz Kammer5edc1412022-05-25 11:12:44 -0400260var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
261
262func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
263 ins := make([]string, 0, len(fg.srcs))
264 outs := make([]string, 0, len(fg.srcs))
265 for _, p := range fg.srcs {
266 ins = append(ins, p.String())
267 outs = append(outs, p.Rel())
268 }
269 return []blueprint.JSONAction{
270 blueprint.JSONAction{
271 Inputs: ins,
272 Outputs: outs,
273 },
274 }
275}
276
Liz Kammer5bde22f2021-04-19 14:04:14 -0400277func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400278 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800279 if fg.properties.Path != nil {
280 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
281 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800282}
283
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700284func (fg *fileGroup) Srcs() Paths {
285 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800286}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700287
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700288func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
289 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
290 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700291 }
292}
Chris Parsonsf874e462022-05-10 13:50:12 -0400293
294func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) {
295 bazelCtx := ctx.Config().BazelContext
296
297 bazelCtx.QueueBazelRequest(
298 fg.GetBazelLabel(ctx, fg),
299 cquery.GetOutputFiles,
Yu Liue4312402023-01-18 09:15:31 -0800300 configKey{arch: Common.String(), osType: CommonOS})
Chris Parsonsf874e462022-05-10 13:50:12 -0400301}
302
303func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool {
Liz Kammer748209c2022-10-24 10:43:27 -0400304 // TODO(b/247782695), TODO(b/242847534) Fix mixed builds for filegroups
305 return false
Chris Parsonsf874e462022-05-10 13:50:12 -0400306}
307
308func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400309 bazelCtx := ctx.Config().BazelContext
310 // This is a short-term solution because we rely on info from Android.bp to handle
311 // a converted module. This will block when we want to remove Android.bp for all
312 // converted modules at some point.
313 // TODO(b/242847534): Implement a long-term solution in which we don't need to rely
314 // on info form Android.bp for modules that are already converted to Bazel
315 relativeRoot := ctx.ModuleDir()
Chris Parsonsf874e462022-05-10 13:50:12 -0400316 if fg.properties.Path != nil {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400317 relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path)
Chris Parsonsf874e462022-05-10 13:50:12 -0400318 }
319
Yu Liue4312402023-01-18 09:15:31 -0800320 filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{arch: Common.String(), osType: CommonOS})
Chris Parsonsf874e462022-05-10 13:50:12 -0400321 if err != nil {
322 ctx.ModuleErrorf(err.Error())
323 return
324 }
325
326 bazelOuts := make(Paths, 0, len(filePaths))
327 for _, p := range filePaths {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400328 bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p))
Chris Parsonsf874e462022-05-10 13:50:12 -0400329 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400330 fg.srcs = bazelOuts
331}
Vinh Tran444154d2022-08-16 13:10:31 -0400332
333func (fg *fileGroup) ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700334 return fg.shouldConvertToLibrary(ctx, ".aidl")
335}
336
337func (fg *fileGroup) ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool {
338 return fg.shouldConvertToLibrary(ctx, ".proto")
339}
340
341func (fg *fileGroup) shouldConvertToLibrary(ctx BazelConversionPathContext, suffix string) bool {
Vinh Tran444154d2022-08-16 13:10:31 -0400342 if len(fg.properties.Srcs) == 0 || !fg.ShouldConvertWithBp2build(ctx) {
343 return false
344 }
345 for _, src := range fg.properties.Srcs {
Yu Liu2aa806b2022-09-01 11:54:47 -0700346 if !strings.HasSuffix(src, suffix) {
Vinh Tran444154d2022-08-16 13:10:31 -0400347 return false
348 }
349 }
350 return true
351}
352
353func (fg *fileGroup) GetAidlLibraryLabel(ctx BazelConversionPathContext) string {
Yu Liu2aa806b2022-09-01 11:54:47 -0700354 return fg.getFileGroupAsLibraryLabel(ctx)
355}
356
357func (fg *fileGroup) GetProtoLibraryLabel(ctx BazelConversionPathContext) string {
358 return fg.getFileGroupAsLibraryLabel(ctx) + convertedProtoLibrarySuffix
359}
360
361func (fg *fileGroup) getFileGroupAsLibraryLabel(ctx BazelConversionPathContext) string {
Vinh Tran444154d2022-08-16 13:10:31 -0400362 if ctx.OtherModuleDir(fg.module) == ctx.ModuleDir() {
363 return ":" + fg.Name()
364 } else {
365 return fg.GetBazelLabel(ctx, fg)
366 }
367}
Sam Delmerico97bd1272022-08-25 14:45:31 -0400368
369// Given a name in srcs prop, check to see if the name references a filegroup
370// and the filegroup is converted to aidl_library
371func IsConvertedToAidlLibrary(ctx BazelConversionPathContext, name string) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700372 if fg, ok := ToFileGroupAsLibrary(ctx, name); ok {
373 return fg.ShouldConvertToAidlLibrary(ctx)
374 }
375 return false
376}
377
378func ToFileGroupAsLibrary(ctx BazelConversionPathContext, name string) (FileGroupAsLibrary, bool) {
Sam Delmerico97bd1272022-08-25 14:45:31 -0400379 if module, ok := ctx.ModuleFromName(name); ok {
380 if IsFilegroup(ctx, module) {
Yu Liu2aa806b2022-09-01 11:54:47 -0700381 if fg, ok := module.(FileGroupAsLibrary); ok {
382 return fg, true
Sam Delmerico97bd1272022-08-25 14:45:31 -0400383 }
384 }
385 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700386 return nil, false
Sam Delmerico97bd1272022-08-25 14:45:31 -0400387}
Anton Hansson7d6dd8b2023-03-06 11:26:17 +0000388
389// Defaults
390type FileGroupDefaults struct {
391 ModuleBase
392 DefaultsModuleBase
393}
394
395func FileGroupDefaultsFactory() Module {
396 module := &FileGroupDefaults{}
397 module.AddProperties(&fileGroupProperties{})
398 InitDefaultsModule(module)
399
400 return module
401}