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 | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame^] | 18 | "fmt" |
| 19 | "strings" |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 20 | |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 22 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 23 | "android/soong/android" |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 26 | func init() { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 27 | android.RegisterModuleType("gensrcs", GenSrcsFactory) |
| 28 | android.RegisterModuleType("genrule", GenRuleFactory) |
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 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 35 | type SourceFileGenerator interface { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 36 | GeneratedSourceFiles() android.Paths |
| 37 | GeneratedHeaderDir() android.Path |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 40 | type HostToolProvider interface { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 41 | HostToolPath() android.OptionalPath |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 42 | } |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 43 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 44 | type generatorProperties struct { |
| 45 | // command to run on one or more input files. Available variables for substitution: |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame^] | 46 | // $(location): the path to the first entry in tools or tool_files |
| 47 | // $(location <label>): the path to the tool or tool_file with name <label> |
| 48 | // $(in): one or more input files |
| 49 | // $(out): a single output file |
| 50 | // $(genDir): the sandbox directory for this tool; contains $(out) |
| 51 | // $$: a literal $ |
| 52 | // |
| 53 | // DO NOT directly reference paths to files in the source tree, or the |
| 54 | // command will be missing proper dependencies to re-run if the files |
| 55 | // change. |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 56 | Cmd string |
| 57 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame^] | 58 | // name of the modules (if any) that produces the host executable. Leave empty for |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 59 | // prebuilts or scripts that do not need a module to build them. |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame^] | 60 | Tools []string |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 61 | |
| 62 | // Local file that is used as the tool |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame^] | 63 | Tool_files []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 |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 85 | out android.WritablePaths |
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 | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 96 | func (g *generator) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 97 | if g, ok := ctx.Module().(*generator); ok { |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame^] | 98 | if len(g.properties.Tools) > 0 { |
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 | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame^] | 101 | }, nil, g.properties.Tools...) |
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) { |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame^] | 107 | if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 { |
| 108 | ctx.ModuleErrorf("at least one `tools` or `tool_files` is required") |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 109 | return |
| 110 | } |
| 111 | |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 112 | g.genPath = android.PathForModuleGen(ctx, "") |
| 113 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame^] | 114 | tools := map[string]android.Path{} |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 115 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame^] | 116 | if len(g.properties.Tools) > 0 { |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 117 | ctx.VisitDirectDeps(func(module blueprint.Module) { |
| 118 | if t, ok := module.(HostToolProvider); ok { |
| 119 | p := t.HostToolPath() |
| 120 | if p.Valid() { |
| 121 | g.deps = append(g.deps, p.Path()) |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame^] | 122 | tool := ctx.OtherModuleName(module) |
| 123 | if _, exists := tools[tool]; !exists { |
| 124 | tools[tool] = p.Path() |
| 125 | } else { |
| 126 | ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], p.Path().String()) |
| 127 | } |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 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 | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame^] | 137 | for _, tool := range g.properties.Tool_files { |
| 138 | toolPath := android.PathForModuleSrc(ctx, tool) |
| 139 | g.deps = append(g.deps, toolPath) |
| 140 | if _, exists := tools[tool]; !exists { |
| 141 | tools[tool] = toolPath |
| 142 | } else { |
| 143 | ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], toolPath.String()) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | cmd, err := android.Expand(g.properties.Cmd, func(name string) (string, error) { |
| 148 | switch name { |
| 149 | case "location": |
| 150 | if len(g.properties.Tools) > 0 { |
| 151 | return tools[g.properties.Tools[0]].String(), nil |
| 152 | } else { |
| 153 | return tools[g.properties.Tool_files[0]].String(), nil |
| 154 | } |
| 155 | case "in": |
| 156 | return "${in}", nil |
| 157 | case "out": |
| 158 | return "${out}", nil |
| 159 | case "genDir": |
| 160 | return g.genPath.String(), nil |
| 161 | default: |
| 162 | if strings.HasPrefix(name, "location ") { |
| 163 | label := strings.TrimSpace(strings.TrimPrefix(name, "location ")) |
| 164 | if tool, ok := tools[label]; ok { |
| 165 | return tool.String(), nil |
| 166 | } else { |
| 167 | return "", fmt.Errorf("unknown location label %q", label) |
| 168 | } |
| 169 | } |
| 170 | return "", fmt.Errorf("unknown variable '$(%s)'", name) |
| 171 | } |
| 172 | }) |
| 173 | |
| 174 | if err != nil { |
| 175 | ctx.PropertyErrorf("cmd", "%s", err.Error()) |
| 176 | } |
| 177 | |
| 178 | g.rule = ctx.Rule(pctx, "generator", blueprint.RuleParams{ |
| 179 | Command: cmd, |
| 180 | }) |
| 181 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 182 | for _, task := range g.tasks(ctx) { |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame^] | 183 | g.generateSourceFile(ctx, task) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame^] | 187 | func (g *generator) generateSourceFile(ctx android.ModuleContext, task generateTask) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 188 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 189 | Rule: g.rule, |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 190 | Outputs: task.out, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 191 | Inputs: task.in, |
| 192 | Implicits: g.deps, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 193 | }) |
| 194 | |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 195 | for _, outputFile := range task.out { |
| 196 | g.outputFiles = append(g.outputFiles, outputFile) |
| 197 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | func generatorFactory(tasks taskFunc, props ...interface{}) (blueprint.Module, []interface{}) { |
| 201 | module := &generator{ |
| 202 | tasks: tasks, |
| 203 | } |
| 204 | |
| 205 | props = append(props, &module.properties) |
| 206 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 207 | return android.InitAndroidModule(module, props...) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | func GenSrcsFactory() (blueprint.Module, []interface{}) { |
| 211 | properties := &genSrcsProperties{} |
| 212 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 213 | tasks := func(ctx android.ModuleContext) []generateTask { |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 214 | srcFiles := ctx.ExpandSources(properties.Srcs, nil) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 215 | tasks := make([]generateTask, 0, len(srcFiles)) |
| 216 | for _, in := range srcFiles { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 217 | tasks = append(tasks, generateTask{ |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 218 | in: android.Paths{in}, |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 219 | out: android.WritablePaths{android.GenPathWithExt(ctx, "", in, properties.Output_extension)}, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 220 | }) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 221 | } |
| 222 | return tasks |
| 223 | } |
| 224 | |
| 225 | return generatorFactory(tasks, properties) |
| 226 | } |
| 227 | |
| 228 | type genSrcsProperties struct { |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 229 | // list of input files |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 230 | Srcs []string |
| 231 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 232 | // extension that will be substituted for each output file |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 233 | Output_extension string |
| 234 | } |
| 235 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 236 | func GenRuleFactory() (blueprint.Module, []interface{}) { |
| 237 | properties := &genRuleProperties{} |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 238 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 239 | tasks := func(ctx android.ModuleContext) []generateTask { |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 240 | outs := make(android.WritablePaths, len(properties.Out)) |
| 241 | for i, out := range properties.Out { |
| 242 | outs[i] = android.PathForModuleGen(ctx, out) |
| 243 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 244 | return []generateTask{ |
| 245 | { |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 246 | in: ctx.ExpandSources(properties.Srcs, nil), |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 247 | out: outs, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 248 | }, |
| 249 | } |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 250 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 251 | |
| 252 | return generatorFactory(tasks, properties) |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 255 | type genRuleProperties struct { |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 256 | // list of input files |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 257 | Srcs []string |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 258 | |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 259 | // names of the output files that will be generated |
| 260 | Out []string |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 261 | } |