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