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