blob: 651ec15df71d9442dbf2a26a9db7c8b31f3f7da2 [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 Cross6510f912017-11-29 00:27:14 -0800137 {"arch", ctx.Config().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 Cross35143d02017-11-16 00:11:20 -0800161 ctx.VisitDirectDepsBlueprint(func(module blueprint.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 {
Colin Cross35143d02017-11-16 00:11:20 -0800170 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800171 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800172 ctx.AddMissingDependencies([]string{tool})
173 } else {
174 ctx.ModuleErrorf("depends on disabled module %q", tool)
175 }
176 break
177 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700178 path = t.HostToolPath()
179 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
180 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
181 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700182 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700183 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
184 break
Colin Cross6f080df2016-11-04 15:32:58 -0700185 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700186 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700187 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700188 break
189 }
190
191 if path.Valid() {
192 g.deps = append(g.deps, path.Path())
193 if _, exists := tools[tool]; !exists {
194 tools[tool] = path.Path()
195 } else {
196 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], path.Path().String())
197 }
198 } else {
199 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700200 }
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700201 default:
202 ctx.ModuleErrorf("unknown dependency on %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700203 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700204 })
205 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700206
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700207 if ctx.Failed() {
208 return
209 }
210
Colin Cross6f080df2016-11-04 15:32:58 -0700211 for _, tool := range g.properties.Tool_files {
212 toolPath := android.PathForModuleSrc(ctx, tool)
213 g.deps = append(g.deps, toolPath)
214 if _, exists := tools[tool]; !exists {
215 tools[tool] = toolPath
216 } else {
217 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], toolPath.String())
218 }
219 }
220
Jeff Gaston02a684b2017-10-27 14:59:27 -0700221 referencedDepfile := false
222
Jeff Gaston437d23c2017-11-08 12:38:00 -0800223 srcFiles := ctx.ExpandSources(g.properties.Srcs, nil)
Nan Zhangea568a42017-11-08 21:20:04 -0800224 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800225
Jeff Gaston437d23c2017-11-08 12:38:00 -0800226 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross6f080df2016-11-04 15:32:58 -0700227 switch name {
228 case "location":
229 if len(g.properties.Tools) > 0 {
230 return tools[g.properties.Tools[0]].String(), nil
231 } else {
232 return tools[g.properties.Tool_files[0]].String(), nil
233 }
234 case "in":
235 return "${in}", nil
236 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700237 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800238 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700239 referencedDepfile = true
Nan Zhangea568a42017-11-08 21:20:04 -0800240 if !Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800241 return "", fmt.Errorf("$(depfile) used without depfile property")
242 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700243 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700244 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800245 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700246 default:
247 if strings.HasPrefix(name, "location ") {
248 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
249 if tool, ok := tools[label]; ok {
250 return tool.String(), nil
251 } else {
252 return "", fmt.Errorf("unknown location label %q", label)
253 }
254 }
255 return "", fmt.Errorf("unknown variable '$(%s)'", name)
256 }
257 })
258
Nan Zhangea568a42017-11-08 21:20:04 -0800259 if Bool(g.properties.Depfile) && !referencedDepfile {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700260 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
261 }
262
Colin Cross6f080df2016-11-04 15:32:58 -0700263 if err != nil {
264 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700265 return
Colin Cross6f080df2016-11-04 15:32:58 -0700266 }
267
Jeff Gastonefc1b412017-03-29 17:29:06 -0700268 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700269 buildDir := android.PathForOutput(ctx).String()
270 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700271
272 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
273 // to be replaced later by ninja_strings.go
Jeff Gaston02a684b2017-10-27 14:59:27 -0700274 depfilePlaceholder := ""
Nan Zhangea568a42017-11-08 21:20:04 -0800275 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700276 depfilePlaceholder = "$depfileArgs"
277 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800278
Jeff Gaston437d23c2017-11-08 12:38:00 -0800279 genDir := android.PathForModuleGen(ctx)
280 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 -0700281
Colin Cross33bfb0a2016-11-21 17:23:08 -0800282 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700283 Command: sandboxCommand,
284 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800285 }
Colin Cross15e86d92017-10-20 15:07:08 -0700286 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800287 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800288 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700289 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800290 }
291 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700292
Jeff Gaston437d23c2017-11-08 12:38:00 -0800293 g.generateSourceFile(ctx, task)
294
Colin Crossd350ecd2015-04-28 13:25:36 -0700295}
296
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700297func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700298 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700299 if len(task.out) == 0 {
300 ctx.ModuleErrorf("must have at least one output file")
301 return
302 }
Colin Cross67a5c132017-05-09 13:45:28 -0700303 if len(task.out) == 1 {
304 desc += " " + task.out[0].Base()
305 }
306
Jeff Gaston02a684b2017-10-27 14:59:27 -0700307 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800308 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700309 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
310 }
311
Colin Crossae887032017-10-23 17:16:14 -0700312 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700313 Rule: g.rule,
314 Description: "generate",
315 Output: task.out[0],
316 ImplicitOutputs: task.out[1:],
317 Inputs: task.in,
318 Implicits: g.deps,
319 Args: map[string]string{
320 "allouts": strings.Join(task.out.Strings(), " "),
321 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800322 }
Nan Zhangea568a42017-11-08 21:20:04 -0800323 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700324 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
325 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800326 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700327
Colin Crossae887032017-10-23 17:16:14 -0700328 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700329
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700330 for _, outputFile := range task.out {
331 g.outputFiles = append(g.outputFiles, outputFile)
332 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700333}
334
Jeff Gaston437d23c2017-11-08 12:38:00 -0800335func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700336 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800337 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700338 }
339
Colin Cross36242852017-06-23 15:06:31 -0700340 module.AddProperties(props...)
341 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700342
Colin Cross36242852017-06-23 15:06:31 -0700343 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700344}
345
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700346func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700347 properties := &genSrcsProperties{}
348
Jeff Gaston437d23c2017-11-08 12:38:00 -0800349 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
350 commands := []string{}
351 outFiles := android.WritablePaths{}
352 genPath := android.PathForModuleGen(ctx).String()
Colin Crossd350ecd2015-04-28 13:25:36 -0700353 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800354 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800355 outFiles = append(outFiles, outFile)
356
357 // replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
358 relOut, err := filepath.Rel(genPath, outFile.String())
359 if err != nil {
360 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
361 }
362 sandboxOutfile := filepath.Join("__SBOX_OUT_DIR__", relOut)
363 command, err := android.Expand(rawCommand, func(name string) (string, error) {
364 switch name {
365 case "in":
366 return in.String(), nil
367 case "out":
368 return sandboxOutfile, nil
369 default:
370 return "$(" + name + ")", nil
371 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700372 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800373 if err != nil {
374 ctx.PropertyErrorf("cmd", err.Error())
375 }
376
377 // escape the command in case for example it contains '#', an odd number of '"', etc
378 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape([]string{command})[0])
379 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700380 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800381 fullCommand := strings.Join(commands, " && ")
382
383 return generateTask{
384 in: srcFiles,
385 out: outFiles,
386 cmd: fullCommand,
387 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700388 }
389
Jeff Gaston437d23c2017-11-08 12:38:00 -0800390 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700391}
392
Colin Cross54190b32017-10-09 15:34:10 -0700393func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700394 m := NewGenSrcs()
395 android.InitAndroidModule(m)
396 return m
397}
398
Colin Crossd350ecd2015-04-28 13:25:36 -0700399type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700400 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800401 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700402}
403
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700404func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700405 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700406
Jeff Gaston437d23c2017-11-08 12:38:00 -0800407 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700408 outs := make(android.WritablePaths, len(properties.Out))
409 for i, out := range properties.Out {
410 outs[i] = android.PathForModuleGen(ctx, out)
411 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800412 return generateTask{
413 in: srcFiles,
414 out: outs,
415 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700416 }
Colin Cross5049f022015-03-18 13:28:46 -0700417 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700418
Jeff Gaston437d23c2017-11-08 12:38:00 -0800419 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700420}
421
Colin Cross54190b32017-10-09 15:34:10 -0700422func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700423 m := NewGenRule()
424 android.InitAndroidModule(m)
425 return m
426}
427
Colin Crossd350ecd2015-04-28 13:25:36 -0700428type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700429 // names of the output files that will be generated
430 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700431}
Nan Zhangea568a42017-11-08 21:20:04 -0800432
433var Bool = proptools.Bool
434var String = proptools.String