blob: 03d4ea6fdd80fd5801d716179939a571ce088cbe [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) {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700166 case hostToolDepTag:
167 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700168 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700169
170 if t, ok := module.(HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800171 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800172 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800173 ctx.AddMissingDependencies([]string{tool})
174 } else {
175 ctx.ModuleErrorf("depends on disabled module %q", tool)
176 }
177 break
178 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700179 path = t.HostToolPath()
180 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
181 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
182 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700183 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700184 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
185 break
Colin Cross6f080df2016-11-04 15:32:58 -0700186 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700187 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700188 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700189 break
190 }
191
192 if path.Valid() {
193 g.deps = append(g.deps, path.Path())
194 if _, exists := tools[tool]; !exists {
195 tools[tool] = path.Path()
196 } else {
197 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], path.Path().String())
198 }
199 } else {
200 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700201 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700202 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700203 })
204 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700205
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700206 if ctx.Failed() {
207 return
208 }
209
Colin Crossc3315992017-12-08 19:12:36 -0800210 toolFiles := ctx.ExpandSources(g.properties.Tool_files, nil)
Colin Crossac87c992017-12-08 18:23:13 -0800211 for _, tool := range toolFiles {
212 g.deps = append(g.deps, tool)
213 if _, exists := tools[tool.Rel()]; !exists {
214 tools[tool.Rel()] = tool
Colin Cross6f080df2016-11-04 15:32:58 -0700215 } else {
Colin Crossac87c992017-12-08 18:23:13 -0800216 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool.Rel()], tool.Rel())
Colin Cross6f080df2016-11-04 15:32:58 -0700217 }
218 }
219
Jeff Gaston02a684b2017-10-27 14:59:27 -0700220 referencedDepfile := false
221
Jeff Gaston437d23c2017-11-08 12:38:00 -0800222 srcFiles := ctx.ExpandSources(g.properties.Srcs, nil)
Nan Zhangea568a42017-11-08 21:20:04 -0800223 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800224
Jeff Gaston437d23c2017-11-08 12:38:00 -0800225 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross85a2e892018-07-09 09:45:06 -0700226 // report the error directly without returning an error to android.Expand to catch multiple errors in a
227 // single run
228 reportError := func(fmt string, args ...interface{}) (string, error) {
229 ctx.PropertyErrorf("cmd", fmt, args...)
230 return "SOONG_ERROR", nil
231 }
232
Colin Cross6f080df2016-11-04 15:32:58 -0700233 switch name {
234 case "location":
Colin Crossc3315992017-12-08 19:12:36 -0800235 if len(g.properties.Tools) == 0 && len(toolFiles) == 0 {
Colin Cross85a2e892018-07-09 09:45:06 -0700236 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
237 } else if len(g.properties.Tools) > 0 {
Colin Cross6f080df2016-11-04 15:32:58 -0700238 return tools[g.properties.Tools[0]].String(), nil
239 } else {
Colin Crossac87c992017-12-08 18:23:13 -0800240 return tools[toolFiles[0].Rel()].String(), nil
Colin Cross6f080df2016-11-04 15:32:58 -0700241 }
242 case "in":
243 return "${in}", nil
244 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700245 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800246 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700247 referencedDepfile = true
Nan Zhangea568a42017-11-08 21:20:04 -0800248 if !Bool(g.properties.Depfile) {
Colin Cross85a2e892018-07-09 09:45:06 -0700249 return reportError("$(depfile) used without depfile property")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800250 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700251 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700252 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800253 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700254 default:
255 if strings.HasPrefix(name, "location ") {
256 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
257 if tool, ok := tools[label]; ok {
258 return tool.String(), nil
259 } else {
Colin Cross85a2e892018-07-09 09:45:06 -0700260 return reportError("unknown location label %q", label)
Colin Cross6f080df2016-11-04 15:32:58 -0700261 }
262 }
Colin Cross85a2e892018-07-09 09:45:06 -0700263 return reportError("unknown variable '$(%s)'", name)
Colin Cross6f080df2016-11-04 15:32:58 -0700264 }
265 })
266
267 if err != nil {
268 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700269 return
Colin Cross6f080df2016-11-04 15:32:58 -0700270 }
271
Colin Cross85a2e892018-07-09 09:45:06 -0700272 if Bool(g.properties.Depfile) && !referencedDepfile {
273 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
274 }
275
Jeff Gastonefc1b412017-03-29 17:29:06 -0700276 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700277 buildDir := android.PathForOutput(ctx).String()
278 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700279
280 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
281 // to be replaced later by ninja_strings.go
Jeff Gaston02a684b2017-10-27 14:59:27 -0700282 depfilePlaceholder := ""
Nan Zhangea568a42017-11-08 21:20:04 -0800283 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700284 depfilePlaceholder = "$depfileArgs"
285 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800286
Jeff Gaston437d23c2017-11-08 12:38:00 -0800287 genDir := android.PathForModuleGen(ctx)
Jeff Gaston31603062017-12-08 12:45:35 -0800288 // Escape the command for the shell
289 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
290 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
291 sandboxPath, genDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700292
Colin Cross33bfb0a2016-11-21 17:23:08 -0800293 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700294 Command: sandboxCommand,
295 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800296 }
Colin Cross15e86d92017-10-20 15:07:08 -0700297 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800298 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800299 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700300 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800301 }
302 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700303
Jeff Gaston437d23c2017-11-08 12:38:00 -0800304 g.generateSourceFile(ctx, task)
305
Colin Crossd350ecd2015-04-28 13:25:36 -0700306}
307
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700308func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700309 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700310 if len(task.out) == 0 {
311 ctx.ModuleErrorf("must have at least one output file")
312 return
313 }
Colin Cross67a5c132017-05-09 13:45:28 -0700314 if len(task.out) == 1 {
315 desc += " " + task.out[0].Base()
316 }
317
Jeff Gaston02a684b2017-10-27 14:59:27 -0700318 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800319 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700320 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
321 }
322
Colin Crossae887032017-10-23 17:16:14 -0700323 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700324 Rule: g.rule,
325 Description: "generate",
326 Output: task.out[0],
327 ImplicitOutputs: task.out[1:],
328 Inputs: task.in,
329 Implicits: g.deps,
330 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800331 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700332 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800333 }
Nan Zhangea568a42017-11-08 21:20:04 -0800334 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700335 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
336 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800337 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700338
Colin Crossae887032017-10-23 17:16:14 -0700339 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700340
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700341 for _, outputFile := range task.out {
342 g.outputFiles = append(g.outputFiles, outputFile)
343 }
Dan Willemsen9da9d492018-02-21 18:28:18 -0800344 g.outputDeps = append(g.outputDeps, task.out[0])
Colin Crossd350ecd2015-04-28 13:25:36 -0700345}
346
Jeff Gaston437d23c2017-11-08 12:38:00 -0800347func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700348 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800349 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700350 }
351
Colin Cross36242852017-06-23 15:06:31 -0700352 module.AddProperties(props...)
353 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700354
Colin Cross36242852017-06-23 15:06:31 -0700355 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700356}
357
Colin Crossbaccf5b2018-02-21 14:07:48 -0800358// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
359func pathToSandboxOut(path android.Path, genDir android.Path) string {
360 relOut, err := filepath.Rel(genDir.String(), path.String())
361 if err != nil {
362 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
363 }
364 return filepath.Join("__SBOX_OUT_DIR__", relOut)
365
366}
367
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700368func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700369 properties := &genSrcsProperties{}
370
Jeff Gaston437d23c2017-11-08 12:38:00 -0800371 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
372 commands := []string{}
373 outFiles := android.WritablePaths{}
Colin Crossbaccf5b2018-02-21 14:07:48 -0800374 genDir := android.PathForModuleGen(ctx)
375 sandboxOuts := []string{}
Colin Crossd350ecd2015-04-28 13:25:36 -0700376 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800377 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800378 outFiles = append(outFiles, outFile)
379
Colin Crossbaccf5b2018-02-21 14:07:48 -0800380 sandboxOutfile := pathToSandboxOut(outFile, genDir)
381 sandboxOuts = append(sandboxOuts, sandboxOutfile)
382
Jeff Gaston437d23c2017-11-08 12:38:00 -0800383 command, err := android.Expand(rawCommand, func(name string) (string, error) {
384 switch name {
385 case "in":
386 return in.String(), nil
387 case "out":
388 return sandboxOutfile, nil
389 default:
390 return "$(" + name + ")", nil
391 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700392 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800393 if err != nil {
394 ctx.PropertyErrorf("cmd", err.Error())
395 }
396
397 // escape the command in case for example it contains '#', an odd number of '"', etc
398 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape([]string{command})[0])
399 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700400 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800401 fullCommand := strings.Join(commands, " && ")
402
403 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800404 in: srcFiles,
405 out: outFiles,
406 sandboxOuts: sandboxOuts,
407 cmd: fullCommand,
Jeff Gaston437d23c2017-11-08 12:38:00 -0800408 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700409 }
410
Jeff Gaston437d23c2017-11-08 12:38:00 -0800411 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700412}
413
Colin Cross54190b32017-10-09 15:34:10 -0700414func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700415 m := NewGenSrcs()
416 android.InitAndroidModule(m)
417 return m
418}
419
Colin Crossd350ecd2015-04-28 13:25:36 -0700420type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700421 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800422 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700423}
424
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700425func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700426 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700427
Jeff Gaston437d23c2017-11-08 12:38:00 -0800428 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700429 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800430 sandboxOuts := make([]string, len(properties.Out))
431 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700432 for i, out := range properties.Out {
433 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800434 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700435 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800436 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800437 in: srcFiles,
438 out: outs,
439 sandboxOuts: sandboxOuts,
440 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700441 }
Colin Cross5049f022015-03-18 13:28:46 -0700442 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700443
Jeff Gaston437d23c2017-11-08 12:38:00 -0800444 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700445}
446
Colin Cross54190b32017-10-09 15:34:10 -0700447func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700448 m := NewGenRule()
449 android.InitAndroidModule(m)
450 return m
451}
452
Colin Crossd350ecd2015-04-28 13:25:36 -0700453type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700454 // names of the output files that will be generated
455 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700456}
Nan Zhangea568a42017-11-08 21:20:04 -0800457
458var Bool = proptools.Bool
459var String = proptools.String