blob: f70c5fc33d5180d045d411e3d73a2092ee1e524b [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 (
Dan Willemsen3f4539b2016-09-28 16:19:10 -070018 "os"
19
Colin Cross70b40592015-03-23 12:57:34 -070020 "github.com/google/blueprint"
Colin Cross5049f022015-03-18 13:28:46 -070021
Colin Cross635c3b02016-05-18 15:37:25 -070022 "android/soong/android"
Colin Cross5049f022015-03-18 13:28:46 -070023)
24
Colin Cross463a90e2015-06-17 14:20:06 -070025func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070026 android.RegisterModuleType("gensrcs", GenSrcsFactory)
27 android.RegisterModuleType("genrule", GenRuleFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070028}
29
Colin Cross5049f022015-03-18 13:28:46 -070030var (
Colin Cross635c3b02016-05-18 15:37:25 -070031 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross5049f022015-03-18 13:28:46 -070032)
33
34func init() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070035 pctx.SourcePathVariable("srcDir", "")
36 pctx.HostBinToolVariable("hostBin", "")
Colin Cross5049f022015-03-18 13:28:46 -070037}
38
39type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070040 GeneratedSourceFiles() android.Paths
41 GeneratedHeaderDir() android.Path
Colin Cross5049f022015-03-18 13:28:46 -070042}
43
Colin Crossd350ecd2015-04-28 13:25:36 -070044type HostToolProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -070045 HostToolPath() android.OptionalPath
Colin Crossd350ecd2015-04-28 13:25:36 -070046}
Colin Cross5049f022015-03-18 13:28:46 -070047
Colin Cross7d5136f2015-05-11 13:39:40 -070048type generatorProperties struct {
49 // command to run on one or more input files. Available variables for substitution:
Dan Willemsenf7f3d692016-04-20 14:54:32 -070050 // $tool: the path to the `tool` or `tool_file`
Colin Cross7d5136f2015-05-11 13:39:40 -070051 // $in: one or more input files
52 // $out: a single output file
53 // $srcDir: the root directory of the source tree
Dan Willemsen3f4539b2016-09-28 16:19:10 -070054 // $genDir: the sandbox directory for this tool; contains $out
Colin Cross7d5136f2015-05-11 13:39:40 -070055 // The host bin directory will be in the path
56 Cmd string
57
58 // name of the module (if any) that produces the host executable. Leave empty for
59 // prebuilts or scripts that do not need a module to build them.
60 Tool string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070061
62 // Local file that is used as the tool
63 Tool_file string
Colin Cross7d5136f2015-05-11 13:39:40 -070064}
65
Colin Crossd350ecd2015-04-28 13:25:36 -070066type generator struct {
Colin Cross635c3b02016-05-18 15:37:25 -070067 android.ModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -070068
Colin Cross7d5136f2015-05-11 13:39:40 -070069 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -070070
71 tasks taskFunc
72
Colin Cross635c3b02016-05-18 15:37:25 -070073 deps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070074 rule blueprint.Rule
75
Colin Cross635c3b02016-05-18 15:37:25 -070076 genPath android.Path
Dan Willemsenb40aab62016-04-20 14:21:14 -070077
Colin Cross635c3b02016-05-18 15:37:25 -070078 outputFiles android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070079}
80
Colin Cross635c3b02016-05-18 15:37:25 -070081type taskFunc func(ctx android.ModuleContext) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -070082
83type generateTask struct {
Colin Cross635c3b02016-05-18 15:37:25 -070084 in android.Paths
Dan Willemsen9c8681f2016-09-28 16:21:00 -070085 out android.WritablePaths
Colin Crossd350ecd2015-04-28 13:25:36 -070086}
87
Colin Cross635c3b02016-05-18 15:37:25 -070088func (g *generator) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -070089 return g.outputFiles
90}
91
Colin Cross635c3b02016-05-18 15:37:25 -070092func (g *generator) GeneratedHeaderDir() android.Path {
Dan Willemsenb40aab62016-04-20 14:21:14 -070093 return g.genPath
94}
95
Colin Cross1e676be2016-10-12 14:38:15 -070096func (g *generator) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6362e272015-10-29 15:25:03 -070097 if g, ok := ctx.Module().(*generator); ok {
98 if g.properties.Tool != "" {
Dan Willemsen490fd492015-11-24 17:53:15 -080099 ctx.AddFarVariationDependencies([]blueprint.Variation{
Colin Crossa1ad8d12016-06-01 17:09:44 -0700100 {"arch", ctx.AConfig().BuildOsVariant},
Colin Crossc99deeb2016-04-11 15:06:20 -0700101 }, nil, g.properties.Tool)
Colin Cross6362e272015-10-29 15:25:03 -0700102 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700103 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700104}
105
Colin Cross635c3b02016-05-18 15:37:25 -0700106func (g *generator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700107 if g.properties.Tool != "" && g.properties.Tool_file != "" {
108 ctx.ModuleErrorf("`tool` and `tool_file` may not be specified at the same time")
109 return
110 }
111
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700112 g.genPath = android.PathForModuleGen(ctx, "")
113
114 cmd := os.Expand(g.properties.Cmd, func(name string) string {
115 switch name {
116 case "$":
117 return "$$"
118 case "tool":
119 return "${tool}"
120 case "in":
121 return "${in}"
122 case "out":
123 return "${out}"
124 case "srcDir":
125 return "${srcDir}"
126 case "genDir":
127 return g.genPath.String()
128 default:
129 ctx.PropertyErrorf("cmd", "unknown variable '%s'", name)
130 }
131 return ""
132 })
133
Colin Crossd350ecd2015-04-28 13:25:36 -0700134 g.rule = ctx.Rule(pctx, "generator", blueprint.RuleParams{
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700135 Command: "PATH=$$PATH:$hostBin " + cmd,
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700136 }, "tool")
Colin Crossd350ecd2015-04-28 13:25:36 -0700137
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700138 var tool string
139 if g.properties.Tool_file != "" {
Colin Cross635c3b02016-05-18 15:37:25 -0700140 toolpath := android.PathForModuleSrc(ctx, g.properties.Tool_file)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700141 g.deps = append(g.deps, toolpath)
142 tool = toolpath.String()
143 } else if g.properties.Tool != "" {
144 ctx.VisitDirectDeps(func(module blueprint.Module) {
145 if t, ok := module.(HostToolProvider); ok {
146 p := t.HostToolPath()
147 if p.Valid() {
148 g.deps = append(g.deps, p.Path())
149 tool = p.String()
150 } else {
151 ctx.ModuleErrorf("host tool %q missing output file", ctx.OtherModuleName(module))
152 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700153 } else {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700154 ctx.ModuleErrorf("unknown dependency %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700155 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700156 })
157 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700158
159 for _, task := range g.tasks(ctx) {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700160 g.generateSourceFile(ctx, task, tool)
Colin Crossd350ecd2015-04-28 13:25:36 -0700161 }
162}
163
Colin Cross635c3b02016-05-18 15:37:25 -0700164func (g *generator) generateSourceFile(ctx android.ModuleContext, task generateTask, tool string) {
165 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Crossd350ecd2015-04-28 13:25:36 -0700166 Rule: g.rule,
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700167 Outputs: task.out,
Colin Crossd350ecd2015-04-28 13:25:36 -0700168 Inputs: task.in,
169 Implicits: g.deps,
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700170 Args: map[string]string{
171 "tool": tool,
172 },
Colin Crossd350ecd2015-04-28 13:25:36 -0700173 })
174
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700175 for _, outputFile := range task.out {
176 g.outputFiles = append(g.outputFiles, outputFile)
177 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700178}
179
180func generatorFactory(tasks taskFunc, props ...interface{}) (blueprint.Module, []interface{}) {
181 module := &generator{
182 tasks: tasks,
183 }
184
185 props = append(props, &module.properties)
186
Colin Cross635c3b02016-05-18 15:37:25 -0700187 return android.InitAndroidModule(module, props...)
Colin Crossd350ecd2015-04-28 13:25:36 -0700188}
189
190func GenSrcsFactory() (blueprint.Module, []interface{}) {
191 properties := &genSrcsProperties{}
192
Colin Cross635c3b02016-05-18 15:37:25 -0700193 tasks := func(ctx android.ModuleContext) []generateTask {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700194 srcFiles := ctx.ExpandSources(properties.Srcs, nil)
Colin Crossd350ecd2015-04-28 13:25:36 -0700195 tasks := make([]generateTask, 0, len(srcFiles))
196 for _, in := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700197 tasks = append(tasks, generateTask{
Colin Cross635c3b02016-05-18 15:37:25 -0700198 in: android.Paths{in},
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700199 out: android.WritablePaths{android.GenPathWithExt(ctx, in, properties.Output_extension)},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700200 })
Colin Crossd350ecd2015-04-28 13:25:36 -0700201 }
202 return tasks
203 }
204
205 return generatorFactory(tasks, properties)
206}
207
208type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700209 // list of input files
Colin Cross5049f022015-03-18 13:28:46 -0700210 Srcs []string
211
Colin Cross7d5136f2015-05-11 13:39:40 -0700212 // extension that will be substituted for each output file
Colin Cross5049f022015-03-18 13:28:46 -0700213 Output_extension string
214}
215
Colin Crossd350ecd2015-04-28 13:25:36 -0700216func GenRuleFactory() (blueprint.Module, []interface{}) {
217 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700218
Colin Cross635c3b02016-05-18 15:37:25 -0700219 tasks := func(ctx android.ModuleContext) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700220 outs := make(android.WritablePaths, len(properties.Out))
221 for i, out := range properties.Out {
222 outs[i] = android.PathForModuleGen(ctx, out)
223 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700224 return []generateTask{
225 {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700226 in: ctx.ExpandSources(properties.Srcs, nil),
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700227 out: outs,
Colin Crossd350ecd2015-04-28 13:25:36 -0700228 },
229 }
Colin Cross5049f022015-03-18 13:28:46 -0700230 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700231
232 return generatorFactory(tasks, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700233}
234
Colin Crossd350ecd2015-04-28 13:25:36 -0700235type genRuleProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700236 // list of input files
Colin Crossd350ecd2015-04-28 13:25:36 -0700237 Srcs []string
Colin Cross5049f022015-03-18 13:28:46 -0700238
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700239 // names of the output files that will be generated
240 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700241}