blob: 87e6747e986adb86f6934839c68694428a237392 [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"
Colin Crossa4ad2b02019-03-18 22:15:32 -070019 "io"
Colin Cross6f080df2016-11-04 15:32:58 -070020 "strings"
Dan Willemsen3f4539b2016-09-28 16:19:10 -070021
Colin Cross70b40592015-03-23 12:57:34 -070022 "github.com/google/blueprint"
Dan Willemsen8eded0a2017-09-13 16:07:44 -070023 "github.com/google/blueprint/bootstrap"
Nan Zhangea568a42017-11-08 21:20:04 -080024 "github.com/google/blueprint/proptools"
Colin Cross5049f022015-03-18 13:28:46 -070025
Colin Cross635c3b02016-05-18 15:37:25 -070026 "android/soong/android"
Jeff Gastonefc1b412017-03-29 17:29:06 -070027 "android/soong/shared"
28 "path/filepath"
Colin Cross5049f022015-03-18 13:28:46 -070029)
30
Colin Cross463a90e2015-06-17 14:20:06 -070031func init() {
Jaewoong Jung98716bd2018-12-10 08:13:18 -080032 android.RegisterModuleType("genrule_defaults", defaultsFactory)
33
Colin Cross54190b32017-10-09 15:34:10 -070034 android.RegisterModuleType("gensrcs", GenSrcsFactory)
35 android.RegisterModuleType("genrule", GenRuleFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070036}
37
Colin Cross5049f022015-03-18 13:28:46 -070038var (
Colin Cross635c3b02016-05-18 15:37:25 -070039 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross5049f022015-03-18 13:28:46 -070040)
41
Jeff Gastonefc1b412017-03-29 17:29:06 -070042func init() {
43 pctx.HostBinToolVariable("sboxCmd", "sbox")
44}
45
Colin Cross5049f022015-03-18 13:28:46 -070046type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070047 GeneratedSourceFiles() android.Paths
Colin Cross5ed99c62016-11-22 12:55:55 -080048 GeneratedHeaderDirs() android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -080049 GeneratedDeps() android.Paths
Colin Cross5049f022015-03-18 13:28:46 -070050}
51
Colin Crossfe17f6f2019-03-28 19:30:56 -070052// Alias for android.HostToolProvider
53// Deprecated: use android.HostToolProvider instead.
Colin Crossd350ecd2015-04-28 13:25:36 -070054type HostToolProvider interface {
Colin Crossfe17f6f2019-03-28 19:30:56 -070055 android.HostToolProvider
Colin Crossd350ecd2015-04-28 13:25:36 -070056}
Colin Cross5049f022015-03-18 13:28:46 -070057
Dan Willemsend6ba0d52017-09-13 15:46:47 -070058type hostToolDependencyTag struct {
59 blueprint.BaseDependencyTag
Colin Cross08f15ab2018-10-04 23:29:14 -070060 label string
Dan Willemsend6ba0d52017-09-13 15:46:47 -070061}
62
Colin Cross7d5136f2015-05-11 13:39:40 -070063type generatorProperties struct {
Jeff Gastonefc1b412017-03-29 17:29:06 -070064 // The command to run on one or more input files. Cmd supports substitution of a few variables
65 // (the actual substitution is implemented in GenerateAndroidBuildActions below)
66 //
67 // Available variables for substitution:
68 //
Colin Cross2296f5b2017-10-17 21:38:14 -070069 // $(location): the path to the first entry in tools or tool_files
Colin Cross08f15ab2018-10-04 23:29:14 -070070 // $(location <label>): the path to the tool, tool_file, input or output with name <label>
Colin Cross2296f5b2017-10-17 21:38:14 -070071 // $(in): one or more input files
72 // $(out): a single output file
73 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
74 // $(genDir): the sandbox directory for this tool; contains $(out)
75 // $$: a literal $
Colin Cross6f080df2016-11-04 15:32:58 -070076 //
Jeff Gastonefc1b412017-03-29 17:29:06 -070077 // All files used must be declared as inputs (to ensure proper up-to-date checks).
78 // Use "$(in)" directly in Cmd to ensure that all inputs used are declared.
Nan Zhangea568a42017-11-08 21:20:04 -080079 Cmd *string
Colin Cross7d5136f2015-05-11 13:39:40 -070080
Colin Cross33bfb0a2016-11-21 17:23:08 -080081 // Enable reading a file containing dependencies in gcc format after the command completes
Nan Zhangea568a42017-11-08 21:20:04 -080082 Depfile *bool
Colin Cross33bfb0a2016-11-21 17:23:08 -080083
Colin Cross6f080df2016-11-04 15:32:58 -070084 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -070085 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -070086 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070087
88 // Local file that is used as the tool
Colin Cross27b922f2019-03-04 22:35:41 -080089 Tool_files []string `android:"path"`
Colin Cross5ed99c62016-11-22 12:55:55 -080090
91 // List of directories to export generated headers from
92 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -080093
94 // list of input files
Colin Cross27b922f2019-03-04 22:35:41 -080095 Srcs []string `android:"path,arch_variant"`
Dan Willemseneefa0262018-11-17 14:01:18 -080096
97 // input files to exclude
Colin Cross27b922f2019-03-04 22:35:41 -080098 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070099}
100
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700101type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700102 android.ModuleBase
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800103 android.DefaultableModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -0700104
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700105 // For other packages to make their own genrules with extra
106 // properties
107 Extra interface{}
108
Colin Cross7d5136f2015-05-11 13:39:40 -0700109 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700110
Jeff Gaston437d23c2017-11-08 12:38:00 -0800111 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700112
Colin Cross2a076922018-10-04 23:28:25 -0700113 deps android.Paths
114 rule blueprint.Rule
115 rawCommand string
Colin Crossd350ecd2015-04-28 13:25:36 -0700116
Colin Cross5ed99c62016-11-22 12:55:55 -0800117 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700118
Colin Cross635c3b02016-05-18 15:37:25 -0700119 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800120 outputDeps android.Paths
Colin Crossa4ad2b02019-03-18 22:15:32 -0700121
122 subName string
Colin Crossd350ecd2015-04-28 13:25:36 -0700123}
124
Jeff Gaston437d23c2017-11-08 12:38:00 -0800125type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700126
127type generateTask struct {
Colin Crossbaccf5b2018-02-21 14:07:48 -0800128 in android.Paths
129 out android.WritablePaths
130 sandboxOuts []string
131 cmd string
Colin Crossd350ecd2015-04-28 13:25:36 -0700132}
133
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700134func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700135 return g.outputFiles
136}
137
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700138func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700139 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800140}
141
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700142func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800143 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700144}
145
Dan Willemsen9da9d492018-02-21 18:28:18 -0800146func (g *Module) GeneratedDeps() android.Paths {
147 return g.outputDeps
148}
149
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700150func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700151 if g, ok := ctx.Module().(*Module); ok {
Colin Cross08f15ab2018-10-04 23:29:14 -0700152 for _, tool := range g.properties.Tools {
153 tag := hostToolDependencyTag{label: tool}
154 if m := android.SrcIsModule(tool); m != "" {
155 tool = m
156 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800157 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700158 {Mutator: "arch", Variation: ctx.Config().BuildOsVariant},
Colin Cross08f15ab2018-10-04 23:29:14 -0700159 }, tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700160 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700161 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700162}
163
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700164func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa4ad2b02019-03-18 22:15:32 -0700165 g.subName = ctx.ModuleSubDir()
166
Colin Cross5ed99c62016-11-22 12:55:55 -0800167 if len(g.properties.Export_include_dirs) > 0 {
168 for _, dir := range g.properties.Export_include_dirs {
169 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
170 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
171 }
172 } else {
173 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
174 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700175
Colin Cross08f15ab2018-10-04 23:29:14 -0700176 locationLabels := map[string][]string{}
177 firstLabel := ""
178
179 addLocationLabel := func(label string, paths []string) {
180 if firstLabel == "" {
181 firstLabel = label
182 }
183 if _, exists := locationLabels[label]; !exists {
184 locationLabels[label] = paths
185 } else {
186 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
187 label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
188 }
189 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700190
Colin Cross6f080df2016-11-04 15:32:58 -0700191 if len(g.properties.Tools) > 0 {
Colin Crossba71a3f2019-03-18 12:12:48 -0700192 seenTools := make(map[string]bool)
193
Colin Cross35143d02017-11-16 00:11:20 -0800194 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700195 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
196 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700197 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700198 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700199
Colin Crossfe17f6f2019-03-28 19:30:56 -0700200 if t, ok := module.(android.HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800201 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800202 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800203 ctx.AddMissingDependencies([]string{tool})
204 } else {
205 ctx.ModuleErrorf("depends on disabled module %q", tool)
206 }
207 break
208 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700209 path = t.HostToolPath()
210 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
211 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
212 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700213 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700214 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
215 break
Colin Cross6f080df2016-11-04 15:32:58 -0700216 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700217 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700218 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700219 break
220 }
221
222 if path.Valid() {
223 g.deps = append(g.deps, path.Path())
Colin Cross08f15ab2018-10-04 23:29:14 -0700224 addLocationLabel(tag.label, []string{path.Path().String()})
Colin Crossba71a3f2019-03-18 12:12:48 -0700225 seenTools[tag.label] = true
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700226 } else {
227 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700228 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700229 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700230 })
Colin Crossba71a3f2019-03-18 12:12:48 -0700231
232 // If AllowMissingDependencies is enabled, the build will not have stopped when
233 // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical
234 // "cmd: unknown location label ..." errors later. Add a dummy file to the local label. The
235 // command that uses this dummy file will never be executed because the rule will be replaced with
236 // an android.Error rule reporting the missing dependencies.
237 if ctx.Config().AllowMissingDependencies() {
238 for _, tool := range g.properties.Tools {
239 if !seenTools[tool] {
240 addLocationLabel(tool, []string{"***missing tool " + tool + "***"})
241 }
242 }
243 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700244 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700245
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700246 if ctx.Failed() {
247 return
248 }
249
Colin Cross08f15ab2018-10-04 23:29:14 -0700250 for _, toolFile := range g.properties.Tool_files {
Colin Cross8a497952019-03-05 22:25:09 -0800251 paths := android.PathsForModuleSrc(ctx, []string{toolFile})
Colin Cross08f15ab2018-10-04 23:29:14 -0700252 g.deps = append(g.deps, paths...)
253 addLocationLabel(toolFile, paths.Strings())
254 }
255
256 var srcFiles android.Paths
257 for _, in := range g.properties.Srcs {
Colin Crossba71a3f2019-03-18 12:12:48 -0700258 paths, missingDeps := android.PathsAndMissingDepsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs)
259 if len(missingDeps) > 0 {
260 if !ctx.Config().AllowMissingDependencies() {
261 panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator",
262 missingDeps))
263 }
264
265 // If AllowMissingDependencies is enabled, the build will not have stopped when
266 // the dependency was added on a missing SourceFileProducer module, which will result in nonsensical
267 // "cmd: label ":..." has no files" errors later. Add a dummy file to the local label. The
268 // command that uses this dummy file will never be executed because the rule will be replaced with
269 // an android.Error rule reporting the missing dependencies.
270 ctx.AddMissingDependencies(missingDeps)
271 addLocationLabel(in, []string{"***missing srcs " + in + "***"})
272 } else {
273 srcFiles = append(srcFiles, paths...)
274 addLocationLabel(in, paths.Strings())
275 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700276 }
277
278 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
279
280 for _, out := range task.out {
281 addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
Colin Cross6f080df2016-11-04 15:32:58 -0700282 }
283
Jeff Gaston02a684b2017-10-27 14:59:27 -0700284 referencedDepfile := false
285
Jeff Gaston437d23c2017-11-08 12:38:00 -0800286 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross85a2e892018-07-09 09:45:06 -0700287 // report the error directly without returning an error to android.Expand to catch multiple errors in a
288 // single run
289 reportError := func(fmt string, args ...interface{}) (string, error) {
290 ctx.PropertyErrorf("cmd", fmt, args...)
291 return "SOONG_ERROR", nil
292 }
293
Colin Cross6f080df2016-11-04 15:32:58 -0700294 switch name {
295 case "location":
Colin Cross08f15ab2018-10-04 23:29:14 -0700296 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
Colin Cross85a2e892018-07-09 09:45:06 -0700297 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700298 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700299 paths := locationLabels[firstLabel]
300 if len(paths) == 0 {
301 return reportError("default label %q has no files", firstLabel)
302 } else if len(paths) > 1 {
303 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
304 firstLabel, firstLabel)
305 }
306 return locationLabels[firstLabel][0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700307 case "in":
308 return "${in}", nil
309 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700310 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800311 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700312 referencedDepfile = true
Nan Zhangea568a42017-11-08 21:20:04 -0800313 if !Bool(g.properties.Depfile) {
Colin Cross85a2e892018-07-09 09:45:06 -0700314 return reportError("$(depfile) used without depfile property")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800315 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700316 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700317 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800318 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700319 default:
320 if strings.HasPrefix(name, "location ") {
321 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Colin Cross08f15ab2018-10-04 23:29:14 -0700322 if paths, ok := locationLabels[label]; ok {
323 if len(paths) == 0 {
324 return reportError("label %q has no files", label)
325 } else if len(paths) > 1 {
326 return reportError("label %q has multiple files, use $(locations %s) to reference it",
327 label, label)
328 }
329 return paths[0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700330 } else {
Colin Cross85a2e892018-07-09 09:45:06 -0700331 return reportError("unknown location label %q", label)
Colin Cross6f080df2016-11-04 15:32:58 -0700332 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700333 } else if strings.HasPrefix(name, "locations ") {
334 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
335 if paths, ok := locationLabels[label]; ok {
336 if len(paths) == 0 {
337 return reportError("label %q has no files", label)
338 }
339 return strings.Join(paths, " "), nil
340 } else {
341 return reportError("unknown locations label %q", label)
342 }
343 } else {
344 return reportError("unknown variable '$(%s)'", name)
Colin Cross6f080df2016-11-04 15:32:58 -0700345 }
Colin Cross6f080df2016-11-04 15:32:58 -0700346 }
347 })
348
349 if err != nil {
350 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700351 return
Colin Cross6f080df2016-11-04 15:32:58 -0700352 }
353
Colin Cross85a2e892018-07-09 09:45:06 -0700354 if Bool(g.properties.Depfile) && !referencedDepfile {
355 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
356 }
357
Jeff Gastonefc1b412017-03-29 17:29:06 -0700358 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700359 buildDir := android.PathForOutput(ctx).String()
360 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700361
362 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
363 // to be replaced later by ninja_strings.go
Jeff Gaston02a684b2017-10-27 14:59:27 -0700364 depfilePlaceholder := ""
Nan Zhangea568a42017-11-08 21:20:04 -0800365 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700366 depfilePlaceholder = "$depfileArgs"
367 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800368
Jeff Gaston437d23c2017-11-08 12:38:00 -0800369 genDir := android.PathForModuleGen(ctx)
Jeff Gaston31603062017-12-08 12:45:35 -0800370 // Escape the command for the shell
371 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
Colin Cross2a076922018-10-04 23:28:25 -0700372 g.rawCommand = rawCommand
Jeff Gaston31603062017-12-08 12:45:35 -0800373 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
374 sandboxPath, genDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700375
Colin Cross33bfb0a2016-11-21 17:23:08 -0800376 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700377 Command: sandboxCommand,
378 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800379 }
Colin Cross15e86d92017-10-20 15:07:08 -0700380 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800381 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800382 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700383 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800384 }
385 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700386
Jeff Gaston437d23c2017-11-08 12:38:00 -0800387 g.generateSourceFile(ctx, task)
388
Colin Crossd350ecd2015-04-28 13:25:36 -0700389}
390
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700391func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700392 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700393 if len(task.out) == 0 {
394 ctx.ModuleErrorf("must have at least one output file")
395 return
396 }
Colin Cross67a5c132017-05-09 13:45:28 -0700397 if len(task.out) == 1 {
398 desc += " " + task.out[0].Base()
399 }
400
Jeff Gaston02a684b2017-10-27 14:59:27 -0700401 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800402 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700403 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
404 }
405
Colin Crossae887032017-10-23 17:16:14 -0700406 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700407 Rule: g.rule,
408 Description: "generate",
409 Output: task.out[0],
410 ImplicitOutputs: task.out[1:],
411 Inputs: task.in,
412 Implicits: g.deps,
413 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800414 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700415 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800416 }
Nan Zhangea568a42017-11-08 21:20:04 -0800417 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700418 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
419 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800420 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700421
Colin Crossae887032017-10-23 17:16:14 -0700422 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700423
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700424 for _, outputFile := range task.out {
425 g.outputFiles = append(g.outputFiles, outputFile)
426 }
Dan Willemsen9da9d492018-02-21 18:28:18 -0800427 g.outputDeps = append(g.outputDeps, task.out[0])
Colin Crossd350ecd2015-04-28 13:25:36 -0700428}
429
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700430// Collect information for opening IDE project files in java/jdeps.go.
431func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
432 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
433 for _, src := range g.properties.Srcs {
434 if strings.HasPrefix(src, ":") {
435 src = strings.Trim(src, ":")
436 dpInfo.Deps = append(dpInfo.Deps, src)
437 }
438 }
439}
440
Colin Crossa4ad2b02019-03-18 22:15:32 -0700441func (g *Module) AndroidMk() android.AndroidMkData {
442 return android.AndroidMkData{
443 Include: "$(BUILD_PHONY_PACKAGE)",
444 Class: "FAKE",
445 OutputFile: android.OptionalPathForPath(g.outputFiles[0]),
446 SubName: g.subName,
447 Extra: []android.AndroidMkExtraFunc{
448 func(w io.Writer, outputFile android.Path) {
449 fmt.Fprintln(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=", strings.Join(g.outputFiles.Strings(), " "))
450 },
451 },
452 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
453 android.WriteAndroidMkData(w, data)
454 if data.SubName != "" {
455 fmt.Fprintln(w, ".PHONY:", name)
456 fmt.Fprintln(w, name, ":", name+g.subName)
457 }
458 },
459 }
460}
461
Jeff Gaston437d23c2017-11-08 12:38:00 -0800462func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700463 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800464 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700465 }
466
Colin Cross36242852017-06-23 15:06:31 -0700467 module.AddProperties(props...)
468 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700469
Colin Cross36242852017-06-23 15:06:31 -0700470 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700471}
472
Colin Crossbaccf5b2018-02-21 14:07:48 -0800473// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
474func pathToSandboxOut(path android.Path, genDir android.Path) string {
475 relOut, err := filepath.Rel(genDir.String(), path.String())
476 if err != nil {
477 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
478 }
479 return filepath.Join("__SBOX_OUT_DIR__", relOut)
480
481}
482
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700483func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700484 properties := &genSrcsProperties{}
485
Jeff Gaston437d23c2017-11-08 12:38:00 -0800486 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
487 commands := []string{}
488 outFiles := android.WritablePaths{}
Colin Crossbaccf5b2018-02-21 14:07:48 -0800489 genDir := android.PathForModuleGen(ctx)
490 sandboxOuts := []string{}
Colin Crossd350ecd2015-04-28 13:25:36 -0700491 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800492 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800493 outFiles = append(outFiles, outFile)
494
Colin Crossbaccf5b2018-02-21 14:07:48 -0800495 sandboxOutfile := pathToSandboxOut(outFile, genDir)
496 sandboxOuts = append(sandboxOuts, sandboxOutfile)
497
Jeff Gaston437d23c2017-11-08 12:38:00 -0800498 command, err := android.Expand(rawCommand, func(name string) (string, error) {
499 switch name {
500 case "in":
501 return in.String(), nil
502 case "out":
503 return sandboxOutfile, nil
504 default:
505 return "$(" + name + ")", nil
506 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700507 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800508 if err != nil {
509 ctx.PropertyErrorf("cmd", err.Error())
510 }
511
512 // escape the command in case for example it contains '#', an odd number of '"', etc
Colin Cross0b9f31f2019-02-28 11:00:01 -0800513 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800514 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700515 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800516 fullCommand := strings.Join(commands, " && ")
517
518 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800519 in: srcFiles,
520 out: outFiles,
521 sandboxOuts: sandboxOuts,
522 cmd: fullCommand,
Jeff Gaston437d23c2017-11-08 12:38:00 -0800523 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700524 }
525
Jeff Gaston437d23c2017-11-08 12:38:00 -0800526 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700527}
528
Colin Cross54190b32017-10-09 15:34:10 -0700529func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700530 m := NewGenSrcs()
531 android.InitAndroidModule(m)
532 return m
533}
534
Colin Crossd350ecd2015-04-28 13:25:36 -0700535type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700536 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800537 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700538}
539
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700540func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700541 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700542
Jeff Gaston437d23c2017-11-08 12:38:00 -0800543 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700544 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800545 sandboxOuts := make([]string, len(properties.Out))
546 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700547 for i, out := range properties.Out {
548 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800549 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700550 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800551 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800552 in: srcFiles,
553 out: outs,
554 sandboxOuts: sandboxOuts,
555 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700556 }
Colin Cross5049f022015-03-18 13:28:46 -0700557 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700558
Jeff Gaston437d23c2017-11-08 12:38:00 -0800559 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700560}
561
Colin Cross54190b32017-10-09 15:34:10 -0700562func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700563 m := NewGenRule()
564 android.InitAndroidModule(m)
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800565 android.InitDefaultableModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700566 return m
567}
568
Colin Crossd350ecd2015-04-28 13:25:36 -0700569type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700570 // names of the output files that will be generated
Colin Crossef354482018-10-23 11:27:50 -0700571 Out []string `android:"arch_variant"`
Colin Cross5049f022015-03-18 13:28:46 -0700572}
Nan Zhangea568a42017-11-08 21:20:04 -0800573
574var Bool = proptools.Bool
575var String = proptools.String
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800576
577//
578// Defaults
579//
580type Defaults struct {
581 android.ModuleBase
582 android.DefaultsModuleBase
583}
584
585func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
586}
587
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800588func defaultsFactory() android.Module {
589 return DefaultsFactory()
590}
591
592func DefaultsFactory(props ...interface{}) android.Module {
593 module := &Defaults{}
594
595 module.AddProperties(props...)
596 module.AddProperties(
597 &generatorProperties{},
598 &genRuleProperties{},
599 )
600
601 android.InitDefaultsModule(module)
602
603 return module
604}