blob: 0f6e00e86f4e123f4311541443c109b712aaa2a0 [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() {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070029 RegisterModuleType("filegroup", FileGroupFactory)
Jingwen Chen32b4ece2021-01-21 03:20:18 -050030}
31
Paul Duffin35816122021-02-24 01:49:52 +000032var PrepareForTestWithFilegroup = FixtureRegisterWithContext(func(ctx RegistrationContext) {
33 ctx.RegisterModuleType("filegroup", FileGroupFactory)
34})
35
Yu Liu2aa806b2022-09-01 11:54:47 -070036var convertedProtoLibrarySuffix = "_bp2build_converted"
37
Sam Delmericoc7681022022-02-04 21:01:20 +000038// IsFilegroup checks that a module is a filegroup type
39func IsFilegroup(ctx bazel.OtherModuleContext, m blueprint.Module) bool {
40 return ctx.OtherModuleType(m) == "filegroup"
41}
42
Sam Delmerico97bd1272022-08-25 14:45:31 -040043var (
44 // ignoring case, checks for proto or protos as an independent word in the name, whether at the
45 // beginning, end, or middle. e.g. "proto.foo", "bar-protos", "baz_proto_srcs" would all match
46 filegroupLikelyProtoPattern = regexp.MustCompile("(?i)(^|[^a-z])proto(s)?([^a-z]|$)")
47 filegroupLikelyAidlPattern = regexp.MustCompile("(?i)(^|[^a-z])aidl([^a-z]|$)")
48
49 ProtoSrcLabelPartition = bazel.LabelPartition{
50 Extensions: []string{".proto"},
51 LabelMapper: isFilegroupWithPattern(filegroupLikelyProtoPattern),
52 }
53 AidlSrcLabelPartition = bazel.LabelPartition{
54 Extensions: []string{".aidl"},
55 LabelMapper: isFilegroupWithPattern(filegroupLikelyAidlPattern),
56 }
57)
58
59func isFilegroupWithPattern(pattern *regexp.Regexp) bazel.LabelMapper {
60 return func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) {
61 m, exists := ctx.ModuleFromName(label.OriginalModuleName)
62 labelStr := label.Label
63 if !exists || !IsFilegroup(ctx, m) {
64 return labelStr, false
65 }
66 likelyMatched := pattern.MatchString(label.OriginalModuleName)
67 return labelStr, likelyMatched
68 }
69}
70
Jingwen Chen32b4ece2021-01-21 03:20:18 -050071// https://docs.bazel.build/versions/master/be/general.html#filegroup
72type bazelFilegroupAttributes struct {
Jingwen Chen07027912021-03-15 06:02:43 -040073 Srcs bazel.LabelListAttribute
Jingwen Chen32b4ece2021-01-21 03:20:18 -050074}
75
Vinh Tran444154d2022-08-16 13:10:31 -040076type bazelAidlLibraryAttributes struct {
77 Srcs bazel.LabelListAttribute
78 Strip_import_prefix *string
79}
80
Spandan Dasbd52ea92023-03-09 23:03:07 +000081// api srcs can be contained in filegroups.
82// this should be generated in api_bp2build workspace as well.
83func (fg *fileGroup) ConvertWithApiBp2build(ctx TopDownMutatorContext) {
84 fg.ConvertWithBp2build(ctx)
85}
86
Liz Kammerbe46fcc2021-11-01 15:32:43 -040087// ConvertWithBp2build performs bp2build conversion of filegroup
88func (fg *fileGroup) ConvertWithBp2build(ctx TopDownMutatorContext) {
Jingwen Chen07027912021-03-15 06:02:43 -040089 srcs := bazel.MakeLabelListAttribute(
90 BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs))
Jingwen Chen5146ac02021-09-02 11:44:42 +000091
92 // For Bazel compatibility, don't generate the filegroup if there is only 1
93 // source file, and that the source file is named the same as the module
94 // itself. In Bazel, eponymous filegroups like this would be an error.
95 //
96 // Instead, dependents on this single-file filegroup can just depend
97 // on the file target, instead of rule target, directly.
98 //
99 // You may ask: what if a filegroup has multiple files, and one of them
100 // shares the name? The answer: we haven't seen that in the wild, and
101 // should lock Soong itself down to prevent the behavior. For now,
102 // we raise an error if bp2build sees this problem.
103 for _, f := range srcs.Value.Includes {
104 if f.Label == fg.Name() {
105 if len(srcs.Value.Includes) > 1 {
106 ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name())
107 }
108 return
109 }
110 }
111
Vinh Tran444154d2022-08-16 13:10:31 -0400112 // Convert module that has only AIDL files to aidl_library
113 // If the module has a mixed bag of AIDL and non-AIDL files, split the filegroup manually
114 // and then convert
115 if fg.ShouldConvertToAidlLibrary(ctx) {
116 attrs := &bazelAidlLibraryAttributes{
117 Srcs: srcs,
118 Strip_import_prefix: fg.properties.Path,
119 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500120
Vinh Tran444154d2022-08-16 13:10:31 -0400121 props := bazel.BazelTargetModuleProperties{
122 Rule_class: "aidl_library",
123 Bzl_load_location: "//build/bazel/rules/aidl:library.bzl",
124 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500125
Vinh Tran444154d2022-08-16 13:10:31 -0400126 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
127 } else {
Yu Liu2aa806b2022-09-01 11:54:47 -0700128 if fg.ShouldConvertToProtoLibrary(ctx) {
Yu Liu2a85fb12022-09-15 22:18:48 -0700129 // TODO(b/246997908): we can remove this tag if we could figure out a
130 // solution for this bug.
Yu Liu2aa806b2022-09-01 11:54:47 -0700131 attrs := &ProtoAttrs{
132 Srcs: srcs,
133 Strip_import_prefix: fg.properties.Path,
134 }
135
Sam Delmericoe9b33f72022-11-21 15:38:54 -0500136 tags := []string{"manual"}
Yu Liu2aa806b2022-09-01 11:54:47 -0700137 ctx.CreateBazelTargetModule(
138 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
Sam Delmericoe9b33f72022-11-21 15:38:54 -0500139 CommonAttributes{
140 Name: fg.Name() + convertedProtoLibrarySuffix,
141 Tags: bazel.MakeStringListAttribute(tags),
142 },
Yu Liu2aa806b2022-09-01 11:54:47 -0700143 attrs)
144 }
145
146 // TODO(b/242847534): Still convert to a filegroup because other unconverted
147 // modules may depend on the filegroup
Vinh Tran444154d2022-08-16 13:10:31 -0400148 attrs := &bazelFilegroupAttributes{
149 Srcs: srcs,
150 }
151
152 props := bazel.BazelTargetModuleProperties{
153 Rule_class: "filegroup",
154 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
155 }
156
157 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
158 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800159}
160
161type fileGroupProperties struct {
162 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -0800163 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -0800164
Colin Cross27b922f2019-03-04 22:35:41 -0800165 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800166
167 // The base path to the files. May be used by other modules to determine which portion
168 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
169 // the base path is stripped off the path and the remaining path is used as the
170 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -0800171 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -0700172
173 // Create a make variable with the specified name that contains the list of files in the
174 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -0800175 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -0800176}
177
178type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700179 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500180 BazelModuleBase
Yu Liu2aa806b2022-09-01 11:54:47 -0700181 FileGroupAsLibrary
Colin Cross068e0fe2016-12-13 15:23:47 -0800182 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700183 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -0800184}
185
Chris Parsonsf874e462022-05-10 13:50:12 -0400186var _ MixedBuildBuildable = (*fileGroup)(nil)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700187var _ SourceFileProducer = (*fileGroup)(nil)
Yu Liu2aa806b2022-09-01 11:54:47 -0700188var _ FileGroupAsLibrary = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -0800189
Patrice Arruda8958a942019-03-12 10:06:00 -0700190// filegroup contains a list of files that are referenced by other modules
191// properties (such as "srcs") using the syntax ":<name>". filegroup are
192// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700193func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -0800194 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -0700195 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700196 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500197 InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700198 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800199}
200
Liz Kammer5edc1412022-05-25 11:12:44 -0400201var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
202
203func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
204 ins := make([]string, 0, len(fg.srcs))
205 outs := make([]string, 0, len(fg.srcs))
206 for _, p := range fg.srcs {
207 ins = append(ins, p.String())
208 outs = append(outs, p.Rel())
209 }
210 return []blueprint.JSONAction{
211 blueprint.JSONAction{
212 Inputs: ins,
213 Outputs: outs,
214 },
215 }
216}
217
Liz Kammer5bde22f2021-04-19 14:04:14 -0400218func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400219 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800220 if fg.properties.Path != nil {
221 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
222 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800223}
224
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700225func (fg *fileGroup) Srcs() Paths {
226 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800227}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700228
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700229func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
230 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
231 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700232 }
233}
Chris Parsonsf874e462022-05-10 13:50:12 -0400234
235func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) {
236 bazelCtx := ctx.Config().BazelContext
237
238 bazelCtx.QueueBazelRequest(
239 fg.GetBazelLabel(ctx, fg),
240 cquery.GetOutputFiles,
Yu Liue4312402023-01-18 09:15:31 -0800241 configKey{arch: Common.String(), osType: CommonOS})
Chris Parsonsf874e462022-05-10 13:50:12 -0400242}
243
244func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool {
Liz Kammer748209c2022-10-24 10:43:27 -0400245 // TODO(b/247782695), TODO(b/242847534) Fix mixed builds for filegroups
246 return false
Chris Parsonsf874e462022-05-10 13:50:12 -0400247}
248
249func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400250 bazelCtx := ctx.Config().BazelContext
251 // This is a short-term solution because we rely on info from Android.bp to handle
252 // a converted module. This will block when we want to remove Android.bp for all
253 // converted modules at some point.
254 // TODO(b/242847534): Implement a long-term solution in which we don't need to rely
255 // on info form Android.bp for modules that are already converted to Bazel
256 relativeRoot := ctx.ModuleDir()
Chris Parsonsf874e462022-05-10 13:50:12 -0400257 if fg.properties.Path != nil {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400258 relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path)
Chris Parsonsf874e462022-05-10 13:50:12 -0400259 }
260
Yu Liue4312402023-01-18 09:15:31 -0800261 filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{arch: Common.String(), osType: CommonOS})
Chris Parsonsf874e462022-05-10 13:50:12 -0400262 if err != nil {
263 ctx.ModuleErrorf(err.Error())
264 return
265 }
266
267 bazelOuts := make(Paths, 0, len(filePaths))
268 for _, p := range filePaths {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400269 bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p))
Chris Parsonsf874e462022-05-10 13:50:12 -0400270 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400271 fg.srcs = bazelOuts
272}
Vinh Tran444154d2022-08-16 13:10:31 -0400273
274func (fg *fileGroup) ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700275 return fg.shouldConvertToLibrary(ctx, ".aidl")
276}
277
278func (fg *fileGroup) ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool {
279 return fg.shouldConvertToLibrary(ctx, ".proto")
280}
281
282func (fg *fileGroup) shouldConvertToLibrary(ctx BazelConversionPathContext, suffix string) bool {
Vinh Tran444154d2022-08-16 13:10:31 -0400283 if len(fg.properties.Srcs) == 0 || !fg.ShouldConvertWithBp2build(ctx) {
284 return false
285 }
286 for _, src := range fg.properties.Srcs {
Yu Liu2aa806b2022-09-01 11:54:47 -0700287 if !strings.HasSuffix(src, suffix) {
Vinh Tran444154d2022-08-16 13:10:31 -0400288 return false
289 }
290 }
291 return true
292}
293
294func (fg *fileGroup) GetAidlLibraryLabel(ctx BazelConversionPathContext) string {
Yu Liu2aa806b2022-09-01 11:54:47 -0700295 return fg.getFileGroupAsLibraryLabel(ctx)
296}
297
298func (fg *fileGroup) GetProtoLibraryLabel(ctx BazelConversionPathContext) string {
299 return fg.getFileGroupAsLibraryLabel(ctx) + convertedProtoLibrarySuffix
300}
301
302func (fg *fileGroup) getFileGroupAsLibraryLabel(ctx BazelConversionPathContext) string {
Vinh Tran444154d2022-08-16 13:10:31 -0400303 if ctx.OtherModuleDir(fg.module) == ctx.ModuleDir() {
304 return ":" + fg.Name()
305 } else {
306 return fg.GetBazelLabel(ctx, fg)
307 }
308}
Sam Delmerico97bd1272022-08-25 14:45:31 -0400309
310// Given a name in srcs prop, check to see if the name references a filegroup
311// and the filegroup is converted to aidl_library
312func IsConvertedToAidlLibrary(ctx BazelConversionPathContext, name string) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700313 if fg, ok := ToFileGroupAsLibrary(ctx, name); ok {
314 return fg.ShouldConvertToAidlLibrary(ctx)
315 }
316 return false
317}
318
319func ToFileGroupAsLibrary(ctx BazelConversionPathContext, name string) (FileGroupAsLibrary, bool) {
Sam Delmerico97bd1272022-08-25 14:45:31 -0400320 if module, ok := ctx.ModuleFromName(name); ok {
321 if IsFilegroup(ctx, module) {
Yu Liu2aa806b2022-09-01 11:54:47 -0700322 if fg, ok := module.(FileGroupAsLibrary); ok {
323 return fg, true
Sam Delmerico97bd1272022-08-25 14:45:31 -0400324 }
325 }
326 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700327 return nil, false
Sam Delmerico97bd1272022-08-25 14:45:31 -0400328}