blob: 99d62076b769739a7e9f33f9bcd627bd4ce1cf2b [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
Chris Parsonsaa8be052020-10-14 16:22:37 -040084// TODO(cparsons): Move to a common location when there is more than just
85// genrule with a bazel_module property.
86type bazelModuleProperties struct {
87 Label string
88}
89
Colin Cross7d5136f2015-05-11 13:39:40 -070090type generatorProperties struct {
Jeff Gastonefc1b412017-03-29 17:29:06 -070091 // The command to run on one or more input files. Cmd supports substitution of a few variables
Jeff Gastonefc1b412017-03-29 17:29:06 -070092 //
93 // Available variables for substitution:
94 //
Colin Cross2296f5b2017-10-17 21:38:14 -070095 // $(location): the path to the first entry in tools or tool_files
Colin Cross08f15ab2018-10-04 23:29:14 -070096 // $(location <label>): the path to the tool, tool_file, input or output with name <label>
Colin Cross2296f5b2017-10-17 21:38:14 -070097 // $(in): one or more input files
98 // $(out): a single output file
99 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
100 // $(genDir): the sandbox directory for this tool; contains $(out)
101 // $$: a literal $
Nan Zhangea568a42017-11-08 21:20:04 -0800102 Cmd *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700103
Colin Cross33bfb0a2016-11-21 17:23:08 -0800104 // Enable reading a file containing dependencies in gcc format after the command completes
Nan Zhangea568a42017-11-08 21:20:04 -0800105 Depfile *bool
Colin Cross33bfb0a2016-11-21 17:23:08 -0800106
Colin Cross6f080df2016-11-04 15:32:58 -0700107 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -0700108 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -0700109 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700110
111 // Local file that is used as the tool
Colin Cross27b922f2019-03-04 22:35:41 -0800112 Tool_files []string `android:"path"`
Colin Cross5ed99c62016-11-22 12:55:55 -0800113
114 // List of directories to export generated headers from
115 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -0800116
117 // list of input files
Colin Cross27b922f2019-03-04 22:35:41 -0800118 Srcs []string `android:"path,arch_variant"`
Dan Willemseneefa0262018-11-17 14:01:18 -0800119
120 // input files to exclude
Colin Cross27b922f2019-03-04 22:35:41 -0800121 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700122
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400123 // in bazel-enabled mode, the bazel label to evaluate instead of this module
Chris Parsonsaa8be052020-10-14 16:22:37 -0400124 Bazel_module bazelModuleProperties
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400125}
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700126type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700127 android.ModuleBase
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800128 android.DefaultableModuleBase
Jiyong Parkfc752ca2019-06-12 13:27:29 +0900129 android.ApexModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -0700130
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700131 // For other packages to make their own genrules with extra
132 // properties
133 Extra interface{}
Colin Cross7228ecd2019-11-18 16:00:16 -0800134 android.ImageInterface
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700135
Colin Cross7d5136f2015-05-11 13:39:40 -0700136 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700137
Jeff Gaston437d23c2017-11-08 12:38:00 -0800138 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700139
Colin Cross1a527682019-09-23 15:55:30 -0700140 deps android.Paths
141 rule blueprint.Rule
142 rawCommands []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700143
Colin Cross5ed99c62016-11-22 12:55:55 -0800144 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700145
Colin Cross635c3b02016-05-18 15:37:25 -0700146 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800147 outputDeps android.Paths
Colin Crossa4ad2b02019-03-18 22:15:32 -0700148
149 subName string
Colin Cross1a527682019-09-23 15:55:30 -0700150 subDir string
bralee1fbf4402020-05-21 10:11:59 +0800151
152 // Collect the module directory for IDE info in java/jdeps.go.
153 modulePaths []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700154}
155
Colin Cross1a527682019-09-23 15:55:30 -0700156type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700157
158type generateTask struct {
Colin Crossbaccf5b2018-02-21 14:07:48 -0800159 in android.Paths
160 out android.WritablePaths
Colin Cross1a527682019-09-23 15:55:30 -0700161 copyTo android.WritablePaths
162 genDir android.WritablePath
Colin Crossbaccf5b2018-02-21 14:07:48 -0800163 sandboxOuts []string
164 cmd string
Colin Cross1a527682019-09-23 15:55:30 -0700165 shard int
166 shards int
Colin Crossd350ecd2015-04-28 13:25:36 -0700167}
168
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700169func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700170 return g.outputFiles
171}
172
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700173func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700174 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800175}
176
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700177func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800178 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700179}
180
Dan Willemsen9da9d492018-02-21 18:28:18 -0800181func (g *Module) GeneratedDeps() android.Paths {
182 return g.outputDeps
183}
184
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000185func toolDepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700186 if g, ok := ctx.Module().(*Module); ok {
Colin Cross08f15ab2018-10-04 23:29:14 -0700187 for _, tool := range g.properties.Tools {
188 tag := hostToolDependencyTag{label: tool}
189 if m := android.SrcIsModule(tool); m != "" {
190 tool = m
191 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700192 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700193 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700194 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700195}
196
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400197// Returns true if information was available from Bazel, false if bazel invocation still needs to occur.
198func (c *Module) generateBazelBuildActions(ctx android.ModuleContext, label string) bool {
199 bazelCtx := ctx.Config().BazelContext
200 filePaths, ok := bazelCtx.GetAllFiles(label)
201 if ok {
202 var bazelOutputFiles android.Paths
203 for _, bazelOutputFile := range filePaths {
204 bazelOutputFiles = append(bazelOutputFiles, android.PathForSource(ctx, bazelOutputFile))
205 }
206 c.outputFiles = bazelOutputFiles
207 c.outputDeps = bazelOutputFiles
208 }
209 return ok
210}
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700211func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa4ad2b02019-03-18 22:15:32 -0700212 g.subName = ctx.ModuleSubDir()
213
bralee1fbf4402020-05-21 10:11:59 +0800214 // Collect the module directory for IDE info in java/jdeps.go.
215 g.modulePaths = append(g.modulePaths, ctx.ModuleDir())
216
Colin Cross5ed99c62016-11-22 12:55:55 -0800217 if len(g.properties.Export_include_dirs) > 0 {
218 for _, dir := range g.properties.Export_include_dirs {
219 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
Colin Cross1a527682019-09-23 15:55:30 -0700220 android.PathForModuleGen(ctx, g.subDir, ctx.ModuleDir(), dir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800221 }
222 } else {
Colin Cross1a527682019-09-23 15:55:30 -0700223 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, g.subDir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800224 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700225
Colin Cross08f15ab2018-10-04 23:29:14 -0700226 locationLabels := map[string][]string{}
227 firstLabel := ""
228
229 addLocationLabel := func(label string, paths []string) {
230 if firstLabel == "" {
231 firstLabel = label
232 }
233 if _, exists := locationLabels[label]; !exists {
234 locationLabels[label] = paths
235 } else {
236 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
237 label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
238 }
239 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700240
Colin Cross6f080df2016-11-04 15:32:58 -0700241 if len(g.properties.Tools) > 0 {
Colin Crossba71a3f2019-03-18 12:12:48 -0700242 seenTools := make(map[string]bool)
243
Colin Cross35143d02017-11-16 00:11:20 -0800244 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700245 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
246 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700247 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700248 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700249
Colin Crossfe17f6f2019-03-28 19:30:56 -0700250 if t, ok := module.(android.HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800251 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800252 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800253 ctx.AddMissingDependencies([]string{tool})
254 } else {
255 ctx.ModuleErrorf("depends on disabled module %q", tool)
256 }
257 break
258 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700259 path = t.HostToolPath()
260 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
261 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
262 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700263 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700264 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
265 break
Colin Cross6f080df2016-11-04 15:32:58 -0700266 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700267 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700268 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700269 break
270 }
271
272 if path.Valid() {
273 g.deps = append(g.deps, path.Path())
Colin Cross08f15ab2018-10-04 23:29:14 -0700274 addLocationLabel(tag.label, []string{path.Path().String()})
Colin Crossba71a3f2019-03-18 12:12:48 -0700275 seenTools[tag.label] = true
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700276 } else {
277 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700278 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700279 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700280 })
Colin Crossba71a3f2019-03-18 12:12:48 -0700281
282 // If AllowMissingDependencies is enabled, the build will not have stopped when
283 // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical
Liz Kammer20ebfb42020-07-28 11:32:07 -0700284 // "cmd: unknown location label ..." errors later. Add a placeholder file to the local label.
285 // The command that uses this placeholder file will never be executed because the rule will be
286 // replaced with an android.Error rule reporting the missing dependencies.
Colin Crossba71a3f2019-03-18 12:12:48 -0700287 if ctx.Config().AllowMissingDependencies() {
288 for _, tool := range g.properties.Tools {
289 if !seenTools[tool] {
290 addLocationLabel(tool, []string{"***missing tool " + tool + "***"})
291 }
292 }
293 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700294 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700295
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700296 if ctx.Failed() {
297 return
298 }
299
Colin Cross08f15ab2018-10-04 23:29:14 -0700300 for _, toolFile := range g.properties.Tool_files {
Colin Cross8a497952019-03-05 22:25:09 -0800301 paths := android.PathsForModuleSrc(ctx, []string{toolFile})
Colin Cross08f15ab2018-10-04 23:29:14 -0700302 g.deps = append(g.deps, paths...)
303 addLocationLabel(toolFile, paths.Strings())
304 }
305
306 var srcFiles android.Paths
307 for _, in := range g.properties.Srcs {
Colin Crossba71a3f2019-03-18 12:12:48 -0700308 paths, missingDeps := android.PathsAndMissingDepsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs)
309 if len(missingDeps) > 0 {
310 if !ctx.Config().AllowMissingDependencies() {
311 panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator",
312 missingDeps))
313 }
314
315 // If AllowMissingDependencies is enabled, the build will not have stopped when
316 // the dependency was added on a missing SourceFileProducer module, which will result in nonsensical
Liz Kammer20ebfb42020-07-28 11:32:07 -0700317 // "cmd: label ":..." has no files" errors later. Add a placeholder file to the local label.
318 // The command that uses this placeholder file will never be executed because the rule will be
319 // replaced with an android.Error rule reporting the missing dependencies.
Colin Crossba71a3f2019-03-18 12:12:48 -0700320 ctx.AddMissingDependencies(missingDeps)
321 addLocationLabel(in, []string{"***missing srcs " + in + "***"})
322 } else {
323 srcFiles = append(srcFiles, paths...)
324 addLocationLabel(in, paths.Strings())
325 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700326 }
327
Colin Cross1a527682019-09-23 15:55:30 -0700328 var copyFrom android.Paths
329 var outputFiles android.WritablePaths
330 var zipArgs strings.Builder
Colin Cross08f15ab2018-10-04 23:29:14 -0700331
Colin Cross1a527682019-09-23 15:55:30 -0700332 for _, task := range g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles) {
333 for _, out := range task.out {
334 addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
Colin Cross85a2e892018-07-09 09:45:06 -0700335 }
336
Bill Peckhamc087be12020-02-13 15:55:10 -0800337 referencedIn := false
Colin Cross1a527682019-09-23 15:55:30 -0700338 referencedDepfile := false
339
340 rawCommand, err := android.ExpandNinjaEscaped(task.cmd, func(name string) (string, bool, error) {
341 // report the error directly without returning an error to android.Expand to catch multiple errors in a
342 // single run
343 reportError := func(fmt string, args ...interface{}) (string, bool, error) {
344 ctx.PropertyErrorf("cmd", fmt, args...)
345 return "SOONG_ERROR", false, nil
Colin Cross6f080df2016-11-04 15:32:58 -0700346 }
Colin Cross1a527682019-09-23 15:55:30 -0700347
348 switch name {
349 case "location":
350 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
351 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700352 }
Colin Cross1a527682019-09-23 15:55:30 -0700353 paths := locationLabels[firstLabel]
354 if len(paths) == 0 {
355 return reportError("default label %q has no files", firstLabel)
356 } else if len(paths) > 1 {
357 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
358 firstLabel, firstLabel)
Colin Cross08f15ab2018-10-04 23:29:14 -0700359 }
Colin Cross1a527682019-09-23 15:55:30 -0700360 return locationLabels[firstLabel][0], false, nil
361 case "in":
Bill Peckhamc087be12020-02-13 15:55:10 -0800362 referencedIn = true
Colin Cross1a527682019-09-23 15:55:30 -0700363 return "${in}", true, nil
364 case "out":
365 return "__SBOX_OUT_FILES__", false, nil
366 case "depfile":
367 referencedDepfile = true
368 if !Bool(g.properties.Depfile) {
369 return reportError("$(depfile) used without depfile property")
370 }
371 return "__SBOX_DEPFILE__", false, nil
372 case "genDir":
373 return "__SBOX_OUT_DIR__", false, nil
374 default:
375 if strings.HasPrefix(name, "location ") {
376 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
377 if paths, ok := locationLabels[label]; ok {
378 if len(paths) == 0 {
379 return reportError("label %q has no files", label)
380 } else if len(paths) > 1 {
381 return reportError("label %q has multiple files, use $(locations %s) to reference it",
382 label, label)
383 }
384 return paths[0], false, nil
385 } else {
386 return reportError("unknown location label %q", label)
387 }
388 } else if strings.HasPrefix(name, "locations ") {
389 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
390 if paths, ok := locationLabels[label]; ok {
391 if len(paths) == 0 {
392 return reportError("label %q has no files", label)
393 }
394 return strings.Join(paths, " "), false, nil
395 } else {
396 return reportError("unknown locations label %q", label)
397 }
398 } else {
399 return reportError("unknown variable '$(%s)'", name)
400 }
Colin Cross6f080df2016-11-04 15:32:58 -0700401 }
Colin Cross1a527682019-09-23 15:55:30 -0700402 })
403
404 if err != nil {
405 ctx.PropertyErrorf("cmd", "%s", err.Error())
406 return
Colin Cross6f080df2016-11-04 15:32:58 -0700407 }
Colin Cross6f080df2016-11-04 15:32:58 -0700408
Colin Cross1a527682019-09-23 15:55:30 -0700409 if Bool(g.properties.Depfile) && !referencedDepfile {
410 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
411 return
412 }
413
414 // tell the sbox command which directory to use as its sandbox root
415 buildDir := android.PathForOutput(ctx).String()
416 sandboxPath := shared.TempDirForOutDir(buildDir)
417
418 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
419 // to be replaced later by ninja_strings.go
420 depfilePlaceholder := ""
421 if Bool(g.properties.Depfile) {
422 depfilePlaceholder = "$depfileArgs"
423 }
424
425 // Escape the command for the shell
426 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
427 g.rawCommands = append(g.rawCommands, rawCommand)
Bill Peckhamc087be12020-02-13 15:55:10 -0800428
429 sandboxCommand := fmt.Sprintf("rm -rf %s && $sboxCmd --sandbox-path %s --output-root %s",
430 task.genDir, sandboxPath, task.genDir)
431
432 if !referencedIn {
433 sandboxCommand = sandboxCommand + hashSrcFiles(srcFiles)
434 }
435
436 sandboxCommand = sandboxCommand + fmt.Sprintf(" -c %s %s $allouts",
437 rawCommand, depfilePlaceholder)
Colin Cross1a527682019-09-23 15:55:30 -0700438
439 ruleParams := blueprint.RuleParams{
440 Command: sandboxCommand,
441 CommandDeps: []string{"$sboxCmd"},
442 }
443 args := []string{"allouts"}
444 if Bool(g.properties.Depfile) {
445 ruleParams.Deps = blueprint.DepsGCC
446 args = append(args, "depfileArgs")
447 }
448 name := "generator"
449 if task.shards > 1 {
450 name += strconv.Itoa(task.shard)
451 }
452 rule := ctx.Rule(pctx, name, ruleParams, args...)
453
454 g.generateSourceFile(ctx, task, rule)
455
456 if len(task.copyTo) > 0 {
457 outputFiles = append(outputFiles, task.copyTo...)
458 copyFrom = append(copyFrom, task.out.Paths()...)
459 zipArgs.WriteString(" -C " + task.genDir.String())
460 zipArgs.WriteString(android.JoinWithPrefix(task.out.Strings(), " -f "))
461 } else {
462 outputFiles = append(outputFiles, task.out...)
463 }
Colin Cross6f080df2016-11-04 15:32:58 -0700464 }
465
Colin Cross1a527682019-09-23 15:55:30 -0700466 if len(copyFrom) > 0 {
467 ctx.Build(pctx, android.BuildParams{
468 Rule: gensrcsMerge,
469 Implicits: copyFrom,
470 Outputs: outputFiles,
471 Args: map[string]string{
472 "zipArgs": zipArgs.String(),
473 "tmpZip": android.PathForModuleGen(ctx, g.subDir+".zip").String(),
474 "genDir": android.PathForModuleGen(ctx, g.subDir).String(),
475 },
476 })
Colin Cross85a2e892018-07-09 09:45:06 -0700477 }
478
Colin Cross1a527682019-09-23 15:55:30 -0700479 g.outputFiles = outputFiles.Paths()
Jeff Gastonefc1b412017-03-29 17:29:06 -0700480
Chris Parsonsaa8be052020-10-14 16:22:37 -0400481 bazelModuleLabel := g.properties.Bazel_module.Label
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400482 bazelActionsUsed := false
483 if ctx.Config().BazelContext.BazelEnabled() && len(bazelModuleLabel) > 0 {
484 bazelActionsUsed = g.generateBazelBuildActions(ctx, bazelModuleLabel)
Jeff Gaston02a684b2017-10-27 14:59:27 -0700485 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400486 if !bazelActionsUsed {
487 // For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of
488 // the genrules on AOSP. That will make things simpler to look at the graph in the common
489 // case. For larger sets of outputs, inject a phony target in between to limit ninja file
490 // growth.
491 if len(g.outputFiles) <= 6 {
492 g.outputDeps = g.outputFiles
493 } else {
494 phonyFile := android.PathForModuleGen(ctx, "genrule-phony")
495 ctx.Build(pctx, android.BuildParams{
496 Rule: blueprint.Phony,
497 Output: phonyFile,
498 Inputs: g.outputFiles,
499 })
500 g.outputDeps = android.Paths{phonyFile}
501 }
502 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700503}
Bill Peckhamc087be12020-02-13 15:55:10 -0800504func hashSrcFiles(srcFiles android.Paths) string {
505 h := sha256.New()
506 for _, src := range srcFiles {
507 h.Write([]byte(src.String()))
508 }
509 return fmt.Sprintf(" --input-hash %x", h.Sum(nil))
510}
511
Colin Cross1a527682019-09-23 15:55:30 -0700512func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask, rule blueprint.Rule) {
Colin Cross67a5c132017-05-09 13:45:28 -0700513 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700514 if len(task.out) == 0 {
515 ctx.ModuleErrorf("must have at least one output file")
516 return
517 }
Colin Cross67a5c132017-05-09 13:45:28 -0700518 if len(task.out) == 1 {
519 desc += " " + task.out[0].Base()
520 }
521
Jeff Gaston02a684b2017-10-27 14:59:27 -0700522 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800523 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700524 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
525 }
526
Colin Cross1a527682019-09-23 15:55:30 -0700527 if task.shards > 1 {
528 desc += " " + strconv.Itoa(task.shard)
529 }
530
Colin Crossae887032017-10-23 17:16:14 -0700531 params := android.BuildParams{
Colin Cross1a527682019-09-23 15:55:30 -0700532 Rule: rule,
533 Description: desc,
Colin Cross15e86d92017-10-20 15:07:08 -0700534 Output: task.out[0],
535 ImplicitOutputs: task.out[1:],
536 Inputs: task.in,
537 Implicits: g.deps,
538 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800539 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700540 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800541 }
Nan Zhangea568a42017-11-08 21:20:04 -0800542 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700543 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
544 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800545 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700546
Colin Crossae887032017-10-23 17:16:14 -0700547 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700548}
549
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700550// Collect information for opening IDE project files in java/jdeps.go.
551func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
552 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
553 for _, src := range g.properties.Srcs {
554 if strings.HasPrefix(src, ":") {
555 src = strings.Trim(src, ":")
556 dpInfo.Deps = append(dpInfo.Deps, src)
557 }
558 }
bralee1fbf4402020-05-21 10:11:59 +0800559 dpInfo.Paths = append(dpInfo.Paths, g.modulePaths...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700560}
561
Colin Crossa4ad2b02019-03-18 22:15:32 -0700562func (g *Module) AndroidMk() android.AndroidMkData {
563 return android.AndroidMkData{
564 Include: "$(BUILD_PHONY_PACKAGE)",
565 Class: "FAKE",
566 OutputFile: android.OptionalPathForPath(g.outputFiles[0]),
567 SubName: g.subName,
568 Extra: []android.AndroidMkExtraFunc{
569 func(w io.Writer, outputFile android.Path) {
Colin Cross1a527682019-09-23 15:55:30 -0700570 fmt.Fprintln(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=", strings.Join(g.outputDeps.Strings(), " "))
Colin Crossa4ad2b02019-03-18 22:15:32 -0700571 },
572 },
573 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
574 android.WriteAndroidMkData(w, data)
575 if data.SubName != "" {
576 fmt.Fprintln(w, ".PHONY:", name)
577 fmt.Fprintln(w, name, ":", name+g.subName)
578 }
579 },
580 }
581}
582
Dan Albertc8060532020-07-22 22:32:17 -0700583func (g *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
584 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900585 // Because generated outputs are checked by client modules(e.g. cc_library, ...)
586 // we can safely ignore the check here.
587 return nil
588}
589
Jeff Gaston437d23c2017-11-08 12:38:00 -0800590func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700591 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800592 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700593 }
594
Colin Cross36242852017-06-23 15:06:31 -0700595 module.AddProperties(props...)
596 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700597
Colin Cross7228ecd2019-11-18 16:00:16 -0800598 module.ImageInterface = noopImageInterface{}
599
Colin Cross36242852017-06-23 15:06:31 -0700600 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700601}
602
Colin Cross7228ecd2019-11-18 16:00:16 -0800603type noopImageInterface struct{}
604
605func (x noopImageInterface) ImageMutatorBegin(android.BaseModuleContext) {}
606func (x noopImageInterface) CoreVariantNeeded(android.BaseModuleContext) bool { return false }
Yifan Hong1b3348d2020-01-21 15:53:22 -0800607func (x noopImageInterface) RamdiskVariantNeeded(android.BaseModuleContext) bool { return false }
Colin Cross7228ecd2019-11-18 16:00:16 -0800608func (x noopImageInterface) RecoveryVariantNeeded(android.BaseModuleContext) bool { return false }
609func (x noopImageInterface) ExtraImageVariations(ctx android.BaseModuleContext) []string { return nil }
610func (x noopImageInterface) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
611}
612
Colin Crossbaccf5b2018-02-21 14:07:48 -0800613// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
614func pathToSandboxOut(path android.Path, genDir android.Path) string {
615 relOut, err := filepath.Rel(genDir.String(), path.String())
616 if err != nil {
617 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
618 }
619 return filepath.Join("__SBOX_OUT_DIR__", relOut)
620
621}
622
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700623func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700624 properties := &genSrcsProperties{}
625
Colin Cross1a527682019-09-23 15:55:30 -0700626 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
627 genDir := android.PathForModuleGen(ctx, "gensrcs")
628 shardSize := defaultShardSize
629 if s := properties.Shard_size; s != nil {
630 shardSize = int(*s)
631 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800632
Colin Cross1a527682019-09-23 15:55:30 -0700633 shards := android.ShardPaths(srcFiles, shardSize)
634 var generateTasks []generateTask
Colin Crossbaccf5b2018-02-21 14:07:48 -0800635
Colin Cross1a527682019-09-23 15:55:30 -0700636 for i, shard := range shards {
637 var commands []string
638 var outFiles android.WritablePaths
639 var copyTo android.WritablePaths
640 var shardDir android.WritablePath
641 var sandboxOuts []string
642
643 if len(shards) > 1 {
644 shardDir = android.PathForModuleGen(ctx, strconv.Itoa(i))
645 } else {
646 shardDir = genDir
Jeff Gaston437d23c2017-11-08 12:38:00 -0800647 }
648
Colin Cross1a527682019-09-23 15:55:30 -0700649 for _, in := range shard {
650 outFile := android.GenPathWithExt(ctx, "gensrcs", in, String(properties.Output_extension))
651 sandboxOutfile := pathToSandboxOut(outFile, genDir)
Jeff Gaston437d23c2017-11-08 12:38:00 -0800652
Colin Cross1a527682019-09-23 15:55:30 -0700653 if len(shards) > 1 {
654 shardFile := android.GenPathWithExt(ctx, strconv.Itoa(i), in, String(properties.Output_extension))
655 copyTo = append(copyTo, outFile)
656 outFile = shardFile
657 }
658
659 outFiles = append(outFiles, outFile)
660 sandboxOuts = append(sandboxOuts, sandboxOutfile)
661
662 command, err := android.Expand(rawCommand, func(name string) (string, error) {
663 switch name {
664 case "in":
665 return in.String(), nil
666 case "out":
667 return sandboxOutfile, nil
668 default:
669 return "$(" + name + ")", nil
670 }
671 })
672 if err != nil {
673 ctx.PropertyErrorf("cmd", err.Error())
674 }
675
676 // escape the command in case for example it contains '#', an odd number of '"', etc
677 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
678 commands = append(commands, command)
679 }
680 fullCommand := strings.Join(commands, " && ")
681
682 generateTasks = append(generateTasks, generateTask{
683 in: shard,
684 out: outFiles,
685 copyTo: copyTo,
686 genDir: shardDir,
687 sandboxOuts: sandboxOuts,
688 cmd: fullCommand,
689 shard: i,
690 shards: len(shards),
691 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800692 }
Colin Cross1a527682019-09-23 15:55:30 -0700693
694 return generateTasks
Colin Crossd350ecd2015-04-28 13:25:36 -0700695 }
696
Colin Cross1a527682019-09-23 15:55:30 -0700697 g := generatorFactory(taskGenerator, properties)
698 g.subDir = "gensrcs"
699 return g
Colin Crossd350ecd2015-04-28 13:25:36 -0700700}
701
Colin Cross54190b32017-10-09 15:34:10 -0700702func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700703 m := NewGenSrcs()
704 android.InitAndroidModule(m)
705 return m
706}
707
Colin Crossd350ecd2015-04-28 13:25:36 -0700708type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700709 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800710 Output_extension *string
Colin Cross1a527682019-09-23 15:55:30 -0700711
712 // maximum number of files that will be passed on a single command line.
713 Shard_size *int64
Colin Cross5049f022015-03-18 13:28:46 -0700714}
715
Colin Cross1a527682019-09-23 15:55:30 -0700716const defaultShardSize = 100
717
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700718func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700719 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700720
Colin Cross1a527682019-09-23 15:55:30 -0700721 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700722 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800723 sandboxOuts := make([]string, len(properties.Out))
724 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700725 for i, out := range properties.Out {
726 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800727 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700728 }
Colin Cross1a527682019-09-23 15:55:30 -0700729 return []generateTask{{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800730 in: srcFiles,
731 out: outs,
Colin Cross1a527682019-09-23 15:55:30 -0700732 genDir: android.PathForModuleGen(ctx),
Colin Crossbaccf5b2018-02-21 14:07:48 -0800733 sandboxOuts: sandboxOuts,
734 cmd: rawCommand,
Colin Cross1a527682019-09-23 15:55:30 -0700735 }}
Colin Cross5049f022015-03-18 13:28:46 -0700736 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700737
Jeff Gaston437d23c2017-11-08 12:38:00 -0800738 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700739}
740
Colin Cross54190b32017-10-09 15:34:10 -0700741func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700742 m := NewGenRule()
743 android.InitAndroidModule(m)
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800744 android.InitDefaultableModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700745 return m
746}
747
Colin Crossd350ecd2015-04-28 13:25:36 -0700748type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700749 // names of the output files that will be generated
Colin Crossef354482018-10-23 11:27:50 -0700750 Out []string `android:"arch_variant"`
Colin Cross5049f022015-03-18 13:28:46 -0700751}
Nan Zhangea568a42017-11-08 21:20:04 -0800752
753var Bool = proptools.Bool
754var String = proptools.String
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800755
756//
757// Defaults
758//
759type Defaults struct {
760 android.ModuleBase
761 android.DefaultsModuleBase
762}
763
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800764func defaultsFactory() android.Module {
765 return DefaultsFactory()
766}
767
768func DefaultsFactory(props ...interface{}) android.Module {
769 module := &Defaults{}
770
771 module.AddProperties(props...)
772 module.AddProperties(
773 &generatorProperties{},
774 &genRuleProperties{},
775 )
776
777 android.InitDefaultsModule(module)
778
779 return module
780}