blob: 4a9b336d170d477529a562b325ee6784b22aac9d [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 Cross463a90e2015-06-17 14:20:06 -070022 "android/soong"
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() {
27 soong.RegisterModuleType("gensrcs", GenSrcsFactory)
28 soong.RegisterModuleType("genrule", GenRuleFactory)
Colin Cross6362e272015-10-29 15:25:03 -070029
Colin Crosse8a67a72016-08-07 21:17:54 -070030 android.RegisterBottomUpMutator("genrule_deps", genruleDepsMutator).Parallel()
Colin Cross463a90e2015-06-17 14:20:06 -070031}
32
Colin Cross5049f022015-03-18 13:28:46 -070033var (
Colin Cross635c3b02016-05-18 15:37:25 -070034 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross5049f022015-03-18 13:28:46 -070035)
36
37func init() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070038 pctx.SourcePathVariable("srcDir", "")
39 pctx.HostBinToolVariable("hostBin", "")
Colin Cross5049f022015-03-18 13:28:46 -070040}
41
42type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070043 GeneratedSourceFiles() android.Paths
44 GeneratedHeaderDir() android.Path
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 {
52 // command to run on one or more input files. Available variables for substitution:
Dan Willemsenf7f3d692016-04-20 14:54:32 -070053 // $tool: the path to the `tool` or `tool_file`
Colin Cross7d5136f2015-05-11 13:39:40 -070054 // $in: one or more input files
55 // $out: a single output file
56 // $srcDir: the root directory of the source tree
Dan Willemsen3f4539b2016-09-28 16:19:10 -070057 // $genDir: the sandbox directory for this tool; contains $out
Colin Cross7d5136f2015-05-11 13:39:40 -070058 // The host bin directory will be in the path
59 Cmd string
60
61 // name of the module (if any) that produces the host executable. Leave empty for
62 // prebuilts or scripts that do not need a module to build them.
63 Tool string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070064
65 // Local file that is used as the tool
66 Tool_file string
Colin Cross7d5136f2015-05-11 13:39:40 -070067}
68
Colin Crossd350ecd2015-04-28 13:25:36 -070069type generator struct {
Colin Cross635c3b02016-05-18 15:37:25 -070070 android.ModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -070071
Colin Cross7d5136f2015-05-11 13:39:40 -070072 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -070073
74 tasks taskFunc
75
Colin Cross635c3b02016-05-18 15:37:25 -070076 deps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070077 rule blueprint.Rule
78
Colin Cross635c3b02016-05-18 15:37:25 -070079 genPath android.Path
Dan Willemsenb40aab62016-04-20 14:21:14 -070080
Colin Cross635c3b02016-05-18 15:37:25 -070081 outputFiles android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070082}
83
Colin Cross635c3b02016-05-18 15:37:25 -070084type taskFunc func(ctx android.ModuleContext) []generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -070085
86type generateTask struct {
Colin Cross635c3b02016-05-18 15:37:25 -070087 in android.Paths
Dan Willemsen9c8681f2016-09-28 16:21:00 -070088 out android.WritablePaths
Colin Crossd350ecd2015-04-28 13:25:36 -070089}
90
Colin Cross635c3b02016-05-18 15:37:25 -070091func (g *generator) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -070092 return g.outputFiles
93}
94
Colin Cross635c3b02016-05-18 15:37:25 -070095func (g *generator) GeneratedHeaderDir() android.Path {
Dan Willemsenb40aab62016-04-20 14:21:14 -070096 return g.genPath
97}
98
Colin Cross635c3b02016-05-18 15:37:25 -070099func genruleDepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6362e272015-10-29 15:25:03 -0700100 if g, ok := ctx.Module().(*generator); ok {
101 if g.properties.Tool != "" {
Dan Willemsen490fd492015-11-24 17:53:15 -0800102 ctx.AddFarVariationDependencies([]blueprint.Variation{
Colin Crossa1ad8d12016-06-01 17:09:44 -0700103 {"arch", ctx.AConfig().BuildOsVariant},
Colin Crossc99deeb2016-04-11 15:06:20 -0700104 }, nil, g.properties.Tool)
Colin Cross6362e272015-10-29 15:25:03 -0700105 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700106 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700107}
108
Colin Cross635c3b02016-05-18 15:37:25 -0700109func (g *generator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700110 if g.properties.Tool != "" && g.properties.Tool_file != "" {
111 ctx.ModuleErrorf("`tool` and `tool_file` may not be specified at the same time")
112 return
113 }
114
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700115 g.genPath = android.PathForModuleGen(ctx, "")
116
117 cmd := os.Expand(g.properties.Cmd, func(name string) string {
118 switch name {
119 case "$":
120 return "$$"
121 case "tool":
122 return "${tool}"
123 case "in":
124 return "${in}"
125 case "out":
126 return "${out}"
127 case "srcDir":
128 return "${srcDir}"
129 case "genDir":
130 return g.genPath.String()
131 default:
132 ctx.PropertyErrorf("cmd", "unknown variable '%s'", name)
133 }
134 return ""
135 })
136
Colin Crossd350ecd2015-04-28 13:25:36 -0700137 g.rule = ctx.Rule(pctx, "generator", blueprint.RuleParams{
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700138 Command: "PATH=$$PATH:$hostBin " + cmd,
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700139 }, "tool")
Colin Crossd350ecd2015-04-28 13:25:36 -0700140
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700141 var tool string
142 if g.properties.Tool_file != "" {
Colin Cross635c3b02016-05-18 15:37:25 -0700143 toolpath := android.PathForModuleSrc(ctx, g.properties.Tool_file)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700144 g.deps = append(g.deps, toolpath)
145 tool = toolpath.String()
146 } else if g.properties.Tool != "" {
147 ctx.VisitDirectDeps(func(module blueprint.Module) {
148 if t, ok := module.(HostToolProvider); ok {
149 p := t.HostToolPath()
150 if p.Valid() {
151 g.deps = append(g.deps, p.Path())
152 tool = p.String()
153 } else {
154 ctx.ModuleErrorf("host tool %q missing output file", ctx.OtherModuleName(module))
155 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700156 } else {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700157 ctx.ModuleErrorf("unknown dependency %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700158 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700159 })
160 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700161
162 for _, task := range g.tasks(ctx) {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700163 g.generateSourceFile(ctx, task, tool)
Colin Crossd350ecd2015-04-28 13:25:36 -0700164 }
165}
166
Colin Cross635c3b02016-05-18 15:37:25 -0700167func (g *generator) generateSourceFile(ctx android.ModuleContext, task generateTask, tool string) {
168 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Crossd350ecd2015-04-28 13:25:36 -0700169 Rule: g.rule,
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700170 Outputs: task.out,
Colin Crossd350ecd2015-04-28 13:25:36 -0700171 Inputs: task.in,
172 Implicits: g.deps,
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700173 Args: map[string]string{
174 "tool": tool,
175 },
Colin Crossd350ecd2015-04-28 13:25:36 -0700176 })
177
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700178 for _, outputFile := range task.out {
179 g.outputFiles = append(g.outputFiles, outputFile)
180 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700181}
182
183func generatorFactory(tasks taskFunc, props ...interface{}) (blueprint.Module, []interface{}) {
184 module := &generator{
185 tasks: tasks,
186 }
187
188 props = append(props, &module.properties)
189
Colin Cross635c3b02016-05-18 15:37:25 -0700190 return android.InitAndroidModule(module, props...)
Colin Crossd350ecd2015-04-28 13:25:36 -0700191}
192
193func GenSrcsFactory() (blueprint.Module, []interface{}) {
194 properties := &genSrcsProperties{}
195
Colin Cross635c3b02016-05-18 15:37:25 -0700196 tasks := func(ctx android.ModuleContext) []generateTask {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700197 srcFiles := ctx.ExpandSources(properties.Srcs, nil)
Colin Crossd350ecd2015-04-28 13:25:36 -0700198 tasks := make([]generateTask, 0, len(srcFiles))
199 for _, in := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700200 tasks = append(tasks, generateTask{
Colin Cross635c3b02016-05-18 15:37:25 -0700201 in: android.Paths{in},
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700202 out: android.WritablePaths{android.GenPathWithExt(ctx, in, properties.Output_extension)},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700203 })
Colin Crossd350ecd2015-04-28 13:25:36 -0700204 }
205 return tasks
206 }
207
208 return generatorFactory(tasks, properties)
209}
210
211type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700212 // list of input files
Colin Cross5049f022015-03-18 13:28:46 -0700213 Srcs []string
214
Colin Cross7d5136f2015-05-11 13:39:40 -0700215 // extension that will be substituted for each output file
Colin Cross5049f022015-03-18 13:28:46 -0700216 Output_extension string
217}
218
Colin Crossd350ecd2015-04-28 13:25:36 -0700219func GenRuleFactory() (blueprint.Module, []interface{}) {
220 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700221
Colin Cross635c3b02016-05-18 15:37:25 -0700222 tasks := func(ctx android.ModuleContext) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700223 outs := make(android.WritablePaths, len(properties.Out))
224 for i, out := range properties.Out {
225 outs[i] = android.PathForModuleGen(ctx, out)
226 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700227 return []generateTask{
228 {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700229 in: ctx.ExpandSources(properties.Srcs, nil),
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700230 out: outs,
Colin Crossd350ecd2015-04-28 13:25:36 -0700231 },
232 }
Colin Cross5049f022015-03-18 13:28:46 -0700233 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700234
235 return generatorFactory(tasks, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700236}
237
Colin Crossd350ecd2015-04-28 13:25:36 -0700238type genRuleProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700239 // list of input files
Colin Crossd350ecd2015-04-28 13:25:36 -0700240 Srcs []string
Colin Cross5049f022015-03-18 13:28:46 -0700241
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700242 // names of the output files that will be generated
243 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700244}