Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package genrule |
| 16 | |
| 17 | import ( |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 18 | "android/soong/android" |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 19 | "io" |
| 20 | "strings" |
| 21 | "text/template" |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | func init() { |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 25 | android.RegisterModuleType("filegroup", FileGroupFactory) |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | type fileGroupProperties struct { |
| 29 | // srcs lists files that will be included in this filegroup |
| 30 | Srcs []string |
| 31 | |
| 32 | Exclude_srcs []string |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 33 | |
| 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 Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 38 | Path *string |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 39 | |
| 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 Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 42 | Export_to_make_var *string |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | type fileGroup struct { |
| 46 | android.ModuleBase |
| 47 | properties fileGroupProperties |
| 48 | srcs android.Paths |
| 49 | } |
| 50 | |
| 51 | var _ 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 Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 56 | func FileGroupFactory() android.Module { |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 57 | module := &fileGroup{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 58 | module.AddProperties(&module.properties) |
| 59 | android.InitAndroidModule(module) |
| 60 | return module |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | func (fg *fileGroup) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 64 | android.ExtractSourcesDeps(ctx, fg.properties.Srcs) |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 65 | android.ExtractSourcesDeps(ctx, fg.properties.Exclude_srcs) |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | func (fg *fileGroup) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 69 | fg.srcs = ctx.ExpandSourcesSubDir(fg.properties.Srcs, fg.properties.Exclude_srcs, String(fg.properties.Path)) |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | func (fg *fileGroup) Srcs() android.Paths { |
| 73 | return fg.srcs |
| 74 | } |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 75 | |
| 76 | var androidMkTemplate = template.Must(template.New("filegroup").Parse(` |
| 77 | ifdef {{.makeVar}} |
| 78 | $(error variable {{.makeVar}} set by soong module is already set in make) |
| 79 | endif |
| 80 | {{.makeVar}} := {{.value}} |
| 81 | .KATI_READONLY := {{.makeVar}} |
| 82 | `)) |
| 83 | |
| 84 | func (fg *fileGroup) AndroidMk() android.AndroidMkData { |
| 85 | return android.AndroidMkData{ |
| 86 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 87 | if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" { |
Colin Cross | d91d7ac | 2017-09-12 22:52:12 -0700 | [diff] [blame] | 88 | androidMkTemplate.Execute(w, map[string]string{ |
| 89 | "makeVar": makeVar, |
| 90 | "value": strings.Join(fg.srcs.Strings(), " "), |
| 91 | }) |
| 92 | } |
| 93 | }, |
| 94 | } |
| 95 | } |