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