blob: e2074127ec6eac518cb919649eaaccf7b4ae2f9c [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 (
Jingwen Chen30f5aaa2020-11-19 05:38:02 -050018 "android/soong/bazel"
Colin Crossd91d7ac2017-09-12 22:52:12 -070019 "strings"
Colin Cross068e0fe2016-12-13 15:23:47 -080020)
21
22func init() {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070023 RegisterModuleType("filegroup", FileGroupFactory)
Jingwen Chena42d6412021-01-26 21:57:27 -050024 RegisterBp2BuildMutator("filegroup", FilegroupBp2Build)
Jingwen Chen32b4ece2021-01-21 03:20:18 -050025}
26
Paul Duffin35816122021-02-24 01:49:52 +000027var PrepareForTestWithFilegroup = FixtureRegisterWithContext(func(ctx RegistrationContext) {
28 ctx.RegisterModuleType("filegroup", FileGroupFactory)
29})
30
Jingwen Chen32b4ece2021-01-21 03:20:18 -050031// https://docs.bazel.build/versions/master/be/general.html#filegroup
32type bazelFilegroupAttributes struct {
Jingwen Chen07027912021-03-15 06:02:43 -040033 Srcs bazel.LabelListAttribute
Jingwen Chen32b4ece2021-01-21 03:20:18 -050034}
35
36type bazelFilegroup struct {
37 BazelTargetModuleBase
38 bazelFilegroupAttributes
39}
40
41func BazelFileGroupFactory() Module {
42 module := &bazelFilegroup{}
43 module.AddProperties(&module.bazelFilegroupAttributes)
44 InitBazelTargetModule(module)
45 return module
46}
47
48func (bfg *bazelFilegroup) Name() string {
49 return bfg.BaseModuleName()
50}
51
52func (bfg *bazelFilegroup) GenerateAndroidBuildActions(ctx ModuleContext) {}
53
Jingwen Chena42d6412021-01-26 21:57:27 -050054func FilegroupBp2Build(ctx TopDownMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -050055 fg, ok := ctx.Module().(*fileGroup)
Jingwen Chen12b4c272021-03-10 02:05:59 -050056 if !ok || !fg.ConvertWithBp2build(ctx) {
Liz Kammer356f7d42021-01-26 09:18:53 -050057 return
Jingwen Chen32b4ece2021-01-21 03:20:18 -050058 }
Jingwen Chen77e8b7b2021-02-05 03:03:24 -050059
Jingwen Chen07027912021-03-15 06:02:43 -040060 srcs := bazel.MakeLabelListAttribute(
61 BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs))
Jingwen Chen1fd14692021-02-05 03:01:50 -050062 attrs := &bazelFilegroupAttributes{
Jingwen Chen07027912021-03-15 06:02:43 -040063 Srcs: srcs,
Jingwen Chen1fd14692021-02-05 03:01:50 -050064 }
65
Jingwen Chen14a8bda2021-06-02 11:10:02 +000066 props := bazel.BazelTargetModuleProperties{
67 Rule_class: "filegroup",
68 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
69 }
Jingwen Chen1fd14692021-02-05 03:01:50 -050070
Liz Kammerfc46bc12021-02-19 11:06:17 -050071 ctx.CreateBazelTargetModule(BazelFileGroupFactory, fg.Name(), props, attrs)
Colin Cross068e0fe2016-12-13 15:23:47 -080072}
73
74type fileGroupProperties struct {
75 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -080076 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -080077
Colin Cross27b922f2019-03-04 22:35:41 -080078 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -080079
80 // The base path to the files. May be used by other modules to determine which portion
81 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
82 // the base path is stripped off the path and the remaining path is used as the
83 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -080084 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -070085
86 // Create a make variable with the specified name that contains the list of files in the
87 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -080088 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -080089}
90
91type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070092 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -050093 BazelModuleBase
Colin Cross068e0fe2016-12-13 15:23:47 -080094 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070095 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -080096}
97
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070098var _ SourceFileProducer = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -080099
Patrice Arruda8958a942019-03-12 10:06:00 -0700100// filegroup contains a list of files that are referenced by other modules
101// properties (such as "srcs") using the syntax ":<name>". filegroup are
102// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700103func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -0800104 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -0700105 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700106 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500107 InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700108 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800109}
110
Liz Kammer5bde22f2021-04-19 14:04:14 -0400111func (fg *fileGroup) generateBazelBuildActions(ctx ModuleContext) bool {
112 if !fg.MixedBuildsEnabled(ctx) {
113 return false
114 }
Colin Cross2fafa3e2019-03-05 12:39:51 -0800115
Liz Kammer5bde22f2021-04-19 14:04:14 -0400116 bazelCtx := ctx.Config().BazelContext
117 filePaths, ok := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), ctx.Arch().ArchType)
118 if !ok {
119 return false
120 }
121
122 bazelOuts := make(Paths, 0, len(filePaths))
123 for _, p := range filePaths {
124 src := PathForBazelOut(ctx, p)
125 bazelOuts = append(bazelOuts, src)
126 }
127
128 fg.srcs = bazelOuts
129
130 return true
131}
132
133func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
134 if fg.generateBazelBuildActions(ctx) {
135 return
136 }
137
138 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}