blob: 2824e4907672be61ead07e48a1eeecfa3fb77dd4 [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
Colin Cross08f15ab2018-10-04 23:29:14 -070055 label string
Dan Willemsend6ba0d52017-09-13 15:46:47 -070056}
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
Colin Cross08f15ab2018-10-04 23:29:14 -070065 // $(location <label>): the path to the tool, tool_file, input or output with name <label>
Colin Cross2296f5b2017-10-17 21:38:14 -070066 // $(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 Cross2a076922018-10-04 23:28:25 -0700104 deps android.Paths
105 rule blueprint.Rule
106 rawCommand string
Colin Crossd350ecd2015-04-28 13:25:36 -0700107
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 Cross08f15ab2018-10-04 23:29:14 -0700143 for _, tool := range g.properties.Tools {
144 tag := hostToolDependencyTag{label: tool}
145 if m := android.SrcIsModule(tool); m != "" {
146 tool = m
147 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800148 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700149 {Mutator: "arch", Variation: ctx.Config().BuildOsVariant},
Colin Cross08f15ab2018-10-04 23:29:14 -0700150 }, tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700151 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700152 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700153}
154
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700155func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross5ed99c62016-11-22 12:55:55 -0800156 if len(g.properties.Export_include_dirs) > 0 {
157 for _, dir := range g.properties.Export_include_dirs {
158 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
159 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
160 }
161 } else {
162 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
163 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700164
Colin Cross08f15ab2018-10-04 23:29:14 -0700165 locationLabels := map[string][]string{}
166 firstLabel := ""
167
168 addLocationLabel := func(label string, paths []string) {
169 if firstLabel == "" {
170 firstLabel = label
171 }
172 if _, exists := locationLabels[label]; !exists {
173 locationLabels[label] = paths
174 } else {
175 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
176 label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
177 }
178 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700179
Colin Cross6f080df2016-11-04 15:32:58 -0700180 if len(g.properties.Tools) > 0 {
Colin Cross35143d02017-11-16 00:11:20 -0800181 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700182 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
183 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700184 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700185 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700186
187 if t, ok := module.(HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800188 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800189 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800190 ctx.AddMissingDependencies([]string{tool})
191 } else {
192 ctx.ModuleErrorf("depends on disabled module %q", tool)
193 }
194 break
195 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700196 path = t.HostToolPath()
197 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
198 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
199 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700200 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700201 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
202 break
Colin Cross6f080df2016-11-04 15:32:58 -0700203 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700204 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700205 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700206 break
207 }
208
209 if path.Valid() {
210 g.deps = append(g.deps, path.Path())
Colin Cross08f15ab2018-10-04 23:29:14 -0700211 addLocationLabel(tag.label, []string{path.Path().String()})
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700212 } else {
213 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700214 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700215 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700216 })
217 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700218
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700219 if ctx.Failed() {
220 return
221 }
222
Colin Cross08f15ab2018-10-04 23:29:14 -0700223 for _, toolFile := range g.properties.Tool_files {
224 paths := ctx.ExpandSources([]string{toolFile}, nil)
225 g.deps = append(g.deps, paths...)
226 addLocationLabel(toolFile, paths.Strings())
227 }
228
229 var srcFiles android.Paths
230 for _, in := range g.properties.Srcs {
231 paths := ctx.ExpandSources([]string{in}, nil)
232 srcFiles = append(srcFiles, paths...)
233 addLocationLabel(in, paths.Strings())
234 }
235
236 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
237
238 for _, out := range task.out {
239 addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
Colin Cross6f080df2016-11-04 15:32:58 -0700240 }
241
Jeff Gaston02a684b2017-10-27 14:59:27 -0700242 referencedDepfile := false
243
Jeff Gaston437d23c2017-11-08 12:38:00 -0800244 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross85a2e892018-07-09 09:45:06 -0700245 // report the error directly without returning an error to android.Expand to catch multiple errors in a
246 // single run
247 reportError := func(fmt string, args ...interface{}) (string, error) {
248 ctx.PropertyErrorf("cmd", fmt, args...)
249 return "SOONG_ERROR", nil
250 }
251
Colin Cross6f080df2016-11-04 15:32:58 -0700252 switch name {
253 case "location":
Colin Cross08f15ab2018-10-04 23:29:14 -0700254 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
Colin Cross85a2e892018-07-09 09:45:06 -0700255 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700256 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700257 paths := locationLabels[firstLabel]
258 if len(paths) == 0 {
259 return reportError("default label %q has no files", firstLabel)
260 } else if len(paths) > 1 {
261 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
262 firstLabel, firstLabel)
263 }
264 return locationLabels[firstLabel][0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700265 case "in":
266 return "${in}", nil
267 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700268 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800269 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700270 referencedDepfile = true
Nan Zhangea568a42017-11-08 21:20:04 -0800271 if !Bool(g.properties.Depfile) {
Colin Cross85a2e892018-07-09 09:45:06 -0700272 return reportError("$(depfile) used without depfile property")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800273 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700274 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700275 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800276 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700277 default:
278 if strings.HasPrefix(name, "location ") {
279 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Colin Cross08f15ab2018-10-04 23:29:14 -0700280 if paths, ok := locationLabels[label]; ok {
281 if len(paths) == 0 {
282 return reportError("label %q has no files", label)
283 } else if len(paths) > 1 {
284 return reportError("label %q has multiple files, use $(locations %s) to reference it",
285 label, label)
286 }
287 return paths[0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700288 } else {
Colin Cross85a2e892018-07-09 09:45:06 -0700289 return reportError("unknown location label %q", label)
Colin Cross6f080df2016-11-04 15:32:58 -0700290 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700291 } else if strings.HasPrefix(name, "locations ") {
292 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
293 if paths, ok := locationLabels[label]; ok {
294 if len(paths) == 0 {
295 return reportError("label %q has no files", label)
296 }
297 return strings.Join(paths, " "), nil
298 } else {
299 return reportError("unknown locations label %q", label)
300 }
301 } else {
302 return reportError("unknown variable '$(%s)'", name)
Colin Cross6f080df2016-11-04 15:32:58 -0700303 }
Colin Cross6f080df2016-11-04 15:32:58 -0700304 }
305 })
306
307 if err != nil {
308 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700309 return
Colin Cross6f080df2016-11-04 15:32:58 -0700310 }
311
Colin Cross85a2e892018-07-09 09:45:06 -0700312 if Bool(g.properties.Depfile) && !referencedDepfile {
313 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
314 }
315
Jeff Gastonefc1b412017-03-29 17:29:06 -0700316 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700317 buildDir := android.PathForOutput(ctx).String()
318 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700319
320 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
321 // to be replaced later by ninja_strings.go
Jeff Gaston02a684b2017-10-27 14:59:27 -0700322 depfilePlaceholder := ""
Nan Zhangea568a42017-11-08 21:20:04 -0800323 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700324 depfilePlaceholder = "$depfileArgs"
325 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800326
Jeff Gaston437d23c2017-11-08 12:38:00 -0800327 genDir := android.PathForModuleGen(ctx)
Jeff Gaston31603062017-12-08 12:45:35 -0800328 // Escape the command for the shell
329 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
Colin Cross2a076922018-10-04 23:28:25 -0700330 g.rawCommand = rawCommand
Jeff Gaston31603062017-12-08 12:45:35 -0800331 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
332 sandboxPath, genDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700333
Colin Cross33bfb0a2016-11-21 17:23:08 -0800334 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700335 Command: sandboxCommand,
336 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800337 }
Colin Cross15e86d92017-10-20 15:07:08 -0700338 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800339 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800340 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700341 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800342 }
343 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700344
Jeff Gaston437d23c2017-11-08 12:38:00 -0800345 g.generateSourceFile(ctx, task)
346
Colin Crossd350ecd2015-04-28 13:25:36 -0700347}
348
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700349func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700350 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700351 if len(task.out) == 0 {
352 ctx.ModuleErrorf("must have at least one output file")
353 return
354 }
Colin Cross67a5c132017-05-09 13:45:28 -0700355 if len(task.out) == 1 {
356 desc += " " + task.out[0].Base()
357 }
358
Jeff Gaston02a684b2017-10-27 14:59:27 -0700359 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800360 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700361 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
362 }
363
Colin Crossae887032017-10-23 17:16:14 -0700364 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700365 Rule: g.rule,
366 Description: "generate",
367 Output: task.out[0],
368 ImplicitOutputs: task.out[1:],
369 Inputs: task.in,
370 Implicits: g.deps,
371 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800372 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700373 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800374 }
Nan Zhangea568a42017-11-08 21:20:04 -0800375 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700376 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
377 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800378 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700379
Colin Crossae887032017-10-23 17:16:14 -0700380 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700381
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700382 for _, outputFile := range task.out {
383 g.outputFiles = append(g.outputFiles, outputFile)
384 }
Dan Willemsen9da9d492018-02-21 18:28:18 -0800385 g.outputDeps = append(g.outputDeps, task.out[0])
Colin Crossd350ecd2015-04-28 13:25:36 -0700386}
387
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700388// Collect information for opening IDE project files in java/jdeps.go.
389func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
390 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
391 for _, src := range g.properties.Srcs {
392 if strings.HasPrefix(src, ":") {
393 src = strings.Trim(src, ":")
394 dpInfo.Deps = append(dpInfo.Deps, src)
395 }
396 }
397}
398
Jeff Gaston437d23c2017-11-08 12:38:00 -0800399func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700400 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800401 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700402 }
403
Colin Cross36242852017-06-23 15:06:31 -0700404 module.AddProperties(props...)
405 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700406
Colin Cross36242852017-06-23 15:06:31 -0700407 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700408}
409
Colin Crossbaccf5b2018-02-21 14:07:48 -0800410// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
411func pathToSandboxOut(path android.Path, genDir android.Path) string {
412 relOut, err := filepath.Rel(genDir.String(), path.String())
413 if err != nil {
414 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
415 }
416 return filepath.Join("__SBOX_OUT_DIR__", relOut)
417
418}
419
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700420func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700421 properties := &genSrcsProperties{}
422
Jeff Gaston437d23c2017-11-08 12:38:00 -0800423 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
424 commands := []string{}
425 outFiles := android.WritablePaths{}
Colin Crossbaccf5b2018-02-21 14:07:48 -0800426 genDir := android.PathForModuleGen(ctx)
427 sandboxOuts := []string{}
Colin Crossd350ecd2015-04-28 13:25:36 -0700428 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800429 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800430 outFiles = append(outFiles, outFile)
431
Colin Crossbaccf5b2018-02-21 14:07:48 -0800432 sandboxOutfile := pathToSandboxOut(outFile, genDir)
433 sandboxOuts = append(sandboxOuts, sandboxOutfile)
434
Jeff Gaston437d23c2017-11-08 12:38:00 -0800435 command, err := android.Expand(rawCommand, func(name string) (string, error) {
436 switch name {
437 case "in":
438 return in.String(), nil
439 case "out":
440 return sandboxOutfile, nil
441 default:
442 return "$(" + name + ")", nil
443 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700444 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800445 if err != nil {
446 ctx.PropertyErrorf("cmd", err.Error())
447 }
448
449 // escape the command in case for example it contains '#', an odd number of '"', etc
450 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape([]string{command})[0])
451 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700452 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800453 fullCommand := strings.Join(commands, " && ")
454
455 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800456 in: srcFiles,
457 out: outFiles,
458 sandboxOuts: sandboxOuts,
459 cmd: fullCommand,
Jeff Gaston437d23c2017-11-08 12:38:00 -0800460 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700461 }
462
Jeff Gaston437d23c2017-11-08 12:38:00 -0800463 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700464}
465
Colin Cross54190b32017-10-09 15:34:10 -0700466func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700467 m := NewGenSrcs()
468 android.InitAndroidModule(m)
469 return m
470}
471
Colin Crossd350ecd2015-04-28 13:25:36 -0700472type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700473 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800474 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700475}
476
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700477func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700478 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700479
Jeff Gaston437d23c2017-11-08 12:38:00 -0800480 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700481 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800482 sandboxOuts := make([]string, len(properties.Out))
483 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700484 for i, out := range properties.Out {
485 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800486 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700487 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800488 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800489 in: srcFiles,
490 out: outs,
491 sandboxOuts: sandboxOuts,
492 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700493 }
Colin Cross5049f022015-03-18 13:28:46 -0700494 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700495
Jeff Gaston437d23c2017-11-08 12:38:00 -0800496 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700497}
498
Colin Cross54190b32017-10-09 15:34:10 -0700499func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700500 m := NewGenRule()
501 android.InitAndroidModule(m)
502 return m
503}
504
Colin Crossd350ecd2015-04-28 13:25:36 -0700505type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700506 // names of the output files that will be generated
507 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700508}
Nan Zhangea568a42017-11-08 21:20:04 -0800509
510var Bool = proptools.Bool
511var String = proptools.String