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