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