| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 1 | // 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 Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 15 | // 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 Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 19 | package genrule | 
|  | 20 |  | 
|  | 21 | import ( | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 22 | "fmt" | 
| Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 23 | "io" | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 24 | "strconv" | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 25 | "strings" | 
| Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 26 |  | 
| Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 27 | "github.com/google/blueprint" | 
| Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 28 | "github.com/google/blueprint/bootstrap" | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 29 | "github.com/google/blueprint/proptools" | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 30 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 31 | "android/soong/android" | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 32 | ) | 
|  | 33 |  | 
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 34 | func init() { | 
| Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 35 | RegisterGenruleBuildComponents(android.InitRegistrationContext) | 
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 36 | } | 
| Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 37 |  | 
| Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 38 | // Test fixture preparer that will register most genrule build components. | 
|  | 39 | // | 
|  | 40 | // Singletons and mutators should only be added here if they are needed for a majority of genrule | 
|  | 41 | // module types, otherwise they should be added under a separate preparer to allow them to be | 
|  | 42 | // selected only when needed to reduce test execution time. | 
|  | 43 | // | 
|  | 44 | // Module types do not have much of an overhead unless they are used so this should include as many | 
|  | 45 | // module types as possible. The exceptions are those module types that require mutators and/or | 
|  | 46 | // singletons in order to function in which case they should be kept together in a separate | 
|  | 47 | // preparer. | 
|  | 48 | var PrepareForTestWithGenRuleBuildComponents = android.GroupFixturePreparers( | 
|  | 49 | android.FixtureRegisterWithContext(RegisterGenruleBuildComponents), | 
|  | 50 | ) | 
|  | 51 |  | 
|  | 52 | // Prepare a fixture to use all genrule module types, mutators and singletons fully. | 
|  | 53 | // | 
|  | 54 | // This should only be used by tests that want to run with as much of the build enabled as possible. | 
|  | 55 | var PrepareForIntegrationTestWithGenrule = android.GroupFixturePreparers( | 
|  | 56 | PrepareForTestWithGenRuleBuildComponents, | 
|  | 57 | ) | 
|  | 58 |  | 
| Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 59 | func RegisterGenruleBuildComponents(ctx android.RegistrationContext) { | 
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 60 | ctx.RegisterModuleType("genrule_defaults", defaultsFactory) | 
|  | 61 |  | 
|  | 62 | ctx.RegisterModuleType("gensrcs", GenSrcsFactory) | 
|  | 63 | ctx.RegisterModuleType("genrule", GenRuleFactory) | 
|  | 64 |  | 
|  | 65 | ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) { | 
|  | 66 | ctx.BottomUp("genrule_tool_deps", toolDepsMutator).Parallel() | 
|  | 67 | }) | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 70 | var ( | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 71 | pctx = android.NewPackageContext("android/soong/genrule") | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 72 |  | 
| Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 73 | // Used by gensrcs when there is more than 1 shard to merge the outputs | 
|  | 74 | // of each shard into a zip file. | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 75 | gensrcsMerge = pctx.AndroidStaticRule("gensrcsMerge", blueprint.RuleParams{ | 
|  | 76 | Command:        "${soongZip} -o ${tmpZip} @${tmpZip}.rsp && ${zipSync} -d ${genDir} ${tmpZip}", | 
|  | 77 | CommandDeps:    []string{"${soongZip}", "${zipSync}"}, | 
|  | 78 | Rspfile:        "${tmpZip}.rsp", | 
|  | 79 | RspfileContent: "${zipArgs}", | 
|  | 80 | }, "tmpZip", "genDir", "zipArgs") | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 81 | ) | 
|  | 82 |  | 
| Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 83 | func init() { | 
| Dan Willemsen | ddf504c | 2019-08-09 16:21:29 -0700 | [diff] [blame] | 84 | pctx.Import("android/soong/android") | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 85 |  | 
|  | 86 | pctx.HostBinToolVariable("soongZip", "soong_zip") | 
|  | 87 | pctx.HostBinToolVariable("zipSync", "zipsync") | 
| Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 90 | type SourceFileGenerator interface { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 91 | GeneratedSourceFiles() android.Paths | 
| Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 92 | GeneratedHeaderDirs() android.Paths | 
| Dan Willemsen | 9da9d49 | 2018-02-21 18:28:18 -0800 | [diff] [blame] | 93 | GeneratedDeps() android.Paths | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 96 | // Alias for android.HostToolProvider | 
|  | 97 | // Deprecated: use android.HostToolProvider instead. | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 98 | type HostToolProvider interface { | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 99 | android.HostToolProvider | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 100 | } | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 101 |  | 
| Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 102 | type hostToolDependencyTag struct { | 
|  | 103 | blueprint.BaseDependencyTag | 
| Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 104 | android.LicenseAnnotationToolchainDependencyTag | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 105 | label string | 
| Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 106 | } | 
| Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 107 |  | 
|  | 108 | func (t hostToolDependencyTag) AllowDisabledModuleDependency(target android.Module) bool { | 
|  | 109 | // Allow depending on a disabled module if it's replaced by a prebuilt | 
|  | 110 | // counterpart. We get the prebuilt through android.PrebuiltGetPreferred in | 
|  | 111 | // GenerateAndroidBuildActions. | 
|  | 112 | return target.IsReplacedByPrebuilt() | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | var _ android.AllowDisabledModuleDependency = (*hostToolDependencyTag)(nil) | 
|  | 116 |  | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 117 | type generatorProperties struct { | 
| Spandan Das | 93e9599 | 2021-07-29 18:26:39 +0000 | [diff] [blame] | 118 | // The command to run on one or more input files. Cmd supports substitution of a few variables. | 
| Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 119 | // | 
|  | 120 | // Available variables for substitution: | 
|  | 121 | // | 
| Spandan Das | 93e9599 | 2021-07-29 18:26:39 +0000 | [diff] [blame] | 122 | //  $(location): the path to the first entry in tools or tool_files. | 
|  | 123 | //  $(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. | 
|  | 124 | //  $(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. | 
|  | 125 | //  $(in): one or more input files. | 
|  | 126 | //  $(out): a single output file. | 
|  | 127 | //  $(depfile): a file to which dependencies will be written, if the depfile property is set to true. | 
|  | 128 | //  $(genDir): the sandbox directory for this tool; contains $(out). | 
| Colin Cross | 2296f5b | 2017-10-17 21:38:14 -0700 | [diff] [blame] | 129 | //  $$: a literal $ | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 130 | Cmd *string | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 131 |  | 
| Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 132 | // Enable reading a file containing dependencies in gcc format after the command completes | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 133 | Depfile *bool | 
| Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 134 |  | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 135 | // name of the modules (if any) that produces the host executable.   Leave empty for | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 136 | // prebuilts or scripts that do not need a module to build them. | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 137 | Tools []string | 
| Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 138 |  | 
| Sam Delmerico | f877563 | 2023-08-14 23:45:41 +0000 | [diff] [blame] | 139 | // Local files that are used by the tool | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 140 | Tool_files []string `android:"path"` | 
| Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 141 |  | 
|  | 142 | // List of directories to export generated headers from | 
|  | 143 | Export_include_dirs []string | 
| Colin Cross | 708c424 | 2017-01-13 18:05:49 -0800 | [diff] [blame] | 144 |  | 
|  | 145 | // list of input files | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 146 | Srcs []string `android:"path,arch_variant"` | 
| Dan Willemsen | eefa026 | 2018-11-17 14:01:18 -0800 | [diff] [blame] | 147 |  | 
|  | 148 | // input files to exclude | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 149 | Exclude_srcs []string `android:"path,arch_variant"` | 
| Justin Yun | 4da4ccc | 2023-07-06 10:56:29 +0900 | [diff] [blame] | 150 |  | 
|  | 151 | // Enable restat to update the output only if the output is changed | 
|  | 152 | Write_if_changed *bool | 
| Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 153 | } | 
| Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 154 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 155 | type Module struct { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 156 | android.ModuleBase | 
| Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 157 | android.DefaultableModuleBase | 
| Jiyong Park | fc752ca | 2019-06-12 13:27:29 +0900 | [diff] [blame] | 158 | android.ApexModuleBase | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 159 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 160 | // For other packages to make their own genrules with extra | 
|  | 161 | // properties | 
|  | 162 | Extra interface{} | 
| Colin Cross | f3bfd02 | 2021-09-27 15:15:06 -0700 | [diff] [blame] | 163 |  | 
|  | 164 | // CmdModifier can be set by wrappers around genrule to modify the command, for example to | 
|  | 165 | // prefix environment variables to it. | 
|  | 166 | CmdModifier func(ctx android.ModuleContext, cmd string) string | 
|  | 167 |  | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 168 | android.ImageInterface | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 169 |  | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 170 | properties generatorProperties | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 171 |  | 
| Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 172 | // For the different tasks that genrule and gensrc generate. genrule will | 
|  | 173 | // generate 1 task, and gensrc will generate 1 or more tasks based on the | 
|  | 174 | // number of shards the input files are sharded into. | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 175 | taskGenerator taskFunc | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 176 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 177 | rule        blueprint.Rule | 
|  | 178 | rawCommands []string | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 179 |  | 
| Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 180 | exportedIncludeDirs android.Paths | 
| Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 181 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 182 | outputFiles android.Paths | 
| Dan Willemsen | 9da9d49 | 2018-02-21 18:28:18 -0800 | [diff] [blame] | 183 | outputDeps  android.Paths | 
| Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 184 |  | 
|  | 185 | subName string | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 186 | subDir  string | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 187 | } | 
|  | 188 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 189 | type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 190 |  | 
|  | 191 | type generateTask struct { | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 192 | in          android.Paths | 
|  | 193 | out         android.WritablePaths | 
|  | 194 | depFile     android.WritablePath | 
|  | 195 | copyTo      android.WritablePaths // For gensrcs to set on gensrcsMerge rule. | 
|  | 196 | genDir      android.WritablePath | 
|  | 197 | extraTools  android.Paths // dependencies on tools used by the generator | 
|  | 198 | extraInputs map[string][]string | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 199 |  | 
| Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 200 | cmd string | 
|  | 201 | // For gensrsc sharding. | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 202 | shard  int | 
|  | 203 | shards int | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 204 | } | 
|  | 205 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 206 | func (g *Module) GeneratedSourceFiles() android.Paths { | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 207 | return g.outputFiles | 
|  | 208 | } | 
|  | 209 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 210 | func (g *Module) Srcs() android.Paths { | 
| Nan Zhang | e42777a | 2018-03-27 16:19:42 -0700 | [diff] [blame] | 211 | return append(android.Paths{}, g.outputFiles...) | 
| Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 212 | } | 
|  | 213 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 214 | func (g *Module) GeneratedHeaderDirs() android.Paths { | 
| Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 215 | return g.exportedIncludeDirs | 
| Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 216 | } | 
|  | 217 |  | 
| Dan Willemsen | 9da9d49 | 2018-02-21 18:28:18 -0800 | [diff] [blame] | 218 | func (g *Module) GeneratedDeps() android.Paths { | 
|  | 219 | return g.outputDeps | 
|  | 220 | } | 
|  | 221 |  | 
| Jooyung Han | 8c7e3ed | 2021-06-28 17:35:58 +0900 | [diff] [blame] | 222 | func (g *Module) OutputFiles(tag string) (android.Paths, error) { | 
|  | 223 | if tag == "" { | 
|  | 224 | return append(android.Paths{}, g.outputFiles...), nil | 
|  | 225 | } | 
|  | 226 | // otherwise, tag should match one of outputs | 
|  | 227 | for _, outputFile := range g.outputFiles { | 
|  | 228 | if outputFile.Rel() == tag { | 
|  | 229 | return android.Paths{outputFile}, nil | 
|  | 230 | } | 
|  | 231 | } | 
|  | 232 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | var _ android.SourceFileProducer = (*Module)(nil) | 
|  | 236 | var _ android.OutputFileProducer = (*Module)(nil) | 
|  | 237 |  | 
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 238 | func toolDepsMutator(ctx android.BottomUpMutatorContext) { | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 239 | if g, ok := ctx.Module().(*Module); ok { | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 240 | for _, tool := range g.properties.Tools { | 
|  | 241 | tag := hostToolDependencyTag{label: tool} | 
|  | 242 | if m := android.SrcIsModule(tool); m != "" { | 
|  | 243 | tool = m | 
|  | 244 | } | 
| Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 245 | ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), tag, tool) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 246 | } | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 247 | } | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 248 | } | 
|  | 249 |  | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 250 | // generateCommonBuildActions contains build action generation logic | 
|  | 251 | // common to both the mixed build case and the legacy case of genrule processing. | 
|  | 252 | // To fully support genrule in mixed builds, the contents of this function should | 
|  | 253 | // approach zero; there should be no genrule action registration done directly | 
|  | 254 | // by Soong logic in the mixed-build case. | 
|  | 255 | func (g *Module) generateCommonBuildActions(ctx android.ModuleContext) { | 
| Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 256 | g.subName = ctx.ModuleSubDir() | 
|  | 257 |  | 
| Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 258 | if len(g.properties.Export_include_dirs) > 0 { | 
|  | 259 | for _, dir := range g.properties.Export_include_dirs { | 
|  | 260 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 261 | android.PathForModuleGen(ctx, g.subDir, ctx.ModuleDir(), dir)) | 
| Liz Kammer | d38c87c | 2023-07-17 09:58:50 -0400 | [diff] [blame] | 262 | // Also export without ModuleDir for consistency with Export_include_dirs not being set | 
|  | 263 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, | 
|  | 264 | android.PathForModuleGen(ctx, g.subDir, dir)) | 
| Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 265 | } | 
|  | 266 | } else { | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 267 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, g.subDir)) | 
| Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 268 | } | 
| Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 269 |  | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 270 | locationLabels := map[string]location{} | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 271 | firstLabel := "" | 
|  | 272 |  | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 273 | addLocationLabel := func(label string, loc location) { | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 274 | if firstLabel == "" { | 
|  | 275 | firstLabel = label | 
|  | 276 | } | 
|  | 277 | if _, exists := locationLabels[label]; !exists { | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 278 | locationLabels[label] = loc | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 279 | } else { | 
| Anton Hansson | 7cd41e5 | 2021-10-08 16:13:10 +0100 | [diff] [blame] | 280 | ctx.ModuleErrorf("multiple locations for label %q: %q and %q (do you have duplicate srcs entries?)", | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 281 | label, locationLabels[label], loc) | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 282 | } | 
|  | 283 | } | 
| Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 284 |  | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 285 | var tools android.Paths | 
|  | 286 | var packagedTools []android.PackagingSpec | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 287 | if len(g.properties.Tools) > 0 { | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 288 | seenTools := make(map[string]bool) | 
|  | 289 |  | 
| Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 290 | ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) { | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 291 | switch tag := ctx.OtherModuleDependencyTag(module).(type) { | 
|  | 292 | case hostToolDependencyTag: | 
| Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 293 | tool := ctx.OtherModuleName(module) | 
| Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 294 | if m, ok := module.(android.Module); ok { | 
|  | 295 | // Necessary to retrieve any prebuilt replacement for the tool, since | 
|  | 296 | // toolDepsMutator runs too late for the prebuilt mutators to have | 
|  | 297 | // replaced the dependency. | 
|  | 298 | module = android.PrebuiltGetPreferred(ctx, m) | 
|  | 299 | } | 
| Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 300 |  | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 301 | switch t := module.(type) { | 
|  | 302 | case android.HostToolProvider: | 
|  | 303 | // A HostToolProvider provides the path to a tool, which will be copied | 
|  | 304 | // into the sandbox. | 
| Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 305 | if !t.(android.Module).Enabled() { | 
| Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 306 | if ctx.Config().AllowMissingDependencies() { | 
| Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 307 | ctx.AddMissingDependencies([]string{tool}) | 
|  | 308 | } else { | 
|  | 309 | ctx.ModuleErrorf("depends on disabled module %q", tool) | 
|  | 310 | } | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 311 | return | 
| Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 312 | } | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 313 | path := t.HostToolPath() | 
|  | 314 | if !path.Valid() { | 
|  | 315 | ctx.ModuleErrorf("host tool %q missing output file", tool) | 
|  | 316 | return | 
|  | 317 | } | 
|  | 318 | if specs := t.TransitivePackagingSpecs(); specs != nil { | 
|  | 319 | // If the HostToolProvider has PackgingSpecs, which are definitions of the | 
|  | 320 | // required relative locations of the tool and its dependencies, use those | 
|  | 321 | // instead.  They will be copied to those relative locations in the sbox | 
|  | 322 | // sandbox. | 
|  | 323 | packagedTools = append(packagedTools, specs...) | 
|  | 324 | // Assume that the first PackagingSpec of the module is the tool. | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 325 | addLocationLabel(tag.label, packagedToolLocation{specs[0]}) | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 326 | } else { | 
|  | 327 | tools = append(tools, path.Path()) | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 328 | addLocationLabel(tag.label, toolLocation{android.Paths{path.Path()}}) | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 329 | } | 
|  | 330 | case bootstrap.GoBinaryTool: | 
|  | 331 | // A GoBinaryTool provides the install path to a tool, which will be copied. | 
| Colin Cross | a44551f | 2021-10-25 15:36:21 -0700 | [diff] [blame] | 332 | p := android.PathForGoBinary(ctx, t) | 
|  | 333 | tools = append(tools, p) | 
|  | 334 | addLocationLabel(tag.label, toolLocation{android.Paths{p}}) | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 335 | default: | 
| Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 336 | ctx.ModuleErrorf("%q is not a host tool provider", tool) | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 337 | return | 
| Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 338 | } | 
|  | 339 |  | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 340 | seenTools[tag.label] = true | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 341 | } | 
| Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 342 | }) | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 343 |  | 
|  | 344 | // If AllowMissingDependencies is enabled, the build will not have stopped when | 
|  | 345 | // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical | 
| Liz Kammer | 20ebfb4 | 2020-07-28 11:32:07 -0700 | [diff] [blame] | 346 | // "cmd: unknown location label ..." errors later.  Add a placeholder file to the local label. | 
|  | 347 | // The command that uses this placeholder file will never be executed because the rule will be | 
|  | 348 | // replaced with an android.Error rule reporting the missing dependencies. | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 349 | if ctx.Config().AllowMissingDependencies() { | 
|  | 350 | for _, tool := range g.properties.Tools { | 
|  | 351 | if !seenTools[tool] { | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 352 | addLocationLabel(tool, errorLocation{"***missing tool " + tool + "***"}) | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 353 | } | 
|  | 354 | } | 
|  | 355 | } | 
| Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 356 | } | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 357 |  | 
| Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 358 | if ctx.Failed() { | 
|  | 359 | return | 
|  | 360 | } | 
|  | 361 |  | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 362 | for _, toolFile := range g.properties.Tool_files { | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 363 | paths := android.PathsForModuleSrc(ctx, []string{toolFile}) | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 364 | tools = append(tools, paths...) | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 365 | addLocationLabel(toolFile, toolLocation{paths}) | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 366 | } | 
|  | 367 |  | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 368 | addLabelsForInputs := func(propName string, include, exclude []string) android.Paths { | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 369 | includeDirInPaths := ctx.DeviceConfig().BuildBrokenInputDir(g.Name()) | 
|  | 370 | var srcFiles android.Paths | 
|  | 371 | for _, in := range include { | 
|  | 372 | paths, missingDeps := android.PathsAndMissingDepsRelativeToModuleSourceDir(android.SourceInput{ | 
|  | 373 | Context: ctx, Paths: []string{in}, ExcludePaths: exclude, IncludeDirs: includeDirInPaths, | 
|  | 374 | }) | 
|  | 375 | if len(missingDeps) > 0 { | 
|  | 376 | if !ctx.Config().AllowMissingDependencies() { | 
|  | 377 | panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator", | 
|  | 378 | missingDeps)) | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | // If AllowMissingDependencies is enabled, the build will not have stopped when | 
|  | 382 | // the dependency was added on a missing SourceFileProducer module, which will result in nonsensical | 
|  | 383 | // "cmd: label ":..." has no files" errors later.  Add a placeholder file to the local label. | 
|  | 384 | // The command that uses this placeholder file will never be executed because the rule will be | 
|  | 385 | // replaced with an android.Error rule reporting the missing dependencies. | 
|  | 386 | ctx.AddMissingDependencies(missingDeps) | 
|  | 387 | addLocationLabel(in, errorLocation{"***missing " + propName + " " + in + "***"}) | 
|  | 388 | } else { | 
|  | 389 | srcFiles = append(srcFiles, paths...) | 
|  | 390 | addLocationLabel(in, inputLocation{paths}) | 
|  | 391 | } | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 392 | } | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 393 | return srcFiles | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 394 | } | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 395 | srcFiles := addLabelsForInputs("srcs", g.properties.Srcs, g.properties.Exclude_srcs) | 
| Aditya Choudhary | 26df39f | 2023-11-29 16:42:42 +0000 | [diff] [blame] | 396 | ctx.SetProvider(blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: srcFiles.Strings()}) | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 397 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 398 | var copyFrom android.Paths | 
|  | 399 | var outputFiles android.WritablePaths | 
|  | 400 | var zipArgs strings.Builder | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 401 |  | 
| Colin Cross | f3bfd02 | 2021-09-27 15:15:06 -0700 | [diff] [blame] | 402 | cmd := String(g.properties.Cmd) | 
|  | 403 | if g.CmdModifier != nil { | 
|  | 404 | cmd = g.CmdModifier(ctx, cmd) | 
|  | 405 | } | 
|  | 406 |  | 
| Liz Kammer | 796921d | 2023-07-11 08:21:41 -0400 | [diff] [blame] | 407 | var extraInputs android.Paths | 
| Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 408 | // Generate tasks, either from genrule or gensrcs. | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 409 | for i, task := range g.taskGenerator(ctx, cmd, srcFiles) { | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 410 | if len(task.out) == 0 { | 
|  | 411 | ctx.ModuleErrorf("must have at least one output file") | 
|  | 412 | return | 
| Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 413 | } | 
|  | 414 |  | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 415 | // Only handle extra inputs once as these currently are the same across all tasks | 
|  | 416 | if i == 0 { | 
|  | 417 | for name, values := range task.extraInputs { | 
|  | 418 | extraInputs = append(extraInputs, addLabelsForInputs(name, values, []string{})...) | 
|  | 419 | } | 
|  | 420 | } | 
|  | 421 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 422 | // Pick a unique path outside the task.genDir for the sbox manifest textproto, | 
|  | 423 | // a unique rule name, and the user-visible description. | 
|  | 424 | manifestName := "genrule.sbox.textproto" | 
|  | 425 | desc := "generate" | 
|  | 426 | name := "generator" | 
|  | 427 | if task.shards > 0 { | 
|  | 428 | manifestName = "genrule_" + strconv.Itoa(task.shard) + ".sbox.textproto" | 
|  | 429 | desc += " " + strconv.Itoa(task.shard) | 
|  | 430 | name += strconv.Itoa(task.shard) | 
|  | 431 | } else if len(task.out) == 1 { | 
|  | 432 | desc += " " + task.out[0].Base() | 
|  | 433 | } | 
|  | 434 |  | 
|  | 435 | manifestPath := android.PathForModuleOut(ctx, manifestName) | 
|  | 436 |  | 
|  | 437 | // Use a RuleBuilder to create a rule that runs the command inside an sbox sandbox. | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 438 | rule := getSandboxedRuleBuilder(ctx, android.NewRuleBuilder(pctx, ctx).Sbox(task.genDir, manifestPath)) | 
| Justin Yun | 4da4ccc | 2023-07-06 10:56:29 +0900 | [diff] [blame] | 439 | if Bool(g.properties.Write_if_changed) { | 
|  | 440 | rule.Restat() | 
|  | 441 | } | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 442 | cmd := rule.Command() | 
|  | 443 |  | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 444 | for _, out := range task.out { | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 445 | addLocationLabel(out.Rel(), outputLocation{out}) | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 446 | } | 
|  | 447 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 448 | referencedDepfile := false | 
|  | 449 |  | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 450 | rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) { | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 451 | // report the error directly without returning an error to android.Expand to catch multiple errors in a | 
|  | 452 | // single run | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 453 | reportError := func(fmt string, args ...interface{}) (string, error) { | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 454 | ctx.PropertyErrorf("cmd", fmt, args...) | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 455 | return "SOONG_ERROR", nil | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 456 | } | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 457 |  | 
| Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 458 | // Apply shell escape to each cases to prevent source file paths containing $ from being evaluated in shell | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 459 | switch name { | 
|  | 460 | case "location": | 
|  | 461 | if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 { | 
|  | 462 | return reportError("at least one `tools` or `tool_files` is required if $(location) is used") | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 463 | } | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 464 | loc := locationLabels[firstLabel] | 
|  | 465 | paths := loc.Paths(cmd) | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 466 | if len(paths) == 0 { | 
|  | 467 | return reportError("default label %q has no files", firstLabel) | 
|  | 468 | } else if len(paths) > 1 { | 
|  | 469 | return reportError("default label %q has multiple files, use $(locations %s) to reference it", | 
|  | 470 | firstLabel, firstLabel) | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 471 | } | 
| Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 472 | return proptools.ShellEscape(paths[0]), nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 473 | case "in": | 
| Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 474 | return strings.Join(proptools.ShellEscapeList(cmd.PathsForInputs(srcFiles)), " "), nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 475 | case "out": | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 476 | var sandboxOuts []string | 
|  | 477 | for _, out := range task.out { | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 478 | sandboxOuts = append(sandboxOuts, cmd.PathForOutput(out)) | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 479 | } | 
| Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 480 | return strings.Join(proptools.ShellEscapeList(sandboxOuts), " "), nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 481 | case "depfile": | 
|  | 482 | referencedDepfile = true | 
|  | 483 | if !Bool(g.properties.Depfile) { | 
|  | 484 | return reportError("$(depfile) used without depfile property") | 
|  | 485 | } | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 486 | return "__SBOX_DEPFILE__", nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 487 | case "genDir": | 
| Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 488 | return proptools.ShellEscape(cmd.PathForOutput(task.genDir)), nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 489 | default: | 
|  | 490 | if strings.HasPrefix(name, "location ") { | 
|  | 491 | label := strings.TrimSpace(strings.TrimPrefix(name, "location ")) | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 492 | if loc, ok := locationLabels[label]; ok { | 
|  | 493 | paths := loc.Paths(cmd) | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 494 | if len(paths) == 0 { | 
|  | 495 | return reportError("label %q has no files", label) | 
|  | 496 | } else if len(paths) > 1 { | 
|  | 497 | return reportError("label %q has multiple files, use $(locations %s) to reference it", | 
|  | 498 | label, label) | 
|  | 499 | } | 
| Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 500 | return proptools.ShellEscape(paths[0]), nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 501 | } else { | 
| Anton Hansson | bebf526 | 2022-02-23 11:42:38 +0000 | [diff] [blame] | 502 | return reportError("unknown location label %q is not in srcs, out, tools or tool_files.", label) | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 503 | } | 
|  | 504 | } else if strings.HasPrefix(name, "locations ") { | 
|  | 505 | label := strings.TrimSpace(strings.TrimPrefix(name, "locations ")) | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 506 | if loc, ok := locationLabels[label]; ok { | 
|  | 507 | paths := loc.Paths(cmd) | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 508 | if len(paths) == 0 { | 
|  | 509 | return reportError("label %q has no files", label) | 
|  | 510 | } | 
| Cole Faust | ce74a59 | 2023-12-07 14:58:45 -0800 | [diff] [blame] | 511 | return strings.Join(proptools.ShellEscapeList(paths), " "), nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 512 | } else { | 
| Anton Hansson | bebf526 | 2022-02-23 11:42:38 +0000 | [diff] [blame] | 513 | return reportError("unknown locations label %q is not in srcs, out, tools or tool_files.", label) | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 514 | } | 
|  | 515 | } else { | 
|  | 516 | return reportError("unknown variable '$(%s)'", name) | 
|  | 517 | } | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 518 | } | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 519 | }) | 
|  | 520 |  | 
|  | 521 | if err != nil { | 
|  | 522 | ctx.PropertyErrorf("cmd", "%s", err.Error()) | 
|  | 523 | return | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 524 | } | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 525 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 526 | if Bool(g.properties.Depfile) && !referencedDepfile { | 
|  | 527 | ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd") | 
|  | 528 | return | 
|  | 529 | } | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 530 | g.rawCommands = append(g.rawCommands, rawCommand) | 
| Bill Peckham | c087be1 | 2020-02-13 15:55:10 -0800 | [diff] [blame] | 531 |  | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 532 | cmd.Text(rawCommand) | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 533 | cmd.Implicits(srcFiles) // need to be able to reference other srcs | 
|  | 534 | cmd.Implicits(extraInputs) | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 535 | cmd.ImplicitOutputs(task.out) | 
|  | 536 | cmd.Implicits(task.in) | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 537 | cmd.ImplicitTools(tools) | 
|  | 538 | cmd.ImplicitTools(task.extraTools) | 
|  | 539 | cmd.ImplicitPackagedTools(packagedTools) | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 540 | if Bool(g.properties.Depfile) { | 
|  | 541 | cmd.ImplicitDepFile(task.depFile) | 
|  | 542 | } | 
|  | 543 |  | 
|  | 544 | // Create the rule to run the genrule command inside sbox. | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 545 | rule.Build(name, desc) | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 546 |  | 
|  | 547 | if len(task.copyTo) > 0 { | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 548 | // If copyTo is set, multiple shards need to be copied into a single directory. | 
|  | 549 | // task.out contains the per-shard paths, and copyTo contains the corresponding | 
|  | 550 | // final path.  The files need to be copied into the final directory by a | 
|  | 551 | // single rule so it can remove the directory before it starts to ensure no | 
|  | 552 | // old files remain.  zipsync already does this, so build up zipArgs that | 
|  | 553 | // zip all the per-shard directories into a single zip. | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 554 | outputFiles = append(outputFiles, task.copyTo...) | 
|  | 555 | copyFrom = append(copyFrom, task.out.Paths()...) | 
|  | 556 | zipArgs.WriteString(" -C " + task.genDir.String()) | 
|  | 557 | zipArgs.WriteString(android.JoinWithPrefix(task.out.Strings(), " -f ")) | 
|  | 558 | } else { | 
|  | 559 | outputFiles = append(outputFiles, task.out...) | 
|  | 560 | } | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 561 | } | 
|  | 562 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 563 | if len(copyFrom) > 0 { | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 564 | // Create a rule that zips all the per-shard directories into a single zip and then | 
|  | 565 | // uses zipsync to unzip it into the final directory. | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 566 | ctx.Build(pctx, android.BuildParams{ | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 567 | Rule:        gensrcsMerge, | 
|  | 568 | Implicits:   copyFrom, | 
|  | 569 | Outputs:     outputFiles, | 
|  | 570 | Description: "merge shards", | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 571 | Args: map[string]string{ | 
|  | 572 | "zipArgs": zipArgs.String(), | 
|  | 573 | "tmpZip":  android.PathForModuleGen(ctx, g.subDir+".zip").String(), | 
|  | 574 | "genDir":  android.PathForModuleGen(ctx, g.subDir).String(), | 
|  | 575 | }, | 
|  | 576 | }) | 
| Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 577 | } | 
|  | 578 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 579 | g.outputFiles = outputFiles.Paths() | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 580 | } | 
| Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 581 |  | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 582 | func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 583 | // Allowlist genrule to use depfile until we have a solution to remove it. | 
| Cole Faust | 27609f1 | 2023-12-12 10:15:59 -0800 | [diff] [blame] | 584 | // TODO(b/307824623): Remove depfile property | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 585 | if Bool(g.properties.Depfile) { | 
| Yu Liu | e7f7cbf | 2023-06-13 18:50:03 +0000 | [diff] [blame] | 586 | sandboxingAllowlistSets := getSandboxingAllowlistSets(ctx) | 
| Yu Liu | e7f7cbf | 2023-06-13 18:50:03 +0000 | [diff] [blame] | 587 | if ctx.DeviceConfig().GenruleSandboxing() && !sandboxingAllowlistSets.depfileAllowSet[g.Name()] { | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 588 | ctx.PropertyErrorf( | 
|  | 589 | "depfile", | 
| Cole Faust | 27609f1 | 2023-12-12 10:15:59 -0800 | [diff] [blame] | 590 | "Deprecated because with genrule sandboxing, dependencies must be known before the action is run "+ | 
|  | 591 | "in order to add them to the sandbox. "+ | 
|  | 592 | "Please specify the dependencies explicitly so that there is no need to use depfile.") | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 593 | } | 
| Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 594 | } | 
|  | 595 |  | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 596 | g.generateCommonBuildActions(ctx) | 
|  | 597 |  | 
|  | 598 | // For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of | 
|  | 599 | // the genrules on AOSP. That will make things simpler to look at the graph in the common | 
|  | 600 | // case. For larger sets of outputs, inject a phony target in between to limit ninja file | 
|  | 601 | // growth. | 
|  | 602 | if len(g.outputFiles) <= 6 { | 
|  | 603 | g.outputDeps = g.outputFiles | 
|  | 604 | } else { | 
|  | 605 | phonyFile := android.PathForModuleGen(ctx, "genrule-phony") | 
|  | 606 | ctx.Build(pctx, android.BuildParams{ | 
|  | 607 | Rule:   blueprint.Phony, | 
|  | 608 | Output: phonyFile, | 
|  | 609 | Inputs: g.outputFiles, | 
|  | 610 | }) | 
|  | 611 | g.outputDeps = android.Paths{phonyFile} | 
| Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 612 | } | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 613 | } | 
|  | 614 |  | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 615 | // Collect information for opening IDE project files in java/jdeps.go. | 
|  | 616 | func (g *Module) IDEInfo(dpInfo *android.IdeInfo) { | 
|  | 617 | dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...) | 
|  | 618 | for _, src := range g.properties.Srcs { | 
|  | 619 | if strings.HasPrefix(src, ":") { | 
|  | 620 | src = strings.Trim(src, ":") | 
|  | 621 | dpInfo.Deps = append(dpInfo.Deps, src) | 
|  | 622 | } | 
|  | 623 | } | 
|  | 624 | } | 
|  | 625 |  | 
| Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 626 | func (g *Module) AndroidMk() android.AndroidMkData { | 
|  | 627 | return android.AndroidMkData{ | 
| Anton Hansson | 72f1849 | 2020-10-30 16:34:45 +0000 | [diff] [blame] | 628 | Class:      "ETC", | 
| Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 629 | OutputFile: android.OptionalPathForPath(g.outputFiles[0]), | 
|  | 630 | SubName:    g.subName, | 
|  | 631 | Extra: []android.AndroidMkExtraFunc{ | 
|  | 632 | func(w io.Writer, outputFile android.Path) { | 
| Anton Hansson | 72f1849 | 2020-10-30 16:34:45 +0000 | [diff] [blame] | 633 | fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true") | 
| Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 634 | }, | 
|  | 635 | }, | 
|  | 636 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { | 
|  | 637 | android.WriteAndroidMkData(w, data) | 
|  | 638 | if data.SubName != "" { | 
|  | 639 | fmt.Fprintln(w, ".PHONY:", name) | 
|  | 640 | fmt.Fprintln(w, name, ":", name+g.subName) | 
|  | 641 | } | 
|  | 642 | }, | 
|  | 643 | } | 
|  | 644 | } | 
|  | 645 |  | 
| Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 646 | var _ android.ApexModule = (*Module)(nil) | 
|  | 647 |  | 
|  | 648 | // Implements android.ApexModule | 
| Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 649 | func (g *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, | 
|  | 650 | sdkVersion android.ApiLevel) error { | 
| Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 651 | // Because generated outputs are checked by client modules(e.g. cc_library, ...) | 
|  | 652 | // we can safely ignore the check here. | 
|  | 653 | return nil | 
|  | 654 | } | 
|  | 655 |  | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 656 | func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module { | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 657 | module := &Module{ | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 658 | taskGenerator: taskGenerator, | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 659 | } | 
|  | 660 |  | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 661 | module.AddProperties(props...) | 
|  | 662 | module.AddProperties(&module.properties) | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 663 |  | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 664 | module.ImageInterface = noopImageInterface{} | 
|  | 665 |  | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 666 | return module | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 667 | } | 
|  | 668 |  | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 669 | type noopImageInterface struct{} | 
|  | 670 |  | 
|  | 671 | func (x noopImageInterface) ImageMutatorBegin(android.BaseModuleContext)                 {} | 
|  | 672 | func (x noopImageInterface) CoreVariantNeeded(android.BaseModuleContext) bool            { return false } | 
| Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 673 | func (x noopImageInterface) RamdiskVariantNeeded(android.BaseModuleContext) bool         { return false } | 
| Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 674 | func (x noopImageInterface) VendorRamdiskVariantNeeded(android.BaseModuleContext) bool   { return false } | 
| Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 675 | func (x noopImageInterface) DebugRamdiskVariantNeeded(android.BaseModuleContext) bool    { return false } | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 676 | func (x noopImageInterface) RecoveryVariantNeeded(android.BaseModuleContext) bool        { return false } | 
|  | 677 | func (x noopImageInterface) ExtraImageVariations(ctx android.BaseModuleContext) []string { return nil } | 
|  | 678 | func (x noopImageInterface) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { | 
|  | 679 | } | 
|  | 680 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 681 | func NewGenSrcs() *Module { | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 682 | properties := &genSrcsProperties{} | 
|  | 683 |  | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 684 | // finalSubDir is the name of the subdirectory that output files will be generated into. | 
|  | 685 | // It is used so that per-shard directories can be placed alongside it an then finally | 
|  | 686 | // merged into it. | 
|  | 687 | const finalSubDir = "gensrcs" | 
|  | 688 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 689 | taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask { | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 690 | shardSize := defaultShardSize | 
|  | 691 | if s := properties.Shard_size; s != nil { | 
|  | 692 | shardSize = int(*s) | 
|  | 693 | } | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 694 |  | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 695 | // gensrcs rules can easily hit command line limits by repeating the command for | 
|  | 696 | // every input file.  Shard the input files into groups. | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 697 | shards := android.ShardPaths(srcFiles, shardSize) | 
|  | 698 | var generateTasks []generateTask | 
| Colin Cross | baccf5b | 2018-02-21 14:07:48 -0800 | [diff] [blame] | 699 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 700 | for i, shard := range shards { | 
|  | 701 | var commands []string | 
|  | 702 | var outFiles android.WritablePaths | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 703 | var commandDepFiles []string | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 704 | var copyTo android.WritablePaths | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 705 |  | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 706 | // When sharding is enabled (i.e. len(shards) > 1), the sbox rules for each | 
|  | 707 | // shard will be write to their own directories and then be merged together | 
|  | 708 | // into finalSubDir.  If sharding is not enabled (i.e. len(shards) == 1), | 
|  | 709 | // the sbox rule will write directly to finalSubDir. | 
|  | 710 | genSubDir := finalSubDir | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 711 | if len(shards) > 1 { | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 712 | genSubDir = strconv.Itoa(i) | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 713 | } | 
|  | 714 |  | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 715 | genDir := android.PathForModuleGen(ctx, genSubDir) | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 716 | // TODO(ccross): this RuleBuilder is a hack to be able to call | 
|  | 717 | // rule.Command().PathForOutput.  Replace this with passing the rule into the | 
|  | 718 | // generator. | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 719 | rule := getSandboxedRuleBuilder(ctx, android.NewRuleBuilder(pctx, ctx).Sbox(genDir, nil)) | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 720 |  | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 721 | for _, in := range shard { | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 722 | outFile := android.GenPathWithExt(ctx, finalSubDir, in, String(properties.Output_extension)) | 
|  | 723 |  | 
|  | 724 | // If sharding is enabled, then outFile is the path to the output file in | 
|  | 725 | // the shard directory, and copyTo is the path to the output file in the | 
|  | 726 | // final directory. | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 727 | if len(shards) > 1 { | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 728 | shardFile := android.GenPathWithExt(ctx, genSubDir, in, String(properties.Output_extension)) | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 729 | copyTo = append(copyTo, outFile) | 
|  | 730 | outFile = shardFile | 
|  | 731 | } | 
|  | 732 |  | 
|  | 733 | outFiles = append(outFiles, outFile) | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 734 |  | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 735 | // pre-expand the command line to replace $in and $out with references to | 
|  | 736 | // a single input and output file. | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 737 | command, err := android.Expand(rawCommand, func(name string) (string, error) { | 
|  | 738 | switch name { | 
|  | 739 | case "in": | 
|  | 740 | return in.String(), nil | 
|  | 741 | case "out": | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 742 | return rule.Command().PathForOutput(outFile), nil | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 743 | case "depfile": | 
|  | 744 | // Generate a depfile for each output file.  Store the list for | 
|  | 745 | // later in order to combine them all into a single depfile. | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 746 | depFile := rule.Command().PathForOutput(outFile.ReplaceExtension(ctx, "d")) | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 747 | commandDepFiles = append(commandDepFiles, depFile) | 
|  | 748 | return depFile, nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 749 | default: | 
|  | 750 | return "$(" + name + ")", nil | 
|  | 751 | } | 
|  | 752 | }) | 
|  | 753 | if err != nil { | 
|  | 754 | ctx.PropertyErrorf("cmd", err.Error()) | 
|  | 755 | } | 
|  | 756 |  | 
|  | 757 | // escape the command in case for example it contains '#', an odd number of '"', etc | 
|  | 758 | command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command)) | 
|  | 759 | commands = append(commands, command) | 
|  | 760 | } | 
|  | 761 | fullCommand := strings.Join(commands, " && ") | 
|  | 762 |  | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 763 | var outputDepfile android.WritablePath | 
|  | 764 | var extraTools android.Paths | 
|  | 765 | if len(commandDepFiles) > 0 { | 
|  | 766 | // Each command wrote to a depfile, but ninja can only handle one | 
|  | 767 | // depfile per rule.  Use the dep_fixer tool at the end of the | 
|  | 768 | // command to combine all the depfiles into a single output depfile. | 
|  | 769 | outputDepfile = android.PathForModuleGen(ctx, genSubDir, "gensrcs.d") | 
|  | 770 | depFixerTool := ctx.Config().HostToolPath(ctx, "dep_fixer") | 
|  | 771 | fullCommand += fmt.Sprintf(" && %s -o $(depfile) %s", | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 772 | rule.Command().PathForTool(depFixerTool), | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 773 | strings.Join(commandDepFiles, " ")) | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 774 | extraTools = append(extraTools, depFixerTool) | 
|  | 775 | } | 
|  | 776 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 777 | generateTasks = append(generateTasks, generateTask{ | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 778 | in:         shard, | 
|  | 779 | out:        outFiles, | 
|  | 780 | depFile:    outputDepfile, | 
|  | 781 | copyTo:     copyTo, | 
|  | 782 | genDir:     genDir, | 
|  | 783 | cmd:        fullCommand, | 
|  | 784 | shard:      i, | 
|  | 785 | shards:     len(shards), | 
|  | 786 | extraTools: extraTools, | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 787 | extraInputs: map[string][]string{ | 
|  | 788 | "data": properties.Data, | 
|  | 789 | }, | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 790 | }) | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 791 | } | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 792 |  | 
|  | 793 | return generateTasks | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 794 | } | 
|  | 795 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 796 | g := generatorFactory(taskGenerator, properties) | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 797 | g.subDir = finalSubDir | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 798 | return g | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 799 | } | 
|  | 800 |  | 
| Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 801 | func GenSrcsFactory() android.Module { | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 802 | m := NewGenSrcs() | 
|  | 803 | android.InitAndroidModule(m) | 
|  | 804 | return m | 
|  | 805 | } | 
|  | 806 |  | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 807 | type genSrcsProperties struct { | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 808 | // extension that will be substituted for each output file | 
| Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 809 | Output_extension *string | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 810 |  | 
|  | 811 | // maximum number of files that will be passed on a single command line. | 
|  | 812 | Shard_size *int64 | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 813 |  | 
|  | 814 | // Additional files needed for build that are not tooling related. | 
|  | 815 | Data []string `android:"path"` | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 816 | } | 
|  | 817 |  | 
| Evgenii Stepanov | f47c90d | 2020-12-02 18:55:09 -0800 | [diff] [blame] | 818 | const defaultShardSize = 50 | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 819 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 820 | func NewGenRule() *Module { | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 821 | properties := &genRuleProperties{} | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 822 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 823 | taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask { | 
| Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 824 | outs := make(android.WritablePaths, len(properties.Out)) | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 825 | var depFile android.WritablePath | 
| Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 826 | for i, out := range properties.Out { | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 827 | outPath := android.PathForModuleGen(ctx, out) | 
|  | 828 | if i == 0 { | 
|  | 829 | depFile = outPath.ReplaceExtension(ctx, "d") | 
|  | 830 | } | 
|  | 831 | outs[i] = outPath | 
| Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 832 | } | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 833 | return []generateTask{{ | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 834 | in:      srcFiles, | 
|  | 835 | out:     outs, | 
|  | 836 | depFile: depFile, | 
|  | 837 | genDir:  android.PathForModuleGen(ctx), | 
|  | 838 | cmd:     rawCommand, | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 839 | }} | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 840 | } | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 841 |  | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 842 | return generatorFactory(taskGenerator, properties) | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 843 | } | 
|  | 844 |  | 
| Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 845 | func GenRuleFactory() android.Module { | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 846 | m := NewGenRule() | 
|  | 847 | android.InitAndroidModule(m) | 
| Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 848 | android.InitDefaultableModule(m) | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 849 | return m | 
|  | 850 | } | 
|  | 851 |  | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 852 | type genRuleProperties struct { | 
| Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 853 | // names of the output files that will be generated | 
| Yu Liu | d620101 | 2022-10-17 12:29:15 -0700 | [diff] [blame] | 854 | Out []string | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 855 | } | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 856 |  | 
|  | 857 | var Bool = proptools.Bool | 
|  | 858 | var String = proptools.String | 
| Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 859 |  | 
| Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 860 | // Defaults | 
| Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 861 | type Defaults struct { | 
|  | 862 | android.ModuleBase | 
|  | 863 | android.DefaultsModuleBase | 
|  | 864 | } | 
|  | 865 |  | 
| Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 866 | func defaultsFactory() android.Module { | 
|  | 867 | return DefaultsFactory() | 
|  | 868 | } | 
|  | 869 |  | 
|  | 870 | func DefaultsFactory(props ...interface{}) android.Module { | 
|  | 871 | module := &Defaults{} | 
|  | 872 |  | 
|  | 873 | module.AddProperties(props...) | 
|  | 874 | module.AddProperties( | 
|  | 875 | &generatorProperties{}, | 
|  | 876 | &genRuleProperties{}, | 
|  | 877 | ) | 
|  | 878 |  | 
|  | 879 | android.InitDefaultsModule(module) | 
|  | 880 |  | 
|  | 881 | return module | 
|  | 882 | } | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 883 |  | 
| Yu Liu | e7f7cbf | 2023-06-13 18:50:03 +0000 | [diff] [blame] | 884 | var sandboxingAllowlistKey = android.NewOnceKey("genruleSandboxingAllowlistKey") | 
|  | 885 |  | 
|  | 886 | type sandboxingAllowlistSets struct { | 
|  | 887 | sandboxingDenyModuleSet map[string]bool | 
|  | 888 | sandboxingDenyPathSet   map[string]bool | 
|  | 889 | depfileAllowSet         map[string]bool | 
|  | 890 | } | 
|  | 891 |  | 
|  | 892 | func getSandboxingAllowlistSets(ctx android.PathContext) *sandboxingAllowlistSets { | 
|  | 893 | return ctx.Config().Once(sandboxingAllowlistKey, func() interface{} { | 
|  | 894 | sandboxingDenyModuleSet := map[string]bool{} | 
|  | 895 | sandboxingDenyPathSet := map[string]bool{} | 
|  | 896 | depfileAllowSet := map[string]bool{} | 
|  | 897 |  | 
|  | 898 | android.AddToStringSet(sandboxingDenyModuleSet, append(DepfileAllowList, SandboxingDenyModuleList...)) | 
|  | 899 | android.AddToStringSet(sandboxingDenyPathSet, SandboxingDenyPathList) | 
|  | 900 | android.AddToStringSet(depfileAllowSet, DepfileAllowList) | 
|  | 901 | return &sandboxingAllowlistSets{ | 
|  | 902 | sandboxingDenyModuleSet: sandboxingDenyModuleSet, | 
|  | 903 | sandboxingDenyPathSet:   sandboxingDenyPathSet, | 
|  | 904 | depfileAllowSet:         depfileAllowSet, | 
|  | 905 | } | 
|  | 906 | }).(*sandboxingAllowlistSets) | 
|  | 907 | } | 
| Liz Kammer | 0db0e34 | 2023-07-18 11:39:30 -0400 | [diff] [blame] | 908 |  | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 909 | func getSandboxedRuleBuilder(ctx android.ModuleContext, r *android.RuleBuilder) *android.RuleBuilder { | 
| Yu Liu | 45d6af5 | 2023-05-24 23:10:18 +0000 | [diff] [blame] | 910 | if !ctx.DeviceConfig().GenruleSandboxing() { | 
|  | 911 | return r.SandboxTools() | 
|  | 912 | } | 
| Yu Liu | e7f7cbf | 2023-06-13 18:50:03 +0000 | [diff] [blame] | 913 | sandboxingAllowlistSets := getSandboxingAllowlistSets(ctx) | 
|  | 914 | if sandboxingAllowlistSets.sandboxingDenyPathSet[ctx.ModuleDir()] || | 
|  | 915 | sandboxingAllowlistSets.sandboxingDenyModuleSet[ctx.ModuleName()] { | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 916 | return r.SandboxTools() | 
|  | 917 | } | 
|  | 918 | return r.SandboxInputs() | 
|  | 919 | } |