blob: e5eec87cfe37d8e556e40b4dcc803958f80d469f [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) {
123 attrs := &ProtoAttrs{
124 Srcs: srcs,
125 Strip_import_prefix: fg.properties.Path,
126 }
127
128 ctx.CreateBazelTargetModule(
129 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
130 CommonAttributes{Name: fg.Name() + convertedProtoLibrarySuffix},
131 attrs)
132 }
133
134 // TODO(b/242847534): Still convert to a filegroup because other unconverted
135 // modules may depend on the filegroup
Vinh Tran444154d2022-08-16 13:10:31 -0400136 attrs := &bazelFilegroupAttributes{
137 Srcs: srcs,
138 }
139
140 props := bazel.BazelTargetModuleProperties{
141 Rule_class: "filegroup",
142 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
143 }
144
145 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
146 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800147}
148
149type fileGroupProperties struct {
150 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -0800151 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -0800152
Colin Cross27b922f2019-03-04 22:35:41 -0800153 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800154
155 // The base path to the files. May be used by other modules to determine which portion
156 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
157 // the base path is stripped off the path and the remaining path is used as the
158 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -0800159 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -0700160
161 // Create a make variable with the specified name that contains the list of files in the
162 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -0800163 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -0800164}
165
166type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700167 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500168 BazelModuleBase
Yu Liu2aa806b2022-09-01 11:54:47 -0700169 FileGroupAsLibrary
Colin Cross068e0fe2016-12-13 15:23:47 -0800170 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700171 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -0800172}
173
Chris Parsonsf874e462022-05-10 13:50:12 -0400174var _ MixedBuildBuildable = (*fileGroup)(nil)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700175var _ SourceFileProducer = (*fileGroup)(nil)
Yu Liu2aa806b2022-09-01 11:54:47 -0700176var _ FileGroupAsLibrary = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -0800177
Patrice Arruda8958a942019-03-12 10:06:00 -0700178// filegroup contains a list of files that are referenced by other modules
179// properties (such as "srcs") using the syntax ":<name>". filegroup are
180// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700181func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -0800182 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -0700183 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700184 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500185 InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700186 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800187}
188
Liz Kammer5edc1412022-05-25 11:12:44 -0400189var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
190
191func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
192 ins := make([]string, 0, len(fg.srcs))
193 outs := make([]string, 0, len(fg.srcs))
194 for _, p := range fg.srcs {
195 ins = append(ins, p.String())
196 outs = append(outs, p.Rel())
197 }
198 return []blueprint.JSONAction{
199 blueprint.JSONAction{
200 Inputs: ins,
201 Outputs: outs,
202 },
203 }
204}
205
Liz Kammer5bde22f2021-04-19 14:04:14 -0400206func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400207 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800208 if fg.properties.Path != nil {
209 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
210 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800211}
212
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700213func (fg *fileGroup) Srcs() Paths {
214 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800215}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700216
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700217func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
218 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
219 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700220 }
221}
Chris Parsonsf874e462022-05-10 13:50:12 -0400222
223func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) {
224 bazelCtx := ctx.Config().BazelContext
225
226 bazelCtx.QueueBazelRequest(
227 fg.GetBazelLabel(ctx, fg),
228 cquery.GetOutputFiles,
229 configKey{Common.String(), CommonOS})
230}
231
232func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool {
233 return true
234}
235
236func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400237 bazelCtx := ctx.Config().BazelContext
238 // This is a short-term solution because we rely on info from Android.bp to handle
239 // a converted module. This will block when we want to remove Android.bp for all
240 // converted modules at some point.
241 // TODO(b/242847534): Implement a long-term solution in which we don't need to rely
242 // on info form Android.bp for modules that are already converted to Bazel
243 relativeRoot := ctx.ModuleDir()
Chris Parsonsf874e462022-05-10 13:50:12 -0400244 if fg.properties.Path != nil {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400245 relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path)
Chris Parsonsf874e462022-05-10 13:50:12 -0400246 }
247
Chris Parsonsf874e462022-05-10 13:50:12 -0400248 filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{Common.String(), CommonOS})
249 if err != nil {
250 ctx.ModuleErrorf(err.Error())
251 return
252 }
253
254 bazelOuts := make(Paths, 0, len(filePaths))
255 for _, p := range filePaths {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400256 bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p))
Chris Parsonsf874e462022-05-10 13:50:12 -0400257 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400258 fg.srcs = bazelOuts
259}
Vinh Tran444154d2022-08-16 13:10:31 -0400260
261func (fg *fileGroup) ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700262 return fg.shouldConvertToLibrary(ctx, ".aidl")
263}
264
265func (fg *fileGroup) ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool {
266 return fg.shouldConvertToLibrary(ctx, ".proto")
267}
268
269func (fg *fileGroup) shouldConvertToLibrary(ctx BazelConversionPathContext, suffix string) bool {
Vinh Tran444154d2022-08-16 13:10:31 -0400270 if len(fg.properties.Srcs) == 0 || !fg.ShouldConvertWithBp2build(ctx) {
271 return false
272 }
273 for _, src := range fg.properties.Srcs {
Yu Liu2aa806b2022-09-01 11:54:47 -0700274 if !strings.HasSuffix(src, suffix) {
Vinh Tran444154d2022-08-16 13:10:31 -0400275 return false
276 }
277 }
278 return true
279}
280
281func (fg *fileGroup) GetAidlLibraryLabel(ctx BazelConversionPathContext) string {
Yu Liu2aa806b2022-09-01 11:54:47 -0700282 return fg.getFileGroupAsLibraryLabel(ctx)
283}
284
285func (fg *fileGroup) GetProtoLibraryLabel(ctx BazelConversionPathContext) string {
286 return fg.getFileGroupAsLibraryLabel(ctx) + convertedProtoLibrarySuffix
287}
288
289func (fg *fileGroup) getFileGroupAsLibraryLabel(ctx BazelConversionPathContext) string {
Vinh Tran444154d2022-08-16 13:10:31 -0400290 if ctx.OtherModuleDir(fg.module) == ctx.ModuleDir() {
291 return ":" + fg.Name()
292 } else {
293 return fg.GetBazelLabel(ctx, fg)
294 }
295}
Sam Delmerico97bd1272022-08-25 14:45:31 -0400296
297// Given a name in srcs prop, check to see if the name references a filegroup
298// and the filegroup is converted to aidl_library
299func IsConvertedToAidlLibrary(ctx BazelConversionPathContext, name string) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700300 if fg, ok := ToFileGroupAsLibrary(ctx, name); ok {
301 return fg.ShouldConvertToAidlLibrary(ctx)
302 }
303 return false
304}
305
306func ToFileGroupAsLibrary(ctx BazelConversionPathContext, name string) (FileGroupAsLibrary, bool) {
Sam Delmerico97bd1272022-08-25 14:45:31 -0400307 if module, ok := ctx.ModuleFromName(name); ok {
308 if IsFilegroup(ctx, module) {
Yu Liu2aa806b2022-09-01 11:54:47 -0700309 if fg, ok := module.(FileGroupAsLibrary); ok {
310 return fg, true
Sam Delmerico97bd1272022-08-25 14:45:31 -0400311 }
312 }
313 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700314 return nil, false
Sam Delmerico97bd1272022-08-25 14:45:31 -0400315}