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