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" |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 19 | "path" |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 20 | "strings" |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 21 | |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 23 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 24 | "android/soong/android" |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 25 | "android/soong/shared" |
| 26 | "path/filepath" |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 29 | func init() { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 30 | android.RegisterModuleType("gensrcs", GenSrcsFactory) |
| 31 | android.RegisterModuleType("genrule", GenRuleFactory) |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 34 | var ( |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 35 | pctx = android.NewPackageContext("android/soong/genrule") |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 36 | ) |
| 37 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 38 | func init() { |
| 39 | pctx.HostBinToolVariable("sboxCmd", "sbox") |
| 40 | } |
| 41 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 42 | type SourceFileGenerator interface { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 43 | GeneratedSourceFiles() android.Paths |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 44 | GeneratedHeaderDirs() android.Paths |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 47 | type HostToolProvider interface { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 48 | HostToolPath() android.OptionalPath |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 49 | } |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 50 | |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame^] | 51 | type hostToolDependencyTag struct { |
| 52 | blueprint.BaseDependencyTag |
| 53 | } |
| 54 | |
| 55 | var hostToolDepTag hostToolDependencyTag |
| 56 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 57 | type generatorProperties struct { |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 58 | // The command to run on one or more input files. Cmd supports substitution of a few variables |
| 59 | // (the actual substitution is implemented in GenerateAndroidBuildActions below) |
| 60 | // |
| 61 | // Available variables for substitution: |
| 62 | // |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 63 | // $(location): the path to the first entry in tools or tool_files |
| 64 | // $(location <label>): the path to the tool or tool_file with name <label> |
| 65 | // $(in): one or more input files |
| 66 | // $(out): a single output file |
Dan Willemsen | 014de6a | 2017-05-09 23:08:55 +0000 | [diff] [blame] | 67 | // $(depfile): a file to which dependencies will be written, if the depfile property is set to true |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 68 | // $(genDir): the sandbox directory for this tool; contains $(out) |
| 69 | // $$: a literal $ |
| 70 | // |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 71 | // All files used must be declared as inputs (to ensure proper up-to-date checks). |
| 72 | // Use "$(in)" directly in Cmd to ensure that all inputs used are declared. |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 73 | Cmd string |
| 74 | |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 75 | // Enable reading a file containing dependencies in gcc format after the command completes |
| 76 | Depfile bool |
| 77 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 78 | // 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] | 79 | // prebuilts or scripts that do not need a module to build them. |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 80 | Tools []string |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 81 | |
| 82 | // Local file that is used as the tool |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 83 | Tool_files []string |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 84 | |
| 85 | // List of directories to export generated headers from |
| 86 | Export_include_dirs []string |
Colin Cross | 708c424 | 2017-01-13 18:05:49 -0800 | [diff] [blame] | 87 | |
| 88 | // list of input files |
| 89 | Srcs []string |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 92 | type generator struct { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 93 | android.ModuleBase |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 94 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 95 | properties generatorProperties |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 96 | |
| 97 | tasks taskFunc |
| 98 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 99 | deps android.Paths |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 100 | rule blueprint.Rule |
| 101 | |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 102 | exportedIncludeDirs android.Paths |
Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 103 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 104 | outputFiles android.Paths |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Colin Cross | 708c424 | 2017-01-13 18:05:49 -0800 | [diff] [blame] | 107 | type taskFunc func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 108 | |
| 109 | type generateTask struct { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 110 | in android.Paths |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 111 | out android.WritablePaths |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 114 | func (g *generator) GeneratedSourceFiles() android.Paths { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 115 | return g.outputFiles |
| 116 | } |
| 117 | |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 118 | func (g *generator) Srcs() android.Paths { |
| 119 | return g.outputFiles |
| 120 | } |
| 121 | |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 122 | func (g *generator) GeneratedHeaderDirs() android.Paths { |
| 123 | return g.exportedIncludeDirs |
Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 126 | func (g *generator) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 708c424 | 2017-01-13 18:05:49 -0800 | [diff] [blame] | 127 | android.ExtractSourcesDeps(ctx, g.properties.Srcs) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 128 | if g, ok := ctx.Module().(*generator); ok { |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 129 | if len(g.properties.Tools) > 0 { |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 130 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 131 | {"arch", ctx.AConfig().BuildOsVariant}, |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame^] | 132 | }, hostToolDepTag, g.properties.Tools...) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 133 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 134 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 137 | func (g *generator) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 138 | if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 { |
| 139 | ctx.ModuleErrorf("at least one `tools` or `tool_files` is required") |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 140 | return |
| 141 | } |
| 142 | |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 143 | if len(g.properties.Export_include_dirs) > 0 { |
| 144 | for _, dir := range g.properties.Export_include_dirs { |
| 145 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, |
| 146 | android.PathForModuleGen(ctx, ctx.ModuleDir(), dir)) |
| 147 | } |
| 148 | } else { |
| 149 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, "")) |
| 150 | } |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 151 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 152 | tools := map[string]android.Path{} |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 153 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 154 | if len(g.properties.Tools) > 0 { |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 155 | ctx.VisitDirectDeps(func(module blueprint.Module) { |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame^] | 156 | switch ctx.OtherModuleDependencyTag(module) { |
| 157 | case android.SourceDepTag: |
| 158 | // Nothing to do |
| 159 | case hostToolDepTag: |
| 160 | tool := ctx.OtherModuleName(module) |
| 161 | |
| 162 | if t, ok := module.(HostToolProvider); ok { |
| 163 | p := t.HostToolPath() |
| 164 | if p.Valid() { |
| 165 | g.deps = append(g.deps, p.Path()) |
| 166 | if _, exists := tools[tool]; !exists { |
| 167 | tools[tool] = p.Path() |
| 168 | } else { |
| 169 | ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], p.Path().String()) |
| 170 | } |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 171 | } else { |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame^] | 172 | ctx.ModuleErrorf("host tool %q missing output file", tool) |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 173 | } |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 174 | } else { |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame^] | 175 | ctx.ModuleErrorf("%q is not a host tool provider", tool) |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 176 | } |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame^] | 177 | default: |
| 178 | ctx.ModuleErrorf("unknown dependency on %q", ctx.OtherModuleName(module)) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 179 | } |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 180 | }) |
| 181 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 182 | |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame^] | 183 | if ctx.Failed() { |
| 184 | return |
| 185 | } |
| 186 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 187 | for _, tool := range g.properties.Tool_files { |
| 188 | toolPath := android.PathForModuleSrc(ctx, tool) |
| 189 | g.deps = append(g.deps, toolPath) |
| 190 | if _, exists := tools[tool]; !exists { |
| 191 | tools[tool] = toolPath |
| 192 | } else { |
| 193 | ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], toolPath.String()) |
| 194 | } |
| 195 | } |
| 196 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 197 | rawCommand, err := android.Expand(g.properties.Cmd, func(name string) (string, error) { |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 198 | switch name { |
| 199 | case "location": |
| 200 | if len(g.properties.Tools) > 0 { |
| 201 | return tools[g.properties.Tools[0]].String(), nil |
| 202 | } else { |
| 203 | return tools[g.properties.Tool_files[0]].String(), nil |
| 204 | } |
| 205 | case "in": |
| 206 | return "${in}", nil |
| 207 | case "out": |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 208 | return "__SBOX_OUT_FILES__", nil |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 209 | case "depfile": |
| 210 | if !g.properties.Depfile { |
| 211 | return "", fmt.Errorf("$(depfile) used without depfile property") |
| 212 | } |
| 213 | return "${depfile}", nil |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 214 | case "genDir": |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 215 | genPath := android.PathForModuleGen(ctx, "").String() |
| 216 | var relativePath string |
Jeff Gaston | 193f2fb | 2017-06-12 15:00:12 -0700 | [diff] [blame] | 217 | var err error |
| 218 | outputPath := android.PathForOutput(ctx).String() |
| 219 | relativePath, err = filepath.Rel(outputPath, genPath) |
| 220 | if err != nil { |
| 221 | panic(err) |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 222 | } |
| 223 | return path.Join("__SBOX_OUT_DIR__", relativePath), nil |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 224 | default: |
| 225 | if strings.HasPrefix(name, "location ") { |
| 226 | label := strings.TrimSpace(strings.TrimPrefix(name, "location ")) |
| 227 | if tool, ok := tools[label]; ok { |
| 228 | return tool.String(), nil |
| 229 | } else { |
| 230 | return "", fmt.Errorf("unknown location label %q", label) |
| 231 | } |
| 232 | } |
| 233 | return "", fmt.Errorf("unknown variable '$(%s)'", name) |
| 234 | } |
| 235 | }) |
| 236 | |
| 237 | if err != nil { |
| 238 | ctx.PropertyErrorf("cmd", "%s", err.Error()) |
Colin Cross | 54c5dd5 | 2017-04-19 14:23:26 -0700 | [diff] [blame] | 239 | return |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 242 | // tell the sbox command which directory to use as its sandbox root |
Jeff Gaston | 193f2fb | 2017-06-12 15:00:12 -0700 | [diff] [blame] | 243 | buildDir := android.PathForOutput(ctx).String() |
| 244 | sandboxPath := shared.TempDirForOutDir(buildDir) |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 245 | |
| 246 | // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written, |
| 247 | // to be replaced later by ninja_strings.go |
Jeff Gaston | 193f2fb | 2017-06-12 15:00:12 -0700 | [diff] [blame] | 248 | sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %q $out", sandboxPath, buildDir, rawCommand) |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 249 | |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 250 | ruleParams := blueprint.RuleParams{ |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 251 | Command: sandboxCommand, |
| 252 | CommandDeps: []string{"$sboxCmd"}, |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 253 | } |
| 254 | var args []string |
| 255 | if g.properties.Depfile { |
| 256 | ruleParams.Deps = blueprint.DepsGCC |
| 257 | args = append(args, "depfile") |
| 258 | } |
| 259 | g.rule = ctx.Rule(pctx, "generator", ruleParams, args...) |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 260 | |
Colin Cross | 708c424 | 2017-01-13 18:05:49 -0800 | [diff] [blame] | 261 | srcFiles := ctx.ExpandSources(g.properties.Srcs, nil) |
| 262 | for _, task := range g.tasks(ctx, srcFiles) { |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 263 | g.generateSourceFile(ctx, task) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 267 | func (g *generator) generateSourceFile(ctx android.ModuleContext, task generateTask) { |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 268 | desc := "generate" |
| 269 | if len(task.out) == 1 { |
| 270 | desc += " " + task.out[0].Base() |
| 271 | } |
| 272 | |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 273 | params := android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 274 | Rule: g.rule, |
| 275 | Description: "generate", |
| 276 | Outputs: task.out, |
| 277 | Inputs: task.in, |
| 278 | Implicits: g.deps, |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 279 | } |
| 280 | if g.properties.Depfile { |
| 281 | depfile := android.GenPathWithExt(ctx, "", task.out[0], task.out[0].Ext()+".d") |
| 282 | params.Depfile = depfile |
| 283 | } |
| 284 | ctx.ModuleBuild(pctx, params) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 285 | |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 286 | for _, outputFile := range task.out { |
| 287 | g.outputFiles = append(g.outputFiles, outputFile) |
| 288 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 291 | func generatorFactory(tasks taskFunc, props ...interface{}) android.Module { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 292 | module := &generator{ |
| 293 | tasks: tasks, |
| 294 | } |
| 295 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 296 | module.AddProperties(props...) |
| 297 | module.AddProperties(&module.properties) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 298 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 299 | android.InitAndroidModule(module) |
| 300 | |
| 301 | return module |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 304 | func GenSrcsFactory() android.Module { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 305 | properties := &genSrcsProperties{} |
| 306 | |
Colin Cross | 708c424 | 2017-01-13 18:05:49 -0800 | [diff] [blame] | 307 | tasks := func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 308 | tasks := make([]generateTask, 0, len(srcFiles)) |
| 309 | for _, in := range srcFiles { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 310 | tasks = append(tasks, generateTask{ |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 311 | in: android.Paths{in}, |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 312 | out: android.WritablePaths{android.GenPathWithExt(ctx, "", in, properties.Output_extension)}, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 313 | }) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 314 | } |
| 315 | return tasks |
| 316 | } |
| 317 | |
| 318 | return generatorFactory(tasks, properties) |
| 319 | } |
| 320 | |
| 321 | type genSrcsProperties struct { |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 322 | // extension that will be substituted for each output file |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 323 | Output_extension string |
| 324 | } |
| 325 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 326 | func GenRuleFactory() android.Module { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 327 | properties := &genRuleProperties{} |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 328 | |
Colin Cross | 708c424 | 2017-01-13 18:05:49 -0800 | [diff] [blame] | 329 | tasks := func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask { |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 330 | outs := make(android.WritablePaths, len(properties.Out)) |
| 331 | for i, out := range properties.Out { |
| 332 | outs[i] = android.PathForModuleGen(ctx, out) |
| 333 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 334 | return []generateTask{ |
| 335 | { |
Colin Cross | 708c424 | 2017-01-13 18:05:49 -0800 | [diff] [blame] | 336 | in: srcFiles, |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 337 | out: outs, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 338 | }, |
| 339 | } |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 340 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 341 | |
| 342 | return generatorFactory(tasks, properties) |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 345 | type genRuleProperties struct { |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 346 | // names of the output files that will be generated |
| 347 | Out []string |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 348 | } |