blob: d667e94a5377aa8b958ec0b732b20ec30ded979d [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 Cross3d680512020-11-13 16:23:53 -080020 "path/filepath"
Colin Cross1a527682019-09-23 15:55:30 -070021 "strconv"
Colin Cross6f080df2016-11-04 15:32:58 -070022 "strings"
Dan Willemsen3f4539b2016-09-28 16:19:10 -070023
Colin Cross70b40592015-03-23 12:57:34 -070024 "github.com/google/blueprint"
Dan Willemsen8eded0a2017-09-13 16:07:44 -070025 "github.com/google/blueprint/bootstrap"
Nan Zhangea568a42017-11-08 21:20:04 -080026 "github.com/google/blueprint/proptools"
Colin Cross5049f022015-03-18 13:28:46 -070027
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Jingwen Chen30f5aaa2020-11-19 05:38:02 -050029 "android/soong/bazel"
Colin Cross5049f022015-03-18 13:28:46 -070030)
31
Colin Cross463a90e2015-06-17 14:20:06 -070032func init() {
Colin Crosse9fe2942020-11-10 18:12:15 -080033 RegisterGenruleBuildComponents(android.InitRegistrationContext)
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000034}
Jaewoong Jung98716bd2018-12-10 08:13:18 -080035
Colin Crosse9fe2942020-11-10 18:12:15 -080036func RegisterGenruleBuildComponents(ctx android.RegistrationContext) {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000037 ctx.RegisterModuleType("genrule_defaults", defaultsFactory)
38
39 ctx.RegisterModuleType("gensrcs", GenSrcsFactory)
40 ctx.RegisterModuleType("genrule", GenRuleFactory)
41
42 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
43 ctx.BottomUp("genrule_tool_deps", toolDepsMutator).Parallel()
44 })
Colin Cross463a90e2015-06-17 14:20:06 -070045}
46
Colin Cross5049f022015-03-18 13:28:46 -070047var (
Colin Cross635c3b02016-05-18 15:37:25 -070048 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross1a527682019-09-23 15:55:30 -070049
50 gensrcsMerge = pctx.AndroidStaticRule("gensrcsMerge", blueprint.RuleParams{
51 Command: "${soongZip} -o ${tmpZip} @${tmpZip}.rsp && ${zipSync} -d ${genDir} ${tmpZip}",
52 CommandDeps: []string{"${soongZip}", "${zipSync}"},
53 Rspfile: "${tmpZip}.rsp",
54 RspfileContent: "${zipArgs}",
55 }, "tmpZip", "genDir", "zipArgs")
Colin Cross5049f022015-03-18 13:28:46 -070056)
57
Jeff Gastonefc1b412017-03-29 17:29:06 -070058func init() {
Dan Willemsenddf504c2019-08-09 16:21:29 -070059 pctx.Import("android/soong/android")
Jeff Gastonefc1b412017-03-29 17:29:06 -070060 pctx.HostBinToolVariable("sboxCmd", "sbox")
Colin Cross1a527682019-09-23 15:55:30 -070061
62 pctx.HostBinToolVariable("soongZip", "soong_zip")
63 pctx.HostBinToolVariable("zipSync", "zipsync")
Jeff Gastonefc1b412017-03-29 17:29:06 -070064}
65
Colin Cross5049f022015-03-18 13:28:46 -070066type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070067 GeneratedSourceFiles() android.Paths
Colin Cross5ed99c62016-11-22 12:55:55 -080068 GeneratedHeaderDirs() android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -080069 GeneratedDeps() android.Paths
Colin Cross5049f022015-03-18 13:28:46 -070070}
71
Colin Crossfe17f6f2019-03-28 19:30:56 -070072// Alias for android.HostToolProvider
73// Deprecated: use android.HostToolProvider instead.
Colin Crossd350ecd2015-04-28 13:25:36 -070074type HostToolProvider interface {
Colin Crossfe17f6f2019-03-28 19:30:56 -070075 android.HostToolProvider
Colin Crossd350ecd2015-04-28 13:25:36 -070076}
Colin Cross5049f022015-03-18 13:28:46 -070077
Dan Willemsend6ba0d52017-09-13 15:46:47 -070078type hostToolDependencyTag struct {
79 blueprint.BaseDependencyTag
Colin Cross08f15ab2018-10-04 23:29:14 -070080 label string
Dan Willemsend6ba0d52017-09-13 15:46:47 -070081}
Colin Cross7d5136f2015-05-11 13:39:40 -070082type generatorProperties struct {
Jeff Gastonefc1b412017-03-29 17:29:06 -070083 // The command to run on one or more input files. Cmd supports substitution of a few variables
Jeff Gastonefc1b412017-03-29 17:29:06 -070084 //
85 // Available variables for substitution:
86 //
Colin Cross2296f5b2017-10-17 21:38:14 -070087 // $(location): the path to the first entry in tools or tool_files
Colin Cross08f15ab2018-10-04 23:29:14 -070088 // $(location <label>): the path to the tool, tool_file, input or output with name <label>
Colin Cross2296f5b2017-10-17 21:38:14 -070089 // $(in): one or more input files
90 // $(out): a single output file
91 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
92 // $(genDir): the sandbox directory for this tool; contains $(out)
93 // $$: a literal $
Nan Zhangea568a42017-11-08 21:20:04 -080094 Cmd *string
Colin Cross7d5136f2015-05-11 13:39:40 -070095
Colin Cross33bfb0a2016-11-21 17:23:08 -080096 // Enable reading a file containing dependencies in gcc format after the command completes
Nan Zhangea568a42017-11-08 21:20:04 -080097 Depfile *bool
Colin Cross33bfb0a2016-11-21 17:23:08 -080098
Colin Cross6f080df2016-11-04 15:32:58 -070099 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -0700100 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -0700101 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700102
103 // Local file that is used as the tool
Colin Cross27b922f2019-03-04 22:35:41 -0800104 Tool_files []string `android:"path"`
Colin Cross5ed99c62016-11-22 12:55:55 -0800105
106 // List of directories to export generated headers from
107 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -0800108
109 // list of input files
Colin Cross27b922f2019-03-04 22:35:41 -0800110 Srcs []string `android:"path,arch_variant"`
Dan Willemseneefa0262018-11-17 14:01:18 -0800111
112 // input files to exclude
Colin Cross27b922f2019-03-04 22:35:41 -0800113 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700114
Jingwen Chen30f5aaa2020-11-19 05:38:02 -0500115 // Properties for Bazel migration purposes.
116 bazel.Properties
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400117}
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700118type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700119 android.ModuleBase
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800120 android.DefaultableModuleBase
Jiyong Parkfc752ca2019-06-12 13:27:29 +0900121 android.ApexModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -0700122
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700123 // For other packages to make their own genrules with extra
124 // properties
125 Extra interface{}
Colin Cross7228ecd2019-11-18 16:00:16 -0800126 android.ImageInterface
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700127
Colin Cross7d5136f2015-05-11 13:39:40 -0700128 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700129
Jeff Gaston437d23c2017-11-08 12:38:00 -0800130 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700131
Colin Cross1a527682019-09-23 15:55:30 -0700132 deps android.Paths
133 rule blueprint.Rule
134 rawCommands []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700135
Colin Cross5ed99c62016-11-22 12:55:55 -0800136 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700137
Colin Cross635c3b02016-05-18 15:37:25 -0700138 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800139 outputDeps android.Paths
Colin Crossa4ad2b02019-03-18 22:15:32 -0700140
141 subName string
Colin Cross1a527682019-09-23 15:55:30 -0700142 subDir string
bralee1fbf4402020-05-21 10:11:59 +0800143
144 // Collect the module directory for IDE info in java/jdeps.go.
145 modulePaths []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700146}
147
Colin Cross1a527682019-09-23 15:55:30 -0700148type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700149
150type generateTask struct {
Colin Cross3ea4eb82020-11-24 13:07:27 -0800151 in android.Paths
152 out android.WritablePaths
153 depFile android.WritablePath
154 copyTo android.WritablePaths
155 genDir android.WritablePath
156 extraTools android.Paths // dependencies on tools used by the generator
157
158 cmd string
159 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}
Colin Crossf1885962020-11-20 15:28:30 -0800205
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700206func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa4ad2b02019-03-18 22:15:32 -0700207 g.subName = ctx.ModuleSubDir()
208
bralee1fbf4402020-05-21 10:11:59 +0800209 // Collect the module directory for IDE info in java/jdeps.go.
210 g.modulePaths = append(g.modulePaths, ctx.ModuleDir())
211
Colin Cross5ed99c62016-11-22 12:55:55 -0800212 if len(g.properties.Export_include_dirs) > 0 {
213 for _, dir := range g.properties.Export_include_dirs {
214 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
Colin Cross1a527682019-09-23 15:55:30 -0700215 android.PathForModuleGen(ctx, g.subDir, ctx.ModuleDir(), dir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800216 }
217 } else {
Colin Cross1a527682019-09-23 15:55:30 -0700218 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, g.subDir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800219 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700220
Colin Cross08f15ab2018-10-04 23:29:14 -0700221 locationLabels := map[string][]string{}
222 firstLabel := ""
223
224 addLocationLabel := func(label string, paths []string) {
225 if firstLabel == "" {
226 firstLabel = label
227 }
228 if _, exists := locationLabels[label]; !exists {
229 locationLabels[label] = paths
230 } else {
231 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
232 label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
233 }
234 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700235
Colin Cross6f080df2016-11-04 15:32:58 -0700236 if len(g.properties.Tools) > 0 {
Colin Crossba71a3f2019-03-18 12:12:48 -0700237 seenTools := make(map[string]bool)
238
Colin Cross35143d02017-11-16 00:11:20 -0800239 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700240 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
241 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700242 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700243 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700244
Colin Crossfe17f6f2019-03-28 19:30:56 -0700245 if t, ok := module.(android.HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800246 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800247 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800248 ctx.AddMissingDependencies([]string{tool})
249 } else {
250 ctx.ModuleErrorf("depends on disabled module %q", tool)
251 }
252 break
253 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700254 path = t.HostToolPath()
255 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
256 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
257 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700258 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700259 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
260 break
Colin Cross6f080df2016-11-04 15:32:58 -0700261 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700262 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700263 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700264 break
265 }
266
267 if path.Valid() {
268 g.deps = append(g.deps, path.Path())
Colin Cross08f15ab2018-10-04 23:29:14 -0700269 addLocationLabel(tag.label, []string{path.Path().String()})
Colin Crossba71a3f2019-03-18 12:12:48 -0700270 seenTools[tag.label] = true
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700271 } else {
272 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700273 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700274 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700275 })
Colin Crossba71a3f2019-03-18 12:12:48 -0700276
277 // If AllowMissingDependencies is enabled, the build will not have stopped when
278 // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical
Liz Kammer20ebfb42020-07-28 11:32:07 -0700279 // "cmd: unknown location label ..." errors later. Add a placeholder file to the local label.
280 // The command that uses this placeholder file will never be executed because the rule will be
281 // replaced with an android.Error rule reporting the missing dependencies.
Colin Crossba71a3f2019-03-18 12:12:48 -0700282 if ctx.Config().AllowMissingDependencies() {
283 for _, tool := range g.properties.Tools {
284 if !seenTools[tool] {
285 addLocationLabel(tool, []string{"***missing tool " + tool + "***"})
286 }
287 }
288 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700289 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700290
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700291 if ctx.Failed() {
292 return
293 }
294
Colin Cross08f15ab2018-10-04 23:29:14 -0700295 for _, toolFile := range g.properties.Tool_files {
Colin Cross8a497952019-03-05 22:25:09 -0800296 paths := android.PathsForModuleSrc(ctx, []string{toolFile})
Colin Cross08f15ab2018-10-04 23:29:14 -0700297 g.deps = append(g.deps, paths...)
298 addLocationLabel(toolFile, paths.Strings())
299 }
300
301 var srcFiles android.Paths
302 for _, in := range g.properties.Srcs {
Colin Crossba71a3f2019-03-18 12:12:48 -0700303 paths, missingDeps := android.PathsAndMissingDepsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs)
304 if len(missingDeps) > 0 {
305 if !ctx.Config().AllowMissingDependencies() {
306 panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator",
307 missingDeps))
308 }
309
310 // If AllowMissingDependencies is enabled, the build will not have stopped when
311 // the dependency was added on a missing SourceFileProducer module, which will result in nonsensical
Liz Kammer20ebfb42020-07-28 11:32:07 -0700312 // "cmd: label ":..." has no files" errors later. Add a placeholder file to the local label.
313 // The command that uses this placeholder file will never be executed because the rule will be
314 // replaced with an android.Error rule reporting the missing dependencies.
Colin Crossba71a3f2019-03-18 12:12:48 -0700315 ctx.AddMissingDependencies(missingDeps)
316 addLocationLabel(in, []string{"***missing srcs " + in + "***"})
317 } else {
318 srcFiles = append(srcFiles, paths...)
319 addLocationLabel(in, paths.Strings())
320 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700321 }
322
Colin Cross1a527682019-09-23 15:55:30 -0700323 var copyFrom android.Paths
324 var outputFiles android.WritablePaths
325 var zipArgs strings.Builder
Colin Cross08f15ab2018-10-04 23:29:14 -0700326
Colin Cross1a527682019-09-23 15:55:30 -0700327 for _, task := range g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles) {
Colin Cross3d680512020-11-13 16:23:53 -0800328 if len(task.out) == 0 {
329 ctx.ModuleErrorf("must have at least one output file")
330 return
Colin Cross85a2e892018-07-09 09:45:06 -0700331 }
332
Colin Cross3d680512020-11-13 16:23:53 -0800333 for _, out := range task.out {
334 addLocationLabel(out.Rel(), []string{android.SboxPathForOutput(out, task.genDir)})
335 }
336
Colin Cross1a527682019-09-23 15:55:30 -0700337 referencedDepfile := false
338
Colin Cross3d680512020-11-13 16:23:53 -0800339 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross1a527682019-09-23 15:55:30 -0700340 // report the error directly without returning an error to android.Expand to catch multiple errors in a
341 // single run
Colin Cross3d680512020-11-13 16:23:53 -0800342 reportError := func(fmt string, args ...interface{}) (string, error) {
Colin Cross1a527682019-09-23 15:55:30 -0700343 ctx.PropertyErrorf("cmd", fmt, args...)
Colin Cross3d680512020-11-13 16:23:53 -0800344 return "SOONG_ERROR", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700345 }
Colin Cross1a527682019-09-23 15:55:30 -0700346
347 switch name {
348 case "location":
349 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
350 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700351 }
Colin Cross1a527682019-09-23 15:55:30 -0700352 paths := locationLabels[firstLabel]
353 if len(paths) == 0 {
354 return reportError("default label %q has no files", firstLabel)
355 } else if len(paths) > 1 {
356 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
357 firstLabel, firstLabel)
Colin Cross08f15ab2018-10-04 23:29:14 -0700358 }
Colin Cross3d680512020-11-13 16:23:53 -0800359 return locationLabels[firstLabel][0], nil
Colin Cross1a527682019-09-23 15:55:30 -0700360 case "in":
Colin Cross3d680512020-11-13 16:23:53 -0800361 return strings.Join(srcFiles.Strings(), " "), nil
Colin Cross1a527682019-09-23 15:55:30 -0700362 case "out":
Colin Cross3d680512020-11-13 16:23:53 -0800363 var sandboxOuts []string
364 for _, out := range task.out {
365 sandboxOuts = append(sandboxOuts, android.SboxPathForOutput(out, task.genDir))
366 }
367 return strings.Join(sandboxOuts, " "), nil
Colin Cross1a527682019-09-23 15:55:30 -0700368 case "depfile":
369 referencedDepfile = true
370 if !Bool(g.properties.Depfile) {
371 return reportError("$(depfile) used without depfile property")
372 }
Colin Cross3d680512020-11-13 16:23:53 -0800373 return "__SBOX_DEPFILE__", nil
Colin Cross1a527682019-09-23 15:55:30 -0700374 case "genDir":
Colin Cross3d680512020-11-13 16:23:53 -0800375 return android.SboxPathForOutput(task.genDir, task.genDir), nil
Colin Cross1a527682019-09-23 15:55:30 -0700376 default:
377 if strings.HasPrefix(name, "location ") {
378 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
379 if paths, ok := locationLabels[label]; ok {
380 if len(paths) == 0 {
381 return reportError("label %q has no files", label)
382 } else if len(paths) > 1 {
383 return reportError("label %q has multiple files, use $(locations %s) to reference it",
384 label, label)
385 }
Colin Cross3d680512020-11-13 16:23:53 -0800386 return paths[0], nil
Colin Cross1a527682019-09-23 15:55:30 -0700387 } else {
388 return reportError("unknown location label %q", label)
389 }
390 } else if strings.HasPrefix(name, "locations ") {
391 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
392 if paths, ok := locationLabels[label]; ok {
393 if len(paths) == 0 {
394 return reportError("label %q has no files", label)
395 }
Colin Cross3d680512020-11-13 16:23:53 -0800396 return strings.Join(paths, " "), nil
Colin Cross1a527682019-09-23 15:55:30 -0700397 } else {
398 return reportError("unknown locations label %q", label)
399 }
400 } else {
401 return reportError("unknown variable '$(%s)'", name)
402 }
Colin Cross6f080df2016-11-04 15:32:58 -0700403 }
Colin Cross1a527682019-09-23 15:55:30 -0700404 })
405
406 if err != nil {
407 ctx.PropertyErrorf("cmd", "%s", err.Error())
408 return
Colin Cross6f080df2016-11-04 15:32:58 -0700409 }
Colin Cross6f080df2016-11-04 15:32:58 -0700410
Colin Cross1a527682019-09-23 15:55:30 -0700411 if Bool(g.properties.Depfile) && !referencedDepfile {
412 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
413 return
414 }
Colin Cross1a527682019-09-23 15:55:30 -0700415 g.rawCommands = append(g.rawCommands, rawCommand)
Bill Peckhamc087be12020-02-13 15:55:10 -0800416
Colin Crosse16ce362020-11-12 08:29:30 -0800417 // Pick a unique path outside the task.genDir for the sbox manifest textproto,
418 // a unique rule name, and the user-visible description.
419 manifestName := "genrule.sbox.textproto"
Colin Cross3d680512020-11-13 16:23:53 -0800420 desc := "generate"
Colin Cross1a527682019-09-23 15:55:30 -0700421 name := "generator"
Colin Cross3d680512020-11-13 16:23:53 -0800422 if task.shards > 0 {
Colin Crosse16ce362020-11-12 08:29:30 -0800423 manifestName = "genrule_" + strconv.Itoa(task.shard) + ".sbox.textproto"
Colin Cross3d680512020-11-13 16:23:53 -0800424 desc += " " + strconv.Itoa(task.shard)
Colin Cross1a527682019-09-23 15:55:30 -0700425 name += strconv.Itoa(task.shard)
Colin Cross3d680512020-11-13 16:23:53 -0800426 } else if len(task.out) == 1 {
427 desc += " " + task.out[0].Base()
Colin Cross1a527682019-09-23 15:55:30 -0700428 }
Colin Cross1a527682019-09-23 15:55:30 -0700429
Colin Crosse16ce362020-11-12 08:29:30 -0800430 manifestPath := android.PathForModuleOut(ctx, manifestName)
431
Colin Cross3d680512020-11-13 16:23:53 -0800432 // Use a RuleBuilder to create a rule that runs the command inside an sbox sandbox.
Colin Crosse16ce362020-11-12 08:29:30 -0800433 rule := android.NewRuleBuilder().Sbox(task.genDir, manifestPath)
Colin Cross3d680512020-11-13 16:23:53 -0800434 cmd := rule.Command()
435 cmd.Text(rawCommand)
436 cmd.ImplicitOutputs(task.out)
437 cmd.Implicits(task.in)
438 cmd.Implicits(g.deps)
Colin Cross3ea4eb82020-11-24 13:07:27 -0800439 cmd.Implicits(task.extraTools)
Colin Cross3d680512020-11-13 16:23:53 -0800440 if Bool(g.properties.Depfile) {
441 cmd.ImplicitDepFile(task.depFile)
442 }
443
444 // Create the rule to run the genrule command inside sbox.
445 rule.Build(pctx, ctx, name, desc)
Colin Cross1a527682019-09-23 15:55:30 -0700446
447 if len(task.copyTo) > 0 {
Colin Cross3d680512020-11-13 16:23:53 -0800448 // If copyTo is set, multiple shards need to be copied into a single directory.
449 // task.out contains the per-shard paths, and copyTo contains the corresponding
450 // final path. The files need to be copied into the final directory by a
451 // single rule so it can remove the directory before it starts to ensure no
452 // old files remain. zipsync already does this, so build up zipArgs that
453 // zip all the per-shard directories into a single zip.
Colin Cross1a527682019-09-23 15:55:30 -0700454 outputFiles = append(outputFiles, task.copyTo...)
455 copyFrom = append(copyFrom, task.out.Paths()...)
456 zipArgs.WriteString(" -C " + task.genDir.String())
457 zipArgs.WriteString(android.JoinWithPrefix(task.out.Strings(), " -f "))
458 } else {
459 outputFiles = append(outputFiles, task.out...)
460 }
Colin Cross6f080df2016-11-04 15:32:58 -0700461 }
462
Colin Cross1a527682019-09-23 15:55:30 -0700463 if len(copyFrom) > 0 {
Colin Cross3d680512020-11-13 16:23:53 -0800464 // Create a rule that zips all the per-shard directories into a single zip and then
465 // uses zipsync to unzip it into the final directory.
Colin Cross1a527682019-09-23 15:55:30 -0700466 ctx.Build(pctx, android.BuildParams{
Colin Crossf1885962020-11-20 15:28:30 -0800467 Rule: gensrcsMerge,
468 Implicits: copyFrom,
469 Outputs: outputFiles,
470 Description: "merge shards",
Colin Cross1a527682019-09-23 15:55:30 -0700471 Args: map[string]string{
472 "zipArgs": zipArgs.String(),
473 "tmpZip": android.PathForModuleGen(ctx, g.subDir+".zip").String(),
474 "genDir": android.PathForModuleGen(ctx, g.subDir).String(),
475 },
476 })
Colin Cross85a2e892018-07-09 09:45:06 -0700477 }
478
Colin Cross1a527682019-09-23 15:55:30 -0700479 g.outputFiles = outputFiles.Paths()
Jeff Gastonefc1b412017-03-29 17:29:06 -0700480
Chris Parsonsaa8be052020-10-14 16:22:37 -0400481 bazelModuleLabel := g.properties.Bazel_module.Label
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400482 bazelActionsUsed := false
483 if ctx.Config().BazelContext.BazelEnabled() && len(bazelModuleLabel) > 0 {
484 bazelActionsUsed = g.generateBazelBuildActions(ctx, bazelModuleLabel)
Jeff Gaston02a684b2017-10-27 14:59:27 -0700485 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400486 if !bazelActionsUsed {
487 // For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of
488 // the genrules on AOSP. That will make things simpler to look at the graph in the common
489 // case. For larger sets of outputs, inject a phony target in between to limit ninja file
490 // growth.
491 if len(g.outputFiles) <= 6 {
492 g.outputDeps = g.outputFiles
493 } else {
494 phonyFile := android.PathForModuleGen(ctx, "genrule-phony")
495 ctx.Build(pctx, android.BuildParams{
496 Rule: blueprint.Phony,
497 Output: phonyFile,
498 Inputs: g.outputFiles,
499 })
500 g.outputDeps = android.Paths{phonyFile}
501 }
502 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700503}
Colin Crossd350ecd2015-04-28 13:25:36 -0700504
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700505// Collect information for opening IDE project files in java/jdeps.go.
506func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
507 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
508 for _, src := range g.properties.Srcs {
509 if strings.HasPrefix(src, ":") {
510 src = strings.Trim(src, ":")
511 dpInfo.Deps = append(dpInfo.Deps, src)
512 }
513 }
bralee1fbf4402020-05-21 10:11:59 +0800514 dpInfo.Paths = append(dpInfo.Paths, g.modulePaths...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700515}
516
Colin Crossa4ad2b02019-03-18 22:15:32 -0700517func (g *Module) AndroidMk() android.AndroidMkData {
518 return android.AndroidMkData{
Anton Hansson72f18492020-10-30 16:34:45 +0000519 Class: "ETC",
Colin Crossa4ad2b02019-03-18 22:15:32 -0700520 OutputFile: android.OptionalPathForPath(g.outputFiles[0]),
521 SubName: g.subName,
522 Extra: []android.AndroidMkExtraFunc{
523 func(w io.Writer, outputFile android.Path) {
Anton Hansson72f18492020-10-30 16:34:45 +0000524 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
Colin Crossa4ad2b02019-03-18 22:15:32 -0700525 },
526 },
527 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
528 android.WriteAndroidMkData(w, data)
529 if data.SubName != "" {
530 fmt.Fprintln(w, ".PHONY:", name)
531 fmt.Fprintln(w, name, ":", name+g.subName)
532 }
533 },
534 }
535}
536
Dan Albertc8060532020-07-22 22:32:17 -0700537func (g *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
538 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900539 // Because generated outputs are checked by client modules(e.g. cc_library, ...)
540 // we can safely ignore the check here.
541 return nil
542}
543
Jeff Gaston437d23c2017-11-08 12:38:00 -0800544func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700545 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800546 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700547 }
548
Colin Cross36242852017-06-23 15:06:31 -0700549 module.AddProperties(props...)
550 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700551
Colin Cross7228ecd2019-11-18 16:00:16 -0800552 module.ImageInterface = noopImageInterface{}
553
Colin Cross36242852017-06-23 15:06:31 -0700554 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700555}
556
Colin Cross7228ecd2019-11-18 16:00:16 -0800557type noopImageInterface struct{}
558
559func (x noopImageInterface) ImageMutatorBegin(android.BaseModuleContext) {}
560func (x noopImageInterface) CoreVariantNeeded(android.BaseModuleContext) bool { return false }
Yifan Hong1b3348d2020-01-21 15:53:22 -0800561func (x noopImageInterface) RamdiskVariantNeeded(android.BaseModuleContext) bool { return false }
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700562func (x noopImageInterface) VendorRamdiskVariantNeeded(android.BaseModuleContext) bool { return false }
Colin Cross7228ecd2019-11-18 16:00:16 -0800563func (x noopImageInterface) RecoveryVariantNeeded(android.BaseModuleContext) bool { return false }
564func (x noopImageInterface) ExtraImageVariations(ctx android.BaseModuleContext) []string { return nil }
565func (x noopImageInterface) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
566}
567
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700568func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700569 properties := &genSrcsProperties{}
570
Colin Crossf1885962020-11-20 15:28:30 -0800571 // finalSubDir is the name of the subdirectory that output files will be generated into.
572 // It is used so that per-shard directories can be placed alongside it an then finally
573 // merged into it.
574 const finalSubDir = "gensrcs"
575
Colin Cross1a527682019-09-23 15:55:30 -0700576 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
Colin Cross1a527682019-09-23 15:55:30 -0700577 shardSize := defaultShardSize
578 if s := properties.Shard_size; s != nil {
579 shardSize = int(*s)
580 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800581
Colin Crossf1885962020-11-20 15:28:30 -0800582 // gensrcs rules can easily hit command line limits by repeating the command for
583 // every input file. Shard the input files into groups.
Colin Cross1a527682019-09-23 15:55:30 -0700584 shards := android.ShardPaths(srcFiles, shardSize)
585 var generateTasks []generateTask
Colin Crossbaccf5b2018-02-21 14:07:48 -0800586
Colin Cross1a527682019-09-23 15:55:30 -0700587 for i, shard := range shards {
588 var commands []string
589 var outFiles android.WritablePaths
Colin Cross3ea4eb82020-11-24 13:07:27 -0800590 var commandDepFiles []string
Colin Cross1a527682019-09-23 15:55:30 -0700591 var copyTo android.WritablePaths
Colin Cross1a527682019-09-23 15:55:30 -0700592
Colin Crossf1885962020-11-20 15:28:30 -0800593 // When sharding is enabled (i.e. len(shards) > 1), the sbox rules for each
594 // shard will be write to their own directories and then be merged together
595 // into finalSubDir. If sharding is not enabled (i.e. len(shards) == 1),
596 // the sbox rule will write directly to finalSubDir.
597 genSubDir := finalSubDir
Colin Cross1a527682019-09-23 15:55:30 -0700598 if len(shards) > 1 {
Colin Crossf1885962020-11-20 15:28:30 -0800599 genSubDir = strconv.Itoa(i)
Jeff Gaston437d23c2017-11-08 12:38:00 -0800600 }
601
Colin Crossf1885962020-11-20 15:28:30 -0800602 genDir := android.PathForModuleGen(ctx, genSubDir)
Jeff Gaston437d23c2017-11-08 12:38:00 -0800603
Colin Cross3ea4eb82020-11-24 13:07:27 -0800604 for _, in := range shard {
Colin Crossf1885962020-11-20 15:28:30 -0800605 outFile := android.GenPathWithExt(ctx, finalSubDir, in, String(properties.Output_extension))
606
607 // If sharding is enabled, then outFile is the path to the output file in
608 // the shard directory, and copyTo is the path to the output file in the
609 // final directory.
Colin Cross1a527682019-09-23 15:55:30 -0700610 if len(shards) > 1 {
Colin Crossf1885962020-11-20 15:28:30 -0800611 shardFile := android.GenPathWithExt(ctx, genSubDir, in, String(properties.Output_extension))
Colin Cross1a527682019-09-23 15:55:30 -0700612 copyTo = append(copyTo, outFile)
613 outFile = shardFile
614 }
615
616 outFiles = append(outFiles, outFile)
Colin Cross1a527682019-09-23 15:55:30 -0700617
Colin Crossf1885962020-11-20 15:28:30 -0800618 // pre-expand the command line to replace $in and $out with references to
619 // a single input and output file.
Colin Cross1a527682019-09-23 15:55:30 -0700620 command, err := android.Expand(rawCommand, func(name string) (string, error) {
621 switch name {
622 case "in":
623 return in.String(), nil
624 case "out":
Colin Crossf1885962020-11-20 15:28:30 -0800625 return android.SboxPathForOutput(outFile, genDir), nil
Colin Cross3ea4eb82020-11-24 13:07:27 -0800626 case "depfile":
627 // Generate a depfile for each output file. Store the list for
628 // later in order to combine them all into a single depfile.
629 depFile := android.SboxPathForOutput(outFile.ReplaceExtension(ctx, "d"), genDir)
630 commandDepFiles = append(commandDepFiles, depFile)
631 return depFile, nil
Colin Cross1a527682019-09-23 15:55:30 -0700632 default:
633 return "$(" + name + ")", nil
634 }
635 })
636 if err != nil {
637 ctx.PropertyErrorf("cmd", err.Error())
638 }
639
640 // escape the command in case for example it contains '#', an odd number of '"', etc
641 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
642 commands = append(commands, command)
643 }
644 fullCommand := strings.Join(commands, " && ")
645
Colin Cross3ea4eb82020-11-24 13:07:27 -0800646 var outputDepfile android.WritablePath
647 var extraTools android.Paths
648 if len(commandDepFiles) > 0 {
649 // Each command wrote to a depfile, but ninja can only handle one
650 // depfile per rule. Use the dep_fixer tool at the end of the
651 // command to combine all the depfiles into a single output depfile.
652 outputDepfile = android.PathForModuleGen(ctx, genSubDir, "gensrcs.d")
653 depFixerTool := ctx.Config().HostToolPath(ctx, "dep_fixer")
654 fullCommand += fmt.Sprintf(" && %s -o $(depfile) %s",
655 depFixerTool.String(), strings.Join(commandDepFiles, " "))
656 extraTools = append(extraTools, depFixerTool)
657 }
658
Colin Cross1a527682019-09-23 15:55:30 -0700659 generateTasks = append(generateTasks, generateTask{
Colin Cross3ea4eb82020-11-24 13:07:27 -0800660 in: shard,
661 out: outFiles,
662 depFile: outputDepfile,
663 copyTo: copyTo,
664 genDir: genDir,
665 cmd: fullCommand,
666 shard: i,
667 shards: len(shards),
668 extraTools: extraTools,
Colin Cross1a527682019-09-23 15:55:30 -0700669 })
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)
Colin Crossf1885962020-11-20 15:28:30 -0800676 g.subDir = finalSubDir
Colin Cross1a527682019-09-23 15:55:30 -0700677 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 Cross3d680512020-11-13 16:23:53 -0800701 var depFile android.WritablePath
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700702 for i, out := range properties.Out {
Colin Cross3d680512020-11-13 16:23:53 -0800703 outPath := android.PathForModuleGen(ctx, out)
704 if i == 0 {
705 depFile = outPath.ReplaceExtension(ctx, "d")
706 }
707 outs[i] = outPath
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700708 }
Colin Cross1a527682019-09-23 15:55:30 -0700709 return []generateTask{{
Colin Cross3d680512020-11-13 16:23:53 -0800710 in: srcFiles,
711 out: outs,
712 depFile: depFile,
713 genDir: android.PathForModuleGen(ctx),
714 cmd: rawCommand,
Colin Cross1a527682019-09-23 15:55:30 -0700715 }}
Colin Cross5049f022015-03-18 13:28:46 -0700716 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700717
Jeff Gaston437d23c2017-11-08 12:38:00 -0800718 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700719}
720
Colin Cross54190b32017-10-09 15:34:10 -0700721func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700722 m := NewGenRule()
723 android.InitAndroidModule(m)
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800724 android.InitDefaultableModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700725 return m
726}
727
Colin Crossd350ecd2015-04-28 13:25:36 -0700728type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700729 // names of the output files that will be generated
Colin Crossef354482018-10-23 11:27:50 -0700730 Out []string `android:"arch_variant"`
Colin Cross5049f022015-03-18 13:28:46 -0700731}
Nan Zhangea568a42017-11-08 21:20:04 -0800732
733var Bool = proptools.Bool
734var String = proptools.String
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800735
736//
737// Defaults
738//
739type Defaults struct {
740 android.ModuleBase
741 android.DefaultsModuleBase
742}
743
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800744func defaultsFactory() android.Module {
745 return DefaultsFactory()
746}
747
748func DefaultsFactory(props ...interface{}) android.Module {
749 module := &Defaults{}
750
751 module.AddProperties(props...)
752 module.AddProperties(
753 &generatorProperties{},
754 &genRuleProperties{},
755 )
756
757 android.InitDefaultsModule(module)
758
759 return module
760}