blob: b1ce804674fc362468c3b2d492d3d5863e3d8bb9 [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
37 GeneratedHeaderDir() android.Path
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 Cross7d5136f2015-05-11 13:39:40 -070068}
69
Colin Crossd350ecd2015-04-28 13:25:36 -070070type generator struct {
Colin Cross635c3b02016-05-18 15:37:25 -070071 android.ModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -070072
Colin Cross7d5136f2015-05-11 13:39:40 -070073 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -070074
75 tasks taskFunc
76
Colin Cross635c3b02016-05-18 15:37:25 -070077 deps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070078 rule blueprint.Rule
79
Colin Cross635c3b02016-05-18 15:37:25 -070080 genPath android.Path
Dan Willemsenb40aab62016-04-20 14:21:14 -070081
Colin Cross635c3b02016-05-18 15:37:25 -070082 outputFiles android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070083}
84
Colin Cross635c3b02016-05-18 15:37:25 -070085type taskFunc func(ctx android.ModuleContext) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -070086
87type generateTask struct {
Colin Cross635c3b02016-05-18 15:37:25 -070088 in android.Paths
Dan Willemsen9c8681f2016-09-28 16:21:00 -070089 out android.WritablePaths
Colin Crossd350ecd2015-04-28 13:25:36 -070090}
91
Colin Cross635c3b02016-05-18 15:37:25 -070092func (g *generator) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -070093 return g.outputFiles
94}
95
Colin Cross635c3b02016-05-18 15:37:25 -070096func (g *generator) GeneratedHeaderDir() android.Path {
Dan Willemsenb40aab62016-04-20 14:21:14 -070097 return g.genPath
98}
99
Colin Cross1e676be2016-10-12 14:38:15 -0700100func (g *generator) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6362e272015-10-29 15:25:03 -0700101 if g, ok := ctx.Module().(*generator); ok {
Colin Cross6f080df2016-11-04 15:32:58 -0700102 if len(g.properties.Tools) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800103 ctx.AddFarVariationDependencies([]blueprint.Variation{
Colin Crossa1ad8d12016-06-01 17:09:44 -0700104 {"arch", ctx.AConfig().BuildOsVariant},
Colin Cross6f080df2016-11-04 15:32:58 -0700105 }, nil, g.properties.Tools...)
Colin Cross6362e272015-10-29 15:25:03 -0700106 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700107 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700108}
109
Colin Cross635c3b02016-05-18 15:37:25 -0700110func (g *generator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6f080df2016-11-04 15:32:58 -0700111 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
112 ctx.ModuleErrorf("at least one `tools` or `tool_files` is required")
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700113 return
114 }
115
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700116 g.genPath = android.PathForModuleGen(ctx, "")
117
Colin Cross6f080df2016-11-04 15:32:58 -0700118 tools := map[string]android.Path{}
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700119
Colin Cross6f080df2016-11-04 15:32:58 -0700120 if len(g.properties.Tools) > 0 {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700121 ctx.VisitDirectDeps(func(module blueprint.Module) {
122 if t, ok := module.(HostToolProvider); ok {
123 p := t.HostToolPath()
124 if p.Valid() {
125 g.deps = append(g.deps, p.Path())
Colin Cross6f080df2016-11-04 15:32:58 -0700126 tool := ctx.OtherModuleName(module)
127 if _, exists := tools[tool]; !exists {
128 tools[tool] = p.Path()
129 } else {
130 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], p.Path().String())
131 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700132 } else {
133 ctx.ModuleErrorf("host tool %q missing output file", ctx.OtherModuleName(module))
134 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700135 } else {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700136 ctx.ModuleErrorf("unknown dependency %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700137 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700138 })
139 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700140
Colin Cross6f080df2016-11-04 15:32:58 -0700141 for _, tool := range g.properties.Tool_files {
142 toolPath := android.PathForModuleSrc(ctx, tool)
143 g.deps = append(g.deps, toolPath)
144 if _, exists := tools[tool]; !exists {
145 tools[tool] = toolPath
146 } else {
147 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], toolPath.String())
148 }
149 }
150
151 cmd, err := android.Expand(g.properties.Cmd, func(name string) (string, error) {
152 switch name {
153 case "location":
154 if len(g.properties.Tools) > 0 {
155 return tools[g.properties.Tools[0]].String(), nil
156 } else {
157 return tools[g.properties.Tool_files[0]].String(), nil
158 }
159 case "in":
160 return "${in}", nil
161 case "out":
162 return "${out}", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800163 case "depfile":
164 if !g.properties.Depfile {
165 return "", fmt.Errorf("$(depfile) used without depfile property")
166 }
167 return "${depfile}", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700168 case "genDir":
169 return g.genPath.String(), nil
170 default:
171 if strings.HasPrefix(name, "location ") {
172 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
173 if tool, ok := tools[label]; ok {
174 return tool.String(), nil
175 } else {
176 return "", fmt.Errorf("unknown location label %q", label)
177 }
178 }
179 return "", fmt.Errorf("unknown variable '$(%s)'", name)
180 }
181 })
182
183 if err != nil {
184 ctx.PropertyErrorf("cmd", "%s", err.Error())
185 }
186
Colin Cross33bfb0a2016-11-21 17:23:08 -0800187 ruleParams := blueprint.RuleParams{
Colin Cross6f080df2016-11-04 15:32:58 -0700188 Command: cmd,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800189 }
190 var args []string
191 if g.properties.Depfile {
192 ruleParams.Deps = blueprint.DepsGCC
193 args = append(args, "depfile")
194 }
195 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700196
Colin Crossd350ecd2015-04-28 13:25:36 -0700197 for _, task := range g.tasks(ctx) {
Colin Cross6f080df2016-11-04 15:32:58 -0700198 g.generateSourceFile(ctx, task)
Colin Crossd350ecd2015-04-28 13:25:36 -0700199 }
200}
201
Colin Cross6f080df2016-11-04 15:32:58 -0700202func (g *generator) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800203 params := android.ModuleBuildParams{
Colin Crossd350ecd2015-04-28 13:25:36 -0700204 Rule: g.rule,
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700205 Outputs: task.out,
Colin Crossd350ecd2015-04-28 13:25:36 -0700206 Inputs: task.in,
207 Implicits: g.deps,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800208 }
209 if g.properties.Depfile {
210 depfile := android.GenPathWithExt(ctx, "", task.out[0], task.out[0].Ext()+".d")
211 params.Depfile = depfile
212 }
213 ctx.ModuleBuild(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700214
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700215 for _, outputFile := range task.out {
216 g.outputFiles = append(g.outputFiles, outputFile)
217 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700218}
219
220func generatorFactory(tasks taskFunc, props ...interface{}) (blueprint.Module, []interface{}) {
221 module := &generator{
222 tasks: tasks,
223 }
224
225 props = append(props, &module.properties)
226
Colin Cross635c3b02016-05-18 15:37:25 -0700227 return android.InitAndroidModule(module, props...)
Colin Crossd350ecd2015-04-28 13:25:36 -0700228}
229
230func GenSrcsFactory() (blueprint.Module, []interface{}) {
231 properties := &genSrcsProperties{}
232
Colin Cross635c3b02016-05-18 15:37:25 -0700233 tasks := func(ctx android.ModuleContext) []generateTask {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700234 srcFiles := ctx.ExpandSources(properties.Srcs, nil)
Colin Crossd350ecd2015-04-28 13:25:36 -0700235 tasks := make([]generateTask, 0, len(srcFiles))
236 for _, in := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700237 tasks = append(tasks, generateTask{
Colin Cross635c3b02016-05-18 15:37:25 -0700238 in: android.Paths{in},
Dan Willemsen21ec4902016-11-02 20:43:13 -0700239 out: android.WritablePaths{android.GenPathWithExt(ctx, "", in, properties.Output_extension)},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700240 })
Colin Crossd350ecd2015-04-28 13:25:36 -0700241 }
242 return tasks
243 }
244
245 return generatorFactory(tasks, properties)
246}
247
248type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700249 // list of input files
Colin Cross5049f022015-03-18 13:28:46 -0700250 Srcs []string
251
Colin Cross7d5136f2015-05-11 13:39:40 -0700252 // extension that will be substituted for each output file
Colin Cross5049f022015-03-18 13:28:46 -0700253 Output_extension string
254}
255
Colin Crossd350ecd2015-04-28 13:25:36 -0700256func GenRuleFactory() (blueprint.Module, []interface{}) {
257 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700258
Colin Cross635c3b02016-05-18 15:37:25 -0700259 tasks := func(ctx android.ModuleContext) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700260 outs := make(android.WritablePaths, len(properties.Out))
261 for i, out := range properties.Out {
262 outs[i] = android.PathForModuleGen(ctx, out)
263 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700264 return []generateTask{
265 {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700266 in: ctx.ExpandSources(properties.Srcs, nil),
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700267 out: outs,
Colin Crossd350ecd2015-04-28 13:25:36 -0700268 },
269 }
Colin Cross5049f022015-03-18 13:28:46 -0700270 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700271
272 return generatorFactory(tasks, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700273}
274
Colin Crossd350ecd2015-04-28 13:25:36 -0700275type genRuleProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700276 // list of input files
Colin Crossd350ecd2015-04-28 13:25:36 -0700277 Srcs []string
Colin Cross5049f022015-03-18 13:28:46 -0700278
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700279 // names of the output files that will be generated
280 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700281}