blob: ecc5ab803f3b949b0a8066e1ca3748e327781be9 [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 Cross70b40592015-03-23 12:57:34 -070018 "github.com/google/blueprint"
Colin Cross5049f022015-03-18 13:28:46 -070019
Colin Cross463a90e2015-06-17 14:20:06 -070020 "android/soong"
Colin Cross635c3b02016-05-18 15:37:25 -070021 "android/soong/android"
Colin Cross5049f022015-03-18 13:28:46 -070022)
23
Colin Cross463a90e2015-06-17 14:20:06 -070024func init() {
25 soong.RegisterModuleType("gensrcs", GenSrcsFactory)
26 soong.RegisterModuleType("genrule", GenRuleFactory)
Colin Cross6362e272015-10-29 15:25:03 -070027
Colin Crosse8a67a72016-08-07 21:17:54 -070028 android.RegisterBottomUpMutator("genrule_deps", genruleDepsMutator).Parallel()
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
35func init() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070036 pctx.SourcePathVariable("srcDir", "")
37 pctx.HostBinToolVariable("hostBin", "")
Colin Cross5049f022015-03-18 13:28:46 -070038}
39
40type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070041 GeneratedSourceFiles() android.Paths
42 GeneratedHeaderDir() android.Path
Colin Cross5049f022015-03-18 13:28:46 -070043}
44
Colin Crossd350ecd2015-04-28 13:25:36 -070045type HostToolProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -070046 HostToolPath() android.OptionalPath
Colin Crossd350ecd2015-04-28 13:25:36 -070047}
Colin Cross5049f022015-03-18 13:28:46 -070048
Colin Cross7d5136f2015-05-11 13:39:40 -070049type generatorProperties struct {
50 // command to run on one or more input files. Available variables for substitution:
Dan Willemsenf7f3d692016-04-20 14:54:32 -070051 // $tool: the path to the `tool` or `tool_file`
Colin Cross7d5136f2015-05-11 13:39:40 -070052 // $in: one or more input files
53 // $out: a single output file
54 // $srcDir: the root directory of the source tree
55 // 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
85 out android.ModuleGenPath
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 Cross635c3b02016-05-18 15:37:25 -070096func genruleDepsMutator(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
Colin Crossd350ecd2015-04-28 13:25:36 -0700112 g.rule = ctx.Rule(pctx, "generator", blueprint.RuleParams{
113 Command: "PATH=$$PATH:$hostBin " + g.properties.Cmd,
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700114 }, "tool")
Colin Crossd350ecd2015-04-28 13:25:36 -0700115
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700116 var tool string
117 if g.properties.Tool_file != "" {
Colin Cross635c3b02016-05-18 15:37:25 -0700118 toolpath := android.PathForModuleSrc(ctx, g.properties.Tool_file)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700119 g.deps = append(g.deps, toolpath)
120 tool = toolpath.String()
121 } else if g.properties.Tool != "" {
122 ctx.VisitDirectDeps(func(module blueprint.Module) {
123 if t, ok := module.(HostToolProvider); ok {
124 p := t.HostToolPath()
125 if p.Valid() {
126 g.deps = append(g.deps, p.Path())
127 tool = p.String()
128 } else {
129 ctx.ModuleErrorf("host tool %q missing output file", ctx.OtherModuleName(module))
130 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700131 } else {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700132 ctx.ModuleErrorf("unknown dependency %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700133 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700134 })
135 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700136
Colin Cross635c3b02016-05-18 15:37:25 -0700137 g.genPath = android.PathForModuleGen(ctx, "")
Dan Willemsenb40aab62016-04-20 14:21:14 -0700138
Colin Crossd350ecd2015-04-28 13:25:36 -0700139 for _, task := range g.tasks(ctx) {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700140 g.generateSourceFile(ctx, task, tool)
Colin Crossd350ecd2015-04-28 13:25:36 -0700141 }
142}
143
Colin Cross635c3b02016-05-18 15:37:25 -0700144func (g *generator) generateSourceFile(ctx android.ModuleContext, task generateTask, tool string) {
145 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Crossd350ecd2015-04-28 13:25:36 -0700146 Rule: g.rule,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700147 Output: task.out,
Colin Crossd350ecd2015-04-28 13:25:36 -0700148 Inputs: task.in,
149 Implicits: g.deps,
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700150 Args: map[string]string{
151 "tool": tool,
152 },
Colin Crossd350ecd2015-04-28 13:25:36 -0700153 })
154
155 g.outputFiles = append(g.outputFiles, task.out)
156}
157
158func generatorFactory(tasks taskFunc, props ...interface{}) (blueprint.Module, []interface{}) {
159 module := &generator{
160 tasks: tasks,
161 }
162
163 props = append(props, &module.properties)
164
Colin Cross635c3b02016-05-18 15:37:25 -0700165 return android.InitAndroidModule(module, props...)
Colin Crossd350ecd2015-04-28 13:25:36 -0700166}
167
168func GenSrcsFactory() (blueprint.Module, []interface{}) {
169 properties := &genSrcsProperties{}
170
Colin Cross635c3b02016-05-18 15:37:25 -0700171 tasks := func(ctx android.ModuleContext) []generateTask {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700172 srcFiles := ctx.ExpandSources(properties.Srcs, nil)
Colin Crossd350ecd2015-04-28 13:25:36 -0700173 tasks := make([]generateTask, 0, len(srcFiles))
174 for _, in := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700175 tasks = append(tasks, generateTask{
Colin Cross635c3b02016-05-18 15:37:25 -0700176 in: android.Paths{in},
177 out: android.GenPathWithExt(ctx, in, properties.Output_extension),
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700178 })
Colin Crossd350ecd2015-04-28 13:25:36 -0700179 }
180 return tasks
181 }
182
183 return generatorFactory(tasks, properties)
184}
185
186type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700187 // list of input files
Colin Cross5049f022015-03-18 13:28:46 -0700188 Srcs []string
189
Colin Cross7d5136f2015-05-11 13:39:40 -0700190 // extension that will be substituted for each output file
Colin Cross5049f022015-03-18 13:28:46 -0700191 Output_extension string
192}
193
Colin Crossd350ecd2015-04-28 13:25:36 -0700194func GenRuleFactory() (blueprint.Module, []interface{}) {
195 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700196
Colin Cross635c3b02016-05-18 15:37:25 -0700197 tasks := func(ctx android.ModuleContext) []generateTask {
Colin Crossd350ecd2015-04-28 13:25:36 -0700198 return []generateTask{
199 {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700200 in: ctx.ExpandSources(properties.Srcs, nil),
Colin Cross635c3b02016-05-18 15:37:25 -0700201 out: android.PathForModuleGen(ctx, properties.Out),
Colin Crossd350ecd2015-04-28 13:25:36 -0700202 },
203 }
Colin Cross5049f022015-03-18 13:28:46 -0700204 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700205
206 return generatorFactory(tasks, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700207}
208
Colin Crossd350ecd2015-04-28 13:25:36 -0700209type genRuleProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700210 // list of input files
Colin Crossd350ecd2015-04-28 13:25:36 -0700211 Srcs []string
Colin Cross5049f022015-03-18 13:28:46 -0700212
Colin Cross7d5136f2015-05-11 13:39:40 -0700213 // name of the output file that will be generated
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700214 Out string
Colin Cross5049f022015-03-18 13:28:46 -0700215}