blob: 4f3ba93b23a46fb5dfdd62fb9fc07d6ea3991b46 [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)
Colin Crossac87c992017-12-08 18:23:13 -0800134 android.ExtractSourcesDeps(ctx, g.properties.Tool_files)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700135 if g, ok := ctx.Module().(*Module); ok {
Colin Cross6f080df2016-11-04 15:32:58 -0700136 if len(g.properties.Tools) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800137 ctx.AddFarVariationDependencies([]blueprint.Variation{
Colin Cross6510f912017-11-29 00:27:14 -0800138 {"arch", ctx.Config().BuildOsVariant},
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700139 }, hostToolDepTag, g.properties.Tools...)
Colin Cross6362e272015-10-29 15:25:03 -0700140 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700141 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700142}
143
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700144func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross5ed99c62016-11-22 12:55:55 -0800145 if len(g.properties.Export_include_dirs) > 0 {
146 for _, dir := range g.properties.Export_include_dirs {
147 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
148 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
149 }
150 } else {
151 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
152 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700153
Colin Cross6f080df2016-11-04 15:32:58 -0700154 tools := map[string]android.Path{}
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700155
Colin Cross6f080df2016-11-04 15:32:58 -0700156 if len(g.properties.Tools) > 0 {
Colin Cross35143d02017-11-16 00:11:20 -0800157 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700158 switch ctx.OtherModuleDependencyTag(module) {
159 case android.SourceDepTag:
160 // Nothing to do
161 case hostToolDepTag:
162 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700163 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700164
165 if t, ok := module.(HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800166 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800167 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800168 ctx.AddMissingDependencies([]string{tool})
169 } else {
170 ctx.ModuleErrorf("depends on disabled module %q", tool)
171 }
172 break
173 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700174 path = t.HostToolPath()
175 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
176 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
177 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700178 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700179 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
180 break
Colin Cross6f080df2016-11-04 15:32:58 -0700181 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700182 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700183 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700184 break
185 }
186
187 if path.Valid() {
188 g.deps = append(g.deps, path.Path())
189 if _, exists := tools[tool]; !exists {
190 tools[tool] = path.Path()
191 } else {
192 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], path.Path().String())
193 }
194 } else {
195 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700196 }
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700197 default:
198 ctx.ModuleErrorf("unknown dependency on %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700199 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700200 })
201 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700202
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700203 if ctx.Failed() {
204 return
205 }
206
Colin Crossc3315992017-12-08 19:12:36 -0800207 toolFiles := ctx.ExpandSources(g.properties.Tool_files, nil)
Colin Crossac87c992017-12-08 18:23:13 -0800208 for _, tool := range toolFiles {
209 g.deps = append(g.deps, tool)
210 if _, exists := tools[tool.Rel()]; !exists {
211 tools[tool.Rel()] = tool
Colin Cross6f080df2016-11-04 15:32:58 -0700212 } else {
Colin Crossac87c992017-12-08 18:23:13 -0800213 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool.Rel()], tool.Rel())
Colin Cross6f080df2016-11-04 15:32:58 -0700214 }
215 }
216
Jeff Gaston02a684b2017-10-27 14:59:27 -0700217 referencedDepfile := false
218
Jeff Gaston437d23c2017-11-08 12:38:00 -0800219 srcFiles := ctx.ExpandSources(g.properties.Srcs, nil)
Nan Zhangea568a42017-11-08 21:20:04 -0800220 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800221
Jeff Gaston437d23c2017-11-08 12:38:00 -0800222 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross6f080df2016-11-04 15:32:58 -0700223 switch name {
224 case "location":
Colin Crossc3315992017-12-08 19:12:36 -0800225 if len(g.properties.Tools) == 0 && len(toolFiles) == 0 {
226 return "", fmt.Errorf("at least one `tools` or `tool_files` is required if $(location) is used")
227 }
228
Colin Cross6f080df2016-11-04 15:32:58 -0700229 if len(g.properties.Tools) > 0 {
230 return tools[g.properties.Tools[0]].String(), nil
231 } else {
Colin Crossac87c992017-12-08 18:23:13 -0800232 return tools[toolFiles[0].Rel()].String(), nil
Colin Cross6f080df2016-11-04 15:32:58 -0700233 }
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)
Jeff Gaston31603062017-12-08 12:45:35 -0800280 // Escape the command for the shell
281 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
282 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
283 sandboxPath, genDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700284
Colin Cross33bfb0a2016-11-21 17:23:08 -0800285 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700286 Command: sandboxCommand,
287 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800288 }
Colin Cross15e86d92017-10-20 15:07:08 -0700289 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800290 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800291 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700292 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800293 }
294 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700295
Jeff Gaston437d23c2017-11-08 12:38:00 -0800296 g.generateSourceFile(ctx, task)
297
Colin Crossd350ecd2015-04-28 13:25:36 -0700298}
299
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700300func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700301 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700302 if len(task.out) == 0 {
303 ctx.ModuleErrorf("must have at least one output file")
304 return
305 }
Colin Cross67a5c132017-05-09 13:45:28 -0700306 if len(task.out) == 1 {
307 desc += " " + task.out[0].Base()
308 }
309
Jeff Gaston02a684b2017-10-27 14:59:27 -0700310 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800311 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700312 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
313 }
314
Colin Crossae887032017-10-23 17:16:14 -0700315 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700316 Rule: g.rule,
317 Description: "generate",
318 Output: task.out[0],
319 ImplicitOutputs: task.out[1:],
320 Inputs: task.in,
321 Implicits: g.deps,
322 Args: map[string]string{
323 "allouts": strings.Join(task.out.Strings(), " "),
324 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800325 }
Nan Zhangea568a42017-11-08 21:20:04 -0800326 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700327 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
328 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800329 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700330
Colin Crossae887032017-10-23 17:16:14 -0700331 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700332
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700333 for _, outputFile := range task.out {
334 g.outputFiles = append(g.outputFiles, outputFile)
335 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700336}
337
Jeff Gaston437d23c2017-11-08 12:38:00 -0800338func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700339 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800340 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700341 }
342
Colin Cross36242852017-06-23 15:06:31 -0700343 module.AddProperties(props...)
344 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700345
Colin Cross36242852017-06-23 15:06:31 -0700346 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700347}
348
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700349func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700350 properties := &genSrcsProperties{}
351
Jeff Gaston437d23c2017-11-08 12:38:00 -0800352 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
353 commands := []string{}
354 outFiles := android.WritablePaths{}
355 genPath := android.PathForModuleGen(ctx).String()
Colin Crossd350ecd2015-04-28 13:25:36 -0700356 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800357 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800358 outFiles = append(outFiles, outFile)
359
360 // replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
361 relOut, err := filepath.Rel(genPath, outFile.String())
362 if err != nil {
363 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
364 }
365 sandboxOutfile := filepath.Join("__SBOX_OUT_DIR__", relOut)
366 command, err := android.Expand(rawCommand, func(name string) (string, error) {
367 switch name {
368 case "in":
369 return in.String(), nil
370 case "out":
371 return sandboxOutfile, nil
372 default:
373 return "$(" + name + ")", nil
374 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700375 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800376 if err != nil {
377 ctx.PropertyErrorf("cmd", err.Error())
378 }
379
380 // escape the command in case for example it contains '#', an odd number of '"', etc
381 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape([]string{command})[0])
382 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700383 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800384 fullCommand := strings.Join(commands, " && ")
385
386 return generateTask{
387 in: srcFiles,
388 out: outFiles,
389 cmd: fullCommand,
390 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700391 }
392
Jeff Gaston437d23c2017-11-08 12:38:00 -0800393 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700394}
395
Colin Cross54190b32017-10-09 15:34:10 -0700396func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700397 m := NewGenSrcs()
398 android.InitAndroidModule(m)
399 return m
400}
401
Colin Crossd350ecd2015-04-28 13:25:36 -0700402type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700403 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800404 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700405}
406
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700407func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700408 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700409
Jeff Gaston437d23c2017-11-08 12:38:00 -0800410 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700411 outs := make(android.WritablePaths, len(properties.Out))
412 for i, out := range properties.Out {
413 outs[i] = android.PathForModuleGen(ctx, out)
414 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800415 return generateTask{
416 in: srcFiles,
417 out: outs,
418 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700419 }
Colin Cross5049f022015-03-18 13:28:46 -0700420 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700421
Jeff Gaston437d23c2017-11-08 12:38:00 -0800422 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700423}
424
Colin Cross54190b32017-10-09 15:34:10 -0700425func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700426 m := NewGenRule()
427 android.InitAndroidModule(m)
428 return m
429}
430
Colin Crossd350ecd2015-04-28 13:25:36 -0700431type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700432 // names of the output files that will be generated
433 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700434}
Nan Zhangea568a42017-11-08 21:20:04 -0800435
436var Bool = proptools.Bool
437var String = proptools.String