blob: 1cec2893aedb9ebd4266fad771e42e01d8ed9b2c [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 Cross1a527682019-09-23 15:55:30 -070020 "strconv"
Colin Cross6f080df2016-11-04 15:32:58 -070021 "strings"
Dan Willemsen3f4539b2016-09-28 16:19:10 -070022
Colin Cross70b40592015-03-23 12:57:34 -070023 "github.com/google/blueprint"
Dan Willemsen8eded0a2017-09-13 16:07:44 -070024 "github.com/google/blueprint/bootstrap"
Nan Zhangea568a42017-11-08 21:20:04 -080025 "github.com/google/blueprint/proptools"
Colin Cross5049f022015-03-18 13:28:46 -070026
Colin Cross635c3b02016-05-18 15:37:25 -070027 "android/soong/android"
Jeff Gastonefc1b412017-03-29 17:29:06 -070028 "android/soong/shared"
Bill Peckhamc087be12020-02-13 15:55:10 -080029 "crypto/sha256"
Jeff Gastonefc1b412017-03-29 17:29:06 -070030 "path/filepath"
Colin Cross5049f022015-03-18 13:28:46 -070031)
32
Colin Cross463a90e2015-06-17 14:20:06 -070033func init() {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000034 registerGenruleBuildComponents(android.InitRegistrationContext)
35}
Jaewoong Jung98716bd2018-12-10 08:13:18 -080036
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000037func registerGenruleBuildComponents(ctx android.RegistrationContext) {
38 ctx.RegisterModuleType("genrule_defaults", defaultsFactory)
39
40 ctx.RegisterModuleType("gensrcs", GenSrcsFactory)
41 ctx.RegisterModuleType("genrule", GenRuleFactory)
42
43 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
44 ctx.BottomUp("genrule_tool_deps", toolDepsMutator).Parallel()
45 })
Colin Cross463a90e2015-06-17 14:20:06 -070046}
47
Colin Cross5049f022015-03-18 13:28:46 -070048var (
Colin Cross635c3b02016-05-18 15:37:25 -070049 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross1a527682019-09-23 15:55:30 -070050
51 gensrcsMerge = pctx.AndroidStaticRule("gensrcsMerge", blueprint.RuleParams{
52 Command: "${soongZip} -o ${tmpZip} @${tmpZip}.rsp && ${zipSync} -d ${genDir} ${tmpZip}",
53 CommandDeps: []string{"${soongZip}", "${zipSync}"},
54 Rspfile: "${tmpZip}.rsp",
55 RspfileContent: "${zipArgs}",
56 }, "tmpZip", "genDir", "zipArgs")
Colin Cross5049f022015-03-18 13:28:46 -070057)
58
Jeff Gastonefc1b412017-03-29 17:29:06 -070059func init() {
Dan Willemsenddf504c2019-08-09 16:21:29 -070060 pctx.Import("android/soong/android")
Jeff Gastonefc1b412017-03-29 17:29:06 -070061 pctx.HostBinToolVariable("sboxCmd", "sbox")
Colin Cross1a527682019-09-23 15:55:30 -070062
63 pctx.HostBinToolVariable("soongZip", "soong_zip")
64 pctx.HostBinToolVariable("zipSync", "zipsync")
Jeff Gastonefc1b412017-03-29 17:29:06 -070065}
66
Colin Cross5049f022015-03-18 13:28:46 -070067type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070068 GeneratedSourceFiles() android.Paths
Colin Cross5ed99c62016-11-22 12:55:55 -080069 GeneratedHeaderDirs() android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -080070 GeneratedDeps() android.Paths
Colin Cross5049f022015-03-18 13:28:46 -070071}
72
Colin Crossfe17f6f2019-03-28 19:30:56 -070073// Alias for android.HostToolProvider
74// Deprecated: use android.HostToolProvider instead.
Colin Crossd350ecd2015-04-28 13:25:36 -070075type HostToolProvider interface {
Colin Crossfe17f6f2019-03-28 19:30:56 -070076 android.HostToolProvider
Colin Crossd350ecd2015-04-28 13:25:36 -070077}
Colin Cross5049f022015-03-18 13:28:46 -070078
Dan Willemsend6ba0d52017-09-13 15:46:47 -070079type hostToolDependencyTag struct {
80 blueprint.BaseDependencyTag
Colin Cross08f15ab2018-10-04 23:29:14 -070081 label string
Dan Willemsend6ba0d52017-09-13 15:46:47 -070082}
83
Colin Cross7d5136f2015-05-11 13:39:40 -070084type generatorProperties struct {
Jeff Gastonefc1b412017-03-29 17:29:06 -070085 // The command to run on one or more input files. Cmd supports substitution of a few variables
Jeff Gastonefc1b412017-03-29 17:29:06 -070086 //
87 // Available variables for substitution:
88 //
Colin Cross2296f5b2017-10-17 21:38:14 -070089 // $(location): the path to the first entry in tools or tool_files
Colin Cross08f15ab2018-10-04 23:29:14 -070090 // $(location <label>): the path to the tool, tool_file, input or output with name <label>
Colin Cross2296f5b2017-10-17 21:38:14 -070091 // $(in): one or more input files
92 // $(out): a single output file
93 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
94 // $(genDir): the sandbox directory for this tool; contains $(out)
95 // $$: a literal $
Nan Zhangea568a42017-11-08 21:20:04 -080096 Cmd *string
Colin Cross7d5136f2015-05-11 13:39:40 -070097
Colin Cross33bfb0a2016-11-21 17:23:08 -080098 // Enable reading a file containing dependencies in gcc format after the command completes
Nan Zhangea568a42017-11-08 21:20:04 -080099 Depfile *bool
Colin Cross33bfb0a2016-11-21 17:23:08 -0800100
Colin Cross6f080df2016-11-04 15:32:58 -0700101 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -0700102 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -0700103 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700104
105 // Local file that is used as the tool
Colin Cross27b922f2019-03-04 22:35:41 -0800106 Tool_files []string `android:"path"`
Colin Cross5ed99c62016-11-22 12:55:55 -0800107
108 // List of directories to export generated headers from
109 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -0800110
111 // list of input files
Colin Cross27b922f2019-03-04 22:35:41 -0800112 Srcs []string `android:"path,arch_variant"`
Dan Willemseneefa0262018-11-17 14:01:18 -0800113
114 // input files to exclude
Colin Cross27b922f2019-03-04 22:35:41 -0800115 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700116}
117
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700118type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700119 android.ModuleBase
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800120 android.DefaultableModuleBase
Jiyong Parkfc752ca2019-06-12 13:27:29 +0900121 android.ApexModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -0700122
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700123 // For other packages to make their own genrules with extra
124 // properties
125 Extra interface{}
Colin Cross7228ecd2019-11-18 16:00:16 -0800126 android.ImageInterface
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700127
Colin Cross7d5136f2015-05-11 13:39:40 -0700128 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700129
Jeff Gaston437d23c2017-11-08 12:38:00 -0800130 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700131
Colin Cross1a527682019-09-23 15:55:30 -0700132 deps android.Paths
133 rule blueprint.Rule
134 rawCommands []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700135
Colin Cross5ed99c62016-11-22 12:55:55 -0800136 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700137
Colin Cross635c3b02016-05-18 15:37:25 -0700138 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800139 outputDeps android.Paths
Colin Crossa4ad2b02019-03-18 22:15:32 -0700140
141 subName string
Colin Cross1a527682019-09-23 15:55:30 -0700142 subDir string
bralee1fbf4402020-05-21 10:11:59 +0800143
144 // Collect the module directory for IDE info in java/jdeps.go.
145 modulePaths []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700146}
147
Colin Cross1a527682019-09-23 15:55:30 -0700148type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700149
150type generateTask struct {
Colin Crossbaccf5b2018-02-21 14:07:48 -0800151 in android.Paths
152 out android.WritablePaths
Colin Cross1a527682019-09-23 15:55:30 -0700153 copyTo android.WritablePaths
154 genDir android.WritablePath
Colin Crossbaccf5b2018-02-21 14:07:48 -0800155 sandboxOuts []string
156 cmd string
Colin Cross1a527682019-09-23 15:55:30 -0700157 shard int
158 shards int
Colin Crossd350ecd2015-04-28 13:25:36 -0700159}
160
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700161func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700162 return g.outputFiles
163}
164
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700165func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700166 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800167}
168
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700169func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800170 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700171}
172
Dan Willemsen9da9d492018-02-21 18:28:18 -0800173func (g *Module) GeneratedDeps() android.Paths {
174 return g.outputDeps
175}
176
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000177func toolDepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700178 if g, ok := ctx.Module().(*Module); ok {
Colin Cross08f15ab2018-10-04 23:29:14 -0700179 for _, tool := range g.properties.Tools {
180 tag := hostToolDependencyTag{label: tool}
181 if m := android.SrcIsModule(tool); m != "" {
182 tool = m
183 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700184 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700185 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700186 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700187}
188
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700189func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa4ad2b02019-03-18 22:15:32 -0700190 g.subName = ctx.ModuleSubDir()
191
bralee1fbf4402020-05-21 10:11:59 +0800192 // Collect the module directory for IDE info in java/jdeps.go.
193 g.modulePaths = append(g.modulePaths, ctx.ModuleDir())
194
Colin Cross5ed99c62016-11-22 12:55:55 -0800195 if len(g.properties.Export_include_dirs) > 0 {
196 for _, dir := range g.properties.Export_include_dirs {
197 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
Colin Cross1a527682019-09-23 15:55:30 -0700198 android.PathForModuleGen(ctx, g.subDir, ctx.ModuleDir(), dir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800199 }
200 } else {
Colin Cross1a527682019-09-23 15:55:30 -0700201 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, g.subDir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800202 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700203
Colin Cross08f15ab2018-10-04 23:29:14 -0700204 locationLabels := map[string][]string{}
205 firstLabel := ""
206
207 addLocationLabel := func(label string, paths []string) {
208 if firstLabel == "" {
209 firstLabel = label
210 }
211 if _, exists := locationLabels[label]; !exists {
212 locationLabels[label] = paths
213 } else {
214 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
215 label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
216 }
217 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700218
Colin Cross6f080df2016-11-04 15:32:58 -0700219 if len(g.properties.Tools) > 0 {
Colin Crossba71a3f2019-03-18 12:12:48 -0700220 seenTools := make(map[string]bool)
221
Colin Cross35143d02017-11-16 00:11:20 -0800222 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700223 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
224 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700225 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700226 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700227
Colin Crossfe17f6f2019-03-28 19:30:56 -0700228 if t, ok := module.(android.HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800229 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800230 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800231 ctx.AddMissingDependencies([]string{tool})
232 } else {
233 ctx.ModuleErrorf("depends on disabled module %q", tool)
234 }
235 break
236 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700237 path = t.HostToolPath()
238 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
239 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
240 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700241 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700242 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
243 break
Colin Cross6f080df2016-11-04 15:32:58 -0700244 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700245 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700246 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700247 break
248 }
249
250 if path.Valid() {
251 g.deps = append(g.deps, path.Path())
Colin Cross08f15ab2018-10-04 23:29:14 -0700252 addLocationLabel(tag.label, []string{path.Path().String()})
Colin Crossba71a3f2019-03-18 12:12:48 -0700253 seenTools[tag.label] = true
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700254 } else {
255 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700256 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700257 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700258 })
Colin Crossba71a3f2019-03-18 12:12:48 -0700259
260 // If AllowMissingDependencies is enabled, the build will not have stopped when
261 // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical
Liz Kammer20ebfb42020-07-28 11:32:07 -0700262 // "cmd: unknown location label ..." errors later. Add a placeholder file to the local label.
263 // The command that uses this placeholder file will never be executed because the rule will be
264 // replaced with an android.Error rule reporting the missing dependencies.
Colin Crossba71a3f2019-03-18 12:12:48 -0700265 if ctx.Config().AllowMissingDependencies() {
266 for _, tool := range g.properties.Tools {
267 if !seenTools[tool] {
268 addLocationLabel(tool, []string{"***missing tool " + tool + "***"})
269 }
270 }
271 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700272 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700273
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700274 if ctx.Failed() {
275 return
276 }
277
Colin Cross08f15ab2018-10-04 23:29:14 -0700278 for _, toolFile := range g.properties.Tool_files {
Colin Cross8a497952019-03-05 22:25:09 -0800279 paths := android.PathsForModuleSrc(ctx, []string{toolFile})
Colin Cross08f15ab2018-10-04 23:29:14 -0700280 g.deps = append(g.deps, paths...)
281 addLocationLabel(toolFile, paths.Strings())
282 }
283
284 var srcFiles android.Paths
285 for _, in := range g.properties.Srcs {
Colin Crossba71a3f2019-03-18 12:12:48 -0700286 paths, missingDeps := android.PathsAndMissingDepsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs)
287 if len(missingDeps) > 0 {
288 if !ctx.Config().AllowMissingDependencies() {
289 panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator",
290 missingDeps))
291 }
292
293 // If AllowMissingDependencies is enabled, the build will not have stopped when
294 // the dependency was added on a missing SourceFileProducer module, which will result in nonsensical
Liz Kammer20ebfb42020-07-28 11:32:07 -0700295 // "cmd: label ":..." has no files" errors later. Add a placeholder file to the local label.
296 // The command that uses this placeholder file will never be executed because the rule will be
297 // replaced with an android.Error rule reporting the missing dependencies.
Colin Crossba71a3f2019-03-18 12:12:48 -0700298 ctx.AddMissingDependencies(missingDeps)
299 addLocationLabel(in, []string{"***missing srcs " + in + "***"})
300 } else {
301 srcFiles = append(srcFiles, paths...)
302 addLocationLabel(in, paths.Strings())
303 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700304 }
305
Colin Cross1a527682019-09-23 15:55:30 -0700306 var copyFrom android.Paths
307 var outputFiles android.WritablePaths
308 var zipArgs strings.Builder
Colin Cross08f15ab2018-10-04 23:29:14 -0700309
Colin Cross1a527682019-09-23 15:55:30 -0700310 for _, task := range g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles) {
311 for _, out := range task.out {
312 addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
Colin Cross85a2e892018-07-09 09:45:06 -0700313 }
314
Bill Peckhamc087be12020-02-13 15:55:10 -0800315 referencedIn := false
Colin Cross1a527682019-09-23 15:55:30 -0700316 referencedDepfile := false
317
318 rawCommand, err := android.ExpandNinjaEscaped(task.cmd, func(name string) (string, bool, error) {
319 // report the error directly without returning an error to android.Expand to catch multiple errors in a
320 // single run
321 reportError := func(fmt string, args ...interface{}) (string, bool, error) {
322 ctx.PropertyErrorf("cmd", fmt, args...)
323 return "SOONG_ERROR", false, nil
Colin Cross6f080df2016-11-04 15:32:58 -0700324 }
Colin Cross1a527682019-09-23 15:55:30 -0700325
326 switch name {
327 case "location":
328 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
329 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700330 }
Colin Cross1a527682019-09-23 15:55:30 -0700331 paths := locationLabels[firstLabel]
332 if len(paths) == 0 {
333 return reportError("default label %q has no files", firstLabel)
334 } else if len(paths) > 1 {
335 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
336 firstLabel, firstLabel)
Colin Cross08f15ab2018-10-04 23:29:14 -0700337 }
Colin Cross1a527682019-09-23 15:55:30 -0700338 return locationLabels[firstLabel][0], false, nil
339 case "in":
Bill Peckhamc087be12020-02-13 15:55:10 -0800340 referencedIn = true
Colin Cross1a527682019-09-23 15:55:30 -0700341 return "${in}", true, nil
342 case "out":
343 return "__SBOX_OUT_FILES__", false, nil
344 case "depfile":
345 referencedDepfile = true
346 if !Bool(g.properties.Depfile) {
347 return reportError("$(depfile) used without depfile property")
348 }
349 return "__SBOX_DEPFILE__", false, nil
350 case "genDir":
351 return "__SBOX_OUT_DIR__", false, nil
352 default:
353 if strings.HasPrefix(name, "location ") {
354 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
355 if paths, ok := locationLabels[label]; ok {
356 if len(paths) == 0 {
357 return reportError("label %q has no files", label)
358 } else if len(paths) > 1 {
359 return reportError("label %q has multiple files, use $(locations %s) to reference it",
360 label, label)
361 }
362 return paths[0], false, nil
363 } else {
364 return reportError("unknown location label %q", label)
365 }
366 } else if strings.HasPrefix(name, "locations ") {
367 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
368 if paths, ok := locationLabels[label]; ok {
369 if len(paths) == 0 {
370 return reportError("label %q has no files", label)
371 }
372 return strings.Join(paths, " "), false, nil
373 } else {
374 return reportError("unknown locations label %q", label)
375 }
376 } else {
377 return reportError("unknown variable '$(%s)'", name)
378 }
Colin Cross6f080df2016-11-04 15:32:58 -0700379 }
Colin Cross1a527682019-09-23 15:55:30 -0700380 })
381
382 if err != nil {
383 ctx.PropertyErrorf("cmd", "%s", err.Error())
384 return
Colin Cross6f080df2016-11-04 15:32:58 -0700385 }
Colin Cross6f080df2016-11-04 15:32:58 -0700386
Colin Cross1a527682019-09-23 15:55:30 -0700387 if Bool(g.properties.Depfile) && !referencedDepfile {
388 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
389 return
390 }
391
392 // tell the sbox command which directory to use as its sandbox root
393 buildDir := android.PathForOutput(ctx).String()
394 sandboxPath := shared.TempDirForOutDir(buildDir)
395
396 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
397 // to be replaced later by ninja_strings.go
398 depfilePlaceholder := ""
399 if Bool(g.properties.Depfile) {
400 depfilePlaceholder = "$depfileArgs"
401 }
402
403 // Escape the command for the shell
404 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
405 g.rawCommands = append(g.rawCommands, rawCommand)
Bill Peckhamc087be12020-02-13 15:55:10 -0800406
407 sandboxCommand := fmt.Sprintf("rm -rf %s && $sboxCmd --sandbox-path %s --output-root %s",
408 task.genDir, sandboxPath, task.genDir)
409
410 if !referencedIn {
411 sandboxCommand = sandboxCommand + hashSrcFiles(srcFiles)
412 }
413
414 sandboxCommand = sandboxCommand + fmt.Sprintf(" -c %s %s $allouts",
415 rawCommand, depfilePlaceholder)
Colin Cross1a527682019-09-23 15:55:30 -0700416
417 ruleParams := blueprint.RuleParams{
418 Command: sandboxCommand,
419 CommandDeps: []string{"$sboxCmd"},
420 }
421 args := []string{"allouts"}
422 if Bool(g.properties.Depfile) {
423 ruleParams.Deps = blueprint.DepsGCC
424 args = append(args, "depfileArgs")
425 }
426 name := "generator"
427 if task.shards > 1 {
428 name += strconv.Itoa(task.shard)
429 }
430 rule := ctx.Rule(pctx, name, ruleParams, args...)
431
432 g.generateSourceFile(ctx, task, rule)
433
434 if len(task.copyTo) > 0 {
435 outputFiles = append(outputFiles, task.copyTo...)
436 copyFrom = append(copyFrom, task.out.Paths()...)
437 zipArgs.WriteString(" -C " + task.genDir.String())
438 zipArgs.WriteString(android.JoinWithPrefix(task.out.Strings(), " -f "))
439 } else {
440 outputFiles = append(outputFiles, task.out...)
441 }
Colin Cross6f080df2016-11-04 15:32:58 -0700442 }
443
Colin Cross1a527682019-09-23 15:55:30 -0700444 if len(copyFrom) > 0 {
445 ctx.Build(pctx, android.BuildParams{
446 Rule: gensrcsMerge,
447 Implicits: copyFrom,
448 Outputs: outputFiles,
449 Args: map[string]string{
450 "zipArgs": zipArgs.String(),
451 "tmpZip": android.PathForModuleGen(ctx, g.subDir+".zip").String(),
452 "genDir": android.PathForModuleGen(ctx, g.subDir).String(),
453 },
454 })
Colin Cross85a2e892018-07-09 09:45:06 -0700455 }
456
Colin Cross1a527682019-09-23 15:55:30 -0700457 g.outputFiles = outputFiles.Paths()
Jeff Gastonefc1b412017-03-29 17:29:06 -0700458
Colin Cross1a527682019-09-23 15:55:30 -0700459 // For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of
460 // the genrules on AOSP. That will make things simpler to look at the graph in the common
461 // case. For larger sets of outputs, inject a phony target in between to limit ninja file
462 // growth.
463 if len(g.outputFiles) <= 6 {
464 g.outputDeps = g.outputFiles
465 } else {
466 phonyFile := android.PathForModuleGen(ctx, "genrule-phony")
467
468 ctx.Build(pctx, android.BuildParams{
469 Rule: blueprint.Phony,
470 Output: phonyFile,
471 Inputs: g.outputFiles,
472 })
473
474 g.outputDeps = android.Paths{phonyFile}
Jeff Gaston02a684b2017-10-27 14:59:27 -0700475 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800476
Colin Crossd350ecd2015-04-28 13:25:36 -0700477}
478
Bill Peckhamc087be12020-02-13 15:55:10 -0800479func hashSrcFiles(srcFiles android.Paths) string {
480 h := sha256.New()
481 for _, src := range srcFiles {
482 h.Write([]byte(src.String()))
483 }
484 return fmt.Sprintf(" --input-hash %x", h.Sum(nil))
485}
486
Colin Cross1a527682019-09-23 15:55:30 -0700487func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask, rule blueprint.Rule) {
Colin Cross67a5c132017-05-09 13:45:28 -0700488 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700489 if len(task.out) == 0 {
490 ctx.ModuleErrorf("must have at least one output file")
491 return
492 }
Colin Cross67a5c132017-05-09 13:45:28 -0700493 if len(task.out) == 1 {
494 desc += " " + task.out[0].Base()
495 }
496
Jeff Gaston02a684b2017-10-27 14:59:27 -0700497 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800498 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700499 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
500 }
501
Colin Cross1a527682019-09-23 15:55:30 -0700502 if task.shards > 1 {
503 desc += " " + strconv.Itoa(task.shard)
504 }
505
Colin Crossae887032017-10-23 17:16:14 -0700506 params := android.BuildParams{
Colin Cross1a527682019-09-23 15:55:30 -0700507 Rule: rule,
508 Description: desc,
Colin Cross15e86d92017-10-20 15:07:08 -0700509 Output: task.out[0],
510 ImplicitOutputs: task.out[1:],
511 Inputs: task.in,
512 Implicits: g.deps,
513 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800514 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700515 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800516 }
Nan Zhangea568a42017-11-08 21:20:04 -0800517 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700518 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
519 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800520 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700521
Colin Crossae887032017-10-23 17:16:14 -0700522 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700523}
524
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700525// Collect information for opening IDE project files in java/jdeps.go.
526func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
527 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
528 for _, src := range g.properties.Srcs {
529 if strings.HasPrefix(src, ":") {
530 src = strings.Trim(src, ":")
531 dpInfo.Deps = append(dpInfo.Deps, src)
532 }
533 }
bralee1fbf4402020-05-21 10:11:59 +0800534 dpInfo.Paths = append(dpInfo.Paths, g.modulePaths...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700535}
536
Colin Crossa4ad2b02019-03-18 22:15:32 -0700537func (g *Module) AndroidMk() android.AndroidMkData {
538 return android.AndroidMkData{
539 Include: "$(BUILD_PHONY_PACKAGE)",
540 Class: "FAKE",
541 OutputFile: android.OptionalPathForPath(g.outputFiles[0]),
542 SubName: g.subName,
543 Extra: []android.AndroidMkExtraFunc{
544 func(w io.Writer, outputFile android.Path) {
Colin Cross1a527682019-09-23 15:55:30 -0700545 fmt.Fprintln(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=", strings.Join(g.outputDeps.Strings(), " "))
Colin Crossa4ad2b02019-03-18 22:15:32 -0700546 },
547 },
548 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
549 android.WriteAndroidMkData(w, data)
550 if data.SubName != "" {
551 fmt.Fprintln(w, ".PHONY:", name)
552 fmt.Fprintln(w, name, ":", name+g.subName)
553 }
554 },
555 }
556}
557
Jooyung Han749dc692020-04-15 11:03:39 +0900558func (g *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error {
559 // Because generated outputs are checked by client modules(e.g. cc_library, ...)
560 // we can safely ignore the check here.
561 return nil
562}
563
Jeff Gaston437d23c2017-11-08 12:38:00 -0800564func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700565 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800566 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700567 }
568
Colin Cross36242852017-06-23 15:06:31 -0700569 module.AddProperties(props...)
570 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700571
Colin Cross7228ecd2019-11-18 16:00:16 -0800572 module.ImageInterface = noopImageInterface{}
573
Colin Cross36242852017-06-23 15:06:31 -0700574 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700575}
576
Colin Cross7228ecd2019-11-18 16:00:16 -0800577type noopImageInterface struct{}
578
579func (x noopImageInterface) ImageMutatorBegin(android.BaseModuleContext) {}
580func (x noopImageInterface) CoreVariantNeeded(android.BaseModuleContext) bool { return false }
Yifan Hong1b3348d2020-01-21 15:53:22 -0800581func (x noopImageInterface) RamdiskVariantNeeded(android.BaseModuleContext) bool { return false }
Colin Cross7228ecd2019-11-18 16:00:16 -0800582func (x noopImageInterface) RecoveryVariantNeeded(android.BaseModuleContext) bool { return false }
583func (x noopImageInterface) ExtraImageVariations(ctx android.BaseModuleContext) []string { return nil }
584func (x noopImageInterface) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
585}
586
Colin Crossbaccf5b2018-02-21 14:07:48 -0800587// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
588func pathToSandboxOut(path android.Path, genDir android.Path) string {
589 relOut, err := filepath.Rel(genDir.String(), path.String())
590 if err != nil {
591 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
592 }
593 return filepath.Join("__SBOX_OUT_DIR__", relOut)
594
595}
596
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700597func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700598 properties := &genSrcsProperties{}
599
Colin Cross1a527682019-09-23 15:55:30 -0700600 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
601 genDir := android.PathForModuleGen(ctx, "gensrcs")
602 shardSize := defaultShardSize
603 if s := properties.Shard_size; s != nil {
604 shardSize = int(*s)
605 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800606
Colin Cross1a527682019-09-23 15:55:30 -0700607 shards := android.ShardPaths(srcFiles, shardSize)
608 var generateTasks []generateTask
Colin Crossbaccf5b2018-02-21 14:07:48 -0800609
Colin Cross1a527682019-09-23 15:55:30 -0700610 for i, shard := range shards {
611 var commands []string
612 var outFiles android.WritablePaths
613 var copyTo android.WritablePaths
614 var shardDir android.WritablePath
615 var sandboxOuts []string
616
617 if len(shards) > 1 {
618 shardDir = android.PathForModuleGen(ctx, strconv.Itoa(i))
619 } else {
620 shardDir = genDir
Jeff Gaston437d23c2017-11-08 12:38:00 -0800621 }
622
Colin Cross1a527682019-09-23 15:55:30 -0700623 for _, in := range shard {
624 outFile := android.GenPathWithExt(ctx, "gensrcs", in, String(properties.Output_extension))
625 sandboxOutfile := pathToSandboxOut(outFile, genDir)
Jeff Gaston437d23c2017-11-08 12:38:00 -0800626
Colin Cross1a527682019-09-23 15:55:30 -0700627 if len(shards) > 1 {
628 shardFile := android.GenPathWithExt(ctx, strconv.Itoa(i), in, String(properties.Output_extension))
629 copyTo = append(copyTo, outFile)
630 outFile = shardFile
631 }
632
633 outFiles = append(outFiles, outFile)
634 sandboxOuts = append(sandboxOuts, sandboxOutfile)
635
636 command, err := android.Expand(rawCommand, func(name string) (string, error) {
637 switch name {
638 case "in":
639 return in.String(), nil
640 case "out":
641 return sandboxOutfile, nil
642 default:
643 return "$(" + name + ")", nil
644 }
645 })
646 if err != nil {
647 ctx.PropertyErrorf("cmd", err.Error())
648 }
649
650 // escape the command in case for example it contains '#', an odd number of '"', etc
651 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
652 commands = append(commands, command)
653 }
654 fullCommand := strings.Join(commands, " && ")
655
656 generateTasks = append(generateTasks, generateTask{
657 in: shard,
658 out: outFiles,
659 copyTo: copyTo,
660 genDir: shardDir,
661 sandboxOuts: sandboxOuts,
662 cmd: fullCommand,
663 shard: i,
664 shards: len(shards),
665 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800666 }
Colin Cross1a527682019-09-23 15:55:30 -0700667
668 return generateTasks
Colin Crossd350ecd2015-04-28 13:25:36 -0700669 }
670
Colin Cross1a527682019-09-23 15:55:30 -0700671 g := generatorFactory(taskGenerator, properties)
672 g.subDir = "gensrcs"
673 return g
Colin Crossd350ecd2015-04-28 13:25:36 -0700674}
675
Colin Cross54190b32017-10-09 15:34:10 -0700676func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700677 m := NewGenSrcs()
678 android.InitAndroidModule(m)
679 return m
680}
681
Colin Crossd350ecd2015-04-28 13:25:36 -0700682type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700683 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800684 Output_extension *string
Colin Cross1a527682019-09-23 15:55:30 -0700685
686 // maximum number of files that will be passed on a single command line.
687 Shard_size *int64
Colin Cross5049f022015-03-18 13:28:46 -0700688}
689
Colin Cross1a527682019-09-23 15:55:30 -0700690const defaultShardSize = 100
691
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700692func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700693 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700694
Colin Cross1a527682019-09-23 15:55:30 -0700695 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700696 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800697 sandboxOuts := make([]string, len(properties.Out))
698 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700699 for i, out := range properties.Out {
700 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800701 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700702 }
Colin Cross1a527682019-09-23 15:55:30 -0700703 return []generateTask{{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800704 in: srcFiles,
705 out: outs,
Colin Cross1a527682019-09-23 15:55:30 -0700706 genDir: android.PathForModuleGen(ctx),
Colin Crossbaccf5b2018-02-21 14:07:48 -0800707 sandboxOuts: sandboxOuts,
708 cmd: rawCommand,
Colin Cross1a527682019-09-23 15:55:30 -0700709 }}
Colin Cross5049f022015-03-18 13:28:46 -0700710 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700711
Jeff Gaston437d23c2017-11-08 12:38:00 -0800712 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700713}
714
Colin Cross54190b32017-10-09 15:34:10 -0700715func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700716 m := NewGenRule()
717 android.InitAndroidModule(m)
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800718 android.InitDefaultableModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700719 return m
720}
721
Colin Crossd350ecd2015-04-28 13:25:36 -0700722type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700723 // names of the output files that will be generated
Colin Crossef354482018-10-23 11:27:50 -0700724 Out []string `android:"arch_variant"`
Colin Cross5049f022015-03-18 13:28:46 -0700725}
Nan Zhangea568a42017-11-08 21:20:04 -0800726
727var Bool = proptools.Bool
728var String = proptools.String
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800729
730//
731// Defaults
732//
733type Defaults struct {
734 android.ModuleBase
735 android.DefaultsModuleBase
736}
737
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800738func defaultsFactory() android.Module {
739 return DefaultsFactory()
740}
741
742func DefaultsFactory(props ...interface{}) android.Module {
743 module := &Defaults{}
744
745 module.AddProperties(props...)
746 module.AddProperties(
747 &generatorProperties{},
748 &genRuleProperties{},
749 )
750
751 android.InitDefaultsModule(module)
752
753 return module
754}