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" |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 24 | "path/filepath" |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 25 | "strconv" |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 26 | "strings" |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 27 | |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 28 | "github.com/google/blueprint" |
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) { |
Colin Cross | 8a96280 | 2024-10-09 15:29:27 -0700 | [diff] [blame] | 66 | ctx.BottomUp("genrule_tool_deps", toolDepsMutator) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 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 | |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 115 | func (t hostToolDependencyTag) AllowDisabledModuleDependencyProxy( |
| 116 | ctx android.OtherModuleProviderContext, target android.ModuleProxy) bool { |
| 117 | return android.OtherModuleProviderOrDefault( |
| 118 | ctx, target, android.CommonPropertiesProviderKey).ReplacedByPrebuilt |
| 119 | } |
| 120 | |
Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 121 | var _ android.AllowDisabledModuleDependency = (*hostToolDependencyTag)(nil) |
| 122 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 123 | type generatorProperties struct { |
Spandan Das | 93e9599 | 2021-07-29 18:26:39 +0000 | [diff] [blame] | 124 | // 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] | 125 | // |
| 126 | // Available variables for substitution: |
| 127 | // |
Spandan Das | 93e9599 | 2021-07-29 18:26:39 +0000 | [diff] [blame] | 128 | // $(location): the path to the first entry in tools or tool_files. |
| 129 | // $(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. |
| 130 | // $(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. |
| 131 | // $(in): one or more input files. |
| 132 | // $(out): a single output file. |
Spandan Das | 93e9599 | 2021-07-29 18:26:39 +0000 | [diff] [blame] | 133 | // $(genDir): the sandbox directory for this tool; contains $(out). |
Colin Cross | 2296f5b | 2017-10-17 21:38:14 -0700 | [diff] [blame] | 134 | // $$: a literal $ |
Aleks Todorov | 1eb06c4 | 2024-06-03 15:23:56 +0100 | [diff] [blame] | 135 | Cmd proptools.Configurable[string] `android:"replace_instead_of_append"` |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 136 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 137 | // 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] | 138 | // prebuilts or scripts that do not need a module to build them. |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 139 | Tools []string |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 140 | |
Sam Delmerico | f877563 | 2023-08-14 23:45:41 +0000 | [diff] [blame] | 141 | // Local files that are used by the tool |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 142 | Tool_files []string `android:"path"` |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 143 | |
| 144 | // List of directories to export generated headers from |
| 145 | Export_include_dirs []string |
Colin Cross | 708c424 | 2017-01-13 18:05:49 -0800 | [diff] [blame] | 146 | |
| 147 | // list of input files |
Cole Faust | f966e38 | 2024-10-08 16:17:56 -0700 | [diff] [blame] | 148 | Srcs proptools.Configurable[[]string] `android:"path,arch_variant"` |
Dan Willemsen | eefa026 | 2018-11-17 14:01:18 -0800 | [diff] [blame] | 149 | |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 150 | // Same as srcs, but will add dependencies on modules via a device os variation and the device's |
| 151 | // first supported arch's variation. Can be used to add a dependency from a host genrule to |
| 152 | // a device module. |
| 153 | Device_first_srcs proptools.Configurable[[]string] `android:"path_device_first"` |
| 154 | |
| 155 | // Same as srcs, but will add dependencies on modules via a device os variation and the common |
| 156 | // arch variation. Can be used to add a dependency from a host genrule to a device module. |
| 157 | Device_common_srcs proptools.Configurable[[]string] `android:"path_device_common"` |
| 158 | |
| 159 | // Same as srcs, but will add dependencies on modules via a common_os os variation. |
| 160 | Common_os_srcs proptools.Configurable[[]string] `android:"path_common_os"` |
| 161 | |
Dan Willemsen | eefa026 | 2018-11-17 14:01:18 -0800 | [diff] [blame] | 162 | // input files to exclude |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 163 | Exclude_srcs []string `android:"path,arch_variant"` |
Justin Yun | 4da4ccc | 2023-07-06 10:56:29 +0900 | [diff] [blame] | 164 | |
| 165 | // Enable restat to update the output only if the output is changed |
| 166 | Write_if_changed *bool |
Cole Faust | 78f3c3a | 2024-08-15 17:19:34 -0700 | [diff] [blame] | 167 | |
| 168 | // When set to true, an additional $(build_number_file) label will be available |
| 169 | // to use in the cmd. This will be the location of a text file containing the |
| 170 | // build number. The dependency on this file will be "order-only", meaning that |
| 171 | // the genrule will not rerun when only this file changes, to avoid rerunning |
| 172 | // the genrule every build, because the build number changes every build. |
| 173 | // This also means that you should not attempt to consume the build number from |
| 174 | // the result of this genrule in another build rule. If you do, the build number |
| 175 | // in the second build rule will be stale when the second build rule rebuilds |
| 176 | // but this genrule does not. Only certain allowlisted modules are allowed to |
| 177 | // use this property, usages of the build number should be kept to the absolute |
| 178 | // minimum. Particularly no modules on the system image may include the build |
| 179 | // number. Prefer using libbuildversion via the use_version_lib property on |
| 180 | // cc modules. |
| 181 | Uses_order_only_build_number_file *bool |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 182 | } |
Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 183 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 184 | type Module struct { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 185 | android.ModuleBase |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 186 | android.DefaultableModuleBase |
Jiyong Park | fc752ca | 2019-06-12 13:27:29 +0900 | [diff] [blame] | 187 | android.ApexModuleBase |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 188 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 189 | // For other packages to make their own genrules with extra |
| 190 | // properties |
| 191 | Extra interface{} |
Colin Cross | f3bfd02 | 2021-09-27 15:15:06 -0700 | [diff] [blame] | 192 | |
| 193 | // CmdModifier can be set by wrappers around genrule to modify the command, for example to |
| 194 | // prefix environment variables to it. |
| 195 | CmdModifier func(ctx android.ModuleContext, cmd string) string |
| 196 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 197 | android.ImageInterface |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 198 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 199 | properties generatorProperties |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 200 | |
Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 201 | // For the different tasks that genrule and gensrc generate. genrule will |
| 202 | // generate 1 task, and gensrc will generate 1 or more tasks based on the |
| 203 | // number of shards the input files are sharded into. |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 204 | taskGenerator taskFunc |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 205 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 206 | rule blueprint.Rule |
| 207 | rawCommands []string |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 208 | |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 209 | exportedIncludeDirs android.Paths |
Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 210 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 211 | outputFiles android.Paths |
Dan Willemsen | 9da9d49 | 2018-02-21 18:28:18 -0800 | [diff] [blame] | 212 | outputDeps android.Paths |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 213 | |
| 214 | subName string |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 215 | subDir string |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 218 | type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 219 | |
| 220 | type generateTask struct { |
Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 221 | in android.Paths |
| 222 | out android.WritablePaths |
Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 223 | copyTo android.WritablePaths // For gensrcs to set on gensrcsMerge rule. |
| 224 | genDir android.WritablePath |
Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 225 | extraInputs map[string][]string |
Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 226 | |
Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 227 | cmd string |
| 228 | // For gensrsc sharding. |
Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 229 | shard int |
| 230 | shards int |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 231 | |
| 232 | // For nsjail tasks |
| 233 | useNsjail bool |
Inseob Kim | 93036a5 | 2024-10-25 17:02:21 +0900 | [diff] [blame^] | 234 | dirSrcs android.DirectoryPaths |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 237 | func (g *Module) GeneratedSourceFiles() android.Paths { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 238 | return g.outputFiles |
| 239 | } |
| 240 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 241 | func (g *Module) Srcs() android.Paths { |
Nan Zhang | e42777a | 2018-03-27 16:19:42 -0700 | [diff] [blame] | 242 | return append(android.Paths{}, g.outputFiles...) |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 245 | func (g *Module) GeneratedHeaderDirs() android.Paths { |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 246 | return g.exportedIncludeDirs |
Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Dan Willemsen | 9da9d49 | 2018-02-21 18:28:18 -0800 | [diff] [blame] | 249 | func (g *Module) GeneratedDeps() android.Paths { |
| 250 | return g.outputDeps |
| 251 | } |
| 252 | |
Jooyung Han | 8c7e3ed | 2021-06-28 17:35:58 +0900 | [diff] [blame] | 253 | var _ android.SourceFileProducer = (*Module)(nil) |
Jooyung Han | 8c7e3ed | 2021-06-28 17:35:58 +0900 | [diff] [blame] | 254 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 255 | func toolDepsMutator(ctx android.BottomUpMutatorContext) { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 256 | if g, ok := ctx.Module().(*Module); ok { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 257 | for _, tool := range g.properties.Tools { |
| 258 | tag := hostToolDependencyTag{label: tool} |
| 259 | if m := android.SrcIsModule(tool); m != "" { |
| 260 | tool = m |
| 261 | } |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 262 | ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), tag, tool) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 263 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 264 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Cole Faust | f23fdc0 | 2024-08-23 15:21:13 -0700 | [diff] [blame] | 267 | var buildNumberAllowlistKey = android.NewOnceKey("genruleBuildNumberAllowlistKey") |
| 268 | |
Cole Faust | 78f3c3a | 2024-08-15 17:19:34 -0700 | [diff] [blame] | 269 | // This allowlist should be kept to the bare minimum, it's |
| 270 | // intended for things that existed before the build number |
| 271 | // was tightly controlled. Prefer using libbuildversion |
| 272 | // via the use_version_lib property of cc modules. |
Cole Faust | f23fdc0 | 2024-08-23 15:21:13 -0700 | [diff] [blame] | 273 | // This is a function instead of a global map so that |
| 274 | // soong plugins cannot add entries to the allowlist |
| 275 | func isModuleInBuildNumberAllowlist(ctx android.ModuleContext) bool { |
| 276 | allowlist := ctx.Config().Once(buildNumberAllowlistKey, func() interface{} { |
Cole Faust | dc01878 | 2024-08-28 11:08:06 -0700 | [diff] [blame] | 277 | // Define the allowlist as a list and then copy it into a map so that |
| 278 | // gofmt doesn't change unnecessary lines trying to align the values of the map. |
| 279 | allowlist := []string{ |
Cole Faust | f23fdc0 | 2024-08-23 15:21:13 -0700 | [diff] [blame] | 280 | // go/keep-sorted start |
Cole Faust | dc01878 | 2024-08-28 11:08:06 -0700 | [diff] [blame] | 281 | "build/soong/tests:gen", |
| 282 | "hardware/google/camera/common/hal/aidl_service:aidl_camera_build_version", |
| 283 | "tools/tradefederation/core:tradefed_zip", |
| 284 | "vendor/google/services/LyricCameraHAL/src/apex:com.google.pixel.camera.hal.manifest", |
Cole Faust | f23fdc0 | 2024-08-23 15:21:13 -0700 | [diff] [blame] | 285 | // go/keep-sorted end |
| 286 | } |
Cole Faust | dc01878 | 2024-08-28 11:08:06 -0700 | [diff] [blame] | 287 | allowlistMap := make(map[string]bool, len(allowlist)) |
| 288 | for _, a := range allowlist { |
| 289 | allowlistMap[a] = true |
| 290 | } |
| 291 | return allowlistMap |
Cole Faust | f23fdc0 | 2024-08-23 15:21:13 -0700 | [diff] [blame] | 292 | }).(map[string]bool) |
| 293 | |
| 294 | _, ok := allowlist[ctx.ModuleDir()+":"+ctx.ModuleName()] |
| 295 | return ok |
Cole Faust | 78f3c3a | 2024-08-15 17:19:34 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 298 | // generateCommonBuildActions contains build action generation logic |
| 299 | // common to both the mixed build case and the legacy case of genrule processing. |
| 300 | // To fully support genrule in mixed builds, the contents of this function should |
| 301 | // approach zero; there should be no genrule action registration done directly |
| 302 | // by Soong logic in the mixed-build case. |
| 303 | func (g *Module) generateCommonBuildActions(ctx android.ModuleContext) { |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 304 | // Add the variant as a suffix to the make modules to create, so that the make modules |
| 305 | // don't conflict because make doesn't know about variants. However, this causes issues with |
| 306 | // tracking required dependencies as the required property in soong is passed straight to make |
| 307 | // without accounting for these suffixes. To make it a little easier to work with, don't use |
| 308 | // a suffix for android_common variants so that java_genrules look like regular 1-variant |
| 309 | // genrules to make. |
| 310 | if ctx.ModuleSubDir() != "android_common" { |
| 311 | g.subName = ctx.ModuleSubDir() |
| 312 | } |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 313 | |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 314 | if len(g.properties.Export_include_dirs) > 0 { |
| 315 | for _, dir := range g.properties.Export_include_dirs { |
| 316 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 317 | android.PathForModuleGen(ctx, g.subDir, ctx.ModuleDir(), dir)) |
Liz Kammer | d38c87c | 2023-07-17 09:58:50 -0400 | [diff] [blame] | 318 | // Also export without ModuleDir for consistency with Export_include_dirs not being set |
| 319 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, |
| 320 | android.PathForModuleGen(ctx, g.subDir, dir)) |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 321 | } |
| 322 | } else { |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 323 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, g.subDir)) |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 324 | } |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 325 | |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 326 | locationLabels := map[string]location{} |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 327 | firstLabel := "" |
| 328 | |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 329 | addLocationLabel := func(label string, loc location) { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 330 | if firstLabel == "" { |
| 331 | firstLabel = label |
| 332 | } |
| 333 | if _, exists := locationLabels[label]; !exists { |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 334 | locationLabels[label] = loc |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 335 | } else { |
Anton Hansson | 7cd41e5 | 2021-10-08 16:13:10 +0100 | [diff] [blame] | 336 | 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] | 337 | label, locationLabels[label], loc) |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 340 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 341 | var tools android.Paths |
| 342 | var packagedTools []android.PackagingSpec |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 343 | if len(g.properties.Tools) > 0 { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 344 | seenTools := make(map[string]bool) |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 345 | ctx.VisitDirectDepsProxyAllowDisabled(func(proxy android.ModuleProxy) { |
| 346 | switch tag := ctx.OtherModuleDependencyTag(proxy).(type) { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 347 | case hostToolDependencyTag: |
Colin Cross | 648daea | 2024-09-12 14:35:29 -0700 | [diff] [blame] | 348 | // Necessary to retrieve any prebuilt replacement for the tool, since |
| 349 | // toolDepsMutator runs too late for the prebuilt mutators to have |
| 350 | // replaced the dependency. |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 351 | module := android.PrebuiltGetPreferred(ctx, proxy) |
| 352 | tool := ctx.OtherModuleName(module) |
| 353 | if h, ok := android.OtherModuleProvider(ctx, module, android.HostToolProviderKey); ok { |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 354 | // A HostToolProvider provides the path to a tool, which will be copied |
| 355 | // into the sandbox. |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 356 | if !android.OtherModuleProviderOrDefault(ctx, module, android.CommonPropertiesProviderKey).Enabled { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 357 | if ctx.Config().AllowMissingDependencies() { |
Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 358 | ctx.AddMissingDependencies([]string{tool}) |
| 359 | } else { |
| 360 | ctx.ModuleErrorf("depends on disabled module %q", tool) |
| 361 | } |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 362 | return |
Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 363 | } |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 364 | path := h.HostToolPath |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 365 | if !path.Valid() { |
| 366 | ctx.ModuleErrorf("host tool %q missing output file", tool) |
| 367 | return |
| 368 | } |
Yu Liu | bad1eef | 2024-08-21 22:37:35 +0000 | [diff] [blame] | 369 | if specs := android.OtherModuleProviderOrDefault( |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 370 | ctx, module, android.InstallFilesProvider).TransitivePackagingSpecs.ToList(); specs != nil { |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 371 | // If the HostToolProvider has PackgingSpecs, which are definitions of the |
| 372 | // required relative locations of the tool and its dependencies, use those |
| 373 | // instead. They will be copied to those relative locations in the sbox |
| 374 | // sandbox. |
Jiyong Park | 8fb0e97 | 2024-03-18 18:29:37 +0900 | [diff] [blame] | 375 | // Care must be taken since TransitivePackagingSpec may return device-side |
| 376 | // paths via the required property. Filter them out. |
| 377 | for i, ps := range specs { |
| 378 | if ps.Partition() != "" { |
| 379 | if i == 0 { |
| 380 | panic("first PackagingSpec is assumed to be the host-side tool") |
| 381 | } |
| 382 | continue |
| 383 | } |
| 384 | packagedTools = append(packagedTools, ps) |
| 385 | } |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 386 | // Assume that the first PackagingSpec of the module is the tool. |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 387 | addLocationLabel(tag.label, packagedToolLocation{specs[0]}) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 388 | } else { |
| 389 | tools = append(tools, path.Path()) |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 390 | addLocationLabel(tag.label, toolLocation{android.Paths{path.Path()}}) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 391 | } |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 392 | } else { |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 393 | ctx.ModuleErrorf("%q is not a host tool provider", tool) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 394 | return |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 397 | seenTools[tag.label] = true |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 398 | } |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 399 | }) |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 400 | |
| 401 | // If AllowMissingDependencies is enabled, the build will not have stopped when |
| 402 | // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical |
Liz Kammer | 20ebfb4 | 2020-07-28 11:32:07 -0700 | [diff] [blame] | 403 | // "cmd: unknown location label ..." errors later. Add a placeholder file to the local label. |
| 404 | // The command that uses this placeholder file will never be executed because the rule will be |
| 405 | // replaced with an android.Error rule reporting the missing dependencies. |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 406 | if ctx.Config().AllowMissingDependencies() { |
| 407 | for _, tool := range g.properties.Tools { |
| 408 | if !seenTools[tool] { |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 409 | addLocationLabel(tool, errorLocation{"***missing tool " + tool + "***"}) |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | } |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 413 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 414 | |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 415 | if ctx.Failed() { |
| 416 | return |
| 417 | } |
| 418 | |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 419 | for _, toolFile := range g.properties.Tool_files { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 420 | paths := android.PathsForModuleSrc(ctx, []string{toolFile}) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 421 | tools = append(tools, paths...) |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 422 | addLocationLabel(toolFile, toolLocation{paths}) |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 425 | addLabelsForInputs := func(propName string, include, exclude []string) android.Paths { |
Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 426 | includeDirInPaths := ctx.DeviceConfig().BuildBrokenInputDir(g.Name()) |
| 427 | var srcFiles android.Paths |
| 428 | for _, in := range include { |
| 429 | paths, missingDeps := android.PathsAndMissingDepsRelativeToModuleSourceDir(android.SourceInput{ |
| 430 | Context: ctx, Paths: []string{in}, ExcludePaths: exclude, IncludeDirs: includeDirInPaths, |
| 431 | }) |
| 432 | if len(missingDeps) > 0 { |
| 433 | if !ctx.Config().AllowMissingDependencies() { |
| 434 | panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator", |
| 435 | missingDeps)) |
| 436 | } |
| 437 | |
| 438 | // If AllowMissingDependencies is enabled, the build will not have stopped when |
| 439 | // the dependency was added on a missing SourceFileProducer module, which will result in nonsensical |
| 440 | // "cmd: label ":..." has no files" errors later. Add a placeholder file to the local label. |
| 441 | // The command that uses this placeholder file will never be executed because the rule will be |
| 442 | // replaced with an android.Error rule reporting the missing dependencies. |
| 443 | ctx.AddMissingDependencies(missingDeps) |
| 444 | addLocationLabel(in, errorLocation{"***missing " + propName + " " + in + "***"}) |
| 445 | } else { |
| 446 | srcFiles = append(srcFiles, paths...) |
| 447 | addLocationLabel(in, inputLocation{paths}) |
| 448 | } |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 449 | } |
Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 450 | return srcFiles |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 451 | } |
Cole Faust | f966e38 | 2024-10-08 16:17:56 -0700 | [diff] [blame] | 452 | srcs := g.properties.Srcs.GetOrDefault(ctx, nil) |
| 453 | srcFiles := addLabelsForInputs("srcs", srcs, g.properties.Exclude_srcs) |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 454 | srcFiles = append(srcFiles, addLabelsForInputs("device_first_srcs", g.properties.Device_first_srcs.GetOrDefault(ctx, nil), nil)...) |
| 455 | srcFiles = append(srcFiles, addLabelsForInputs("device_common_srcs", g.properties.Device_common_srcs.GetOrDefault(ctx, nil), nil)...) |
| 456 | srcFiles = append(srcFiles, addLabelsForInputs("common_os_srcs", g.properties.Common_os_srcs.GetOrDefault(ctx, nil), nil)...) |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 457 | android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: srcFiles.Strings()}) |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 458 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 459 | var copyFrom android.Paths |
| 460 | var outputFiles android.WritablePaths |
| 461 | var zipArgs strings.Builder |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 462 | |
Aleks Todorov | 1eb06c4 | 2024-06-03 15:23:56 +0100 | [diff] [blame] | 463 | cmd := g.properties.Cmd.GetOrDefault(ctx, "") |
Colin Cross | f3bfd02 | 2021-09-27 15:15:06 -0700 | [diff] [blame] | 464 | if g.CmdModifier != nil { |
| 465 | cmd = g.CmdModifier(ctx, cmd) |
| 466 | } |
| 467 | |
Liz Kammer | 796921d | 2023-07-11 08:21:41 -0400 | [diff] [blame] | 468 | var extraInputs android.Paths |
Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 469 | // Generate tasks, either from genrule or gensrcs. |
Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 470 | for i, task := range g.taskGenerator(ctx, cmd, srcFiles) { |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 471 | if len(task.out) == 0 { |
| 472 | ctx.ModuleErrorf("must have at least one output file") |
| 473 | return |
Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 476 | // Only handle extra inputs once as these currently are the same across all tasks |
| 477 | if i == 0 { |
| 478 | for name, values := range task.extraInputs { |
| 479 | extraInputs = append(extraInputs, addLabelsForInputs(name, values, []string{})...) |
| 480 | } |
| 481 | } |
| 482 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 483 | // Pick a unique path outside the task.genDir for the sbox manifest textproto, |
| 484 | // a unique rule name, and the user-visible description. |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 485 | var rule *android.RuleBuilder |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 486 | desc := "generate" |
| 487 | name := "generator" |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 488 | if task.useNsjail { |
| 489 | rule = android.NewRuleBuilder(pctx, ctx).Nsjail(task.genDir, android.PathForModuleOut(ctx, "nsjail_build_sandbox")) |
| 490 | } else { |
| 491 | manifestName := "genrule.sbox.textproto" |
| 492 | if task.shards > 0 { |
| 493 | manifestName = "genrule_" + strconv.Itoa(task.shard) + ".sbox.textproto" |
| 494 | desc += " " + strconv.Itoa(task.shard) |
| 495 | name += strconv.Itoa(task.shard) |
| 496 | } else if len(task.out) == 1 { |
| 497 | desc += " " + task.out[0].Base() |
| 498 | } |
| 499 | |
| 500 | manifestPath := android.PathForModuleOut(ctx, manifestName) |
| 501 | |
| 502 | // Use a RuleBuilder to create a rule that runs the command inside an sbox sandbox. |
| 503 | rule = getSandboxedRuleBuilder(ctx, android.NewRuleBuilder(pctx, ctx).Sbox(task.genDir, manifestPath)) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 504 | } |
Justin Yun | 4da4ccc | 2023-07-06 10:56:29 +0900 | [diff] [blame] | 505 | if Bool(g.properties.Write_if_changed) { |
| 506 | rule.Restat() |
| 507 | } |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 508 | cmd := rule.Command() |
| 509 | |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 510 | for _, out := range task.out { |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 511 | addLocationLabel(out.Rel(), outputLocation{out}) |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 514 | rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) { |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 515 | // report the error directly without returning an error to android.Expand to catch multiple errors in a |
| 516 | // single run |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 517 | reportError := func(fmt string, args ...interface{}) (string, error) { |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 518 | ctx.PropertyErrorf("cmd", fmt, args...) |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 519 | return "SOONG_ERROR", nil |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 520 | } |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 521 | |
Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 522 | // 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] | 523 | switch name { |
| 524 | case "location": |
| 525 | if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 { |
| 526 | 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] | 527 | } |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 528 | loc := locationLabels[firstLabel] |
| 529 | paths := loc.Paths(cmd) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 530 | if len(paths) == 0 { |
| 531 | return reportError("default label %q has no files", firstLabel) |
| 532 | } else if len(paths) > 1 { |
| 533 | return reportError("default label %q has multiple files, use $(locations %s) to reference it", |
| 534 | firstLabel, firstLabel) |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 535 | } |
Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 536 | return proptools.ShellEscape(paths[0]), nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 537 | case "in": |
Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 538 | return strings.Join(proptools.ShellEscapeList(cmd.PathsForInputs(srcFiles)), " "), nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 539 | case "out": |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 540 | var sandboxOuts []string |
| 541 | for _, out := range task.out { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 542 | sandboxOuts = append(sandboxOuts, cmd.PathForOutput(out)) |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 543 | } |
Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 544 | return strings.Join(proptools.ShellEscapeList(sandboxOuts), " "), nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 545 | case "genDir": |
Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 546 | return proptools.ShellEscape(cmd.PathForOutput(task.genDir)), nil |
Cole Faust | 78f3c3a | 2024-08-15 17:19:34 -0700 | [diff] [blame] | 547 | case "build_number_file": |
| 548 | if !proptools.Bool(g.properties.Uses_order_only_build_number_file) { |
| 549 | return reportError("to use the $(build_number_file) label, you must set uses_order_only_build_number_file: true") |
| 550 | } |
| 551 | return proptools.ShellEscape(cmd.PathForInput(ctx.Config().BuildNumberFile(ctx))), nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 552 | default: |
| 553 | if strings.HasPrefix(name, "location ") { |
| 554 | label := strings.TrimSpace(strings.TrimPrefix(name, "location ")) |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 555 | if loc, ok := locationLabels[label]; ok { |
| 556 | paths := loc.Paths(cmd) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 557 | if len(paths) == 0 { |
| 558 | return reportError("label %q has no files", label) |
| 559 | } else if len(paths) > 1 { |
| 560 | return reportError("label %q has multiple files, use $(locations %s) to reference it", |
| 561 | label, label) |
| 562 | } |
Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 563 | return proptools.ShellEscape(paths[0]), nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 564 | } else { |
Anton Hansson | bebf526 | 2022-02-23 11:42:38 +0000 | [diff] [blame] | 565 | 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] | 566 | } |
| 567 | } else if strings.HasPrefix(name, "locations ") { |
| 568 | label := strings.TrimSpace(strings.TrimPrefix(name, "locations ")) |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 569 | if loc, ok := locationLabels[label]; ok { |
| 570 | paths := loc.Paths(cmd) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 571 | if len(paths) == 0 { |
| 572 | return reportError("label %q has no files", label) |
| 573 | } |
Cole Faust | ce74a59 | 2023-12-07 14:58:45 -0800 | [diff] [blame] | 574 | return strings.Join(proptools.ShellEscapeList(paths), " "), nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 575 | } else { |
Anton Hansson | bebf526 | 2022-02-23 11:42:38 +0000 | [diff] [blame] | 576 | 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] | 577 | } |
| 578 | } else { |
| 579 | return reportError("unknown variable '$(%s)'", name) |
| 580 | } |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 581 | } |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 582 | }) |
| 583 | |
| 584 | if err != nil { |
| 585 | ctx.PropertyErrorf("cmd", "%s", err.Error()) |
| 586 | return |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 587 | } |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 588 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 589 | g.rawCommands = append(g.rawCommands, rawCommand) |
Bill Peckham | c087be1 | 2020-02-13 15:55:10 -0800 | [diff] [blame] | 590 | |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 591 | cmd.Text(rawCommand) |
Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 592 | cmd.Implicits(srcFiles) // need to be able to reference other srcs |
| 593 | cmd.Implicits(extraInputs) |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 594 | cmd.ImplicitOutputs(task.out) |
| 595 | cmd.Implicits(task.in) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 596 | cmd.ImplicitTools(tools) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 597 | cmd.ImplicitPackagedTools(packagedTools) |
Cole Faust | 78f3c3a | 2024-08-15 17:19:34 -0700 | [diff] [blame] | 598 | if proptools.Bool(g.properties.Uses_order_only_build_number_file) { |
Cole Faust | f23fdc0 | 2024-08-23 15:21:13 -0700 | [diff] [blame] | 599 | if !isModuleInBuildNumberAllowlist(ctx) { |
Cole Faust | 78f3c3a | 2024-08-15 17:19:34 -0700 | [diff] [blame] | 600 | ctx.ModuleErrorf("Only allowlisted modules may use uses_order_only_build_number_file: true") |
| 601 | } |
| 602 | cmd.OrderOnly(ctx.Config().BuildNumberFile(ctx)) |
| 603 | } |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 604 | |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 605 | if task.useNsjail { |
Inseob Kim | 76e1985 | 2024-10-10 17:57:22 +0900 | [diff] [blame] | 606 | for _, input := range task.dirSrcs { |
Inseob Kim | 93036a5 | 2024-10-25 17:02:21 +0900 | [diff] [blame^] | 607 | cmd.ImplicitDirectory(input) |
| 608 | // TODO(b/375551969): remove glob |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 609 | if paths, err := ctx.GlobWithDeps(filepath.Join(input.String(), "**/*"), nil); err == nil { |
| 610 | rule.NsjailImplicits(android.PathsForSource(ctx, paths)) |
Inseob Kim | 76e1985 | 2024-10-10 17:57:22 +0900 | [diff] [blame] | 611 | } else { |
| 612 | ctx.PropertyErrorf("dir_srcs", "can't glob %q", input.String()) |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 617 | // Create the rule to run the genrule command inside sbox. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 618 | rule.Build(name, desc) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 619 | |
| 620 | if len(task.copyTo) > 0 { |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 621 | // If copyTo is set, multiple shards need to be copied into a single directory. |
| 622 | // task.out contains the per-shard paths, and copyTo contains the corresponding |
| 623 | // final path. The files need to be copied into the final directory by a |
| 624 | // single rule so it can remove the directory before it starts to ensure no |
| 625 | // old files remain. zipsync already does this, so build up zipArgs that |
| 626 | // zip all the per-shard directories into a single zip. |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 627 | outputFiles = append(outputFiles, task.copyTo...) |
| 628 | copyFrom = append(copyFrom, task.out.Paths()...) |
| 629 | zipArgs.WriteString(" -C " + task.genDir.String()) |
| 630 | zipArgs.WriteString(android.JoinWithPrefix(task.out.Strings(), " -f ")) |
| 631 | } else { |
| 632 | outputFiles = append(outputFiles, task.out...) |
| 633 | } |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 636 | if len(copyFrom) > 0 { |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 637 | // Create a rule that zips all the per-shard directories into a single zip and then |
| 638 | // uses zipsync to unzip it into the final directory. |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 639 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 640 | Rule: gensrcsMerge, |
| 641 | Implicits: copyFrom, |
| 642 | Outputs: outputFiles, |
| 643 | Description: "merge shards", |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 644 | Args: map[string]string{ |
| 645 | "zipArgs": zipArgs.String(), |
| 646 | "tmpZip": android.PathForModuleGen(ctx, g.subDir+".zip").String(), |
| 647 | "genDir": android.PathForModuleGen(ctx, g.subDir).String(), |
| 648 | }, |
| 649 | }) |
Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 652 | g.outputFiles = outputFiles.Paths() |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 653 | } |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 654 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 655 | func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 656 | g.generateCommonBuildActions(ctx) |
| 657 | |
| 658 | // For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of |
| 659 | // the genrules on AOSP. That will make things simpler to look at the graph in the common |
| 660 | // case. For larger sets of outputs, inject a phony target in between to limit ninja file |
| 661 | // growth. |
| 662 | if len(g.outputFiles) <= 6 { |
| 663 | g.outputDeps = g.outputFiles |
| 664 | } else { |
| 665 | phonyFile := android.PathForModuleGen(ctx, "genrule-phony") |
| 666 | ctx.Build(pctx, android.BuildParams{ |
| 667 | Rule: blueprint.Phony, |
| 668 | Output: phonyFile, |
| 669 | Inputs: g.outputFiles, |
| 670 | }) |
| 671 | g.outputDeps = android.Paths{phonyFile} |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 672 | } |
mrziwang | 4514ef2 | 2024-06-07 13:31:48 -0700 | [diff] [blame] | 673 | |
| 674 | g.setOutputFiles(ctx) |
Cole Faust | 481b669 | 2024-10-11 16:25:07 -0700 | [diff] [blame] | 675 | |
| 676 | if ctx.Os() == android.Windows { |
| 677 | // Make doesn't support windows: |
| 678 | // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/module_arch_supported.mk;l=66;drc=f264690860bb6ee7762784d6b7201aae057ba6f2 |
| 679 | g.HideFromMake() |
| 680 | } |
mrziwang | 4514ef2 | 2024-06-07 13:31:48 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | func (g *Module) setOutputFiles(ctx android.ModuleContext) { |
| 684 | if len(g.outputFiles) == 0 { |
| 685 | return |
| 686 | } |
| 687 | ctx.SetOutputFiles(g.outputFiles, "") |
| 688 | // non-empty-string-tag should match one of the outputs |
| 689 | for _, files := range g.outputFiles { |
| 690 | ctx.SetOutputFiles(android.Paths{files}, files.Rel()) |
| 691 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 692 | } |
| 693 | |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 694 | // Collect information for opening IDE project files in java/jdeps.go. |
Cole Faust | b36d31d | 2024-08-27 16:04:28 -0700 | [diff] [blame] | 695 | func (g *Module) IDEInfo(ctx android.BaseModuleContext, dpInfo *android.IdeInfo) { |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 696 | dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...) |
Cole Faust | f966e38 | 2024-10-08 16:17:56 -0700 | [diff] [blame] | 697 | for _, src := range g.properties.Srcs.GetOrDefault(ctx, nil) { |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 698 | if strings.HasPrefix(src, ":") { |
| 699 | src = strings.Trim(src, ":") |
| 700 | dpInfo.Deps = append(dpInfo.Deps, src) |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 705 | func (g *Module) AndroidMk() android.AndroidMkData { |
| 706 | return android.AndroidMkData{ |
Anton Hansson | 72f1849 | 2020-10-30 16:34:45 +0000 | [diff] [blame] | 707 | Class: "ETC", |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 708 | OutputFile: android.OptionalPathForPath(g.outputFiles[0]), |
| 709 | SubName: g.subName, |
| 710 | Extra: []android.AndroidMkExtraFunc{ |
| 711 | func(w io.Writer, outputFile android.Path) { |
Anton Hansson | 72f1849 | 2020-10-30 16:34:45 +0000 | [diff] [blame] | 712 | fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true") |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 713 | }, |
| 714 | }, |
| 715 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 716 | android.WriteAndroidMkData(w, data) |
| 717 | if data.SubName != "" { |
| 718 | fmt.Fprintln(w, ".PHONY:", name) |
| 719 | fmt.Fprintln(w, name, ":", name+g.subName) |
| 720 | } |
| 721 | }, |
| 722 | } |
| 723 | } |
| 724 | |
Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 725 | var _ android.ApexModule = (*Module)(nil) |
| 726 | |
| 727 | // Implements android.ApexModule |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 728 | func (g *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, |
| 729 | sdkVersion android.ApiLevel) error { |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 730 | // Because generated outputs are checked by client modules(e.g. cc_library, ...) |
| 731 | // we can safely ignore the check here. |
| 732 | return nil |
| 733 | } |
| 734 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 735 | func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 736 | module := &Module{ |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 737 | taskGenerator: taskGenerator, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 740 | module.AddProperties(props...) |
| 741 | module.AddProperties(&module.properties) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 742 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 743 | module.ImageInterface = noopImageInterface{} |
| 744 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 745 | return module |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 748 | type noopImageInterface struct{} |
| 749 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 750 | func (x noopImageInterface) ImageMutatorBegin(android.ImageInterfaceContext) {} |
| 751 | func (x noopImageInterface) VendorVariantNeeded(android.ImageInterfaceContext) bool { return false } |
| 752 | func (x noopImageInterface) ProductVariantNeeded(android.ImageInterfaceContext) bool { return false } |
| 753 | func (x noopImageInterface) CoreVariantNeeded(android.ImageInterfaceContext) bool { return false } |
| 754 | func (x noopImageInterface) RamdiskVariantNeeded(android.ImageInterfaceContext) bool { return false } |
| 755 | func (x noopImageInterface) VendorRamdiskVariantNeeded(android.ImageInterfaceContext) bool { |
| 756 | return false |
| 757 | } |
| 758 | func (x noopImageInterface) DebugRamdiskVariantNeeded(android.ImageInterfaceContext) bool { |
| 759 | return false |
| 760 | } |
| 761 | func (x noopImageInterface) RecoveryVariantNeeded(android.ImageInterfaceContext) bool { return false } |
| 762 | func (x noopImageInterface) ExtraImageVariations(ctx android.ImageInterfaceContext) []string { |
| 763 | return nil |
| 764 | } |
| 765 | func (x noopImageInterface) SetImageVariation(ctx android.ImageInterfaceContext, variation string) { |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 766 | } |
| 767 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 768 | func NewGenSrcs() *Module { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 769 | properties := &genSrcsProperties{} |
| 770 | |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 771 | // finalSubDir is the name of the subdirectory that output files will be generated into. |
| 772 | // It is used so that per-shard directories can be placed alongside it an then finally |
| 773 | // merged into it. |
| 774 | const finalSubDir = "gensrcs" |
| 775 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 776 | taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask { |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 777 | shardSize := defaultShardSize |
| 778 | if s := properties.Shard_size; s != nil { |
| 779 | shardSize = int(*s) |
| 780 | } |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 781 | |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 782 | // gensrcs rules can easily hit command line limits by repeating the command for |
| 783 | // every input file. Shard the input files into groups. |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 784 | shards := android.ShardPaths(srcFiles, shardSize) |
| 785 | var generateTasks []generateTask |
Colin Cross | baccf5b | 2018-02-21 14:07:48 -0800 | [diff] [blame] | 786 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 787 | for i, shard := range shards { |
| 788 | var commands []string |
| 789 | var outFiles android.WritablePaths |
| 790 | var copyTo android.WritablePaths |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 791 | |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 792 | // When sharding is enabled (i.e. len(shards) > 1), the sbox rules for each |
| 793 | // shard will be write to their own directories and then be merged together |
| 794 | // into finalSubDir. If sharding is not enabled (i.e. len(shards) == 1), |
| 795 | // the sbox rule will write directly to finalSubDir. |
| 796 | genSubDir := finalSubDir |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 797 | if len(shards) > 1 { |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 798 | genSubDir = strconv.Itoa(i) |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 799 | } |
| 800 | |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 801 | genDir := android.PathForModuleGen(ctx, genSubDir) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 802 | // TODO(ccross): this RuleBuilder is a hack to be able to call |
| 803 | // rule.Command().PathForOutput. Replace this with passing the rule into the |
| 804 | // generator. |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 805 | rule := getSandboxedRuleBuilder(ctx, android.NewRuleBuilder(pctx, ctx).Sbox(genDir, nil)) |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 806 | |
Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 807 | for _, in := range shard { |
yangbill | 6d032dd | 2024-04-18 03:05:49 +0000 | [diff] [blame] | 808 | outFile := android.GenPathWithExtAndTrimExt(ctx, finalSubDir, in, String(properties.Output_extension), String(properties.Trim_extension)) |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 809 | |
| 810 | // If sharding is enabled, then outFile is the path to the output file in |
| 811 | // the shard directory, and copyTo is the path to the output file in the |
| 812 | // final directory. |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 813 | if len(shards) > 1 { |
yangbill | 6d032dd | 2024-04-18 03:05:49 +0000 | [diff] [blame] | 814 | shardFile := android.GenPathWithExtAndTrimExt(ctx, genSubDir, in, String(properties.Output_extension), String(properties.Trim_extension)) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 815 | copyTo = append(copyTo, outFile) |
| 816 | outFile = shardFile |
| 817 | } |
| 818 | |
| 819 | outFiles = append(outFiles, outFile) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 820 | |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 821 | // pre-expand the command line to replace $in and $out with references to |
| 822 | // a single input and output file. |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 823 | command, err := android.Expand(rawCommand, func(name string) (string, error) { |
| 824 | switch name { |
| 825 | case "in": |
| 826 | return in.String(), nil |
| 827 | case "out": |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 828 | return rule.Command().PathForOutput(outFile), nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 829 | default: |
| 830 | return "$(" + name + ")", nil |
| 831 | } |
| 832 | }) |
| 833 | if err != nil { |
| 834 | ctx.PropertyErrorf("cmd", err.Error()) |
| 835 | } |
| 836 | |
| 837 | // escape the command in case for example it contains '#', an odd number of '"', etc |
| 838 | command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command)) |
| 839 | commands = append(commands, command) |
| 840 | } |
| 841 | fullCommand := strings.Join(commands, " && ") |
| 842 | |
| 843 | generateTasks = append(generateTasks, generateTask{ |
Cole Faust | 5549257 | 2024-01-25 18:00:33 -0800 | [diff] [blame] | 844 | in: shard, |
| 845 | out: outFiles, |
| 846 | copyTo: copyTo, |
| 847 | genDir: genDir, |
| 848 | cmd: fullCommand, |
| 849 | shard: i, |
| 850 | shards: len(shards), |
Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 851 | extraInputs: map[string][]string{ |
| 852 | "data": properties.Data, |
| 853 | }, |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 854 | }) |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 855 | } |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 856 | |
| 857 | return generateTasks |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 860 | g := generatorFactory(taskGenerator, properties) |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 861 | g.subDir = finalSubDir |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 862 | return g |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 865 | func GenSrcsFactory() android.Module { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 866 | m := NewGenSrcs() |
| 867 | android.InitAndroidModule(m) |
Colin Cross | 483b4c4 | 2024-05-09 13:08:02 -0700 | [diff] [blame] | 868 | android.InitDefaultableModule(m) |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 869 | return m |
| 870 | } |
| 871 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 872 | type genSrcsProperties struct { |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 873 | // extension that will be substituted for each output file |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 874 | Output_extension *string |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 875 | |
| 876 | // maximum number of files that will be passed on a single command line. |
| 877 | Shard_size *int64 |
Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 878 | |
| 879 | // Additional files needed for build that are not tooling related. |
| 880 | Data []string `android:"path"` |
yangbill | 6d032dd | 2024-04-18 03:05:49 +0000 | [diff] [blame] | 881 | |
| 882 | // Trim the matched extension for each input file, and it should start with ".". |
| 883 | Trim_extension *string |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Evgenii Stepanov | f47c90d | 2020-12-02 18:55:09 -0800 | [diff] [blame] | 886 | const defaultShardSize = 50 |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 887 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 888 | func NewGenRule() *Module { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 889 | properties := &genRuleProperties{} |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 890 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 891 | taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask { |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 892 | useNsjail := Bool(properties.Use_nsjail) |
| 893 | |
Inseob Kim | 76e1985 | 2024-10-10 17:57:22 +0900 | [diff] [blame] | 894 | dirSrcs := android.DirectoryPathsForModuleSrc(ctx, properties.Dir_srcs) |
| 895 | if len(dirSrcs) > 0 && !useNsjail { |
| 896 | ctx.PropertyErrorf("dir_srcs", "can't use dir_srcs if use_nsjail is false") |
| 897 | return nil |
| 898 | } |
| 899 | |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 900 | outs := make(android.WritablePaths, len(properties.Out)) |
| 901 | for i, out := range properties.Out { |
Cole Faust | 5549257 | 2024-01-25 18:00:33 -0800 | [diff] [blame] | 902 | outs[i] = android.PathForModuleGen(ctx, out) |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 903 | } |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 904 | return []generateTask{{ |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 905 | in: srcFiles, |
| 906 | out: outs, |
| 907 | genDir: android.PathForModuleGen(ctx), |
| 908 | cmd: rawCommand, |
| 909 | useNsjail: useNsjail, |
Inseob Kim | 76e1985 | 2024-10-10 17:57:22 +0900 | [diff] [blame] | 910 | dirSrcs: dirSrcs, |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 911 | }} |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 912 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 913 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 914 | return generatorFactory(taskGenerator, properties) |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 917 | func GenRuleFactory() android.Module { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 918 | m := NewGenRule() |
| 919 | android.InitAndroidModule(m) |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 920 | android.InitDefaultableModule(m) |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 921 | return m |
| 922 | } |
| 923 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 924 | type genRuleProperties struct { |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 925 | Use_nsjail *bool |
| 926 | |
Inseob Kim | 76e1985 | 2024-10-10 17:57:22 +0900 | [diff] [blame] | 927 | // List of input directories. Can be set only when use_nsjail is true. Currently, usage of |
| 928 | // dir_srcs is limited only to Trusty build. |
| 929 | Dir_srcs []string `android:"path"` |
| 930 | |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 931 | // names of the output files that will be generated |
kellyhung | 750334a | 2024-03-14 01:03:49 +0800 | [diff] [blame] | 932 | Out []string `android:"arch_variant"` |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 933 | } |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 934 | |
| 935 | var Bool = proptools.Bool |
| 936 | var String = proptools.String |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 937 | |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 938 | // Defaults |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 939 | type Defaults struct { |
| 940 | android.ModuleBase |
| 941 | android.DefaultsModuleBase |
| 942 | } |
| 943 | |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 944 | func defaultsFactory() android.Module { |
| 945 | return DefaultsFactory() |
| 946 | } |
| 947 | |
| 948 | func DefaultsFactory(props ...interface{}) android.Module { |
| 949 | module := &Defaults{} |
| 950 | |
| 951 | module.AddProperties(props...) |
| 952 | module.AddProperties( |
| 953 | &generatorProperties{}, |
| 954 | &genRuleProperties{}, |
| 955 | ) |
| 956 | |
| 957 | android.InitDefaultsModule(module) |
| 958 | |
| 959 | return module |
| 960 | } |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 961 | |
Yu Liu | e7f7cbf | 2023-06-13 18:50:03 +0000 | [diff] [blame] | 962 | var sandboxingAllowlistKey = android.NewOnceKey("genruleSandboxingAllowlistKey") |
| 963 | |
| 964 | type sandboxingAllowlistSets struct { |
| 965 | sandboxingDenyModuleSet map[string]bool |
Yu Liu | e7f7cbf | 2023-06-13 18:50:03 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | func getSandboxingAllowlistSets(ctx android.PathContext) *sandboxingAllowlistSets { |
| 969 | return ctx.Config().Once(sandboxingAllowlistKey, func() interface{} { |
| 970 | sandboxingDenyModuleSet := map[string]bool{} |
Yu Liu | e7f7cbf | 2023-06-13 18:50:03 +0000 | [diff] [blame] | 971 | |
Cole Faust | 5549257 | 2024-01-25 18:00:33 -0800 | [diff] [blame] | 972 | android.AddToStringSet(sandboxingDenyModuleSet, SandboxingDenyModuleList) |
Yu Liu | e7f7cbf | 2023-06-13 18:50:03 +0000 | [diff] [blame] | 973 | return &sandboxingAllowlistSets{ |
| 974 | sandboxingDenyModuleSet: sandboxingDenyModuleSet, |
Yu Liu | e7f7cbf | 2023-06-13 18:50:03 +0000 | [diff] [blame] | 975 | } |
| 976 | }).(*sandboxingAllowlistSets) |
| 977 | } |
Liz Kammer | 0db0e34 | 2023-07-18 11:39:30 -0400 | [diff] [blame] | 978 | |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 979 | func getSandboxedRuleBuilder(ctx android.ModuleContext, r *android.RuleBuilder) *android.RuleBuilder { |
Yu Liu | 45d6af5 | 2023-05-24 23:10:18 +0000 | [diff] [blame] | 980 | if !ctx.DeviceConfig().GenruleSandboxing() { |
| 981 | return r.SandboxTools() |
| 982 | } |
Yu Liu | e7f7cbf | 2023-06-13 18:50:03 +0000 | [diff] [blame] | 983 | sandboxingAllowlistSets := getSandboxingAllowlistSets(ctx) |
Cole Faust | e762b94 | 2024-03-15 12:46:14 -0700 | [diff] [blame] | 984 | if sandboxingAllowlistSets.sandboxingDenyModuleSet[ctx.ModuleName()] { |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 985 | return r.SandboxTools() |
| 986 | } |
| 987 | return r.SandboxInputs() |
| 988 | } |