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