blob: 921a64e0c1c60af8fbe4d5a586ba5354c658f7d4 [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"
Jeff Gastonefc1b412017-03-29 17:29:06 -070019 "path"
Colin Cross6f080df2016-11-04 15:32:58 -070020 "strings"
Dan Willemsen3f4539b2016-09-28 16:19:10 -070021
Colin Cross70b40592015-03-23 12:57:34 -070022 "github.com/google/blueprint"
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 Cross798bfce2016-10-12 14:28:16 -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 Cross6f080df2016-11-04 15:32:58 -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
Dan Willemsen014de6a2017-05-09 23:08:55 +000067 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
Colin Cross6f080df2016-11-04 15:32:58 -070068 // $(genDir): the sandbox directory for this tool; contains $(out)
69 // $$: a literal $
70 //
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
Colin Crossd350ecd2015-04-28 13:25:36 -070092type generator struct {
Colin Cross635c3b02016-05-18 15:37:25 -070093 android.ModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -070094
Colin Cross7d5136f2015-05-11 13:39:40 -070095 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -070096
97 tasks taskFunc
98
Colin Cross635c3b02016-05-18 15:37:25 -070099 deps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700100 rule blueprint.Rule
101
Colin Cross5ed99c62016-11-22 12:55:55 -0800102 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700103
Colin Cross635c3b02016-05-18 15:37:25 -0700104 outputFiles android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700105}
106
Colin Cross708c4242017-01-13 18:05:49 -0800107type taskFunc func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700108
109type generateTask struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700110 in android.Paths
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700111 out android.WritablePaths
Colin Crossd350ecd2015-04-28 13:25:36 -0700112}
113
Colin Cross635c3b02016-05-18 15:37:25 -0700114func (g *generator) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700115 return g.outputFiles
116}
117
Colin Cross068e0fe2016-12-13 15:23:47 -0800118func (g *generator) Srcs() android.Paths {
119 return g.outputFiles
120}
121
Colin Cross5ed99c62016-11-22 12:55:55 -0800122func (g *generator) GeneratedHeaderDirs() android.Paths {
123 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700124}
125
Colin Cross1e676be2016-10-12 14:38:15 -0700126func (g *generator) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross708c4242017-01-13 18:05:49 -0800127 android.ExtractSourcesDeps(ctx, g.properties.Srcs)
Colin Cross6362e272015-10-29 15:25:03 -0700128 if g, ok := ctx.Module().(*generator); ok {
Colin Cross6f080df2016-11-04 15:32:58 -0700129 if len(g.properties.Tools) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800130 ctx.AddFarVariationDependencies([]blueprint.Variation{
Colin Crossa1ad8d12016-06-01 17:09:44 -0700131 {"arch", ctx.AConfig().BuildOsVariant},
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700132 }, hostToolDepTag, g.properties.Tools...)
Colin Cross6362e272015-10-29 15:25:03 -0700133 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700134 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700135}
136
Colin Cross635c3b02016-05-18 15:37:25 -0700137func (g *generator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6f080df2016-11-04 15:32:58 -0700138 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
139 ctx.ModuleErrorf("at least one `tools` or `tool_files` is required")
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700140 return
141 }
142
Colin Cross5ed99c62016-11-22 12:55:55 -0800143 if len(g.properties.Export_include_dirs) > 0 {
144 for _, dir := range g.properties.Export_include_dirs {
145 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
146 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
147 }
148 } else {
149 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
150 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700151
Colin Cross6f080df2016-11-04 15:32:58 -0700152 tools := map[string]android.Path{}
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700153
Colin Cross6f080df2016-11-04 15:32:58 -0700154 if len(g.properties.Tools) > 0 {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700155 ctx.VisitDirectDeps(func(module blueprint.Module) {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700156 switch ctx.OtherModuleDependencyTag(module) {
157 case android.SourceDepTag:
158 // Nothing to do
159 case hostToolDepTag:
160 tool := ctx.OtherModuleName(module)
161
162 if t, ok := module.(HostToolProvider); ok {
163 p := t.HostToolPath()
164 if p.Valid() {
165 g.deps = append(g.deps, p.Path())
166 if _, exists := tools[tool]; !exists {
167 tools[tool] = p.Path()
168 } else {
169 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], p.Path().String())
170 }
Colin Cross6f080df2016-11-04 15:32:58 -0700171 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700172 ctx.ModuleErrorf("host tool %q missing output file", tool)
Colin Cross6f080df2016-11-04 15:32:58 -0700173 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700174 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700175 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700176 }
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700177 default:
178 ctx.ModuleErrorf("unknown dependency on %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700179 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700180 })
181 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700182
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700183 if ctx.Failed() {
184 return
185 }
186
Colin Cross6f080df2016-11-04 15:32:58 -0700187 for _, tool := range g.properties.Tool_files {
188 toolPath := android.PathForModuleSrc(ctx, tool)
189 g.deps = append(g.deps, toolPath)
190 if _, exists := tools[tool]; !exists {
191 tools[tool] = toolPath
192 } else {
193 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], toolPath.String())
194 }
195 }
196
Jeff Gastonefc1b412017-03-29 17:29:06 -0700197 rawCommand, err := android.Expand(g.properties.Cmd, func(name string) (string, error) {
Colin Cross6f080df2016-11-04 15:32:58 -0700198 switch name {
199 case "location":
200 if len(g.properties.Tools) > 0 {
201 return tools[g.properties.Tools[0]].String(), nil
202 } else {
203 return tools[g.properties.Tool_files[0]].String(), nil
204 }
205 case "in":
206 return "${in}", nil
207 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700208 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800209 case "depfile":
210 if !g.properties.Depfile {
211 return "", fmt.Errorf("$(depfile) used without depfile property")
212 }
213 return "${depfile}", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700214 case "genDir":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700215 genPath := android.PathForModuleGen(ctx, "").String()
216 var relativePath string
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700217 var err error
218 outputPath := android.PathForOutput(ctx).String()
219 relativePath, err = filepath.Rel(outputPath, genPath)
220 if err != nil {
221 panic(err)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700222 }
223 return path.Join("__SBOX_OUT_DIR__", relativePath), nil
Colin Cross6f080df2016-11-04 15:32:58 -0700224 default:
225 if strings.HasPrefix(name, "location ") {
226 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
227 if tool, ok := tools[label]; ok {
228 return tool.String(), nil
229 } else {
230 return "", fmt.Errorf("unknown location label %q", label)
231 }
232 }
233 return "", fmt.Errorf("unknown variable '$(%s)'", name)
234 }
235 })
236
237 if err != nil {
238 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700239 return
Colin Cross6f080df2016-11-04 15:32:58 -0700240 }
241
Jeff Gastonefc1b412017-03-29 17:29:06 -0700242 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700243 buildDir := android.PathForOutput(ctx).String()
244 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700245
246 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
247 // to be replaced later by ninja_strings.go
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700248 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %q $out", sandboxPath, buildDir, rawCommand)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700249
Colin Cross33bfb0a2016-11-21 17:23:08 -0800250 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700251 Command: sandboxCommand,
252 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800253 }
254 var args []string
255 if g.properties.Depfile {
256 ruleParams.Deps = blueprint.DepsGCC
257 args = append(args, "depfile")
258 }
259 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700260
Colin Cross708c4242017-01-13 18:05:49 -0800261 srcFiles := ctx.ExpandSources(g.properties.Srcs, nil)
262 for _, task := range g.tasks(ctx, srcFiles) {
Colin Cross6f080df2016-11-04 15:32:58 -0700263 g.generateSourceFile(ctx, task)
Colin Crossd350ecd2015-04-28 13:25:36 -0700264 }
265}
266
Colin Cross6f080df2016-11-04 15:32:58 -0700267func (g *generator) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700268 desc := "generate"
269 if len(task.out) == 1 {
270 desc += " " + task.out[0].Base()
271 }
272
Colin Cross33bfb0a2016-11-21 17:23:08 -0800273 params := android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700274 Rule: g.rule,
275 Description: "generate",
276 Outputs: task.out,
277 Inputs: task.in,
278 Implicits: g.deps,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800279 }
280 if g.properties.Depfile {
281 depfile := android.GenPathWithExt(ctx, "", task.out[0], task.out[0].Ext()+".d")
282 params.Depfile = depfile
283 }
284 ctx.ModuleBuild(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700285
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700286 for _, outputFile := range task.out {
287 g.outputFiles = append(g.outputFiles, outputFile)
288 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700289}
290
Colin Cross36242852017-06-23 15:06:31 -0700291func generatorFactory(tasks taskFunc, props ...interface{}) android.Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700292 module := &generator{
293 tasks: tasks,
294 }
295
Colin Cross36242852017-06-23 15:06:31 -0700296 module.AddProperties(props...)
297 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700298
Colin Cross36242852017-06-23 15:06:31 -0700299 android.InitAndroidModule(module)
300
301 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700302}
303
Colin Cross36242852017-06-23 15:06:31 -0700304func GenSrcsFactory() android.Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700305 properties := &genSrcsProperties{}
306
Colin Cross708c4242017-01-13 18:05:49 -0800307 tasks := func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask {
Colin Crossd350ecd2015-04-28 13:25:36 -0700308 tasks := make([]generateTask, 0, len(srcFiles))
309 for _, in := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700310 tasks = append(tasks, generateTask{
Colin Cross635c3b02016-05-18 15:37:25 -0700311 in: android.Paths{in},
Dan Willemsen21ec4902016-11-02 20:43:13 -0700312 out: android.WritablePaths{android.GenPathWithExt(ctx, "", in, properties.Output_extension)},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700313 })
Colin Crossd350ecd2015-04-28 13:25:36 -0700314 }
315 return tasks
316 }
317
318 return generatorFactory(tasks, properties)
319}
320
321type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700322 // extension that will be substituted for each output file
Colin Cross5049f022015-03-18 13:28:46 -0700323 Output_extension string
324}
325
Colin Cross36242852017-06-23 15:06:31 -0700326func GenRuleFactory() android.Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700327 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700328
Colin Cross708c4242017-01-13 18:05:49 -0800329 tasks := func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700330 outs := make(android.WritablePaths, len(properties.Out))
331 for i, out := range properties.Out {
332 outs[i] = android.PathForModuleGen(ctx, out)
333 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700334 return []generateTask{
335 {
Colin Cross708c4242017-01-13 18:05:49 -0800336 in: srcFiles,
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700337 out: outs,
Colin Crossd350ecd2015-04-28 13:25:36 -0700338 },
339 }
Colin Cross5049f022015-03-18 13:28:46 -0700340 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700341
342 return generatorFactory(tasks, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700343}
344
Colin Crossd350ecd2015-04-28 13:25:36 -0700345type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700346 // names of the output files that will be generated
347 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700348}