blob: 9f4aa10d3deef0998dee0e2c95f6ced010bfdd25 [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
15package genrule
16
17import (
Colin Cross068e0fe2016-12-13 15:23:47 -080018 "android/soong/android"
Colin Crossd91d7ac2017-09-12 22:52:12 -070019 "io"
20 "strings"
21 "text/template"
Colin Cross068e0fe2016-12-13 15:23:47 -080022)
23
24func init() {
Colin Cross795c3772017-03-16 16:50:10 -070025 android.RegisterModuleType("filegroup", FileGroupFactory)
Colin Cross068e0fe2016-12-13 15:23:47 -080026}
27
28type fileGroupProperties struct {
29 // srcs lists files that will be included in this filegroup
30 Srcs []string
31
32 Exclude_srcs []string
Colin Crossfaeb7aa2017-02-01 14:12:44 -080033
34 // The base path to the files. May be used by other modules to determine which portion
35 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
36 // the base path is stripped off the path and the remaining path is used as the
37 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -080038 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -070039
40 // Create a make variable with the specified name that contains the list of files in the
41 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -080042 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -080043}
44
45type fileGroup struct {
46 android.ModuleBase
47 properties fileGroupProperties
48 srcs android.Paths
49}
50
51var _ android.SourceFileProducer = (*fileGroup)(nil)
52
53// filegroup modules contain a list of files, and can be used to export files across package
54// boundaries. filegroups (and genrules) can be referenced from srcs properties of other modules
55// using the syntax ":module".
Colin Cross36242852017-06-23 15:06:31 -070056func FileGroupFactory() android.Module {
Colin Cross068e0fe2016-12-13 15:23:47 -080057 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -070058 module.AddProperties(&module.properties)
59 android.InitAndroidModule(module)
60 return module
Colin Cross068e0fe2016-12-13 15:23:47 -080061}
62
63func (fg *fileGroup) DepsMutator(ctx android.BottomUpMutatorContext) {
64 android.ExtractSourcesDeps(ctx, fg.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +000065 android.ExtractSourcesDeps(ctx, fg.properties.Exclude_srcs)
Colin Cross068e0fe2016-12-13 15:23:47 -080066}
67
68func (fg *fileGroup) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhangea568a42017-11-08 21:20:04 -080069 fg.srcs = ctx.ExpandSourcesSubDir(fg.properties.Srcs, fg.properties.Exclude_srcs, String(fg.properties.Path))
Colin Cross068e0fe2016-12-13 15:23:47 -080070}
71
72func (fg *fileGroup) Srcs() android.Paths {
73 return fg.srcs
74}
Colin Crossd91d7ac2017-09-12 22:52:12 -070075
76var androidMkTemplate = template.Must(template.New("filegroup").Parse(`
77ifdef {{.makeVar}}
78 $(error variable {{.makeVar}} set by soong module is already set in make)
79endif
80{{.makeVar}} := {{.value}}
81.KATI_READONLY := {{.makeVar}}
82`))
83
84func (fg *fileGroup) AndroidMk() android.AndroidMkData {
85 return android.AndroidMkData{
86 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Nan Zhangea568a42017-11-08 21:20:04 -080087 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
Colin Crossd91d7ac2017-09-12 22:52:12 -070088 androidMkTemplate.Execute(w, map[string]string{
89 "makeVar": makeVar,
90 "value": strings.Join(fg.srcs.Strings(), " "),
91 })
92 }
93 },
94 }
95}