blob: 983941001fd5b4e13ed3ae072ed73df9a22d4385 [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 Crossac87c992017-12-08 18:23:13 -0800145 toolFiles := ctx.ExpandSources(g.properties.Tool_files, nil)
146 if len(g.properties.Tools) == 0 && len(toolFiles) == 0 {
Colin Cross6f080df2016-11-04 15:32:58 -0700147 ctx.ModuleErrorf("at least one `tools` or `tool_files` is required")
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700148 return
149 }
150
Colin Cross5ed99c62016-11-22 12:55:55 -0800151 if len(g.properties.Export_include_dirs) > 0 {
152 for _, dir := range g.properties.Export_include_dirs {
153 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
154 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
155 }
156 } else {
157 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
158 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700159
Colin Cross6f080df2016-11-04 15:32:58 -0700160 tools := map[string]android.Path{}
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700161
Colin Cross6f080df2016-11-04 15:32:58 -0700162 if len(g.properties.Tools) > 0 {
Colin Cross35143d02017-11-16 00:11:20 -0800163 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700164 switch ctx.OtherModuleDependencyTag(module) {
165 case android.SourceDepTag:
166 // Nothing to do
167 case hostToolDepTag:
168 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700169 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700170
171 if t, ok := module.(HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800172 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800173 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800174 ctx.AddMissingDependencies([]string{tool})
175 } else {
176 ctx.ModuleErrorf("depends on disabled module %q", tool)
177 }
178 break
179 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700180 path = t.HostToolPath()
181 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
182 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
183 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700184 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700185 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
186 break
Colin Cross6f080df2016-11-04 15:32:58 -0700187 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700188 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700189 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700190 break
191 }
192
193 if path.Valid() {
194 g.deps = append(g.deps, path.Path())
195 if _, exists := tools[tool]; !exists {
196 tools[tool] = path.Path()
197 } else {
198 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], path.Path().String())
199 }
200 } else {
201 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700202 }
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700203 default:
204 ctx.ModuleErrorf("unknown dependency on %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700205 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700206 })
207 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700208
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700209 if ctx.Failed() {
210 return
211 }
212
Colin Crossac87c992017-12-08 18:23:13 -0800213 for _, tool := range toolFiles {
214 g.deps = append(g.deps, tool)
215 if _, exists := tools[tool.Rel()]; !exists {
216 tools[tool.Rel()] = tool
Colin Cross6f080df2016-11-04 15:32:58 -0700217 } else {
Colin Crossac87c992017-12-08 18:23:13 -0800218 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool.Rel()], tool.Rel())
Colin Cross6f080df2016-11-04 15:32:58 -0700219 }
220 }
221
Jeff Gaston02a684b2017-10-27 14:59:27 -0700222 referencedDepfile := false
223
Jeff Gaston437d23c2017-11-08 12:38:00 -0800224 srcFiles := ctx.ExpandSources(g.properties.Srcs, nil)
Nan Zhangea568a42017-11-08 21:20:04 -0800225 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800226
Jeff Gaston437d23c2017-11-08 12:38:00 -0800227 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross6f080df2016-11-04 15:32:58 -0700228 switch name {
229 case "location":
230 if len(g.properties.Tools) > 0 {
231 return tools[g.properties.Tools[0]].String(), nil
232 } else {
Colin Crossac87c992017-12-08 18:23:13 -0800233 return tools[toolFiles[0].Rel()].String(), nil
Colin Cross6f080df2016-11-04 15:32:58 -0700234 }
235 case "in":
236 return "${in}", nil
237 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700238 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800239 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700240 referencedDepfile = true
Nan Zhangea568a42017-11-08 21:20:04 -0800241 if !Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800242 return "", fmt.Errorf("$(depfile) used without depfile property")
243 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700244 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700245 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800246 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700247 default:
248 if strings.HasPrefix(name, "location ") {
249 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
250 if tool, ok := tools[label]; ok {
251 return tool.String(), nil
252 } else {
253 return "", fmt.Errorf("unknown location label %q", label)
254 }
255 }
256 return "", fmt.Errorf("unknown variable '$(%s)'", name)
257 }
258 })
259
Nan Zhangea568a42017-11-08 21:20:04 -0800260 if Bool(g.properties.Depfile) && !referencedDepfile {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700261 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
262 }
263
Colin Cross6f080df2016-11-04 15:32:58 -0700264 if err != nil {
265 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700266 return
Colin Cross6f080df2016-11-04 15:32:58 -0700267 }
268
Jeff Gastonefc1b412017-03-29 17:29:06 -0700269 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700270 buildDir := android.PathForOutput(ctx).String()
271 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700272
273 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
274 // to be replaced later by ninja_strings.go
Jeff Gaston02a684b2017-10-27 14:59:27 -0700275 depfilePlaceholder := ""
Nan Zhangea568a42017-11-08 21:20:04 -0800276 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700277 depfilePlaceholder = "$depfileArgs"
278 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800279
Jeff Gaston437d23c2017-11-08 12:38:00 -0800280 genDir := android.PathForModuleGen(ctx)
281 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 -0700282
Colin Cross33bfb0a2016-11-21 17:23:08 -0800283 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700284 Command: sandboxCommand,
285 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800286 }
Colin Cross15e86d92017-10-20 15:07:08 -0700287 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800288 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800289 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700290 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800291 }
292 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700293
Jeff Gaston437d23c2017-11-08 12:38:00 -0800294 g.generateSourceFile(ctx, task)
295
Colin Crossd350ecd2015-04-28 13:25:36 -0700296}
297
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700298func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700299 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700300 if len(task.out) == 0 {
301 ctx.ModuleErrorf("must have at least one output file")
302 return
303 }
Colin Cross67a5c132017-05-09 13:45:28 -0700304 if len(task.out) == 1 {
305 desc += " " + task.out[0].Base()
306 }
307
Jeff Gaston02a684b2017-10-27 14:59:27 -0700308 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800309 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700310 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
311 }
312
Colin Crossae887032017-10-23 17:16:14 -0700313 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700314 Rule: g.rule,
315 Description: "generate",
316 Output: task.out[0],
317 ImplicitOutputs: task.out[1:],
318 Inputs: task.in,
319 Implicits: g.deps,
320 Args: map[string]string{
321 "allouts": strings.Join(task.out.Strings(), " "),
322 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800323 }
Nan Zhangea568a42017-11-08 21:20:04 -0800324 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700325 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
326 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800327 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700328
Colin Crossae887032017-10-23 17:16:14 -0700329 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700330
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700331 for _, outputFile := range task.out {
332 g.outputFiles = append(g.outputFiles, outputFile)
333 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700334}
335
Jeff Gaston437d23c2017-11-08 12:38:00 -0800336func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700337 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800338 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700339 }
340
Colin Cross36242852017-06-23 15:06:31 -0700341 module.AddProperties(props...)
342 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700343
Colin Cross36242852017-06-23 15:06:31 -0700344 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700345}
346
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700347func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700348 properties := &genSrcsProperties{}
349
Jeff Gaston437d23c2017-11-08 12:38:00 -0800350 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
351 commands := []string{}
352 outFiles := android.WritablePaths{}
353 genPath := android.PathForModuleGen(ctx).String()
Colin Crossd350ecd2015-04-28 13:25:36 -0700354 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800355 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800356 outFiles = append(outFiles, outFile)
357
358 // replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
359 relOut, err := filepath.Rel(genPath, outFile.String())
360 if err != nil {
361 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
362 }
363 sandboxOutfile := filepath.Join("__SBOX_OUT_DIR__", relOut)
364 command, err := android.Expand(rawCommand, func(name string) (string, error) {
365 switch name {
366 case "in":
367 return in.String(), nil
368 case "out":
369 return sandboxOutfile, nil
370 default:
371 return "$(" + name + ")", nil
372 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700373 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800374 if err != nil {
375 ctx.PropertyErrorf("cmd", err.Error())
376 }
377
378 // escape the command in case for example it contains '#', an odd number of '"', etc
379 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape([]string{command})[0])
380 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700381 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800382 fullCommand := strings.Join(commands, " && ")
383
384 return generateTask{
385 in: srcFiles,
386 out: outFiles,
387 cmd: fullCommand,
388 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700389 }
390
Jeff Gaston437d23c2017-11-08 12:38:00 -0800391 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700392}
393
Colin Cross54190b32017-10-09 15:34:10 -0700394func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700395 m := NewGenSrcs()
396 android.InitAndroidModule(m)
397 return m
398}
399
Colin Crossd350ecd2015-04-28 13:25:36 -0700400type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700401 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800402 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700403}
404
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700405func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700406 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700407
Jeff Gaston437d23c2017-11-08 12:38:00 -0800408 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700409 outs := make(android.WritablePaths, len(properties.Out))
410 for i, out := range properties.Out {
411 outs[i] = android.PathForModuleGen(ctx, out)
412 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800413 return generateTask{
414 in: srcFiles,
415 out: outs,
416 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700417 }
Colin Cross5049f022015-03-18 13:28:46 -0700418 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700419
Jeff Gaston437d23c2017-11-08 12:38:00 -0800420 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700421}
422
Colin Cross54190b32017-10-09 15:34:10 -0700423func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700424 m := NewGenRule()
425 android.InitAndroidModule(m)
426 return m
427}
428
Colin Crossd350ecd2015-04-28 13:25:36 -0700429type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700430 // names of the output files that will be generated
431 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700432}
Nan Zhangea568a42017-11-08 21:20:04 -0800433
434var Bool = proptools.Bool
435var String = proptools.String