blob: c5de1fdccae939fe0fdc4c054f2bcd6cb4109c24 [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
Colin Cross7d5136f2015-05-11 13:39:40 -070051type generatorProperties struct {
Jeff Gastonefc1b412017-03-29 17:29:06 -070052 // The command to run on one or more input files. Cmd supports substitution of a few variables
53 // (the actual substitution is implemented in GenerateAndroidBuildActions below)
54 //
55 // Available variables for substitution:
56 //
Colin Cross6f080df2016-11-04 15:32:58 -070057 // $(location): the path to the first entry in tools or tool_files
58 // $(location <label>): the path to the tool or tool_file with name <label>
59 // $(in): one or more input files
60 // $(out): a single output file
Dan Willemsen014de6a2017-05-09 23:08:55 +000061 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
Colin Cross6f080df2016-11-04 15:32:58 -070062 // $(genDir): the sandbox directory for this tool; contains $(out)
63 // $$: a literal $
64 //
Jeff Gastonefc1b412017-03-29 17:29:06 -070065 // All files used must be declared as inputs (to ensure proper up-to-date checks).
66 // Use "$(in)" directly in Cmd to ensure that all inputs used are declared.
Colin Cross7d5136f2015-05-11 13:39:40 -070067 Cmd string
68
Colin Cross33bfb0a2016-11-21 17:23:08 -080069 // Enable reading a file containing dependencies in gcc format after the command completes
70 Depfile bool
71
Colin Cross6f080df2016-11-04 15:32:58 -070072 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -070073 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -070074 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070075
76 // Local file that is used as the tool
Colin Cross6f080df2016-11-04 15:32:58 -070077 Tool_files []string
Colin Cross5ed99c62016-11-22 12:55:55 -080078
79 // List of directories to export generated headers from
80 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -080081
82 // list of input files
83 Srcs []string
Colin Cross7d5136f2015-05-11 13:39:40 -070084}
85
Colin Crossd350ecd2015-04-28 13:25:36 -070086type generator struct {
Colin Cross635c3b02016-05-18 15:37:25 -070087 android.ModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -070088
Colin Cross7d5136f2015-05-11 13:39:40 -070089 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -070090
91 tasks taskFunc
92
Colin Cross635c3b02016-05-18 15:37:25 -070093 deps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070094 rule blueprint.Rule
95
Colin Cross5ed99c62016-11-22 12:55:55 -080096 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070097
Colin Cross635c3b02016-05-18 15:37:25 -070098 outputFiles android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070099}
100
Colin Cross708c4242017-01-13 18:05:49 -0800101type taskFunc func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700102
103type generateTask struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700104 in android.Paths
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700105 out android.WritablePaths
Colin Crossd350ecd2015-04-28 13:25:36 -0700106}
107
Colin Cross635c3b02016-05-18 15:37:25 -0700108func (g *generator) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700109 return g.outputFiles
110}
111
Colin Cross068e0fe2016-12-13 15:23:47 -0800112func (g *generator) Srcs() android.Paths {
113 return g.outputFiles
114}
115
Colin Cross5ed99c62016-11-22 12:55:55 -0800116func (g *generator) GeneratedHeaderDirs() android.Paths {
117 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700118}
119
Colin Cross1e676be2016-10-12 14:38:15 -0700120func (g *generator) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross708c4242017-01-13 18:05:49 -0800121 android.ExtractSourcesDeps(ctx, g.properties.Srcs)
Colin Cross6362e272015-10-29 15:25:03 -0700122 if g, ok := ctx.Module().(*generator); ok {
Colin Cross6f080df2016-11-04 15:32:58 -0700123 if len(g.properties.Tools) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800124 ctx.AddFarVariationDependencies([]blueprint.Variation{
Colin Crossa1ad8d12016-06-01 17:09:44 -0700125 {"arch", ctx.AConfig().BuildOsVariant},
Colin Cross6f080df2016-11-04 15:32:58 -0700126 }, nil, g.properties.Tools...)
Colin Cross6362e272015-10-29 15:25:03 -0700127 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700128 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700129}
130
Colin Cross635c3b02016-05-18 15:37:25 -0700131func (g *generator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6f080df2016-11-04 15:32:58 -0700132 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
133 ctx.ModuleErrorf("at least one `tools` or `tool_files` is required")
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700134 return
135 }
136
Colin Cross5ed99c62016-11-22 12:55:55 -0800137 if len(g.properties.Export_include_dirs) > 0 {
138 for _, dir := range g.properties.Export_include_dirs {
139 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
140 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
141 }
142 } else {
143 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
144 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700145
Colin Cross6f080df2016-11-04 15:32:58 -0700146 tools := map[string]android.Path{}
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700147
Colin Cross6f080df2016-11-04 15:32:58 -0700148 if len(g.properties.Tools) > 0 {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700149 ctx.VisitDirectDeps(func(module blueprint.Module) {
150 if t, ok := module.(HostToolProvider); ok {
151 p := t.HostToolPath()
152 if p.Valid() {
153 g.deps = append(g.deps, p.Path())
Colin Cross6f080df2016-11-04 15:32:58 -0700154 tool := ctx.OtherModuleName(module)
155 if _, exists := tools[tool]; !exists {
156 tools[tool] = p.Path()
157 } else {
158 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], p.Path().String())
159 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700160 } else {
161 ctx.ModuleErrorf("host tool %q missing output file", ctx.OtherModuleName(module))
162 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700163 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700164 })
165 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700166
Colin Cross6f080df2016-11-04 15:32:58 -0700167 for _, tool := range g.properties.Tool_files {
168 toolPath := android.PathForModuleSrc(ctx, tool)
169 g.deps = append(g.deps, toolPath)
170 if _, exists := tools[tool]; !exists {
171 tools[tool] = toolPath
172 } else {
173 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], toolPath.String())
174 }
175 }
176
Jeff Gastonefc1b412017-03-29 17:29:06 -0700177 rawCommand, err := android.Expand(g.properties.Cmd, func(name string) (string, error) {
Colin Cross6f080df2016-11-04 15:32:58 -0700178 switch name {
179 case "location":
180 if len(g.properties.Tools) > 0 {
181 return tools[g.properties.Tools[0]].String(), nil
182 } else {
183 return tools[g.properties.Tool_files[0]].String(), nil
184 }
185 case "in":
186 return "${in}", nil
187 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700188 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800189 case "depfile":
190 if !g.properties.Depfile {
191 return "", fmt.Errorf("$(depfile) used without depfile property")
192 }
193 return "${depfile}", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700194 case "genDir":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700195 genPath := android.PathForModuleGen(ctx, "").String()
196 var relativePath string
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700197 var err error
198 outputPath := android.PathForOutput(ctx).String()
199 relativePath, err = filepath.Rel(outputPath, genPath)
200 if err != nil {
201 panic(err)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700202 }
203 return path.Join("__SBOX_OUT_DIR__", relativePath), nil
Colin Cross6f080df2016-11-04 15:32:58 -0700204 default:
205 if strings.HasPrefix(name, "location ") {
206 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
207 if tool, ok := tools[label]; ok {
208 return tool.String(), nil
209 } else {
210 return "", fmt.Errorf("unknown location label %q", label)
211 }
212 }
213 return "", fmt.Errorf("unknown variable '$(%s)'", name)
214 }
215 })
216
217 if err != nil {
218 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700219 return
Colin Cross6f080df2016-11-04 15:32:58 -0700220 }
221
Jeff Gastonefc1b412017-03-29 17:29:06 -0700222 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700223 buildDir := android.PathForOutput(ctx).String()
224 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700225
226 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
227 // to be replaced later by ninja_strings.go
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700228 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %q $out", sandboxPath, buildDir, rawCommand)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700229
Colin Cross33bfb0a2016-11-21 17:23:08 -0800230 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700231 Command: sandboxCommand,
232 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800233 }
234 var args []string
235 if g.properties.Depfile {
236 ruleParams.Deps = blueprint.DepsGCC
237 args = append(args, "depfile")
238 }
239 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700240
Colin Cross708c4242017-01-13 18:05:49 -0800241 srcFiles := ctx.ExpandSources(g.properties.Srcs, nil)
242 for _, task := range g.tasks(ctx, srcFiles) {
Colin Cross6f080df2016-11-04 15:32:58 -0700243 g.generateSourceFile(ctx, task)
Colin Crossd350ecd2015-04-28 13:25:36 -0700244 }
245}
246
Colin Cross6f080df2016-11-04 15:32:58 -0700247func (g *generator) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700248 desc := "generate"
249 if len(task.out) == 1 {
250 desc += " " + task.out[0].Base()
251 }
252
Colin Cross33bfb0a2016-11-21 17:23:08 -0800253 params := android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700254 Rule: g.rule,
255 Description: "generate",
256 Outputs: task.out,
257 Inputs: task.in,
258 Implicits: g.deps,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800259 }
260 if g.properties.Depfile {
261 depfile := android.GenPathWithExt(ctx, "", task.out[0], task.out[0].Ext()+".d")
262 params.Depfile = depfile
263 }
264 ctx.ModuleBuild(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700265
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700266 for _, outputFile := range task.out {
267 g.outputFiles = append(g.outputFiles, outputFile)
268 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700269}
270
Colin Cross36242852017-06-23 15:06:31 -0700271func generatorFactory(tasks taskFunc, props ...interface{}) android.Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700272 module := &generator{
273 tasks: tasks,
274 }
275
Colin Cross36242852017-06-23 15:06:31 -0700276 module.AddProperties(props...)
277 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700278
Colin Cross36242852017-06-23 15:06:31 -0700279 android.InitAndroidModule(module)
280
281 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700282}
283
Colin Cross36242852017-06-23 15:06:31 -0700284func GenSrcsFactory() android.Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700285 properties := &genSrcsProperties{}
286
Colin Cross708c4242017-01-13 18:05:49 -0800287 tasks := func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask {
Colin Crossd350ecd2015-04-28 13:25:36 -0700288 tasks := make([]generateTask, 0, len(srcFiles))
289 for _, in := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700290 tasks = append(tasks, generateTask{
Colin Cross635c3b02016-05-18 15:37:25 -0700291 in: android.Paths{in},
Dan Willemsen21ec4902016-11-02 20:43:13 -0700292 out: android.WritablePaths{android.GenPathWithExt(ctx, "", in, properties.Output_extension)},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700293 })
Colin Crossd350ecd2015-04-28 13:25:36 -0700294 }
295 return tasks
296 }
297
298 return generatorFactory(tasks, properties)
299}
300
301type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700302 // extension that will be substituted for each output file
Colin Cross5049f022015-03-18 13:28:46 -0700303 Output_extension string
304}
305
Colin Cross36242852017-06-23 15:06:31 -0700306func GenRuleFactory() android.Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700307 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700308
Colin Cross708c4242017-01-13 18:05:49 -0800309 tasks := func(ctx android.ModuleContext, srcFiles android.Paths) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700310 outs := make(android.WritablePaths, len(properties.Out))
311 for i, out := range properties.Out {
312 outs[i] = android.PathForModuleGen(ctx, out)
313 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700314 return []generateTask{
315 {
Colin Cross708c4242017-01-13 18:05:49 -0800316 in: srcFiles,
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700317 out: outs,
Colin Crossd350ecd2015-04-28 13:25:36 -0700318 },
319 }
Colin Cross5049f022015-03-18 13:28:46 -0700320 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700321
322 return generatorFactory(tasks, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700323}
324
Colin Crossd350ecd2015-04-28 13:25:36 -0700325type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700326 // names of the output files that will be generated
327 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700328}