blob: 76af804bf8c0ffcaa4f7a58114cf9d9c565879a8 [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 "io"
19 "strings"
20 "text/template"
Colin Cross068e0fe2016-12-13 15:23:47 -080021)
22
23func init() {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070024 RegisterModuleType("filegroup", FileGroupFactory)
Colin Cross068e0fe2016-12-13 15:23:47 -080025}
26
27type fileGroupProperties struct {
28 // srcs lists files that will be included in this filegroup
Colin Cross27b922f2019-03-04 22:35:41 -080029 Srcs []string `android:"path"`
Colin Cross068e0fe2016-12-13 15:23:47 -080030
Colin Cross27b922f2019-03-04 22:35:41 -080031 Exclude_srcs []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -080032
33 // The base path to the files. May be used by other modules to determine which portion
34 // of the path to use. For example, when a filegroup is used as data in a cc_test rule,
35 // the base path is stripped off the path and the remaining path is used as the
36 // installation directory.
Nan Zhangea568a42017-11-08 21:20:04 -080037 Path *string
Colin Crossd91d7ac2017-09-12 22:52:12 -070038
39 // Create a make variable with the specified name that contains the list of files in the
40 // filegroup, relative to the root of the source tree.
Nan Zhangea568a42017-11-08 21:20:04 -080041 Export_to_make_var *string
Colin Cross068e0fe2016-12-13 15:23:47 -080042}
43
44type fileGroup struct {
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070045 ModuleBase
Colin Cross068e0fe2016-12-13 15:23:47 -080046 properties fileGroupProperties
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070047 srcs Paths
Colin Cross068e0fe2016-12-13 15:23:47 -080048}
49
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070050var _ SourceFileProducer = (*fileGroup)(nil)
Colin Cross068e0fe2016-12-13 15:23:47 -080051
52// filegroup modules contain a list of files, and can be used to export files across package
53// boundaries. filegroups (and genrules) can be referenced from srcs properties of other modules
54// using the syntax ":module".
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070055func FileGroupFactory() Module {
Colin Cross068e0fe2016-12-13 15:23:47 -080056 module := &fileGroup{}
Colin Cross36242852017-06-23 15:06:31 -070057 module.AddProperties(&module.properties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070058 InitAndroidModule(module)
Colin Cross36242852017-06-23 15:06:31 -070059 return module
Colin Cross068e0fe2016-12-13 15:23:47 -080060}
61
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070062func (fg *fileGroup) GenerateAndroidBuildActions(ctx ModuleContext) {
Nan Zhangea568a42017-11-08 21:20:04 -080063 fg.srcs = ctx.ExpandSourcesSubDir(fg.properties.Srcs, fg.properties.Exclude_srcs, String(fg.properties.Path))
Colin Cross068e0fe2016-12-13 15:23:47 -080064}
65
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070066func (fg *fileGroup) Srcs() Paths {
67 return append(Paths{}, fg.srcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -080068}
Colin Crossd91d7ac2017-09-12 22:52:12 -070069
70var androidMkTemplate = template.Must(template.New("filegroup").Parse(`
71ifdef {{.makeVar}}
72 $(error variable {{.makeVar}} set by soong module is already set in make)
73endif
74{{.makeVar}} := {{.value}}
75.KATI_READONLY := {{.makeVar}}
76`))
77
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070078func (fg *fileGroup) AndroidMk() AndroidMkData {
79 return AndroidMkData{
80 Custom: func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) {
Nan Zhangea568a42017-11-08 21:20:04 -080081 if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
Colin Crossd91d7ac2017-09-12 22:52:12 -070082 androidMkTemplate.Execute(w, map[string]string{
83 "makeVar": makeVar,
84 "value": strings.Join(fg.srcs.Strings(), " "),
85 })
86 }
87 },
88 }
89}