blob: f390f81311bf333fe1fc61167d9ecaf686df6138 [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"
Dan Willemsen8eded0a2017-09-13 16:07:44 -070023 "github.com/google/blueprint/bootstrap"
Colin Cross5049f022015-03-18 13:28:46 -070024
Colin Cross635c3b02016-05-18 15:37:25 -070025 "android/soong/android"
Jeff Gastonefc1b412017-03-29 17:29:06 -070026 "android/soong/shared"
27 "path/filepath"
Colin Cross5049f022015-03-18 13:28:46 -070028)
29
Colin Cross463a90e2015-06-17 14:20:06 -070030func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070031 android.RegisterModuleType("gensrcs", GenSrcsFactory)
32 android.RegisterModuleType("genrule", GenRuleFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070033}
34
Colin Cross5049f022015-03-18 13:28:46 -070035var (
Colin Cross635c3b02016-05-18 15:37:25 -070036 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross5049f022015-03-18 13:28:46 -070037)
38
Jeff Gastonefc1b412017-03-29 17:29:06 -070039func init() {
40 pctx.HostBinToolVariable("sboxCmd", "sbox")
41}
42
Colin Cross5049f022015-03-18 13:28:46 -070043type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070044 GeneratedSourceFiles() android.Paths
Colin Cross5ed99c62016-11-22 12:55:55 -080045 GeneratedHeaderDirs() android.Paths
Colin Cross5049f022015-03-18 13:28:46 -070046}
47
Colin Crossd350ecd2015-04-28 13:25:36 -070048type HostToolProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -070049 HostToolPath() android.OptionalPath
Colin Crossd350ecd2015-04-28 13:25:36 -070050}
Colin Cross5049f022015-03-18 13:28:46 -070051
Dan Willemsend6ba0d52017-09-13 15:46:47 -070052type hostToolDependencyTag struct {
53 blueprint.BaseDependencyTag
54}
55
56var hostToolDepTag hostToolDependencyTag
57
Colin Cross7d5136f2015-05-11 13:39:40 -070058type generatorProperties struct {
Jeff Gastonefc1b412017-03-29 17:29:06 -070059 // 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 Cross6f080df2016-11-04 15:32:58 -070064 // $(location): the path to the first entry in tools or tool_files
65 // $(location <label>): the path to the tool or tool_file with name <label>
66 // $(in): one or more input files
67 // $(out): a single output file
Dan Willemsen014de6a2017-05-09 23:08:55 +000068 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
Colin Cross6f080df2016-11-04 15:32:58 -070069 // $(genDir): the sandbox directory for this tool; contains $(out)
70 // $$: a literal $
71 //
Jeff Gastonefc1b412017-03-29 17:29:06 -070072 // All files used must be declared as inputs (to ensure proper up-to-date checks).
73 // Use "$(in)" directly in Cmd to ensure that all inputs used are declared.
Colin Cross7d5136f2015-05-11 13:39:40 -070074 Cmd string
75
Colin Cross33bfb0a2016-11-21 17:23:08 -080076 // Enable reading a file containing dependencies in gcc format after the command completes
77 Depfile bool
78
Colin Cross6f080df2016-11-04 15:32:58 -070079 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -070080 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -070081 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070082
83 // Local file that is used as the tool
Colin Cross6f080df2016-11-04 15:32:58 -070084 Tool_files []string
Colin Cross5ed99c62016-11-22 12:55:55 -080085
86 // List of directories to export generated headers from
87 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -080088
89 // list of input files
90 Srcs []string
Colin Cross7d5136f2015-05-11 13:39:40 -070091}
92
Colin Crossd350ecd2015-04-28 13:25:36 -070093type generator struct {
Colin Cross635c3b02016-05-18 15:37:25 -070094 android.ModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -070095
Colin Cross7d5136f2015-05-11 13:39:40 -070096 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -070097
98 tasks taskFunc
99
Colin Cross635c3b02016-05-18 15:37:25 -0700100 deps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700101 rule blueprint.Rule
102
Colin Cross5ed99c62016-11-22 12:55:55 -0800103 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700104
Colin Cross635c3b02016-05-18 15:37:25 -0700105 outputFiles android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700106}
107
Colin Cross708c4242017-01-13 18:05:49 -0800108type taskFunc func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700109
110type generateTask struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700111 in android.Paths
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700112 out android.WritablePaths
Colin Crossd350ecd2015-04-28 13:25:36 -0700113}
114
Colin Cross635c3b02016-05-18 15:37:25 -0700115func (g *generator) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700116 return g.outputFiles
117}
118
Colin Cross068e0fe2016-12-13 15:23:47 -0800119func (g *generator) Srcs() android.Paths {
120 return g.outputFiles
121}
122
Colin Cross5ed99c62016-11-22 12:55:55 -0800123func (g *generator) GeneratedHeaderDirs() android.Paths {
124 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700125}
126
Colin Cross1e676be2016-10-12 14:38:15 -0700127func (g *generator) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross708c4242017-01-13 18:05:49 -0800128 android.ExtractSourcesDeps(ctx, g.properties.Srcs)
Colin Cross6362e272015-10-29 15:25:03 -0700129 if g, ok := ctx.Module().(*generator); ok {
Colin Cross6f080df2016-11-04 15:32:58 -0700130 if len(g.properties.Tools) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800131 ctx.AddFarVariationDependencies([]blueprint.Variation{
Colin Crossa1ad8d12016-06-01 17:09:44 -0700132 {"arch", ctx.AConfig().BuildOsVariant},
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700133 }, hostToolDepTag, g.properties.Tools...)
Colin Cross6362e272015-10-29 15:25:03 -0700134 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700135 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700136}
137
Colin Cross635c3b02016-05-18 15:37:25 -0700138func (g *generator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6f080df2016-11-04 15:32:58 -0700139 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
140 ctx.ModuleErrorf("at least one `tools` or `tool_files` is required")
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700141 return
142 }
143
Colin Cross5ed99c62016-11-22 12:55:55 -0800144 if len(g.properties.Export_include_dirs) > 0 {
145 for _, dir := range g.properties.Export_include_dirs {
146 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
147 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
148 }
149 } else {
150 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
151 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700152
Colin Cross6f080df2016-11-04 15:32:58 -0700153 tools := map[string]android.Path{}
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700154
Colin Cross6f080df2016-11-04 15:32:58 -0700155 if len(g.properties.Tools) > 0 {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700156 ctx.VisitDirectDeps(func(module blueprint.Module) {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700157 switch ctx.OtherModuleDependencyTag(module) {
158 case android.SourceDepTag:
159 // Nothing to do
160 case hostToolDepTag:
161 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700162 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700163
164 if t, ok := module.(HostToolProvider); ok {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700165 path = t.HostToolPath()
166 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
167 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
168 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700169 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700170 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
171 break
Colin Cross6f080df2016-11-04 15:32:58 -0700172 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700173 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700174 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700175 break
176 }
177
178 if path.Valid() {
179 g.deps = append(g.deps, path.Path())
180 if _, exists := tools[tool]; !exists {
181 tools[tool] = path.Path()
182 } else {
183 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], path.Path().String())
184 }
185 } else {
186 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700187 }
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700188 default:
189 ctx.ModuleErrorf("unknown dependency on %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700190 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700191 })
192 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700193
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700194 if ctx.Failed() {
195 return
196 }
197
Colin Cross6f080df2016-11-04 15:32:58 -0700198 for _, tool := range g.properties.Tool_files {
199 toolPath := android.PathForModuleSrc(ctx, tool)
200 g.deps = append(g.deps, toolPath)
201 if _, exists := tools[tool]; !exists {
202 tools[tool] = toolPath
203 } else {
204 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], toolPath.String())
205 }
206 }
207
Jeff Gastonefc1b412017-03-29 17:29:06 -0700208 rawCommand, err := android.Expand(g.properties.Cmd, func(name string) (string, error) {
Colin Cross6f080df2016-11-04 15:32:58 -0700209 switch name {
210 case "location":
211 if len(g.properties.Tools) > 0 {
212 return tools[g.properties.Tools[0]].String(), nil
213 } else {
214 return tools[g.properties.Tool_files[0]].String(), nil
215 }
216 case "in":
217 return "${in}", nil
218 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700219 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800220 case "depfile":
221 if !g.properties.Depfile {
222 return "", fmt.Errorf("$(depfile) used without depfile property")
223 }
224 return "${depfile}", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700225 case "genDir":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700226 genPath := android.PathForModuleGen(ctx, "").String()
227 var relativePath string
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700228 var err error
229 outputPath := android.PathForOutput(ctx).String()
230 relativePath, err = filepath.Rel(outputPath, genPath)
231 if err != nil {
232 panic(err)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700233 }
234 return path.Join("__SBOX_OUT_DIR__", relativePath), 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
248 if err != nil {
249 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700250 return
Colin Cross6f080df2016-11-04 15:32:58 -0700251 }
252
Jeff Gastonefc1b412017-03-29 17:29:06 -0700253 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700254 buildDir := android.PathForOutput(ctx).String()
255 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700256
257 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
258 // to be replaced later by ninja_strings.go
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700259 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %q $out", sandboxPath, buildDir, rawCommand)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700260
Colin Cross33bfb0a2016-11-21 17:23:08 -0800261 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700262 Command: sandboxCommand,
263 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800264 }
265 var args []string
266 if g.properties.Depfile {
267 ruleParams.Deps = blueprint.DepsGCC
268 args = append(args, "depfile")
269 }
270 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700271
Colin Cross708c4242017-01-13 18:05:49 -0800272 srcFiles := ctx.ExpandSources(g.properties.Srcs, nil)
273 for _, task := range g.tasks(ctx, srcFiles) {
Colin Cross6f080df2016-11-04 15:32:58 -0700274 g.generateSourceFile(ctx, task)
Colin Crossd350ecd2015-04-28 13:25:36 -0700275 }
276}
277
Colin Cross6f080df2016-11-04 15:32:58 -0700278func (g *generator) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700279 desc := "generate"
280 if len(task.out) == 1 {
281 desc += " " + task.out[0].Base()
282 }
283
Colin Cross33bfb0a2016-11-21 17:23:08 -0800284 params := android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700285 Rule: g.rule,
286 Description: "generate",
287 Outputs: task.out,
288 Inputs: task.in,
289 Implicits: g.deps,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800290 }
291 if g.properties.Depfile {
292 depfile := android.GenPathWithExt(ctx, "", task.out[0], task.out[0].Ext()+".d")
293 params.Depfile = depfile
294 }
295 ctx.ModuleBuild(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700296
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700297 for _, outputFile := range task.out {
298 g.outputFiles = append(g.outputFiles, outputFile)
299 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700300}
301
Colin Cross36242852017-06-23 15:06:31 -0700302func generatorFactory(tasks taskFunc, props ...interface{}) android.Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700303 module := &generator{
304 tasks: tasks,
305 }
306
Colin Cross36242852017-06-23 15:06:31 -0700307 module.AddProperties(props...)
308 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700309
Colin Cross36242852017-06-23 15:06:31 -0700310 android.InitAndroidModule(module)
311
312 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700313}
314
Colin Cross36242852017-06-23 15:06:31 -0700315func GenSrcsFactory() android.Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700316 properties := &genSrcsProperties{}
317
Colin Cross708c4242017-01-13 18:05:49 -0800318 tasks := func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask {
Colin Crossd350ecd2015-04-28 13:25:36 -0700319 tasks := make([]generateTask, 0, len(srcFiles))
320 for _, in := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700321 tasks = append(tasks, generateTask{
Colin Cross635c3b02016-05-18 15:37:25 -0700322 in: android.Paths{in},
Dan Willemsen21ec4902016-11-02 20:43:13 -0700323 out: android.WritablePaths{android.GenPathWithExt(ctx, "", in, properties.Output_extension)},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700324 })
Colin Crossd350ecd2015-04-28 13:25:36 -0700325 }
326 return tasks
327 }
328
329 return generatorFactory(tasks, properties)
330}
331
332type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700333 // extension that will be substituted for each output file
Colin Cross5049f022015-03-18 13:28:46 -0700334 Output_extension string
335}
336
Colin Cross36242852017-06-23 15:06:31 -0700337func GenRuleFactory() android.Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700338 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700339
Colin Cross708c4242017-01-13 18:05:49 -0800340 tasks := func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700341 outs := make(android.WritablePaths, len(properties.Out))
342 for i, out := range properties.Out {
343 outs[i] = android.PathForModuleGen(ctx, out)
344 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700345 return []generateTask{
346 {
Colin Cross708c4242017-01-13 18:05:49 -0800347 in: srcFiles,
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700348 out: outs,
Colin Crossd350ecd2015-04-28 13:25:36 -0700349 },
350 }
Colin Cross5049f022015-03-18 13:28:46 -0700351 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700352
353 return generatorFactory(tasks, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700354}
355
Colin Crossd350ecd2015-04-28 13:25:36 -0700356type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700357 // names of the output files that will be generated
358 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700359}