blob: 504223de9f59993ab5357f1157d4b657359979b9 [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"
Colin Crossd91d7ac2017-09-12 22:52:12 -070019 "strings"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000020
21 "android/soong/bazel"
Chris Parsonsf874e462022-05-10 13:50:12 -040022 "android/soong/bazel/cquery"
Sam Delmericoc7681022022-02-04 21:01:20 +000023
24 "github.com/google/blueprint"
Colin Cross068e0fe2016-12-13 15:23:47 -080025)
26
27func init() {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070028 RegisterModuleType("filegroup", FileGroupFactory)
Jingwen Chen32b4ece2021-01-21 03:20:18 -050029}
30
Paul Duffin35816122021-02-24 01:49:52 +000031var PrepareForTestWithFilegroup = FixtureRegisterWithContext(func(ctx RegistrationContext) {
32 ctx.RegisterModuleType("filegroup", FileGroupFactory)
33})
34
Sam Delmericoc7681022022-02-04 21:01:20 +000035// IsFilegroup checks that a module is a filegroup type
36func IsFilegroup(ctx bazel.OtherModuleContext, m blueprint.Module) bool {
37 return ctx.OtherModuleType(m) == "filegroup"
38}
39
Jingwen Chen32b4ece2021-01-21 03:20:18 -050040// https://docs.bazel.build/versions/master/be/general.html#filegroup
41type bazelFilegroupAttributes struct {
Jingwen Chen07027912021-03-15 06:02:43 -040042 Srcs bazel.LabelListAttribute
Jingwen Chen32b4ece2021-01-21 03:20:18 -050043}
44
Liz Kammerbe46fcc2021-11-01 15:32:43 -040045// ConvertWithBp2build performs bp2build conversion of filegroup
46func (fg *fileGroup) ConvertWithBp2build(ctx TopDownMutatorContext) {
Jingwen Chen07027912021-03-15 06:02:43 -040047 srcs := bazel.MakeLabelListAttribute(
48 BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs))
Jingwen Chen5146ac02021-09-02 11:44:42 +000049
50 // For Bazel compatibility, don't generate the filegroup if there is only 1
51 // source file, and that the source file is named the same as the module
52 // itself. In Bazel, eponymous filegroups like this would be an error.
53 //
54 // Instead, dependents on this single-file filegroup can just depend
55 // on the file target, instead of rule target, directly.
56 //
57 // You may ask: what if a filegroup has multiple files, and one of them
58 // shares the name? The answer: we haven't seen that in the wild, and
59 // should lock Soong itself down to prevent the behavior. For now,
60 // we raise an error if bp2build sees this problem.
61 for _, f := range srcs.Value.Includes {
62 if f.Label == fg.Name() {
63 if len(srcs.Value.Includes) > 1 {
64 ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name())
65 }
66 return
67 }
68 }
69
Jingwen Chen1fd14692021-02-05 03:01:50 -050070 attrs := &bazelFilegroupAttributes{
Jingwen Chen07027912021-03-15 06:02:43 -040071 Srcs: srcs,
Jingwen Chen1fd14692021-02-05 03:01:50 -050072 }
73
Jingwen Chen14a8bda2021-06-02 11:10:02 +000074 props := bazel.BazelTargetModuleProperties{
75 Rule_class: "filegroup",
76 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
77 }
Jingwen Chen1fd14692021-02-05 03:01:50 -050078
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000079 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
Colin Cross068e0fe2016-12-13 15:23:47 -080080}
81
82type fileGroupProperties struct {
83 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -080084 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -080085
Colin Cross27b922f2019-03-04 22:35:41 -080086 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -080087
88 // The base path to the files. May be used by other modules to determine which portion
89 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
90 // the base path is stripped off the path and the remaining path is used as the
91 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -080092 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -070093
94 // Create a make variable with the specified name that contains the list of files in the
95 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -080096 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -080097}
98
99type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700100 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500101 BazelModuleBase
Colin Cross068e0fe2016-12-13 15:23:47 -0800102 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700103 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -0800104}
105
Chris Parsonsf874e462022-05-10 13:50:12 -0400106var _ MixedBuildBuildable = (*fileGroup)(nil)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700107var _ SourceFileProducer = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -0800108
Patrice Arruda8958a942019-03-12 10:06:00 -0700109// filegroup contains a list of files that are referenced by other modules
110// properties (such as "srcs") using the syntax ":<name>". filegroup are
111// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700112func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -0800113 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -0700114 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700115 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500116 InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700117 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800118}
119
Liz Kammer5edc1412022-05-25 11:12:44 -0400120var _ blueprint.JSONActionSupplier = (*fileGroup)(nil)
121
122func (fg *fileGroup) JSONActions() []blueprint.JSONAction {
123 ins := make([]string, 0, len(fg.srcs))
124 outs := make([]string, 0, len(fg.srcs))
125 for _, p := range fg.srcs {
126 ins = append(ins, p.String())
127 outs = append(outs, p.Rel())
128 }
129 return []blueprint.JSONAction{
130 blueprint.JSONAction{
131 Inputs: ins,
132 Outputs: outs,
133 },
134 }
135}
136
Liz Kammer5bde22f2021-04-19 14:04:14 -0400137func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400138 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800139 if fg.properties.Path != nil {
140 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
141 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800142}
143
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700144func (fg *fileGroup) Srcs() Paths {
145 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800146}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700147
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700148func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
149 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
150 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700151 }
152}
Chris Parsonsf874e462022-05-10 13:50:12 -0400153
154func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) {
155 bazelCtx := ctx.Config().BazelContext
156
157 bazelCtx.QueueBazelRequest(
158 fg.GetBazelLabel(ctx, fg),
159 cquery.GetOutputFiles,
160 configKey{Common.String(), CommonOS})
161}
162
163func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool {
164 return true
165}
166
167func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400168 bazelCtx := ctx.Config().BazelContext
169 // This is a short-term solution because we rely on info from Android.bp to handle
170 // a converted module. This will block when we want to remove Android.bp for all
171 // converted modules at some point.
172 // TODO(b/242847534): Implement a long-term solution in which we don't need to rely
173 // on info form Android.bp for modules that are already converted to Bazel
174 relativeRoot := ctx.ModuleDir()
Chris Parsonsf874e462022-05-10 13:50:12 -0400175 if fg.properties.Path != nil {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400176 relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path)
Chris Parsonsf874e462022-05-10 13:50:12 -0400177 }
178
Chris Parsonsf874e462022-05-10 13:50:12 -0400179 filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{Common.String(), CommonOS})
180 if err != nil {
181 ctx.ModuleErrorf(err.Error())
182 return
183 }
184
185 bazelOuts := make(Paths, 0, len(filePaths))
186 for _, p := range filePaths {
Vinh Tran16fe8e12022-08-16 16:45:44 -0400187 bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p))
Chris Parsonsf874e462022-05-10 13:50:12 -0400188 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400189 fg.srcs = bazelOuts
190}