blob: e609f63627f34fcc159466c44c27765fef9ad71d [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
Sam Delmericoc7681022022-02-04 21:01:20 +000036// IsFilegroup checks that a module is a filegroup type
37func IsFilegroup(ctx bazel.OtherModuleContext, m blueprint.Module) bool {
38 return ctx.OtherModuleType(m) == "filegroup"
39}
40
Sam Delmerico97bd1272022-08-25 14:45:31 -040041var (
42 // ignoring case, checks for proto or protos as an independent word in the name, whether at the
43 // beginning, end, or middle. e.g. "proto.foo", "bar-protos", "baz_proto_srcs" would all match
44 filegroupLikelyProtoPattern = regexp.MustCompile("(?i)(^|[^a-z])proto(s)?([^a-z]|$)")
45 filegroupLikelyAidlPattern = regexp.MustCompile("(?i)(^|[^a-z])aidl([^a-z]|$)")
46
47 ProtoSrcLabelPartition = bazel.LabelPartition{
48 Extensions: []string{".proto"},
49 LabelMapper: isFilegroupWithPattern(filegroupLikelyProtoPattern),
50 }
51 AidlSrcLabelPartition = bazel.LabelPartition{
52 Extensions: []string{".aidl"},
53 LabelMapper: isFilegroupWithPattern(filegroupLikelyAidlPattern),
54 }
55)
56
57func isFilegroupWithPattern(pattern *regexp.Regexp) bazel.LabelMapper {
58 return func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) {
59 m, exists := ctx.ModuleFromName(label.OriginalModuleName)
60 labelStr := label.Label
61 if !exists || !IsFilegroup(ctx, m) {
62 return labelStr, false
63 }
64 likelyMatched := pattern.MatchString(label.OriginalModuleName)
65 return labelStr, likelyMatched
66 }
67}
68
Jingwen Chen32b4ece2021-01-21 03:20:18 -050069// https://docs.bazel.build/versions/master/be/general.html#filegroup
70type bazelFilegroupAttributes struct {
Jingwen Chen07027912021-03-15 06:02:43 -040071 Srcs bazel.LabelListAttribute
Jingwen Chen32b4ece2021-01-21 03:20:18 -050072}
73
Vinh Tran444154d2022-08-16 13:10:31 -040074type bazelAidlLibraryAttributes struct {
75 Srcs bazel.LabelListAttribute
76 Strip_import_prefix *string
77}
78
Liz Kammerbe46fcc2021-11-01 15:32:43 -040079// ConvertWithBp2build performs bp2build conversion of filegroup
80func (fg *fileGroup) ConvertWithBp2build(ctx TopDownMutatorContext) {
Jingwen Chen07027912021-03-15 06:02:43 -040081 srcs := bazel.MakeLabelListAttribute(
82 BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs))
Jingwen Chen5146ac02021-09-02 11:44:42 +000083
84 // For Bazel compatibility, don't generate the filegroup if there is only 1
85 // source file, and that the source file is named the same as the module
86 // itself. In Bazel, eponymous filegroups like this would be an error.
87 //
88 // Instead, dependents on this single-file filegroup can just depend
89 // on the file target, instead of rule target, directly.
90 //
91 // You may ask: what if a filegroup has multiple files, and one of them
92 // shares the name? The answer: we haven't seen that in the wild, and
93 // should lock Soong itself down to prevent the behavior. For now,
94 // we raise an error if bp2build sees this problem.
95 for _, f := range srcs.Value.Includes {
96 if f.Label == fg.Name() {
97 if len(srcs.Value.Includes) > 1 {
98 ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name())
99 }
100 return
101 }
102 }
103
Vinh Tran444154d2022-08-16 13:10:31 -0400104 // Convert module that has only AIDL files to aidl_library
105 // If the module has a mixed bag of AIDL and non-AIDL files, split the filegroup manually
106 // and then convert
107 if fg.ShouldConvertToAidlLibrary(ctx) {
108 attrs := &bazelAidlLibraryAttributes{
109 Srcs: srcs,
110 Strip_import_prefix: fg.properties.Path,
111 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500112
Vinh Tran444154d2022-08-16 13:10:31 -0400113 props := bazel.BazelTargetModuleProperties{
114 Rule_class: "aidl_library",
115 Bzl_load_location: "//build/bazel/rules/aidl:library.bzl",
116 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500117
Vinh Tran444154d2022-08-16 13:10:31 -0400118 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
119 } else {
120 attrs := &bazelFilegroupAttributes{
121 Srcs: srcs,
122 }
123
124 props := bazel.BazelTargetModuleProperties{
125 Rule_class: "filegroup",
126 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
127 }
128
129 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
130 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800131}
132
133type fileGroupProperties struct {
134 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -0800135 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -0800136
Colin Cross27b922f2019-03-04 22:35:41 -0800137 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800138
139 // The base path to the files. May be used by other modules to determine which portion
140 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
141 // the base path is stripped off the path and the remaining path is used as the
142 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -0800143 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -0700144
145 // Create a make variable with the specified name that contains the list of files in the
146 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -0800147 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -0800148}
149
150type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700151 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500152 BazelModuleBase
Vinh Tran444154d2022-08-16 13:10:31 -0400153 Bp2buildAidlLibrary
Colin Cross068e0fe2016-12-13 15:23:47 -0800154 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700155 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -0800156}
157
Chris Parsonsf874e462022-05-10 13:50:12 -0400158var _ MixedBuildBuildable = (*fileGroup)(nil)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700159var _ SourceFileProducer = (*fileGroup)(nil)
Vinh Tran444154d2022-08-16 13:10:31 -0400160var _ Bp2buildAidlLibrary = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -0800161
Patrice Arruda8958a942019-03-12 10:06:00 -0700162// filegroup contains a list of files that are referenced by other modules
163// properties (such as "srcs") using the syntax ":<name>". filegroup are
164// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700165func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -0800166 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -0700167 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700168 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500169 InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700170 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800171}
172
Liz Kammer5edc1412022-05-25 11:12:44 -0400173var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
174
175func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
176 ins := make([]string, 0, len(fg.srcs))
177 outs := make([]string, 0, len(fg.srcs))
178 for _, p := range fg.srcs {
179 ins = append(ins, p.String())
180 outs = append(outs, p.Rel())
181 }
182 return []blueprint.JSONAction{
183 blueprint.JSONAction{
184 Inputs: ins,
185 Outputs: outs,
186 },
187 }
188}
189
Liz Kammer5bde22f2021-04-19 14:04:14 -0400190func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400191 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800192 if fg.properties.Path != nil {
193 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
194 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800195}
196
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700197func (fg *fileGroup) Srcs() Paths {
198 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800199}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700200
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700201func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
202 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
203 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700204 }
205}
Chris Parsonsf874e462022-05-10 13:50:12 -0400206
207func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) {
208 bazelCtx := ctx.Config().BazelContext
209
210 bazelCtx.QueueBazelRequest(
211 fg.GetBazelLabel(ctx, fg),
212 cquery.GetOutputFiles,
213 configKey{Common.String(), CommonOS})
214}
215
216func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool {
217 return true
218}
219
220func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400221 bazelCtx := ctx.Config().BazelContext
222 // This is a short-term solution because we rely on info from Android.bp to handle
223 // a converted module. This will block when we want to remove Android.bp for all
224 // converted modules at some point.
225 // TODO(b/242847534): Implement a long-term solution in which we don't need to rely
226 // on info form Android.bp for modules that are already converted to Bazel
227 relativeRoot := ctx.ModuleDir()
Chris Parsonsf874e462022-05-10 13:50:12 -0400228 if fg.properties.Path != nil {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400229 relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path)
Chris Parsonsf874e462022-05-10 13:50:12 -0400230 }
231
Chris Parsonsf874e462022-05-10 13:50:12 -0400232 filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{Common.String(), CommonOS})
233 if err != nil {
234 ctx.ModuleErrorf(err.Error())
235 return
236 }
237
238 bazelOuts := make(Paths, 0, len(filePaths))
239 for _, p := range filePaths {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400240 bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p))
Chris Parsonsf874e462022-05-10 13:50:12 -0400241 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400242 fg.srcs = bazelOuts
243}
Vinh Tran444154d2022-08-16 13:10:31 -0400244
245func (fg *fileGroup) ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool {
246 if len(fg.properties.Srcs) == 0 || !fg.ShouldConvertWithBp2build(ctx) {
247 return false
248 }
249 for _, src := range fg.properties.Srcs {
250 if !strings.HasSuffix(src, ".aidl") {
251 return false
252 }
253 }
254 return true
255}
256
257func (fg *fileGroup) GetAidlLibraryLabel(ctx BazelConversionPathContext) string {
258 if ctx.OtherModuleDir(fg.module) == ctx.ModuleDir() {
259 return ":" + fg.Name()
260 } else {
261 return fg.GetBazelLabel(ctx, fg)
262 }
263}
Sam Delmerico97bd1272022-08-25 14:45:31 -0400264
265// Given a name in srcs prop, check to see if the name references a filegroup
266// and the filegroup is converted to aidl_library
267func IsConvertedToAidlLibrary(ctx BazelConversionPathContext, name string) bool {
268 if module, ok := ctx.ModuleFromName(name); ok {
269 if IsFilegroup(ctx, module) {
270 if fg, ok := module.(Bp2buildAidlLibrary); ok {
271 return fg.ShouldConvertToAidlLibrary(ctx)
272 }
273 }
274 }
275 return false
276}