| 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 | 3d68051 | 2020-11-13 16:23:53 -0800 | [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 |  | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 28 | "android/soong/bazel/cquery" | 
| Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 29 |  | 
| Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 30 | "github.com/google/blueprint" | 
| Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 31 | "github.com/google/blueprint/bootstrap" | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 32 | "github.com/google/blueprint/proptools" | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 33 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 34 | "android/soong/android" | 
| Jingwen Chen | 30f5aaa | 2020-11-19 05:38:02 -0500 | [diff] [blame] | 35 | "android/soong/bazel" | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 36 | ) | 
|  | 37 |  | 
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 38 | func init() { | 
| Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 39 | RegisterGenruleBuildComponents(android.InitRegistrationContext) | 
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 40 | } | 
| Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 41 |  | 
| Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 42 | // Test fixture preparer that will register most genrule build components. | 
|  | 43 | // | 
|  | 44 | // Singletons and mutators should only be added here if they are needed for a majority of genrule | 
|  | 45 | // module types, otherwise they should be added under a separate preparer to allow them to be | 
|  | 46 | // selected only when needed to reduce test execution time. | 
|  | 47 | // | 
|  | 48 | // Module types do not have much of an overhead unless they are used so this should include as many | 
|  | 49 | // module types as possible. The exceptions are those module types that require mutators and/or | 
|  | 50 | // singletons in order to function in which case they should be kept together in a separate | 
|  | 51 | // preparer. | 
|  | 52 | var PrepareForTestWithGenRuleBuildComponents = android.GroupFixturePreparers( | 
|  | 53 | android.FixtureRegisterWithContext(RegisterGenruleBuildComponents), | 
|  | 54 | ) | 
|  | 55 |  | 
|  | 56 | // Prepare a fixture to use all genrule module types, mutators and singletons fully. | 
|  | 57 | // | 
|  | 58 | // This should only be used by tests that want to run with as much of the build enabled as possible. | 
|  | 59 | var PrepareForIntegrationTestWithGenrule = android.GroupFixturePreparers( | 
|  | 60 | PrepareForTestWithGenRuleBuildComponents, | 
|  | 61 | ) | 
|  | 62 |  | 
| Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 63 | func RegisterGenruleBuildComponents(ctx android.RegistrationContext) { | 
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 64 | ctx.RegisterModuleType("genrule_defaults", defaultsFactory) | 
|  | 65 |  | 
|  | 66 | ctx.RegisterModuleType("gensrcs", GenSrcsFactory) | 
|  | 67 | ctx.RegisterModuleType("genrule", GenRuleFactory) | 
|  | 68 |  | 
|  | 69 | ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) { | 
|  | 70 | ctx.BottomUp("genrule_tool_deps", toolDepsMutator).Parallel() | 
|  | 71 | }) | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 74 | var ( | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 75 | pctx = android.NewPackageContext("android/soong/genrule") | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 76 |  | 
| Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 77 | // Used by gensrcs when there is more than 1 shard to merge the outputs | 
|  | 78 | // of each shard into a zip file. | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 79 | gensrcsMerge = pctx.AndroidStaticRule("gensrcsMerge", blueprint.RuleParams{ | 
|  | 80 | Command:        "${soongZip} -o ${tmpZip} @${tmpZip}.rsp && ${zipSync} -d ${genDir} ${tmpZip}", | 
|  | 81 | CommandDeps:    []string{"${soongZip}", "${zipSync}"}, | 
|  | 82 | Rspfile:        "${tmpZip}.rsp", | 
|  | 83 | RspfileContent: "${zipArgs}", | 
|  | 84 | }, "tmpZip", "genDir", "zipArgs") | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 85 | ) | 
|  | 86 |  | 
| Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 87 | func init() { | 
| Dan Willemsen | ddf504c | 2019-08-09 16:21:29 -0700 | [diff] [blame] | 88 | pctx.Import("android/soong/android") | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 89 |  | 
|  | 90 | pctx.HostBinToolVariable("soongZip", "soong_zip") | 
|  | 91 | pctx.HostBinToolVariable("zipSync", "zipsync") | 
| Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 94 | type SourceFileGenerator interface { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 95 | GeneratedSourceFiles() android.Paths | 
| Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 96 | GeneratedHeaderDirs() android.Paths | 
| Dan Willemsen | 9da9d49 | 2018-02-21 18:28:18 -0800 | [diff] [blame] | 97 | GeneratedDeps() android.Paths | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 100 | // Alias for android.HostToolProvider | 
|  | 101 | // Deprecated: use android.HostToolProvider instead. | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 102 | type HostToolProvider interface { | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 103 | android.HostToolProvider | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 104 | } | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 105 |  | 
| Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 106 | type hostToolDependencyTag struct { | 
|  | 107 | blueprint.BaseDependencyTag | 
| Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 108 | android.LicenseAnnotationToolchainDependencyTag | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 109 | label string | 
| Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 110 | } | 
| Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 111 |  | 
|  | 112 | func (t hostToolDependencyTag) AllowDisabledModuleDependency(target android.Module) bool { | 
|  | 113 | // Allow depending on a disabled module if it's replaced by a prebuilt | 
|  | 114 | // counterpart. We get the prebuilt through android.PrebuiltGetPreferred in | 
|  | 115 | // GenerateAndroidBuildActions. | 
|  | 116 | return target.IsReplacedByPrebuilt() | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | var _ android.AllowDisabledModuleDependency = (*hostToolDependencyTag)(nil) | 
|  | 120 |  | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 121 | type generatorProperties struct { | 
| Spandan Das | 93e9599 | 2021-07-29 18:26:39 +0000 | [diff] [blame] | 122 | // 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] | 123 | // | 
|  | 124 | // Available variables for substitution: | 
|  | 125 | // | 
| Spandan Das | 93e9599 | 2021-07-29 18:26:39 +0000 | [diff] [blame] | 126 | //  $(location): the path to the first entry in tools or tool_files. | 
|  | 127 | //  $(location <label>): the path to the tool, tool_file, input or output with name <label>. Use $(location) if <label> refers to a rule that outputs exactly one file. | 
|  | 128 | //  $(locations <label>): the paths to the tools, tool_files, inputs or outputs with name <label>. Use $(locations) if <label> refers to a rule that outputs two or more files. | 
|  | 129 | //  $(in): one or more input files. | 
|  | 130 | //  $(out): a single output file. | 
|  | 131 | //  $(depfile): a file to which dependencies will be written, if the depfile property is set to true. | 
|  | 132 | //  $(genDir): the sandbox directory for this tool; contains $(out). | 
| Colin Cross | 2296f5b | 2017-10-17 21:38:14 -0700 | [diff] [blame] | 133 | //  $$: a literal $ | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 134 | Cmd *string | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 135 |  | 
| Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 136 | // Enable reading a file containing dependencies in gcc format after the command completes | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 137 | Depfile *bool | 
| Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 138 |  | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 139 | // 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] | 140 | // prebuilts or scripts that do not need a module to build them. | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 141 | Tools []string | 
| Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 142 |  | 
| Sam Delmerico | f877563 | 2023-08-14 23:45:41 +0000 | [diff] [blame] | 143 | // Local files that are used by the tool | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 144 | Tool_files []string `android:"path"` | 
| Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 145 |  | 
|  | 146 | // List of directories to export generated headers from | 
|  | 147 | Export_include_dirs []string | 
| Colin Cross | 708c424 | 2017-01-13 18:05:49 -0800 | [diff] [blame] | 148 |  | 
|  | 149 | // list of input files | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 150 | Srcs []string `android:"path,arch_variant"` | 
| Dan Willemsen | eefa026 | 2018-11-17 14:01:18 -0800 | [diff] [blame] | 151 |  | 
|  | 152 | // input files to exclude | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 153 | Exclude_srcs []string `android:"path,arch_variant"` | 
| Justin Yun | 4da4ccc | 2023-07-06 10:56:29 +0900 | [diff] [blame] | 154 |  | 
|  | 155 | // Enable restat to update the output only if the output is changed | 
|  | 156 | Write_if_changed *bool | 
| Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 157 | } | 
| Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 158 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 159 | type Module struct { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 160 | android.ModuleBase | 
| Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 161 | android.DefaultableModuleBase | 
| Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 162 | android.BazelModuleBase | 
| Jiyong Park | fc752ca | 2019-06-12 13:27:29 +0900 | [diff] [blame] | 163 | android.ApexModuleBase | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 164 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 165 | // For other packages to make their own genrules with extra | 
|  | 166 | // properties | 
|  | 167 | Extra interface{} | 
| Colin Cross | f3bfd02 | 2021-09-27 15:15:06 -0700 | [diff] [blame] | 168 |  | 
|  | 169 | // CmdModifier can be set by wrappers around genrule to modify the command, for example to | 
|  | 170 | // prefix environment variables to it. | 
|  | 171 | CmdModifier func(ctx android.ModuleContext, cmd string) string | 
|  | 172 |  | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 173 | android.ImageInterface | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 174 |  | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 175 | properties generatorProperties | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 176 |  | 
| Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 177 | // For the different tasks that genrule and gensrc generate. genrule will | 
|  | 178 | // generate 1 task, and gensrc will generate 1 or more tasks based on the | 
|  | 179 | // number of shards the input files are sharded into. | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 180 | taskGenerator taskFunc | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 181 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 182 | rule        blueprint.Rule | 
|  | 183 | rawCommands []string | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 184 |  | 
| Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 185 | exportedIncludeDirs android.Paths | 
| Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 186 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 187 | outputFiles android.Paths | 
| Dan Willemsen | 9da9d49 | 2018-02-21 18:28:18 -0800 | [diff] [blame] | 188 | outputDeps  android.Paths | 
| Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 189 |  | 
|  | 190 | subName string | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 191 | subDir  string | 
| bralee | 1fbf440 | 2020-05-21 10:11:59 +0800 | [diff] [blame] | 192 |  | 
|  | 193 | // Collect the module directory for IDE info in java/jdeps.go. | 
|  | 194 | modulePaths []string | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 195 | } | 
|  | 196 |  | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 197 | var _ android.MixedBuildBuildable = (*Module)(nil) | 
|  | 198 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 199 | type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 200 |  | 
|  | 201 | type generateTask struct { | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 202 | in          android.Paths | 
|  | 203 | out         android.WritablePaths | 
|  | 204 | depFile     android.WritablePath | 
|  | 205 | copyTo      android.WritablePaths // For gensrcs to set on gensrcsMerge rule. | 
|  | 206 | genDir      android.WritablePath | 
|  | 207 | extraTools  android.Paths // dependencies on tools used by the generator | 
|  | 208 | extraInputs map[string][]string | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 209 |  | 
| Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 210 | cmd string | 
|  | 211 | // For gensrsc sharding. | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 212 | shard  int | 
|  | 213 | shards int | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 214 | } | 
|  | 215 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 216 | func (g *Module) GeneratedSourceFiles() android.Paths { | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 217 | return g.outputFiles | 
|  | 218 | } | 
|  | 219 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 220 | func (g *Module) Srcs() android.Paths { | 
| Nan Zhang | e42777a | 2018-03-27 16:19:42 -0700 | [diff] [blame] | 221 | return append(android.Paths{}, g.outputFiles...) | 
| Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 222 | } | 
|  | 223 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 224 | func (g *Module) GeneratedHeaderDirs() android.Paths { | 
| Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 225 | return g.exportedIncludeDirs | 
| Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 226 | } | 
|  | 227 |  | 
| Dan Willemsen | 9da9d49 | 2018-02-21 18:28:18 -0800 | [diff] [blame] | 228 | func (g *Module) GeneratedDeps() android.Paths { | 
|  | 229 | return g.outputDeps | 
|  | 230 | } | 
|  | 231 |  | 
| Jooyung Han | 8c7e3ed | 2021-06-28 17:35:58 +0900 | [diff] [blame] | 232 | func (g *Module) OutputFiles(tag string) (android.Paths, error) { | 
|  | 233 | if tag == "" { | 
|  | 234 | return append(android.Paths{}, g.outputFiles...), nil | 
|  | 235 | } | 
|  | 236 | // otherwise, tag should match one of outputs | 
|  | 237 | for _, outputFile := range g.outputFiles { | 
|  | 238 | if outputFile.Rel() == tag { | 
|  | 239 | return android.Paths{outputFile}, nil | 
|  | 240 | } | 
|  | 241 | } | 
|  | 242 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | var _ android.SourceFileProducer = (*Module)(nil) | 
|  | 246 | var _ android.OutputFileProducer = (*Module)(nil) | 
|  | 247 |  | 
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 248 | func toolDepsMutator(ctx android.BottomUpMutatorContext) { | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 249 | if g, ok := ctx.Module().(*Module); ok { | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 250 | for _, tool := range g.properties.Tools { | 
|  | 251 | tag := hostToolDependencyTag{label: tool} | 
|  | 252 | if m := android.SrcIsModule(tool); m != "" { | 
|  | 253 | tool = m | 
|  | 254 | } | 
| Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 255 | ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), tag, tool) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 256 | } | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 257 | } | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 258 | } | 
|  | 259 |  | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 260 | func (g *Module) ProcessBazelQueryResponse(ctx android.ModuleContext) { | 
|  | 261 | g.generateCommonBuildActions(ctx) | 
|  | 262 |  | 
|  | 263 | label := g.GetBazelLabel(ctx, g) | 
| Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 264 | bazelCtx := ctx.Config().BazelContext | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 265 | filePaths, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx)) | 
|  | 266 | if err != nil { | 
|  | 267 | ctx.ModuleErrorf(err.Error()) | 
|  | 268 | return | 
| Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 269 | } | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 270 |  | 
|  | 271 | var bazelOutputFiles android.Paths | 
|  | 272 | exportIncludeDirs := map[string]bool{} | 
|  | 273 | for _, bazelOutputFile := range filePaths { | 
| Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame] | 274 | bazelOutputFiles = append(bazelOutputFiles, android.PathForBazelOutRelative(ctx, ctx.ModuleDir(), bazelOutputFile)) | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 275 | exportIncludeDirs[filepath.Dir(bazelOutputFile)] = true | 
|  | 276 | } | 
|  | 277 | g.outputFiles = bazelOutputFiles | 
|  | 278 | g.outputDeps = bazelOutputFiles | 
|  | 279 | for includePath, _ := range exportIncludeDirs { | 
|  | 280 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForBazelOut(ctx, includePath)) | 
|  | 281 | } | 
| Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 282 | } | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 283 |  | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 284 | // generateCommonBuildActions contains build action generation logic | 
|  | 285 | // common to both the mixed build case and the legacy case of genrule processing. | 
|  | 286 | // To fully support genrule in mixed builds, the contents of this function should | 
|  | 287 | // approach zero; there should be no genrule action registration done directly | 
|  | 288 | // by Soong logic in the mixed-build case. | 
|  | 289 | func (g *Module) generateCommonBuildActions(ctx android.ModuleContext) { | 
| Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 290 | g.subName = ctx.ModuleSubDir() | 
|  | 291 |  | 
| bralee | 1fbf440 | 2020-05-21 10:11:59 +0800 | [diff] [blame] | 292 | // Collect the module directory for IDE info in java/jdeps.go. | 
|  | 293 | g.modulePaths = append(g.modulePaths, ctx.ModuleDir()) | 
|  | 294 |  | 
| Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 295 | if len(g.properties.Export_include_dirs) > 0 { | 
|  | 296 | for _, dir := range g.properties.Export_include_dirs { | 
|  | 297 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 298 | android.PathForModuleGen(ctx, g.subDir, ctx.ModuleDir(), dir)) | 
| Liz Kammer | d38c87c | 2023-07-17 09:58:50 -0400 | [diff] [blame] | 299 | // Also export without ModuleDir for consistency with Export_include_dirs not being set | 
|  | 300 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, | 
|  | 301 | android.PathForModuleGen(ctx, g.subDir, dir)) | 
| Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 302 | } | 
|  | 303 | } else { | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 304 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, g.subDir)) | 
| Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 305 | } | 
| Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 306 |  | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 307 | locationLabels := map[string]location{} | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 308 | firstLabel := "" | 
|  | 309 |  | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 310 | addLocationLabel := func(label string, loc location) { | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 311 | if firstLabel == "" { | 
|  | 312 | firstLabel = label | 
|  | 313 | } | 
|  | 314 | if _, exists := locationLabels[label]; !exists { | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 315 | locationLabels[label] = loc | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 316 | } else { | 
| Anton Hansson | 7cd41e5 | 2021-10-08 16:13:10 +0100 | [diff] [blame] | 317 | 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] | 318 | label, locationLabels[label], loc) | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 319 | } | 
|  | 320 | } | 
| Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 321 |  | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 322 | var tools android.Paths | 
|  | 323 | var packagedTools []android.PackagingSpec | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 324 | if len(g.properties.Tools) > 0 { | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 325 | seenTools := make(map[string]bool) | 
|  | 326 |  | 
| Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 327 | ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) { | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 328 | switch tag := ctx.OtherModuleDependencyTag(module).(type) { | 
|  | 329 | case hostToolDependencyTag: | 
| Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 330 | tool := ctx.OtherModuleName(module) | 
| Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 331 | if m, ok := module.(android.Module); ok { | 
|  | 332 | // Necessary to retrieve any prebuilt replacement for the tool, since | 
|  | 333 | // toolDepsMutator runs too late for the prebuilt mutators to have | 
|  | 334 | // replaced the dependency. | 
|  | 335 | module = android.PrebuiltGetPreferred(ctx, m) | 
|  | 336 | } | 
| Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 337 |  | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 338 | switch t := module.(type) { | 
|  | 339 | case android.HostToolProvider: | 
|  | 340 | // A HostToolProvider provides the path to a tool, which will be copied | 
|  | 341 | // into the sandbox. | 
| Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 342 | if !t.(android.Module).Enabled() { | 
| Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 343 | if ctx.Config().AllowMissingDependencies() { | 
| Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 344 | ctx.AddMissingDependencies([]string{tool}) | 
|  | 345 | } else { | 
|  | 346 | ctx.ModuleErrorf("depends on disabled module %q", tool) | 
|  | 347 | } | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 348 | return | 
| Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 349 | } | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 350 | path := t.HostToolPath() | 
|  | 351 | if !path.Valid() { | 
|  | 352 | ctx.ModuleErrorf("host tool %q missing output file", tool) | 
|  | 353 | return | 
|  | 354 | } | 
|  | 355 | if specs := t.TransitivePackagingSpecs(); specs != nil { | 
|  | 356 | // If the HostToolProvider has PackgingSpecs, which are definitions of the | 
|  | 357 | // required relative locations of the tool and its dependencies, use those | 
|  | 358 | // instead.  They will be copied to those relative locations in the sbox | 
|  | 359 | // sandbox. | 
|  | 360 | packagedTools = append(packagedTools, specs...) | 
|  | 361 | // Assume that the first PackagingSpec of the module is the tool. | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 362 | addLocationLabel(tag.label, packagedToolLocation{specs[0]}) | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 363 | } else { | 
|  | 364 | tools = append(tools, path.Path()) | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 365 | addLocationLabel(tag.label, toolLocation{android.Paths{path.Path()}}) | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 366 | } | 
|  | 367 | case bootstrap.GoBinaryTool: | 
|  | 368 | // 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] | 369 | p := android.PathForGoBinary(ctx, t) | 
|  | 370 | tools = append(tools, p) | 
|  | 371 | addLocationLabel(tag.label, toolLocation{android.Paths{p}}) | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 372 | default: | 
| Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 373 | ctx.ModuleErrorf("%q is not a host tool provider", tool) | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 374 | return | 
| Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 375 | } | 
|  | 376 |  | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 377 | seenTools[tag.label] = true | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 378 | } | 
| Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 379 | }) | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 380 |  | 
|  | 381 | // If AllowMissingDependencies is enabled, the build will not have stopped when | 
|  | 382 | // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical | 
| Liz Kammer | 20ebfb4 | 2020-07-28 11:32:07 -0700 | [diff] [blame] | 383 | // "cmd: unknown location label ..." errors later.  Add a placeholder file to the local label. | 
|  | 384 | // The command that uses this placeholder file will never be executed because the rule will be | 
|  | 385 | // replaced with an android.Error rule reporting the missing dependencies. | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 386 | if ctx.Config().AllowMissingDependencies() { | 
|  | 387 | for _, tool := range g.properties.Tools { | 
|  | 388 | if !seenTools[tool] { | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 389 | addLocationLabel(tool, errorLocation{"***missing tool " + tool + "***"}) | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 390 | } | 
|  | 391 | } | 
|  | 392 | } | 
| Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 393 | } | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 394 |  | 
| Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 395 | if ctx.Failed() { | 
|  | 396 | return | 
|  | 397 | } | 
|  | 398 |  | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 399 | for _, toolFile := range g.properties.Tool_files { | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 400 | paths := android.PathsForModuleSrc(ctx, []string{toolFile}) | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 401 | tools = append(tools, paths...) | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 402 | addLocationLabel(toolFile, toolLocation{paths}) | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 403 | } | 
|  | 404 |  | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 405 | addLabelsForInputs := func(propName string, include, exclude []string) android.Paths { | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 406 | includeDirInPaths := ctx.DeviceConfig().BuildBrokenInputDir(g.Name()) | 
|  | 407 | var srcFiles android.Paths | 
|  | 408 | for _, in := range include { | 
|  | 409 | paths, missingDeps := android.PathsAndMissingDepsRelativeToModuleSourceDir(android.SourceInput{ | 
|  | 410 | Context: ctx, Paths: []string{in}, ExcludePaths: exclude, IncludeDirs: includeDirInPaths, | 
|  | 411 | }) | 
|  | 412 | if len(missingDeps) > 0 { | 
|  | 413 | if !ctx.Config().AllowMissingDependencies() { | 
|  | 414 | panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator", | 
|  | 415 | missingDeps)) | 
|  | 416 | } | 
|  | 417 |  | 
|  | 418 | // If AllowMissingDependencies is enabled, the build will not have stopped when | 
|  | 419 | // the dependency was added on a missing SourceFileProducer module, which will result in nonsensical | 
|  | 420 | // "cmd: label ":..." has no files" errors later.  Add a placeholder file to the local label. | 
|  | 421 | // The command that uses this placeholder file will never be executed because the rule will be | 
|  | 422 | // replaced with an android.Error rule reporting the missing dependencies. | 
|  | 423 | ctx.AddMissingDependencies(missingDeps) | 
|  | 424 | addLocationLabel(in, errorLocation{"***missing " + propName + " " + in + "***"}) | 
|  | 425 | } else { | 
|  | 426 | srcFiles = append(srcFiles, paths...) | 
|  | 427 | addLocationLabel(in, inputLocation{paths}) | 
|  | 428 | } | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 429 | } | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 430 | return srcFiles | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 431 | } | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 432 | srcFiles := addLabelsForInputs("srcs", g.properties.Srcs, g.properties.Exclude_srcs) | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 433 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 434 | var copyFrom android.Paths | 
|  | 435 | var outputFiles android.WritablePaths | 
|  | 436 | var zipArgs strings.Builder | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 437 |  | 
| Colin Cross | f3bfd02 | 2021-09-27 15:15:06 -0700 | [diff] [blame] | 438 | cmd := String(g.properties.Cmd) | 
|  | 439 | if g.CmdModifier != nil { | 
|  | 440 | cmd = g.CmdModifier(ctx, cmd) | 
|  | 441 | } | 
|  | 442 |  | 
| Liz Kammer | 796921d | 2023-07-11 08:21:41 -0400 | [diff] [blame] | 443 | var extraInputs android.Paths | 
| Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 444 | // Generate tasks, either from genrule or gensrcs. | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 445 | for i, task := range g.taskGenerator(ctx, cmd, srcFiles) { | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 446 | if len(task.out) == 0 { | 
|  | 447 | ctx.ModuleErrorf("must have at least one output file") | 
|  | 448 | return | 
| Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 449 | } | 
|  | 450 |  | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 451 | // Only handle extra inputs once as these currently are the same across all tasks | 
|  | 452 | if i == 0 { | 
|  | 453 | for name, values := range task.extraInputs { | 
|  | 454 | extraInputs = append(extraInputs, addLabelsForInputs(name, values, []string{})...) | 
|  | 455 | } | 
|  | 456 | } | 
|  | 457 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 458 | // Pick a unique path outside the task.genDir for the sbox manifest textproto, | 
|  | 459 | // a unique rule name, and the user-visible description. | 
|  | 460 | manifestName := "genrule.sbox.textproto" | 
|  | 461 | desc := "generate" | 
|  | 462 | name := "generator" | 
|  | 463 | if task.shards > 0 { | 
|  | 464 | manifestName = "genrule_" + strconv.Itoa(task.shard) + ".sbox.textproto" | 
|  | 465 | desc += " " + strconv.Itoa(task.shard) | 
|  | 466 | name += strconv.Itoa(task.shard) | 
|  | 467 | } else if len(task.out) == 1 { | 
|  | 468 | desc += " " + task.out[0].Base() | 
|  | 469 | } | 
|  | 470 |  | 
|  | 471 | manifestPath := android.PathForModuleOut(ctx, manifestName) | 
|  | 472 |  | 
|  | 473 | // 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] | 474 | rule := getSandboxedRuleBuilder(ctx, android.NewRuleBuilder(pctx, ctx).Sbox(task.genDir, manifestPath)) | 
| Justin Yun | 4da4ccc | 2023-07-06 10:56:29 +0900 | [diff] [blame] | 475 | if Bool(g.properties.Write_if_changed) { | 
|  | 476 | rule.Restat() | 
|  | 477 | } | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 478 | cmd := rule.Command() | 
|  | 479 |  | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 480 | for _, out := range task.out { | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 481 | addLocationLabel(out.Rel(), outputLocation{out}) | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 482 | } | 
|  | 483 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 484 | referencedDepfile := false | 
|  | 485 |  | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 486 | rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) { | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 487 | // report the error directly without returning an error to android.Expand to catch multiple errors in a | 
|  | 488 | // single run | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 489 | reportError := func(fmt string, args ...interface{}) (string, error) { | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 490 | ctx.PropertyErrorf("cmd", fmt, args...) | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 491 | return "SOONG_ERROR", nil | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 492 | } | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 493 |  | 
| Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 494 | // 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] | 495 | switch name { | 
|  | 496 | case "location": | 
|  | 497 | if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 { | 
|  | 498 | 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] | 499 | } | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 500 | loc := locationLabels[firstLabel] | 
|  | 501 | paths := loc.Paths(cmd) | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 502 | if len(paths) == 0 { | 
|  | 503 | return reportError("default label %q has no files", firstLabel) | 
|  | 504 | } else if len(paths) > 1 { | 
|  | 505 | return reportError("default label %q has multiple files, use $(locations %s) to reference it", | 
|  | 506 | firstLabel, firstLabel) | 
| Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 507 | } | 
| Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 508 | return proptools.ShellEscape(paths[0]), nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 509 | case "in": | 
| Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 510 | return strings.Join(proptools.ShellEscapeList(cmd.PathsForInputs(srcFiles)), " "), nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 511 | case "out": | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 512 | var sandboxOuts []string | 
|  | 513 | for _, out := range task.out { | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 514 | sandboxOuts = append(sandboxOuts, cmd.PathForOutput(out)) | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 515 | } | 
| Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 516 | return strings.Join(proptools.ShellEscapeList(sandboxOuts), " "), nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 517 | case "depfile": | 
|  | 518 | referencedDepfile = true | 
|  | 519 | if !Bool(g.properties.Depfile) { | 
|  | 520 | return reportError("$(depfile) used without depfile property") | 
|  | 521 | } | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 522 | return "__SBOX_DEPFILE__", nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 523 | case "genDir": | 
| Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 524 | return proptools.ShellEscape(cmd.PathForOutput(task.genDir)), nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 525 | default: | 
|  | 526 | if strings.HasPrefix(name, "location ") { | 
|  | 527 | label := strings.TrimSpace(strings.TrimPrefix(name, "location ")) | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 528 | if loc, ok := locationLabels[label]; ok { | 
|  | 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("label %q has no files", label) | 
|  | 532 | } else if len(paths) > 1 { | 
|  | 533 | return reportError("label %q has multiple files, use $(locations %s) to reference it", | 
|  | 534 | label, label) | 
|  | 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 | } else { | 
| Anton Hansson | bebf526 | 2022-02-23 11:42:38 +0000 | [diff] [blame] | 538 | 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] | 539 | } | 
|  | 540 | } else if strings.HasPrefix(name, "locations ") { | 
|  | 541 | label := strings.TrimSpace(strings.TrimPrefix(name, "locations ")) | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 542 | if loc, ok := locationLabels[label]; ok { | 
|  | 543 | paths := loc.Paths(cmd) | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 544 | if len(paths) == 0 { | 
|  | 545 | return reportError("label %q has no files", label) | 
|  | 546 | } | 
| Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 547 | return proptools.ShellEscape(strings.Join(paths, " ")), nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 548 | } else { | 
| Anton Hansson | bebf526 | 2022-02-23 11:42:38 +0000 | [diff] [blame] | 549 | 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] | 550 | } | 
|  | 551 | } else { | 
|  | 552 | return reportError("unknown variable '$(%s)'", name) | 
|  | 553 | } | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 554 | } | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 555 | }) | 
|  | 556 |  | 
|  | 557 | if err != nil { | 
|  | 558 | ctx.PropertyErrorf("cmd", "%s", err.Error()) | 
|  | 559 | return | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 560 | } | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 561 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 562 | if Bool(g.properties.Depfile) && !referencedDepfile { | 
|  | 563 | ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd") | 
|  | 564 | return | 
|  | 565 | } | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 566 | g.rawCommands = append(g.rawCommands, rawCommand) | 
| Bill Peckham | c087be1 | 2020-02-13 15:55:10 -0800 | [diff] [blame] | 567 |  | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 568 | cmd.Text(rawCommand) | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 569 | cmd.Implicits(srcFiles) // need to be able to reference other srcs | 
|  | 570 | cmd.Implicits(extraInputs) | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 571 | cmd.ImplicitOutputs(task.out) | 
|  | 572 | cmd.Implicits(task.in) | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 573 | cmd.ImplicitTools(tools) | 
|  | 574 | cmd.ImplicitTools(task.extraTools) | 
|  | 575 | cmd.ImplicitPackagedTools(packagedTools) | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 576 | if Bool(g.properties.Depfile) { | 
|  | 577 | cmd.ImplicitDepFile(task.depFile) | 
|  | 578 | } | 
|  | 579 |  | 
|  | 580 | // Create the rule to run the genrule command inside sbox. | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 581 | rule.Build(name, desc) | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 582 |  | 
|  | 583 | if len(task.copyTo) > 0 { | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 584 | // If copyTo is set, multiple shards need to be copied into a single directory. | 
|  | 585 | // task.out contains the per-shard paths, and copyTo contains the corresponding | 
|  | 586 | // final path.  The files need to be copied into the final directory by a | 
|  | 587 | // single rule so it can remove the directory before it starts to ensure no | 
|  | 588 | // old files remain.  zipsync already does this, so build up zipArgs that | 
|  | 589 | // zip all the per-shard directories into a single zip. | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 590 | outputFiles = append(outputFiles, task.copyTo...) | 
|  | 591 | copyFrom = append(copyFrom, task.out.Paths()...) | 
|  | 592 | zipArgs.WriteString(" -C " + task.genDir.String()) | 
|  | 593 | zipArgs.WriteString(android.JoinWithPrefix(task.out.Strings(), " -f ")) | 
|  | 594 | } else { | 
|  | 595 | outputFiles = append(outputFiles, task.out...) | 
|  | 596 | } | 
| Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 597 | } | 
|  | 598 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 599 | if len(copyFrom) > 0 { | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 600 | // Create a rule that zips all the per-shard directories into a single zip and then | 
|  | 601 | // uses zipsync to unzip it into the final directory. | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 602 | ctx.Build(pctx, android.BuildParams{ | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 603 | Rule:        gensrcsMerge, | 
|  | 604 | Implicits:   copyFrom, | 
|  | 605 | Outputs:     outputFiles, | 
|  | 606 | Description: "merge shards", | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 607 | Args: map[string]string{ | 
|  | 608 | "zipArgs": zipArgs.String(), | 
|  | 609 | "tmpZip":  android.PathForModuleGen(ctx, g.subDir+".zip").String(), | 
|  | 610 | "genDir":  android.PathForModuleGen(ctx, g.subDir).String(), | 
|  | 611 | }, | 
|  | 612 | }) | 
| Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 613 | } | 
|  | 614 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 615 | g.outputFiles = outputFiles.Paths() | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 616 | } | 
| Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 617 |  | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 618 | func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 619 | // Allowlist genrule to use depfile until we have a solution to remove it. | 
|  | 620 | // TODO(b/235582219): Remove allowlist for genrule | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 621 | if Bool(g.properties.Depfile) { | 
| Yu Liu | e7f7cbf | 2023-06-13 18:50:03 +0000 | [diff] [blame] | 622 | sandboxingAllowlistSets := getSandboxingAllowlistSets(ctx) | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 623 | // TODO(b/283852474): Checking the GenruleSandboxing flag is temporary in | 
|  | 624 | // order to pass the presubmit before internal master is updated. | 
| Yu Liu | e7f7cbf | 2023-06-13 18:50:03 +0000 | [diff] [blame] | 625 | if ctx.DeviceConfig().GenruleSandboxing() && !sandboxingAllowlistSets.depfileAllowSet[g.Name()] { | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 626 | ctx.PropertyErrorf( | 
|  | 627 | "depfile", | 
|  | 628 | "Deprecated to ensure the module type is convertible to Bazel. "+ | 
|  | 629 | "Try specifying the dependencies explicitly so that there is no need to use depfile. "+ | 
|  | 630 | "If not possible, the escape hatch is to add the module to allowlists.go to bypass the error.") | 
|  | 631 | } | 
| Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 632 | } | 
|  | 633 |  | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 634 | g.generateCommonBuildActions(ctx) | 
|  | 635 |  | 
|  | 636 | // For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of | 
|  | 637 | // the genrules on AOSP. That will make things simpler to look at the graph in the common | 
|  | 638 | // case. For larger sets of outputs, inject a phony target in between to limit ninja file | 
|  | 639 | // growth. | 
|  | 640 | if len(g.outputFiles) <= 6 { | 
|  | 641 | g.outputDeps = g.outputFiles | 
|  | 642 | } else { | 
|  | 643 | phonyFile := android.PathForModuleGen(ctx, "genrule-phony") | 
|  | 644 | ctx.Build(pctx, android.BuildParams{ | 
|  | 645 | Rule:   blueprint.Phony, | 
|  | 646 | Output: phonyFile, | 
|  | 647 | Inputs: g.outputFiles, | 
|  | 648 | }) | 
|  | 649 | g.outputDeps = android.Paths{phonyFile} | 
| Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 650 | } | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 651 | } | 
|  | 652 |  | 
|  | 653 | func (g *Module) QueueBazelCall(ctx android.BaseModuleContext) { | 
|  | 654 | bazelCtx := ctx.Config().BazelContext | 
|  | 655 | bazelCtx.QueueBazelRequest(g.GetBazelLabel(ctx, g), cquery.GetOutputFiles, android.GetConfigKey(ctx)) | 
|  | 656 | } | 
|  | 657 |  | 
|  | 658 | func (g *Module) IsMixedBuildSupported(ctx android.BaseModuleContext) bool { | 
|  | 659 | return true | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 660 | } | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 661 |  | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 662 | // Collect information for opening IDE project files in java/jdeps.go. | 
|  | 663 | func (g *Module) IDEInfo(dpInfo *android.IdeInfo) { | 
|  | 664 | dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...) | 
|  | 665 | for _, src := range g.properties.Srcs { | 
|  | 666 | if strings.HasPrefix(src, ":") { | 
|  | 667 | src = strings.Trim(src, ":") | 
|  | 668 | dpInfo.Deps = append(dpInfo.Deps, src) | 
|  | 669 | } | 
|  | 670 | } | 
| bralee | 1fbf440 | 2020-05-21 10:11:59 +0800 | [diff] [blame] | 671 | dpInfo.Paths = append(dpInfo.Paths, g.modulePaths...) | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 672 | } | 
|  | 673 |  | 
| Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 674 | func (g *Module) AndroidMk() android.AndroidMkData { | 
|  | 675 | return android.AndroidMkData{ | 
| Anton Hansson | 72f1849 | 2020-10-30 16:34:45 +0000 | [diff] [blame] | 676 | Class:      "ETC", | 
| Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 677 | OutputFile: android.OptionalPathForPath(g.outputFiles[0]), | 
|  | 678 | SubName:    g.subName, | 
|  | 679 | Extra: []android.AndroidMkExtraFunc{ | 
|  | 680 | func(w io.Writer, outputFile android.Path) { | 
| Anton Hansson | 72f1849 | 2020-10-30 16:34:45 +0000 | [diff] [blame] | 681 | fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true") | 
| Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 682 | }, | 
|  | 683 | }, | 
|  | 684 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { | 
|  | 685 | android.WriteAndroidMkData(w, data) | 
|  | 686 | if data.SubName != "" { | 
|  | 687 | fmt.Fprintln(w, ".PHONY:", name) | 
|  | 688 | fmt.Fprintln(w, name, ":", name+g.subName) | 
|  | 689 | } | 
|  | 690 | }, | 
|  | 691 | } | 
|  | 692 | } | 
|  | 693 |  | 
| Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 694 | var _ android.ApexModule = (*Module)(nil) | 
|  | 695 |  | 
|  | 696 | // Implements android.ApexModule | 
| Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 697 | func (g *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, | 
|  | 698 | sdkVersion android.ApiLevel) error { | 
| Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 699 | // Because generated outputs are checked by client modules(e.g. cc_library, ...) | 
|  | 700 | // we can safely ignore the check here. | 
|  | 701 | return nil | 
|  | 702 | } | 
|  | 703 |  | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 704 | func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module { | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 705 | module := &Module{ | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 706 | taskGenerator: taskGenerator, | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 707 | } | 
|  | 708 |  | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 709 | module.AddProperties(props...) | 
|  | 710 | module.AddProperties(&module.properties) | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 711 |  | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 712 | module.ImageInterface = noopImageInterface{} | 
|  | 713 |  | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 714 | return module | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 715 | } | 
|  | 716 |  | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 717 | type noopImageInterface struct{} | 
|  | 718 |  | 
|  | 719 | func (x noopImageInterface) ImageMutatorBegin(android.BaseModuleContext)                 {} | 
|  | 720 | func (x noopImageInterface) CoreVariantNeeded(android.BaseModuleContext) bool            { return false } | 
| Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 721 | func (x noopImageInterface) RamdiskVariantNeeded(android.BaseModuleContext) bool         { return false } | 
| Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 722 | func (x noopImageInterface) VendorRamdiskVariantNeeded(android.BaseModuleContext) bool   { return false } | 
| Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 723 | func (x noopImageInterface) DebugRamdiskVariantNeeded(android.BaseModuleContext) bool    { return false } | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 724 | func (x noopImageInterface) RecoveryVariantNeeded(android.BaseModuleContext) bool        { return false } | 
|  | 725 | func (x noopImageInterface) ExtraImageVariations(ctx android.BaseModuleContext) []string { return nil } | 
|  | 726 | func (x noopImageInterface) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { | 
|  | 727 | } | 
|  | 728 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 729 | func NewGenSrcs() *Module { | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 730 | properties := &genSrcsProperties{} | 
|  | 731 |  | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 732 | // finalSubDir is the name of the subdirectory that output files will be generated into. | 
|  | 733 | // It is used so that per-shard directories can be placed alongside it an then finally | 
|  | 734 | // merged into it. | 
|  | 735 | const finalSubDir = "gensrcs" | 
|  | 736 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 737 | taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask { | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 738 | shardSize := defaultShardSize | 
|  | 739 | if s := properties.Shard_size; s != nil { | 
|  | 740 | shardSize = int(*s) | 
|  | 741 | } | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 742 |  | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 743 | // gensrcs rules can easily hit command line limits by repeating the command for | 
|  | 744 | // every input file.  Shard the input files into groups. | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 745 | shards := android.ShardPaths(srcFiles, shardSize) | 
|  | 746 | var generateTasks []generateTask | 
| Colin Cross | baccf5b | 2018-02-21 14:07:48 -0800 | [diff] [blame] | 747 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 748 | for i, shard := range shards { | 
|  | 749 | var commands []string | 
|  | 750 | var outFiles android.WritablePaths | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 751 | var commandDepFiles []string | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 752 | var copyTo android.WritablePaths | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 753 |  | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 754 | // When sharding is enabled (i.e. len(shards) > 1), the sbox rules for each | 
|  | 755 | // shard will be write to their own directories and then be merged together | 
|  | 756 | // into finalSubDir.  If sharding is not enabled (i.e. len(shards) == 1), | 
|  | 757 | // the sbox rule will write directly to finalSubDir. | 
|  | 758 | genSubDir := finalSubDir | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 759 | if len(shards) > 1 { | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 760 | genSubDir = strconv.Itoa(i) | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 761 | } | 
|  | 762 |  | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 763 | genDir := android.PathForModuleGen(ctx, genSubDir) | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 764 | // TODO(ccross): this RuleBuilder is a hack to be able to call | 
|  | 765 | // rule.Command().PathForOutput.  Replace this with passing the rule into the | 
|  | 766 | // generator. | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 767 | rule := getSandboxedRuleBuilder(ctx, android.NewRuleBuilder(pctx, ctx).Sbox(genDir, nil)) | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 768 |  | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 769 | for _, in := range shard { | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 770 | outFile := android.GenPathWithExt(ctx, finalSubDir, in, String(properties.Output_extension)) | 
|  | 771 |  | 
|  | 772 | // If sharding is enabled, then outFile is the path to the output file in | 
|  | 773 | // the shard directory, and copyTo is the path to the output file in the | 
|  | 774 | // final directory. | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 775 | if len(shards) > 1 { | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 776 | shardFile := android.GenPathWithExt(ctx, genSubDir, in, String(properties.Output_extension)) | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 777 | copyTo = append(copyTo, outFile) | 
|  | 778 | outFile = shardFile | 
|  | 779 | } | 
|  | 780 |  | 
|  | 781 | outFiles = append(outFiles, outFile) | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 782 |  | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 783 | // pre-expand the command line to replace $in and $out with references to | 
|  | 784 | // a single input and output file. | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 785 | command, err := android.Expand(rawCommand, func(name string) (string, error) { | 
|  | 786 | switch name { | 
|  | 787 | case "in": | 
|  | 788 | return in.String(), nil | 
|  | 789 | case "out": | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 790 | return rule.Command().PathForOutput(outFile), nil | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 791 | case "depfile": | 
|  | 792 | // Generate a depfile for each output file.  Store the list for | 
|  | 793 | // later in order to combine them all into a single depfile. | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 794 | depFile := rule.Command().PathForOutput(outFile.ReplaceExtension(ctx, "d")) | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 795 | commandDepFiles = append(commandDepFiles, depFile) | 
|  | 796 | return depFile, nil | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 797 | default: | 
|  | 798 | return "$(" + name + ")", nil | 
|  | 799 | } | 
|  | 800 | }) | 
|  | 801 | if err != nil { | 
|  | 802 | ctx.PropertyErrorf("cmd", err.Error()) | 
|  | 803 | } | 
|  | 804 |  | 
|  | 805 | // escape the command in case for example it contains '#', an odd number of '"', etc | 
|  | 806 | command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command)) | 
|  | 807 | commands = append(commands, command) | 
|  | 808 | } | 
|  | 809 | fullCommand := strings.Join(commands, " && ") | 
|  | 810 |  | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 811 | var outputDepfile android.WritablePath | 
|  | 812 | var extraTools android.Paths | 
|  | 813 | if len(commandDepFiles) > 0 { | 
|  | 814 | // Each command wrote to a depfile, but ninja can only handle one | 
|  | 815 | // depfile per rule.  Use the dep_fixer tool at the end of the | 
|  | 816 | // command to combine all the depfiles into a single output depfile. | 
|  | 817 | outputDepfile = android.PathForModuleGen(ctx, genSubDir, "gensrcs.d") | 
|  | 818 | depFixerTool := ctx.Config().HostToolPath(ctx, "dep_fixer") | 
|  | 819 | fullCommand += fmt.Sprintf(" && %s -o $(depfile) %s", | 
| Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 820 | rule.Command().PathForTool(depFixerTool), | 
| Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 821 | strings.Join(commandDepFiles, " ")) | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 822 | extraTools = append(extraTools, depFixerTool) | 
|  | 823 | } | 
|  | 824 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 825 | generateTasks = append(generateTasks, generateTask{ | 
| Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 826 | in:         shard, | 
|  | 827 | out:        outFiles, | 
|  | 828 | depFile:    outputDepfile, | 
|  | 829 | copyTo:     copyTo, | 
|  | 830 | genDir:     genDir, | 
|  | 831 | cmd:        fullCommand, | 
|  | 832 | shard:      i, | 
|  | 833 | shards:     len(shards), | 
|  | 834 | extraTools: extraTools, | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 835 | extraInputs: map[string][]string{ | 
|  | 836 | "data": properties.Data, | 
|  | 837 | }, | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 838 | }) | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 839 | } | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 840 |  | 
|  | 841 | return generateTasks | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 842 | } | 
|  | 843 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 844 | g := generatorFactory(taskGenerator, properties) | 
| Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 845 | g.subDir = finalSubDir | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 846 | return g | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 847 | } | 
|  | 848 |  | 
| Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 849 | func GenSrcsFactory() android.Module { | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 850 | m := NewGenSrcs() | 
|  | 851 | android.InitAndroidModule(m) | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 852 | android.InitBazelModule(m) | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 853 | return m | 
|  | 854 | } | 
|  | 855 |  | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 856 | type genSrcsProperties struct { | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 857 | // extension that will be substituted for each output file | 
| Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 858 | Output_extension *string | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 859 |  | 
|  | 860 | // maximum number of files that will be passed on a single command line. | 
|  | 861 | Shard_size *int64 | 
| Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 862 |  | 
|  | 863 | // Additional files needed for build that are not tooling related. | 
|  | 864 | Data []string `android:"path"` | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 865 | } | 
|  | 866 |  | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 867 | type bazelGensrcsAttributes struct { | 
|  | 868 | Srcs             bazel.LabelListAttribute | 
|  | 869 | Output_extension *string | 
|  | 870 | Tools            bazel.LabelListAttribute | 
| Cole Faust | f0d4d4f | 2023-08-14 11:37:54 -0700 | [diff] [blame] | 871 | Cmd              bazel.StringAttribute | 
| Liz Kammer | 8bd9242 | 2023-06-09 13:41:08 -0400 | [diff] [blame] | 872 | Data             bazel.LabelListAttribute | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 873 | } | 
|  | 874 |  | 
| Evgenii Stepanov | f47c90d | 2020-12-02 18:55:09 -0800 | [diff] [blame] | 875 | const defaultShardSize = 50 | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 876 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 877 | func NewGenRule() *Module { | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 878 | properties := &genRuleProperties{} | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 879 |  | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 880 | taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask { | 
| Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 881 | outs := make(android.WritablePaths, len(properties.Out)) | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 882 | var depFile android.WritablePath | 
| Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 883 | for i, out := range properties.Out { | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 884 | outPath := android.PathForModuleGen(ctx, out) | 
|  | 885 | if i == 0 { | 
|  | 886 | depFile = outPath.ReplaceExtension(ctx, "d") | 
|  | 887 | } | 
|  | 888 | outs[i] = outPath | 
| Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 889 | } | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 890 | return []generateTask{{ | 
| Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 891 | in:      srcFiles, | 
|  | 892 | out:     outs, | 
|  | 893 | depFile: depFile, | 
|  | 894 | genDir:  android.PathForModuleGen(ctx), | 
|  | 895 | cmd:     rawCommand, | 
| Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 896 | }} | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 897 | } | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 898 |  | 
| Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 899 | return generatorFactory(taskGenerator, properties) | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 900 | } | 
|  | 901 |  | 
| Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 902 | func GenRuleFactory() android.Module { | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 903 | m := NewGenRule() | 
|  | 904 | android.InitAndroidModule(m) | 
| Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 905 | android.InitDefaultableModule(m) | 
| Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 906 | android.InitBazelModule(m) | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 907 | return m | 
|  | 908 | } | 
|  | 909 |  | 
| Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 910 | type genRuleProperties struct { | 
| Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 911 | // names of the output files that will be generated | 
| Yu Liu | d620101 | 2022-10-17 12:29:15 -0700 | [diff] [blame] | 912 | Out []string | 
| Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 913 | } | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 914 |  | 
| Romain Jobredeaux | 9973ace | 2023-08-30 00:27:09 -0400 | [diff] [blame] | 915 | type BazelGenruleAttributes struct { | 
| Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 916 | Srcs  bazel.LabelListAttribute | 
| Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 917 | Outs  []string | 
| Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 918 | Tools bazel.LabelListAttribute | 
| Cole Faust | f0d4d4f | 2023-08-14 11:37:54 -0700 | [diff] [blame] | 919 | Cmd   bazel.StringAttribute | 
| Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 920 | } | 
|  | 921 |  | 
| Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 922 | // ConvertWithBp2build converts a Soong module -> Bazel target. | 
|  | 923 | func (m *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) { | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 924 | // Bazel only has the "tools" attribute. | 
| Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 925 | tools_prop := android.BazelLabelForModuleDeps(ctx, m.properties.Tools) | 
|  | 926 | tool_files_prop := android.BazelLabelForModuleSrc(ctx, m.properties.Tool_files) | 
|  | 927 | tools_prop.Append(tool_files_prop) | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 928 |  | 
| Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 929 | tools := bazel.MakeLabelListAttribute(tools_prop) | 
| Yu Liu | d620101 | 2022-10-17 12:29:15 -0700 | [diff] [blame] | 930 | srcs := bazel.LabelListAttribute{} | 
|  | 931 | srcs_labels := bazel.LabelList{} | 
|  | 932 | // Only cc_genrule is arch specific | 
|  | 933 | if ctx.ModuleType() == "cc_genrule" { | 
|  | 934 | for axis, configToProps := range m.GetArchVariantProperties(ctx, &generatorProperties{}) { | 
|  | 935 | for config, props := range configToProps { | 
|  | 936 | if props, ok := props.(*generatorProperties); ok { | 
|  | 937 | labels := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs) | 
|  | 938 | srcs_labels.Append(labels) | 
|  | 939 | srcs.SetSelectValue(axis, config, labels) | 
|  | 940 | } | 
|  | 941 | } | 
|  | 942 | } | 
|  | 943 | } else { | 
|  | 944 | srcs_labels = android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs) | 
|  | 945 | srcs = bazel.MakeLabelListAttribute(srcs_labels) | 
|  | 946 | } | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 947 |  | 
|  | 948 | var allReplacements bazel.LabelList | 
| Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 949 | allReplacements.Append(tools.Value) | 
| Yu Liu | d620101 | 2022-10-17 12:29:15 -0700 | [diff] [blame] | 950 | allReplacements.Append(bazel.FirstUniqueBazelLabelList(srcs_labels)) | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 951 |  | 
| Liz Kammer | 8bd9242 | 2023-06-09 13:41:08 -0400 | [diff] [blame] | 952 | // The Output_extension prop is not in an immediately accessible field | 
|  | 953 | // in the Module struct, so use GetProperties and cast it | 
|  | 954 | // to the known struct prop. | 
|  | 955 | var outputExtension *string | 
|  | 956 | var data bazel.LabelListAttribute | 
|  | 957 | if ctx.ModuleType() == "gensrcs" { | 
|  | 958 | for _, propIntf := range m.GetProperties() { | 
|  | 959 | if props, ok := propIntf.(*genSrcsProperties); ok { | 
|  | 960 | outputExtension = props.Output_extension | 
|  | 961 | dataFiles := android.BazelLabelForModuleSrc(ctx, props.Data) | 
|  | 962 | allReplacements.Append(bazel.FirstUniqueBazelLabelList(dataFiles)) | 
|  | 963 | data = bazel.MakeLabelListAttribute(dataFiles) | 
|  | 964 | break | 
|  | 965 | } | 
|  | 966 | } | 
|  | 967 | } | 
|  | 968 |  | 
| Cole Faust | f0d4d4f | 2023-08-14 11:37:54 -0700 | [diff] [blame] | 969 | replaceVariables := func(cmd string) string { | 
|  | 970 | // Replace in and out variables with $< and $@ | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 971 | if ctx.ModuleType() == "gensrcs" { | 
| Cole Faust | f0d4d4f | 2023-08-14 11:37:54 -0700 | [diff] [blame] | 972 | cmd = strings.ReplaceAll(cmd, "$(in)", "$(SRC)") | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 973 | cmd = strings.ReplaceAll(cmd, "$(out)", "$(OUT)") | 
|  | 974 | } else { | 
| Cole Faust | f0d4d4f | 2023-08-14 11:37:54 -0700 | [diff] [blame] | 975 | cmd = strings.Replace(cmd, "$(in)", "$(SRCS)", -1) | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 976 | cmd = strings.Replace(cmd, "$(out)", "$(OUTS)", -1) | 
|  | 977 | } | 
| Vinh Tran | 32a98a5 | 2022-09-23 13:08:34 -0400 | [diff] [blame] | 978 | cmd = strings.Replace(cmd, "$(genDir)", "$(RULEDIR)", -1) | 
| Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 979 | if len(tools.Value.Includes) > 0 { | 
|  | 980 | cmd = strings.Replace(cmd, "$(location)", fmt.Sprintf("$(location %s)", tools.Value.Includes[0].Label), -1) | 
|  | 981 | cmd = strings.Replace(cmd, "$(locations)", fmt.Sprintf("$(locations %s)", tools.Value.Includes[0].Label), -1) | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 982 | } | 
|  | 983 | for _, l := range allReplacements.Includes { | 
| Jingwen Chen | 38e6264 | 2021-04-19 05:00:15 +0000 | [diff] [blame] | 984 | bpLoc := fmt.Sprintf("$(location %s)", l.OriginalModuleName) | 
|  | 985 | bpLocs := fmt.Sprintf("$(locations %s)", l.OriginalModuleName) | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 986 | bazelLoc := fmt.Sprintf("$(location %s)", l.Label) | 
|  | 987 | bazelLocs := fmt.Sprintf("$(locations %s)", l.Label) | 
|  | 988 | cmd = strings.Replace(cmd, bpLoc, bazelLoc, -1) | 
|  | 989 | cmd = strings.Replace(cmd, bpLocs, bazelLocs, -1) | 
|  | 990 | } | 
| Cole Faust | f0d4d4f | 2023-08-14 11:37:54 -0700 | [diff] [blame] | 991 | return cmd | 
|  | 992 | } | 
|  | 993 |  | 
|  | 994 | var cmdProp bazel.StringAttribute | 
|  | 995 | cmdProp.SetValue(replaceVariables(proptools.String(m.properties.Cmd))) | 
|  | 996 | allProductVariableProps := android.ProductVariableProperties(ctx, m) | 
|  | 997 | if productVariableProps, ok := allProductVariableProps["Cmd"]; ok { | 
|  | 998 | for productVariable, value := range productVariableProps { | 
|  | 999 | var cmd string | 
|  | 1000 | if strValue, ok := value.(*string); ok && strValue != nil { | 
|  | 1001 | cmd = *strValue | 
|  | 1002 | } | 
|  | 1003 | cmd = replaceVariables(cmd) | 
|  | 1004 | cmdProp.SetSelectValue(productVariable.ConfigurationAxis(), productVariable.SelectKey(), &cmd) | 
|  | 1005 | } | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1006 | } | 
|  | 1007 |  | 
| Spandan Das | 39b6cc5 | 2023-04-12 19:05:49 +0000 | [diff] [blame] | 1008 | tags := android.ApexAvailableTagsWithoutTestApexes(ctx, m) | 
| Sam Delmerico | eddd3c0 | 2022-12-02 17:31:58 -0500 | [diff] [blame] | 1009 |  | 
| Liz Kammer | 0db0e34 | 2023-07-18 11:39:30 -0400 | [diff] [blame] | 1010 | bazelName := m.Name() | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 1011 | if ctx.ModuleType() == "gensrcs" { | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 1012 | props := bazel.BazelTargetModuleProperties{ | 
|  | 1013 | Rule_class:        "gensrcs", | 
|  | 1014 | Bzl_load_location: "//build/bazel/rules:gensrcs.bzl", | 
|  | 1015 | } | 
|  | 1016 | attrs := &bazelGensrcsAttributes{ | 
|  | 1017 | Srcs:             srcs, | 
|  | 1018 | Output_extension: outputExtension, | 
| Cole Faust | f0d4d4f | 2023-08-14 11:37:54 -0700 | [diff] [blame] | 1019 | Cmd:              cmdProp, | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 1020 | Tools:            tools, | 
| Liz Kammer | 8bd9242 | 2023-06-09 13:41:08 -0400 | [diff] [blame] | 1021 | Data:             data, | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 1022 | } | 
| Sam Delmerico | eddd3c0 | 2022-12-02 17:31:58 -0500 | [diff] [blame] | 1023 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{ | 
|  | 1024 | Name: m.Name(), | 
|  | 1025 | Tags: tags, | 
|  | 1026 | }, attrs) | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 1027 | } else { | 
| Spandan Das | a99348d | 2023-08-01 23:10:05 +0000 | [diff] [blame] | 1028 | outs := m.RawOutputFiles(ctx) | 
| Chris Parsons | b7950a9 | 2023-06-16 17:41:42 +0000 | [diff] [blame] | 1029 | for _, out := range outs { | 
|  | 1030 | if out == bazelName { | 
|  | 1031 | // This is a workaround to circumvent a Bazel warning where a genrule's | 
|  | 1032 | // out may not have the same name as the target itself. This makes no | 
|  | 1033 | // difference for reverse dependencies, because they may depend on the | 
|  | 1034 | // out file by name. | 
|  | 1035 | bazelName = bazelName + "-gen" | 
|  | 1036 | break | 
|  | 1037 | } | 
|  | 1038 | } | 
| Romain Jobredeaux | 9973ace | 2023-08-30 00:27:09 -0400 | [diff] [blame] | 1039 | attrs := &BazelGenruleAttributes{ | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 1040 | Srcs:  srcs, | 
|  | 1041 | Outs:  outs, | 
| Cole Faust | f0d4d4f | 2023-08-14 11:37:54 -0700 | [diff] [blame] | 1042 | Cmd:   cmdProp, | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 1043 | Tools: tools, | 
|  | 1044 | } | 
|  | 1045 | props := bazel.BazelTargetModuleProperties{ | 
|  | 1046 | Rule_class: "genrule", | 
|  | 1047 | } | 
| Sam Delmerico | eddd3c0 | 2022-12-02 17:31:58 -0500 | [diff] [blame] | 1048 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{ | 
| Chris Parsons | b7950a9 | 2023-06-16 17:41:42 +0000 | [diff] [blame] | 1049 | Name: bazelName, | 
| Sam Delmerico | eddd3c0 | 2022-12-02 17:31:58 -0500 | [diff] [blame] | 1050 | Tags: tags, | 
|  | 1051 | }, attrs) | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1052 | } | 
| Liz Kammer | 0db0e34 | 2023-07-18 11:39:30 -0400 | [diff] [blame] | 1053 |  | 
|  | 1054 | if m.needsCcLibraryHeadersBp2build() { | 
|  | 1055 | includeDirs := make([]string, len(m.properties.Export_include_dirs)*2) | 
|  | 1056 | for i, dir := range m.properties.Export_include_dirs { | 
|  | 1057 | includeDirs[i*2] = dir | 
|  | 1058 | includeDirs[i*2+1] = filepath.Clean(filepath.Join(ctx.ModuleDir(), dir)) | 
|  | 1059 | } | 
|  | 1060 | attrs := &ccHeaderLibraryAttrs{ | 
|  | 1061 | Hdrs:            []string{":" + bazelName}, | 
|  | 1062 | Export_includes: includeDirs, | 
|  | 1063 | } | 
|  | 1064 | props := bazel.BazelTargetModuleProperties{ | 
|  | 1065 | Rule_class:        "cc_library_headers", | 
|  | 1066 | Bzl_load_location: "//build/bazel/rules/cc:cc_library_headers.bzl", | 
|  | 1067 | } | 
|  | 1068 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{ | 
|  | 1069 | Name: m.Name() + genruleHeaderLibrarySuffix, | 
|  | 1070 | Tags: tags, | 
|  | 1071 | }, attrs) | 
| Liz Kammer | 0db0e34 | 2023-07-18 11:39:30 -0400 | [diff] [blame] | 1072 | } | 
|  | 1073 | } | 
|  | 1074 |  | 
|  | 1075 | const genruleHeaderLibrarySuffix = "__header_library" | 
|  | 1076 |  | 
|  | 1077 | func (m *Module) needsCcLibraryHeadersBp2build() bool { | 
|  | 1078 | return len(m.properties.Export_include_dirs) > 0 | 
|  | 1079 | } | 
|  | 1080 |  | 
|  | 1081 | // GenruleCcHeaderMapper is a bazel.LabelMapper function to map genrules to a cc_library_headers | 
|  | 1082 | // target when they export multiple include directories. | 
|  | 1083 | func GenruleCcHeaderLabelMapper(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) { | 
|  | 1084 | mod, exists := ctx.ModuleFromName(label.OriginalModuleName) | 
|  | 1085 | if !exists { | 
|  | 1086 | return label.Label, false | 
|  | 1087 | } | 
|  | 1088 | if m, ok := mod.(*Module); ok { | 
|  | 1089 | if m.needsCcLibraryHeadersBp2build() { | 
|  | 1090 | return label.Label + genruleHeaderLibrarySuffix, true | 
|  | 1091 | } | 
|  | 1092 | } | 
|  | 1093 | return label.Label, false | 
|  | 1094 | } | 
|  | 1095 |  | 
|  | 1096 | type ccHeaderLibraryAttrs struct { | 
|  | 1097 | Hdrs []string | 
|  | 1098 |  | 
|  | 1099 | Export_includes []string | 
| Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 1100 | } | 
|  | 1101 |  | 
| Spandan Das | a99348d | 2023-08-01 23:10:05 +0000 | [diff] [blame] | 1102 | // RawOutputFfiles returns the raw outputs specified in Android.bp | 
|  | 1103 | // This does not contain the fully resolved path relative to the top of the tree | 
|  | 1104 | func (g *Module) RawOutputFiles(ctx android.BazelConversionContext) []string { | 
|  | 1105 | if ctx.Config().BuildMode != android.Bp2build { | 
|  | 1106 | ctx.ModuleErrorf("RawOutputFiles is only supported in bp2build mode") | 
|  | 1107 | } | 
|  | 1108 | // The Out prop is not in an immediately accessible field | 
|  | 1109 | // in the Module struct, so use GetProperties and cast it | 
|  | 1110 | // to the known struct prop. | 
|  | 1111 | var outs []string | 
|  | 1112 | for _, propIntf := range g.GetProperties() { | 
|  | 1113 | if props, ok := propIntf.(*genRuleProperties); ok { | 
|  | 1114 | outs = props.Out | 
|  | 1115 | break | 
|  | 1116 | } | 
|  | 1117 | } | 
|  | 1118 | return outs | 
|  | 1119 | } | 
|  | 1120 |  | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 1121 | var Bool = proptools.Bool | 
|  | 1122 | var String = proptools.String | 
| Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 1123 |  | 
| Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 1124 | // Defaults | 
| Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 1125 | type Defaults struct { | 
|  | 1126 | android.ModuleBase | 
|  | 1127 | android.DefaultsModuleBase | 
|  | 1128 | } | 
|  | 1129 |  | 
| Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 1130 | func defaultsFactory() android.Module { | 
|  | 1131 | return DefaultsFactory() | 
|  | 1132 | } | 
|  | 1133 |  | 
|  | 1134 | func DefaultsFactory(props ...interface{}) android.Module { | 
|  | 1135 | module := &Defaults{} | 
|  | 1136 |  | 
|  | 1137 | module.AddProperties(props...) | 
|  | 1138 | module.AddProperties( | 
|  | 1139 | &generatorProperties{}, | 
|  | 1140 | &genRuleProperties{}, | 
|  | 1141 | ) | 
|  | 1142 |  | 
|  | 1143 | android.InitDefaultsModule(module) | 
|  | 1144 |  | 
|  | 1145 | return module | 
|  | 1146 | } | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 1147 |  | 
| Yu Liu | e7f7cbf | 2023-06-13 18:50:03 +0000 | [diff] [blame] | 1148 | var sandboxingAllowlistKey = android.NewOnceKey("genruleSandboxingAllowlistKey") | 
|  | 1149 |  | 
|  | 1150 | type sandboxingAllowlistSets struct { | 
|  | 1151 | sandboxingDenyModuleSet map[string]bool | 
|  | 1152 | sandboxingDenyPathSet   map[string]bool | 
|  | 1153 | depfileAllowSet         map[string]bool | 
|  | 1154 | } | 
|  | 1155 |  | 
|  | 1156 | func getSandboxingAllowlistSets(ctx android.PathContext) *sandboxingAllowlistSets { | 
|  | 1157 | return ctx.Config().Once(sandboxingAllowlistKey, func() interface{} { | 
|  | 1158 | sandboxingDenyModuleSet := map[string]bool{} | 
|  | 1159 | sandboxingDenyPathSet := map[string]bool{} | 
|  | 1160 | depfileAllowSet := map[string]bool{} | 
|  | 1161 |  | 
|  | 1162 | android.AddToStringSet(sandboxingDenyModuleSet, append(DepfileAllowList, SandboxingDenyModuleList...)) | 
|  | 1163 | android.AddToStringSet(sandboxingDenyPathSet, SandboxingDenyPathList) | 
|  | 1164 | android.AddToStringSet(depfileAllowSet, DepfileAllowList) | 
|  | 1165 | return &sandboxingAllowlistSets{ | 
|  | 1166 | sandboxingDenyModuleSet: sandboxingDenyModuleSet, | 
|  | 1167 | sandboxingDenyPathSet:   sandboxingDenyPathSet, | 
|  | 1168 | depfileAllowSet:         depfileAllowSet, | 
|  | 1169 | } | 
|  | 1170 | }).(*sandboxingAllowlistSets) | 
|  | 1171 | } | 
| Liz Kammer | 0db0e34 | 2023-07-18 11:39:30 -0400 | [diff] [blame] | 1172 |  | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 1173 | func getSandboxedRuleBuilder(ctx android.ModuleContext, r *android.RuleBuilder) *android.RuleBuilder { | 
| Yu Liu | 45d6af5 | 2023-05-24 23:10:18 +0000 | [diff] [blame] | 1174 | if !ctx.DeviceConfig().GenruleSandboxing() { | 
|  | 1175 | return r.SandboxTools() | 
|  | 1176 | } | 
| Yu Liu | e7f7cbf | 2023-06-13 18:50:03 +0000 | [diff] [blame] | 1177 | sandboxingAllowlistSets := getSandboxingAllowlistSets(ctx) | 
|  | 1178 | if sandboxingAllowlistSets.sandboxingDenyPathSet[ctx.ModuleDir()] || | 
|  | 1179 | sandboxingAllowlistSets.sandboxingDenyModuleSet[ctx.ModuleName()] { | 
| Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 1180 | return r.SandboxTools() | 
|  | 1181 | } | 
|  | 1182 | return r.SandboxInputs() | 
|  | 1183 | } |