blob: 5a2ac845a2b075675fb4c5b714bea2d5b1ed9330 [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"
Colin Cross5049f022015-03-18 13:28:46 -070022
Colin Cross635c3b02016-05-18 15:37:25 -070023 "android/soong/android"
Colin Cross5049f022015-03-18 13:28:46 -070024)
25
Colin Cross463a90e2015-06-17 14:20:06 -070026func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070027 android.RegisterModuleType("gensrcs", GenSrcsFactory)
28 android.RegisterModuleType("genrule", GenRuleFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070029}
30
Colin Cross5049f022015-03-18 13:28:46 -070031var (
Colin Cross635c3b02016-05-18 15:37:25 -070032 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross5049f022015-03-18 13:28:46 -070033)
34
Colin Cross5049f022015-03-18 13:28:46 -070035type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070036 GeneratedSourceFiles() android.Paths
Colin Cross5ed99c62016-11-22 12:55:55 -080037 GeneratedHeaderDirs() android.Paths
Colin Cross5049f022015-03-18 13:28:46 -070038}
39
Colin Crossd350ecd2015-04-28 13:25:36 -070040type HostToolProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -070041 HostToolPath() android.OptionalPath
Colin Crossd350ecd2015-04-28 13:25:36 -070042}
Colin Cross5049f022015-03-18 13:28:46 -070043
Colin Cross7d5136f2015-05-11 13:39:40 -070044type generatorProperties struct {
45 // command to run on one or more input files. Available variables for substitution:
Colin Cross6f080df2016-11-04 15:32:58 -070046 // $(location): the path to the first entry in tools or tool_files
47 // $(location <label>): the path to the tool or tool_file with name <label>
48 // $(in): one or more input files
49 // $(out): a single output file
Colin Cross33bfb0a2016-11-21 17:23:08 -080050 // $(deps): a file to which dependencies will be written, if the depfile property is set to true
Colin Cross6f080df2016-11-04 15:32:58 -070051 // $(genDir): the sandbox directory for this tool; contains $(out)
52 // $$: a literal $
53 //
54 // DO NOT directly reference paths to files in the source tree, or the
55 // command will be missing proper dependencies to re-run if the files
56 // change.
Colin Cross7d5136f2015-05-11 13:39:40 -070057 Cmd string
58
Colin Cross33bfb0a2016-11-21 17:23:08 -080059 // Enable reading a file containing dependencies in gcc format after the command completes
60 Depfile bool
61
Colin Cross6f080df2016-11-04 15:32:58 -070062 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -070063 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -070064 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070065
66 // Local file that is used as the tool
Colin Cross6f080df2016-11-04 15:32:58 -070067 Tool_files []string
Colin Cross5ed99c62016-11-22 12:55:55 -080068
69 // List of directories to export generated headers from
70 Export_include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -070071}
72
Colin Crossd350ecd2015-04-28 13:25:36 -070073type generator struct {
Colin Cross635c3b02016-05-18 15:37:25 -070074 android.ModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -070075
Colin Cross7d5136f2015-05-11 13:39:40 -070076 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -070077
78 tasks taskFunc
79
Colin Cross635c3b02016-05-18 15:37:25 -070080 deps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070081 rule blueprint.Rule
82
Colin Cross5ed99c62016-11-22 12:55:55 -080083 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070084
Colin Cross635c3b02016-05-18 15:37:25 -070085 outputFiles android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070086}
87
Colin Cross635c3b02016-05-18 15:37:25 -070088type taskFunc func(ctx android.ModuleContext) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -070089
90type generateTask struct {
Colin Cross635c3b02016-05-18 15:37:25 -070091 in android.Paths
Dan Willemsen9c8681f2016-09-28 16:21:00 -070092 out android.WritablePaths
Colin Crossd350ecd2015-04-28 13:25:36 -070093}
94
Colin Cross635c3b02016-05-18 15:37:25 -070095func (g *generator) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -070096 return g.outputFiles
97}
98
Colin Cross068e0fe2016-12-13 15:23:47 -080099func (g *generator) Srcs() android.Paths {
100 return g.outputFiles
101}
102
Colin Cross5ed99c62016-11-22 12:55:55 -0800103func (g *generator) GeneratedHeaderDirs() android.Paths {
104 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700105}
106
Colin Cross1e676be2016-10-12 14:38:15 -0700107func (g *generator) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6362e272015-10-29 15:25:03 -0700108 if g, ok := ctx.Module().(*generator); ok {
Colin Cross6f080df2016-11-04 15:32:58 -0700109 if len(g.properties.Tools) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800110 ctx.AddFarVariationDependencies([]blueprint.Variation{
Colin Crossa1ad8d12016-06-01 17:09:44 -0700111 {"arch", ctx.AConfig().BuildOsVariant},
Colin Cross6f080df2016-11-04 15:32:58 -0700112 }, nil, g.properties.Tools...)
Colin Cross6362e272015-10-29 15:25:03 -0700113 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700114 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700115}
116
Colin Cross635c3b02016-05-18 15:37:25 -0700117func (g *generator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6f080df2016-11-04 15:32:58 -0700118 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
119 ctx.ModuleErrorf("at least one `tools` or `tool_files` is required")
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700120 return
121 }
122
Colin Cross5ed99c62016-11-22 12:55:55 -0800123 if len(g.properties.Export_include_dirs) > 0 {
124 for _, dir := range g.properties.Export_include_dirs {
125 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
126 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
127 }
128 } else {
129 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
130 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700131
Colin Cross6f080df2016-11-04 15:32:58 -0700132 tools := map[string]android.Path{}
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700133
Colin Cross6f080df2016-11-04 15:32:58 -0700134 if len(g.properties.Tools) > 0 {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700135 ctx.VisitDirectDeps(func(module blueprint.Module) {
136 if t, ok := module.(HostToolProvider); ok {
137 p := t.HostToolPath()
138 if p.Valid() {
139 g.deps = append(g.deps, p.Path())
Colin Cross6f080df2016-11-04 15:32:58 -0700140 tool := ctx.OtherModuleName(module)
141 if _, exists := tools[tool]; !exists {
142 tools[tool] = p.Path()
143 } else {
144 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], p.Path().String())
145 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700146 } else {
147 ctx.ModuleErrorf("host tool %q missing output file", ctx.OtherModuleName(module))
148 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700149 } else {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700150 ctx.ModuleErrorf("unknown dependency %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700151 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700152 })
153 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700154
Colin Cross6f080df2016-11-04 15:32:58 -0700155 for _, tool := range g.properties.Tool_files {
156 toolPath := android.PathForModuleSrc(ctx, tool)
157 g.deps = append(g.deps, toolPath)
158 if _, exists := tools[tool]; !exists {
159 tools[tool] = toolPath
160 } else {
161 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], toolPath.String())
162 }
163 }
164
165 cmd, err := android.Expand(g.properties.Cmd, func(name string) (string, error) {
166 switch name {
167 case "location":
168 if len(g.properties.Tools) > 0 {
169 return tools[g.properties.Tools[0]].String(), nil
170 } else {
171 return tools[g.properties.Tool_files[0]].String(), nil
172 }
173 case "in":
174 return "${in}", nil
175 case "out":
176 return "${out}", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800177 case "depfile":
178 if !g.properties.Depfile {
179 return "", fmt.Errorf("$(depfile) used without depfile property")
180 }
181 return "${depfile}", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700182 case "genDir":
Colin Cross5ed99c62016-11-22 12:55:55 -0800183 return android.PathForModuleGen(ctx, "").String(), nil
Colin Cross6f080df2016-11-04 15:32:58 -0700184 default:
185 if strings.HasPrefix(name, "location ") {
186 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
187 if tool, ok := tools[label]; ok {
188 return tool.String(), nil
189 } else {
190 return "", fmt.Errorf("unknown location label %q", label)
191 }
192 }
193 return "", fmt.Errorf("unknown variable '$(%s)'", name)
194 }
195 })
196
197 if err != nil {
198 ctx.PropertyErrorf("cmd", "%s", err.Error())
199 }
200
Colin Cross33bfb0a2016-11-21 17:23:08 -0800201 ruleParams := blueprint.RuleParams{
Colin Cross6f080df2016-11-04 15:32:58 -0700202 Command: cmd,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800203 }
204 var args []string
205 if g.properties.Depfile {
206 ruleParams.Deps = blueprint.DepsGCC
207 args = append(args, "depfile")
208 }
209 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700210
Colin Crossd350ecd2015-04-28 13:25:36 -0700211 for _, task := range g.tasks(ctx) {
Colin Cross6f080df2016-11-04 15:32:58 -0700212 g.generateSourceFile(ctx, task)
Colin Crossd350ecd2015-04-28 13:25:36 -0700213 }
214}
215
Colin Cross6f080df2016-11-04 15:32:58 -0700216func (g *generator) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800217 params := android.ModuleBuildParams{
Colin Crossd350ecd2015-04-28 13:25:36 -0700218 Rule: g.rule,
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700219 Outputs: task.out,
Colin Crossd350ecd2015-04-28 13:25:36 -0700220 Inputs: task.in,
221 Implicits: g.deps,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800222 }
223 if g.properties.Depfile {
224 depfile := android.GenPathWithExt(ctx, "", task.out[0], task.out[0].Ext()+".d")
225 params.Depfile = depfile
226 }
227 ctx.ModuleBuild(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700228
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700229 for _, outputFile := range task.out {
230 g.outputFiles = append(g.outputFiles, outputFile)
231 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700232}
233
234func generatorFactory(tasks taskFunc, props ...interface{}) (blueprint.Module, []interface{}) {
235 module := &generator{
236 tasks: tasks,
237 }
238
239 props = append(props, &module.properties)
240
Colin Cross635c3b02016-05-18 15:37:25 -0700241 return android.InitAndroidModule(module, props...)
Colin Crossd350ecd2015-04-28 13:25:36 -0700242}
243
244func GenSrcsFactory() (blueprint.Module, []interface{}) {
245 properties := &genSrcsProperties{}
246
Colin Cross635c3b02016-05-18 15:37:25 -0700247 tasks := func(ctx android.ModuleContext) []generateTask {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700248 srcFiles := ctx.ExpandSources(properties.Srcs, nil)
Colin Crossd350ecd2015-04-28 13:25:36 -0700249 tasks := make([]generateTask, 0, len(srcFiles))
250 for _, in := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700251 tasks = append(tasks, generateTask{
Colin Cross635c3b02016-05-18 15:37:25 -0700252 in: android.Paths{in},
Dan Willemsen21ec4902016-11-02 20:43:13 -0700253 out: android.WritablePaths{android.GenPathWithExt(ctx, "", in, properties.Output_extension)},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700254 })
Colin Crossd350ecd2015-04-28 13:25:36 -0700255 }
256 return tasks
257 }
258
259 return generatorFactory(tasks, properties)
260}
261
262type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700263 // list of input files
Colin Cross5049f022015-03-18 13:28:46 -0700264 Srcs []string
265
Colin Cross7d5136f2015-05-11 13:39:40 -0700266 // extension that will be substituted for each output file
Colin Cross5049f022015-03-18 13:28:46 -0700267 Output_extension string
268}
269
Colin Crossd350ecd2015-04-28 13:25:36 -0700270func GenRuleFactory() (blueprint.Module, []interface{}) {
271 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700272
Colin Cross635c3b02016-05-18 15:37:25 -0700273 tasks := func(ctx android.ModuleContext) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700274 outs := make(android.WritablePaths, len(properties.Out))
275 for i, out := range properties.Out {
276 outs[i] = android.PathForModuleGen(ctx, out)
277 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700278 return []generateTask{
279 {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700280 in: ctx.ExpandSources(properties.Srcs, nil),
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700281 out: outs,
Colin Crossd350ecd2015-04-28 13:25:36 -0700282 },
283 }
Colin Cross5049f022015-03-18 13:28:46 -0700284 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700285
286 return generatorFactory(tasks, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700287}
288
Colin Crossd350ecd2015-04-28 13:25:36 -0700289type genRuleProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700290 // list of input files
Colin Crossd350ecd2015-04-28 13:25:36 -0700291 Srcs []string
Colin Cross5049f022015-03-18 13:28:46 -0700292
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700293 // names of the output files that will be generated
294 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700295}