blob: ed206b036831d573b485fd869ea4e74ff13f535a [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.
38 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.
42 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)
65}
66
67func (fg *fileGroup) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -080068 fg.srcs = ctx.ExpandSourcesSubDir(fg.properties.Srcs, fg.properties.Exclude_srcs, fg.properties.Path)
Colin Cross068e0fe2016-12-13 15:23:47 -080069}
70
71func (fg *fileGroup) Srcs() android.Paths {
72 return fg.srcs
73}
Colin Crossd91d7ac2017-09-12 22:52:12 -070074
75var androidMkTemplate = template.Must(template.New("filegroup").Parse(`
76ifdef {{.makeVar}}
77 $(error variable {{.makeVar}} set by soong module is already set in make)
78endif
79{{.makeVar}} := {{.value}}
80.KATI_READONLY := {{.makeVar}}
81`))
82
83func (fg *fileGroup) AndroidMk() android.AndroidMkData {
84 return android.AndroidMkData{
85 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
86 if makeVar := fg.properties.Export_to_make_var; makeVar != "" {
87 androidMkTemplate.Execute(w, map[string]string{
88 "makeVar": makeVar,
89 "value": strings.Join(fg.srcs.Strings(), " "),
90 })
91 }
92 },
93 }
94}