blob: 7d929bc4e3fb67fd910370d036a15caa0566a84e [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
Liz Kammerbe46fcc2021-11-01 15:32:43 -040081// ConvertWithBp2build performs bp2build conversion of filegroup
82func (fg *fileGroup) ConvertWithBp2build(ctx TopDownMutatorContext) {
Jingwen Chen07027912021-03-15 06:02:43 -040083 srcs := bazel.MakeLabelListAttribute(
84 BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs))
Jingwen Chen5146ac02021-09-02 11:44:42 +000085
86 // For Bazel compatibility, don't generate the filegroup if there is only 1
87 // source file, and that the source file is named the same as the module
88 // itself. In Bazel, eponymous filegroups like this would be an error.
89 //
90 // Instead, dependents on this single-file filegroup can just depend
91 // on the file target, instead of rule target, directly.
92 //
93 // You may ask: what if a filegroup has multiple files, and one of them
94 // shares the name? The answer: we haven't seen that in the wild, and
95 // should lock Soong itself down to prevent the behavior. For now,
96 // we raise an error if bp2build sees this problem.
97 for _, f := range srcs.Value.Includes {
98 if f.Label == fg.Name() {
99 if len(srcs.Value.Includes) > 1 {
100 ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name())
101 }
102 return
103 }
104 }
105
Vinh Tran444154d2022-08-16 13:10:31 -0400106 // Convert module that has only AIDL files to aidl_library
107 // If the module has a mixed bag of AIDL and non-AIDL files, split the filegroup manually
108 // and then convert
109 if fg.ShouldConvertToAidlLibrary(ctx) {
110 attrs := &bazelAidlLibraryAttributes{
111 Srcs: srcs,
112 Strip_import_prefix: fg.properties.Path,
113 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500114
Vinh Tran444154d2022-08-16 13:10:31 -0400115 props := bazel.BazelTargetModuleProperties{
116 Rule_class: "aidl_library",
117 Bzl_load_location: "//build/bazel/rules/aidl:library.bzl",
118 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500119
Vinh Tran444154d2022-08-16 13:10:31 -0400120 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
121 } else {
Yu Liu2aa806b2022-09-01 11:54:47 -0700122 if fg.ShouldConvertToProtoLibrary(ctx) {
Yu Liu2a85fb12022-09-15 22:18:48 -0700123 // TODO(b/246997908): we can remove this tag if we could figure out a
124 // solution for this bug.
Yu Liu2aa806b2022-09-01 11:54:47 -0700125 attrs := &ProtoAttrs{
126 Srcs: srcs,
127 Strip_import_prefix: fg.properties.Path,
128 }
129
Sam Delmericoe9b33f72022-11-21 15:38:54 -0500130 tags := []string{"manual"}
Yu Liu2aa806b2022-09-01 11:54:47 -0700131 ctx.CreateBazelTargetModule(
132 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
Sam Delmericoe9b33f72022-11-21 15:38:54 -0500133 CommonAttributes{
134 Name: fg.Name() + convertedProtoLibrarySuffix,
135 Tags: bazel.MakeStringListAttribute(tags),
136 },
Yu Liu2aa806b2022-09-01 11:54:47 -0700137 attrs)
138 }
139
140 // TODO(b/242847534): Still convert to a filegroup because other unconverted
141 // modules may depend on the filegroup
Vinh Tran444154d2022-08-16 13:10:31 -0400142 attrs := &bazelFilegroupAttributes{
143 Srcs: srcs,
144 }
145
146 props := bazel.BazelTargetModuleProperties{
147 Rule_class: "filegroup",
148 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
149 }
150
151 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
152 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800153}
154
155type fileGroupProperties struct {
156 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -0800157 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -0800158
Colin Cross27b922f2019-03-04 22:35:41 -0800159 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800160
161 // The base path to the files. May be used by other modules to determine which portion
162 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
163 // the base path is stripped off the path and the remaining path is used as the
164 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -0800165 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -0700166
167 // Create a make variable with the specified name that contains the list of files in the
168 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -0800169 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -0800170}
171
172type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700173 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500174 BazelModuleBase
Yu Liu2aa806b2022-09-01 11:54:47 -0700175 FileGroupAsLibrary
Colin Cross068e0fe2016-12-13 15:23:47 -0800176 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700177 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -0800178}
179
Chris Parsonsf874e462022-05-10 13:50:12 -0400180var _ MixedBuildBuildable = (*fileGroup)(nil)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700181var _ SourceFileProducer = (*fileGroup)(nil)
Yu Liu2aa806b2022-09-01 11:54:47 -0700182var _ FileGroupAsLibrary = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -0800183
Patrice Arruda8958a942019-03-12 10:06:00 -0700184// filegroup contains a list of files that are referenced by other modules
185// properties (such as "srcs") using the syntax ":<name>". filegroup are
186// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700187func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -0800188 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -0700189 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700190 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500191 InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700192 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800193}
194
Liz Kammer5edc1412022-05-25 11:12:44 -0400195var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
196
197func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
198 ins := make([]string, 0, len(fg.srcs))
199 outs := make([]string, 0, len(fg.srcs))
200 for _, p := range fg.srcs {
201 ins = append(ins, p.String())
202 outs = append(outs, p.Rel())
203 }
204 return []blueprint.JSONAction{
205 blueprint.JSONAction{
206 Inputs: ins,
207 Outputs: outs,
208 },
209 }
210}
211
Liz Kammer5bde22f2021-04-19 14:04:14 -0400212func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400213 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800214 if fg.properties.Path != nil {
215 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
216 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800217}
218
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700219func (fg *fileGroup) Srcs() Paths {
220 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800221}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700222
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700223func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
224 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
225 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700226 }
227}
Chris Parsonsf874e462022-05-10 13:50:12 -0400228
229func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) {
230 bazelCtx := ctx.Config().BazelContext
231
232 bazelCtx.QueueBazelRequest(
233 fg.GetBazelLabel(ctx, fg),
234 cquery.GetOutputFiles,
Yu Liue4312402023-01-18 09:15:31 -0800235 configKey{arch: Common.String(), osType: CommonOS})
Chris Parsonsf874e462022-05-10 13:50:12 -0400236}
237
238func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool {
Liz Kammer748209c2022-10-24 10:43:27 -0400239 // TODO(b/247782695), TODO(b/242847534) Fix mixed builds for filegroups
240 return false
Chris Parsonsf874e462022-05-10 13:50:12 -0400241}
242
243func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400244 bazelCtx := ctx.Config().BazelContext
245 // This is a short-term solution because we rely on info from Android.bp to handle
246 // a converted module. This will block when we want to remove Android.bp for all
247 // converted modules at some point.
248 // TODO(b/242847534): Implement a long-term solution in which we don't need to rely
249 // on info form Android.bp for modules that are already converted to Bazel
250 relativeRoot := ctx.ModuleDir()
Chris Parsonsf874e462022-05-10 13:50:12 -0400251 if fg.properties.Path != nil {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400252 relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path)
Chris Parsonsf874e462022-05-10 13:50:12 -0400253 }
254
Yu Liue4312402023-01-18 09:15:31 -0800255 filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{arch: Common.String(), osType: CommonOS})
Chris Parsonsf874e462022-05-10 13:50:12 -0400256 if err != nil {
257 ctx.ModuleErrorf(err.Error())
258 return
259 }
260
261 bazelOuts := make(Paths, 0, len(filePaths))
262 for _, p := range filePaths {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400263 bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p))
Chris Parsonsf874e462022-05-10 13:50:12 -0400264 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400265 fg.srcs = bazelOuts
266}
Vinh Tran444154d2022-08-16 13:10:31 -0400267
268func (fg *fileGroup) ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700269 return fg.shouldConvertToLibrary(ctx, ".aidl")
270}
271
272func (fg *fileGroup) ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool {
273 return fg.shouldConvertToLibrary(ctx, ".proto")
274}
275
276func (fg *fileGroup) shouldConvertToLibrary(ctx BazelConversionPathContext, suffix string) bool {
Vinh Tran444154d2022-08-16 13:10:31 -0400277 if len(fg.properties.Srcs) == 0 || !fg.ShouldConvertWithBp2build(ctx) {
278 return false
279 }
280 for _, src := range fg.properties.Srcs {
Yu Liu2aa806b2022-09-01 11:54:47 -0700281 if !strings.HasSuffix(src, suffix) {
Vinh Tran444154d2022-08-16 13:10:31 -0400282 return false
283 }
284 }
285 return true
286}
287
288func (fg *fileGroup) GetAidlLibraryLabel(ctx BazelConversionPathContext) string {
Yu Liu2aa806b2022-09-01 11:54:47 -0700289 return fg.getFileGroupAsLibraryLabel(ctx)
290}
291
292func (fg *fileGroup) GetProtoLibraryLabel(ctx BazelConversionPathContext) string {
293 return fg.getFileGroupAsLibraryLabel(ctx) + convertedProtoLibrarySuffix
294}
295
296func (fg *fileGroup) getFileGroupAsLibraryLabel(ctx BazelConversionPathContext) string {
Vinh Tran444154d2022-08-16 13:10:31 -0400297 if ctx.OtherModuleDir(fg.module) == ctx.ModuleDir() {
298 return ":" + fg.Name()
299 } else {
300 return fg.GetBazelLabel(ctx, fg)
301 }
302}
Sam Delmerico97bd1272022-08-25 14:45:31 -0400303
304// Given a name in srcs prop, check to see if the name references a filegroup
305// and the filegroup is converted to aidl_library
306func IsConvertedToAidlLibrary(ctx BazelConversionPathContext, name string) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700307 if fg, ok := ToFileGroupAsLibrary(ctx, name); ok {
308 return fg.ShouldConvertToAidlLibrary(ctx)
309 }
310 return false
311}
312
313func ToFileGroupAsLibrary(ctx BazelConversionPathContext, name string) (FileGroupAsLibrary, bool) {
Sam Delmerico97bd1272022-08-25 14:45:31 -0400314 if module, ok := ctx.ModuleFromName(name); ok {
315 if IsFilegroup(ctx, module) {
Yu Liu2aa806b2022-09-01 11:54:47 -0700316 if fg, ok := module.(FileGroupAsLibrary); ok {
317 return fg, true
Sam Delmerico97bd1272022-08-25 14:45:31 -0400318 }
319 }
320 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700321 return nil, false
Sam Delmerico97bd1272022-08-25 14:45:31 -0400322}