blob: 674a196cb6ededb885e05e74b9702d46b172c416 [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
27// https://docs.bazel.build/versions/master/be/general.html#filegroup
28type bazelFilegroupAttributes struct {
Liz Kammer356f7d42021-01-26 09:18:53 -050029 Srcs bazel.LabelList
Jingwen Chen32b4ece2021-01-21 03:20:18 -050030}
31
32type bazelFilegroup struct {
33 BazelTargetModuleBase
34 bazelFilegroupAttributes
35}
36
37func BazelFileGroupFactory() Module {
38 module := &bazelFilegroup{}
39 module.AddProperties(&module.bazelFilegroupAttributes)
40 InitBazelTargetModule(module)
41 return module
42}
43
44func (bfg *bazelFilegroup) Name() string {
45 return bfg.BaseModuleName()
46}
47
48func (bfg *bazelFilegroup) GenerateAndroidBuildActions(ctx ModuleContext) {}
49
Jingwen Chena42d6412021-01-26 21:57:27 -050050func FilegroupBp2Build(ctx TopDownMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -050051 fg, ok := ctx.Module().(*fileGroup)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -050052 if !ok || !fg.properties.Bazel_module.Bp2build_available {
Liz Kammer356f7d42021-01-26 09:18:53 -050053 return
Jingwen Chen32b4ece2021-01-21 03:20:18 -050054 }
Jingwen Chen77e8b7b2021-02-05 03:03:24 -050055
Jingwen Chen1fd14692021-02-05 03:01:50 -050056 attrs := &bazelFilegroupAttributes{
Liz Kammer356f7d42021-01-26 09:18:53 -050057 Srcs: BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs),
Jingwen Chen1fd14692021-02-05 03:01:50 -050058 }
59
Jingwen Chenfb4692a2021-02-07 10:05:16 -050060 props := bazel.NewBazelTargetModuleProperties(fg.Name(), "filegroup", "")
Jingwen Chen1fd14692021-02-05 03:01:50 -050061
62 ctx.CreateBazelTargetModule(BazelFileGroupFactory, props, attrs)
Colin Cross068e0fe2016-12-13 15:23:47 -080063}
64
65type fileGroupProperties struct {
66 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -080067 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -080068
Colin Cross27b922f2019-03-04 22:35:41 -080069 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -080070
71 // The base path to the files. May be used by other modules to determine which portion
72 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
73 // the base path is stripped off the path and the remaining path is used as the
74 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -080075 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -070076
77 // Create a make variable with the specified name that contains the list of files in the
78 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -080079 Export_to_make_var *string
Jingwen Chen30f5aaa2020-11-19 05:38:02 -050080
81 // Properties for Bazel migration purposes.
82 bazel.Properties
Colin Cross068e0fe2016-12-13 15:23:47 -080083}
84
85type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070086 ModuleBase
Colin Cross068e0fe2016-12-13 15:23:47 -080087 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070088 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -080089}
90
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070091var _ SourceFileProducer = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -080092
Patrice Arruda8958a942019-03-12 10:06:00 -070093// filegroup contains a list of files that are referenced by other modules
94// properties (such as "srcs") using the syntax ":<name>". filegroup are
95// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070096func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -080097 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -070098 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070099 InitAndroidModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700100 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800101}
102
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700103func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -0800104 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800105
106 if fg.properties.Path != nil {
107 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
108 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800109}
110
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700111func (fg *fileGroup) Srcs() Paths {
112 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800113}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700114
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700115func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
116 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
117 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700118 }
119}