blob: 178587ad50cf4e9844f8d126c533c25c0e581491 [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
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400117 // in bazel-enabled mode, the bazel label to evaluate instead of this module
118 Bazel_module string
119}
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700120type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700121 android.ModuleBase
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800122 android.DefaultableModuleBase
Jiyong Parkfc752ca2019-06-12 13:27:29 +0900123 android.ApexModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -0700124
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700125 // For other packages to make their own genrules with extra
126 // properties
127 Extra interface{}
Colin Cross7228ecd2019-11-18 16:00:16 -0800128 android.ImageInterface
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700129
Colin Cross7d5136f2015-05-11 13:39:40 -0700130 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700131
Jeff Gaston437d23c2017-11-08 12:38:00 -0800132 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700133
Colin Cross1a527682019-09-23 15:55:30 -0700134 deps android.Paths
135 rule blueprint.Rule
136 rawCommands []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700137
Colin Cross5ed99c62016-11-22 12:55:55 -0800138 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700139
Colin Cross635c3b02016-05-18 15:37:25 -0700140 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800141 outputDeps android.Paths
Colin Crossa4ad2b02019-03-18 22:15:32 -0700142
143 subName string
Colin Cross1a527682019-09-23 15:55:30 -0700144 subDir string
bralee1fbf4402020-05-21 10:11:59 +0800145
146 // Collect the module directory for IDE info in java/jdeps.go.
147 modulePaths []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700148}
149
Colin Cross1a527682019-09-23 15:55:30 -0700150type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700151
152type generateTask struct {
Colin Crossbaccf5b2018-02-21 14:07:48 -0800153 in android.Paths
154 out android.WritablePaths
Colin Cross1a527682019-09-23 15:55:30 -0700155 copyTo android.WritablePaths
156 genDir android.WritablePath
Colin Crossbaccf5b2018-02-21 14:07:48 -0800157 sandboxOuts []string
158 cmd string
Colin Cross1a527682019-09-23 15:55:30 -0700159 shard int
160 shards int
Colin Crossd350ecd2015-04-28 13:25:36 -0700161}
162
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700163func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700164 return g.outputFiles
165}
166
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700167func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700168 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800169}
170
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700171func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800172 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700173}
174
Dan Willemsen9da9d492018-02-21 18:28:18 -0800175func (g *Module) GeneratedDeps() android.Paths {
176 return g.outputDeps
177}
178
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000179func toolDepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700180 if g, ok := ctx.Module().(*Module); ok {
Colin Cross08f15ab2018-10-04 23:29:14 -0700181 for _, tool := range g.properties.Tools {
182 tag := hostToolDependencyTag{label: tool}
183 if m := android.SrcIsModule(tool); m != "" {
184 tool = m
185 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700186 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700187 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700188 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700189}
190
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400191// Returns true if information was available from Bazel, false if bazel invocation still needs to occur.
192func (c *Module) generateBazelBuildActions(ctx android.ModuleContext, label string) bool {
193 bazelCtx := ctx.Config().BazelContext
194 filePaths, ok := bazelCtx.GetAllFiles(label)
195 if ok {
196 var bazelOutputFiles android.Paths
197 for _, bazelOutputFile := range filePaths {
198 bazelOutputFiles = append(bazelOutputFiles, android.PathForSource(ctx, bazelOutputFile))
199 }
200 c.outputFiles = bazelOutputFiles
201 c.outputDeps = bazelOutputFiles
202 }
203 return ok
204}
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700205func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa4ad2b02019-03-18 22:15:32 -0700206 g.subName = ctx.ModuleSubDir()
207
bralee1fbf4402020-05-21 10:11:59 +0800208 // Collect the module directory for IDE info in java/jdeps.go.
209 g.modulePaths = append(g.modulePaths, ctx.ModuleDir())
210
Colin Cross5ed99c62016-11-22 12:55:55 -0800211 if len(g.properties.Export_include_dirs) > 0 {
212 for _, dir := range g.properties.Export_include_dirs {
213 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
Colin Cross1a527682019-09-23 15:55:30 -0700214 android.PathForModuleGen(ctx, g.subDir, ctx.ModuleDir(), dir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800215 }
216 } else {
Colin Cross1a527682019-09-23 15:55:30 -0700217 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, g.subDir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800218 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700219
Colin Cross08f15ab2018-10-04 23:29:14 -0700220 locationLabels := map[string][]string{}
221 firstLabel := ""
222
223 addLocationLabel := func(label string, paths []string) {
224 if firstLabel == "" {
225 firstLabel = label
226 }
227 if _, exists := locationLabels[label]; !exists {
228 locationLabels[label] = paths
229 } else {
230 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
231 label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
232 }
233 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700234
Colin Cross6f080df2016-11-04 15:32:58 -0700235 if len(g.properties.Tools) > 0 {
Colin Crossba71a3f2019-03-18 12:12:48 -0700236 seenTools := make(map[string]bool)
237
Colin Cross35143d02017-11-16 00:11:20 -0800238 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700239 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
240 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700241 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700242 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700243
Colin Crossfe17f6f2019-03-28 19:30:56 -0700244 if t, ok := module.(android.HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800245 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800246 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800247 ctx.AddMissingDependencies([]string{tool})
248 } else {
249 ctx.ModuleErrorf("depends on disabled module %q", tool)
250 }
251 break
252 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700253 path = t.HostToolPath()
254 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
255 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
256 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700257 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700258 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
259 break
Colin Cross6f080df2016-11-04 15:32:58 -0700260 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700261 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700262 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700263 break
264 }
265
266 if path.Valid() {
267 g.deps = append(g.deps, path.Path())
Colin Cross08f15ab2018-10-04 23:29:14 -0700268 addLocationLabel(tag.label, []string{path.Path().String()})
Colin Crossba71a3f2019-03-18 12:12:48 -0700269 seenTools[tag.label] = true
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700270 } else {
271 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700272 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700273 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700274 })
Colin Crossba71a3f2019-03-18 12:12:48 -0700275
276 // If AllowMissingDependencies is enabled, the build will not have stopped when
277 // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical
Liz Kammer20ebfb42020-07-28 11:32:07 -0700278 // "cmd: unknown location label ..." errors later. Add a placeholder file to the local label.
279 // The command that uses this placeholder file will never be executed because the rule will be
280 // replaced with an android.Error rule reporting the missing dependencies.
Colin Crossba71a3f2019-03-18 12:12:48 -0700281 if ctx.Config().AllowMissingDependencies() {
282 for _, tool := range g.properties.Tools {
283 if !seenTools[tool] {
284 addLocationLabel(tool, []string{"***missing tool " + tool + "***"})
285 }
286 }
287 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700288 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700289
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700290 if ctx.Failed() {
291 return
292 }
293
Colin Cross08f15ab2018-10-04 23:29:14 -0700294 for _, toolFile := range g.properties.Tool_files {
Colin Cross8a497952019-03-05 22:25:09 -0800295 paths := android.PathsForModuleSrc(ctx, []string{toolFile})
Colin Cross08f15ab2018-10-04 23:29:14 -0700296 g.deps = append(g.deps, paths...)
297 addLocationLabel(toolFile, paths.Strings())
298 }
299
300 var srcFiles android.Paths
301 for _, in := range g.properties.Srcs {
Colin Crossba71a3f2019-03-18 12:12:48 -0700302 paths, missingDeps := android.PathsAndMissingDepsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs)
303 if len(missingDeps) > 0 {
304 if !ctx.Config().AllowMissingDependencies() {
305 panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator",
306 missingDeps))
307 }
308
309 // If AllowMissingDependencies is enabled, the build will not have stopped when
310 // the dependency was added on a missing SourceFileProducer module, which will result in nonsensical
Liz Kammer20ebfb42020-07-28 11:32:07 -0700311 // "cmd: label ":..." has no files" errors later. Add a placeholder file to the local label.
312 // The command that uses this placeholder file will never be executed because the rule will be
313 // replaced with an android.Error rule reporting the missing dependencies.
Colin Crossba71a3f2019-03-18 12:12:48 -0700314 ctx.AddMissingDependencies(missingDeps)
315 addLocationLabel(in, []string{"***missing srcs " + in + "***"})
316 } else {
317 srcFiles = append(srcFiles, paths...)
318 addLocationLabel(in, paths.Strings())
319 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700320 }
321
Colin Cross1a527682019-09-23 15:55:30 -0700322 var copyFrom android.Paths
323 var outputFiles android.WritablePaths
324 var zipArgs strings.Builder
Colin Cross08f15ab2018-10-04 23:29:14 -0700325
Colin Cross1a527682019-09-23 15:55:30 -0700326 for _, task := range g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles) {
327 for _, out := range task.out {
328 addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
Colin Cross85a2e892018-07-09 09:45:06 -0700329 }
330
Bill Peckhamc087be12020-02-13 15:55:10 -0800331 referencedIn := false
Colin Cross1a527682019-09-23 15:55:30 -0700332 referencedDepfile := false
333
334 rawCommand, err := android.ExpandNinjaEscaped(task.cmd, func(name string) (string, bool, error) {
335 // report the error directly without returning an error to android.Expand to catch multiple errors in a
336 // single run
337 reportError := func(fmt string, args ...interface{}) (string, bool, error) {
338 ctx.PropertyErrorf("cmd", fmt, args...)
339 return "SOONG_ERROR", false, nil
Colin Cross6f080df2016-11-04 15:32:58 -0700340 }
Colin Cross1a527682019-09-23 15:55:30 -0700341
342 switch name {
343 case "location":
344 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
345 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700346 }
Colin Cross1a527682019-09-23 15:55:30 -0700347 paths := locationLabels[firstLabel]
348 if len(paths) == 0 {
349 return reportError("default label %q has no files", firstLabel)
350 } else if len(paths) > 1 {
351 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
352 firstLabel, firstLabel)
Colin Cross08f15ab2018-10-04 23:29:14 -0700353 }
Colin Cross1a527682019-09-23 15:55:30 -0700354 return locationLabels[firstLabel][0], false, nil
355 case "in":
Bill Peckhamc087be12020-02-13 15:55:10 -0800356 referencedIn = true
Colin Cross1a527682019-09-23 15:55:30 -0700357 return "${in}", true, nil
358 case "out":
359 return "__SBOX_OUT_FILES__", false, nil
360 case "depfile":
361 referencedDepfile = true
362 if !Bool(g.properties.Depfile) {
363 return reportError("$(depfile) used without depfile property")
364 }
365 return "__SBOX_DEPFILE__", false, nil
366 case "genDir":
367 return "__SBOX_OUT_DIR__", false, nil
368 default:
369 if strings.HasPrefix(name, "location ") {
370 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
371 if paths, ok := locationLabels[label]; ok {
372 if len(paths) == 0 {
373 return reportError("label %q has no files", label)
374 } else if len(paths) > 1 {
375 return reportError("label %q has multiple files, use $(locations %s) to reference it",
376 label, label)
377 }
378 return paths[0], false, nil
379 } else {
380 return reportError("unknown location label %q", label)
381 }
382 } else if strings.HasPrefix(name, "locations ") {
383 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
384 if paths, ok := locationLabels[label]; ok {
385 if len(paths) == 0 {
386 return reportError("label %q has no files", label)
387 }
388 return strings.Join(paths, " "), false, nil
389 } else {
390 return reportError("unknown locations label %q", label)
391 }
392 } else {
393 return reportError("unknown variable '$(%s)'", name)
394 }
Colin Cross6f080df2016-11-04 15:32:58 -0700395 }
Colin Cross1a527682019-09-23 15:55:30 -0700396 })
397
398 if err != nil {
399 ctx.PropertyErrorf("cmd", "%s", err.Error())
400 return
Colin Cross6f080df2016-11-04 15:32:58 -0700401 }
Colin Cross6f080df2016-11-04 15:32:58 -0700402
Colin Cross1a527682019-09-23 15:55:30 -0700403 if Bool(g.properties.Depfile) && !referencedDepfile {
404 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
405 return
406 }
407
408 // tell the sbox command which directory to use as its sandbox root
409 buildDir := android.PathForOutput(ctx).String()
410 sandboxPath := shared.TempDirForOutDir(buildDir)
411
412 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
413 // to be replaced later by ninja_strings.go
414 depfilePlaceholder := ""
415 if Bool(g.properties.Depfile) {
416 depfilePlaceholder = "$depfileArgs"
417 }
418
419 // Escape the command for the shell
420 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
421 g.rawCommands = append(g.rawCommands, rawCommand)
Bill Peckhamc087be12020-02-13 15:55:10 -0800422
423 sandboxCommand := fmt.Sprintf("rm -rf %s && $sboxCmd --sandbox-path %s --output-root %s",
424 task.genDir, sandboxPath, task.genDir)
425
426 if !referencedIn {
427 sandboxCommand = sandboxCommand + hashSrcFiles(srcFiles)
428 }
429
430 sandboxCommand = sandboxCommand + fmt.Sprintf(" -c %s %s $allouts",
431 rawCommand, depfilePlaceholder)
Colin Cross1a527682019-09-23 15:55:30 -0700432
433 ruleParams := blueprint.RuleParams{
434 Command: sandboxCommand,
435 CommandDeps: []string{"$sboxCmd"},
436 }
437 args := []string{"allouts"}
438 if Bool(g.properties.Depfile) {
439 ruleParams.Deps = blueprint.DepsGCC
440 args = append(args, "depfileArgs")
441 }
442 name := "generator"
443 if task.shards > 1 {
444 name += strconv.Itoa(task.shard)
445 }
446 rule := ctx.Rule(pctx, name, ruleParams, args...)
447
448 g.generateSourceFile(ctx, task, rule)
449
450 if len(task.copyTo) > 0 {
451 outputFiles = append(outputFiles, task.copyTo...)
452 copyFrom = append(copyFrom, task.out.Paths()...)
453 zipArgs.WriteString(" -C " + task.genDir.String())
454 zipArgs.WriteString(android.JoinWithPrefix(task.out.Strings(), " -f "))
455 } else {
456 outputFiles = append(outputFiles, task.out...)
457 }
Colin Cross6f080df2016-11-04 15:32:58 -0700458 }
459
Colin Cross1a527682019-09-23 15:55:30 -0700460 if len(copyFrom) > 0 {
461 ctx.Build(pctx, android.BuildParams{
462 Rule: gensrcsMerge,
463 Implicits: copyFrom,
464 Outputs: outputFiles,
465 Args: map[string]string{
466 "zipArgs": zipArgs.String(),
467 "tmpZip": android.PathForModuleGen(ctx, g.subDir+".zip").String(),
468 "genDir": android.PathForModuleGen(ctx, g.subDir).String(),
469 },
470 })
Colin Cross85a2e892018-07-09 09:45:06 -0700471 }
472
Colin Cross1a527682019-09-23 15:55:30 -0700473 g.outputFiles = outputFiles.Paths()
Jeff Gastonefc1b412017-03-29 17:29:06 -0700474
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400475 bazelModuleLabel := g.properties.Bazel_module
476 bazelActionsUsed := false
477 if ctx.Config().BazelContext.BazelEnabled() && len(bazelModuleLabel) > 0 {
478 bazelActionsUsed = g.generateBazelBuildActions(ctx, bazelModuleLabel)
Jeff Gaston02a684b2017-10-27 14:59:27 -0700479 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400480 if !bazelActionsUsed {
481 // For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of
482 // the genrules on AOSP. That will make things simpler to look at the graph in the common
483 // case. For larger sets of outputs, inject a phony target in between to limit ninja file
484 // growth.
485 if len(g.outputFiles) <= 6 {
486 g.outputDeps = g.outputFiles
487 } else {
488 phonyFile := android.PathForModuleGen(ctx, "genrule-phony")
489 ctx.Build(pctx, android.BuildParams{
490 Rule: blueprint.Phony,
491 Output: phonyFile,
492 Inputs: g.outputFiles,
493 })
494 g.outputDeps = android.Paths{phonyFile}
495 }
496 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700497}
Bill Peckhamc087be12020-02-13 15:55:10 -0800498func hashSrcFiles(srcFiles android.Paths) string {
499 h := sha256.New()
500 for _, src := range srcFiles {
501 h.Write([]byte(src.String()))
502 }
503 return fmt.Sprintf(" --input-hash %x", h.Sum(nil))
504}
505
Colin Cross1a527682019-09-23 15:55:30 -0700506func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask, rule blueprint.Rule) {
Colin Cross67a5c132017-05-09 13:45:28 -0700507 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700508 if len(task.out) == 0 {
509 ctx.ModuleErrorf("must have at least one output file")
510 return
511 }
Colin Cross67a5c132017-05-09 13:45:28 -0700512 if len(task.out) == 1 {
513 desc += " " + task.out[0].Base()
514 }
515
Jeff Gaston02a684b2017-10-27 14:59:27 -0700516 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800517 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700518 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
519 }
520
Colin Cross1a527682019-09-23 15:55:30 -0700521 if task.shards > 1 {
522 desc += " " + strconv.Itoa(task.shard)
523 }
524
Colin Crossae887032017-10-23 17:16:14 -0700525 params := android.BuildParams{
Colin Cross1a527682019-09-23 15:55:30 -0700526 Rule: rule,
527 Description: desc,
Colin Cross15e86d92017-10-20 15:07:08 -0700528 Output: task.out[0],
529 ImplicitOutputs: task.out[1:],
530 Inputs: task.in,
531 Implicits: g.deps,
532 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800533 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700534 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800535 }
Nan Zhangea568a42017-11-08 21:20:04 -0800536 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700537 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
538 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800539 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700540
Colin Crossae887032017-10-23 17:16:14 -0700541 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700542}
543
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700544// Collect information for opening IDE project files in java/jdeps.go.
545func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
546 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
547 for _, src := range g.properties.Srcs {
548 if strings.HasPrefix(src, ":") {
549 src = strings.Trim(src, ":")
550 dpInfo.Deps = append(dpInfo.Deps, src)
551 }
552 }
bralee1fbf4402020-05-21 10:11:59 +0800553 dpInfo.Paths = append(dpInfo.Paths, g.modulePaths...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700554}
555
Colin Crossa4ad2b02019-03-18 22:15:32 -0700556func (g *Module) AndroidMk() android.AndroidMkData {
557 return android.AndroidMkData{
558 Include: "$(BUILD_PHONY_PACKAGE)",
559 Class: "FAKE",
560 OutputFile: android.OptionalPathForPath(g.outputFiles[0]),
561 SubName: g.subName,
562 Extra: []android.AndroidMkExtraFunc{
563 func(w io.Writer, outputFile android.Path) {
Colin Cross1a527682019-09-23 15:55:30 -0700564 fmt.Fprintln(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=", strings.Join(g.outputDeps.Strings(), " "))
Colin Crossa4ad2b02019-03-18 22:15:32 -0700565 },
566 },
567 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
568 android.WriteAndroidMkData(w, data)
569 if data.SubName != "" {
570 fmt.Fprintln(w, ".PHONY:", name)
571 fmt.Fprintln(w, name, ":", name+g.subName)
572 }
573 },
574 }
575}
576
Dan Albertc8060532020-07-22 22:32:17 -0700577func (g *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
578 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900579 // Because generated outputs are checked by client modules(e.g. cc_library, ...)
580 // we can safely ignore the check here.
581 return nil
582}
583
Jeff Gaston437d23c2017-11-08 12:38:00 -0800584func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700585 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800586 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700587 }
588
Colin Cross36242852017-06-23 15:06:31 -0700589 module.AddProperties(props...)
590 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700591
Colin Cross7228ecd2019-11-18 16:00:16 -0800592 module.ImageInterface = noopImageInterface{}
593
Colin Cross36242852017-06-23 15:06:31 -0700594 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700595}
596
Colin Cross7228ecd2019-11-18 16:00:16 -0800597type noopImageInterface struct{}
598
599func (x noopImageInterface) ImageMutatorBegin(android.BaseModuleContext) {}
600func (x noopImageInterface) CoreVariantNeeded(android.BaseModuleContext) bool { return false }
Yifan Hong1b3348d2020-01-21 15:53:22 -0800601func (x noopImageInterface) RamdiskVariantNeeded(android.BaseModuleContext) bool { return false }
Colin Cross7228ecd2019-11-18 16:00:16 -0800602func (x noopImageInterface) RecoveryVariantNeeded(android.BaseModuleContext) bool { return false }
603func (x noopImageInterface) ExtraImageVariations(ctx android.BaseModuleContext) []string { return nil }
604func (x noopImageInterface) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
605}
606
Colin Crossbaccf5b2018-02-21 14:07:48 -0800607// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
608func pathToSandboxOut(path android.Path, genDir android.Path) string {
609 relOut, err := filepath.Rel(genDir.String(), path.String())
610 if err != nil {
611 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
612 }
613 return filepath.Join("__SBOX_OUT_DIR__", relOut)
614
615}
616
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700617func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700618 properties := &genSrcsProperties{}
619
Colin Cross1a527682019-09-23 15:55:30 -0700620 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
621 genDir := android.PathForModuleGen(ctx, "gensrcs")
622 shardSize := defaultShardSize
623 if s := properties.Shard_size; s != nil {
624 shardSize = int(*s)
625 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800626
Colin Cross1a527682019-09-23 15:55:30 -0700627 shards := android.ShardPaths(srcFiles, shardSize)
628 var generateTasks []generateTask
Colin Crossbaccf5b2018-02-21 14:07:48 -0800629
Colin Cross1a527682019-09-23 15:55:30 -0700630 for i, shard := range shards {
631 var commands []string
632 var outFiles android.WritablePaths
633 var copyTo android.WritablePaths
634 var shardDir android.WritablePath
635 var sandboxOuts []string
636
637 if len(shards) > 1 {
638 shardDir = android.PathForModuleGen(ctx, strconv.Itoa(i))
639 } else {
640 shardDir = genDir
Jeff Gaston437d23c2017-11-08 12:38:00 -0800641 }
642
Colin Cross1a527682019-09-23 15:55:30 -0700643 for _, in := range shard {
644 outFile := android.GenPathWithExt(ctx, "gensrcs", in, String(properties.Output_extension))
645 sandboxOutfile := pathToSandboxOut(outFile, genDir)
Jeff Gaston437d23c2017-11-08 12:38:00 -0800646
Colin Cross1a527682019-09-23 15:55:30 -0700647 if len(shards) > 1 {
648 shardFile := android.GenPathWithExt(ctx, strconv.Itoa(i), in, String(properties.Output_extension))
649 copyTo = append(copyTo, outFile)
650 outFile = shardFile
651 }
652
653 outFiles = append(outFiles, outFile)
654 sandboxOuts = append(sandboxOuts, sandboxOutfile)
655
656 command, err := android.Expand(rawCommand, func(name string) (string, error) {
657 switch name {
658 case "in":
659 return in.String(), nil
660 case "out":
661 return sandboxOutfile, nil
662 default:
663 return "$(" + name + ")", nil
664 }
665 })
666 if err != nil {
667 ctx.PropertyErrorf("cmd", err.Error())
668 }
669
670 // escape the command in case for example it contains '#', an odd number of '"', etc
671 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
672 commands = append(commands, command)
673 }
674 fullCommand := strings.Join(commands, " && ")
675
676 generateTasks = append(generateTasks, generateTask{
677 in: shard,
678 out: outFiles,
679 copyTo: copyTo,
680 genDir: shardDir,
681 sandboxOuts: sandboxOuts,
682 cmd: fullCommand,
683 shard: i,
684 shards: len(shards),
685 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800686 }
Colin Cross1a527682019-09-23 15:55:30 -0700687
688 return generateTasks
Colin Crossd350ecd2015-04-28 13:25:36 -0700689 }
690
Colin Cross1a527682019-09-23 15:55:30 -0700691 g := generatorFactory(taskGenerator, properties)
692 g.subDir = "gensrcs"
693 return g
Colin Crossd350ecd2015-04-28 13:25:36 -0700694}
695
Colin Cross54190b32017-10-09 15:34:10 -0700696func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700697 m := NewGenSrcs()
698 android.InitAndroidModule(m)
699 return m
700}
701
Colin Crossd350ecd2015-04-28 13:25:36 -0700702type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700703 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800704 Output_extension *string
Colin Cross1a527682019-09-23 15:55:30 -0700705
706 // maximum number of files that will be passed on a single command line.
707 Shard_size *int64
Colin Cross5049f022015-03-18 13:28:46 -0700708}
709
Colin Cross1a527682019-09-23 15:55:30 -0700710const defaultShardSize = 100
711
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700712func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700713 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700714
Colin Cross1a527682019-09-23 15:55:30 -0700715 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700716 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800717 sandboxOuts := make([]string, len(properties.Out))
718 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700719 for i, out := range properties.Out {
720 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800721 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700722 }
Colin Cross1a527682019-09-23 15:55:30 -0700723 return []generateTask{{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800724 in: srcFiles,
725 out: outs,
Colin Cross1a527682019-09-23 15:55:30 -0700726 genDir: android.PathForModuleGen(ctx),
Colin Crossbaccf5b2018-02-21 14:07:48 -0800727 sandboxOuts: sandboxOuts,
728 cmd: rawCommand,
Colin Cross1a527682019-09-23 15:55:30 -0700729 }}
Colin Cross5049f022015-03-18 13:28:46 -0700730 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700731
Jeff Gaston437d23c2017-11-08 12:38:00 -0800732 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700733}
734
Colin Cross54190b32017-10-09 15:34:10 -0700735func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700736 m := NewGenRule()
737 android.InitAndroidModule(m)
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800738 android.InitDefaultableModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700739 return m
740}
741
Colin Crossd350ecd2015-04-28 13:25:36 -0700742type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700743 // names of the output files that will be generated
Colin Crossef354482018-10-23 11:27:50 -0700744 Out []string `android:"arch_variant"`
Colin Cross5049f022015-03-18 13:28:46 -0700745}
Nan Zhangea568a42017-11-08 21:20:04 -0800746
747var Bool = proptools.Bool
748var String = proptools.String
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800749
750//
751// Defaults
752//
753type Defaults struct {
754 android.ModuleBase
755 android.DefaultsModuleBase
756}
757
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800758func defaultsFactory() android.Module {
759 return DefaultsFactory()
760}
761
762func DefaultsFactory(props ...interface{}) android.Module {
763 module := &Defaults{}
764
765 module.AddProperties(props...)
766 module.AddProperties(
767 &generatorProperties{},
768 &genRuleProperties{},
769 )
770
771 android.InitDefaultsModule(module)
772
773 return module
774}