blob: e423ebccf01d210c36da0de7c6493f61db89ebbf [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 Crossbaccf5b2018-02-21 14:07:48 -0800115 in android.Paths
116 out android.WritablePaths
117 sandboxOuts []string
118 cmd string
Colin Crossd350ecd2015-04-28 13:25:36 -0700119}
120
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700121func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700122 return g.outputFiles
123}
124
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700125func (g *Module) Srcs() android.Paths {
Colin Cross068e0fe2016-12-13 15:23:47 -0800126 return g.outputFiles
127}
128
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700129func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800130 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700131}
132
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700133func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross708c4242017-01-13 18:05:49 -0800134 android.ExtractSourcesDeps(ctx, g.properties.Srcs)
Colin Crossac87c992017-12-08 18:23:13 -0800135 android.ExtractSourcesDeps(ctx, g.properties.Tool_files)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700136 if g, ok := ctx.Module().(*Module); ok {
Colin Cross6f080df2016-11-04 15:32:58 -0700137 if len(g.properties.Tools) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800138 ctx.AddFarVariationDependencies([]blueprint.Variation{
Colin Cross6510f912017-11-29 00:27:14 -0800139 {"arch", ctx.Config().BuildOsVariant},
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700140 }, hostToolDepTag, g.properties.Tools...)
Colin Cross6362e272015-10-29 15:25:03 -0700141 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700142 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700143}
144
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700145func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross5ed99c62016-11-22 12:55:55 -0800146 if len(g.properties.Export_include_dirs) > 0 {
147 for _, dir := range g.properties.Export_include_dirs {
148 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
149 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
150 }
151 } else {
152 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
153 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700154
Colin Cross6f080df2016-11-04 15:32:58 -0700155 tools := map[string]android.Path{}
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700156
Colin Cross6f080df2016-11-04 15:32:58 -0700157 if len(g.properties.Tools) > 0 {
Colin Cross35143d02017-11-16 00:11:20 -0800158 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700159 switch ctx.OtherModuleDependencyTag(module) {
160 case android.SourceDepTag:
161 // Nothing to do
162 case hostToolDepTag:
163 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700164 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700165
166 if t, ok := module.(HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800167 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800168 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800169 ctx.AddMissingDependencies([]string{tool})
170 } else {
171 ctx.ModuleErrorf("depends on disabled module %q", tool)
172 }
173 break
174 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700175 path = t.HostToolPath()
176 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
177 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
178 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700179 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700180 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
181 break
Colin Cross6f080df2016-11-04 15:32:58 -0700182 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700183 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700184 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700185 break
186 }
187
188 if path.Valid() {
189 g.deps = append(g.deps, path.Path())
190 if _, exists := tools[tool]; !exists {
191 tools[tool] = path.Path()
192 } else {
193 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], path.Path().String())
194 }
195 } else {
196 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700197 }
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700198 default:
199 ctx.ModuleErrorf("unknown dependency on %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700200 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700201 })
202 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700203
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700204 if ctx.Failed() {
205 return
206 }
207
Colin Crossc3315992017-12-08 19:12:36 -0800208 toolFiles := ctx.ExpandSources(g.properties.Tool_files, nil)
Colin Crossac87c992017-12-08 18:23:13 -0800209 for _, tool := range toolFiles {
210 g.deps = append(g.deps, tool)
211 if _, exists := tools[tool.Rel()]; !exists {
212 tools[tool.Rel()] = tool
Colin Cross6f080df2016-11-04 15:32:58 -0700213 } else {
Colin Crossac87c992017-12-08 18:23:13 -0800214 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool.Rel()], tool.Rel())
Colin Cross6f080df2016-11-04 15:32:58 -0700215 }
216 }
217
Jeff Gaston02a684b2017-10-27 14:59:27 -0700218 referencedDepfile := false
219
Jeff Gaston437d23c2017-11-08 12:38:00 -0800220 srcFiles := ctx.ExpandSources(g.properties.Srcs, nil)
Nan Zhangea568a42017-11-08 21:20:04 -0800221 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800222
Jeff Gaston437d23c2017-11-08 12:38:00 -0800223 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross6f080df2016-11-04 15:32:58 -0700224 switch name {
225 case "location":
Colin Crossc3315992017-12-08 19:12:36 -0800226 if len(g.properties.Tools) == 0 && len(toolFiles) == 0 {
227 return "", fmt.Errorf("at least one `tools` or `tool_files` is required if $(location) is used")
228 }
229
Colin Cross6f080df2016-11-04 15:32:58 -0700230 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)
Jeff Gaston31603062017-12-08 12:45:35 -0800281 // Escape the command for the shell
282 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
283 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
284 sandboxPath, genDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700285
Colin Cross33bfb0a2016-11-21 17:23:08 -0800286 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700287 Command: sandboxCommand,
288 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800289 }
Colin Cross15e86d92017-10-20 15:07:08 -0700290 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800291 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800292 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700293 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800294 }
295 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700296
Jeff Gaston437d23c2017-11-08 12:38:00 -0800297 g.generateSourceFile(ctx, task)
298
Colin Crossd350ecd2015-04-28 13:25:36 -0700299}
300
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700301func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700302 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700303 if len(task.out) == 0 {
304 ctx.ModuleErrorf("must have at least one output file")
305 return
306 }
Colin Cross67a5c132017-05-09 13:45:28 -0700307 if len(task.out) == 1 {
308 desc += " " + task.out[0].Base()
309 }
310
Jeff Gaston02a684b2017-10-27 14:59:27 -0700311 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800312 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700313 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
314 }
315
Colin Crossae887032017-10-23 17:16:14 -0700316 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700317 Rule: g.rule,
318 Description: "generate",
319 Output: task.out[0],
320 ImplicitOutputs: task.out[1:],
321 Inputs: task.in,
322 Implicits: g.deps,
323 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800324 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700325 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800326 }
Nan Zhangea568a42017-11-08 21:20:04 -0800327 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700328 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
329 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800330 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700331
Colin Crossae887032017-10-23 17:16:14 -0700332 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700333
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700334 for _, outputFile := range task.out {
335 g.outputFiles = append(g.outputFiles, outputFile)
336 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700337}
338
Jeff Gaston437d23c2017-11-08 12:38:00 -0800339func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700340 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800341 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700342 }
343
Colin Cross36242852017-06-23 15:06:31 -0700344 module.AddProperties(props...)
345 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700346
Colin Cross36242852017-06-23 15:06:31 -0700347 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700348}
349
Colin Crossbaccf5b2018-02-21 14:07:48 -0800350// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
351func pathToSandboxOut(path android.Path, genDir android.Path) string {
352 relOut, err := filepath.Rel(genDir.String(), path.String())
353 if err != nil {
354 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
355 }
356 return filepath.Join("__SBOX_OUT_DIR__", relOut)
357
358}
359
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700360func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700361 properties := &genSrcsProperties{}
362
Jeff Gaston437d23c2017-11-08 12:38:00 -0800363 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
364 commands := []string{}
365 outFiles := android.WritablePaths{}
Colin Crossbaccf5b2018-02-21 14:07:48 -0800366 genDir := android.PathForModuleGen(ctx)
367 sandboxOuts := []string{}
Colin Crossd350ecd2015-04-28 13:25:36 -0700368 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800369 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800370 outFiles = append(outFiles, outFile)
371
Colin Crossbaccf5b2018-02-21 14:07:48 -0800372 sandboxOutfile := pathToSandboxOut(outFile, genDir)
373 sandboxOuts = append(sandboxOuts, sandboxOutfile)
374
Jeff Gaston437d23c2017-11-08 12:38:00 -0800375 command, err := android.Expand(rawCommand, func(name string) (string, error) {
376 switch name {
377 case "in":
378 return in.String(), nil
379 case "out":
380 return sandboxOutfile, nil
381 default:
382 return "$(" + name + ")", nil
383 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700384 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800385 if err != nil {
386 ctx.PropertyErrorf("cmd", err.Error())
387 }
388
389 // escape the command in case for example it contains '#', an odd number of '"', etc
390 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape([]string{command})[0])
391 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700392 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800393 fullCommand := strings.Join(commands, " && ")
394
395 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800396 in: srcFiles,
397 out: outFiles,
398 sandboxOuts: sandboxOuts,
399 cmd: fullCommand,
Jeff Gaston437d23c2017-11-08 12:38:00 -0800400 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700401 }
402
Jeff Gaston437d23c2017-11-08 12:38:00 -0800403 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700404}
405
Colin Cross54190b32017-10-09 15:34:10 -0700406func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700407 m := NewGenSrcs()
408 android.InitAndroidModule(m)
409 return m
410}
411
Colin Crossd350ecd2015-04-28 13:25:36 -0700412type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700413 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800414 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700415}
416
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700417func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700418 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700419
Jeff Gaston437d23c2017-11-08 12:38:00 -0800420 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700421 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800422 sandboxOuts := make([]string, len(properties.Out))
423 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700424 for i, out := range properties.Out {
425 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800426 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700427 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800428 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800429 in: srcFiles,
430 out: outs,
431 sandboxOuts: sandboxOuts,
432 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700433 }
Colin Cross5049f022015-03-18 13:28:46 -0700434 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700435
Jeff Gaston437d23c2017-11-08 12:38:00 -0800436 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700437}
438
Colin Cross54190b32017-10-09 15:34:10 -0700439func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700440 m := NewGenRule()
441 android.InitAndroidModule(m)
442 return m
443}
444
Colin Crossd350ecd2015-04-28 13:25:36 -0700445type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700446 // names of the output files that will be generated
447 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700448}
Nan Zhangea568a42017-11-08 21:20:04 -0800449
450var Bool = proptools.Bool
451var String = proptools.String