Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 18 | "github.com/google/blueprint" |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 19 | |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 20 | "android/soong" |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 21 | "android/soong/common" |
| 22 | ) |
| 23 | |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 24 | func init() { |
| 25 | soong.RegisterModuleType("gensrcs", GenSrcsFactory) |
| 26 | soong.RegisterModuleType("genrule", GenRuleFactory) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 27 | |
| 28 | common.RegisterBottomUpMutator("genrule_deps", genruleDepsMutator) |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 31 | var ( |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 32 | pctx = common.NewPackageContext("android/soong/genrule") |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | func init() { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 36 | pctx.SourcePathVariable("srcDir", "") |
| 37 | pctx.HostBinToolVariable("hostBin", "") |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | type SourceFileGenerator interface { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 41 | GeneratedSourceFiles() common.Paths |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 44 | type HostToolProvider interface { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 45 | HostToolPath() common.OptionalPath |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 46 | } |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 47 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 48 | type generatorProperties struct { |
| 49 | // command to run on one or more input files. Available variables for substitution: |
| 50 | // $in: one or more input files |
| 51 | // $out: a single output file |
| 52 | // $srcDir: the root directory of the source tree |
| 53 | // The host bin directory will be in the path |
| 54 | Cmd string |
| 55 | |
| 56 | // name of the module (if any) that produces the host executable. Leave empty for |
| 57 | // prebuilts or scripts that do not need a module to build them. |
| 58 | Tool string |
| 59 | } |
| 60 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 61 | type generator struct { |
| 62 | common.AndroidModuleBase |
| 63 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 64 | properties generatorProperties |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 65 | |
| 66 | tasks taskFunc |
| 67 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 68 | deps common.Paths |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 69 | rule blueprint.Rule |
| 70 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 71 | outputFiles common.Paths |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | type taskFunc func(ctx common.AndroidModuleContext) []generateTask |
| 75 | |
| 76 | type generateTask struct { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 77 | in common.Paths |
| 78 | out common.ModuleGenPath |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 81 | func (g *generator) GeneratedSourceFiles() common.Paths { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 82 | return g.outputFiles |
| 83 | } |
| 84 | |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 85 | func genruleDepsMutator(ctx common.AndroidBottomUpMutatorContext) { |
| 86 | if g, ok := ctx.Module().(*generator); ok { |
| 87 | if g.properties.Tool != "" { |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 88 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
Colin Cross | 1604ecf | 2015-12-17 16:33:43 -0800 | [diff] [blame] | 89 | {"host_or_device", common.Host.String()}, |
| 90 | {"host_type", common.CurrentHostType().String()}, |
Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame^] | 91 | }, nil, g.properties.Tool) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 92 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 93 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | func (g *generator) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) { |
| 97 | g.rule = ctx.Rule(pctx, "generator", blueprint.RuleParams{ |
| 98 | Command: "PATH=$$PATH:$hostBin " + g.properties.Cmd, |
| 99 | }) |
| 100 | |
| 101 | ctx.VisitDirectDeps(func(module blueprint.Module) { |
| 102 | if t, ok := module.(HostToolProvider); ok { |
| 103 | p := t.HostToolPath() |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 104 | if p.Valid() { |
| 105 | g.deps = append(g.deps, p.Path()) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 106 | } else { |
| 107 | ctx.ModuleErrorf("host tool %q missing output file", ctx.OtherModuleName(module)) |
| 108 | } |
| 109 | } else { |
| 110 | ctx.ModuleErrorf("unknown dependency %q", ctx.OtherModuleName(module)) |
| 111 | } |
| 112 | }) |
| 113 | |
| 114 | for _, task := range g.tasks(ctx) { |
| 115 | g.generateSourceFile(ctx, task) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func (g *generator) generateSourceFile(ctx common.AndroidModuleContext, task generateTask) { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 120 | ctx.ModuleBuild(pctx, common.ModuleBuildParams{ |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 121 | Rule: g.rule, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 122 | Output: task.out, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 123 | Inputs: task.in, |
| 124 | Implicits: g.deps, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 125 | }) |
| 126 | |
| 127 | g.outputFiles = append(g.outputFiles, task.out) |
| 128 | } |
| 129 | |
| 130 | func generatorFactory(tasks taskFunc, props ...interface{}) (blueprint.Module, []interface{}) { |
| 131 | module := &generator{ |
| 132 | tasks: tasks, |
| 133 | } |
| 134 | |
| 135 | props = append(props, &module.properties) |
| 136 | |
| 137 | return common.InitAndroidModule(module, props...) |
| 138 | } |
| 139 | |
| 140 | func GenSrcsFactory() (blueprint.Module, []interface{}) { |
| 141 | properties := &genSrcsProperties{} |
| 142 | |
| 143 | tasks := func(ctx common.AndroidModuleContext) []generateTask { |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 144 | srcFiles := ctx.ExpandSources(properties.Srcs, nil) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 145 | tasks := make([]generateTask, 0, len(srcFiles)) |
| 146 | for _, in := range srcFiles { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 147 | tasks = append(tasks, generateTask{ |
| 148 | in: common.Paths{in}, |
| 149 | out: common.GenPathWithExt(ctx, in, properties.Output_extension), |
| 150 | }) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 151 | } |
| 152 | return tasks |
| 153 | } |
| 154 | |
| 155 | return generatorFactory(tasks, properties) |
| 156 | } |
| 157 | |
| 158 | type genSrcsProperties struct { |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 159 | // list of input files |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 160 | Srcs []string |
| 161 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 162 | // extension that will be substituted for each output file |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 163 | Output_extension string |
| 164 | } |
| 165 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 166 | func GenRuleFactory() (blueprint.Module, []interface{}) { |
| 167 | properties := &genRuleProperties{} |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 168 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 169 | tasks := func(ctx common.AndroidModuleContext) []generateTask { |
| 170 | return []generateTask{ |
| 171 | { |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 172 | in: ctx.ExpandSources(properties.Srcs, nil), |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 173 | out: properties.Out, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 174 | }, |
| 175 | } |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 176 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 177 | |
| 178 | return generatorFactory(tasks, properties) |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 181 | type genRuleProperties struct { |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 182 | // list of input files |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 183 | Srcs []string |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 184 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 185 | // name of the output file that will be generated |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 186 | Out common.ModuleGenPath |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 187 | } |