blob: 4a734dad2e5027c29814861ff0281da0f74ca79e [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 Cross54190b32017-10-09 15:34:10 -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 Cross2296f5b2017-10-17 21:38:14 -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
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 Cross6f080df2016-11-04 15:32:58 -070071 //
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
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070093type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -070094 android.ModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -070095
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070096 // For other packages to make their own genrules with extra
97 // properties
98 Extra interface{}
99
Colin Cross7d5136f2015-05-11 13:39:40 -0700100 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700101
102 tasks taskFunc
103
Colin Cross635c3b02016-05-18 15:37:25 -0700104 deps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700105 rule blueprint.Rule
106
Colin Cross5ed99c62016-11-22 12:55:55 -0800107 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700108
Colin Cross635c3b02016-05-18 15:37:25 -0700109 outputFiles android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700110}
111
Colin Cross708c4242017-01-13 18:05:49 -0800112type taskFunc func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700113
114type generateTask struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700115 in android.Paths
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700116 out android.WritablePaths
Colin Crossd350ecd2015-04-28 13:25:36 -0700117}
118
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700119func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700120 return g.outputFiles
121}
122
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700123func (g *Module) Srcs() android.Paths {
Colin Cross068e0fe2016-12-13 15:23:47 -0800124 return g.outputFiles
125}
126
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700127func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800128 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700129}
130
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700131func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross708c4242017-01-13 18:05:49 -0800132 android.ExtractSourcesDeps(ctx, g.properties.Srcs)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700133 if g, ok := ctx.Module().(*Module); ok {
Colin Cross6f080df2016-11-04 15:32:58 -0700134 if len(g.properties.Tools) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800135 ctx.AddFarVariationDependencies([]blueprint.Variation{
Colin Crossa1ad8d12016-06-01 17:09:44 -0700136 {"arch", ctx.AConfig().BuildOsVariant},
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700137 }, hostToolDepTag, g.properties.Tools...)
Colin Cross6362e272015-10-29 15:25:03 -0700138 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700139 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700140}
141
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700142func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6f080df2016-11-04 15:32:58 -0700143 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
144 ctx.ModuleErrorf("at least one `tools` or `tool_files` is required")
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700145 return
146 }
147
Colin Cross5ed99c62016-11-22 12:55:55 -0800148 if len(g.properties.Export_include_dirs) > 0 {
149 for _, dir := range g.properties.Export_include_dirs {
150 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
151 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
152 }
153 } else {
154 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
155 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700156
Colin Cross6f080df2016-11-04 15:32:58 -0700157 tools := map[string]android.Path{}
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700158
Colin Cross6f080df2016-11-04 15:32:58 -0700159 if len(g.properties.Tools) > 0 {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700160 ctx.VisitDirectDeps(func(module blueprint.Module) {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700161 switch ctx.OtherModuleDependencyTag(module) {
162 case android.SourceDepTag:
163 // Nothing to do
164 case hostToolDepTag:
165 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700166 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700167
168 if t, ok := module.(HostToolProvider); ok {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700169 path = t.HostToolPath()
170 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
171 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
172 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700173 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700174 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
175 break
Colin Cross6f080df2016-11-04 15:32:58 -0700176 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700177 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700178 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700179 break
180 }
181
182 if path.Valid() {
183 g.deps = append(g.deps, path.Path())
184 if _, exists := tools[tool]; !exists {
185 tools[tool] = path.Path()
186 } else {
187 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], path.Path().String())
188 }
189 } else {
190 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700191 }
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700192 default:
193 ctx.ModuleErrorf("unknown dependency on %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700194 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700195 })
196 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700197
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700198 if ctx.Failed() {
199 return
200 }
201
Colin Cross6f080df2016-11-04 15:32:58 -0700202 for _, tool := range g.properties.Tool_files {
203 toolPath := android.PathForModuleSrc(ctx, tool)
204 g.deps = append(g.deps, toolPath)
205 if _, exists := tools[tool]; !exists {
206 tools[tool] = toolPath
207 } else {
208 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], toolPath.String())
209 }
210 }
211
Jeff Gastonefc1b412017-03-29 17:29:06 -0700212 rawCommand, err := android.Expand(g.properties.Cmd, func(name string) (string, error) {
Colin Cross6f080df2016-11-04 15:32:58 -0700213 switch name {
214 case "location":
215 if len(g.properties.Tools) > 0 {
216 return tools[g.properties.Tools[0]].String(), nil
217 } else {
218 return tools[g.properties.Tool_files[0]].String(), nil
219 }
220 case "in":
221 return "${in}", nil
222 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700223 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800224 case "depfile":
225 if !g.properties.Depfile {
226 return "", fmt.Errorf("$(depfile) used without depfile property")
227 }
228 return "${depfile}", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700229 case "genDir":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700230 genPath := android.PathForModuleGen(ctx, "").String()
231 var relativePath string
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700232 var err error
233 outputPath := android.PathForOutput(ctx).String()
234 relativePath, err = filepath.Rel(outputPath, genPath)
235 if err != nil {
236 panic(err)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700237 }
238 return path.Join("__SBOX_OUT_DIR__", relativePath), nil
Colin Cross6f080df2016-11-04 15:32:58 -0700239 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
252 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 Gaston193f2fb2017-06-12 15:00:12 -0700263 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %q $out", sandboxPath, buildDir, rawCommand)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700264
Colin Cross33bfb0a2016-11-21 17:23:08 -0800265 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700266 Command: sandboxCommand,
267 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800268 }
269 var args []string
270 if g.properties.Depfile {
271 ruleParams.Deps = blueprint.DepsGCC
272 args = append(args, "depfile")
273 }
274 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700275
Colin Cross708c4242017-01-13 18:05:49 -0800276 srcFiles := ctx.ExpandSources(g.properties.Srcs, nil)
277 for _, task := range g.tasks(ctx, srcFiles) {
Colin Cross6f080df2016-11-04 15:32:58 -0700278 g.generateSourceFile(ctx, task)
Colin Crossd350ecd2015-04-28 13:25:36 -0700279 }
280}
281
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700282func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700283 desc := "generate"
284 if len(task.out) == 1 {
285 desc += " " + task.out[0].Base()
286 }
287
Colin Cross33bfb0a2016-11-21 17:23:08 -0800288 params := android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700289 Rule: g.rule,
290 Description: "generate",
291 Outputs: task.out,
292 Inputs: task.in,
293 Implicits: g.deps,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800294 }
295 if g.properties.Depfile {
296 depfile := android.GenPathWithExt(ctx, "", task.out[0], task.out[0].Ext()+".d")
297 params.Depfile = depfile
298 }
299 ctx.ModuleBuild(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700300
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700301 for _, outputFile := range task.out {
302 g.outputFiles = append(g.outputFiles, outputFile)
303 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700304}
305
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700306func generatorFactory(tasks taskFunc, props ...interface{}) *Module {
307 module := &Module{
Colin Crossd350ecd2015-04-28 13:25:36 -0700308 tasks: tasks,
309 }
310
Colin Cross36242852017-06-23 15:06:31 -0700311 module.AddProperties(props...)
312 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700313
Colin Cross36242852017-06-23 15:06:31 -0700314 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700315}
316
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700317func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700318 properties := &genSrcsProperties{}
319
Colin Cross708c4242017-01-13 18:05:49 -0800320 tasks := func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask {
Colin Crossd350ecd2015-04-28 13:25:36 -0700321 tasks := make([]generateTask, 0, len(srcFiles))
322 for _, in := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700323 tasks = append(tasks, generateTask{
Colin Cross635c3b02016-05-18 15:37:25 -0700324 in: android.Paths{in},
Dan Willemsen21ec4902016-11-02 20:43:13 -0700325 out: android.WritablePaths{android.GenPathWithExt(ctx, "", in, properties.Output_extension)},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700326 })
Colin Crossd350ecd2015-04-28 13:25:36 -0700327 }
328 return tasks
329 }
330
331 return generatorFactory(tasks, properties)
332}
333
Colin Cross54190b32017-10-09 15:34:10 -0700334func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700335 m := NewGenSrcs()
336 android.InitAndroidModule(m)
337 return m
338}
339
Colin Crossd350ecd2015-04-28 13:25:36 -0700340type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700341 // extension that will be substituted for each output file
Colin Cross5049f022015-03-18 13:28:46 -0700342 Output_extension string
343}
344
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700345func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700346 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700347
Colin Cross708c4242017-01-13 18:05:49 -0800348 tasks := func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700349 outs := make(android.WritablePaths, len(properties.Out))
350 for i, out := range properties.Out {
351 outs[i] = android.PathForModuleGen(ctx, out)
352 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700353 return []generateTask{
354 {
Colin Cross708c4242017-01-13 18:05:49 -0800355 in: srcFiles,
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700356 out: outs,
Colin Crossd350ecd2015-04-28 13:25:36 -0700357 },
358 }
Colin Cross5049f022015-03-18 13:28:46 -0700359 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700360
361 return generatorFactory(tasks, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700362}
363
Colin Cross54190b32017-10-09 15:34:10 -0700364func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700365 m := NewGenRule()
366 android.InitAndroidModule(m)
367 return m
368}
369
Colin Crossd350ecd2015-04-28 13:25:36 -0700370type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700371 // names of the output files that will be generated
372 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700373}