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