blob: a7c5d65f692efe3c307dee74c16239e16ebc9433 [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"
29 "path/filepath"
Colin Cross5049f022015-03-18 13:28:46 -070030)
31
Colin Cross463a90e2015-06-17 14:20:06 -070032func init() {
Jaewoong Jung98716bd2018-12-10 08:13:18 -080033 android.RegisterModuleType("genrule_defaults", defaultsFactory)
34
Colin Cross54190b32017-10-09 15:34:10 -070035 android.RegisterModuleType("gensrcs", GenSrcsFactory)
36 android.RegisterModuleType("genrule", GenRuleFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070037}
38
Colin Cross5049f022015-03-18 13:28:46 -070039var (
Colin Cross635c3b02016-05-18 15:37:25 -070040 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross1a527682019-09-23 15:55:30 -070041
42 gensrcsMerge = pctx.AndroidStaticRule("gensrcsMerge", blueprint.RuleParams{
43 Command: "${soongZip} -o ${tmpZip} @${tmpZip}.rsp && ${zipSync} -d ${genDir} ${tmpZip}",
44 CommandDeps: []string{"${soongZip}", "${zipSync}"},
45 Rspfile: "${tmpZip}.rsp",
46 RspfileContent: "${zipArgs}",
47 }, "tmpZip", "genDir", "zipArgs")
Colin Cross5049f022015-03-18 13:28:46 -070048)
49
Jeff Gastonefc1b412017-03-29 17:29:06 -070050func init() {
Dan Willemsenddf504c2019-08-09 16:21:29 -070051 pctx.Import("android/soong/android")
Jeff Gastonefc1b412017-03-29 17:29:06 -070052 pctx.HostBinToolVariable("sboxCmd", "sbox")
Colin Cross1a527682019-09-23 15:55:30 -070053
54 pctx.HostBinToolVariable("soongZip", "soong_zip")
55 pctx.HostBinToolVariable("zipSync", "zipsync")
Jeff Gastonefc1b412017-03-29 17:29:06 -070056}
57
Colin Cross5049f022015-03-18 13:28:46 -070058type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070059 GeneratedSourceFiles() android.Paths
Colin Cross5ed99c62016-11-22 12:55:55 -080060 GeneratedHeaderDirs() android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -080061 GeneratedDeps() android.Paths
Colin Cross5049f022015-03-18 13:28:46 -070062}
63
Colin Crossfe17f6f2019-03-28 19:30:56 -070064// Alias for android.HostToolProvider
65// Deprecated: use android.HostToolProvider instead.
Colin Crossd350ecd2015-04-28 13:25:36 -070066type HostToolProvider interface {
Colin Crossfe17f6f2019-03-28 19:30:56 -070067 android.HostToolProvider
Colin Crossd350ecd2015-04-28 13:25:36 -070068}
Colin Cross5049f022015-03-18 13:28:46 -070069
Dan Willemsend6ba0d52017-09-13 15:46:47 -070070type hostToolDependencyTag struct {
71 blueprint.BaseDependencyTag
Colin Cross08f15ab2018-10-04 23:29:14 -070072 label string
Dan Willemsend6ba0d52017-09-13 15:46:47 -070073}
74
Colin Cross7d5136f2015-05-11 13:39:40 -070075type generatorProperties struct {
Jeff Gastonefc1b412017-03-29 17:29:06 -070076 // The command to run on one or more input files. Cmd supports substitution of a few variables
77 // (the actual substitution is implemented in GenerateAndroidBuildActions below)
78 //
79 // Available variables for substitution:
80 //
Colin Cross2296f5b2017-10-17 21:38:14 -070081 // $(location): the path to the first entry in tools or tool_files
Colin Cross08f15ab2018-10-04 23:29:14 -070082 // $(location <label>): the path to the tool, tool_file, input or output with name <label>
Colin Cross2296f5b2017-10-17 21:38:14 -070083 // $(in): one or more input files
84 // $(out): a single output file
85 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
86 // $(genDir): the sandbox directory for this tool; contains $(out)
87 // $$: a literal $
Colin Cross6f080df2016-11-04 15:32:58 -070088 //
Jeff Gastonefc1b412017-03-29 17:29:06 -070089 // All files used must be declared as inputs (to ensure proper up-to-date checks).
90 // Use "$(in)" directly in Cmd to ensure that all inputs used are declared.
Nan Zhangea568a42017-11-08 21:20:04 -080091 Cmd *string
Colin Cross7d5136f2015-05-11 13:39:40 -070092
Colin Cross33bfb0a2016-11-21 17:23:08 -080093 // Enable reading a file containing dependencies in gcc format after the command completes
Nan Zhangea568a42017-11-08 21:20:04 -080094 Depfile *bool
Colin Cross33bfb0a2016-11-21 17:23:08 -080095
Colin Cross6f080df2016-11-04 15:32:58 -070096 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -070097 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -070098 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070099
100 // Local file that is used as the tool
Colin Cross27b922f2019-03-04 22:35:41 -0800101 Tool_files []string `android:"path"`
Colin Cross5ed99c62016-11-22 12:55:55 -0800102
103 // List of directories to export generated headers from
104 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -0800105
106 // list of input files
Colin Cross27b922f2019-03-04 22:35:41 -0800107 Srcs []string `android:"path,arch_variant"`
Dan Willemseneefa0262018-11-17 14:01:18 -0800108
109 // input files to exclude
Colin Cross27b922f2019-03-04 22:35:41 -0800110 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700111}
112
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700113type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700114 android.ModuleBase
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800115 android.DefaultableModuleBase
Jiyong Parkfc752ca2019-06-12 13:27:29 +0900116 android.ApexModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -0700117
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700118 // For other packages to make their own genrules with extra
119 // properties
120 Extra interface{}
121
Colin Cross7d5136f2015-05-11 13:39:40 -0700122 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700123
Jeff Gaston437d23c2017-11-08 12:38:00 -0800124 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700125
Colin Cross1a527682019-09-23 15:55:30 -0700126 deps android.Paths
127 rule blueprint.Rule
128 rawCommands []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700129
Colin Cross5ed99c62016-11-22 12:55:55 -0800130 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700131
Colin Cross635c3b02016-05-18 15:37:25 -0700132 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800133 outputDeps android.Paths
Colin Crossa4ad2b02019-03-18 22:15:32 -0700134
135 subName string
Colin Cross1a527682019-09-23 15:55:30 -0700136 subDir string
Colin Crossd350ecd2015-04-28 13:25:36 -0700137}
138
Colin Cross1a527682019-09-23 15:55:30 -0700139type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700140
141type generateTask struct {
Colin Crossbaccf5b2018-02-21 14:07:48 -0800142 in android.Paths
143 out android.WritablePaths
Colin Cross1a527682019-09-23 15:55:30 -0700144 copyTo android.WritablePaths
145 genDir android.WritablePath
Colin Crossbaccf5b2018-02-21 14:07:48 -0800146 sandboxOuts []string
147 cmd string
Colin Cross1a527682019-09-23 15:55:30 -0700148 shard int
149 shards int
Colin Crossd350ecd2015-04-28 13:25:36 -0700150}
151
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700152func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700153 return g.outputFiles
154}
155
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700156func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700157 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800158}
159
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700160func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800161 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700162}
163
Dan Willemsen9da9d492018-02-21 18:28:18 -0800164func (g *Module) GeneratedDeps() android.Paths {
165 return g.outputDeps
166}
167
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700168func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700169 if g, ok := ctx.Module().(*Module); ok {
Colin Cross08f15ab2018-10-04 23:29:14 -0700170 for _, tool := range g.properties.Tools {
171 tag := hostToolDependencyTag{label: tool}
172 if m := android.SrcIsModule(tool); m != "" {
173 tool = m
174 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700175 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700176 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700177 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700178}
179
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700180func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa4ad2b02019-03-18 22:15:32 -0700181 g.subName = ctx.ModuleSubDir()
182
Colin Cross5ed99c62016-11-22 12:55:55 -0800183 if len(g.properties.Export_include_dirs) > 0 {
184 for _, dir := range g.properties.Export_include_dirs {
185 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
Colin Cross1a527682019-09-23 15:55:30 -0700186 android.PathForModuleGen(ctx, g.subDir, ctx.ModuleDir(), dir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800187 }
188 } else {
Colin Cross1a527682019-09-23 15:55:30 -0700189 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, g.subDir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800190 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700191
Colin Cross08f15ab2018-10-04 23:29:14 -0700192 locationLabels := map[string][]string{}
193 firstLabel := ""
194
195 addLocationLabel := func(label string, paths []string) {
196 if firstLabel == "" {
197 firstLabel = label
198 }
199 if _, exists := locationLabels[label]; !exists {
200 locationLabels[label] = paths
201 } else {
202 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
203 label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
204 }
205 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700206
Colin Cross6f080df2016-11-04 15:32:58 -0700207 if len(g.properties.Tools) > 0 {
Colin Crossba71a3f2019-03-18 12:12:48 -0700208 seenTools := make(map[string]bool)
209
Colin Cross35143d02017-11-16 00:11:20 -0800210 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700211 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
212 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700213 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700214 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700215
Colin Crossfe17f6f2019-03-28 19:30:56 -0700216 if t, ok := module.(android.HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800217 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800218 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800219 ctx.AddMissingDependencies([]string{tool})
220 } else {
221 ctx.ModuleErrorf("depends on disabled module %q", tool)
222 }
223 break
224 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700225 path = t.HostToolPath()
226 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
227 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
228 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700229 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700230 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
231 break
Colin Cross6f080df2016-11-04 15:32:58 -0700232 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700233 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700234 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700235 break
236 }
237
238 if path.Valid() {
239 g.deps = append(g.deps, path.Path())
Colin Cross08f15ab2018-10-04 23:29:14 -0700240 addLocationLabel(tag.label, []string{path.Path().String()})
Colin Crossba71a3f2019-03-18 12:12:48 -0700241 seenTools[tag.label] = true
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700242 } else {
243 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700244 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700245 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700246 })
Colin Crossba71a3f2019-03-18 12:12:48 -0700247
248 // If AllowMissingDependencies is enabled, the build will not have stopped when
249 // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical
250 // "cmd: unknown location label ..." errors later. Add a dummy file to the local label. The
251 // command that uses this dummy file will never be executed because the rule will be replaced with
252 // an android.Error rule reporting the missing dependencies.
253 if ctx.Config().AllowMissingDependencies() {
254 for _, tool := range g.properties.Tools {
255 if !seenTools[tool] {
256 addLocationLabel(tool, []string{"***missing tool " + tool + "***"})
257 }
258 }
259 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700260 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700261
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700262 if ctx.Failed() {
263 return
264 }
265
Colin Cross08f15ab2018-10-04 23:29:14 -0700266 for _, toolFile := range g.properties.Tool_files {
Colin Cross8a497952019-03-05 22:25:09 -0800267 paths := android.PathsForModuleSrc(ctx, []string{toolFile})
Colin Cross08f15ab2018-10-04 23:29:14 -0700268 g.deps = append(g.deps, paths...)
269 addLocationLabel(toolFile, paths.Strings())
270 }
271
272 var srcFiles android.Paths
273 for _, in := range g.properties.Srcs {
Colin Crossba71a3f2019-03-18 12:12:48 -0700274 paths, missingDeps := android.PathsAndMissingDepsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs)
275 if len(missingDeps) > 0 {
276 if !ctx.Config().AllowMissingDependencies() {
277 panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator",
278 missingDeps))
279 }
280
281 // If AllowMissingDependencies is enabled, the build will not have stopped when
282 // the dependency was added on a missing SourceFileProducer module, which will result in nonsensical
283 // "cmd: label ":..." has no files" errors later. Add a dummy file to the local label. The
284 // command that uses this dummy file will never be executed because the rule will be replaced with
285 // an android.Error rule reporting the missing dependencies.
286 ctx.AddMissingDependencies(missingDeps)
287 addLocationLabel(in, []string{"***missing srcs " + in + "***"})
288 } else {
289 srcFiles = append(srcFiles, paths...)
290 addLocationLabel(in, paths.Strings())
291 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700292 }
293
Colin Cross1a527682019-09-23 15:55:30 -0700294 var copyFrom android.Paths
295 var outputFiles android.WritablePaths
296 var zipArgs strings.Builder
Colin Cross08f15ab2018-10-04 23:29:14 -0700297
Colin Cross1a527682019-09-23 15:55:30 -0700298 for _, task := range g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles) {
299 for _, out := range task.out {
300 addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
Colin Cross85a2e892018-07-09 09:45:06 -0700301 }
302
Colin Cross1a527682019-09-23 15:55:30 -0700303 referencedDepfile := false
304
305 rawCommand, err := android.ExpandNinjaEscaped(task.cmd, func(name string) (string, bool, error) {
306 // report the error directly without returning an error to android.Expand to catch multiple errors in a
307 // single run
308 reportError := func(fmt string, args ...interface{}) (string, bool, error) {
309 ctx.PropertyErrorf("cmd", fmt, args...)
310 return "SOONG_ERROR", false, nil
Colin Cross6f080df2016-11-04 15:32:58 -0700311 }
Colin Cross1a527682019-09-23 15:55:30 -0700312
313 switch name {
314 case "location":
315 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
316 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700317 }
Colin Cross1a527682019-09-23 15:55:30 -0700318 paths := locationLabels[firstLabel]
319 if len(paths) == 0 {
320 return reportError("default label %q has no files", firstLabel)
321 } else if len(paths) > 1 {
322 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
323 firstLabel, firstLabel)
Colin Cross08f15ab2018-10-04 23:29:14 -0700324 }
Colin Cross1a527682019-09-23 15:55:30 -0700325 return locationLabels[firstLabel][0], false, nil
326 case "in":
327 return "${in}", true, nil
328 case "out":
329 return "__SBOX_OUT_FILES__", false, nil
330 case "depfile":
331 referencedDepfile = true
332 if !Bool(g.properties.Depfile) {
333 return reportError("$(depfile) used without depfile property")
334 }
335 return "__SBOX_DEPFILE__", false, nil
336 case "genDir":
337 return "__SBOX_OUT_DIR__", false, nil
338 default:
339 if strings.HasPrefix(name, "location ") {
340 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
341 if paths, ok := locationLabels[label]; ok {
342 if len(paths) == 0 {
343 return reportError("label %q has no files", label)
344 } else if len(paths) > 1 {
345 return reportError("label %q has multiple files, use $(locations %s) to reference it",
346 label, label)
347 }
348 return paths[0], false, nil
349 } else {
350 return reportError("unknown location label %q", label)
351 }
352 } else if strings.HasPrefix(name, "locations ") {
353 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
354 if paths, ok := locationLabels[label]; ok {
355 if len(paths) == 0 {
356 return reportError("label %q has no files", label)
357 }
358 return strings.Join(paths, " "), false, nil
359 } else {
360 return reportError("unknown locations label %q", label)
361 }
362 } else {
363 return reportError("unknown variable '$(%s)'", name)
364 }
Colin Cross6f080df2016-11-04 15:32:58 -0700365 }
Colin Cross1a527682019-09-23 15:55:30 -0700366 })
367
368 if err != nil {
369 ctx.PropertyErrorf("cmd", "%s", err.Error())
370 return
Colin Cross6f080df2016-11-04 15:32:58 -0700371 }
Colin Cross6f080df2016-11-04 15:32:58 -0700372
Colin Cross1a527682019-09-23 15:55:30 -0700373 if Bool(g.properties.Depfile) && !referencedDepfile {
374 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
375 return
376 }
377
378 // tell the sbox command which directory to use as its sandbox root
379 buildDir := android.PathForOutput(ctx).String()
380 sandboxPath := shared.TempDirForOutDir(buildDir)
381
382 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
383 // to be replaced later by ninja_strings.go
384 depfilePlaceholder := ""
385 if Bool(g.properties.Depfile) {
386 depfilePlaceholder = "$depfileArgs"
387 }
388
389 // Escape the command for the shell
390 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
391 g.rawCommands = append(g.rawCommands, rawCommand)
392 sandboxCommand := fmt.Sprintf("rm -rf %s && $sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
393 task.genDir, sandboxPath, task.genDir, rawCommand, depfilePlaceholder)
394
395 ruleParams := blueprint.RuleParams{
396 Command: sandboxCommand,
397 CommandDeps: []string{"$sboxCmd"},
398 }
399 args := []string{"allouts"}
400 if Bool(g.properties.Depfile) {
401 ruleParams.Deps = blueprint.DepsGCC
402 args = append(args, "depfileArgs")
403 }
404 name := "generator"
405 if task.shards > 1 {
406 name += strconv.Itoa(task.shard)
407 }
408 rule := ctx.Rule(pctx, name, ruleParams, args...)
409
410 g.generateSourceFile(ctx, task, rule)
411
412 if len(task.copyTo) > 0 {
413 outputFiles = append(outputFiles, task.copyTo...)
414 copyFrom = append(copyFrom, task.out.Paths()...)
415 zipArgs.WriteString(" -C " + task.genDir.String())
416 zipArgs.WriteString(android.JoinWithPrefix(task.out.Strings(), " -f "))
417 } else {
418 outputFiles = append(outputFiles, task.out...)
419 }
Colin Cross6f080df2016-11-04 15:32:58 -0700420 }
421
Colin Cross1a527682019-09-23 15:55:30 -0700422 if len(copyFrom) > 0 {
423 ctx.Build(pctx, android.BuildParams{
424 Rule: gensrcsMerge,
425 Implicits: copyFrom,
426 Outputs: outputFiles,
427 Args: map[string]string{
428 "zipArgs": zipArgs.String(),
429 "tmpZip": android.PathForModuleGen(ctx, g.subDir+".zip").String(),
430 "genDir": android.PathForModuleGen(ctx, g.subDir).String(),
431 },
432 })
Colin Cross85a2e892018-07-09 09:45:06 -0700433 }
434
Colin Cross1a527682019-09-23 15:55:30 -0700435 g.outputFiles = outputFiles.Paths()
Jeff Gastonefc1b412017-03-29 17:29:06 -0700436
Colin Cross1a527682019-09-23 15:55:30 -0700437 // For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of
438 // the genrules on AOSP. That will make things simpler to look at the graph in the common
439 // case. For larger sets of outputs, inject a phony target in between to limit ninja file
440 // growth.
441 if len(g.outputFiles) <= 6 {
442 g.outputDeps = g.outputFiles
443 } else {
444 phonyFile := android.PathForModuleGen(ctx, "genrule-phony")
445
446 ctx.Build(pctx, android.BuildParams{
447 Rule: blueprint.Phony,
448 Output: phonyFile,
449 Inputs: g.outputFiles,
450 })
451
452 g.outputDeps = android.Paths{phonyFile}
Jeff Gaston02a684b2017-10-27 14:59:27 -0700453 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800454
Colin Crossd350ecd2015-04-28 13:25:36 -0700455}
456
Colin Cross1a527682019-09-23 15:55:30 -0700457func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask, rule blueprint.Rule) {
Colin Cross67a5c132017-05-09 13:45:28 -0700458 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700459 if len(task.out) == 0 {
460 ctx.ModuleErrorf("must have at least one output file")
461 return
462 }
Colin Cross67a5c132017-05-09 13:45:28 -0700463 if len(task.out) == 1 {
464 desc += " " + task.out[0].Base()
465 }
466
Jeff Gaston02a684b2017-10-27 14:59:27 -0700467 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800468 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700469 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
470 }
471
Colin Cross1a527682019-09-23 15:55:30 -0700472 if task.shards > 1 {
473 desc += " " + strconv.Itoa(task.shard)
474 }
475
Colin Crossae887032017-10-23 17:16:14 -0700476 params := android.BuildParams{
Colin Cross1a527682019-09-23 15:55:30 -0700477 Rule: rule,
478 Description: desc,
Colin Cross15e86d92017-10-20 15:07:08 -0700479 Output: task.out[0],
480 ImplicitOutputs: task.out[1:],
481 Inputs: task.in,
482 Implicits: g.deps,
483 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800484 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700485 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800486 }
Nan Zhangea568a42017-11-08 21:20:04 -0800487 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700488 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
489 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800490 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700491
Colin Crossae887032017-10-23 17:16:14 -0700492 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700493}
494
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700495// Collect information for opening IDE project files in java/jdeps.go.
496func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
497 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
498 for _, src := range g.properties.Srcs {
499 if strings.HasPrefix(src, ":") {
500 src = strings.Trim(src, ":")
501 dpInfo.Deps = append(dpInfo.Deps, src)
502 }
503 }
504}
505
Colin Crossa4ad2b02019-03-18 22:15:32 -0700506func (g *Module) AndroidMk() android.AndroidMkData {
507 return android.AndroidMkData{
508 Include: "$(BUILD_PHONY_PACKAGE)",
509 Class: "FAKE",
510 OutputFile: android.OptionalPathForPath(g.outputFiles[0]),
511 SubName: g.subName,
512 Extra: []android.AndroidMkExtraFunc{
513 func(w io.Writer, outputFile android.Path) {
Colin Cross1a527682019-09-23 15:55:30 -0700514 fmt.Fprintln(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=", strings.Join(g.outputDeps.Strings(), " "))
Colin Crossa4ad2b02019-03-18 22:15:32 -0700515 },
516 },
517 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
518 android.WriteAndroidMkData(w, data)
519 if data.SubName != "" {
520 fmt.Fprintln(w, ".PHONY:", name)
521 fmt.Fprintln(w, name, ":", name+g.subName)
522 }
523 },
524 }
525}
526
Jeff Gaston437d23c2017-11-08 12:38:00 -0800527func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700528 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800529 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700530 }
531
Colin Cross36242852017-06-23 15:06:31 -0700532 module.AddProperties(props...)
533 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700534
Colin Cross36242852017-06-23 15:06:31 -0700535 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700536}
537
Colin Crossbaccf5b2018-02-21 14:07:48 -0800538// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
539func pathToSandboxOut(path android.Path, genDir android.Path) string {
540 relOut, err := filepath.Rel(genDir.String(), path.String())
541 if err != nil {
542 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
543 }
544 return filepath.Join("__SBOX_OUT_DIR__", relOut)
545
546}
547
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700548func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700549 properties := &genSrcsProperties{}
550
Colin Cross1a527682019-09-23 15:55:30 -0700551 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
552 genDir := android.PathForModuleGen(ctx, "gensrcs")
553 shardSize := defaultShardSize
554 if s := properties.Shard_size; s != nil {
555 shardSize = int(*s)
556 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800557
Colin Cross1a527682019-09-23 15:55:30 -0700558 shards := android.ShardPaths(srcFiles, shardSize)
559 var generateTasks []generateTask
Colin Crossbaccf5b2018-02-21 14:07:48 -0800560
Colin Cross1a527682019-09-23 15:55:30 -0700561 for i, shard := range shards {
562 var commands []string
563 var outFiles android.WritablePaths
564 var copyTo android.WritablePaths
565 var shardDir android.WritablePath
566 var sandboxOuts []string
567
568 if len(shards) > 1 {
569 shardDir = android.PathForModuleGen(ctx, strconv.Itoa(i))
570 } else {
571 shardDir = genDir
Jeff Gaston437d23c2017-11-08 12:38:00 -0800572 }
573
Colin Cross1a527682019-09-23 15:55:30 -0700574 for _, in := range shard {
575 outFile := android.GenPathWithExt(ctx, "gensrcs", in, String(properties.Output_extension))
576 sandboxOutfile := pathToSandboxOut(outFile, genDir)
Jeff Gaston437d23c2017-11-08 12:38:00 -0800577
Colin Cross1a527682019-09-23 15:55:30 -0700578 if len(shards) > 1 {
579 shardFile := android.GenPathWithExt(ctx, strconv.Itoa(i), in, String(properties.Output_extension))
580 copyTo = append(copyTo, outFile)
581 outFile = shardFile
582 }
583
584 outFiles = append(outFiles, outFile)
585 sandboxOuts = append(sandboxOuts, sandboxOutfile)
586
587 command, err := android.Expand(rawCommand, func(name string) (string, error) {
588 switch name {
589 case "in":
590 return in.String(), nil
591 case "out":
592 return sandboxOutfile, nil
593 default:
594 return "$(" + name + ")", nil
595 }
596 })
597 if err != nil {
598 ctx.PropertyErrorf("cmd", err.Error())
599 }
600
601 // escape the command in case for example it contains '#', an odd number of '"', etc
602 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
603 commands = append(commands, command)
604 }
605 fullCommand := strings.Join(commands, " && ")
606
607 generateTasks = append(generateTasks, generateTask{
608 in: shard,
609 out: outFiles,
610 copyTo: copyTo,
611 genDir: shardDir,
612 sandboxOuts: sandboxOuts,
613 cmd: fullCommand,
614 shard: i,
615 shards: len(shards),
616 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800617 }
Colin Cross1a527682019-09-23 15:55:30 -0700618
619 return generateTasks
Colin Crossd350ecd2015-04-28 13:25:36 -0700620 }
621
Colin Cross1a527682019-09-23 15:55:30 -0700622 g := generatorFactory(taskGenerator, properties)
623 g.subDir = "gensrcs"
624 return g
Colin Crossd350ecd2015-04-28 13:25:36 -0700625}
626
Colin Cross54190b32017-10-09 15:34:10 -0700627func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700628 m := NewGenSrcs()
629 android.InitAndroidModule(m)
630 return m
631}
632
Colin Crossd350ecd2015-04-28 13:25:36 -0700633type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700634 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800635 Output_extension *string
Colin Cross1a527682019-09-23 15:55:30 -0700636
637 // maximum number of files that will be passed on a single command line.
638 Shard_size *int64
Colin Cross5049f022015-03-18 13:28:46 -0700639}
640
Colin Cross1a527682019-09-23 15:55:30 -0700641const defaultShardSize = 100
642
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700643func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700644 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700645
Colin Cross1a527682019-09-23 15:55:30 -0700646 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700647 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800648 sandboxOuts := make([]string, len(properties.Out))
649 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700650 for i, out := range properties.Out {
651 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800652 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700653 }
Colin Cross1a527682019-09-23 15:55:30 -0700654 return []generateTask{{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800655 in: srcFiles,
656 out: outs,
Colin Cross1a527682019-09-23 15:55:30 -0700657 genDir: android.PathForModuleGen(ctx),
Colin Crossbaccf5b2018-02-21 14:07:48 -0800658 sandboxOuts: sandboxOuts,
659 cmd: rawCommand,
Colin Cross1a527682019-09-23 15:55:30 -0700660 }}
Colin Cross5049f022015-03-18 13:28:46 -0700661 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700662
Jeff Gaston437d23c2017-11-08 12:38:00 -0800663 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700664}
665
Colin Cross54190b32017-10-09 15:34:10 -0700666func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700667 m := NewGenRule()
668 android.InitAndroidModule(m)
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800669 android.InitDefaultableModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700670 return m
671}
672
Colin Crossd350ecd2015-04-28 13:25:36 -0700673type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700674 // names of the output files that will be generated
Colin Crossef354482018-10-23 11:27:50 -0700675 Out []string `android:"arch_variant"`
Colin Cross5049f022015-03-18 13:28:46 -0700676}
Nan Zhangea568a42017-11-08 21:20:04 -0800677
678var Bool = proptools.Bool
679var String = proptools.String
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800680
681//
682// Defaults
683//
684type Defaults struct {
685 android.ModuleBase
686 android.DefaultsModuleBase
687}
688
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800689func defaultsFactory() android.Module {
690 return DefaultsFactory()
691}
692
693func DefaultsFactory(props ...interface{}) android.Module {
694 module := &Defaults{}
695
696 module.AddProperties(props...)
697 module.AddProperties(
698 &generatorProperties{},
699 &genRuleProperties{},
700 )
701
702 android.InitDefaultsModule(module)
703
704 return module
705}