blob: 8b8087155c68aa3abbde6b3989029f9ce5fc08e3 [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
86 // (the actual substitution is implemented in GenerateAndroidBuildActions below)
87 //
88 // Available variables for substitution:
89 //
Colin Cross2296f5b2017-10-17 21:38:14 -070090 // $(location): the path to the first entry in tools or tool_files
Colin Cross08f15ab2018-10-04 23:29:14 -070091 // $(location <label>): the path to the tool, tool_file, input or output with name <label>
Colin Cross2296f5b2017-10-17 21:38:14 -070092 // $(in): one or more input files
93 // $(out): a single output file
94 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
95 // $(genDir): the sandbox directory for this tool; contains $(out)
96 // $$: a literal $
Colin Cross6f080df2016-11-04 15:32:58 -070097 //
Jeff Gastonefc1b412017-03-29 17:29:06 -070098 // All files used must be declared as inputs (to ensure proper up-to-date checks).
99 // Use "$(in)" directly in Cmd to ensure that all inputs used are declared.
Nan Zhangea568a42017-11-08 21:20:04 -0800100 Cmd *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700101
Colin Cross33bfb0a2016-11-21 17:23:08 -0800102 // Enable reading a file containing dependencies in gcc format after the command completes
Nan Zhangea568a42017-11-08 21:20:04 -0800103 Depfile *bool
Colin Cross33bfb0a2016-11-21 17:23:08 -0800104
Colin Cross6f080df2016-11-04 15:32:58 -0700105 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -0700106 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -0700107 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700108
109 // Local file that is used as the tool
Colin Cross27b922f2019-03-04 22:35:41 -0800110 Tool_files []string `android:"path"`
Colin Cross5ed99c62016-11-22 12:55:55 -0800111
112 // List of directories to export generated headers from
113 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -0800114
115 // list of input files
Colin Cross27b922f2019-03-04 22:35:41 -0800116 Srcs []string `android:"path,arch_variant"`
Dan Willemseneefa0262018-11-17 14:01:18 -0800117
118 // input files to exclude
Colin Cross27b922f2019-03-04 22:35:41 -0800119 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700120}
121
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700122type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700123 android.ModuleBase
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800124 android.DefaultableModuleBase
Jiyong Parkfc752ca2019-06-12 13:27:29 +0900125 android.ApexModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -0700126
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700127 // For other packages to make their own genrules with extra
128 // properties
129 Extra interface{}
Colin Cross7228ecd2019-11-18 16:00:16 -0800130 android.ImageInterface
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700131
Colin Cross7d5136f2015-05-11 13:39:40 -0700132 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700133
Jeff Gaston437d23c2017-11-08 12:38:00 -0800134 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700135
Colin Cross1a527682019-09-23 15:55:30 -0700136 deps android.Paths
137 rule blueprint.Rule
138 rawCommands []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700139
Colin Cross5ed99c62016-11-22 12:55:55 -0800140 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700141
Colin Cross635c3b02016-05-18 15:37:25 -0700142 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800143 outputDeps android.Paths
Colin Crossa4ad2b02019-03-18 22:15:32 -0700144
145 subName string
Colin Cross1a527682019-09-23 15:55:30 -0700146 subDir string
bralee1fbf4402020-05-21 10:11:59 +0800147
148 // Collect the module directory for IDE info in java/jdeps.go.
149 modulePaths []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700150}
151
Colin Cross1a527682019-09-23 15:55:30 -0700152type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700153
154type generateTask struct {
Colin Crossbaccf5b2018-02-21 14:07:48 -0800155 in android.Paths
156 out android.WritablePaths
Colin Cross1a527682019-09-23 15:55:30 -0700157 copyTo android.WritablePaths
158 genDir android.WritablePath
Colin Crossbaccf5b2018-02-21 14:07:48 -0800159 sandboxOuts []string
160 cmd string
Colin Cross1a527682019-09-23 15:55:30 -0700161 shard int
162 shards int
Colin Crossd350ecd2015-04-28 13:25:36 -0700163}
164
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700165func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700166 return g.outputFiles
167}
168
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700169func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700170 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800171}
172
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700173func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800174 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700175}
176
Dan Willemsen9da9d492018-02-21 18:28:18 -0800177func (g *Module) GeneratedDeps() android.Paths {
178 return g.outputDeps
179}
180
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000181func toolDepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700182 if g, ok := ctx.Module().(*Module); ok {
Colin Cross08f15ab2018-10-04 23:29:14 -0700183 for _, tool := range g.properties.Tools {
184 tag := hostToolDependencyTag{label: tool}
185 if m := android.SrcIsModule(tool); m != "" {
186 tool = m
187 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700188 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700189 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700190 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700191}
192
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700193func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa4ad2b02019-03-18 22:15:32 -0700194 g.subName = ctx.ModuleSubDir()
195
bralee1fbf4402020-05-21 10:11:59 +0800196 // Collect the module directory for IDE info in java/jdeps.go.
197 g.modulePaths = append(g.modulePaths, ctx.ModuleDir())
198
Colin Cross5ed99c62016-11-22 12:55:55 -0800199 if len(g.properties.Export_include_dirs) > 0 {
200 for _, dir := range g.properties.Export_include_dirs {
201 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
Colin Cross1a527682019-09-23 15:55:30 -0700202 android.PathForModuleGen(ctx, g.subDir, ctx.ModuleDir(), dir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800203 }
204 } else {
Colin Cross1a527682019-09-23 15:55:30 -0700205 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, g.subDir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800206 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700207
Colin Cross08f15ab2018-10-04 23:29:14 -0700208 locationLabels := map[string][]string{}
209 firstLabel := ""
210
211 addLocationLabel := func(label string, paths []string) {
212 if firstLabel == "" {
213 firstLabel = label
214 }
215 if _, exists := locationLabels[label]; !exists {
216 locationLabels[label] = paths
217 } else {
218 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
219 label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
220 }
221 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700222
Colin Cross6f080df2016-11-04 15:32:58 -0700223 if len(g.properties.Tools) > 0 {
Colin Crossba71a3f2019-03-18 12:12:48 -0700224 seenTools := make(map[string]bool)
225
Colin Cross35143d02017-11-16 00:11:20 -0800226 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700227 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
228 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700229 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700230 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700231
Colin Crossfe17f6f2019-03-28 19:30:56 -0700232 if t, ok := module.(android.HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800233 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800234 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800235 ctx.AddMissingDependencies([]string{tool})
236 } else {
237 ctx.ModuleErrorf("depends on disabled module %q", tool)
238 }
239 break
240 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700241 path = t.HostToolPath()
242 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
243 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
244 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700245 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700246 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
247 break
Colin Cross6f080df2016-11-04 15:32:58 -0700248 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700249 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700250 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700251 break
252 }
253
254 if path.Valid() {
255 g.deps = append(g.deps, path.Path())
Colin Cross08f15ab2018-10-04 23:29:14 -0700256 addLocationLabel(tag.label, []string{path.Path().String()})
Colin Crossba71a3f2019-03-18 12:12:48 -0700257 seenTools[tag.label] = true
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700258 } else {
259 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700260 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700261 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700262 })
Colin Crossba71a3f2019-03-18 12:12:48 -0700263
264 // If AllowMissingDependencies is enabled, the build will not have stopped when
265 // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical
266 // "cmd: unknown location label ..." errors later. Add a dummy file to the local label. The
267 // command that uses this dummy file will never be executed because the rule will be replaced with
268 // an android.Error rule reporting the missing dependencies.
269 if ctx.Config().AllowMissingDependencies() {
270 for _, tool := range g.properties.Tools {
271 if !seenTools[tool] {
272 addLocationLabel(tool, []string{"***missing tool " + tool + "***"})
273 }
274 }
275 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700276 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700277
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700278 if ctx.Failed() {
279 return
280 }
281
Colin Cross08f15ab2018-10-04 23:29:14 -0700282 for _, toolFile := range g.properties.Tool_files {
Colin Cross8a497952019-03-05 22:25:09 -0800283 paths := android.PathsForModuleSrc(ctx, []string{toolFile})
Colin Cross08f15ab2018-10-04 23:29:14 -0700284 g.deps = append(g.deps, paths...)
285 addLocationLabel(toolFile, paths.Strings())
286 }
287
288 var srcFiles android.Paths
289 for _, in := range g.properties.Srcs {
Colin Crossba71a3f2019-03-18 12:12:48 -0700290 paths, missingDeps := android.PathsAndMissingDepsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs)
291 if len(missingDeps) > 0 {
292 if !ctx.Config().AllowMissingDependencies() {
293 panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator",
294 missingDeps))
295 }
296
297 // If AllowMissingDependencies is enabled, the build will not have stopped when
298 // the dependency was added on a missing SourceFileProducer module, which will result in nonsensical
299 // "cmd: label ":..." has no files" errors later. Add a dummy file to the local label. The
300 // command that uses this dummy file will never be executed because the rule will be replaced with
301 // an android.Error rule reporting the missing dependencies.
302 ctx.AddMissingDependencies(missingDeps)
303 addLocationLabel(in, []string{"***missing srcs " + in + "***"})
304 } else {
305 srcFiles = append(srcFiles, paths...)
306 addLocationLabel(in, paths.Strings())
307 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700308 }
309
Colin Cross1a527682019-09-23 15:55:30 -0700310 var copyFrom android.Paths
311 var outputFiles android.WritablePaths
312 var zipArgs strings.Builder
Colin Cross08f15ab2018-10-04 23:29:14 -0700313
Colin Cross1a527682019-09-23 15:55:30 -0700314 for _, task := range g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles) {
315 for _, out := range task.out {
316 addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
Colin Cross85a2e892018-07-09 09:45:06 -0700317 }
318
Bill Peckhamc087be12020-02-13 15:55:10 -0800319 referencedIn := false
Colin Cross1a527682019-09-23 15:55:30 -0700320 referencedDepfile := false
321
322 rawCommand, err := android.ExpandNinjaEscaped(task.cmd, func(name string) (string, bool, error) {
323 // report the error directly without returning an error to android.Expand to catch multiple errors in a
324 // single run
325 reportError := func(fmt string, args ...interface{}) (string, bool, error) {
326 ctx.PropertyErrorf("cmd", fmt, args...)
327 return "SOONG_ERROR", false, nil
Colin Cross6f080df2016-11-04 15:32:58 -0700328 }
Colin Cross1a527682019-09-23 15:55:30 -0700329
330 switch name {
331 case "location":
332 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
333 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700334 }
Colin Cross1a527682019-09-23 15:55:30 -0700335 paths := locationLabels[firstLabel]
336 if len(paths) == 0 {
337 return reportError("default label %q has no files", firstLabel)
338 } else if len(paths) > 1 {
339 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
340 firstLabel, firstLabel)
Colin Cross08f15ab2018-10-04 23:29:14 -0700341 }
Colin Cross1a527682019-09-23 15:55:30 -0700342 return locationLabels[firstLabel][0], false, nil
343 case "in":
Bill Peckhamc087be12020-02-13 15:55:10 -0800344 referencedIn = true
Colin Cross1a527682019-09-23 15:55:30 -0700345 return "${in}", true, nil
346 case "out":
347 return "__SBOX_OUT_FILES__", false, nil
348 case "depfile":
349 referencedDepfile = true
350 if !Bool(g.properties.Depfile) {
351 return reportError("$(depfile) used without depfile property")
352 }
353 return "__SBOX_DEPFILE__", false, nil
354 case "genDir":
355 return "__SBOX_OUT_DIR__", false, nil
356 default:
357 if strings.HasPrefix(name, "location ") {
358 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
359 if paths, ok := locationLabels[label]; ok {
360 if len(paths) == 0 {
361 return reportError("label %q has no files", label)
362 } else if len(paths) > 1 {
363 return reportError("label %q has multiple files, use $(locations %s) to reference it",
364 label, label)
365 }
366 return paths[0], false, nil
367 } else {
368 return reportError("unknown location label %q", label)
369 }
370 } else if strings.HasPrefix(name, "locations ") {
371 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
372 if paths, ok := locationLabels[label]; ok {
373 if len(paths) == 0 {
374 return reportError("label %q has no files", label)
375 }
376 return strings.Join(paths, " "), false, nil
377 } else {
378 return reportError("unknown locations label %q", label)
379 }
380 } else {
381 return reportError("unknown variable '$(%s)'", name)
382 }
Colin Cross6f080df2016-11-04 15:32:58 -0700383 }
Colin Cross1a527682019-09-23 15:55:30 -0700384 })
385
386 if err != nil {
387 ctx.PropertyErrorf("cmd", "%s", err.Error())
388 return
Colin Cross6f080df2016-11-04 15:32:58 -0700389 }
Colin Cross6f080df2016-11-04 15:32:58 -0700390
Colin Cross1a527682019-09-23 15:55:30 -0700391 if Bool(g.properties.Depfile) && !referencedDepfile {
392 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
393 return
394 }
395
396 // tell the sbox command which directory to use as its sandbox root
397 buildDir := android.PathForOutput(ctx).String()
398 sandboxPath := shared.TempDirForOutDir(buildDir)
399
400 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
401 // to be replaced later by ninja_strings.go
402 depfilePlaceholder := ""
403 if Bool(g.properties.Depfile) {
404 depfilePlaceholder = "$depfileArgs"
405 }
406
407 // Escape the command for the shell
408 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
409 g.rawCommands = append(g.rawCommands, rawCommand)
Bill Peckhamc087be12020-02-13 15:55:10 -0800410
411 sandboxCommand := fmt.Sprintf("rm -rf %s && $sboxCmd --sandbox-path %s --output-root %s",
412 task.genDir, sandboxPath, task.genDir)
413
414 if !referencedIn {
415 sandboxCommand = sandboxCommand + hashSrcFiles(srcFiles)
416 }
417
418 sandboxCommand = sandboxCommand + fmt.Sprintf(" -c %s %s $allouts",
419 rawCommand, depfilePlaceholder)
Colin Cross1a527682019-09-23 15:55:30 -0700420
421 ruleParams := blueprint.RuleParams{
422 Command: sandboxCommand,
423 CommandDeps: []string{"$sboxCmd"},
424 }
425 args := []string{"allouts"}
426 if Bool(g.properties.Depfile) {
427 ruleParams.Deps = blueprint.DepsGCC
428 args = append(args, "depfileArgs")
429 }
430 name := "generator"
431 if task.shards > 1 {
432 name += strconv.Itoa(task.shard)
433 }
434 rule := ctx.Rule(pctx, name, ruleParams, args...)
435
436 g.generateSourceFile(ctx, task, rule)
437
438 if len(task.copyTo) > 0 {
439 outputFiles = append(outputFiles, task.copyTo...)
440 copyFrom = append(copyFrom, task.out.Paths()...)
441 zipArgs.WriteString(" -C " + task.genDir.String())
442 zipArgs.WriteString(android.JoinWithPrefix(task.out.Strings(), " -f "))
443 } else {
444 outputFiles = append(outputFiles, task.out...)
445 }
Colin Cross6f080df2016-11-04 15:32:58 -0700446 }
447
Colin Cross1a527682019-09-23 15:55:30 -0700448 if len(copyFrom) > 0 {
449 ctx.Build(pctx, android.BuildParams{
450 Rule: gensrcsMerge,
451 Implicits: copyFrom,
452 Outputs: outputFiles,
453 Args: map[string]string{
454 "zipArgs": zipArgs.String(),
455 "tmpZip": android.PathForModuleGen(ctx, g.subDir+".zip").String(),
456 "genDir": android.PathForModuleGen(ctx, g.subDir).String(),
457 },
458 })
Colin Cross85a2e892018-07-09 09:45:06 -0700459 }
460
Colin Cross1a527682019-09-23 15:55:30 -0700461 g.outputFiles = outputFiles.Paths()
Jeff Gastonefc1b412017-03-29 17:29:06 -0700462
Colin Cross1a527682019-09-23 15:55:30 -0700463 // For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of
464 // the genrules on AOSP. That will make things simpler to look at the graph in the common
465 // case. For larger sets of outputs, inject a phony target in between to limit ninja file
466 // growth.
467 if len(g.outputFiles) <= 6 {
468 g.outputDeps = g.outputFiles
469 } else {
470 phonyFile := android.PathForModuleGen(ctx, "genrule-phony")
471
472 ctx.Build(pctx, android.BuildParams{
473 Rule: blueprint.Phony,
474 Output: phonyFile,
475 Inputs: g.outputFiles,
476 })
477
478 g.outputDeps = android.Paths{phonyFile}
Jeff Gaston02a684b2017-10-27 14:59:27 -0700479 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800480
Colin Crossd350ecd2015-04-28 13:25:36 -0700481}
482
Bill Peckhamc087be12020-02-13 15:55:10 -0800483func hashSrcFiles(srcFiles android.Paths) string {
484 h := sha256.New()
485 for _, src := range srcFiles {
486 h.Write([]byte(src.String()))
487 }
488 return fmt.Sprintf(" --input-hash %x", h.Sum(nil))
489}
490
Colin Cross1a527682019-09-23 15:55:30 -0700491func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask, rule blueprint.Rule) {
Colin Cross67a5c132017-05-09 13:45:28 -0700492 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700493 if len(task.out) == 0 {
494 ctx.ModuleErrorf("must have at least one output file")
495 return
496 }
Colin Cross67a5c132017-05-09 13:45:28 -0700497 if len(task.out) == 1 {
498 desc += " " + task.out[0].Base()
499 }
500
Jeff Gaston02a684b2017-10-27 14:59:27 -0700501 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800502 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700503 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
504 }
505
Colin Cross1a527682019-09-23 15:55:30 -0700506 if task.shards > 1 {
507 desc += " " + strconv.Itoa(task.shard)
508 }
509
Colin Crossae887032017-10-23 17:16:14 -0700510 params := android.BuildParams{
Colin Cross1a527682019-09-23 15:55:30 -0700511 Rule: rule,
512 Description: desc,
Colin Cross15e86d92017-10-20 15:07:08 -0700513 Output: task.out[0],
514 ImplicitOutputs: task.out[1:],
515 Inputs: task.in,
516 Implicits: g.deps,
517 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800518 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700519 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800520 }
Nan Zhangea568a42017-11-08 21:20:04 -0800521 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700522 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
523 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800524 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700525
Colin Crossae887032017-10-23 17:16:14 -0700526 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700527}
528
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700529// Collect information for opening IDE project files in java/jdeps.go.
530func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
531 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
532 for _, src := range g.properties.Srcs {
533 if strings.HasPrefix(src, ":") {
534 src = strings.Trim(src, ":")
535 dpInfo.Deps = append(dpInfo.Deps, src)
536 }
537 }
bralee1fbf4402020-05-21 10:11:59 +0800538 dpInfo.Paths = append(dpInfo.Paths, g.modulePaths...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700539}
540
Colin Crossa4ad2b02019-03-18 22:15:32 -0700541func (g *Module) AndroidMk() android.AndroidMkData {
542 return android.AndroidMkData{
543 Include: "$(BUILD_PHONY_PACKAGE)",
544 Class: "FAKE",
545 OutputFile: android.OptionalPathForPath(g.outputFiles[0]),
546 SubName: g.subName,
547 Extra: []android.AndroidMkExtraFunc{
548 func(w io.Writer, outputFile android.Path) {
Colin Cross1a527682019-09-23 15:55:30 -0700549 fmt.Fprintln(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=", strings.Join(g.outputDeps.Strings(), " "))
Colin Crossa4ad2b02019-03-18 22:15:32 -0700550 },
551 },
552 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
553 android.WriteAndroidMkData(w, data)
554 if data.SubName != "" {
555 fmt.Fprintln(w, ".PHONY:", name)
556 fmt.Fprintln(w, name, ":", name+g.subName)
557 }
558 },
559 }
560}
561
Jooyung Han749dc692020-04-15 11:03:39 +0900562func (g *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error {
563 // Because generated outputs are checked by client modules(e.g. cc_library, ...)
564 // we can safely ignore the check here.
565 return nil
566}
567
Jeff Gaston437d23c2017-11-08 12:38:00 -0800568func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700569 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800570 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700571 }
572
Colin Cross36242852017-06-23 15:06:31 -0700573 module.AddProperties(props...)
574 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700575
Colin Cross7228ecd2019-11-18 16:00:16 -0800576 module.ImageInterface = noopImageInterface{}
577
Colin Cross36242852017-06-23 15:06:31 -0700578 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700579}
580
Colin Cross7228ecd2019-11-18 16:00:16 -0800581type noopImageInterface struct{}
582
583func (x noopImageInterface) ImageMutatorBegin(android.BaseModuleContext) {}
584func (x noopImageInterface) CoreVariantNeeded(android.BaseModuleContext) bool { return false }
Yifan Hong1b3348d2020-01-21 15:53:22 -0800585func (x noopImageInterface) RamdiskVariantNeeded(android.BaseModuleContext) bool { return false }
Colin Cross7228ecd2019-11-18 16:00:16 -0800586func (x noopImageInterface) RecoveryVariantNeeded(android.BaseModuleContext) bool { return false }
587func (x noopImageInterface) ExtraImageVariations(ctx android.BaseModuleContext) []string { return nil }
588func (x noopImageInterface) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
589}
590
Colin Crossbaccf5b2018-02-21 14:07:48 -0800591// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
592func pathToSandboxOut(path android.Path, genDir android.Path) string {
593 relOut, err := filepath.Rel(genDir.String(), path.String())
594 if err != nil {
595 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
596 }
597 return filepath.Join("__SBOX_OUT_DIR__", relOut)
598
599}
600
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700601func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700602 properties := &genSrcsProperties{}
603
Colin Cross1a527682019-09-23 15:55:30 -0700604 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
605 genDir := android.PathForModuleGen(ctx, "gensrcs")
606 shardSize := defaultShardSize
607 if s := properties.Shard_size; s != nil {
608 shardSize = int(*s)
609 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800610
Colin Cross1a527682019-09-23 15:55:30 -0700611 shards := android.ShardPaths(srcFiles, shardSize)
612 var generateTasks []generateTask
Colin Crossbaccf5b2018-02-21 14:07:48 -0800613
Colin Cross1a527682019-09-23 15:55:30 -0700614 for i, shard := range shards {
615 var commands []string
616 var outFiles android.WritablePaths
617 var copyTo android.WritablePaths
618 var shardDir android.WritablePath
619 var sandboxOuts []string
620
621 if len(shards) > 1 {
622 shardDir = android.PathForModuleGen(ctx, strconv.Itoa(i))
623 } else {
624 shardDir = genDir
Jeff Gaston437d23c2017-11-08 12:38:00 -0800625 }
626
Colin Cross1a527682019-09-23 15:55:30 -0700627 for _, in := range shard {
628 outFile := android.GenPathWithExt(ctx, "gensrcs", in, String(properties.Output_extension))
629 sandboxOutfile := pathToSandboxOut(outFile, genDir)
Jeff Gaston437d23c2017-11-08 12:38:00 -0800630
Colin Cross1a527682019-09-23 15:55:30 -0700631 if len(shards) > 1 {
632 shardFile := android.GenPathWithExt(ctx, strconv.Itoa(i), in, String(properties.Output_extension))
633 copyTo = append(copyTo, outFile)
634 outFile = shardFile
635 }
636
637 outFiles = append(outFiles, outFile)
638 sandboxOuts = append(sandboxOuts, sandboxOutfile)
639
640 command, err := android.Expand(rawCommand, func(name string) (string, error) {
641 switch name {
642 case "in":
643 return in.String(), nil
644 case "out":
645 return sandboxOutfile, nil
646 default:
647 return "$(" + name + ")", nil
648 }
649 })
650 if err != nil {
651 ctx.PropertyErrorf("cmd", err.Error())
652 }
653
654 // escape the command in case for example it contains '#', an odd number of '"', etc
655 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
656 commands = append(commands, command)
657 }
658 fullCommand := strings.Join(commands, " && ")
659
660 generateTasks = append(generateTasks, generateTask{
661 in: shard,
662 out: outFiles,
663 copyTo: copyTo,
664 genDir: shardDir,
665 sandboxOuts: sandboxOuts,
666 cmd: fullCommand,
667 shard: i,
668 shards: len(shards),
669 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800670 }
Colin Cross1a527682019-09-23 15:55:30 -0700671
672 return generateTasks
Colin Crossd350ecd2015-04-28 13:25:36 -0700673 }
674
Colin Cross1a527682019-09-23 15:55:30 -0700675 g := generatorFactory(taskGenerator, properties)
676 g.subDir = "gensrcs"
677 return g
Colin Crossd350ecd2015-04-28 13:25:36 -0700678}
679
Colin Cross54190b32017-10-09 15:34:10 -0700680func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700681 m := NewGenSrcs()
682 android.InitAndroidModule(m)
683 return m
684}
685
Colin Crossd350ecd2015-04-28 13:25:36 -0700686type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700687 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800688 Output_extension *string
Colin Cross1a527682019-09-23 15:55:30 -0700689
690 // maximum number of files that will be passed on a single command line.
691 Shard_size *int64
Colin Cross5049f022015-03-18 13:28:46 -0700692}
693
Colin Cross1a527682019-09-23 15:55:30 -0700694const defaultShardSize = 100
695
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700696func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700697 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700698
Colin Cross1a527682019-09-23 15:55:30 -0700699 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700700 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800701 sandboxOuts := make([]string, len(properties.Out))
702 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700703 for i, out := range properties.Out {
704 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800705 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700706 }
Colin Cross1a527682019-09-23 15:55:30 -0700707 return []generateTask{{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800708 in: srcFiles,
709 out: outs,
Colin Cross1a527682019-09-23 15:55:30 -0700710 genDir: android.PathForModuleGen(ctx),
Colin Crossbaccf5b2018-02-21 14:07:48 -0800711 sandboxOuts: sandboxOuts,
712 cmd: rawCommand,
Colin Cross1a527682019-09-23 15:55:30 -0700713 }}
Colin Cross5049f022015-03-18 13:28:46 -0700714 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700715
Jeff Gaston437d23c2017-11-08 12:38:00 -0800716 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700717}
718
Colin Cross54190b32017-10-09 15:34:10 -0700719func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700720 m := NewGenRule()
721 android.InitAndroidModule(m)
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800722 android.InitDefaultableModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700723 return m
724}
725
Colin Crossd350ecd2015-04-28 13:25:36 -0700726type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700727 // names of the output files that will be generated
Colin Crossef354482018-10-23 11:27:50 -0700728 Out []string `android:"arch_variant"`
Colin Cross5049f022015-03-18 13:28:46 -0700729}
Nan Zhangea568a42017-11-08 21:20:04 -0800730
731var Bool = proptools.Bool
732var String = proptools.String
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800733
734//
735// Defaults
736//
737type Defaults struct {
738 android.ModuleBase
739 android.DefaultsModuleBase
740}
741
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800742func defaultsFactory() android.Module {
743 return DefaultsFactory()
744}
745
746func DefaultsFactory(props ...interface{}) android.Module {
747 module := &Defaults{}
748
749 module.AddProperties(props...)
750 module.AddProperties(
751 &generatorProperties{},
752 &genRuleProperties{},
753 )
754
755 android.InitDefaultsModule(module)
756
757 return module
758}