blob: b70c075a6b42197aecbc81316bc205d4fa7976d9 [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
Colin Crossef354482018-10-23 11:27:50 -070090 Srcs []string `android:"arch_variant"`
Dan Willemseneefa0262018-11-17 14:01:18 -080091
92 // input files to exclude
93 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070094}
95
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070096type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -070097 android.ModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -070098
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070099 // For other packages to make their own genrules with extra
100 // properties
101 Extra interface{}
102
Colin Cross7d5136f2015-05-11 13:39:40 -0700103 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700104
Jeff Gaston437d23c2017-11-08 12:38:00 -0800105 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700106
Colin Cross2a076922018-10-04 23:28:25 -0700107 deps android.Paths
108 rule blueprint.Rule
109 rawCommand string
Colin Crossd350ecd2015-04-28 13:25:36 -0700110
Colin Cross5ed99c62016-11-22 12:55:55 -0800111 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700112
Colin Cross635c3b02016-05-18 15:37:25 -0700113 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800114 outputDeps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700115}
116
Jeff Gaston437d23c2017-11-08 12:38:00 -0800117type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700118
119type generateTask struct {
Colin Crossbaccf5b2018-02-21 14:07:48 -0800120 in android.Paths
121 out android.WritablePaths
122 sandboxOuts []string
123 cmd string
Colin Crossd350ecd2015-04-28 13:25:36 -0700124}
125
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700126func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700127 return g.outputFiles
128}
129
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700130func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700131 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800132}
133
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700134func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800135 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700136}
137
Dan Willemsen9da9d492018-02-21 18:28:18 -0800138func (g *Module) GeneratedDeps() android.Paths {
139 return g.outputDeps
140}
141
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700142func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross708c4242017-01-13 18:05:49 -0800143 android.ExtractSourcesDeps(ctx, g.properties.Srcs)
Colin Crossac87c992017-12-08 18:23:13 -0800144 android.ExtractSourcesDeps(ctx, g.properties.Tool_files)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700145 if g, ok := ctx.Module().(*Module); ok {
Colin Cross08f15ab2018-10-04 23:29:14 -0700146 for _, tool := range g.properties.Tools {
147 tag := hostToolDependencyTag{label: tool}
148 if m := android.SrcIsModule(tool); m != "" {
149 tool = m
150 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800151 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700152 {Mutator: "arch", Variation: ctx.Config().BuildOsVariant},
Colin Cross08f15ab2018-10-04 23:29:14 -0700153 }, tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700154 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700155 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700156}
157
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700158func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross5ed99c62016-11-22 12:55:55 -0800159 if len(g.properties.Export_include_dirs) > 0 {
160 for _, dir := range g.properties.Export_include_dirs {
161 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
162 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
163 }
164 } else {
165 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
166 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700167
Colin Cross08f15ab2018-10-04 23:29:14 -0700168 locationLabels := map[string][]string{}
169 firstLabel := ""
170
171 addLocationLabel := func(label string, paths []string) {
172 if firstLabel == "" {
173 firstLabel = label
174 }
175 if _, exists := locationLabels[label]; !exists {
176 locationLabels[label] = paths
177 } else {
178 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
179 label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
180 }
181 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700182
Colin Cross6f080df2016-11-04 15:32:58 -0700183 if len(g.properties.Tools) > 0 {
Colin Cross35143d02017-11-16 00:11:20 -0800184 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700185 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
186 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700187 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700188 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700189
190 if t, ok := module.(HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800191 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800192 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800193 ctx.AddMissingDependencies([]string{tool})
194 } else {
195 ctx.ModuleErrorf("depends on disabled module %q", tool)
196 }
197 break
198 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700199 path = t.HostToolPath()
200 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
201 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
202 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700203 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700204 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
205 break
Colin Cross6f080df2016-11-04 15:32:58 -0700206 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700207 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700208 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700209 break
210 }
211
212 if path.Valid() {
213 g.deps = append(g.deps, path.Path())
Colin Cross08f15ab2018-10-04 23:29:14 -0700214 addLocationLabel(tag.label, []string{path.Path().String()})
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700215 } else {
216 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700217 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700218 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700219 })
220 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700221
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700222 if ctx.Failed() {
223 return
224 }
225
Colin Cross08f15ab2018-10-04 23:29:14 -0700226 for _, toolFile := range g.properties.Tool_files {
227 paths := ctx.ExpandSources([]string{toolFile}, nil)
228 g.deps = append(g.deps, paths...)
229 addLocationLabel(toolFile, paths.Strings())
230 }
231
232 var srcFiles android.Paths
233 for _, in := range g.properties.Srcs {
Dan Willemseneefa0262018-11-17 14:01:18 -0800234 paths := ctx.ExpandSources([]string{in}, g.properties.Exclude_srcs)
Colin Cross08f15ab2018-10-04 23:29:14 -0700235 srcFiles = append(srcFiles, paths...)
236 addLocationLabel(in, paths.Strings())
237 }
238
239 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
240
241 for _, out := range task.out {
242 addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
Colin Cross6f080df2016-11-04 15:32:58 -0700243 }
244
Jeff Gaston02a684b2017-10-27 14:59:27 -0700245 referencedDepfile := false
246
Jeff Gaston437d23c2017-11-08 12:38:00 -0800247 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross85a2e892018-07-09 09:45:06 -0700248 // report the error directly without returning an error to android.Expand to catch multiple errors in a
249 // single run
250 reportError := func(fmt string, args ...interface{}) (string, error) {
251 ctx.PropertyErrorf("cmd", fmt, args...)
252 return "SOONG_ERROR", nil
253 }
254
Colin Cross6f080df2016-11-04 15:32:58 -0700255 switch name {
256 case "location":
Colin Cross08f15ab2018-10-04 23:29:14 -0700257 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
Colin Cross85a2e892018-07-09 09:45:06 -0700258 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700259 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700260 paths := locationLabels[firstLabel]
261 if len(paths) == 0 {
262 return reportError("default label %q has no files", firstLabel)
263 } else if len(paths) > 1 {
264 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
265 firstLabel, firstLabel)
266 }
267 return locationLabels[firstLabel][0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700268 case "in":
269 return "${in}", nil
270 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700271 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800272 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700273 referencedDepfile = true
Nan Zhangea568a42017-11-08 21:20:04 -0800274 if !Bool(g.properties.Depfile) {
Colin Cross85a2e892018-07-09 09:45:06 -0700275 return reportError("$(depfile) used without depfile property")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800276 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700277 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700278 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800279 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700280 default:
281 if strings.HasPrefix(name, "location ") {
282 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Colin Cross08f15ab2018-10-04 23:29:14 -0700283 if paths, ok := locationLabels[label]; ok {
284 if len(paths) == 0 {
285 return reportError("label %q has no files", label)
286 } else if len(paths) > 1 {
287 return reportError("label %q has multiple files, use $(locations %s) to reference it",
288 label, label)
289 }
290 return paths[0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700291 } else {
Colin Cross85a2e892018-07-09 09:45:06 -0700292 return reportError("unknown location label %q", label)
Colin Cross6f080df2016-11-04 15:32:58 -0700293 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700294 } else if strings.HasPrefix(name, "locations ") {
295 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
296 if paths, ok := locationLabels[label]; ok {
297 if len(paths) == 0 {
298 return reportError("label %q has no files", label)
299 }
300 return strings.Join(paths, " "), nil
301 } else {
302 return reportError("unknown locations label %q", label)
303 }
304 } else {
305 return reportError("unknown variable '$(%s)'", name)
Colin Cross6f080df2016-11-04 15:32:58 -0700306 }
Colin Cross6f080df2016-11-04 15:32:58 -0700307 }
308 })
309
310 if err != nil {
311 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700312 return
Colin Cross6f080df2016-11-04 15:32:58 -0700313 }
314
Colin Cross85a2e892018-07-09 09:45:06 -0700315 if Bool(g.properties.Depfile) && !referencedDepfile {
316 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
317 }
318
Jeff Gastonefc1b412017-03-29 17:29:06 -0700319 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700320 buildDir := android.PathForOutput(ctx).String()
321 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700322
323 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
324 // to be replaced later by ninja_strings.go
Jeff Gaston02a684b2017-10-27 14:59:27 -0700325 depfilePlaceholder := ""
Nan Zhangea568a42017-11-08 21:20:04 -0800326 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700327 depfilePlaceholder = "$depfileArgs"
328 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800329
Jeff Gaston437d23c2017-11-08 12:38:00 -0800330 genDir := android.PathForModuleGen(ctx)
Jeff Gaston31603062017-12-08 12:45:35 -0800331 // Escape the command for the shell
332 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
Colin Cross2a076922018-10-04 23:28:25 -0700333 g.rawCommand = rawCommand
Jeff Gaston31603062017-12-08 12:45:35 -0800334 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
335 sandboxPath, genDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700336
Colin Cross33bfb0a2016-11-21 17:23:08 -0800337 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700338 Command: sandboxCommand,
339 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800340 }
Colin Cross15e86d92017-10-20 15:07:08 -0700341 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800342 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800343 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700344 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800345 }
346 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700347
Jeff Gaston437d23c2017-11-08 12:38:00 -0800348 g.generateSourceFile(ctx, task)
349
Colin Crossd350ecd2015-04-28 13:25:36 -0700350}
351
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700352func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700353 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700354 if len(task.out) == 0 {
355 ctx.ModuleErrorf("must have at least one output file")
356 return
357 }
Colin Cross67a5c132017-05-09 13:45:28 -0700358 if len(task.out) == 1 {
359 desc += " " + task.out[0].Base()
360 }
361
Jeff Gaston02a684b2017-10-27 14:59:27 -0700362 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800363 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700364 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
365 }
366
Colin Crossae887032017-10-23 17:16:14 -0700367 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700368 Rule: g.rule,
369 Description: "generate",
370 Output: task.out[0],
371 ImplicitOutputs: task.out[1:],
372 Inputs: task.in,
373 Implicits: g.deps,
374 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800375 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700376 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800377 }
Nan Zhangea568a42017-11-08 21:20:04 -0800378 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700379 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
380 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800381 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700382
Colin Crossae887032017-10-23 17:16:14 -0700383 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700384
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700385 for _, outputFile := range task.out {
386 g.outputFiles = append(g.outputFiles, outputFile)
387 }
Dan Willemsen9da9d492018-02-21 18:28:18 -0800388 g.outputDeps = append(g.outputDeps, task.out[0])
Colin Crossd350ecd2015-04-28 13:25:36 -0700389}
390
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700391// Collect information for opening IDE project files in java/jdeps.go.
392func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
393 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
394 for _, src := range g.properties.Srcs {
395 if strings.HasPrefix(src, ":") {
396 src = strings.Trim(src, ":")
397 dpInfo.Deps = append(dpInfo.Deps, src)
398 }
399 }
400}
401
Jeff Gaston437d23c2017-11-08 12:38:00 -0800402func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700403 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800404 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700405 }
406
Colin Cross36242852017-06-23 15:06:31 -0700407 module.AddProperties(props...)
408 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700409
Colin Cross36242852017-06-23 15:06:31 -0700410 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700411}
412
Colin Crossbaccf5b2018-02-21 14:07:48 -0800413// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
414func pathToSandboxOut(path android.Path, genDir android.Path) string {
415 relOut, err := filepath.Rel(genDir.String(), path.String())
416 if err != nil {
417 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
418 }
419 return filepath.Join("__SBOX_OUT_DIR__", relOut)
420
421}
422
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700423func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700424 properties := &genSrcsProperties{}
425
Jeff Gaston437d23c2017-11-08 12:38:00 -0800426 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
427 commands := []string{}
428 outFiles := android.WritablePaths{}
Colin Crossbaccf5b2018-02-21 14:07:48 -0800429 genDir := android.PathForModuleGen(ctx)
430 sandboxOuts := []string{}
Colin Crossd350ecd2015-04-28 13:25:36 -0700431 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800432 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800433 outFiles = append(outFiles, outFile)
434
Colin Crossbaccf5b2018-02-21 14:07:48 -0800435 sandboxOutfile := pathToSandboxOut(outFile, genDir)
436 sandboxOuts = append(sandboxOuts, sandboxOutfile)
437
Jeff Gaston437d23c2017-11-08 12:38:00 -0800438 command, err := android.Expand(rawCommand, func(name string) (string, error) {
439 switch name {
440 case "in":
441 return in.String(), nil
442 case "out":
443 return sandboxOutfile, nil
444 default:
445 return "$(" + name + ")", nil
446 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700447 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800448 if err != nil {
449 ctx.PropertyErrorf("cmd", err.Error())
450 }
451
452 // escape the command in case for example it contains '#', an odd number of '"', etc
453 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape([]string{command})[0])
454 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700455 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800456 fullCommand := strings.Join(commands, " && ")
457
458 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800459 in: srcFiles,
460 out: outFiles,
461 sandboxOuts: sandboxOuts,
462 cmd: fullCommand,
Jeff Gaston437d23c2017-11-08 12:38:00 -0800463 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700464 }
465
Jeff Gaston437d23c2017-11-08 12:38:00 -0800466 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700467}
468
Colin Cross54190b32017-10-09 15:34:10 -0700469func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700470 m := NewGenSrcs()
471 android.InitAndroidModule(m)
472 return m
473}
474
Colin Crossd350ecd2015-04-28 13:25:36 -0700475type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700476 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800477 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700478}
479
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700480func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700481 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700482
Jeff Gaston437d23c2017-11-08 12:38:00 -0800483 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700484 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800485 sandboxOuts := make([]string, len(properties.Out))
486 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700487 for i, out := range properties.Out {
488 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800489 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700490 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800491 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800492 in: srcFiles,
493 out: outs,
494 sandboxOuts: sandboxOuts,
495 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700496 }
Colin Cross5049f022015-03-18 13:28:46 -0700497 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700498
Jeff Gaston437d23c2017-11-08 12:38:00 -0800499 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700500}
501
Colin Cross54190b32017-10-09 15:34:10 -0700502func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700503 m := NewGenRule()
504 android.InitAndroidModule(m)
505 return m
506}
507
Colin Crossd350ecd2015-04-28 13:25:36 -0700508type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700509 // names of the output files that will be generated
Colin Crossef354482018-10-23 11:27:50 -0700510 Out []string `android:"arch_variant"`
Colin Cross5049f022015-03-18 13:28:46 -0700511}
Nan Zhangea568a42017-11-08 21:20:04 -0800512
513var Bool = proptools.Bool
514var String = proptools.String