blob: c9bf958a3089ba3b60a381f06db1e003ae3331c8 [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
Alex Humesky29e3bbe2020-11-20 21:30:13 -050015// A genrule module takes a list of source files ("srcs" property), an optional
16// list of tools ("tools" property), and a command line ("cmd" property), to
17// generate output files ("out" property).
18
Colin Cross5049f022015-03-18 13:28:46 -070019package genrule
20
21import (
Colin Cross6f080df2016-11-04 15:32:58 -070022 "fmt"
Colin Crossa4ad2b02019-03-18 22:15:32 -070023 "io"
Colin Cross3d680512020-11-13 16:23:53 -080024 "path/filepath"
Colin Cross1a527682019-09-23 15:55:30 -070025 "strconv"
Colin Cross6f080df2016-11-04 15:32:58 -070026 "strings"
Dan Willemsen3f4539b2016-09-28 16:19:10 -070027
Colin Cross70b40592015-03-23 12:57:34 -070028 "github.com/google/blueprint"
Dan Willemsen8eded0a2017-09-13 16:07:44 -070029 "github.com/google/blueprint/bootstrap"
Nan Zhangea568a42017-11-08 21:20:04 -080030 "github.com/google/blueprint/proptools"
Colin Cross5049f022015-03-18 13:28:46 -070031
Colin Cross635c3b02016-05-18 15:37:25 -070032 "android/soong/android"
Jingwen Chen30f5aaa2020-11-19 05:38:02 -050033 "android/soong/bazel"
Colin Cross5049f022015-03-18 13:28:46 -070034)
35
Colin Cross463a90e2015-06-17 14:20:06 -070036func init() {
Colin Crosse9fe2942020-11-10 18:12:15 -080037 RegisterGenruleBuildComponents(android.InitRegistrationContext)
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000038}
Jaewoong Jung98716bd2018-12-10 08:13:18 -080039
Paul Duffin672cb9f2021-03-03 02:30:37 +000040// Test fixture preparer that will register most genrule build components.
41//
42// Singletons and mutators should only be added here if they are needed for a majority of genrule
43// module types, otherwise they should be added under a separate preparer to allow them to be
44// selected only when needed to reduce test execution time.
45//
46// Module types do not have much of an overhead unless they are used so this should include as many
47// module types as possible. The exceptions are those module types that require mutators and/or
48// singletons in order to function in which case they should be kept together in a separate
49// preparer.
50var PrepareForTestWithGenRuleBuildComponents = android.GroupFixturePreparers(
51 android.FixtureRegisterWithContext(RegisterGenruleBuildComponents),
52)
53
54// Prepare a fixture to use all genrule module types, mutators and singletons fully.
55//
56// This should only be used by tests that want to run with as much of the build enabled as possible.
57var PrepareForIntegrationTestWithGenrule = android.GroupFixturePreparers(
58 PrepareForTestWithGenRuleBuildComponents,
59)
60
Colin Crosse9fe2942020-11-10 18:12:15 -080061func RegisterGenruleBuildComponents(ctx android.RegistrationContext) {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000062 ctx.RegisterModuleType("genrule_defaults", defaultsFactory)
63
64 ctx.RegisterModuleType("gensrcs", GenSrcsFactory)
65 ctx.RegisterModuleType("genrule", GenRuleFactory)
66
67 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
68 ctx.BottomUp("genrule_tool_deps", toolDepsMutator).Parallel()
69 })
Jingwen Chen316e07c2020-12-14 09:09:52 -050070
Jingwen Chena42d6412021-01-26 21:57:27 -050071 android.RegisterBp2BuildMutator("genrule", GenruleBp2Build)
Wei Libcd39942021-09-16 23:57:28 +000072 android.RegisterBp2BuildMutator("cc_genrule", CcGenruleBp2Build)
Colin Cross463a90e2015-06-17 14:20:06 -070073}
74
Liz Kammer356f7d42021-01-26 09:18:53 -050075func RegisterGenruleBp2BuildDeps(ctx android.RegisterMutatorsContext) {
76 ctx.BottomUp("genrule_tool_deps", toolDepsMutator)
77}
78
Colin Cross5049f022015-03-18 13:28:46 -070079var (
Colin Cross635c3b02016-05-18 15:37:25 -070080 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross1a527682019-09-23 15:55:30 -070081
Alex Humesky29e3bbe2020-11-20 21:30:13 -050082 // Used by gensrcs when there is more than 1 shard to merge the outputs
83 // of each shard into a zip file.
Colin Cross1a527682019-09-23 15:55:30 -070084 gensrcsMerge = pctx.AndroidStaticRule("gensrcsMerge", blueprint.RuleParams{
85 Command: "${soongZip} -o ${tmpZip} @${tmpZip}.rsp && ${zipSync} -d ${genDir} ${tmpZip}",
86 CommandDeps: []string{"${soongZip}", "${zipSync}"},
87 Rspfile: "${tmpZip}.rsp",
88 RspfileContent: "${zipArgs}",
89 }, "tmpZip", "genDir", "zipArgs")
Colin Cross5049f022015-03-18 13:28:46 -070090)
91
Jeff Gastonefc1b412017-03-29 17:29:06 -070092func init() {
Dan Willemsenddf504c2019-08-09 16:21:29 -070093 pctx.Import("android/soong/android")
Colin Cross1a527682019-09-23 15:55:30 -070094
95 pctx.HostBinToolVariable("soongZip", "soong_zip")
96 pctx.HostBinToolVariable("zipSync", "zipsync")
Jeff Gastonefc1b412017-03-29 17:29:06 -070097}
98
Colin Cross5049f022015-03-18 13:28:46 -070099type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700100 GeneratedSourceFiles() android.Paths
Colin Cross5ed99c62016-11-22 12:55:55 -0800101 GeneratedHeaderDirs() android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800102 GeneratedDeps() android.Paths
Colin Cross5049f022015-03-18 13:28:46 -0700103}
104
Colin Crossfe17f6f2019-03-28 19:30:56 -0700105// Alias for android.HostToolProvider
106// Deprecated: use android.HostToolProvider instead.
Colin Crossd350ecd2015-04-28 13:25:36 -0700107type HostToolProvider interface {
Colin Crossfe17f6f2019-03-28 19:30:56 -0700108 android.HostToolProvider
Colin Crossd350ecd2015-04-28 13:25:36 -0700109}
Colin Cross5049f022015-03-18 13:28:46 -0700110
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700111type hostToolDependencyTag struct {
112 blueprint.BaseDependencyTag
Colin Cross08f15ab2018-10-04 23:29:14 -0700113 label string
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700114}
Colin Cross7d5136f2015-05-11 13:39:40 -0700115type generatorProperties struct {
Spandan Das93e95992021-07-29 18:26:39 +0000116 // The command to run on one or more input files. Cmd supports substitution of a few variables.
Jeff Gastonefc1b412017-03-29 17:29:06 -0700117 //
118 // Available variables for substitution:
119 //
Spandan Das93e95992021-07-29 18:26:39 +0000120 // $(location): the path to the first entry in tools or tool_files.
121 // $(location <label>): the path to the tool, tool_file, input or output with name <label>. Use $(location) if <label> refers to a rule that outputs exactly one file.
122 // $(locations <label>): the paths to the tools, tool_files, inputs or outputs with name <label>. Use $(locations) if <label> refers to a rule that outputs two or more files.
123 // $(in): one or more input files.
124 // $(out): a single output file.
125 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true.
126 // $(genDir): the sandbox directory for this tool; contains $(out).
Colin Cross2296f5b2017-10-17 21:38:14 -0700127 // $$: a literal $
Nan Zhangea568a42017-11-08 21:20:04 -0800128 Cmd *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700129
Colin Cross33bfb0a2016-11-21 17:23:08 -0800130 // Enable reading a file containing dependencies in gcc format after the command completes
Nan Zhangea568a42017-11-08 21:20:04 -0800131 Depfile *bool
Colin Cross33bfb0a2016-11-21 17:23:08 -0800132
Colin Cross6f080df2016-11-04 15:32:58 -0700133 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -0700134 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -0700135 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700136
137 // Local file that is used as the tool
Colin Cross27b922f2019-03-04 22:35:41 -0800138 Tool_files []string `android:"path"`
Colin Cross5ed99c62016-11-22 12:55:55 -0800139
140 // List of directories to export generated headers from
141 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -0800142
143 // list of input files
Colin Cross27b922f2019-03-04 22:35:41 -0800144 Srcs []string `android:"path,arch_variant"`
Dan Willemseneefa0262018-11-17 14:01:18 -0800145
146 // input files to exclude
Colin Cross27b922f2019-03-04 22:35:41 -0800147 Exclude_srcs []string `android:"path,arch_variant"`
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400148}
Alex Humesky29e3bbe2020-11-20 21:30:13 -0500149
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700150type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700151 android.ModuleBase
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800152 android.DefaultableModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500153 android.BazelModuleBase
Jiyong Parkfc752ca2019-06-12 13:27:29 +0900154 android.ApexModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -0700155
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700156 // For other packages to make their own genrules with extra
157 // properties
158 Extra interface{}
Colin Crossf3bfd022021-09-27 15:15:06 -0700159
160 // CmdModifier can be set by wrappers around genrule to modify the command, for example to
161 // prefix environment variables to it.
162 CmdModifier func(ctx android.ModuleContext, cmd string) string
163
Colin Cross7228ecd2019-11-18 16:00:16 -0800164 android.ImageInterface
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700165
Colin Cross7d5136f2015-05-11 13:39:40 -0700166 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700167
Alex Humesky29e3bbe2020-11-20 21:30:13 -0500168 // For the different tasks that genrule and gensrc generate. genrule will
169 // generate 1 task, and gensrc will generate 1 or more tasks based on the
170 // number of shards the input files are sharded into.
Jeff Gaston437d23c2017-11-08 12:38:00 -0800171 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700172
Colin Cross1a527682019-09-23 15:55:30 -0700173 rule blueprint.Rule
174 rawCommands []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700175
Colin Cross5ed99c62016-11-22 12:55:55 -0800176 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700177
Colin Cross635c3b02016-05-18 15:37:25 -0700178 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800179 outputDeps android.Paths
Colin Crossa4ad2b02019-03-18 22:15:32 -0700180
181 subName string
Colin Cross1a527682019-09-23 15:55:30 -0700182 subDir string
bralee1fbf4402020-05-21 10:11:59 +0800183
184 // Collect the module directory for IDE info in java/jdeps.go.
185 modulePaths []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700186}
187
Colin Cross1a527682019-09-23 15:55:30 -0700188type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700189
190type generateTask struct {
Colin Cross3ea4eb82020-11-24 13:07:27 -0800191 in android.Paths
192 out android.WritablePaths
193 depFile android.WritablePath
Alex Humesky29e3bbe2020-11-20 21:30:13 -0500194 copyTo android.WritablePaths // For gensrcs to set on gensrcsMerge rule.
Colin Cross3ea4eb82020-11-24 13:07:27 -0800195 genDir android.WritablePath
196 extraTools android.Paths // dependencies on tools used by the generator
197
Alex Humesky29e3bbe2020-11-20 21:30:13 -0500198 cmd string
199 // For gensrsc sharding.
Colin Cross3ea4eb82020-11-24 13:07:27 -0800200 shard int
201 shards int
Colin Crossd350ecd2015-04-28 13:25:36 -0700202}
203
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700204func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700205 return g.outputFiles
206}
207
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700208func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700209 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800210}
211
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700212func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800213 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700214}
215
Dan Willemsen9da9d492018-02-21 18:28:18 -0800216func (g *Module) GeneratedDeps() android.Paths {
217 return g.outputDeps
218}
219
Jooyung Han8c7e3ed2021-06-28 17:35:58 +0900220func (g *Module) OutputFiles(tag string) (android.Paths, error) {
221 if tag == "" {
222 return append(android.Paths{}, g.outputFiles...), nil
223 }
224 // otherwise, tag should match one of outputs
225 for _, outputFile := range g.outputFiles {
226 if outputFile.Rel() == tag {
227 return android.Paths{outputFile}, nil
228 }
229 }
230 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
231}
232
233var _ android.SourceFileProducer = (*Module)(nil)
234var _ android.OutputFileProducer = (*Module)(nil)
235
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000236func toolDepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700237 if g, ok := ctx.Module().(*Module); ok {
Colin Cross08f15ab2018-10-04 23:29:14 -0700238 for _, tool := range g.properties.Tools {
239 tag := hostToolDependencyTag{label: tool}
240 if m := android.SrcIsModule(tool); m != "" {
241 tool = m
242 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700243 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700244 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700245 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700246}
247
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400248// Returns true if information was available from Bazel, false if bazel invocation still needs to occur.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +0000249func (c *Module) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400250 bazelCtx := ctx.Config().BazelContext
Chris Parsons787fb362021-10-14 18:43:51 -0400251 filePaths, ok := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx))
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400252 if ok {
253 var bazelOutputFiles android.Paths
Chris Parsonse59af4e2021-03-31 13:32:41 -0400254 exportIncludeDirs := map[string]bool{}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400255 for _, bazelOutputFile := range filePaths {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500256 bazelOutputFiles = append(bazelOutputFiles, android.PathForBazelOut(ctx, bazelOutputFile))
Chris Parsonse59af4e2021-03-31 13:32:41 -0400257 exportIncludeDirs[filepath.Dir(bazelOutputFile)] = true
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400258 }
259 c.outputFiles = bazelOutputFiles
260 c.outputDeps = bazelOutputFiles
Chris Parsonse59af4e2021-03-31 13:32:41 -0400261 for includePath, _ := range exportIncludeDirs {
262 c.exportedIncludeDirs = append(c.exportedIncludeDirs, android.PathForBazelOut(ctx, includePath))
263 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400264 }
265 return ok
266}
Colin Crossf1885962020-11-20 15:28:30 -0800267
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700268func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa4ad2b02019-03-18 22:15:32 -0700269 g.subName = ctx.ModuleSubDir()
270
bralee1fbf4402020-05-21 10:11:59 +0800271 // Collect the module directory for IDE info in java/jdeps.go.
272 g.modulePaths = append(g.modulePaths, ctx.ModuleDir())
273
Colin Cross5ed99c62016-11-22 12:55:55 -0800274 if len(g.properties.Export_include_dirs) > 0 {
275 for _, dir := range g.properties.Export_include_dirs {
276 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
Colin Cross1a527682019-09-23 15:55:30 -0700277 android.PathForModuleGen(ctx, g.subDir, ctx.ModuleDir(), dir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800278 }
279 } else {
Colin Cross1a527682019-09-23 15:55:30 -0700280 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, g.subDir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800281 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700282
Colin Crossd11cf622021-03-23 22:30:35 -0700283 locationLabels := map[string]location{}
Colin Cross08f15ab2018-10-04 23:29:14 -0700284 firstLabel := ""
285
Colin Crossd11cf622021-03-23 22:30:35 -0700286 addLocationLabel := func(label string, loc location) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700287 if firstLabel == "" {
288 firstLabel = label
289 }
290 if _, exists := locationLabels[label]; !exists {
Colin Crossd11cf622021-03-23 22:30:35 -0700291 locationLabels[label] = loc
Colin Cross08f15ab2018-10-04 23:29:14 -0700292 } else {
293 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
Colin Crossd11cf622021-03-23 22:30:35 -0700294 label, locationLabels[label], loc)
Colin Cross08f15ab2018-10-04 23:29:14 -0700295 }
296 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700297
Colin Crossba9e4032020-11-24 16:32:22 -0800298 var tools android.Paths
299 var packagedTools []android.PackagingSpec
Colin Cross6f080df2016-11-04 15:32:58 -0700300 if len(g.properties.Tools) > 0 {
Colin Crossba71a3f2019-03-18 12:12:48 -0700301 seenTools := make(map[string]bool)
302
Colin Cross35143d02017-11-16 00:11:20 -0800303 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700304 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
305 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700306 tool := ctx.OtherModuleName(module)
307
Colin Crossba9e4032020-11-24 16:32:22 -0800308 switch t := module.(type) {
309 case android.HostToolProvider:
310 // A HostToolProvider provides the path to a tool, which will be copied
311 // into the sandbox.
Colin Cross35143d02017-11-16 00:11:20 -0800312 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800313 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800314 ctx.AddMissingDependencies([]string{tool})
315 } else {
316 ctx.ModuleErrorf("depends on disabled module %q", tool)
317 }
Colin Crossba9e4032020-11-24 16:32:22 -0800318 return
Colin Cross35143d02017-11-16 00:11:20 -0800319 }
Colin Crossba9e4032020-11-24 16:32:22 -0800320 path := t.HostToolPath()
321 if !path.Valid() {
322 ctx.ModuleErrorf("host tool %q missing output file", tool)
323 return
324 }
325 if specs := t.TransitivePackagingSpecs(); specs != nil {
326 // If the HostToolProvider has PackgingSpecs, which are definitions of the
327 // required relative locations of the tool and its dependencies, use those
328 // instead. They will be copied to those relative locations in the sbox
329 // sandbox.
330 packagedTools = append(packagedTools, specs...)
331 // Assume that the first PackagingSpec of the module is the tool.
Colin Crossd11cf622021-03-23 22:30:35 -0700332 addLocationLabel(tag.label, packagedToolLocation{specs[0]})
Colin Crossba9e4032020-11-24 16:32:22 -0800333 } else {
334 tools = append(tools, path.Path())
Colin Crossd11cf622021-03-23 22:30:35 -0700335 addLocationLabel(tag.label, toolLocation{android.Paths{path.Path()}})
Colin Crossba9e4032020-11-24 16:32:22 -0800336 }
337 case bootstrap.GoBinaryTool:
338 // A GoBinaryTool provides the install path to a tool, which will be copied.
Colin Crossa44551f2021-10-25 15:36:21 -0700339 p := android.PathForGoBinary(ctx, t)
340 tools = append(tools, p)
341 addLocationLabel(tag.label, toolLocation{android.Paths{p}})
Colin Crossba9e4032020-11-24 16:32:22 -0800342 default:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700343 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Colin Crossba9e4032020-11-24 16:32:22 -0800344 return
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700345 }
346
Colin Crossba9e4032020-11-24 16:32:22 -0800347 seenTools[tag.label] = true
Colin Crossd350ecd2015-04-28 13:25:36 -0700348 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700349 })
Colin Crossba71a3f2019-03-18 12:12:48 -0700350
351 // If AllowMissingDependencies is enabled, the build will not have stopped when
352 // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical
Liz Kammer20ebfb42020-07-28 11:32:07 -0700353 // "cmd: unknown location label ..." errors later. Add a placeholder file to the local label.
354 // The command that uses this placeholder file will never be executed because the rule will be
355 // replaced with an android.Error rule reporting the missing dependencies.
Colin Crossba71a3f2019-03-18 12:12:48 -0700356 if ctx.Config().AllowMissingDependencies() {
357 for _, tool := range g.properties.Tools {
358 if !seenTools[tool] {
Colin Crossd11cf622021-03-23 22:30:35 -0700359 addLocationLabel(tool, errorLocation{"***missing tool " + tool + "***"})
Colin Crossba71a3f2019-03-18 12:12:48 -0700360 }
361 }
362 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700363 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700364
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700365 if ctx.Failed() {
366 return
367 }
368
Colin Cross08f15ab2018-10-04 23:29:14 -0700369 for _, toolFile := range g.properties.Tool_files {
Colin Cross8a497952019-03-05 22:25:09 -0800370 paths := android.PathsForModuleSrc(ctx, []string{toolFile})
Colin Crossba9e4032020-11-24 16:32:22 -0800371 tools = append(tools, paths...)
Colin Crossd11cf622021-03-23 22:30:35 -0700372 addLocationLabel(toolFile, toolLocation{paths})
Colin Cross08f15ab2018-10-04 23:29:14 -0700373 }
374
375 var srcFiles android.Paths
376 for _, in := range g.properties.Srcs {
Colin Crossba71a3f2019-03-18 12:12:48 -0700377 paths, missingDeps := android.PathsAndMissingDepsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs)
378 if len(missingDeps) > 0 {
379 if !ctx.Config().AllowMissingDependencies() {
380 panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator",
381 missingDeps))
382 }
383
384 // If AllowMissingDependencies is enabled, the build will not have stopped when
385 // the dependency was added on a missing SourceFileProducer module, which will result in nonsensical
Liz Kammer20ebfb42020-07-28 11:32:07 -0700386 // "cmd: label ":..." has no files" errors later. Add a placeholder file to the local label.
387 // The command that uses this placeholder file will never be executed because the rule will be
388 // replaced with an android.Error rule reporting the missing dependencies.
Colin Crossba71a3f2019-03-18 12:12:48 -0700389 ctx.AddMissingDependencies(missingDeps)
Colin Crossd11cf622021-03-23 22:30:35 -0700390 addLocationLabel(in, errorLocation{"***missing srcs " + in + "***"})
Colin Crossba71a3f2019-03-18 12:12:48 -0700391 } else {
392 srcFiles = append(srcFiles, paths...)
Colin Crossd11cf622021-03-23 22:30:35 -0700393 addLocationLabel(in, inputLocation{paths})
Colin Crossba71a3f2019-03-18 12:12:48 -0700394 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700395 }
396
Colin Cross1a527682019-09-23 15:55:30 -0700397 var copyFrom android.Paths
398 var outputFiles android.WritablePaths
399 var zipArgs strings.Builder
Colin Cross08f15ab2018-10-04 23:29:14 -0700400
Colin Crossf3bfd022021-09-27 15:15:06 -0700401 cmd := String(g.properties.Cmd)
402 if g.CmdModifier != nil {
403 cmd = g.CmdModifier(ctx, cmd)
404 }
405
Alex Humesky29e3bbe2020-11-20 21:30:13 -0500406 // Generate tasks, either from genrule or gensrcs.
Colin Crossf3bfd022021-09-27 15:15:06 -0700407 for _, task := range g.taskGenerator(ctx, cmd, srcFiles) {
Colin Cross3d680512020-11-13 16:23:53 -0800408 if len(task.out) == 0 {
409 ctx.ModuleErrorf("must have at least one output file")
410 return
Colin Cross85a2e892018-07-09 09:45:06 -0700411 }
412
Colin Crossf1a035e2020-11-16 17:32:30 -0800413 // Pick a unique path outside the task.genDir for the sbox manifest textproto,
414 // a unique rule name, and the user-visible description.
415 manifestName := "genrule.sbox.textproto"
416 desc := "generate"
417 name := "generator"
418 if task.shards > 0 {
419 manifestName = "genrule_" + strconv.Itoa(task.shard) + ".sbox.textproto"
420 desc += " " + strconv.Itoa(task.shard)
421 name += strconv.Itoa(task.shard)
422 } else if len(task.out) == 1 {
423 desc += " " + task.out[0].Base()
424 }
425
426 manifestPath := android.PathForModuleOut(ctx, manifestName)
427
428 // Use a RuleBuilder to create a rule that runs the command inside an sbox sandbox.
Colin Crossba9e4032020-11-24 16:32:22 -0800429 rule := android.NewRuleBuilder(pctx, ctx).Sbox(task.genDir, manifestPath).SandboxTools()
Colin Crossf1a035e2020-11-16 17:32:30 -0800430 cmd := rule.Command()
431
Colin Cross3d680512020-11-13 16:23:53 -0800432 for _, out := range task.out {
Colin Crossd11cf622021-03-23 22:30:35 -0700433 addLocationLabel(out.Rel(), outputLocation{out})
Colin Cross3d680512020-11-13 16:23:53 -0800434 }
435
Colin Cross1a527682019-09-23 15:55:30 -0700436 referencedDepfile := false
437
Colin Cross3d680512020-11-13 16:23:53 -0800438 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross1a527682019-09-23 15:55:30 -0700439 // report the error directly without returning an error to android.Expand to catch multiple errors in a
440 // single run
Colin Cross3d680512020-11-13 16:23:53 -0800441 reportError := func(fmt string, args ...interface{}) (string, error) {
Colin Cross1a527682019-09-23 15:55:30 -0700442 ctx.PropertyErrorf("cmd", fmt, args...)
Colin Cross3d680512020-11-13 16:23:53 -0800443 return "SOONG_ERROR", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700444 }
Colin Cross1a527682019-09-23 15:55:30 -0700445
446 switch name {
447 case "location":
448 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
449 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700450 }
Colin Crossd11cf622021-03-23 22:30:35 -0700451 loc := locationLabels[firstLabel]
452 paths := loc.Paths(cmd)
Colin Cross1a527682019-09-23 15:55:30 -0700453 if len(paths) == 0 {
454 return reportError("default label %q has no files", firstLabel)
455 } else if len(paths) > 1 {
456 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
457 firstLabel, firstLabel)
Colin Cross08f15ab2018-10-04 23:29:14 -0700458 }
Colin Crossd11cf622021-03-23 22:30:35 -0700459 return paths[0], nil
Colin Cross1a527682019-09-23 15:55:30 -0700460 case "in":
Colin Crossd11cf622021-03-23 22:30:35 -0700461 return strings.Join(cmd.PathsForInputs(srcFiles), " "), nil
Colin Cross1a527682019-09-23 15:55:30 -0700462 case "out":
Colin Cross3d680512020-11-13 16:23:53 -0800463 var sandboxOuts []string
464 for _, out := range task.out {
Colin Crossf1a035e2020-11-16 17:32:30 -0800465 sandboxOuts = append(sandboxOuts, cmd.PathForOutput(out))
Colin Cross3d680512020-11-13 16:23:53 -0800466 }
467 return strings.Join(sandboxOuts, " "), nil
Colin Cross1a527682019-09-23 15:55:30 -0700468 case "depfile":
469 referencedDepfile = true
470 if !Bool(g.properties.Depfile) {
471 return reportError("$(depfile) used without depfile property")
472 }
Colin Cross3d680512020-11-13 16:23:53 -0800473 return "__SBOX_DEPFILE__", nil
Colin Cross1a527682019-09-23 15:55:30 -0700474 case "genDir":
Colin Crossf1a035e2020-11-16 17:32:30 -0800475 return cmd.PathForOutput(task.genDir), nil
Colin Cross1a527682019-09-23 15:55:30 -0700476 default:
477 if strings.HasPrefix(name, "location ") {
478 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Colin Crossd11cf622021-03-23 22:30:35 -0700479 if loc, ok := locationLabels[label]; ok {
480 paths := loc.Paths(cmd)
Colin Cross1a527682019-09-23 15:55:30 -0700481 if len(paths) == 0 {
482 return reportError("label %q has no files", label)
483 } else if len(paths) > 1 {
484 return reportError("label %q has multiple files, use $(locations %s) to reference it",
485 label, label)
486 }
Colin Cross3d680512020-11-13 16:23:53 -0800487 return paths[0], nil
Colin Cross1a527682019-09-23 15:55:30 -0700488 } else {
489 return reportError("unknown location label %q", label)
490 }
491 } else if strings.HasPrefix(name, "locations ") {
492 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
Colin Crossd11cf622021-03-23 22:30:35 -0700493 if loc, ok := locationLabels[label]; ok {
494 paths := loc.Paths(cmd)
Colin Cross1a527682019-09-23 15:55:30 -0700495 if len(paths) == 0 {
496 return reportError("label %q has no files", label)
497 }
Colin Cross3d680512020-11-13 16:23:53 -0800498 return strings.Join(paths, " "), nil
Colin Cross1a527682019-09-23 15:55:30 -0700499 } else {
500 return reportError("unknown locations label %q", label)
501 }
502 } else {
503 return reportError("unknown variable '$(%s)'", name)
504 }
Colin Cross6f080df2016-11-04 15:32:58 -0700505 }
Colin Cross1a527682019-09-23 15:55:30 -0700506 })
507
508 if err != nil {
509 ctx.PropertyErrorf("cmd", "%s", err.Error())
510 return
Colin Cross6f080df2016-11-04 15:32:58 -0700511 }
Colin Cross6f080df2016-11-04 15:32:58 -0700512
Colin Cross1a527682019-09-23 15:55:30 -0700513 if Bool(g.properties.Depfile) && !referencedDepfile {
514 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
515 return
516 }
Colin Cross1a527682019-09-23 15:55:30 -0700517 g.rawCommands = append(g.rawCommands, rawCommand)
Bill Peckhamc087be12020-02-13 15:55:10 -0800518
Colin Cross3d680512020-11-13 16:23:53 -0800519 cmd.Text(rawCommand)
520 cmd.ImplicitOutputs(task.out)
521 cmd.Implicits(task.in)
Colin Crossba9e4032020-11-24 16:32:22 -0800522 cmd.ImplicitTools(tools)
523 cmd.ImplicitTools(task.extraTools)
524 cmd.ImplicitPackagedTools(packagedTools)
Colin Cross3d680512020-11-13 16:23:53 -0800525 if Bool(g.properties.Depfile) {
526 cmd.ImplicitDepFile(task.depFile)
527 }
528
529 // Create the rule to run the genrule command inside sbox.
Colin Crossf1a035e2020-11-16 17:32:30 -0800530 rule.Build(name, desc)
Colin Cross1a527682019-09-23 15:55:30 -0700531
532 if len(task.copyTo) > 0 {
Colin Cross3d680512020-11-13 16:23:53 -0800533 // If copyTo is set, multiple shards need to be copied into a single directory.
534 // task.out contains the per-shard paths, and copyTo contains the corresponding
535 // final path. The files need to be copied into the final directory by a
536 // single rule so it can remove the directory before it starts to ensure no
537 // old files remain. zipsync already does this, so build up zipArgs that
538 // zip all the per-shard directories into a single zip.
Colin Cross1a527682019-09-23 15:55:30 -0700539 outputFiles = append(outputFiles, task.copyTo...)
540 copyFrom = append(copyFrom, task.out.Paths()...)
541 zipArgs.WriteString(" -C " + task.genDir.String())
542 zipArgs.WriteString(android.JoinWithPrefix(task.out.Strings(), " -f "))
543 } else {
544 outputFiles = append(outputFiles, task.out...)
545 }
Colin Cross6f080df2016-11-04 15:32:58 -0700546 }
547
Colin Cross1a527682019-09-23 15:55:30 -0700548 if len(copyFrom) > 0 {
Colin Cross3d680512020-11-13 16:23:53 -0800549 // Create a rule that zips all the per-shard directories into a single zip and then
550 // uses zipsync to unzip it into the final directory.
Colin Cross1a527682019-09-23 15:55:30 -0700551 ctx.Build(pctx, android.BuildParams{
Colin Crossf1885962020-11-20 15:28:30 -0800552 Rule: gensrcsMerge,
553 Implicits: copyFrom,
554 Outputs: outputFiles,
555 Description: "merge shards",
Colin Cross1a527682019-09-23 15:55:30 -0700556 Args: map[string]string{
557 "zipArgs": zipArgs.String(),
558 "tmpZip": android.PathForModuleGen(ctx, g.subDir+".zip").String(),
559 "genDir": android.PathForModuleGen(ctx, g.subDir).String(),
560 },
561 })
Colin Cross85a2e892018-07-09 09:45:06 -0700562 }
563
Colin Cross1a527682019-09-23 15:55:30 -0700564 g.outputFiles = outputFiles.Paths()
Jeff Gastonefc1b412017-03-29 17:29:06 -0700565
Liz Kammerbdc60992021-02-24 16:55:11 -0500566 bazelModuleLabel := g.GetBazelLabel(ctx, g)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400567 bazelActionsUsed := false
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400568 if g.MixedBuildsEnabled(ctx) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +0000569 bazelActionsUsed = g.GenerateBazelBuildActions(ctx, bazelModuleLabel)
Jeff Gaston02a684b2017-10-27 14:59:27 -0700570 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400571 if !bazelActionsUsed {
572 // For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of
573 // the genrules on AOSP. That will make things simpler to look at the graph in the common
574 // case. For larger sets of outputs, inject a phony target in between to limit ninja file
575 // growth.
576 if len(g.outputFiles) <= 6 {
577 g.outputDeps = g.outputFiles
578 } else {
579 phonyFile := android.PathForModuleGen(ctx, "genrule-phony")
580 ctx.Build(pctx, android.BuildParams{
581 Rule: blueprint.Phony,
582 Output: phonyFile,
583 Inputs: g.outputFiles,
584 })
585 g.outputDeps = android.Paths{phonyFile}
586 }
587 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700588}
Colin Crossd350ecd2015-04-28 13:25:36 -0700589
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700590// Collect information for opening IDE project files in java/jdeps.go.
591func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
592 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
593 for _, src := range g.properties.Srcs {
594 if strings.HasPrefix(src, ":") {
595 src = strings.Trim(src, ":")
596 dpInfo.Deps = append(dpInfo.Deps, src)
597 }
598 }
bralee1fbf4402020-05-21 10:11:59 +0800599 dpInfo.Paths = append(dpInfo.Paths, g.modulePaths...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700600}
601
Colin Crossa4ad2b02019-03-18 22:15:32 -0700602func (g *Module) AndroidMk() android.AndroidMkData {
603 return android.AndroidMkData{
Anton Hansson72f18492020-10-30 16:34:45 +0000604 Class: "ETC",
Colin Crossa4ad2b02019-03-18 22:15:32 -0700605 OutputFile: android.OptionalPathForPath(g.outputFiles[0]),
606 SubName: g.subName,
607 Extra: []android.AndroidMkExtraFunc{
608 func(w io.Writer, outputFile android.Path) {
Anton Hansson72f18492020-10-30 16:34:45 +0000609 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
Colin Crossa4ad2b02019-03-18 22:15:32 -0700610 },
611 },
612 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
613 android.WriteAndroidMkData(w, data)
614 if data.SubName != "" {
615 fmt.Fprintln(w, ".PHONY:", name)
616 fmt.Fprintln(w, name, ":", name+g.subName)
617 }
618 },
619 }
620}
621
Jiyong Park45bf82e2020-12-15 22:29:02 +0900622var _ android.ApexModule = (*Module)(nil)
623
624// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -0700625func (g *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
626 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900627 // Because generated outputs are checked by client modules(e.g. cc_library, ...)
628 // we can safely ignore the check here.
629 return nil
630}
631
Jeff Gaston437d23c2017-11-08 12:38:00 -0800632func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700633 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800634 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700635 }
636
Colin Cross36242852017-06-23 15:06:31 -0700637 module.AddProperties(props...)
638 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700639
Colin Cross7228ecd2019-11-18 16:00:16 -0800640 module.ImageInterface = noopImageInterface{}
641
Colin Cross36242852017-06-23 15:06:31 -0700642 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700643}
644
Colin Cross7228ecd2019-11-18 16:00:16 -0800645type noopImageInterface struct{}
646
647func (x noopImageInterface) ImageMutatorBegin(android.BaseModuleContext) {}
648func (x noopImageInterface) CoreVariantNeeded(android.BaseModuleContext) bool { return false }
Yifan Hong1b3348d2020-01-21 15:53:22 -0800649func (x noopImageInterface) RamdiskVariantNeeded(android.BaseModuleContext) bool { return false }
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700650func (x noopImageInterface) VendorRamdiskVariantNeeded(android.BaseModuleContext) bool { return false }
Inseob Kim08758f02021-04-08 21:13:22 +0900651func (x noopImageInterface) DebugRamdiskVariantNeeded(android.BaseModuleContext) bool { return false }
Colin Cross7228ecd2019-11-18 16:00:16 -0800652func (x noopImageInterface) RecoveryVariantNeeded(android.BaseModuleContext) bool { return false }
653func (x noopImageInterface) ExtraImageVariations(ctx android.BaseModuleContext) []string { return nil }
654func (x noopImageInterface) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
655}
656
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700657func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700658 properties := &genSrcsProperties{}
659
Colin Crossf1885962020-11-20 15:28:30 -0800660 // finalSubDir is the name of the subdirectory that output files will be generated into.
661 // It is used so that per-shard directories can be placed alongside it an then finally
662 // merged into it.
663 const finalSubDir = "gensrcs"
664
Colin Cross1a527682019-09-23 15:55:30 -0700665 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
Colin Cross1a527682019-09-23 15:55:30 -0700666 shardSize := defaultShardSize
667 if s := properties.Shard_size; s != nil {
668 shardSize = int(*s)
669 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800670
Colin Crossf1885962020-11-20 15:28:30 -0800671 // gensrcs rules can easily hit command line limits by repeating the command for
672 // every input file. Shard the input files into groups.
Colin Cross1a527682019-09-23 15:55:30 -0700673 shards := android.ShardPaths(srcFiles, shardSize)
674 var generateTasks []generateTask
Colin Crossbaccf5b2018-02-21 14:07:48 -0800675
Colin Cross1a527682019-09-23 15:55:30 -0700676 for i, shard := range shards {
677 var commands []string
678 var outFiles android.WritablePaths
Colin Cross3ea4eb82020-11-24 13:07:27 -0800679 var commandDepFiles []string
Colin Cross1a527682019-09-23 15:55:30 -0700680 var copyTo android.WritablePaths
Colin Cross1a527682019-09-23 15:55:30 -0700681
Colin Crossf1885962020-11-20 15:28:30 -0800682 // When sharding is enabled (i.e. len(shards) > 1), the sbox rules for each
683 // shard will be write to their own directories and then be merged together
684 // into finalSubDir. If sharding is not enabled (i.e. len(shards) == 1),
685 // the sbox rule will write directly to finalSubDir.
686 genSubDir := finalSubDir
Colin Cross1a527682019-09-23 15:55:30 -0700687 if len(shards) > 1 {
Colin Crossf1885962020-11-20 15:28:30 -0800688 genSubDir = strconv.Itoa(i)
Jeff Gaston437d23c2017-11-08 12:38:00 -0800689 }
690
Colin Crossf1885962020-11-20 15:28:30 -0800691 genDir := android.PathForModuleGen(ctx, genSubDir)
Colin Crossf1a035e2020-11-16 17:32:30 -0800692 // TODO(ccross): this RuleBuilder is a hack to be able to call
693 // rule.Command().PathForOutput. Replace this with passing the rule into the
694 // generator.
Colin Crossba9e4032020-11-24 16:32:22 -0800695 rule := android.NewRuleBuilder(pctx, ctx).Sbox(genDir, nil).SandboxTools()
Jeff Gaston437d23c2017-11-08 12:38:00 -0800696
Colin Cross3ea4eb82020-11-24 13:07:27 -0800697 for _, in := range shard {
Colin Crossf1885962020-11-20 15:28:30 -0800698 outFile := android.GenPathWithExt(ctx, finalSubDir, in, String(properties.Output_extension))
699
700 // If sharding is enabled, then outFile is the path to the output file in
701 // the shard directory, and copyTo is the path to the output file in the
702 // final directory.
Colin Cross1a527682019-09-23 15:55:30 -0700703 if len(shards) > 1 {
Colin Crossf1885962020-11-20 15:28:30 -0800704 shardFile := android.GenPathWithExt(ctx, genSubDir, in, String(properties.Output_extension))
Colin Cross1a527682019-09-23 15:55:30 -0700705 copyTo = append(copyTo, outFile)
706 outFile = shardFile
707 }
708
709 outFiles = append(outFiles, outFile)
Colin Cross1a527682019-09-23 15:55:30 -0700710
Colin Crossf1885962020-11-20 15:28:30 -0800711 // pre-expand the command line to replace $in and $out with references to
712 // a single input and output file.
Colin Cross1a527682019-09-23 15:55:30 -0700713 command, err := android.Expand(rawCommand, func(name string) (string, error) {
714 switch name {
715 case "in":
716 return in.String(), nil
717 case "out":
Colin Crossf1a035e2020-11-16 17:32:30 -0800718 return rule.Command().PathForOutput(outFile), nil
Colin Cross3ea4eb82020-11-24 13:07:27 -0800719 case "depfile":
720 // Generate a depfile for each output file. Store the list for
721 // later in order to combine them all into a single depfile.
Colin Crossf1a035e2020-11-16 17:32:30 -0800722 depFile := rule.Command().PathForOutput(outFile.ReplaceExtension(ctx, "d"))
Colin Cross3ea4eb82020-11-24 13:07:27 -0800723 commandDepFiles = append(commandDepFiles, depFile)
724 return depFile, nil
Colin Cross1a527682019-09-23 15:55:30 -0700725 default:
726 return "$(" + name + ")", nil
727 }
728 })
729 if err != nil {
730 ctx.PropertyErrorf("cmd", err.Error())
731 }
732
733 // escape the command in case for example it contains '#', an odd number of '"', etc
734 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
735 commands = append(commands, command)
736 }
737 fullCommand := strings.Join(commands, " && ")
738
Colin Cross3ea4eb82020-11-24 13:07:27 -0800739 var outputDepfile android.WritablePath
740 var extraTools android.Paths
741 if len(commandDepFiles) > 0 {
742 // Each command wrote to a depfile, but ninja can only handle one
743 // depfile per rule. Use the dep_fixer tool at the end of the
744 // command to combine all the depfiles into a single output depfile.
745 outputDepfile = android.PathForModuleGen(ctx, genSubDir, "gensrcs.d")
746 depFixerTool := ctx.Config().HostToolPath(ctx, "dep_fixer")
747 fullCommand += fmt.Sprintf(" && %s -o $(depfile) %s",
Colin Crossd11cf622021-03-23 22:30:35 -0700748 rule.Command().PathForTool(depFixerTool),
Colin Crossba9e4032020-11-24 16:32:22 -0800749 strings.Join(commandDepFiles, " "))
Colin Cross3ea4eb82020-11-24 13:07:27 -0800750 extraTools = append(extraTools, depFixerTool)
751 }
752
Colin Cross1a527682019-09-23 15:55:30 -0700753 generateTasks = append(generateTasks, generateTask{
Colin Cross3ea4eb82020-11-24 13:07:27 -0800754 in: shard,
755 out: outFiles,
756 depFile: outputDepfile,
757 copyTo: copyTo,
758 genDir: genDir,
759 cmd: fullCommand,
760 shard: i,
761 shards: len(shards),
762 extraTools: extraTools,
Colin Cross1a527682019-09-23 15:55:30 -0700763 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800764 }
Colin Cross1a527682019-09-23 15:55:30 -0700765
766 return generateTasks
Colin Crossd350ecd2015-04-28 13:25:36 -0700767 }
768
Colin Cross1a527682019-09-23 15:55:30 -0700769 g := generatorFactory(taskGenerator, properties)
Colin Crossf1885962020-11-20 15:28:30 -0800770 g.subDir = finalSubDir
Colin Cross1a527682019-09-23 15:55:30 -0700771 return g
Colin Crossd350ecd2015-04-28 13:25:36 -0700772}
773
Colin Cross54190b32017-10-09 15:34:10 -0700774func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700775 m := NewGenSrcs()
776 android.InitAndroidModule(m)
777 return m
778}
779
Colin Crossd350ecd2015-04-28 13:25:36 -0700780type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700781 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800782 Output_extension *string
Colin Cross1a527682019-09-23 15:55:30 -0700783
784 // maximum number of files that will be passed on a single command line.
785 Shard_size *int64
Colin Cross5049f022015-03-18 13:28:46 -0700786}
787
Evgenii Stepanovf47c90d2020-12-02 18:55:09 -0800788const defaultShardSize = 50
Colin Cross1a527682019-09-23 15:55:30 -0700789
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700790func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700791 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700792
Colin Cross1a527682019-09-23 15:55:30 -0700793 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700794 outs := make(android.WritablePaths, len(properties.Out))
Colin Cross3d680512020-11-13 16:23:53 -0800795 var depFile android.WritablePath
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700796 for i, out := range properties.Out {
Colin Cross3d680512020-11-13 16:23:53 -0800797 outPath := android.PathForModuleGen(ctx, out)
798 if i == 0 {
799 depFile = outPath.ReplaceExtension(ctx, "d")
800 }
801 outs[i] = outPath
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700802 }
Colin Cross1a527682019-09-23 15:55:30 -0700803 return []generateTask{{
Colin Cross3d680512020-11-13 16:23:53 -0800804 in: srcFiles,
805 out: outs,
806 depFile: depFile,
807 genDir: android.PathForModuleGen(ctx),
808 cmd: rawCommand,
Colin Cross1a527682019-09-23 15:55:30 -0700809 }}
Colin Cross5049f022015-03-18 13:28:46 -0700810 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700811
Jeff Gaston437d23c2017-11-08 12:38:00 -0800812 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700813}
814
Colin Cross54190b32017-10-09 15:34:10 -0700815func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700816 m := NewGenRule()
817 android.InitAndroidModule(m)
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800818 android.InitDefaultableModule(m)
Liz Kammerea6666f2021-02-17 10:17:28 -0500819 android.InitBazelModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700820 return m
821}
822
Colin Crossd350ecd2015-04-28 13:25:36 -0700823type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700824 // names of the output files that will be generated
Colin Crossef354482018-10-23 11:27:50 -0700825 Out []string `android:"arch_variant"`
Colin Cross5049f022015-03-18 13:28:46 -0700826}
Nan Zhangea568a42017-11-08 21:20:04 -0800827
Jingwen Chen316e07c2020-12-14 09:09:52 -0500828type bazelGenruleAttributes struct {
Jingwen Chen07027912021-03-15 06:02:43 -0400829 Srcs bazel.LabelListAttribute
Jingwen Chen316e07c2020-12-14 09:09:52 -0500830 Outs []string
Jingwen Chen07027912021-03-15 06:02:43 -0400831 Tools bazel.LabelListAttribute
Jingwen Chen316e07c2020-12-14 09:09:52 -0500832 Cmd string
833}
834
Wei Libcd39942021-09-16 23:57:28 +0000835// CcGenruleBp2Build is for cc_genrule.
836func CcGenruleBp2Build(ctx android.TopDownMutatorContext) {
837 m, ok := ctx.Module().(*Module)
838 if !ok || !m.ConvertWithBp2build(ctx) {
839 return
840 }
841
842 if ctx.ModuleType() != "cc_genrule" {
843 // Not a cc_genrule.
844 return
845 }
846
847 genruleBp2Build(ctx)
848}
849
850// GenruleBp2Build is used for genrule.
Jingwen Chena42d6412021-01-26 21:57:27 -0500851func GenruleBp2Build(ctx android.TopDownMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500852 m, ok := ctx.Module().(*Module)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500853 if !ok || !m.ConvertWithBp2build(ctx) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500854 return
Jingwen Chen316e07c2020-12-14 09:09:52 -0500855 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500856
Jingwen Chen66480452021-03-18 03:41:55 -0400857 if ctx.ModuleType() != "genrule" {
Wei Libcd39942021-09-16 23:57:28 +0000858 // Not a regular genrule.
Jingwen Chen66480452021-03-18 03:41:55 -0400859 return
860 }
861
Wei Libcd39942021-09-16 23:57:28 +0000862 genruleBp2Build(ctx)
863}
864
865func genruleBp2Build(ctx android.TopDownMutatorContext) {
866 m, _ := ctx.Module().(*Module)
Liz Kammer356f7d42021-01-26 09:18:53 -0500867 // Bazel only has the "tools" attribute.
Jingwen Chen07027912021-03-15 06:02:43 -0400868 tools_prop := android.BazelLabelForModuleDeps(ctx, m.properties.Tools)
869 tool_files_prop := android.BazelLabelForModuleSrc(ctx, m.properties.Tool_files)
870 tools_prop.Append(tool_files_prop)
Liz Kammer356f7d42021-01-26 09:18:53 -0500871
Jingwen Chen07027912021-03-15 06:02:43 -0400872 tools := bazel.MakeLabelListAttribute(tools_prop)
873 srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Srcs))
Liz Kammer356f7d42021-01-26 09:18:53 -0500874
875 var allReplacements bazel.LabelList
Jingwen Chen07027912021-03-15 06:02:43 -0400876 allReplacements.Append(tools.Value)
877 allReplacements.Append(srcs.Value)
Liz Kammer356f7d42021-01-26 09:18:53 -0500878
879 // Replace in and out variables with $< and $@
880 var cmd string
881 if m.properties.Cmd != nil {
882 cmd = strings.Replace(*m.properties.Cmd, "$(in)", "$(SRCS)", -1)
883 cmd = strings.Replace(cmd, "$(out)", "$(OUTS)", -1)
Wei Libcd39942021-09-16 23:57:28 +0000884 genDir := "$(GENDIR)"
885 if ctx.ModuleType() == "cc_genrule" {
886 genDir = "$(RULEDIR)"
887 }
888 cmd = strings.Replace(cmd, "$(genDir)", genDir, -1)
Jingwen Chen07027912021-03-15 06:02:43 -0400889 if len(tools.Value.Includes) > 0 {
890 cmd = strings.Replace(cmd, "$(location)", fmt.Sprintf("$(location %s)", tools.Value.Includes[0].Label), -1)
891 cmd = strings.Replace(cmd, "$(locations)", fmt.Sprintf("$(locations %s)", tools.Value.Includes[0].Label), -1)
Liz Kammer356f7d42021-01-26 09:18:53 -0500892 }
893 for _, l := range allReplacements.Includes {
Jingwen Chen38e62642021-04-19 05:00:15 +0000894 bpLoc := fmt.Sprintf("$(location %s)", l.OriginalModuleName)
895 bpLocs := fmt.Sprintf("$(locations %s)", l.OriginalModuleName)
Liz Kammer356f7d42021-01-26 09:18:53 -0500896 bazelLoc := fmt.Sprintf("$(location %s)", l.Label)
897 bazelLocs := fmt.Sprintf("$(locations %s)", l.Label)
898 cmd = strings.Replace(cmd, bpLoc, bazelLoc, -1)
899 cmd = strings.Replace(cmd, bpLocs, bazelLocs, -1)
900 }
901 }
902
903 // The Out prop is not in an immediately accessible field
904 // in the Module struct, so use GetProperties and cast it
905 // to the known struct prop.
906 var outs []string
907 for _, propIntf := range m.GetProperties() {
908 if props, ok := propIntf.(*genRuleProperties); ok {
909 outs = props.Out
910 break
911 }
912 }
913
Jingwen Chen1fd14692021-02-05 03:01:50 -0500914 attrs := &bazelGenruleAttributes{
Liz Kammer356f7d42021-01-26 09:18:53 -0500915 Srcs: srcs,
916 Outs: outs,
917 Cmd: cmd,
918 Tools: tools,
Jingwen Chen1fd14692021-02-05 03:01:50 -0500919 }
920
Liz Kammerfc46bc12021-02-19 11:06:17 -0500921 props := bazel.BazelTargetModuleProperties{
922 Rule_class: "genrule",
923 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500924
925 // Create the BazelTargetModule.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000926 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
Jingwen Chen316e07c2020-12-14 09:09:52 -0500927}
928
Nan Zhangea568a42017-11-08 21:20:04 -0800929var Bool = proptools.Bool
930var String = proptools.String
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800931
932//
933// Defaults
934//
935type Defaults struct {
936 android.ModuleBase
937 android.DefaultsModuleBase
938}
939
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800940func defaultsFactory() android.Module {
941 return DefaultsFactory()
942}
943
944func DefaultsFactory(props ...interface{}) android.Module {
945 module := &Defaults{}
946
947 module.AddProperties(props...)
948 module.AddProperties(
949 &generatorProperties{},
950 &genRuleProperties{},
951 )
952
953 android.InitDefaultsModule(module)
954
955 return module
956}