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