blob: 42be88fe12734759d782661e3a5b6bcc3487e9aa [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
Dan Willemsen9da9d492018-02-21 18:28:18 -080046 GeneratedDeps() android.Paths
Colin Cross5049f022015-03-18 13:28:46 -070047}
48
Colin Crossd350ecd2015-04-28 13:25:36 -070049type HostToolProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -070050 HostToolPath() android.OptionalPath
Colin Crossd350ecd2015-04-28 13:25:36 -070051}
Colin Cross5049f022015-03-18 13:28:46 -070052
Dan Willemsend6ba0d52017-09-13 15:46:47 -070053type hostToolDependencyTag struct {
54 blueprint.BaseDependencyTag
55}
56
57var hostToolDepTag hostToolDependencyTag
58
Colin Cross7d5136f2015-05-11 13:39:40 -070059type generatorProperties struct {
Jeff Gastonefc1b412017-03-29 17:29:06 -070060 // The command to run on one or more input files. Cmd supports substitution of a few variables
61 // (the actual substitution is implemented in GenerateAndroidBuildActions below)
62 //
63 // Available variables for substitution:
64 //
Colin Cross2296f5b2017-10-17 21:38:14 -070065 // $(location): the path to the first entry in tools or tool_files
66 // $(location <label>): the path to the tool or tool_file with name <label>
67 // $(in): one or more input files
68 // $(out): a single output file
69 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
70 // $(genDir): the sandbox directory for this tool; contains $(out)
71 // $$: a literal $
Colin Cross6f080df2016-11-04 15:32:58 -070072 //
Jeff Gastonefc1b412017-03-29 17:29:06 -070073 // All files used must be declared as inputs (to ensure proper up-to-date checks).
74 // Use "$(in)" directly in Cmd to ensure that all inputs used are declared.
Nan Zhangea568a42017-11-08 21:20:04 -080075 Cmd *string
Colin Cross7d5136f2015-05-11 13:39:40 -070076
Colin Cross33bfb0a2016-11-21 17:23:08 -080077 // Enable reading a file containing dependencies in gcc format after the command completes
Nan Zhangea568a42017-11-08 21:20:04 -080078 Depfile *bool
Colin Cross33bfb0a2016-11-21 17:23:08 -080079
Colin Cross6f080df2016-11-04 15:32:58 -070080 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -070081 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -070082 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070083
84 // Local file that is used as the tool
Colin Cross6f080df2016-11-04 15:32:58 -070085 Tool_files []string
Colin Cross5ed99c62016-11-22 12:55:55 -080086
87 // List of directories to export generated headers from
88 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -080089
90 // list of input files
91 Srcs []string
Colin Cross7d5136f2015-05-11 13:39:40 -070092}
93
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070094type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -070095 android.ModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -070096
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070097 // For other packages to make their own genrules with extra
98 // properties
99 Extra interface{}
100
Colin Cross7d5136f2015-05-11 13:39:40 -0700101 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700102
Jeff Gaston437d23c2017-11-08 12:38:00 -0800103 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700104
Colin Cross635c3b02016-05-18 15:37:25 -0700105 deps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700106 rule blueprint.Rule
107
Colin Cross5ed99c62016-11-22 12:55:55 -0800108 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700109
Colin Cross635c3b02016-05-18 15:37:25 -0700110 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800111 outputDeps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700112}
113
Jeff Gaston437d23c2017-11-08 12:38:00 -0800114type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700115
116type generateTask struct {
Colin Crossbaccf5b2018-02-21 14:07:48 -0800117 in android.Paths
118 out android.WritablePaths
119 sandboxOuts []string
120 cmd string
Colin Crossd350ecd2015-04-28 13:25:36 -0700121}
122
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700123func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700124 return g.outputFiles
125}
126
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700127func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700128 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800129}
130
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700131func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800132 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700133}
134
Dan Willemsen9da9d492018-02-21 18:28:18 -0800135func (g *Module) GeneratedDeps() android.Paths {
136 return g.outputDeps
137}
138
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700139func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross708c4242017-01-13 18:05:49 -0800140 android.ExtractSourcesDeps(ctx, g.properties.Srcs)
Colin Crossac87c992017-12-08 18:23:13 -0800141 android.ExtractSourcesDeps(ctx, g.properties.Tool_files)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700142 if g, ok := ctx.Module().(*Module); ok {
Colin Cross6f080df2016-11-04 15:32:58 -0700143 if len(g.properties.Tools) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800144 ctx.AddFarVariationDependencies([]blueprint.Variation{
Colin Cross6510f912017-11-29 00:27:14 -0800145 {"arch", ctx.Config().BuildOsVariant},
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700146 }, hostToolDepTag, g.properties.Tools...)
Colin Cross6362e272015-10-29 15:25:03 -0700147 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700148 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700149}
150
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700151func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross5ed99c62016-11-22 12:55:55 -0800152 if len(g.properties.Export_include_dirs) > 0 {
153 for _, dir := range g.properties.Export_include_dirs {
154 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
155 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
156 }
157 } else {
158 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
159 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700160
Colin Cross6f080df2016-11-04 15:32:58 -0700161 tools := map[string]android.Path{}
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700162
Colin Cross6f080df2016-11-04 15:32:58 -0700163 if len(g.properties.Tools) > 0 {
Colin Cross35143d02017-11-16 00:11:20 -0800164 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700165 switch ctx.OtherModuleDependencyTag(module) {
166 case android.SourceDepTag:
167 // Nothing to do
168 case hostToolDepTag:
169 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700170 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700171
172 if t, ok := module.(HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800173 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800174 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800175 ctx.AddMissingDependencies([]string{tool})
176 } else {
177 ctx.ModuleErrorf("depends on disabled module %q", tool)
178 }
179 break
180 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700181 path = t.HostToolPath()
182 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
183 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
184 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700185 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700186 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
187 break
Colin Cross6f080df2016-11-04 15:32:58 -0700188 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700189 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700190 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700191 break
192 }
193
194 if path.Valid() {
195 g.deps = append(g.deps, path.Path())
196 if _, exists := tools[tool]; !exists {
197 tools[tool] = path.Path()
198 } else {
199 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], path.Path().String())
200 }
201 } else {
202 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700203 }
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700204 default:
205 ctx.ModuleErrorf("unknown dependency on %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700206 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700207 })
208 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700209
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700210 if ctx.Failed() {
211 return
212 }
213
Colin Crossc3315992017-12-08 19:12:36 -0800214 toolFiles := ctx.ExpandSources(g.properties.Tool_files, nil)
Colin Crossac87c992017-12-08 18:23:13 -0800215 for _, tool := range toolFiles {
216 g.deps = append(g.deps, tool)
217 if _, exists := tools[tool.Rel()]; !exists {
218 tools[tool.Rel()] = tool
Colin Cross6f080df2016-11-04 15:32:58 -0700219 } else {
Colin Crossac87c992017-12-08 18:23:13 -0800220 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool.Rel()], tool.Rel())
Colin Cross6f080df2016-11-04 15:32:58 -0700221 }
222 }
223
Jeff Gaston02a684b2017-10-27 14:59:27 -0700224 referencedDepfile := false
225
Jeff Gaston437d23c2017-11-08 12:38:00 -0800226 srcFiles := ctx.ExpandSources(g.properties.Srcs, nil)
Nan Zhangea568a42017-11-08 21:20:04 -0800227 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800228
Jeff Gaston437d23c2017-11-08 12:38:00 -0800229 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross6f080df2016-11-04 15:32:58 -0700230 switch name {
231 case "location":
Colin Crossc3315992017-12-08 19:12:36 -0800232 if len(g.properties.Tools) == 0 && len(toolFiles) == 0 {
233 return "", fmt.Errorf("at least one `tools` or `tool_files` is required if $(location) is used")
234 }
235
Colin Cross6f080df2016-11-04 15:32:58 -0700236 if len(g.properties.Tools) > 0 {
237 return tools[g.properties.Tools[0]].String(), nil
238 } else {
Colin Crossac87c992017-12-08 18:23:13 -0800239 return tools[toolFiles[0].Rel()].String(), nil
Colin Cross6f080df2016-11-04 15:32:58 -0700240 }
241 case "in":
242 return "${in}", nil
243 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700244 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800245 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700246 referencedDepfile = true
Nan Zhangea568a42017-11-08 21:20:04 -0800247 if !Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800248 return "", fmt.Errorf("$(depfile) used without depfile property")
249 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700250 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700251 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800252 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700253 default:
254 if strings.HasPrefix(name, "location ") {
255 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
256 if tool, ok := tools[label]; ok {
257 return tool.String(), nil
258 } else {
259 return "", fmt.Errorf("unknown location label %q", label)
260 }
261 }
262 return "", fmt.Errorf("unknown variable '$(%s)'", name)
263 }
264 })
265
Nan Zhangea568a42017-11-08 21:20:04 -0800266 if Bool(g.properties.Depfile) && !referencedDepfile {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700267 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
268 }
269
Colin Cross6f080df2016-11-04 15:32:58 -0700270 if err != nil {
271 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700272 return
Colin Cross6f080df2016-11-04 15:32:58 -0700273 }
274
Jeff Gastonefc1b412017-03-29 17:29:06 -0700275 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700276 buildDir := android.PathForOutput(ctx).String()
277 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700278
279 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
280 // to be replaced later by ninja_strings.go
Jeff Gaston02a684b2017-10-27 14:59:27 -0700281 depfilePlaceholder := ""
Nan Zhangea568a42017-11-08 21:20:04 -0800282 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700283 depfilePlaceholder = "$depfileArgs"
284 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800285
Jeff Gaston437d23c2017-11-08 12:38:00 -0800286 genDir := android.PathForModuleGen(ctx)
Jeff Gaston31603062017-12-08 12:45:35 -0800287 // Escape the command for the shell
288 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
289 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
290 sandboxPath, genDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700291
Colin Cross33bfb0a2016-11-21 17:23:08 -0800292 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700293 Command: sandboxCommand,
294 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800295 }
Colin Cross15e86d92017-10-20 15:07:08 -0700296 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800297 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800298 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700299 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800300 }
301 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700302
Jeff Gaston437d23c2017-11-08 12:38:00 -0800303 g.generateSourceFile(ctx, task)
304
Colin Crossd350ecd2015-04-28 13:25:36 -0700305}
306
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700307func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700308 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700309 if len(task.out) == 0 {
310 ctx.ModuleErrorf("must have at least one output file")
311 return
312 }
Colin Cross67a5c132017-05-09 13:45:28 -0700313 if len(task.out) == 1 {
314 desc += " " + task.out[0].Base()
315 }
316
Jeff Gaston02a684b2017-10-27 14:59:27 -0700317 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800318 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700319 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
320 }
321
Colin Crossae887032017-10-23 17:16:14 -0700322 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700323 Rule: g.rule,
324 Description: "generate",
325 Output: task.out[0],
326 ImplicitOutputs: task.out[1:],
327 Inputs: task.in,
328 Implicits: g.deps,
329 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800330 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700331 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800332 }
Nan Zhangea568a42017-11-08 21:20:04 -0800333 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700334 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
335 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800336 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700337
Colin Crossae887032017-10-23 17:16:14 -0700338 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700339
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700340 for _, outputFile := range task.out {
341 g.outputFiles = append(g.outputFiles, outputFile)
342 }
Dan Willemsen9da9d492018-02-21 18:28:18 -0800343 g.outputDeps = append(g.outputDeps, task.out[0])
Colin Crossd350ecd2015-04-28 13:25:36 -0700344}
345
Jeff Gaston437d23c2017-11-08 12:38:00 -0800346func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700347 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800348 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700349 }
350
Colin Cross36242852017-06-23 15:06:31 -0700351 module.AddProperties(props...)
352 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700353
Colin Cross36242852017-06-23 15:06:31 -0700354 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700355}
356
Colin Crossbaccf5b2018-02-21 14:07:48 -0800357// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
358func pathToSandboxOut(path android.Path, genDir android.Path) string {
359 relOut, err := filepath.Rel(genDir.String(), path.String())
360 if err != nil {
361 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
362 }
363 return filepath.Join("__SBOX_OUT_DIR__", relOut)
364
365}
366
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700367func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700368 properties := &genSrcsProperties{}
369
Jeff Gaston437d23c2017-11-08 12:38:00 -0800370 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
371 commands := []string{}
372 outFiles := android.WritablePaths{}
Colin Crossbaccf5b2018-02-21 14:07:48 -0800373 genDir := android.PathForModuleGen(ctx)
374 sandboxOuts := []string{}
Colin Crossd350ecd2015-04-28 13:25:36 -0700375 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800376 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800377 outFiles = append(outFiles, outFile)
378
Colin Crossbaccf5b2018-02-21 14:07:48 -0800379 sandboxOutfile := pathToSandboxOut(outFile, genDir)
380 sandboxOuts = append(sandboxOuts, sandboxOutfile)
381
Jeff Gaston437d23c2017-11-08 12:38:00 -0800382 command, err := android.Expand(rawCommand, func(name string) (string, error) {
383 switch name {
384 case "in":
385 return in.String(), nil
386 case "out":
387 return sandboxOutfile, nil
388 default:
389 return "$(" + name + ")", nil
390 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700391 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800392 if err != nil {
393 ctx.PropertyErrorf("cmd", err.Error())
394 }
395
396 // escape the command in case for example it contains '#', an odd number of '"', etc
397 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape([]string{command})[0])
398 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700399 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800400 fullCommand := strings.Join(commands, " && ")
401
402 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800403 in: srcFiles,
404 out: outFiles,
405 sandboxOuts: sandboxOuts,
406 cmd: fullCommand,
Jeff Gaston437d23c2017-11-08 12:38:00 -0800407 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700408 }
409
Jeff Gaston437d23c2017-11-08 12:38:00 -0800410 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700411}
412
Colin Cross54190b32017-10-09 15:34:10 -0700413func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700414 m := NewGenSrcs()
415 android.InitAndroidModule(m)
416 return m
417}
418
Colin Crossd350ecd2015-04-28 13:25:36 -0700419type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700420 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800421 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700422}
423
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700424func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700425 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700426
Jeff Gaston437d23c2017-11-08 12:38:00 -0800427 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700428 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800429 sandboxOuts := make([]string, len(properties.Out))
430 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700431 for i, out := range properties.Out {
432 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800433 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700434 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800435 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800436 in: srcFiles,
437 out: outs,
438 sandboxOuts: sandboxOuts,
439 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700440 }
Colin Cross5049f022015-03-18 13:28:46 -0700441 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700442
Jeff Gaston437d23c2017-11-08 12:38:00 -0800443 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700444}
445
Colin Cross54190b32017-10-09 15:34:10 -0700446func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700447 m := NewGenRule()
448 android.InitAndroidModule(m)
449 return m
450}
451
Colin Crossd350ecd2015-04-28 13:25:36 -0700452type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700453 // names of the output files that will be generated
454 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700455}
Nan Zhangea568a42017-11-08 21:20:04 -0800456
457var Bool = proptools.Bool
458var String = proptools.String