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