blob: 50262d2e437c3cbd8a9c28f7031f38c289cefe86 [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
Chris Parsonsf874e462022-05-10 13:50:12 -040028 "android/soong/bazel/cquery"
Jihoon Kangc170af42022-08-20 05:26:38 +000029
Colin Cross70b40592015-03-23 12:57:34 -070030 "github.com/google/blueprint"
Dan Willemsen8eded0a2017-09-13 16:07:44 -070031 "github.com/google/blueprint/bootstrap"
Nan Zhangea568a42017-11-08 21:20:04 -080032 "github.com/google/blueprint/proptools"
Colin Cross5049f022015-03-18 13:28:46 -070033
Colin Cross635c3b02016-05-18 15:37:25 -070034 "android/soong/android"
Jingwen Chen30f5aaa2020-11-19 05:38:02 -050035 "android/soong/bazel"
Colin Cross5049f022015-03-18 13:28:46 -070036)
37
Colin Cross463a90e2015-06-17 14:20:06 -070038func init() {
Colin Crosse9fe2942020-11-10 18:12:15 -080039 RegisterGenruleBuildComponents(android.InitRegistrationContext)
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000040}
Jaewoong Jung98716bd2018-12-10 08:13:18 -080041
Paul Duffin672cb9f2021-03-03 02:30:37 +000042// Test fixture preparer that will register most genrule build components.
43//
44// Singletons and mutators should only be added here if they are needed for a majority of genrule
45// module types, otherwise they should be added under a separate preparer to allow them to be
46// selected only when needed to reduce test execution time.
47//
48// Module types do not have much of an overhead unless they are used so this should include as many
49// module types as possible. The exceptions are those module types that require mutators and/or
50// singletons in order to function in which case they should be kept together in a separate
51// preparer.
52var PrepareForTestWithGenRuleBuildComponents = android.GroupFixturePreparers(
53 android.FixtureRegisterWithContext(RegisterGenruleBuildComponents),
54)
55
56// Prepare a fixture to use all genrule module types, mutators and singletons fully.
57//
58// This should only be used by tests that want to run with as much of the build enabled as possible.
59var PrepareForIntegrationTestWithGenrule = android.GroupFixturePreparers(
60 PrepareForTestWithGenRuleBuildComponents,
61)
62
Colin Crosse9fe2942020-11-10 18:12:15 -080063func RegisterGenruleBuildComponents(ctx android.RegistrationContext) {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000064 ctx.RegisterModuleType("genrule_defaults", defaultsFactory)
65
66 ctx.RegisterModuleType("gensrcs", GenSrcsFactory)
67 ctx.RegisterModuleType("genrule", GenRuleFactory)
68
69 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
70 ctx.BottomUp("genrule_tool_deps", toolDepsMutator).Parallel()
71 })
Liz Kammer356f7d42021-01-26 09:18:53 -050072}
73
Colin Cross5049f022015-03-18 13:28:46 -070074var (
Colin Cross635c3b02016-05-18 15:37:25 -070075 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross1a527682019-09-23 15:55:30 -070076
Alex Humesky29e3bbe2020-11-20 21:30:13 -050077 // Used by gensrcs when there is more than 1 shard to merge the outputs
78 // of each shard into a zip file.
Colin Cross1a527682019-09-23 15:55:30 -070079 gensrcsMerge = pctx.AndroidStaticRule("gensrcsMerge", blueprint.RuleParams{
80 Command: "${soongZip} -o ${tmpZip} @${tmpZip}.rsp && ${zipSync} -d ${genDir} ${tmpZip}",
81 CommandDeps: []string{"${soongZip}", "${zipSync}"},
82 Rspfile: "${tmpZip}.rsp",
83 RspfileContent: "${zipArgs}",
84 }, "tmpZip", "genDir", "zipArgs")
Colin Cross5049f022015-03-18 13:28:46 -070085)
86
Jeff Gastonefc1b412017-03-29 17:29:06 -070087func init() {
Dan Willemsenddf504c2019-08-09 16:21:29 -070088 pctx.Import("android/soong/android")
Colin Cross1a527682019-09-23 15:55:30 -070089
90 pctx.HostBinToolVariable("soongZip", "soong_zip")
91 pctx.HostBinToolVariable("zipSync", "zipsync")
Jeff Gastonefc1b412017-03-29 17:29:06 -070092}
93
Colin Cross5049f022015-03-18 13:28:46 -070094type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070095 GeneratedSourceFiles() android.Paths
Colin Cross5ed99c62016-11-22 12:55:55 -080096 GeneratedHeaderDirs() android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -080097 GeneratedDeps() android.Paths
Colin Cross5049f022015-03-18 13:28:46 -070098}
99
Colin Crossfe17f6f2019-03-28 19:30:56 -0700100// Alias for android.HostToolProvider
101// Deprecated: use android.HostToolProvider instead.
Colin Crossd350ecd2015-04-28 13:25:36 -0700102type HostToolProvider interface {
Colin Crossfe17f6f2019-03-28 19:30:56 -0700103 android.HostToolProvider
Colin Crossd350ecd2015-04-28 13:25:36 -0700104}
Colin Cross5049f022015-03-18 13:28:46 -0700105
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700106type hostToolDependencyTag struct {
107 blueprint.BaseDependencyTag
Colin Cross65cb3142021-12-10 23:05:02 +0000108 android.LicenseAnnotationToolchainDependencyTag
Colin Cross08f15ab2018-10-04 23:29:14 -0700109 label string
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700110}
Martin Stjernholmdbd814d2022-01-12 23:18:30 +0000111
112func (t hostToolDependencyTag) AllowDisabledModuleDependency(target android.Module) bool {
113 // Allow depending on a disabled module if it's replaced by a prebuilt
114 // counterpart. We get the prebuilt through android.PrebuiltGetPreferred in
115 // GenerateAndroidBuildActions.
116 return target.IsReplacedByPrebuilt()
117}
118
119var _ android.AllowDisabledModuleDependency = (*hostToolDependencyTag)(nil)
120
Colin Cross7d5136f2015-05-11 13:39:40 -0700121type generatorProperties struct {
Spandan Das93e95992021-07-29 18:26:39 +0000122 // The command to run on one or more input files. Cmd supports substitution of a few variables.
Jeff Gastonefc1b412017-03-29 17:29:06 -0700123 //
124 // Available variables for substitution:
125 //
Spandan Das93e95992021-07-29 18:26:39 +0000126 // $(location): the path to the first entry in tools or tool_files.
127 // $(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.
128 // $(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.
129 // $(in): one or more input files.
130 // $(out): a single output file.
131 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true.
132 // $(genDir): the sandbox directory for this tool; contains $(out).
Colin Cross2296f5b2017-10-17 21:38:14 -0700133 // $$: a literal $
Nan Zhangea568a42017-11-08 21:20:04 -0800134 Cmd *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700135
Colin Cross33bfb0a2016-11-21 17:23:08 -0800136 // Enable reading a file containing dependencies in gcc format after the command completes
Nan Zhangea568a42017-11-08 21:20:04 -0800137 Depfile *bool
Colin Cross33bfb0a2016-11-21 17:23:08 -0800138
Colin Cross6f080df2016-11-04 15:32:58 -0700139 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -0700140 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -0700141 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700142
143 // Local file that is used as the tool
Colin Cross27b922f2019-03-04 22:35:41 -0800144 Tool_files []string `android:"path"`
Colin Cross5ed99c62016-11-22 12:55:55 -0800145
146 // List of directories to export generated headers from
147 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -0800148
149 // list of input files
Colin Cross27b922f2019-03-04 22:35:41 -0800150 Srcs []string `android:"path,arch_variant"`
Dan Willemseneefa0262018-11-17 14:01:18 -0800151
152 // input files to exclude
Colin Cross27b922f2019-03-04 22:35:41 -0800153 Exclude_srcs []string `android:"path,arch_variant"`
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400154}
Alex Humesky29e3bbe2020-11-20 21:30:13 -0500155
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700156type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700157 android.ModuleBase
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800158 android.DefaultableModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500159 android.BazelModuleBase
Jiyong Parkfc752ca2019-06-12 13:27:29 +0900160 android.ApexModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -0700161
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700162 // For other packages to make their own genrules with extra
163 // properties
164 Extra interface{}
Colin Crossf3bfd022021-09-27 15:15:06 -0700165
166 // CmdModifier can be set by wrappers around genrule to modify the command, for example to
167 // prefix environment variables to it.
168 CmdModifier func(ctx android.ModuleContext, cmd string) string
169
Colin Cross7228ecd2019-11-18 16:00:16 -0800170 android.ImageInterface
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700171
Colin Cross7d5136f2015-05-11 13:39:40 -0700172 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700173
Alex Humesky29e3bbe2020-11-20 21:30:13 -0500174 // For the different tasks that genrule and gensrc generate. genrule will
175 // generate 1 task, and gensrc will generate 1 or more tasks based on the
176 // number of shards the input files are sharded into.
Jeff Gaston437d23c2017-11-08 12:38:00 -0800177 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700178
Colin Cross1a527682019-09-23 15:55:30 -0700179 rule blueprint.Rule
180 rawCommands []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700181
Colin Cross5ed99c62016-11-22 12:55:55 -0800182 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700183
Colin Cross635c3b02016-05-18 15:37:25 -0700184 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800185 outputDeps android.Paths
Colin Crossa4ad2b02019-03-18 22:15:32 -0700186
187 subName string
Colin Cross1a527682019-09-23 15:55:30 -0700188 subDir string
bralee1fbf4402020-05-21 10:11:59 +0800189
190 // Collect the module directory for IDE info in java/jdeps.go.
191 modulePaths []string
Colin Crossd350ecd2015-04-28 13:25:36 -0700192}
193
Chris Parsonsf874e462022-05-10 13:50:12 -0400194var _ android.MixedBuildBuildable = (*Module)(nil)
195
Colin Cross1a527682019-09-23 15:55:30 -0700196type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700197
198type generateTask struct {
Colin Cross3ea4eb82020-11-24 13:07:27 -0800199 in android.Paths
200 out android.WritablePaths
201 depFile android.WritablePath
Alex Humesky29e3bbe2020-11-20 21:30:13 -0500202 copyTo android.WritablePaths // For gensrcs to set on gensrcsMerge rule.
Colin Cross3ea4eb82020-11-24 13:07:27 -0800203 genDir android.WritablePath
204 extraTools android.Paths // dependencies on tools used by the generator
205
Alex Humesky29e3bbe2020-11-20 21:30:13 -0500206 cmd string
207 // For gensrsc sharding.
Colin Cross3ea4eb82020-11-24 13:07:27 -0800208 shard int
209 shards int
Colin Crossd350ecd2015-04-28 13:25:36 -0700210}
211
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700212func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700213 return g.outputFiles
214}
215
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700216func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700217 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800218}
219
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700220func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800221 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700222}
223
Dan Willemsen9da9d492018-02-21 18:28:18 -0800224func (g *Module) GeneratedDeps() android.Paths {
225 return g.outputDeps
226}
227
Jooyung Han8c7e3ed2021-06-28 17:35:58 +0900228func (g *Module) OutputFiles(tag string) (android.Paths, error) {
229 if tag == "" {
230 return append(android.Paths{}, g.outputFiles...), nil
231 }
232 // otherwise, tag should match one of outputs
233 for _, outputFile := range g.outputFiles {
234 if outputFile.Rel() == tag {
235 return android.Paths{outputFile}, nil
236 }
237 }
238 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
239}
240
241var _ android.SourceFileProducer = (*Module)(nil)
242var _ android.OutputFileProducer = (*Module)(nil)
243
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000244func toolDepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700245 if g, ok := ctx.Module().(*Module); ok {
Colin Cross08f15ab2018-10-04 23:29:14 -0700246 for _, tool := range g.properties.Tools {
247 tag := hostToolDependencyTag{label: tool}
248 if m := android.SrcIsModule(tool); m != "" {
249 tool = m
250 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700251 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700252 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700253 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700254}
255
Chris Parsonsf874e462022-05-10 13:50:12 -0400256func (g *Module) ProcessBazelQueryResponse(ctx android.ModuleContext) {
257 g.generateCommonBuildActions(ctx)
258
259 label := g.GetBazelLabel(ctx, g)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400260 bazelCtx := ctx.Config().BazelContext
Chris Parsonsf874e462022-05-10 13:50:12 -0400261 filePaths, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx))
262 if err != nil {
263 ctx.ModuleErrorf(err.Error())
264 return
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400265 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400266
267 var bazelOutputFiles android.Paths
268 exportIncludeDirs := map[string]bool{}
269 for _, bazelOutputFile := range filePaths {
Cole Faust01243362022-06-02 12:11:12 -0700270 bazelOutputFiles = append(bazelOutputFiles, android.PathForBazelOutRelative(ctx, ctx.ModuleDir(), bazelOutputFile))
Chris Parsonsf874e462022-05-10 13:50:12 -0400271 exportIncludeDirs[filepath.Dir(bazelOutputFile)] = true
272 }
273 g.outputFiles = bazelOutputFiles
274 g.outputDeps = bazelOutputFiles
275 for includePath, _ := range exportIncludeDirs {
276 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForBazelOut(ctx, includePath))
277 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400278}
Colin Crossf1885962020-11-20 15:28:30 -0800279
Chris Parsonsf874e462022-05-10 13:50:12 -0400280// generateCommonBuildActions contains build action generation logic
281// common to both the mixed build case and the legacy case of genrule processing.
282// To fully support genrule in mixed builds, the contents of this function should
283// approach zero; there should be no genrule action registration done directly
284// by Soong logic in the mixed-build case.
285func (g *Module) generateCommonBuildActions(ctx android.ModuleContext) {
Colin Crossa4ad2b02019-03-18 22:15:32 -0700286 g.subName = ctx.ModuleSubDir()
287
bralee1fbf4402020-05-21 10:11:59 +0800288 // Collect the module directory for IDE info in java/jdeps.go.
289 g.modulePaths = append(g.modulePaths, ctx.ModuleDir())
290
Colin Cross5ed99c62016-11-22 12:55:55 -0800291 if len(g.properties.Export_include_dirs) > 0 {
292 for _, dir := range g.properties.Export_include_dirs {
293 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
Colin Cross1a527682019-09-23 15:55:30 -0700294 android.PathForModuleGen(ctx, g.subDir, ctx.ModuleDir(), dir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800295 }
296 } else {
Colin Cross1a527682019-09-23 15:55:30 -0700297 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, g.subDir))
Colin Cross5ed99c62016-11-22 12:55:55 -0800298 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700299
Colin Crossd11cf622021-03-23 22:30:35 -0700300 locationLabels := map[string]location{}
Colin Cross08f15ab2018-10-04 23:29:14 -0700301 firstLabel := ""
302
Colin Crossd11cf622021-03-23 22:30:35 -0700303 addLocationLabel := func(label string, loc location) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700304 if firstLabel == "" {
305 firstLabel = label
306 }
307 if _, exists := locationLabels[label]; !exists {
Colin Crossd11cf622021-03-23 22:30:35 -0700308 locationLabels[label] = loc
Colin Cross08f15ab2018-10-04 23:29:14 -0700309 } else {
Anton Hansson7cd41e52021-10-08 16:13:10 +0100310 ctx.ModuleErrorf("multiple locations for label %q: %q and %q (do you have duplicate srcs entries?)",
Colin Crossd11cf622021-03-23 22:30:35 -0700311 label, locationLabels[label], loc)
Colin Cross08f15ab2018-10-04 23:29:14 -0700312 }
313 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700314
Colin Crossba9e4032020-11-24 16:32:22 -0800315 var tools android.Paths
316 var packagedTools []android.PackagingSpec
Colin Cross6f080df2016-11-04 15:32:58 -0700317 if len(g.properties.Tools) > 0 {
Colin Crossba71a3f2019-03-18 12:12:48 -0700318 seenTools := make(map[string]bool)
319
Colin Cross35143d02017-11-16 00:11:20 -0800320 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700321 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
322 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700323 tool := ctx.OtherModuleName(module)
Martin Stjernholmdbd814d2022-01-12 23:18:30 +0000324 if m, ok := module.(android.Module); ok {
325 // Necessary to retrieve any prebuilt replacement for the tool, since
326 // toolDepsMutator runs too late for the prebuilt mutators to have
327 // replaced the dependency.
328 module = android.PrebuiltGetPreferred(ctx, m)
329 }
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700330
Colin Crossba9e4032020-11-24 16:32:22 -0800331 switch t := module.(type) {
332 case android.HostToolProvider:
333 // A HostToolProvider provides the path to a tool, which will be copied
334 // into the sandbox.
Colin Cross35143d02017-11-16 00:11:20 -0800335 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800336 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800337 ctx.AddMissingDependencies([]string{tool})
338 } else {
339 ctx.ModuleErrorf("depends on disabled module %q", tool)
340 }
Colin Crossba9e4032020-11-24 16:32:22 -0800341 return
Colin Cross35143d02017-11-16 00:11:20 -0800342 }
Colin Crossba9e4032020-11-24 16:32:22 -0800343 path := t.HostToolPath()
344 if !path.Valid() {
345 ctx.ModuleErrorf("host tool %q missing output file", tool)
346 return
347 }
348 if specs := t.TransitivePackagingSpecs(); specs != nil {
349 // If the HostToolProvider has PackgingSpecs, which are definitions of the
350 // required relative locations of the tool and its dependencies, use those
351 // instead. They will be copied to those relative locations in the sbox
352 // sandbox.
353 packagedTools = append(packagedTools, specs...)
354 // Assume that the first PackagingSpec of the module is the tool.
Colin Crossd11cf622021-03-23 22:30:35 -0700355 addLocationLabel(tag.label, packagedToolLocation{specs[0]})
Colin Crossba9e4032020-11-24 16:32:22 -0800356 } else {
357 tools = append(tools, path.Path())
Colin Crossd11cf622021-03-23 22:30:35 -0700358 addLocationLabel(tag.label, toolLocation{android.Paths{path.Path()}})
Colin Crossba9e4032020-11-24 16:32:22 -0800359 }
360 case bootstrap.GoBinaryTool:
361 // A GoBinaryTool provides the install path to a tool, which will be copied.
Colin Crossa44551f2021-10-25 15:36:21 -0700362 p := android.PathForGoBinary(ctx, t)
363 tools = append(tools, p)
364 addLocationLabel(tag.label, toolLocation{android.Paths{p}})
Colin Crossba9e4032020-11-24 16:32:22 -0800365 default:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700366 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Colin Crossba9e4032020-11-24 16:32:22 -0800367 return
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700368 }
369
Colin Crossba9e4032020-11-24 16:32:22 -0800370 seenTools[tag.label] = true
Colin Crossd350ecd2015-04-28 13:25:36 -0700371 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700372 })
Colin Crossba71a3f2019-03-18 12:12:48 -0700373
374 // If AllowMissingDependencies is enabled, the build will not have stopped when
375 // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical
Liz Kammer20ebfb42020-07-28 11:32:07 -0700376 // "cmd: unknown location label ..." errors later. Add a placeholder file to the local label.
377 // The command that uses this placeholder file will never be executed because the rule will be
378 // replaced with an android.Error rule reporting the missing dependencies.
Colin Crossba71a3f2019-03-18 12:12:48 -0700379 if ctx.Config().AllowMissingDependencies() {
380 for _, tool := range g.properties.Tools {
381 if !seenTools[tool] {
Colin Crossd11cf622021-03-23 22:30:35 -0700382 addLocationLabel(tool, errorLocation{"***missing tool " + tool + "***"})
Colin Crossba71a3f2019-03-18 12:12:48 -0700383 }
384 }
385 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700386 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700387
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700388 if ctx.Failed() {
389 return
390 }
391
Colin Cross08f15ab2018-10-04 23:29:14 -0700392 for _, toolFile := range g.properties.Tool_files {
Colin Cross8a497952019-03-05 22:25:09 -0800393 paths := android.PathsForModuleSrc(ctx, []string{toolFile})
Colin Crossba9e4032020-11-24 16:32:22 -0800394 tools = append(tools, paths...)
Colin Crossd11cf622021-03-23 22:30:35 -0700395 addLocationLabel(toolFile, toolLocation{paths})
Colin Cross08f15ab2018-10-04 23:29:14 -0700396 }
397
Liz Kammer619be462022-01-28 15:13:39 -0500398 includeDirInPaths := ctx.DeviceConfig().BuildBrokenInputDir(g.Name())
Colin Cross08f15ab2018-10-04 23:29:14 -0700399 var srcFiles android.Paths
400 for _, in := range g.properties.Srcs {
Liz Kammer619be462022-01-28 15:13:39 -0500401 paths, missingDeps := android.PathsAndMissingDepsRelativeToModuleSourceDir(android.SourceInput{
402 Context: ctx, Paths: []string{in}, ExcludePaths: g.properties.Exclude_srcs, IncludeDirs: includeDirInPaths,
403 })
Colin Crossba71a3f2019-03-18 12:12:48 -0700404 if len(missingDeps) > 0 {
405 if !ctx.Config().AllowMissingDependencies() {
406 panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator",
407 missingDeps))
408 }
409
410 // If AllowMissingDependencies is enabled, the build will not have stopped when
411 // the dependency was added on a missing SourceFileProducer module, which will result in nonsensical
Liz Kammer20ebfb42020-07-28 11:32:07 -0700412 // "cmd: label ":..." has no files" errors later. Add a placeholder file to the local label.
413 // The command that uses this placeholder file will never be executed because the rule will be
414 // replaced with an android.Error rule reporting the missing dependencies.
Colin Crossba71a3f2019-03-18 12:12:48 -0700415 ctx.AddMissingDependencies(missingDeps)
Colin Crossd11cf622021-03-23 22:30:35 -0700416 addLocationLabel(in, errorLocation{"***missing srcs " + in + "***"})
Colin Crossba71a3f2019-03-18 12:12:48 -0700417 } else {
418 srcFiles = append(srcFiles, paths...)
Colin Crossd11cf622021-03-23 22:30:35 -0700419 addLocationLabel(in, inputLocation{paths})
Colin Crossba71a3f2019-03-18 12:12:48 -0700420 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700421 }
422
Colin Cross1a527682019-09-23 15:55:30 -0700423 var copyFrom android.Paths
424 var outputFiles android.WritablePaths
425 var zipArgs strings.Builder
Colin Cross08f15ab2018-10-04 23:29:14 -0700426
Colin Crossf3bfd022021-09-27 15:15:06 -0700427 cmd := String(g.properties.Cmd)
428 if g.CmdModifier != nil {
429 cmd = g.CmdModifier(ctx, cmd)
430 }
431
Alex Humesky29e3bbe2020-11-20 21:30:13 -0500432 // Generate tasks, either from genrule or gensrcs.
Colin Crossf3bfd022021-09-27 15:15:06 -0700433 for _, task := range g.taskGenerator(ctx, cmd, srcFiles) {
Colin Cross3d680512020-11-13 16:23:53 -0800434 if len(task.out) == 0 {
435 ctx.ModuleErrorf("must have at least one output file")
436 return
Colin Cross85a2e892018-07-09 09:45:06 -0700437 }
438
Colin Crossf1a035e2020-11-16 17:32:30 -0800439 // Pick a unique path outside the task.genDir for the sbox manifest textproto,
440 // a unique rule name, and the user-visible description.
441 manifestName := "genrule.sbox.textproto"
442 desc := "generate"
443 name := "generator"
444 if task.shards > 0 {
445 manifestName = "genrule_" + strconv.Itoa(task.shard) + ".sbox.textproto"
446 desc += " " + strconv.Itoa(task.shard)
447 name += strconv.Itoa(task.shard)
448 } else if len(task.out) == 1 {
449 desc += " " + task.out[0].Base()
450 }
451
452 manifestPath := android.PathForModuleOut(ctx, manifestName)
453
454 // Use a RuleBuilder to create a rule that runs the command inside an sbox sandbox.
Yu Liu6a7940c2023-05-09 17:12:22 -0700455 rule := getSandboxedRuleBuilder(ctx, android.NewRuleBuilder(pctx, ctx).Sbox(task.genDir, manifestPath))
Colin Crossf1a035e2020-11-16 17:32:30 -0800456 cmd := rule.Command()
457
Colin Cross3d680512020-11-13 16:23:53 -0800458 for _, out := range task.out {
Colin Crossd11cf622021-03-23 22:30:35 -0700459 addLocationLabel(out.Rel(), outputLocation{out})
Colin Cross3d680512020-11-13 16:23:53 -0800460 }
461
Colin Cross1a527682019-09-23 15:55:30 -0700462 referencedDepfile := false
463
Colin Cross3d680512020-11-13 16:23:53 -0800464 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross1a527682019-09-23 15:55:30 -0700465 // report the error directly without returning an error to android.Expand to catch multiple errors in a
466 // single run
Colin Cross3d680512020-11-13 16:23:53 -0800467 reportError := func(fmt string, args ...interface{}) (string, error) {
Colin Cross1a527682019-09-23 15:55:30 -0700468 ctx.PropertyErrorf("cmd", fmt, args...)
Colin Cross3d680512020-11-13 16:23:53 -0800469 return "SOONG_ERROR", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700470 }
Colin Cross1a527682019-09-23 15:55:30 -0700471
Jihoon Kangc170af42022-08-20 05:26:38 +0000472 // Apply shell escape to each cases to prevent source file paths containing $ from being evaluated in shell
Colin Cross1a527682019-09-23 15:55:30 -0700473 switch name {
474 case "location":
475 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
476 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700477 }
Colin Crossd11cf622021-03-23 22:30:35 -0700478 loc := locationLabels[firstLabel]
479 paths := loc.Paths(cmd)
Colin Cross1a527682019-09-23 15:55:30 -0700480 if len(paths) == 0 {
481 return reportError("default label %q has no files", firstLabel)
482 } else if len(paths) > 1 {
483 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
484 firstLabel, firstLabel)
Colin Cross08f15ab2018-10-04 23:29:14 -0700485 }
Jihoon Kangc170af42022-08-20 05:26:38 +0000486 return proptools.ShellEscape(paths[0]), nil
Colin Cross1a527682019-09-23 15:55:30 -0700487 case "in":
Jihoon Kangc170af42022-08-20 05:26:38 +0000488 return strings.Join(proptools.ShellEscapeList(cmd.PathsForInputs(srcFiles)), " "), nil
Colin Cross1a527682019-09-23 15:55:30 -0700489 case "out":
Colin Cross3d680512020-11-13 16:23:53 -0800490 var sandboxOuts []string
491 for _, out := range task.out {
Colin Crossf1a035e2020-11-16 17:32:30 -0800492 sandboxOuts = append(sandboxOuts, cmd.PathForOutput(out))
Colin Cross3d680512020-11-13 16:23:53 -0800493 }
Jihoon Kangc170af42022-08-20 05:26:38 +0000494 return strings.Join(proptools.ShellEscapeList(sandboxOuts), " "), nil
Colin Cross1a527682019-09-23 15:55:30 -0700495 case "depfile":
496 referencedDepfile = true
497 if !Bool(g.properties.Depfile) {
498 return reportError("$(depfile) used without depfile property")
499 }
Colin Cross3d680512020-11-13 16:23:53 -0800500 return "__SBOX_DEPFILE__", nil
Colin Cross1a527682019-09-23 15:55:30 -0700501 case "genDir":
Jihoon Kangc170af42022-08-20 05:26:38 +0000502 return proptools.ShellEscape(cmd.PathForOutput(task.genDir)), nil
Colin Cross1a527682019-09-23 15:55:30 -0700503 default:
504 if strings.HasPrefix(name, "location ") {
505 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Colin Crossd11cf622021-03-23 22:30:35 -0700506 if loc, ok := locationLabels[label]; ok {
507 paths := loc.Paths(cmd)
Colin Cross1a527682019-09-23 15:55:30 -0700508 if len(paths) == 0 {
509 return reportError("label %q has no files", label)
510 } else if len(paths) > 1 {
511 return reportError("label %q has multiple files, use $(locations %s) to reference it",
512 label, label)
513 }
Jihoon Kangc170af42022-08-20 05:26:38 +0000514 return proptools.ShellEscape(paths[0]), nil
Colin Cross1a527682019-09-23 15:55:30 -0700515 } else {
Anton Hanssonbebf5262022-02-23 11:42:38 +0000516 return reportError("unknown location label %q is not in srcs, out, tools or tool_files.", label)
Colin Cross1a527682019-09-23 15:55:30 -0700517 }
518 } else if strings.HasPrefix(name, "locations ") {
519 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
Colin Crossd11cf622021-03-23 22:30:35 -0700520 if loc, ok := locationLabels[label]; ok {
521 paths := loc.Paths(cmd)
Colin Cross1a527682019-09-23 15:55:30 -0700522 if len(paths) == 0 {
523 return reportError("label %q has no files", label)
524 }
Jihoon Kangc170af42022-08-20 05:26:38 +0000525 return proptools.ShellEscape(strings.Join(paths, " ")), nil
Colin Cross1a527682019-09-23 15:55:30 -0700526 } else {
Anton Hanssonbebf5262022-02-23 11:42:38 +0000527 return reportError("unknown locations label %q is not in srcs, out, tools or tool_files.", label)
Colin Cross1a527682019-09-23 15:55:30 -0700528 }
529 } else {
530 return reportError("unknown variable '$(%s)'", name)
531 }
Colin Cross6f080df2016-11-04 15:32:58 -0700532 }
Colin Cross1a527682019-09-23 15:55:30 -0700533 })
534
535 if err != nil {
536 ctx.PropertyErrorf("cmd", "%s", err.Error())
537 return
Colin Cross6f080df2016-11-04 15:32:58 -0700538 }
Colin Cross6f080df2016-11-04 15:32:58 -0700539
Colin Cross1a527682019-09-23 15:55:30 -0700540 if Bool(g.properties.Depfile) && !referencedDepfile {
541 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
542 return
543 }
Colin Cross1a527682019-09-23 15:55:30 -0700544 g.rawCommands = append(g.rawCommands, rawCommand)
Bill Peckhamc087be12020-02-13 15:55:10 -0800545
Colin Cross3d680512020-11-13 16:23:53 -0800546 cmd.Text(rawCommand)
547 cmd.ImplicitOutputs(task.out)
548 cmd.Implicits(task.in)
Colin Crossba9e4032020-11-24 16:32:22 -0800549 cmd.ImplicitTools(tools)
550 cmd.ImplicitTools(task.extraTools)
551 cmd.ImplicitPackagedTools(packagedTools)
Colin Cross3d680512020-11-13 16:23:53 -0800552 if Bool(g.properties.Depfile) {
553 cmd.ImplicitDepFile(task.depFile)
554 }
555
556 // Create the rule to run the genrule command inside sbox.
Colin Crossf1a035e2020-11-16 17:32:30 -0800557 rule.Build(name, desc)
Colin Cross1a527682019-09-23 15:55:30 -0700558
559 if len(task.copyTo) > 0 {
Colin Cross3d680512020-11-13 16:23:53 -0800560 // If copyTo is set, multiple shards need to be copied into a single directory.
561 // task.out contains the per-shard paths, and copyTo contains the corresponding
562 // final path. The files need to be copied into the final directory by a
563 // single rule so it can remove the directory before it starts to ensure no
564 // old files remain. zipsync already does this, so build up zipArgs that
565 // zip all the per-shard directories into a single zip.
Colin Cross1a527682019-09-23 15:55:30 -0700566 outputFiles = append(outputFiles, task.copyTo...)
567 copyFrom = append(copyFrom, task.out.Paths()...)
568 zipArgs.WriteString(" -C " + task.genDir.String())
569 zipArgs.WriteString(android.JoinWithPrefix(task.out.Strings(), " -f "))
570 } else {
571 outputFiles = append(outputFiles, task.out...)
572 }
Colin Cross6f080df2016-11-04 15:32:58 -0700573 }
574
Colin Cross1a527682019-09-23 15:55:30 -0700575 if len(copyFrom) > 0 {
Colin Cross3d680512020-11-13 16:23:53 -0800576 // Create a rule that zips all the per-shard directories into a single zip and then
577 // uses zipsync to unzip it into the final directory.
Colin Cross1a527682019-09-23 15:55:30 -0700578 ctx.Build(pctx, android.BuildParams{
Colin Crossf1885962020-11-20 15:28:30 -0800579 Rule: gensrcsMerge,
580 Implicits: copyFrom,
581 Outputs: outputFiles,
582 Description: "merge shards",
Colin Cross1a527682019-09-23 15:55:30 -0700583 Args: map[string]string{
584 "zipArgs": zipArgs.String(),
585 "tmpZip": android.PathForModuleGen(ctx, g.subDir+".zip").String(),
586 "genDir": android.PathForModuleGen(ctx, g.subDir).String(),
587 },
588 })
Colin Cross85a2e892018-07-09 09:45:06 -0700589 }
590
Colin Cross1a527682019-09-23 15:55:30 -0700591 g.outputFiles = outputFiles.Paths()
Chris Parsonsf874e462022-05-10 13:50:12 -0400592}
Jeff Gastonefc1b412017-03-29 17:29:06 -0700593
Chris Parsonsf874e462022-05-10 13:50:12 -0400594func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Vinh Tran140d5882022-06-10 14:23:27 -0400595 // Allowlist genrule to use depfile until we have a solution to remove it.
596 // TODO(b/235582219): Remove allowlist for genrule
Yu Liu6a7940c2023-05-09 17:12:22 -0700597 if Bool(g.properties.Depfile) {
Yu Liue7f7cbf2023-06-13 18:50:03 +0000598 sandboxingAllowlistSets := getSandboxingAllowlistSets(ctx)
Yu Liu6a7940c2023-05-09 17:12:22 -0700599 // TODO(b/283852474): Checking the GenruleSandboxing flag is temporary in
600 // order to pass the presubmit before internal master is updated.
Yu Liue7f7cbf2023-06-13 18:50:03 +0000601 if ctx.DeviceConfig().GenruleSandboxing() && !sandboxingAllowlistSets.depfileAllowSet[g.Name()] {
Yu Liu6a7940c2023-05-09 17:12:22 -0700602 ctx.PropertyErrorf(
603 "depfile",
604 "Deprecated to ensure the module type is convertible to Bazel. "+
605 "Try specifying the dependencies explicitly so that there is no need to use depfile. "+
606 "If not possible, the escape hatch is to add the module to allowlists.go to bypass the error.")
607 }
Vinh Tran140d5882022-06-10 14:23:27 -0400608 }
609
Chris Parsonsf874e462022-05-10 13:50:12 -0400610 g.generateCommonBuildActions(ctx)
611
612 // For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of
613 // the genrules on AOSP. That will make things simpler to look at the graph in the common
614 // case. For larger sets of outputs, inject a phony target in between to limit ninja file
615 // growth.
616 if len(g.outputFiles) <= 6 {
617 g.outputDeps = g.outputFiles
618 } else {
619 phonyFile := android.PathForModuleGen(ctx, "genrule-phony")
620 ctx.Build(pctx, android.BuildParams{
621 Rule: blueprint.Phony,
622 Output: phonyFile,
623 Inputs: g.outputFiles,
624 })
625 g.outputDeps = android.Paths{phonyFile}
Jeff Gaston02a684b2017-10-27 14:59:27 -0700626 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400627}
628
629func (g *Module) QueueBazelCall(ctx android.BaseModuleContext) {
630 bazelCtx := ctx.Config().BazelContext
631 bazelCtx.QueueBazelRequest(g.GetBazelLabel(ctx, g), cquery.GetOutputFiles, android.GetConfigKey(ctx))
632}
633
634func (g *Module) IsMixedBuildSupported(ctx android.BaseModuleContext) bool {
635 return true
Colin Crossd350ecd2015-04-28 13:25:36 -0700636}
Colin Crossd350ecd2015-04-28 13:25:36 -0700637
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700638// Collect information for opening IDE project files in java/jdeps.go.
639func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
640 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
641 for _, src := range g.properties.Srcs {
642 if strings.HasPrefix(src, ":") {
643 src = strings.Trim(src, ":")
644 dpInfo.Deps = append(dpInfo.Deps, src)
645 }
646 }
bralee1fbf4402020-05-21 10:11:59 +0800647 dpInfo.Paths = append(dpInfo.Paths, g.modulePaths...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700648}
649
Colin Crossa4ad2b02019-03-18 22:15:32 -0700650func (g *Module) AndroidMk() android.AndroidMkData {
651 return android.AndroidMkData{
Anton Hansson72f18492020-10-30 16:34:45 +0000652 Class: "ETC",
Colin Crossa4ad2b02019-03-18 22:15:32 -0700653 OutputFile: android.OptionalPathForPath(g.outputFiles[0]),
654 SubName: g.subName,
655 Extra: []android.AndroidMkExtraFunc{
656 func(w io.Writer, outputFile android.Path) {
Anton Hansson72f18492020-10-30 16:34:45 +0000657 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
Colin Crossa4ad2b02019-03-18 22:15:32 -0700658 },
659 },
660 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
661 android.WriteAndroidMkData(w, data)
662 if data.SubName != "" {
663 fmt.Fprintln(w, ".PHONY:", name)
664 fmt.Fprintln(w, name, ":", name+g.subName)
665 }
666 },
667 }
668}
669
Jiyong Park45bf82e2020-12-15 22:29:02 +0900670var _ android.ApexModule = (*Module)(nil)
671
672// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -0700673func (g *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
674 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900675 // Because generated outputs are checked by client modules(e.g. cc_library, ...)
676 // we can safely ignore the check here.
677 return nil
678}
679
Jeff Gaston437d23c2017-11-08 12:38:00 -0800680func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700681 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800682 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700683 }
684
Colin Cross36242852017-06-23 15:06:31 -0700685 module.AddProperties(props...)
686 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700687
Colin Cross7228ecd2019-11-18 16:00:16 -0800688 module.ImageInterface = noopImageInterface{}
689
Colin Cross36242852017-06-23 15:06:31 -0700690 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700691}
692
Colin Cross7228ecd2019-11-18 16:00:16 -0800693type noopImageInterface struct{}
694
695func (x noopImageInterface) ImageMutatorBegin(android.BaseModuleContext) {}
696func (x noopImageInterface) CoreVariantNeeded(android.BaseModuleContext) bool { return false }
Yifan Hong1b3348d2020-01-21 15:53:22 -0800697func (x noopImageInterface) RamdiskVariantNeeded(android.BaseModuleContext) bool { return false }
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700698func (x noopImageInterface) VendorRamdiskVariantNeeded(android.BaseModuleContext) bool { return false }
Inseob Kim08758f02021-04-08 21:13:22 +0900699func (x noopImageInterface) DebugRamdiskVariantNeeded(android.BaseModuleContext) bool { return false }
Colin Cross7228ecd2019-11-18 16:00:16 -0800700func (x noopImageInterface) RecoveryVariantNeeded(android.BaseModuleContext) bool { return false }
701func (x noopImageInterface) ExtraImageVariations(ctx android.BaseModuleContext) []string { return nil }
702func (x noopImageInterface) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
703}
704
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700705func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700706 properties := &genSrcsProperties{}
707
Colin Crossf1885962020-11-20 15:28:30 -0800708 // finalSubDir is the name of the subdirectory that output files will be generated into.
709 // It is used so that per-shard directories can be placed alongside it an then finally
710 // merged into it.
711 const finalSubDir = "gensrcs"
712
Colin Cross1a527682019-09-23 15:55:30 -0700713 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
Colin Cross1a527682019-09-23 15:55:30 -0700714 shardSize := defaultShardSize
715 if s := properties.Shard_size; s != nil {
716 shardSize = int(*s)
717 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800718
Colin Crossf1885962020-11-20 15:28:30 -0800719 // gensrcs rules can easily hit command line limits by repeating the command for
720 // every input file. Shard the input files into groups.
Colin Cross1a527682019-09-23 15:55:30 -0700721 shards := android.ShardPaths(srcFiles, shardSize)
722 var generateTasks []generateTask
Colin Crossbaccf5b2018-02-21 14:07:48 -0800723
Colin Cross1a527682019-09-23 15:55:30 -0700724 for i, shard := range shards {
725 var commands []string
726 var outFiles android.WritablePaths
Colin Cross3ea4eb82020-11-24 13:07:27 -0800727 var commandDepFiles []string
Colin Cross1a527682019-09-23 15:55:30 -0700728 var copyTo android.WritablePaths
Colin Cross1a527682019-09-23 15:55:30 -0700729
Colin Crossf1885962020-11-20 15:28:30 -0800730 // When sharding is enabled (i.e. len(shards) > 1), the sbox rules for each
731 // shard will be write to their own directories and then be merged together
732 // into finalSubDir. If sharding is not enabled (i.e. len(shards) == 1),
733 // the sbox rule will write directly to finalSubDir.
734 genSubDir := finalSubDir
Colin Cross1a527682019-09-23 15:55:30 -0700735 if len(shards) > 1 {
Colin Crossf1885962020-11-20 15:28:30 -0800736 genSubDir = strconv.Itoa(i)
Jeff Gaston437d23c2017-11-08 12:38:00 -0800737 }
738
Colin Crossf1885962020-11-20 15:28:30 -0800739 genDir := android.PathForModuleGen(ctx, genSubDir)
Colin Crossf1a035e2020-11-16 17:32:30 -0800740 // TODO(ccross): this RuleBuilder is a hack to be able to call
741 // rule.Command().PathForOutput. Replace this with passing the rule into the
742 // generator.
Yu Liu6a7940c2023-05-09 17:12:22 -0700743 rule := getSandboxedRuleBuilder(ctx, android.NewRuleBuilder(pctx, ctx).Sbox(genDir, nil))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800744
Colin Cross3ea4eb82020-11-24 13:07:27 -0800745 for _, in := range shard {
Colin Crossf1885962020-11-20 15:28:30 -0800746 outFile := android.GenPathWithExt(ctx, finalSubDir, in, String(properties.Output_extension))
747
748 // If sharding is enabled, then outFile is the path to the output file in
749 // the shard directory, and copyTo is the path to the output file in the
750 // final directory.
Colin Cross1a527682019-09-23 15:55:30 -0700751 if len(shards) > 1 {
Colin Crossf1885962020-11-20 15:28:30 -0800752 shardFile := android.GenPathWithExt(ctx, genSubDir, in, String(properties.Output_extension))
Colin Cross1a527682019-09-23 15:55:30 -0700753 copyTo = append(copyTo, outFile)
754 outFile = shardFile
755 }
756
757 outFiles = append(outFiles, outFile)
Colin Cross1a527682019-09-23 15:55:30 -0700758
Colin Crossf1885962020-11-20 15:28:30 -0800759 // pre-expand the command line to replace $in and $out with references to
760 // a single input and output file.
Colin Cross1a527682019-09-23 15:55:30 -0700761 command, err := android.Expand(rawCommand, func(name string) (string, error) {
762 switch name {
763 case "in":
764 return in.String(), nil
765 case "out":
Colin Crossf1a035e2020-11-16 17:32:30 -0800766 return rule.Command().PathForOutput(outFile), nil
Colin Cross3ea4eb82020-11-24 13:07:27 -0800767 case "depfile":
768 // Generate a depfile for each output file. Store the list for
769 // later in order to combine them all into a single depfile.
Colin Crossf1a035e2020-11-16 17:32:30 -0800770 depFile := rule.Command().PathForOutput(outFile.ReplaceExtension(ctx, "d"))
Colin Cross3ea4eb82020-11-24 13:07:27 -0800771 commandDepFiles = append(commandDepFiles, depFile)
772 return depFile, nil
Colin Cross1a527682019-09-23 15:55:30 -0700773 default:
774 return "$(" + name + ")", nil
775 }
776 })
777 if err != nil {
778 ctx.PropertyErrorf("cmd", err.Error())
779 }
780
781 // escape the command in case for example it contains '#', an odd number of '"', etc
782 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
783 commands = append(commands, command)
784 }
785 fullCommand := strings.Join(commands, " && ")
786
Colin Cross3ea4eb82020-11-24 13:07:27 -0800787 var outputDepfile android.WritablePath
788 var extraTools android.Paths
789 if len(commandDepFiles) > 0 {
790 // Each command wrote to a depfile, but ninja can only handle one
791 // depfile per rule. Use the dep_fixer tool at the end of the
792 // command to combine all the depfiles into a single output depfile.
793 outputDepfile = android.PathForModuleGen(ctx, genSubDir, "gensrcs.d")
794 depFixerTool := ctx.Config().HostToolPath(ctx, "dep_fixer")
795 fullCommand += fmt.Sprintf(" && %s -o $(depfile) %s",
Colin Crossd11cf622021-03-23 22:30:35 -0700796 rule.Command().PathForTool(depFixerTool),
Colin Crossba9e4032020-11-24 16:32:22 -0800797 strings.Join(commandDepFiles, " "))
Colin Cross3ea4eb82020-11-24 13:07:27 -0800798 extraTools = append(extraTools, depFixerTool)
799 }
800
Colin Cross1a527682019-09-23 15:55:30 -0700801 generateTasks = append(generateTasks, generateTask{
Colin Cross3ea4eb82020-11-24 13:07:27 -0800802 in: shard,
803 out: outFiles,
804 depFile: outputDepfile,
805 copyTo: copyTo,
806 genDir: genDir,
807 cmd: fullCommand,
808 shard: i,
809 shards: len(shards),
810 extraTools: extraTools,
Colin Cross1a527682019-09-23 15:55:30 -0700811 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800812 }
Colin Cross1a527682019-09-23 15:55:30 -0700813
814 return generateTasks
Colin Crossd350ecd2015-04-28 13:25:36 -0700815 }
816
Colin Cross1a527682019-09-23 15:55:30 -0700817 g := generatorFactory(taskGenerator, properties)
Colin Crossf1885962020-11-20 15:28:30 -0800818 g.subDir = finalSubDir
Colin Cross1a527682019-09-23 15:55:30 -0700819 return g
Colin Crossd350ecd2015-04-28 13:25:36 -0700820}
821
Colin Cross54190b32017-10-09 15:34:10 -0700822func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700823 m := NewGenSrcs()
824 android.InitAndroidModule(m)
Vinh Tranb69e1ae2022-05-20 18:54:09 -0400825 android.InitBazelModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700826 return m
827}
828
Colin Crossd350ecd2015-04-28 13:25:36 -0700829type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700830 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800831 Output_extension *string
Colin Cross1a527682019-09-23 15:55:30 -0700832
833 // maximum number of files that will be passed on a single command line.
834 Shard_size *int64
Colin Cross5049f022015-03-18 13:28:46 -0700835}
836
Vinh Tranb69e1ae2022-05-20 18:54:09 -0400837type bazelGensrcsAttributes struct {
838 Srcs bazel.LabelListAttribute
839 Output_extension *string
840 Tools bazel.LabelListAttribute
841 Cmd string
842}
843
Evgenii Stepanovf47c90d2020-12-02 18:55:09 -0800844const defaultShardSize = 50
Colin Cross1a527682019-09-23 15:55:30 -0700845
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700846func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700847 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700848
Colin Cross1a527682019-09-23 15:55:30 -0700849 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700850 outs := make(android.WritablePaths, len(properties.Out))
Colin Cross3d680512020-11-13 16:23:53 -0800851 var depFile android.WritablePath
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700852 for i, out := range properties.Out {
Colin Cross3d680512020-11-13 16:23:53 -0800853 outPath := android.PathForModuleGen(ctx, out)
854 if i == 0 {
855 depFile = outPath.ReplaceExtension(ctx, "d")
856 }
857 outs[i] = outPath
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700858 }
Colin Cross1a527682019-09-23 15:55:30 -0700859 return []generateTask{{
Colin Cross3d680512020-11-13 16:23:53 -0800860 in: srcFiles,
861 out: outs,
862 depFile: depFile,
863 genDir: android.PathForModuleGen(ctx),
864 cmd: rawCommand,
Colin Cross1a527682019-09-23 15:55:30 -0700865 }}
Colin Cross5049f022015-03-18 13:28:46 -0700866 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700867
Jeff Gaston437d23c2017-11-08 12:38:00 -0800868 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700869}
870
Colin Cross54190b32017-10-09 15:34:10 -0700871func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700872 m := NewGenRule()
873 android.InitAndroidModule(m)
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800874 android.InitDefaultableModule(m)
Liz Kammerea6666f2021-02-17 10:17:28 -0500875 android.InitBazelModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700876 return m
877}
878
Colin Crossd350ecd2015-04-28 13:25:36 -0700879type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700880 // names of the output files that will be generated
Yu Liud6201012022-10-17 12:29:15 -0700881 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700882}
Nan Zhangea568a42017-11-08 21:20:04 -0800883
Jingwen Chen316e07c2020-12-14 09:09:52 -0500884type bazelGenruleAttributes struct {
Jingwen Chen07027912021-03-15 06:02:43 -0400885 Srcs bazel.LabelListAttribute
Jingwen Chen316e07c2020-12-14 09:09:52 -0500886 Outs []string
Jingwen Chen07027912021-03-15 06:02:43 -0400887 Tools bazel.LabelListAttribute
Jingwen Chen316e07c2020-12-14 09:09:52 -0500888 Cmd string
889}
890
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400891// ConvertWithBp2build converts a Soong module -> Bazel target.
892func (m *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500893 // Bazel only has the "tools" attribute.
Jingwen Chen07027912021-03-15 06:02:43 -0400894 tools_prop := android.BazelLabelForModuleDeps(ctx, m.properties.Tools)
895 tool_files_prop := android.BazelLabelForModuleSrc(ctx, m.properties.Tool_files)
896 tools_prop.Append(tool_files_prop)
Liz Kammer356f7d42021-01-26 09:18:53 -0500897
Jingwen Chen07027912021-03-15 06:02:43 -0400898 tools := bazel.MakeLabelListAttribute(tools_prop)
Yu Liud6201012022-10-17 12:29:15 -0700899 srcs := bazel.LabelListAttribute{}
900 srcs_labels := bazel.LabelList{}
901 // Only cc_genrule is arch specific
902 if ctx.ModuleType() == "cc_genrule" {
903 for axis, configToProps := range m.GetArchVariantProperties(ctx, &generatorProperties{}) {
904 for config, props := range configToProps {
905 if props, ok := props.(*generatorProperties); ok {
906 labels := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs)
907 srcs_labels.Append(labels)
908 srcs.SetSelectValue(axis, config, labels)
909 }
910 }
911 }
912 } else {
913 srcs_labels = android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
914 srcs = bazel.MakeLabelListAttribute(srcs_labels)
915 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500916
917 var allReplacements bazel.LabelList
Jingwen Chen07027912021-03-15 06:02:43 -0400918 allReplacements.Append(tools.Value)
Yu Liud6201012022-10-17 12:29:15 -0700919 allReplacements.Append(bazel.FirstUniqueBazelLabelList(srcs_labels))
Liz Kammer356f7d42021-01-26 09:18:53 -0500920
921 // Replace in and out variables with $< and $@
922 var cmd string
923 if m.properties.Cmd != nil {
Vinh Tranb69e1ae2022-05-20 18:54:09 -0400924 if ctx.ModuleType() == "gensrcs" {
925 cmd = strings.ReplaceAll(*m.properties.Cmd, "$(in)", "$(SRC)")
926 cmd = strings.ReplaceAll(cmd, "$(out)", "$(OUT)")
927 } else {
928 cmd = strings.Replace(*m.properties.Cmd, "$(in)", "$(SRCS)", -1)
929 cmd = strings.Replace(cmd, "$(out)", "$(OUTS)", -1)
930 }
Vinh Tran32a98a52022-09-23 13:08:34 -0400931 cmd = strings.Replace(cmd, "$(genDir)", "$(RULEDIR)", -1)
Jingwen Chen07027912021-03-15 06:02:43 -0400932 if len(tools.Value.Includes) > 0 {
933 cmd = strings.Replace(cmd, "$(location)", fmt.Sprintf("$(location %s)", tools.Value.Includes[0].Label), -1)
934 cmd = strings.Replace(cmd, "$(locations)", fmt.Sprintf("$(locations %s)", tools.Value.Includes[0].Label), -1)
Liz Kammer356f7d42021-01-26 09:18:53 -0500935 }
936 for _, l := range allReplacements.Includes {
Jingwen Chen38e62642021-04-19 05:00:15 +0000937 bpLoc := fmt.Sprintf("$(location %s)", l.OriginalModuleName)
938 bpLocs := fmt.Sprintf("$(locations %s)", l.OriginalModuleName)
Liz Kammer356f7d42021-01-26 09:18:53 -0500939 bazelLoc := fmt.Sprintf("$(location %s)", l.Label)
940 bazelLocs := fmt.Sprintf("$(locations %s)", l.Label)
941 cmd = strings.Replace(cmd, bpLoc, bazelLoc, -1)
942 cmd = strings.Replace(cmd, bpLocs, bazelLocs, -1)
943 }
944 }
945
Spandan Das39b6cc52023-04-12 19:05:49 +0000946 tags := android.ApexAvailableTagsWithoutTestApexes(ctx, m)
Sam Delmericoeddd3c02022-12-02 17:31:58 -0500947
Vinh Tranb69e1ae2022-05-20 18:54:09 -0400948 if ctx.ModuleType() == "gensrcs" {
949 // The Output_extension prop is not in an immediately accessible field
950 // in the Module struct, so use GetProperties and cast it
951 // to the known struct prop.
952 var outputExtension *string
953 for _, propIntf := range m.GetProperties() {
954 if props, ok := propIntf.(*genSrcsProperties); ok {
955 outputExtension = props.Output_extension
956 break
957 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500958 }
Vinh Tranb69e1ae2022-05-20 18:54:09 -0400959 props := bazel.BazelTargetModuleProperties{
960 Rule_class: "gensrcs",
961 Bzl_load_location: "//build/bazel/rules:gensrcs.bzl",
962 }
963 attrs := &bazelGensrcsAttributes{
964 Srcs: srcs,
965 Output_extension: outputExtension,
966 Cmd: cmd,
967 Tools: tools,
968 }
Sam Delmericoeddd3c02022-12-02 17:31:58 -0500969 ctx.CreateBazelTargetModule(props, android.CommonAttributes{
970 Name: m.Name(),
971 Tags: tags,
972 }, attrs)
Vinh Tranb69e1ae2022-05-20 18:54:09 -0400973 } else {
974 // The Out prop is not in an immediately accessible field
975 // in the Module struct, so use GetProperties and cast it
976 // to the known struct prop.
977 var outs []string
978 for _, propIntf := range m.GetProperties() {
979 if props, ok := propIntf.(*genRuleProperties); ok {
980 outs = props.Out
981 break
982 }
983 }
984 attrs := &bazelGenruleAttributes{
985 Srcs: srcs,
986 Outs: outs,
987 Cmd: cmd,
988 Tools: tools,
989 }
990 props := bazel.BazelTargetModuleProperties{
991 Rule_class: "genrule",
992 }
Sam Delmericoeddd3c02022-12-02 17:31:58 -0500993 ctx.CreateBazelTargetModule(props, android.CommonAttributes{
994 Name: m.Name(),
995 Tags: tags,
996 }, attrs)
Liz Kammer356f7d42021-01-26 09:18:53 -0500997 }
Jingwen Chen316e07c2020-12-14 09:09:52 -0500998}
999
Nan Zhangea568a42017-11-08 21:20:04 -08001000var Bool = proptools.Bool
1001var String = proptools.String
Jaewoong Jung98716bd2018-12-10 08:13:18 -08001002
Jaewoong Jung98716bd2018-12-10 08:13:18 -08001003// Defaults
Jaewoong Jung98716bd2018-12-10 08:13:18 -08001004type Defaults struct {
1005 android.ModuleBase
1006 android.DefaultsModuleBase
1007}
1008
Jaewoong Jung98716bd2018-12-10 08:13:18 -08001009func defaultsFactory() android.Module {
1010 return DefaultsFactory()
1011}
1012
1013func DefaultsFactory(props ...interface{}) android.Module {
1014 module := &Defaults{}
1015
1016 module.AddProperties(props...)
1017 module.AddProperties(
1018 &generatorProperties{},
1019 &genRuleProperties{},
1020 )
1021
1022 android.InitDefaultsModule(module)
1023
1024 return module
1025}
Yu Liu6a7940c2023-05-09 17:12:22 -07001026
Yu Liue7f7cbf2023-06-13 18:50:03 +00001027var sandboxingAllowlistKey = android.NewOnceKey("genruleSandboxingAllowlistKey")
1028
1029type sandboxingAllowlistSets struct {
1030 sandboxingDenyModuleSet map[string]bool
1031 sandboxingDenyPathSet map[string]bool
1032 depfileAllowSet map[string]bool
1033}
1034
1035func getSandboxingAllowlistSets(ctx android.PathContext) *sandboxingAllowlistSets {
1036 return ctx.Config().Once(sandboxingAllowlistKey, func() interface{} {
1037 sandboxingDenyModuleSet := map[string]bool{}
1038 sandboxingDenyPathSet := map[string]bool{}
1039 depfileAllowSet := map[string]bool{}
1040
1041 android.AddToStringSet(sandboxingDenyModuleSet, append(DepfileAllowList, SandboxingDenyModuleList...))
1042 android.AddToStringSet(sandboxingDenyPathSet, SandboxingDenyPathList)
1043 android.AddToStringSet(depfileAllowSet, DepfileAllowList)
1044 return &sandboxingAllowlistSets{
1045 sandboxingDenyModuleSet: sandboxingDenyModuleSet,
1046 sandboxingDenyPathSet: sandboxingDenyPathSet,
1047 depfileAllowSet: depfileAllowSet,
1048 }
1049 }).(*sandboxingAllowlistSets)
1050}
Yu Liu6a7940c2023-05-09 17:12:22 -07001051func getSandboxedRuleBuilder(ctx android.ModuleContext, r *android.RuleBuilder) *android.RuleBuilder {
Yu Liu45d6af52023-05-24 23:10:18 +00001052 if !ctx.DeviceConfig().GenruleSandboxing() {
1053 return r.SandboxTools()
1054 }
Yu Liue7f7cbf2023-06-13 18:50:03 +00001055 sandboxingAllowlistSets := getSandboxingAllowlistSets(ctx)
1056 if sandboxingAllowlistSets.sandboxingDenyPathSet[ctx.ModuleDir()] ||
1057 sandboxingAllowlistSets.sandboxingDenyModuleSet[ctx.ModuleName()] {
Yu Liu6a7940c2023-05-09 17:12:22 -07001058 return r.SandboxTools()
1059 }
1060 return r.SandboxInputs()
1061}