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