blob: f19e2aa0135471aaca014a80816d715b13973883 [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 Cross2a076922018-10-04 23:28:25 -0700105 deps android.Paths
106 rule blueprint.Rule
107 rawCommand string
Colin Crossd350ecd2015-04-28 13:25:36 -0700108
Colin Cross5ed99c62016-11-22 12:55:55 -0800109 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700110
Colin Cross635c3b02016-05-18 15:37:25 -0700111 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800112 outputDeps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700113}
114
Jeff Gaston437d23c2017-11-08 12:38:00 -0800115type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700116
117type generateTask struct {
Colin Crossbaccf5b2018-02-21 14:07:48 -0800118 in android.Paths
119 out android.WritablePaths
120 sandboxOuts []string
121 cmd string
Colin Crossd350ecd2015-04-28 13:25:36 -0700122}
123
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700124func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700125 return g.outputFiles
126}
127
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700128func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700129 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800130}
131
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700132func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800133 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700134}
135
Dan Willemsen9da9d492018-02-21 18:28:18 -0800136func (g *Module) GeneratedDeps() android.Paths {
137 return g.outputDeps
138}
139
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700140func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross708c4242017-01-13 18:05:49 -0800141 android.ExtractSourcesDeps(ctx, g.properties.Srcs)
Colin Crossac87c992017-12-08 18:23:13 -0800142 android.ExtractSourcesDeps(ctx, g.properties.Tool_files)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700143 if g, ok := ctx.Module().(*Module); ok {
Colin Cross6f080df2016-11-04 15:32:58 -0700144 if len(g.properties.Tools) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800145 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700146 {Mutator: "arch", Variation: ctx.Config().BuildOsVariant},
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700147 }, hostToolDepTag, g.properties.Tools...)
Colin Cross6362e272015-10-29 15:25:03 -0700148 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700149 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700150}
151
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700152func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross5ed99c62016-11-22 12:55:55 -0800153 if len(g.properties.Export_include_dirs) > 0 {
154 for _, dir := range g.properties.Export_include_dirs {
155 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
156 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
157 }
158 } else {
159 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
160 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700161
Colin Cross6f080df2016-11-04 15:32:58 -0700162 tools := map[string]android.Path{}
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700163
Colin Cross6f080df2016-11-04 15:32:58 -0700164 if len(g.properties.Tools) > 0 {
Colin Cross35143d02017-11-16 00:11:20 -0800165 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700166 switch ctx.OtherModuleDependencyTag(module) {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700167 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 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700203 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700204 })
205 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700206
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700207 if ctx.Failed() {
208 return
209 }
210
Colin Crossc3315992017-12-08 19:12:36 -0800211 toolFiles := ctx.ExpandSources(g.properties.Tool_files, nil)
Colin Crossac87c992017-12-08 18:23:13 -0800212 for _, tool := range toolFiles {
213 g.deps = append(g.deps, tool)
214 if _, exists := tools[tool.Rel()]; !exists {
215 tools[tool.Rel()] = tool
Colin Cross6f080df2016-11-04 15:32:58 -0700216 } else {
Colin Crossac87c992017-12-08 18:23:13 -0800217 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool.Rel()], tool.Rel())
Colin Cross6f080df2016-11-04 15:32:58 -0700218 }
219 }
220
Jeff Gaston02a684b2017-10-27 14:59:27 -0700221 referencedDepfile := false
222
Jeff Gaston437d23c2017-11-08 12:38:00 -0800223 srcFiles := ctx.ExpandSources(g.properties.Srcs, nil)
Nan Zhangea568a42017-11-08 21:20:04 -0800224 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800225
Jeff Gaston437d23c2017-11-08 12:38:00 -0800226 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross85a2e892018-07-09 09:45:06 -0700227 // report the error directly without returning an error to android.Expand to catch multiple errors in a
228 // single run
229 reportError := func(fmt string, args ...interface{}) (string, error) {
230 ctx.PropertyErrorf("cmd", fmt, args...)
231 return "SOONG_ERROR", nil
232 }
233
Colin Cross6f080df2016-11-04 15:32:58 -0700234 switch name {
235 case "location":
Colin Crossc3315992017-12-08 19:12:36 -0800236 if len(g.properties.Tools) == 0 && len(toolFiles) == 0 {
Colin Cross85a2e892018-07-09 09:45:06 -0700237 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
238 } else if len(g.properties.Tools) > 0 {
Colin Cross6f080df2016-11-04 15:32:58 -0700239 return tools[g.properties.Tools[0]].String(), nil
240 } else {
Colin Crossac87c992017-12-08 18:23:13 -0800241 return tools[toolFiles[0].Rel()].String(), nil
Colin Cross6f080df2016-11-04 15:32:58 -0700242 }
243 case "in":
244 return "${in}", nil
245 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700246 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800247 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700248 referencedDepfile = true
Nan Zhangea568a42017-11-08 21:20:04 -0800249 if !Bool(g.properties.Depfile) {
Colin Cross85a2e892018-07-09 09:45:06 -0700250 return reportError("$(depfile) used without depfile property")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800251 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700252 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700253 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800254 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700255 default:
256 if strings.HasPrefix(name, "location ") {
257 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
258 if tool, ok := tools[label]; ok {
259 return tool.String(), nil
260 } else {
Colin Cross85a2e892018-07-09 09:45:06 -0700261 return reportError("unknown location label %q", label)
Colin Cross6f080df2016-11-04 15:32:58 -0700262 }
263 }
Colin Cross85a2e892018-07-09 09:45:06 -0700264 return reportError("unknown variable '$(%s)'", name)
Colin Cross6f080df2016-11-04 15:32:58 -0700265 }
266 })
267
268 if err != nil {
269 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700270 return
Colin Cross6f080df2016-11-04 15:32:58 -0700271 }
272
Colin Cross85a2e892018-07-09 09:45:06 -0700273 if Bool(g.properties.Depfile) && !referencedDepfile {
274 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
275 }
276
Jeff Gastonefc1b412017-03-29 17:29:06 -0700277 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700278 buildDir := android.PathForOutput(ctx).String()
279 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700280
281 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
282 // to be replaced later by ninja_strings.go
Jeff Gaston02a684b2017-10-27 14:59:27 -0700283 depfilePlaceholder := ""
Nan Zhangea568a42017-11-08 21:20:04 -0800284 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700285 depfilePlaceholder = "$depfileArgs"
286 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800287
Jeff Gaston437d23c2017-11-08 12:38:00 -0800288 genDir := android.PathForModuleGen(ctx)
Jeff Gaston31603062017-12-08 12:45:35 -0800289 // Escape the command for the shell
290 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
Colin Cross2a076922018-10-04 23:28:25 -0700291 g.rawCommand = rawCommand
Jeff Gaston31603062017-12-08 12:45:35 -0800292 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
293 sandboxPath, genDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700294
Colin Cross33bfb0a2016-11-21 17:23:08 -0800295 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700296 Command: sandboxCommand,
297 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800298 }
Colin Cross15e86d92017-10-20 15:07:08 -0700299 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800300 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800301 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700302 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800303 }
304 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700305
Jeff Gaston437d23c2017-11-08 12:38:00 -0800306 g.generateSourceFile(ctx, task)
307
Colin Crossd350ecd2015-04-28 13:25:36 -0700308}
309
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700310func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700311 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700312 if len(task.out) == 0 {
313 ctx.ModuleErrorf("must have at least one output file")
314 return
315 }
Colin Cross67a5c132017-05-09 13:45:28 -0700316 if len(task.out) == 1 {
317 desc += " " + task.out[0].Base()
318 }
319
Jeff Gaston02a684b2017-10-27 14:59:27 -0700320 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800321 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700322 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
323 }
324
Colin Crossae887032017-10-23 17:16:14 -0700325 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700326 Rule: g.rule,
327 Description: "generate",
328 Output: task.out[0],
329 ImplicitOutputs: task.out[1:],
330 Inputs: task.in,
331 Implicits: g.deps,
332 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800333 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700334 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800335 }
Nan Zhangea568a42017-11-08 21:20:04 -0800336 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700337 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
338 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800339 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700340
Colin Crossae887032017-10-23 17:16:14 -0700341 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700342
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700343 for _, outputFile := range task.out {
344 g.outputFiles = append(g.outputFiles, outputFile)
345 }
Dan Willemsen9da9d492018-02-21 18:28:18 -0800346 g.outputDeps = append(g.outputDeps, task.out[0])
Colin Crossd350ecd2015-04-28 13:25:36 -0700347}
348
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700349// Collect information for opening IDE project files in java/jdeps.go.
350func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
351 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
352 for _, src := range g.properties.Srcs {
353 if strings.HasPrefix(src, ":") {
354 src = strings.Trim(src, ":")
355 dpInfo.Deps = append(dpInfo.Deps, src)
356 }
357 }
358}
359
Jeff Gaston437d23c2017-11-08 12:38:00 -0800360func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700361 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800362 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700363 }
364
Colin Cross36242852017-06-23 15:06:31 -0700365 module.AddProperties(props...)
366 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700367
Colin Cross36242852017-06-23 15:06:31 -0700368 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700369}
370
Colin Crossbaccf5b2018-02-21 14:07:48 -0800371// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
372func pathToSandboxOut(path android.Path, genDir android.Path) string {
373 relOut, err := filepath.Rel(genDir.String(), path.String())
374 if err != nil {
375 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
376 }
377 return filepath.Join("__SBOX_OUT_DIR__", relOut)
378
379}
380
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700381func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700382 properties := &genSrcsProperties{}
383
Jeff Gaston437d23c2017-11-08 12:38:00 -0800384 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
385 commands := []string{}
386 outFiles := android.WritablePaths{}
Colin Crossbaccf5b2018-02-21 14:07:48 -0800387 genDir := android.PathForModuleGen(ctx)
388 sandboxOuts := []string{}
Colin Crossd350ecd2015-04-28 13:25:36 -0700389 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800390 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800391 outFiles = append(outFiles, outFile)
392
Colin Crossbaccf5b2018-02-21 14:07:48 -0800393 sandboxOutfile := pathToSandboxOut(outFile, genDir)
394 sandboxOuts = append(sandboxOuts, sandboxOutfile)
395
Jeff Gaston437d23c2017-11-08 12:38:00 -0800396 command, err := android.Expand(rawCommand, func(name string) (string, error) {
397 switch name {
398 case "in":
399 return in.String(), nil
400 case "out":
401 return sandboxOutfile, nil
402 default:
403 return "$(" + name + ")", nil
404 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700405 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800406 if err != nil {
407 ctx.PropertyErrorf("cmd", err.Error())
408 }
409
410 // escape the command in case for example it contains '#', an odd number of '"', etc
411 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape([]string{command})[0])
412 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700413 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800414 fullCommand := strings.Join(commands, " && ")
415
416 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800417 in: srcFiles,
418 out: outFiles,
419 sandboxOuts: sandboxOuts,
420 cmd: fullCommand,
Jeff Gaston437d23c2017-11-08 12:38:00 -0800421 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700422 }
423
Jeff Gaston437d23c2017-11-08 12:38:00 -0800424 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700425}
426
Colin Cross54190b32017-10-09 15:34:10 -0700427func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700428 m := NewGenSrcs()
429 android.InitAndroidModule(m)
430 return m
431}
432
Colin Crossd350ecd2015-04-28 13:25:36 -0700433type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700434 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800435 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700436}
437
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700438func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700439 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700440
Jeff Gaston437d23c2017-11-08 12:38:00 -0800441 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700442 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800443 sandboxOuts := make([]string, len(properties.Out))
444 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700445 for i, out := range properties.Out {
446 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800447 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700448 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800449 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800450 in: srcFiles,
451 out: outs,
452 sandboxOuts: sandboxOuts,
453 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700454 }
Colin Cross5049f022015-03-18 13:28:46 -0700455 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700456
Jeff Gaston437d23c2017-11-08 12:38:00 -0800457 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700458}
459
Colin Cross54190b32017-10-09 15:34:10 -0700460func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700461 m := NewGenRule()
462 android.InitAndroidModule(m)
463 return m
464}
465
Colin Crossd350ecd2015-04-28 13:25:36 -0700466type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700467 // names of the output files that will be generated
468 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700469}
Nan Zhangea568a42017-11-08 21:20:04 -0800470
471var Bool = proptools.Bool
472var String = proptools.String