blob: af4d89a51649657a3ee03e9ba6e3c3c3107a6003 [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.
125 tags := []string{"manual"}
Yu Liu2aa806b2022-09-01 11:54:47 -0700126 attrs := &ProtoAttrs{
127 Srcs: srcs,
128 Strip_import_prefix: fg.properties.Path,
Yu Liu2a85fb12022-09-15 22:18:48 -0700129 Tags: tags,
Yu Liu2aa806b2022-09-01 11:54:47 -0700130 }
131
132 ctx.CreateBazelTargetModule(
133 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
134 CommonAttributes{Name: fg.Name() + convertedProtoLibrarySuffix},
135 attrs)
136 }
137
138 // TODO(b/242847534): Still convert to a filegroup because other unconverted
139 // modules may depend on the filegroup
Vinh Tran444154d2022-08-16 13:10:31 -0400140 attrs := &bazelFilegroupAttributes{
141 Srcs: srcs,
142 }
143
144 props := bazel.BazelTargetModuleProperties{
145 Rule_class: "filegroup",
146 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
147 }
148
149 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
150 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800151}
152
153type fileGroupProperties struct {
154 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -0800155 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -0800156
Colin Cross27b922f2019-03-04 22:35:41 -0800157 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800158
159 // The base path to the files. May be used by other modules to determine which portion
160 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
161 // the base path is stripped off the path and the remaining path is used as the
162 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -0800163 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -0700164
165 // Create a make variable with the specified name that contains the list of files in the
166 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -0800167 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -0800168}
169
170type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700171 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500172 BazelModuleBase
Yu Liu2aa806b2022-09-01 11:54:47 -0700173 FileGroupAsLibrary
Colin Cross068e0fe2016-12-13 15:23:47 -0800174 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700175 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -0800176}
177
Chris Parsonsf874e462022-05-10 13:50:12 -0400178var _ MixedBuildBuildable = (*fileGroup)(nil)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700179var _ SourceFileProducer = (*fileGroup)(nil)
Yu Liu2aa806b2022-09-01 11:54:47 -0700180var _ FileGroupAsLibrary = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -0800181
Patrice Arruda8958a942019-03-12 10:06:00 -0700182// filegroup contains a list of files that are referenced by other modules
183// properties (such as "srcs") using the syntax ":<name>". filegroup are
184// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700185func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -0800186 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -0700187 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700188 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500189 InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700190 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800191}
192
Liz Kammer5edc1412022-05-25 11:12:44 -0400193var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
194
195func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
196 ins := make([]string, 0, len(fg.srcs))
197 outs := make([]string, 0, len(fg.srcs))
198 for _, p := range fg.srcs {
199 ins = append(ins, p.String())
200 outs = append(outs, p.Rel())
201 }
202 return []blueprint.JSONAction{
203 blueprint.JSONAction{
204 Inputs: ins,
205 Outputs: outs,
206 },
207 }
208}
209
Liz Kammer5bde22f2021-04-19 14:04:14 -0400210func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400211 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800212 if fg.properties.Path != nil {
213 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
214 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800215}
216
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700217func (fg *fileGroup) Srcs() Paths {
218 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800219}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700220
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700221func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
222 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
223 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700224 }
225}
Chris Parsonsf874e462022-05-10 13:50:12 -0400226
227func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) {
228 bazelCtx := ctx.Config().BazelContext
229
230 bazelCtx.QueueBazelRequest(
231 fg.GetBazelLabel(ctx, fg),
232 cquery.GetOutputFiles,
233 configKey{Common.String(), CommonOS})
234}
235
236func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool {
Liz Kammer748209c2022-10-24 10:43:27 -0400237 // TODO(b/247782695), TODO(b/242847534) Fix mixed builds for filegroups
238 return false
Chris Parsonsf874e462022-05-10 13:50:12 -0400239}
240
241func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400242 bazelCtx := ctx.Config().BazelContext
243 // This is a short-term solution because we rely on info from Android.bp to handle
244 // a converted module. This will block when we want to remove Android.bp for all
245 // converted modules at some point.
246 // TODO(b/242847534): Implement a long-term solution in which we don't need to rely
247 // on info form Android.bp for modules that are already converted to Bazel
248 relativeRoot := ctx.ModuleDir()
Chris Parsonsf874e462022-05-10 13:50:12 -0400249 if fg.properties.Path != nil {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400250 relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path)
Chris Parsonsf874e462022-05-10 13:50:12 -0400251 }
252
Chris Parsonsf874e462022-05-10 13:50:12 -0400253 filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{Common.String(), CommonOS})
254 if err != nil {
255 ctx.ModuleErrorf(err.Error())
256 return
257 }
258
259 bazelOuts := make(Paths, 0, len(filePaths))
260 for _, p := range filePaths {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400261 bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p))
Chris Parsonsf874e462022-05-10 13:50:12 -0400262 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400263 fg.srcs = bazelOuts
264}
Vinh Tran444154d2022-08-16 13:10:31 -0400265
266func (fg *fileGroup) ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700267 return fg.shouldConvertToLibrary(ctx, ".aidl")
268}
269
270func (fg *fileGroup) ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool {
271 return fg.shouldConvertToLibrary(ctx, ".proto")
272}
273
274func (fg *fileGroup) shouldConvertToLibrary(ctx BazelConversionPathContext, suffix string) bool {
Vinh Tran444154d2022-08-16 13:10:31 -0400275 if len(fg.properties.Srcs) == 0 || !fg.ShouldConvertWithBp2build(ctx) {
276 return false
277 }
278 for _, src := range fg.properties.Srcs {
Yu Liu2aa806b2022-09-01 11:54:47 -0700279 if !strings.HasSuffix(src, suffix) {
Vinh Tran444154d2022-08-16 13:10:31 -0400280 return false
281 }
282 }
283 return true
284}
285
286func (fg *fileGroup) GetAidlLibraryLabel(ctx BazelConversionPathContext) string {
Yu Liu2aa806b2022-09-01 11:54:47 -0700287 return fg.getFileGroupAsLibraryLabel(ctx)
288}
289
290func (fg *fileGroup) GetProtoLibraryLabel(ctx BazelConversionPathContext) string {
291 return fg.getFileGroupAsLibraryLabel(ctx) + convertedProtoLibrarySuffix
292}
293
294func (fg *fileGroup) getFileGroupAsLibraryLabel(ctx BazelConversionPathContext) string {
Vinh Tran444154d2022-08-16 13:10:31 -0400295 if ctx.OtherModuleDir(fg.module) == ctx.ModuleDir() {
296 return ":" + fg.Name()
297 } else {
298 return fg.GetBazelLabel(ctx, fg)
299 }
300}
Sam Delmerico97bd1272022-08-25 14:45:31 -0400301
302// Given a name in srcs prop, check to see if the name references a filegroup
303// and the filegroup is converted to aidl_library
304func IsConvertedToAidlLibrary(ctx BazelConversionPathContext, name string) bool {
Yu Liu2aa806b2022-09-01 11:54:47 -0700305 if fg, ok := ToFileGroupAsLibrary(ctx, name); ok {
306 return fg.ShouldConvertToAidlLibrary(ctx)
307 }
308 return false
309}
310
311func ToFileGroupAsLibrary(ctx BazelConversionPathContext, name string) (FileGroupAsLibrary, bool) {
Sam Delmerico97bd1272022-08-25 14:45:31 -0400312 if module, ok := ctx.ModuleFromName(name); ok {
313 if IsFilegroup(ctx, module) {
Yu Liu2aa806b2022-09-01 11:54:47 -0700314 if fg, ok := module.(FileGroupAsLibrary); ok {
315 return fg, true
Sam Delmerico97bd1272022-08-25 14:45:31 -0400316 }
317 }
318 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700319 return nil, false
Sam Delmerico97bd1272022-08-25 14:45:31 -0400320}