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" |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
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 | 2296f5b | 2017-10-17 21:38:14 -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 |
| 68 | // $(depfile): a file to which dependencies will be written, if the depfile property is set to true |
| 69 | // $(genDir): the sandbox directory for this tool; contains $(out) |
| 70 | // $$: a literal $ |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 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. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 74 | Cmd *string |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 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 |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 77 | Depfile *bool |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 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 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 102 | taskGenerator taskFunc |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 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 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 112 | type taskFunc func(ctx android.ModuleContext, rawCommand string, 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 |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 117 | cmd string |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 120 | func (g *Module) GeneratedSourceFiles() android.Paths { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 121 | return g.outputFiles |
| 122 | } |
| 123 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 124 | func (g *Module) Srcs() android.Paths { |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 125 | return g.outputFiles |
| 126 | } |
| 127 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 128 | func (g *Module) GeneratedHeaderDirs() android.Paths { |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 129 | return g.exportedIncludeDirs |
Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 132 | func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 708c424 | 2017-01-13 18:05:49 -0800 | [diff] [blame] | 133 | android.ExtractSourcesDeps(ctx, g.properties.Srcs) |
Colin Cross | ac87c99 | 2017-12-08 18:23:13 -0800 | [diff] [blame] | 134 | android.ExtractSourcesDeps(ctx, g.properties.Tool_files) |
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 | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 138 | {"arch", ctx.Config().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 | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 145 | if len(g.properties.Export_include_dirs) > 0 { |
| 146 | for _, dir := range g.properties.Export_include_dirs { |
| 147 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, |
| 148 | android.PathForModuleGen(ctx, ctx.ModuleDir(), dir)) |
| 149 | } |
| 150 | } else { |
| 151 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, "")) |
| 152 | } |
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 | tools := map[string]android.Path{} |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 155 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 156 | if len(g.properties.Tools) > 0 { |
Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 157 | ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) { |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 158 | switch ctx.OtherModuleDependencyTag(module) { |
| 159 | case android.SourceDepTag: |
| 160 | // Nothing to do |
| 161 | case hostToolDepTag: |
| 162 | tool := ctx.OtherModuleName(module) |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 163 | var path android.OptionalPath |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 164 | |
| 165 | if t, ok := module.(HostToolProvider); ok { |
Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 166 | if !t.(android.Module).Enabled() { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 167 | if ctx.Config().AllowMissingDependencies() { |
Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 168 | ctx.AddMissingDependencies([]string{tool}) |
| 169 | } else { |
| 170 | ctx.ModuleErrorf("depends on disabled module %q", tool) |
| 171 | } |
| 172 | break |
| 173 | } |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 174 | path = t.HostToolPath() |
| 175 | } else if t, ok := module.(bootstrap.GoBinaryTool); ok { |
| 176 | if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil { |
| 177 | path = android.OptionalPathForPath(android.PathForOutput(ctx, s)) |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 178 | } else { |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 179 | ctx.ModuleErrorf("cannot find path for %q: %v", tool, err) |
| 180 | break |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 181 | } |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 182 | } else { |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 183 | ctx.ModuleErrorf("%q is not a host tool provider", tool) |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 184 | break |
| 185 | } |
| 186 | |
| 187 | if path.Valid() { |
| 188 | g.deps = append(g.deps, path.Path()) |
| 189 | if _, exists := tools[tool]; !exists { |
| 190 | tools[tool] = path.Path() |
| 191 | } else { |
| 192 | ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], path.Path().String()) |
| 193 | } |
| 194 | } else { |
| 195 | ctx.ModuleErrorf("host tool %q missing output file", tool) |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 196 | } |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 197 | default: |
| 198 | ctx.ModuleErrorf("unknown dependency on %q", ctx.OtherModuleName(module)) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 199 | } |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 200 | }) |
| 201 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 202 | |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 203 | if ctx.Failed() { |
| 204 | return |
| 205 | } |
| 206 | |
Colin Cross | c331599 | 2017-12-08 19:12:36 -0800 | [diff] [blame^] | 207 | toolFiles := ctx.ExpandSources(g.properties.Tool_files, nil) |
Colin Cross | ac87c99 | 2017-12-08 18:23:13 -0800 | [diff] [blame] | 208 | for _, tool := range toolFiles { |
| 209 | g.deps = append(g.deps, tool) |
| 210 | if _, exists := tools[tool.Rel()]; !exists { |
| 211 | tools[tool.Rel()] = tool |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 212 | } else { |
Colin Cross | ac87c99 | 2017-12-08 18:23:13 -0800 | [diff] [blame] | 213 | ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool.Rel()], tool.Rel()) |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 217 | referencedDepfile := false |
| 218 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 219 | srcFiles := ctx.ExpandSources(g.properties.Srcs, nil) |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 220 | task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles) |
Jeff Gaston | 5acec2b | 2017-11-06 14:15:16 -0800 | [diff] [blame] | 221 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 222 | rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) { |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 223 | switch name { |
| 224 | case "location": |
Colin Cross | c331599 | 2017-12-08 19:12:36 -0800 | [diff] [blame^] | 225 | if len(g.properties.Tools) == 0 && len(toolFiles) == 0 { |
| 226 | return "", fmt.Errorf("at least one `tools` or `tool_files` is required if $(location) is used") |
| 227 | } |
| 228 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 229 | if len(g.properties.Tools) > 0 { |
| 230 | return tools[g.properties.Tools[0]].String(), nil |
| 231 | } else { |
Colin Cross | ac87c99 | 2017-12-08 18:23:13 -0800 | [diff] [blame] | 232 | return tools[toolFiles[0].Rel()].String(), nil |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 233 | } |
| 234 | case "in": |
| 235 | return "${in}", nil |
| 236 | case "out": |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 237 | return "__SBOX_OUT_FILES__", nil |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 238 | case "depfile": |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 239 | referencedDepfile = true |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 240 | if !Bool(g.properties.Depfile) { |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 241 | return "", fmt.Errorf("$(depfile) used without depfile property") |
| 242 | } |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 243 | return "__SBOX_DEPFILE__", nil |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 244 | case "genDir": |
Jeff Gaston | 5acec2b | 2017-11-06 14:15:16 -0800 | [diff] [blame] | 245 | return "__SBOX_OUT_DIR__", nil |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 246 | default: |
| 247 | if strings.HasPrefix(name, "location ") { |
| 248 | label := strings.TrimSpace(strings.TrimPrefix(name, "location ")) |
| 249 | if tool, ok := tools[label]; ok { |
| 250 | return tool.String(), nil |
| 251 | } else { |
| 252 | return "", fmt.Errorf("unknown location label %q", label) |
| 253 | } |
| 254 | } |
| 255 | return "", fmt.Errorf("unknown variable '$(%s)'", name) |
| 256 | } |
| 257 | }) |
| 258 | |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 259 | if Bool(g.properties.Depfile) && !referencedDepfile { |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 260 | ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd") |
| 261 | } |
| 262 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 263 | if err != nil { |
| 264 | ctx.PropertyErrorf("cmd", "%s", err.Error()) |
Colin Cross | 54c5dd5 | 2017-04-19 14:23:26 -0700 | [diff] [blame] | 265 | return |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 268 | // tell the sbox command which directory to use as its sandbox root |
Jeff Gaston | 193f2fb | 2017-06-12 15:00:12 -0700 | [diff] [blame] | 269 | buildDir := android.PathForOutput(ctx).String() |
| 270 | sandboxPath := shared.TempDirForOutDir(buildDir) |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 271 | |
| 272 | // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written, |
| 273 | // to be replaced later by ninja_strings.go |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 274 | depfilePlaceholder := "" |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 275 | if Bool(g.properties.Depfile) { |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 276 | depfilePlaceholder = "$depfileArgs" |
| 277 | } |
Jeff Gaston | 5acec2b | 2017-11-06 14:15:16 -0800 | [diff] [blame] | 278 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 279 | genDir := android.PathForModuleGen(ctx) |
| 280 | 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] | 281 | |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 282 | ruleParams := blueprint.RuleParams{ |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 283 | Command: sandboxCommand, |
| 284 | CommandDeps: []string{"$sboxCmd"}, |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 285 | } |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 286 | args := []string{"allouts"} |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 287 | if Bool(g.properties.Depfile) { |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 288 | ruleParams.Deps = blueprint.DepsGCC |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 289 | args = append(args, "depfileArgs") |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 290 | } |
| 291 | g.rule = ctx.Rule(pctx, "generator", ruleParams, args...) |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 292 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 293 | g.generateSourceFile(ctx, task) |
| 294 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 297 | func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) { |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 298 | desc := "generate" |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 299 | if len(task.out) == 0 { |
| 300 | ctx.ModuleErrorf("must have at least one output file") |
| 301 | return |
| 302 | } |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 303 | if len(task.out) == 1 { |
| 304 | desc += " " + task.out[0].Base() |
| 305 | } |
| 306 | |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 307 | var depFile android.ModuleGenPath |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 308 | if Bool(g.properties.Depfile) { |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 309 | depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d") |
| 310 | } |
| 311 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 312 | params := android.BuildParams{ |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 313 | Rule: g.rule, |
| 314 | Description: "generate", |
| 315 | Output: task.out[0], |
| 316 | ImplicitOutputs: task.out[1:], |
| 317 | Inputs: task.in, |
| 318 | Implicits: g.deps, |
| 319 | Args: map[string]string{ |
| 320 | "allouts": strings.Join(task.out.Strings(), " "), |
| 321 | }, |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 322 | } |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 323 | if Bool(g.properties.Depfile) { |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 324 | params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d") |
| 325 | params.Args["depfileArgs"] = "--depfile-out " + depFile.String() |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 326 | } |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 327 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 328 | ctx.Build(pctx, params) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 329 | |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 330 | for _, outputFile := range task.out { |
| 331 | g.outputFiles = append(g.outputFiles, outputFile) |
| 332 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 335 | func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 336 | module := &Module{ |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 337 | taskGenerator: taskGenerator, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 340 | module.AddProperties(props...) |
| 341 | module.AddProperties(&module.properties) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 342 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 343 | return module |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 346 | func NewGenSrcs() *Module { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 347 | properties := &genSrcsProperties{} |
| 348 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 349 | taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask { |
| 350 | commands := []string{} |
| 351 | outFiles := android.WritablePaths{} |
| 352 | genPath := android.PathForModuleGen(ctx).String() |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 353 | for _, in := range srcFiles { |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 354 | outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension)) |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 355 | outFiles = append(outFiles, outFile) |
| 356 | |
| 357 | // replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>" |
| 358 | relOut, err := filepath.Rel(genPath, outFile.String()) |
| 359 | if err != nil { |
| 360 | panic(fmt.Sprintf("Could not make ${out} relative: %v", err)) |
| 361 | } |
| 362 | sandboxOutfile := filepath.Join("__SBOX_OUT_DIR__", relOut) |
| 363 | command, err := android.Expand(rawCommand, func(name string) (string, error) { |
| 364 | switch name { |
| 365 | case "in": |
| 366 | return in.String(), nil |
| 367 | case "out": |
| 368 | return sandboxOutfile, nil |
| 369 | default: |
| 370 | return "$(" + name + ")", nil |
| 371 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 372 | }) |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 373 | if err != nil { |
| 374 | ctx.PropertyErrorf("cmd", err.Error()) |
| 375 | } |
| 376 | |
| 377 | // escape the command in case for example it contains '#', an odd number of '"', etc |
| 378 | command = fmt.Sprintf("bash -c %v", proptools.ShellEscape([]string{command})[0]) |
| 379 | commands = append(commands, command) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 380 | } |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 381 | fullCommand := strings.Join(commands, " && ") |
| 382 | |
| 383 | return generateTask{ |
| 384 | in: srcFiles, |
| 385 | out: outFiles, |
| 386 | cmd: fullCommand, |
| 387 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 390 | return generatorFactory(taskGenerator, properties) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 393 | func GenSrcsFactory() android.Module { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 394 | m := NewGenSrcs() |
| 395 | android.InitAndroidModule(m) |
| 396 | return m |
| 397 | } |
| 398 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 399 | type genSrcsProperties struct { |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 400 | // extension that will be substituted for each output file |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 401 | Output_extension *string |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 404 | func NewGenRule() *Module { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 405 | properties := &genRuleProperties{} |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 406 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 407 | taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask { |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 408 | outs := make(android.WritablePaths, len(properties.Out)) |
| 409 | for i, out := range properties.Out { |
| 410 | outs[i] = android.PathForModuleGen(ctx, out) |
| 411 | } |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 412 | return generateTask{ |
| 413 | in: srcFiles, |
| 414 | out: outs, |
| 415 | cmd: rawCommand, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 416 | } |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 417 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 418 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 419 | return generatorFactory(taskGenerator, properties) |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 422 | func GenRuleFactory() android.Module { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 423 | m := NewGenRule() |
| 424 | android.InitAndroidModule(m) |
| 425 | return m |
| 426 | } |
| 427 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 428 | type genRuleProperties struct { |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 429 | // names of the output files that will be generated |
| 430 | Out []string |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 431 | } |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 432 | |
| 433 | var Bool = proptools.Bool |
| 434 | var String = proptools.String |