blob: cffbf7387f4b497714430195947d67530f17959c [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// 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
15package genrule
16
17import (
Colin Cross6f080df2016-11-04 15:32:58 -070018 "fmt"
19 "strings"
Dan Willemsen3f4539b2016-09-28 16:19:10 -070020
Colin Cross70b40592015-03-23 12:57:34 -070021 "github.com/google/blueprint"
Dan Willemsen8eded0a2017-09-13 16:07:44 -070022 "github.com/google/blueprint/bootstrap"
Colin Cross5049f022015-03-18 13:28:46 -070023
Colin Cross635c3b02016-05-18 15:37:25 -070024 "android/soong/android"
Jeff Gastonefc1b412017-03-29 17:29:06 -070025 "android/soong/shared"
26 "path/filepath"
Colin Cross5049f022015-03-18 13:28:46 -070027)
28
Colin Cross463a90e2015-06-17 14:20:06 -070029func init() {
Colin Cross54190b32017-10-09 15:34:10 -070030 android.RegisterModuleType("gensrcs", GenSrcsFactory)
31 android.RegisterModuleType("genrule", GenRuleFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070032}
33
Colin Cross5049f022015-03-18 13:28:46 -070034var (
Colin Cross635c3b02016-05-18 15:37:25 -070035 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross5049f022015-03-18 13:28:46 -070036)
37
Jeff Gastonefc1b412017-03-29 17:29:06 -070038func init() {
39 pctx.HostBinToolVariable("sboxCmd", "sbox")
40}
41
Colin Cross5049f022015-03-18 13:28:46 -070042type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070043 GeneratedSourceFiles() android.Paths
Colin Cross5ed99c62016-11-22 12:55:55 -080044 GeneratedHeaderDirs() android.Paths
Colin Cross5049f022015-03-18 13:28:46 -070045}
46
Colin Crossd350ecd2015-04-28 13:25:36 -070047type HostToolProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -070048 HostToolPath() android.OptionalPath
Colin Crossd350ecd2015-04-28 13:25:36 -070049}
Colin Cross5049f022015-03-18 13:28:46 -070050
Dan Willemsend6ba0d52017-09-13 15:46:47 -070051type hostToolDependencyTag struct {
52 blueprint.BaseDependencyTag
53}
54
55var hostToolDepTag hostToolDependencyTag
56
Colin Cross7d5136f2015-05-11 13:39:40 -070057type generatorProperties struct {
Jeff Gastonefc1b412017-03-29 17:29:06 -070058 // The command to run on one or more input files. Cmd supports substitution of a few variables
59 // (the actual substitution is implemented in GenerateAndroidBuildActions below)
60 //
61 // Available variables for substitution:
62 //
Colin Cross2296f5b2017-10-17 21:38:14 -070063 // $(location): the path to the first entry in tools or tool_files
64 // $(location <label>): the path to the tool or tool_file with name <label>
65 // $(in): one or more input files
66 // $(out): a single output file
67 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
68 // $(genDir): the sandbox directory for this tool; contains $(out)
69 // $$: a literal $
Colin Cross6f080df2016-11-04 15:32:58 -070070 //
Jeff Gastonefc1b412017-03-29 17:29:06 -070071 // All files used must be declared as inputs (to ensure proper up-to-date checks).
72 // Use "$(in)" directly in Cmd to ensure that all inputs used are declared.
Colin Cross7d5136f2015-05-11 13:39:40 -070073 Cmd string
74
Colin Cross33bfb0a2016-11-21 17:23:08 -080075 // Enable reading a file containing dependencies in gcc format after the command completes
76 Depfile bool
77
Colin Cross6f080df2016-11-04 15:32:58 -070078 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -070079 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -070080 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070081
82 // Local file that is used as the tool
Colin Cross6f080df2016-11-04 15:32:58 -070083 Tool_files []string
Colin Cross5ed99c62016-11-22 12:55:55 -080084
85 // List of directories to export generated headers from
86 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -080087
88 // list of input files
89 Srcs []string
Colin Cross7d5136f2015-05-11 13:39:40 -070090}
91
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070092type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -070093 android.ModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -070094
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070095 // For other packages to make their own genrules with extra
96 // properties
97 Extra interface{}
98
Colin Cross7d5136f2015-05-11 13:39:40 -070099 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700100
101 tasks taskFunc
102
Colin Cross635c3b02016-05-18 15:37:25 -0700103 deps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700104 rule blueprint.Rule
105
Colin Cross5ed99c62016-11-22 12:55:55 -0800106 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700107
Colin Cross635c3b02016-05-18 15:37:25 -0700108 outputFiles android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700109}
110
Colin Cross708c4242017-01-13 18:05:49 -0800111type taskFunc func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700112
113type generateTask struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700114 in android.Paths
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700115 out android.WritablePaths
Colin Crossd350ecd2015-04-28 13:25:36 -0700116}
117
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700118func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700119 return g.outputFiles
120}
121
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700122func (g *Module) Srcs() android.Paths {
Colin Cross068e0fe2016-12-13 15:23:47 -0800123 return g.outputFiles
124}
125
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700126func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800127 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700128}
129
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700130func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross708c4242017-01-13 18:05:49 -0800131 android.ExtractSourcesDeps(ctx, g.properties.Srcs)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700132 if g, ok := ctx.Module().(*Module); ok {
Colin Cross6f080df2016-11-04 15:32:58 -0700133 if len(g.properties.Tools) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800134 ctx.AddFarVariationDependencies([]blueprint.Variation{
Colin Crossa1ad8d12016-06-01 17:09:44 -0700135 {"arch", ctx.AConfig().BuildOsVariant},
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700136 }, hostToolDepTag, g.properties.Tools...)
Colin Cross6362e272015-10-29 15:25:03 -0700137 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700138 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700139}
140
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700141func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6f080df2016-11-04 15:32:58 -0700142 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
143 ctx.ModuleErrorf("at least one `tools` or `tool_files` is required")
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700144 return
145 }
146
Colin Cross5ed99c62016-11-22 12:55:55 -0800147 if len(g.properties.Export_include_dirs) > 0 {
148 for _, dir := range g.properties.Export_include_dirs {
149 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
150 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
151 }
152 } else {
153 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
154 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700155
Colin Cross6f080df2016-11-04 15:32:58 -0700156 tools := map[string]android.Path{}
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700157
Colin Cross6f080df2016-11-04 15:32:58 -0700158 if len(g.properties.Tools) > 0 {
Colin Crossd11fcda2017-10-23 17:59:01 -0700159 ctx.VisitDirectDeps(func(module android.Module) {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700160 switch ctx.OtherModuleDependencyTag(module) {
161 case android.SourceDepTag:
162 // Nothing to do
163 case hostToolDepTag:
164 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700165 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700166
167 if t, ok := module.(HostToolProvider); ok {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700168 path = t.HostToolPath()
169 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
170 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
171 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700172 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700173 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
174 break
Colin Cross6f080df2016-11-04 15:32:58 -0700175 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700176 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700177 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700178 break
179 }
180
181 if path.Valid() {
182 g.deps = append(g.deps, path.Path())
183 if _, exists := tools[tool]; !exists {
184 tools[tool] = path.Path()
185 } else {
186 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], path.Path().String())
187 }
188 } else {
189 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700190 }
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700191 default:
192 ctx.ModuleErrorf("unknown dependency on %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700193 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700194 })
195 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700196
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700197 if ctx.Failed() {
198 return
199 }
200
Colin Cross6f080df2016-11-04 15:32:58 -0700201 for _, tool := range g.properties.Tool_files {
202 toolPath := android.PathForModuleSrc(ctx, tool)
203 g.deps = append(g.deps, toolPath)
204 if _, exists := tools[tool]; !exists {
205 tools[tool] = toolPath
206 } else {
207 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], toolPath.String())
208 }
209 }
210
Jeff Gaston02a684b2017-10-27 14:59:27 -0700211 referencedDepfile := false
212
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800213 ruleOutputDir := android.PathForModuleGen(ctx, "").String()
214
Jeff Gastonefc1b412017-03-29 17:29:06 -0700215 rawCommand, err := android.Expand(g.properties.Cmd, func(name string) (string, error) {
Colin Cross6f080df2016-11-04 15:32:58 -0700216 switch name {
217 case "location":
218 if len(g.properties.Tools) > 0 {
219 return tools[g.properties.Tools[0]].String(), nil
220 } else {
221 return tools[g.properties.Tool_files[0]].String(), nil
222 }
223 case "in":
224 return "${in}", nil
225 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700226 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800227 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700228 referencedDepfile = true
Colin Cross33bfb0a2016-11-21 17:23:08 -0800229 if !g.properties.Depfile {
230 return "", fmt.Errorf("$(depfile) used without depfile property")
231 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700232 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700233 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800234 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700235 default:
236 if strings.HasPrefix(name, "location ") {
237 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
238 if tool, ok := tools[label]; ok {
239 return tool.String(), nil
240 } else {
241 return "", fmt.Errorf("unknown location label %q", label)
242 }
243 }
244 return "", fmt.Errorf("unknown variable '$(%s)'", name)
245 }
246 })
247
Jeff Gaston02a684b2017-10-27 14:59:27 -0700248 if g.properties.Depfile && !referencedDepfile {
249 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
250 }
251
Colin Cross6f080df2016-11-04 15:32:58 -0700252 if err != nil {
253 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700254 return
Colin Cross6f080df2016-11-04 15:32:58 -0700255 }
256
Jeff Gastonefc1b412017-03-29 17:29:06 -0700257 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700258 buildDir := android.PathForOutput(ctx).String()
259 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700260
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 Gaston02a684b2017-10-27 14:59:27 -0700263 depfilePlaceholder := ""
264 if g.properties.Depfile {
265 depfilePlaceholder = "$depfileArgs"
266 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800267
268 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %q %s $allouts", sandboxPath, ruleOutputDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700269
Colin Cross33bfb0a2016-11-21 17:23:08 -0800270 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700271 Command: sandboxCommand,
272 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800273 }
Colin Cross15e86d92017-10-20 15:07:08 -0700274 args := []string{"allouts"}
Colin Cross33bfb0a2016-11-21 17:23:08 -0800275 if g.properties.Depfile {
276 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700277 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800278 }
279 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700280
Colin Cross708c4242017-01-13 18:05:49 -0800281 srcFiles := ctx.ExpandSources(g.properties.Srcs, nil)
282 for _, task := range g.tasks(ctx, srcFiles) {
Colin Cross6f080df2016-11-04 15:32:58 -0700283 g.generateSourceFile(ctx, task)
Colin Crossd350ecd2015-04-28 13:25:36 -0700284 }
285}
286
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700287func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700288 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700289 if len(task.out) == 0 {
290 ctx.ModuleErrorf("must have at least one output file")
291 return
292 }
Colin Cross67a5c132017-05-09 13:45:28 -0700293 if len(task.out) == 1 {
294 desc += " " + task.out[0].Base()
295 }
296
Jeff Gaston02a684b2017-10-27 14:59:27 -0700297 var depFile android.ModuleGenPath
298 if g.properties.Depfile {
299 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
300 }
301
Colin Crossae887032017-10-23 17:16:14 -0700302 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700303 Rule: g.rule,
304 Description: "generate",
305 Output: task.out[0],
306 ImplicitOutputs: task.out[1:],
307 Inputs: task.in,
308 Implicits: g.deps,
309 Args: map[string]string{
310 "allouts": strings.Join(task.out.Strings(), " "),
311 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800312 }
313 if g.properties.Depfile {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700314 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
315 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800316 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700317
Colin Crossae887032017-10-23 17:16:14 -0700318 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700319
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700320 for _, outputFile := range task.out {
321 g.outputFiles = append(g.outputFiles, outputFile)
322 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700323}
324
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700325func generatorFactory(tasks taskFunc, props ...interface{}) *Module {
326 module := &Module{
Colin Crossd350ecd2015-04-28 13:25:36 -0700327 tasks: tasks,
328 }
329
Colin Cross36242852017-06-23 15:06:31 -0700330 module.AddProperties(props...)
331 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700332
Colin Cross36242852017-06-23 15:06:31 -0700333 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700334}
335
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700336func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700337 properties := &genSrcsProperties{}
338
Colin Cross708c4242017-01-13 18:05:49 -0800339 tasks := func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask {
Colin Crossd350ecd2015-04-28 13:25:36 -0700340 tasks := make([]generateTask, 0, len(srcFiles))
341 for _, in := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700342 tasks = append(tasks, generateTask{
Colin Cross635c3b02016-05-18 15:37:25 -0700343 in: android.Paths{in},
Dan Willemsen21ec4902016-11-02 20:43:13 -0700344 out: android.WritablePaths{android.GenPathWithExt(ctx, "", in, properties.Output_extension)},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700345 })
Colin Crossd350ecd2015-04-28 13:25:36 -0700346 }
347 return tasks
348 }
349
350 return generatorFactory(tasks, properties)
351}
352
Colin Cross54190b32017-10-09 15:34:10 -0700353func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700354 m := NewGenSrcs()
355 android.InitAndroidModule(m)
356 return m
357}
358
Colin Crossd350ecd2015-04-28 13:25:36 -0700359type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700360 // extension that will be substituted for each output file
Colin Cross5049f022015-03-18 13:28:46 -0700361 Output_extension string
362}
363
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700364func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700365 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700366
Colin Cross708c4242017-01-13 18:05:49 -0800367 tasks := func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700368 outs := make(android.WritablePaths, len(properties.Out))
369 for i, out := range properties.Out {
370 outs[i] = android.PathForModuleGen(ctx, out)
371 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700372 return []generateTask{
373 {
Colin Cross708c4242017-01-13 18:05:49 -0800374 in: srcFiles,
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700375 out: outs,
Colin Crossd350ecd2015-04-28 13:25:36 -0700376 },
377 }
Colin Cross5049f022015-03-18 13:28:46 -0700378 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700379
380 return generatorFactory(tasks, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700381}
382
Colin Cross54190b32017-10-09 15:34:10 -0700383func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700384 m := NewGenRule()
385 android.InitAndroidModule(m)
386 return m
387}
388
Colin Crossd350ecd2015-04-28 13:25:36 -0700389type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700390 // names of the output files that will be generated
391 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700392}