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