blob: baa6b335bc3ad3257cae8751a50707a31b911262 [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 Cross6362e272015-10-29 15:25:03 -070028
Colin Crosse8a67a72016-08-07 21:17:54 -070029 android.RegisterBottomUpMutator("genrule_deps", genruleDepsMutator).Parallel()
Colin Cross463a90e2015-06-17 14:20:06 -070030}
31
Colin Cross5049f022015-03-18 13:28:46 -070032var (
Colin Cross635c3b02016-05-18 15:37:25 -070033 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross5049f022015-03-18 13:28:46 -070034)
35
36func init() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070037 pctx.SourcePathVariable("srcDir", "")
38 pctx.HostBinToolVariable("hostBin", "")
Colin Cross5049f022015-03-18 13:28:46 -070039}
40
41type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070042 GeneratedSourceFiles() android.Paths
43 GeneratedHeaderDir() android.Path
Colin Cross5049f022015-03-18 13:28:46 -070044}
45
Colin Crossd350ecd2015-04-28 13:25:36 -070046type HostToolProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -070047 HostToolPath() android.OptionalPath
Colin Crossd350ecd2015-04-28 13:25:36 -070048}
Colin Cross5049f022015-03-18 13:28:46 -070049
Colin Cross7d5136f2015-05-11 13:39:40 -070050type generatorProperties struct {
51 // command to run on one or more input files. Available variables for substitution:
Dan Willemsenf7f3d692016-04-20 14:54:32 -070052 // $tool: the path to the `tool` or `tool_file`
Colin Cross7d5136f2015-05-11 13:39:40 -070053 // $in: one or more input files
54 // $out: a single output file
55 // $srcDir: the root directory of the source tree
Dan Willemsen3f4539b2016-09-28 16:19:10 -070056 // $genDir: the sandbox directory for this tool; contains $out
Colin Cross7d5136f2015-05-11 13:39:40 -070057 // The host bin directory will be in the path
58 Cmd string
59
60 // name of the module (if any) that produces the host executable. Leave empty for
61 // prebuilts or scripts that do not need a module to build them.
62 Tool string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070063
64 // Local file that is used as the tool
65 Tool_file string
Colin Cross7d5136f2015-05-11 13:39:40 -070066}
67
Colin Crossd350ecd2015-04-28 13:25:36 -070068type generator struct {
Colin Cross635c3b02016-05-18 15:37:25 -070069 android.ModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -070070
Colin Cross7d5136f2015-05-11 13:39:40 -070071 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -070072
73 tasks taskFunc
74
Colin Cross635c3b02016-05-18 15:37:25 -070075 deps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070076 rule blueprint.Rule
77
Colin Cross635c3b02016-05-18 15:37:25 -070078 genPath android.Path
Dan Willemsenb40aab62016-04-20 14:21:14 -070079
Colin Cross635c3b02016-05-18 15:37:25 -070080 outputFiles android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070081}
82
Colin Cross635c3b02016-05-18 15:37:25 -070083type taskFunc func(ctx android.ModuleContext) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -070084
85type generateTask struct {
Colin Cross635c3b02016-05-18 15:37:25 -070086 in android.Paths
Dan Willemsen9c8681f2016-09-28 16:21:00 -070087 out android.WritablePaths
Colin Crossd350ecd2015-04-28 13:25:36 -070088}
89
Colin Cross635c3b02016-05-18 15:37:25 -070090func (g *generator) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -070091 return g.outputFiles
92}
93
Colin Cross635c3b02016-05-18 15:37:25 -070094func (g *generator) GeneratedHeaderDir() android.Path {
Dan Willemsenb40aab62016-04-20 14:21:14 -070095 return g.genPath
96}
97
Colin Cross635c3b02016-05-18 15:37:25 -070098func genruleDepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6362e272015-10-29 15:25:03 -070099 if g, ok := ctx.Module().(*generator); ok {
100 if g.properties.Tool != "" {
Dan Willemsen490fd492015-11-24 17:53:15 -0800101 ctx.AddFarVariationDependencies([]blueprint.Variation{
Colin Crossa1ad8d12016-06-01 17:09:44 -0700102 {"arch", ctx.AConfig().BuildOsVariant},
Colin Crossc99deeb2016-04-11 15:06:20 -0700103 }, nil, g.properties.Tool)
Colin Cross6362e272015-10-29 15:25:03 -0700104 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700105 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700106}
107
Colin Cross635c3b02016-05-18 15:37:25 -0700108func (g *generator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700109 if g.properties.Tool != "" && g.properties.Tool_file != "" {
110 ctx.ModuleErrorf("`tool` and `tool_file` may not be specified at the same time")
111 return
112 }
113
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700114 g.genPath = android.PathForModuleGen(ctx, "")
115
116 cmd := os.Expand(g.properties.Cmd, func(name string) string {
117 switch name {
118 case "$":
119 return "$$"
120 case "tool":
121 return "${tool}"
122 case "in":
123 return "${in}"
124 case "out":
125 return "${out}"
126 case "srcDir":
127 return "${srcDir}"
128 case "genDir":
129 return g.genPath.String()
130 default:
131 ctx.PropertyErrorf("cmd", "unknown variable '%s'", name)
132 }
133 return ""
134 })
135
Colin Crossd350ecd2015-04-28 13:25:36 -0700136 g.rule = ctx.Rule(pctx, "generator", blueprint.RuleParams{
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700137 Command: "PATH=$$PATH:$hostBin " + cmd,
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700138 }, "tool")
Colin Crossd350ecd2015-04-28 13:25:36 -0700139
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700140 var tool string
141 if g.properties.Tool_file != "" {
Colin Cross635c3b02016-05-18 15:37:25 -0700142 toolpath := android.PathForModuleSrc(ctx, g.properties.Tool_file)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700143 g.deps = append(g.deps, toolpath)
144 tool = toolpath.String()
145 } else if g.properties.Tool != "" {
146 ctx.VisitDirectDeps(func(module blueprint.Module) {
147 if t, ok := module.(HostToolProvider); ok {
148 p := t.HostToolPath()
149 if p.Valid() {
150 g.deps = append(g.deps, p.Path())
151 tool = p.String()
152 } else {
153 ctx.ModuleErrorf("host tool %q missing output file", ctx.OtherModuleName(module))
154 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700155 } else {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700156 ctx.ModuleErrorf("unknown dependency %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700157 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700158 })
159 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700160
161 for _, task := range g.tasks(ctx) {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700162 g.generateSourceFile(ctx, task, tool)
Colin Crossd350ecd2015-04-28 13:25:36 -0700163 }
164}
165
Colin Cross635c3b02016-05-18 15:37:25 -0700166func (g *generator) generateSourceFile(ctx android.ModuleContext, task generateTask, tool string) {
167 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Crossd350ecd2015-04-28 13:25:36 -0700168 Rule: g.rule,
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700169 Outputs: task.out,
Colin Crossd350ecd2015-04-28 13:25:36 -0700170 Inputs: task.in,
171 Implicits: g.deps,
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700172 Args: map[string]string{
173 "tool": tool,
174 },
Colin Crossd350ecd2015-04-28 13:25:36 -0700175 })
176
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700177 for _, outputFile := range task.out {
178 g.outputFiles = append(g.outputFiles, outputFile)
179 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700180}
181
182func generatorFactory(tasks taskFunc, props ...interface{}) (blueprint.Module, []interface{}) {
183 module := &generator{
184 tasks: tasks,
185 }
186
187 props = append(props, &module.properties)
188
Colin Cross635c3b02016-05-18 15:37:25 -0700189 return android.InitAndroidModule(module, props...)
Colin Crossd350ecd2015-04-28 13:25:36 -0700190}
191
192func GenSrcsFactory() (blueprint.Module, []interface{}) {
193 properties := &genSrcsProperties{}
194
Colin Cross635c3b02016-05-18 15:37:25 -0700195 tasks := func(ctx android.ModuleContext) []generateTask {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700196 srcFiles := ctx.ExpandSources(properties.Srcs, nil)
Colin Crossd350ecd2015-04-28 13:25:36 -0700197 tasks := make([]generateTask, 0, len(srcFiles))
198 for _, in := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700199 tasks = append(tasks, generateTask{
Colin Cross635c3b02016-05-18 15:37:25 -0700200 in: android.Paths{in},
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700201 out: android.WritablePaths{android.GenPathWithExt(ctx, in, properties.Output_extension)},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700202 })
Colin Crossd350ecd2015-04-28 13:25:36 -0700203 }
204 return tasks
205 }
206
207 return generatorFactory(tasks, properties)
208}
209
210type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700211 // list of input files
Colin Cross5049f022015-03-18 13:28:46 -0700212 Srcs []string
213
Colin Cross7d5136f2015-05-11 13:39:40 -0700214 // extension that will be substituted for each output file
Colin Cross5049f022015-03-18 13:28:46 -0700215 Output_extension string
216}
217
Colin Crossd350ecd2015-04-28 13:25:36 -0700218func GenRuleFactory() (blueprint.Module, []interface{}) {
219 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700220
Colin Cross635c3b02016-05-18 15:37:25 -0700221 tasks := func(ctx android.ModuleContext) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700222 outs := make(android.WritablePaths, len(properties.Out))
223 for i, out := range properties.Out {
224 outs[i] = android.PathForModuleGen(ctx, out)
225 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700226 return []generateTask{
227 {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700228 in: ctx.ExpandSources(properties.Srcs, nil),
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700229 out: outs,
Colin Crossd350ecd2015-04-28 13:25:36 -0700230 },
231 }
Colin Cross5049f022015-03-18 13:28:46 -0700232 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700233
234 return generatorFactory(tasks, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700235}
236
Colin Crossd350ecd2015-04-28 13:25:36 -0700237type genRuleProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700238 // list of input files
Colin Crossd350ecd2015-04-28 13:25:36 -0700239 Srcs []string
Colin Cross5049f022015-03-18 13:28:46 -0700240
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700241 // names of the output files that will be generated
242 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700243}