blob: fd4a2fe49f2572614239600bfaeefd9d5ba68db0 [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"
Jingwen Chen32b4ece2021-01-21 03:20:18 -050020
21 "github.com/google/blueprint/proptools"
Colin Cross068e0fe2016-12-13 15:23:47 -080022)
23
24func init() {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070025 RegisterModuleType("filegroup", FileGroupFactory)
Jingwen Chen32b4ece2021-01-21 03:20:18 -050026 RegisterBp2BuildMutator("filegroup", bp2buildMutator)
27}
28
29// https://docs.bazel.build/versions/master/be/general.html#filegroup
30type bazelFilegroupAttributes struct {
31 Name *string
32 Srcs []string
33}
34
35type bazelFilegroup struct {
36 BazelTargetModuleBase
37 bazelFilegroupAttributes
38}
39
40func BazelFileGroupFactory() Module {
41 module := &bazelFilegroup{}
42 module.AddProperties(&module.bazelFilegroupAttributes)
43 InitBazelTargetModule(module)
44 return module
45}
46
47func (bfg *bazelFilegroup) Name() string {
48 return bfg.BaseModuleName()
49}
50
51func (bfg *bazelFilegroup) GenerateAndroidBuildActions(ctx ModuleContext) {}
52
53// TODO: Create helper functions to avoid this boilerplate.
54func bp2buildMutator(ctx TopDownMutatorContext) {
55 if m, ok := ctx.Module().(*fileGroup); ok {
56 name := "__bp2build__" + m.base().BaseModuleName()
57 ctx.CreateModule(BazelFileGroupFactory, &bazelFilegroupAttributes{
58 Name: proptools.StringPtr(name),
59 Srcs: m.properties.Srcs,
60 }, &bazel.BazelTargetModuleProperties{
61 Rule_class: "filegroup",
62 })
63 }
Colin Cross068e0fe2016-12-13 15:23:47 -080064}
65
66type fileGroupProperties struct {
67 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -080068 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -080069
Colin Cross27b922f2019-03-04 22:35:41 -080070 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -080071
72 // The base path to the files. May be used by other modules to determine which portion
73 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
74 // the base path is stripped off the path and the remaining path is used as the
75 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -080076 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -070077
78 // Create a make variable with the specified name that contains the list of files in the
79 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -080080 Export_to_make_var *string
Jingwen Chen30f5aaa2020-11-19 05:38:02 -050081
82 // Properties for Bazel migration purposes.
83 bazel.Properties
Colin Cross068e0fe2016-12-13 15:23:47 -080084}
85
86type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070087 ModuleBase
Colin Cross068e0fe2016-12-13 15:23:47 -080088 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070089 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -080090}
91
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070092var _ SourceFileProducer = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -080093
Patrice Arruda8958a942019-03-12 10:06:00 -070094// filegroup contains a list of files that are referenced by other modules
95// properties (such as "srcs") using the syntax ":<name>". filegroup are
96// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070097func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -080098 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -070099 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700100 InitAndroidModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700101 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800102}
103
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700104func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -0800105 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800106
107 if fg.properties.Path != nil {
108 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
109 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800110}
111
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700112func (fg *fileGroup) Srcs() Paths {
113 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800114}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700115
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700116func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
117 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
118 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700119 }
120}