blob: a79374d1f046436f425e4a0c1ec57f1b2306b286 [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 Chen5146ac02021-09-02 11:44:42 +000045
46 // For Bazel compatibility, don't generate the filegroup if there is only 1
47 // source file, and that the source file is named the same as the module
48 // itself. In Bazel, eponymous filegroups like this would be an error.
49 //
50 // Instead, dependents on this single-file filegroup can just depend
51 // on the file target, instead of rule target, directly.
52 //
53 // You may ask: what if a filegroup has multiple files, and one of them
54 // shares the name? The answer: we haven't seen that in the wild, and
55 // should lock Soong itself down to prevent the behavior. For now,
56 // we raise an error if bp2build sees this problem.
57 for _, f := range srcs.Value.Includes {
58 if f.Label == fg.Name() {
59 if len(srcs.Value.Includes) > 1 {
60 ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name())
61 }
62 return
63 }
64 }
65
Jingwen Chen1fd14692021-02-05 03:01:50 -050066 attrs := &bazelFilegroupAttributes{
Jingwen Chen07027912021-03-15 06:02:43 -040067 Srcs: srcs,
Jingwen Chen1fd14692021-02-05 03:01:50 -050068 }
69
Jingwen Chen14a8bda2021-06-02 11:10:02 +000070 props := bazel.BazelTargetModuleProperties{
71 Rule_class: "filegroup",
72 Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
73 }
Jingwen Chen1fd14692021-02-05 03:01:50 -050074
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000075 ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
Colin Cross068e0fe2016-12-13 15:23:47 -080076}
77
78type fileGroupProperties struct {
79 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -080080 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -080081
Colin Cross27b922f2019-03-04 22:35:41 -080082 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -080083
84 // The base path to the files. May be used by other modules to determine which portion
85 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
86 // the base path is stripped off the path and the remaining path is used as the
87 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -080088 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -070089
90 // Create a make variable with the specified name that contains the list of files in the
91 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -080092 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -080093}
94
95type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070096 ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -050097 BazelModuleBase
Colin Cross068e0fe2016-12-13 15:23:47 -080098 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070099 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -0800100}
101
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700102var _ SourceFileProducer = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -0800103
Patrice Arruda8958a942019-03-12 10:06:00 -0700104// filegroup contains a list of files that are referenced by other modules
105// properties (such as "srcs") using the syntax ":<name>". filegroup are
106// also be used to export files across package boundaries.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700107func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -0800108 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -0700109 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700110 InitAndroidModule(module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500111 InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700112 return module
Colin Cross068e0fe2016-12-13 15:23:47 -0800113}
114
Jingwen Chen8f222742021-10-07 12:02:23 +0000115func (fg *fileGroup) maybeGenerateBazelBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400116 if !fg.MixedBuildsEnabled(ctx) {
Jingwen Chen8f222742021-10-07 12:02:23 +0000117 return
118 }
119
120 archVariant := ctx.Arch().ArchType
Chris Parsons787fb362021-10-14 18:43:51 -0400121 osVariant := ctx.Os()
Jingwen Chen8f222742021-10-07 12:02:23 +0000122 if len(fg.Srcs()) == 1 && fg.Srcs()[0].Base() == fg.Name() {
123 // This will be a regular file target, not filegroup, in Bazel.
124 // See FilegroupBp2Build for more information.
125 archVariant = Common
Chris Parsons787fb362021-10-14 18:43:51 -0400126 osVariant = CommonOS
Liz Kammer5bde22f2021-04-19 14:04:14 -0400127 }
Colin Cross2fafa3e2019-03-05 12:39:51 -0800128
Liz Kammer5bde22f2021-04-19 14:04:14 -0400129 bazelCtx := ctx.Config().BazelContext
Chris Parsons787fb362021-10-14 18:43:51 -0400130 filePaths, ok := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{archVariant, osVariant})
Liz Kammer5bde22f2021-04-19 14:04:14 -0400131 if !ok {
Jingwen Chen8f222742021-10-07 12:02:23 +0000132 return
Liz Kammer5bde22f2021-04-19 14:04:14 -0400133 }
134
135 bazelOuts := make(Paths, 0, len(filePaths))
136 for _, p := range filePaths {
137 src := PathForBazelOut(ctx, p)
138 bazelOuts = append(bazelOuts, src)
139 }
140
141 fg.srcs = bazelOuts
Liz Kammer5bde22f2021-04-19 14:04:14 -0400142}
143
144func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Liz Kammer5bde22f2021-04-19 14:04:14 -0400145 fg.srcs = PathsForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)
Colin Cross2fafa3e2019-03-05 12:39:51 -0800146 if fg.properties.Path != nil {
147 fg.srcs = PathsWithModuleSrcSubDir(ctx, fg.srcs, String(fg.properties.Path))
148 }
Jingwen Chen8f222742021-10-07 12:02:23 +0000149
150 fg.maybeGenerateBazelBuildActions(ctx)
Colin Cross068e0fe2016-12-13 15:23:47 -0800151}
152
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700153func (fg *fileGroup) Srcs() Paths {
154 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800155}
Colin Crossd91d7ac2017-09-12 22:52:12 -0700156
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700157func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
158 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
159 ctx.StrictRaw(makeVar, strings.Join(fg.srcs.Strings(), " "))
Colin Crossd91d7ac2017-09-12 22:52:12 -0700160 }
161}