blob: c5b7e1d3443f25eb2b850f0a130e8a83806ab49f [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// 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
15package genrule
16
17import (
Colin Cross6f080df2016-11-04 15:32:58 -070018 "fmt"
19 "strings"
Dan Willemsen3f4539b2016-09-28 16:19:10 -070020
Colin Cross70b40592015-03-23 12:57:34 -070021 "github.com/google/blueprint"
Dan Willemsen8eded0a2017-09-13 16:07:44 -070022 "github.com/google/blueprint/bootstrap"
Nan Zhangea568a42017-11-08 21:20:04 -080023 "github.com/google/blueprint/proptools"
Colin Cross5049f022015-03-18 13:28:46 -070024
Colin Cross635c3b02016-05-18 15:37:25 -070025 "android/soong/android"
Jeff Gastonefc1b412017-03-29 17:29:06 -070026 "android/soong/shared"
27 "path/filepath"
Colin Cross5049f022015-03-18 13:28:46 -070028)
29
Colin Cross463a90e2015-06-17 14:20:06 -070030func init() {
Colin Cross54190b32017-10-09 15:34:10 -070031 android.RegisterModuleType("gensrcs", GenSrcsFactory)
32 android.RegisterModuleType("genrule", GenRuleFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070033}
34
Colin Cross5049f022015-03-18 13:28:46 -070035var (
Colin Cross635c3b02016-05-18 15:37:25 -070036 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross5049f022015-03-18 13:28:46 -070037)
38
Jeff Gastonefc1b412017-03-29 17:29:06 -070039func init() {
40 pctx.HostBinToolVariable("sboxCmd", "sbox")
41}
42
Colin Cross5049f022015-03-18 13:28:46 -070043type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070044 GeneratedSourceFiles() android.Paths
Colin Cross5ed99c62016-11-22 12:55:55 -080045 GeneratedHeaderDirs() android.Paths
Colin Cross5049f022015-03-18 13:28:46 -070046}
47
Colin Crossd350ecd2015-04-28 13:25:36 -070048type HostToolProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -070049 HostToolPath() android.OptionalPath
Colin Crossd350ecd2015-04-28 13:25:36 -070050}
Colin Cross5049f022015-03-18 13:28:46 -070051
Dan Willemsend6ba0d52017-09-13 15:46:47 -070052type hostToolDependencyTag struct {
53 blueprint.BaseDependencyTag
54}
55
56var hostToolDepTag hostToolDependencyTag
57
Colin Cross7d5136f2015-05-11 13:39:40 -070058type generatorProperties struct {
Jeff Gastonefc1b412017-03-29 17:29:06 -070059 // The command to run on one or more input files. Cmd supports substitution of a few variables
60 // (the actual substitution is implemented in GenerateAndroidBuildActions below)
61 //
62 // Available variables for substitution:
63 //
Colin Cross2296f5b2017-10-17 21:38:14 -070064 // $(location): the path to the first entry in tools or tool_files
65 // $(location <label>): the path to the tool or tool_file with name <label>
66 // $(in): one or more input files
67 // $(out): a single output file
68 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
69 // $(genDir): the sandbox directory for this tool; contains $(out)
70 // $$: a literal $
Colin Cross6f080df2016-11-04 15:32:58 -070071 //
Jeff Gastonefc1b412017-03-29 17:29:06 -070072 // All files used must be declared as inputs (to ensure proper up-to-date checks).
73 // Use "$(in)" directly in Cmd to ensure that all inputs used are declared.
Nan Zhangea568a42017-11-08 21:20:04 -080074 Cmd *string
Colin Cross7d5136f2015-05-11 13:39:40 -070075
Colin Cross33bfb0a2016-11-21 17:23:08 -080076 // Enable reading a file containing dependencies in gcc format after the command completes
Nan Zhangea568a42017-11-08 21:20:04 -080077 Depfile *bool
Colin Cross33bfb0a2016-11-21 17:23:08 -080078
Colin Cross6f080df2016-11-04 15:32:58 -070079 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -070080 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -070081 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070082
83 // Local file that is used as the tool
Colin Cross6f080df2016-11-04 15:32:58 -070084 Tool_files []string
Colin Cross5ed99c62016-11-22 12:55:55 -080085
86 // List of directories to export generated headers from
87 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -080088
89 // list of input files
90 Srcs []string
Colin Cross7d5136f2015-05-11 13:39:40 -070091}
92
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070093type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -070094 android.ModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -070095
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070096 // For other packages to make their own genrules with extra
97 // properties
98 Extra interface{}
99
Colin Cross7d5136f2015-05-11 13:39:40 -0700100 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700101
Jeff Gaston437d23c2017-11-08 12:38:00 -0800102 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700103
Colin Cross635c3b02016-05-18 15:37:25 -0700104 deps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700105 rule blueprint.Rule
106
Colin Cross5ed99c62016-11-22 12:55:55 -0800107 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700108
Colin Cross635c3b02016-05-18 15:37:25 -0700109 outputFiles android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700110}
111
Jeff Gaston437d23c2017-11-08 12:38:00 -0800112type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700113
114type generateTask struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700115 in android.Paths
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700116 out android.WritablePaths
Jeff Gaston437d23c2017-11-08 12:38:00 -0800117 cmd string
Colin Crossd350ecd2015-04-28 13:25:36 -0700118}
119
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700120func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700121 return g.outputFiles
122}
123
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700124func (g *Module) Srcs() android.Paths {
Colin Cross068e0fe2016-12-13 15:23:47 -0800125 return g.outputFiles
126}
127
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700128func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800129 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700130}
131
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700132func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross708c4242017-01-13 18:05:49 -0800133 android.ExtractSourcesDeps(ctx, g.properties.Srcs)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700134 if g, ok := ctx.Module().(*Module); ok {
Colin Cross6f080df2016-11-04 15:32:58 -0700135 if len(g.properties.Tools) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800136 ctx.AddFarVariationDependencies([]blueprint.Variation{
Colin Crossa1ad8d12016-06-01 17:09:44 -0700137 {"arch", ctx.AConfig().BuildOsVariant},
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700138 }, hostToolDepTag, g.properties.Tools...)
Colin Cross6362e272015-10-29 15:25:03 -0700139 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700140 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700141}
142
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700143func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6f080df2016-11-04 15:32:58 -0700144 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
145 ctx.ModuleErrorf("at least one `tools` or `tool_files` is required")
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700146 return
147 }
148
Colin Cross5ed99c62016-11-22 12:55:55 -0800149 if len(g.properties.Export_include_dirs) > 0 {
150 for _, dir := range g.properties.Export_include_dirs {
151 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
152 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
153 }
154 } else {
155 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
156 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700157
Colin Cross6f080df2016-11-04 15:32:58 -0700158 tools := map[string]android.Path{}
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700159
Colin Cross6f080df2016-11-04 15:32:58 -0700160 if len(g.properties.Tools) > 0 {
Colin Crossd11fcda2017-10-23 17:59:01 -0700161 ctx.VisitDirectDeps(func(module android.Module) {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700162 switch ctx.OtherModuleDependencyTag(module) {
163 case android.SourceDepTag:
164 // Nothing to do
165 case hostToolDepTag:
166 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700167 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700168
169 if t, ok := module.(HostToolProvider); ok {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700170 path = t.HostToolPath()
171 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
172 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
173 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700174 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700175 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
176 break
Colin Cross6f080df2016-11-04 15:32:58 -0700177 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700178 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700179 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700180 break
181 }
182
183 if path.Valid() {
184 g.deps = append(g.deps, path.Path())
185 if _, exists := tools[tool]; !exists {
186 tools[tool] = path.Path()
187 } else {
188 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], path.Path().String())
189 }
190 } else {
191 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700192 }
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700193 default:
194 ctx.ModuleErrorf("unknown dependency on %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700195 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700196 })
197 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700198
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700199 if ctx.Failed() {
200 return
201 }
202
Colin Cross6f080df2016-11-04 15:32:58 -0700203 for _, tool := range g.properties.Tool_files {
204 toolPath := android.PathForModuleSrc(ctx, tool)
205 g.deps = append(g.deps, toolPath)
206 if _, exists := tools[tool]; !exists {
207 tools[tool] = toolPath
208 } else {
209 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], toolPath.String())
210 }
211 }
212
Jeff Gaston02a684b2017-10-27 14:59:27 -0700213 referencedDepfile := false
214
Jeff Gaston437d23c2017-11-08 12:38:00 -0800215 srcFiles := ctx.ExpandSources(g.properties.Srcs, nil)
Nan Zhangea568a42017-11-08 21:20:04 -0800216 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800217
Jeff Gaston437d23c2017-11-08 12:38:00 -0800218 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross6f080df2016-11-04 15:32:58 -0700219 switch name {
220 case "location":
221 if len(g.properties.Tools) > 0 {
222 return tools[g.properties.Tools[0]].String(), nil
223 } else {
224 return tools[g.properties.Tool_files[0]].String(), nil
225 }
226 case "in":
227 return "${in}", nil
228 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700229 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800230 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700231 referencedDepfile = true
Nan Zhangea568a42017-11-08 21:20:04 -0800232 if !Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800233 return "", fmt.Errorf("$(depfile) used without depfile property")
234 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700235 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700236 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800237 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700238 default:
239 if strings.HasPrefix(name, "location ") {
240 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
241 if tool, ok := tools[label]; ok {
242 return tool.String(), nil
243 } else {
244 return "", fmt.Errorf("unknown location label %q", label)
245 }
246 }
247 return "", fmt.Errorf("unknown variable '$(%s)'", name)
248 }
249 })
250
Nan Zhangea568a42017-11-08 21:20:04 -0800251 if Bool(g.properties.Depfile) && !referencedDepfile {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700252 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
253 }
254
Colin Cross6f080df2016-11-04 15:32:58 -0700255 if err != nil {
256 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700257 return
Colin Cross6f080df2016-11-04 15:32:58 -0700258 }
259
Jeff Gastonefc1b412017-03-29 17:29:06 -0700260 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700261 buildDir := android.PathForOutput(ctx).String()
262 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700263
264 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
265 // to be replaced later by ninja_strings.go
Jeff Gaston02a684b2017-10-27 14:59:27 -0700266 depfilePlaceholder := ""
Nan Zhangea568a42017-11-08 21:20:04 -0800267 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700268 depfilePlaceholder = "$depfileArgs"
269 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800270
Jeff Gaston437d23c2017-11-08 12:38:00 -0800271 genDir := android.PathForModuleGen(ctx)
272 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %q %s $allouts", sandboxPath, genDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700273
Colin Cross33bfb0a2016-11-21 17:23:08 -0800274 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700275 Command: sandboxCommand,
276 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800277 }
Colin Cross15e86d92017-10-20 15:07:08 -0700278 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800279 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800280 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700281 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800282 }
283 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700284
Jeff Gaston437d23c2017-11-08 12:38:00 -0800285 g.generateSourceFile(ctx, task)
286
Colin Crossd350ecd2015-04-28 13:25:36 -0700287}
288
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700289func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700290 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700291 if len(task.out) == 0 {
292 ctx.ModuleErrorf("must have at least one output file")
293 return
294 }
Colin Cross67a5c132017-05-09 13:45:28 -0700295 if len(task.out) == 1 {
296 desc += " " + task.out[0].Base()
297 }
298
Jeff Gaston02a684b2017-10-27 14:59:27 -0700299 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800300 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700301 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
302 }
303
Colin Crossae887032017-10-23 17:16:14 -0700304 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700305 Rule: g.rule,
306 Description: "generate",
307 Output: task.out[0],
308 ImplicitOutputs: task.out[1:],
309 Inputs: task.in,
310 Implicits: g.deps,
311 Args: map[string]string{
312 "allouts": strings.Join(task.out.Strings(), " "),
313 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800314 }
Nan Zhangea568a42017-11-08 21:20:04 -0800315 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700316 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
317 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800318 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700319
Colin Crossae887032017-10-23 17:16:14 -0700320 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700321
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700322 for _, outputFile := range task.out {
323 g.outputFiles = append(g.outputFiles, outputFile)
324 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700325}
326
Jeff Gaston437d23c2017-11-08 12:38:00 -0800327func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700328 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800329 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700330 }
331
Colin Cross36242852017-06-23 15:06:31 -0700332 module.AddProperties(props...)
333 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700334
Colin Cross36242852017-06-23 15:06:31 -0700335 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700336}
337
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700338func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700339 properties := &genSrcsProperties{}
340
Jeff Gaston437d23c2017-11-08 12:38:00 -0800341 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
342 commands := []string{}
343 outFiles := android.WritablePaths{}
344 genPath := android.PathForModuleGen(ctx).String()
Colin Crossd350ecd2015-04-28 13:25:36 -0700345 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800346 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800347 outFiles = append(outFiles, outFile)
348
349 // replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
350 relOut, err := filepath.Rel(genPath, outFile.String())
351 if err != nil {
352 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
353 }
354 sandboxOutfile := filepath.Join("__SBOX_OUT_DIR__", relOut)
355 command, err := android.Expand(rawCommand, func(name string) (string, error) {
356 switch name {
357 case "in":
358 return in.String(), nil
359 case "out":
360 return sandboxOutfile, nil
361 default:
362 return "$(" + name + ")", nil
363 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700364 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800365 if err != nil {
366 ctx.PropertyErrorf("cmd", err.Error())
367 }
368
369 // escape the command in case for example it contains '#', an odd number of '"', etc
370 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape([]string{command})[0])
371 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700372 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800373 fullCommand := strings.Join(commands, " && ")
374
375 return generateTask{
376 in: srcFiles,
377 out: outFiles,
378 cmd: fullCommand,
379 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700380 }
381
Jeff Gaston437d23c2017-11-08 12:38:00 -0800382 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700383}
384
Colin Cross54190b32017-10-09 15:34:10 -0700385func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700386 m := NewGenSrcs()
387 android.InitAndroidModule(m)
388 return m
389}
390
Colin Crossd350ecd2015-04-28 13:25:36 -0700391type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700392 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800393 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700394}
395
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700396func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700397 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700398
Jeff Gaston437d23c2017-11-08 12:38:00 -0800399 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700400 outs := make(android.WritablePaths, len(properties.Out))
401 for i, out := range properties.Out {
402 outs[i] = android.PathForModuleGen(ctx, out)
403 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800404 return generateTask{
405 in: srcFiles,
406 out: outs,
407 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700408 }
Colin Cross5049f022015-03-18 13:28:46 -0700409 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700410
Jeff Gaston437d23c2017-11-08 12:38:00 -0800411 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700412}
413
Colin Cross54190b32017-10-09 15:34:10 -0700414func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700415 m := NewGenRule()
416 android.InitAndroidModule(m)
417 return m
418}
419
Colin Crossd350ecd2015-04-28 13:25:36 -0700420type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700421 // names of the output files that will be generated
422 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700423}
Nan Zhangea568a42017-11-08 21:20:04 -0800424
425var Bool = proptools.Bool
426var String = proptools.String