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 |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame^] | 50 | // $(deps): a file to which dependencies will be written, if the depfile property is set to true |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 51 | // $(genDir): the sandbox directory for this tool; contains $(out) |
| 52 | // $$: a literal $ |
| 53 | // |
| 54 | // DO NOT directly reference paths to files in the source tree, or the |
| 55 | // command will be missing proper dependencies to re-run if the files |
| 56 | // change. |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 57 | Cmd string |
| 58 | |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame^] | 59 | // Enable reading a file containing dependencies in gcc format after the command completes |
| 60 | Depfile bool |
| 61 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 62 | // 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] | 63 | // prebuilts or scripts that do not need a module to build them. |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 64 | Tools []string |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 65 | |
| 66 | // Local file that is used as the tool |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 67 | Tool_files []string |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 70 | type generator struct { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 71 | android.ModuleBase |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 72 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 73 | properties generatorProperties |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 74 | |
| 75 | tasks taskFunc |
| 76 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 77 | deps android.Paths |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 78 | rule blueprint.Rule |
| 79 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 80 | genPath android.Path |
Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 81 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 82 | outputFiles android.Paths |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 85 | type taskFunc func(ctx android.ModuleContext) []generateTask |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 86 | |
| 87 | type generateTask struct { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 88 | in android.Paths |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 89 | out android.WritablePaths |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 92 | func (g *generator) GeneratedSourceFiles() android.Paths { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 93 | return g.outputFiles |
| 94 | } |
| 95 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 96 | func (g *generator) GeneratedHeaderDir() android.Path { |
Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 97 | return g.genPath |
| 98 | } |
| 99 | |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 100 | func (g *generator) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 101 | if g, ok := ctx.Module().(*generator); ok { |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 102 | if len(g.properties.Tools) > 0 { |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 103 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 104 | {"arch", ctx.AConfig().BuildOsVariant}, |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 105 | }, nil, g.properties.Tools...) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 106 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 107 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 110 | func (g *generator) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 111 | if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 { |
| 112 | ctx.ModuleErrorf("at least one `tools` or `tool_files` is required") |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 113 | return |
| 114 | } |
| 115 | |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 116 | g.genPath = android.PathForModuleGen(ctx, "") |
| 117 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 118 | tools := map[string]android.Path{} |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 119 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 120 | if len(g.properties.Tools) > 0 { |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 121 | ctx.VisitDirectDeps(func(module blueprint.Module) { |
| 122 | if t, ok := module.(HostToolProvider); ok { |
| 123 | p := t.HostToolPath() |
| 124 | if p.Valid() { |
| 125 | g.deps = append(g.deps, p.Path()) |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 126 | tool := ctx.OtherModuleName(module) |
| 127 | if _, exists := tools[tool]; !exists { |
| 128 | tools[tool] = p.Path() |
| 129 | } else { |
| 130 | ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], p.Path().String()) |
| 131 | } |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 132 | } else { |
| 133 | ctx.ModuleErrorf("host tool %q missing output file", ctx.OtherModuleName(module)) |
| 134 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 135 | } else { |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 136 | ctx.ModuleErrorf("unknown dependency %q", ctx.OtherModuleName(module)) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 137 | } |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 138 | }) |
| 139 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 140 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 141 | for _, tool := range g.properties.Tool_files { |
| 142 | toolPath := android.PathForModuleSrc(ctx, tool) |
| 143 | g.deps = append(g.deps, toolPath) |
| 144 | if _, exists := tools[tool]; !exists { |
| 145 | tools[tool] = toolPath |
| 146 | } else { |
| 147 | ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], toolPath.String()) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | cmd, err := android.Expand(g.properties.Cmd, func(name string) (string, error) { |
| 152 | switch name { |
| 153 | case "location": |
| 154 | if len(g.properties.Tools) > 0 { |
| 155 | return tools[g.properties.Tools[0]].String(), nil |
| 156 | } else { |
| 157 | return tools[g.properties.Tool_files[0]].String(), nil |
| 158 | } |
| 159 | case "in": |
| 160 | return "${in}", nil |
| 161 | case "out": |
| 162 | return "${out}", nil |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame^] | 163 | case "depfile": |
| 164 | if !g.properties.Depfile { |
| 165 | return "", fmt.Errorf("$(depfile) used without depfile property") |
| 166 | } |
| 167 | return "${depfile}", nil |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 168 | case "genDir": |
| 169 | return g.genPath.String(), nil |
| 170 | default: |
| 171 | if strings.HasPrefix(name, "location ") { |
| 172 | label := strings.TrimSpace(strings.TrimPrefix(name, "location ")) |
| 173 | if tool, ok := tools[label]; ok { |
| 174 | return tool.String(), nil |
| 175 | } else { |
| 176 | return "", fmt.Errorf("unknown location label %q", label) |
| 177 | } |
| 178 | } |
| 179 | return "", fmt.Errorf("unknown variable '$(%s)'", name) |
| 180 | } |
| 181 | }) |
| 182 | |
| 183 | if err != nil { |
| 184 | ctx.PropertyErrorf("cmd", "%s", err.Error()) |
| 185 | } |
| 186 | |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame^] | 187 | ruleParams := blueprint.RuleParams{ |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 188 | Command: cmd, |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame^] | 189 | } |
| 190 | var args []string |
| 191 | if g.properties.Depfile { |
| 192 | ruleParams.Deps = blueprint.DepsGCC |
| 193 | args = append(args, "depfile") |
| 194 | } |
| 195 | g.rule = ctx.Rule(pctx, "generator", ruleParams, args...) |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 196 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 197 | for _, task := range g.tasks(ctx) { |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 198 | g.generateSourceFile(ctx, task) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 202 | func (g *generator) generateSourceFile(ctx android.ModuleContext, task generateTask) { |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame^] | 203 | params := android.ModuleBuildParams{ |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 204 | Rule: g.rule, |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 205 | Outputs: task.out, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 206 | Inputs: task.in, |
| 207 | Implicits: g.deps, |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame^] | 208 | } |
| 209 | if g.properties.Depfile { |
| 210 | depfile := android.GenPathWithExt(ctx, "", task.out[0], task.out[0].Ext()+".d") |
| 211 | params.Depfile = depfile |
| 212 | } |
| 213 | ctx.ModuleBuild(pctx, params) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 214 | |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 215 | for _, outputFile := range task.out { |
| 216 | g.outputFiles = append(g.outputFiles, outputFile) |
| 217 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | func generatorFactory(tasks taskFunc, props ...interface{}) (blueprint.Module, []interface{}) { |
| 221 | module := &generator{ |
| 222 | tasks: tasks, |
| 223 | } |
| 224 | |
| 225 | props = append(props, &module.properties) |
| 226 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 227 | return android.InitAndroidModule(module, props...) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | func GenSrcsFactory() (blueprint.Module, []interface{}) { |
| 231 | properties := &genSrcsProperties{} |
| 232 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 233 | tasks := func(ctx android.ModuleContext) []generateTask { |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 234 | srcFiles := ctx.ExpandSources(properties.Srcs, nil) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 235 | tasks := make([]generateTask, 0, len(srcFiles)) |
| 236 | for _, in := range srcFiles { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 237 | tasks = append(tasks, generateTask{ |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 238 | in: android.Paths{in}, |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 239 | out: android.WritablePaths{android.GenPathWithExt(ctx, "", in, properties.Output_extension)}, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 240 | }) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 241 | } |
| 242 | return tasks |
| 243 | } |
| 244 | |
| 245 | return generatorFactory(tasks, properties) |
| 246 | } |
| 247 | |
| 248 | type genSrcsProperties struct { |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 249 | // list of input files |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 250 | Srcs []string |
| 251 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 252 | // extension that will be substituted for each output file |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 253 | Output_extension string |
| 254 | } |
| 255 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 256 | func GenRuleFactory() (blueprint.Module, []interface{}) { |
| 257 | properties := &genRuleProperties{} |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 258 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 259 | tasks := func(ctx android.ModuleContext) []generateTask { |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 260 | outs := make(android.WritablePaths, len(properties.Out)) |
| 261 | for i, out := range properties.Out { |
| 262 | outs[i] = android.PathForModuleGen(ctx, out) |
| 263 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 264 | return []generateTask{ |
| 265 | { |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 266 | in: ctx.ExpandSources(properties.Srcs, nil), |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 267 | out: outs, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 268 | }, |
| 269 | } |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 270 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 271 | |
| 272 | return generatorFactory(tasks, properties) |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 275 | type genRuleProperties struct { |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 276 | // list of input files |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 277 | Srcs []string |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 278 | |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 279 | // names of the output files that will be generated |
| 280 | Out []string |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 281 | } |