blob: 54d01d39f854e7af129df6458aa460808fd82e31 [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 (
Colin Crossd91d7ac2017-09-12 22:52:12 -070018 "strings"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000019
20 "android/soong/bazel"
Colin Cross068e0fe2016-12-13 15:23:47 -080021)
22
23func init() {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070024 RegisterModuleType("filegroup", FileGroupFactory)
Jingwen Chena42d6412021-01-26 21:57:27 -050025 RegisterBp2BuildMutator("filegroup", FilegroupBp2Build)
Jingwen Chen32b4ece2021-01-21 03:20:18 -050026}
27
Paul Duffin35816122021-02-24 01:49:52 +000028var PrepareForTestWithFilegroup = FixtureRegisterWithContext(func(ctx RegistrationContext) {
29 ctx.RegisterModuleType("filegroup", FileGroupFactory)
30})
31
Jingwen Chen32b4ece2021-01-21 03:20:18 -050032// https://docs.bazel.build/versions/master/be/general.html#filegroup
33type bazelFilegroupAttributes struct {
Jingwen Chen07027912021-03-15 06:02:43 -040034 Srcs bazel.LabelListAttribute
Jingwen Chen32b4ece2021-01-21 03:20:18 -050035}
36
Jingwen Chena42d6412021-01-26 21:57:27 -050037func FilegroupBp2Build(ctx TopDownMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -050038 fg, ok := ctx.Module().(*fileGroup)
Jingwen Chen12b4c272021-03-10 02:05:59 -050039 if !ok || !fg.ConvertWithBp2build(ctx) {
Liz Kammer356f7d42021-01-26 09:18:53 -050040 return
Jingwen Chen32b4ece2021-01-21 03:20:18 -050041 }
Jingwen Chen77e8b7b2021-02-05 03:03:24 -050042
Jingwen Chen07027912021-03-15 06:02:43 -040043 srcs := bazel.MakeLabelListAttribute(
44 BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs))
Jingwen Chen1fd14692021-02-05 03:01:50 -050045 attrs := &bazelFilegroupAttributes{
Jingwen Chen07027912021-03-15 06:02:43 -040046 Srcs: srcs,
Jingwen Chen1fd14692021-02-05 03:01:50 -050047 }
48
Jingwen Chen14a8bda2021-06-02 11:10:02 +000049 props := bazel.BazelTargetModuleProperties{
50 Rule_class: "filegroup",
51 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
52 }
Jingwen Chen1fd14692021-02-05 03:01:50 -050053
Liz Kammer2ada09a2021-08-11 00:17:36 -040054 ctx.CreateBazelTargetModule(fg.Name(), props, attrs)
Colin Cross068e0fe2016-12-13 15:23:47 -080055}
56
57type fileGroupProperties struct {
58 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -080059 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -080060
Colin Cross27b922f2019-03-04 22:35:41 -080061 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -080062
63 // The base path to the files. May be used by other modules to determine which portion
64 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
65 // the base path is stripped off the path and the remaining path is used as the
66 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -080067 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -070068
69 // Create a make variable with the specified name that contains the list of files in the
70 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -080071 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -080072}
73
74type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070075 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -050076 BazelModuleBase
Colin Cross068e0fe2016-12-13 15:23:47 -080077 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070078 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -080079}
80
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070081var _ SourceFileProducer = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -080082
Patrice Arruda8958a942019-03-12 10:06:00 -070083// filegroup contains a list of files that are referenced by other modules
84// properties (such as "srcs") using the syntax ":<name>". filegroup are
85// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070086func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -080087 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -070088 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070089 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -050090 InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -070091 return module
Colin Cross068e0fe2016-12-13 15:23:47 -080092}
93
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000094func (fg *fileGroup) GenerateBazelBuildActions(ctx ModuleContext) bool {
Liz Kammer5bde22f2021-04-19 14:04:14 -040095 if !fg.MixedBuildsEnabled(ctx) {
96 return false
97 }
Colin Cross2fafa3e2019-03-05 12:39:51 -080098
Liz Kammer5bde22f2021-04-19 14:04:14 -040099 bazelCtx := ctx.Config().BazelContext
100 filePaths, ok := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), ctx.Arch().ArchType)
101 if !ok {
102 return false
103 }
104
105 bazelOuts := make(Paths, 0, len(filePaths))
106 for _, p := range filePaths {
107 src := PathForBazelOut(ctx, p)
108 bazelOuts = append(bazelOuts, src)
109 }
110
111 fg.srcs = bazelOuts
112
113 return true
114}
115
116func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +0000117 if fg.GenerateBazelBuildActions(ctx) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400118 return
119 }
120
121 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800122 if fg.properties.Path != nil {
123 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
124 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800125}
126
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700127func (fg *fileGroup) Srcs() Paths {
128 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800129}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700130
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700131func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
132 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
133 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700134 }
135}