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