blob: d9b6c77a17743e75fb65681225313517f77cdf25 [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 Cross5049f022015-03-18 13:28:46 -070021 "android/soong/common"
22)
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
28 common.RegisterBottomUpMutator("genrule_deps", genruleDepsMutator)
Colin Cross463a90e2015-06-17 14:20:06 -070029}
30
Colin Cross5049f022015-03-18 13:28:46 -070031var (
Dan Willemsen34cc69e2015-09-23 15:26:20 -070032 pctx = common.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 {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070041 GeneratedSourceFiles() common.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070042 GeneratedHeaderDir() common.Path
Colin Cross5049f022015-03-18 13:28:46 -070043}
44
Colin Crossd350ecd2015-04-28 13:25:36 -070045type HostToolProvider interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070046 HostToolPath() common.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 {
67 common.AndroidModuleBase
68
Colin Cross7d5136f2015-05-11 13:39:40 -070069 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -070070
71 tasks taskFunc
72
Dan Willemsen34cc69e2015-09-23 15:26:20 -070073 deps common.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070074 rule blueprint.Rule
75
Dan Willemsenb40aab62016-04-20 14:21:14 -070076 genPath common.Path
77
Dan Willemsen34cc69e2015-09-23 15:26:20 -070078 outputFiles common.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070079}
80
81type taskFunc func(ctx common.AndroidModuleContext) []generateTask
82
83type generateTask struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070084 in common.Paths
85 out common.ModuleGenPath
Colin Crossd350ecd2015-04-28 13:25:36 -070086}
87
Dan Willemsen34cc69e2015-09-23 15:26:20 -070088func (g *generator) GeneratedSourceFiles() common.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -070089 return g.outputFiles
90}
91
Dan Willemsenb40aab62016-04-20 14:21:14 -070092func (g *generator) GeneratedHeaderDir() common.Path {
93 return g.genPath
94}
95
Colin Cross6362e272015-10-29 15:25:03 -070096func genruleDepsMutator(ctx common.AndroidBottomUpMutatorContext) {
97 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 Cross1604ecf2015-12-17 16:33:43 -0800100 {"host_or_device", common.Host.String()},
101 {"host_type", common.CurrentHostType().String()},
Colin Crossc99deeb2016-04-11 15:06:20 -0700102 }, nil, g.properties.Tool)
Colin Cross6362e272015-10-29 15:25:03 -0700103 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700104 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700105}
106
107func (g *generator) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700108 if g.properties.Tool != "" && g.properties.Tool_file != "" {
109 ctx.ModuleErrorf("`tool` and `tool_file` may not be specified at the same time")
110 return
111 }
112
Colin Crossd350ecd2015-04-28 13:25:36 -0700113 g.rule = ctx.Rule(pctx, "generator", blueprint.RuleParams{
114 Command: "PATH=$$PATH:$hostBin " + g.properties.Cmd,
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700115 }, "tool")
Colin Crossd350ecd2015-04-28 13:25:36 -0700116
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700117 var tool string
118 if g.properties.Tool_file != "" {
119 toolpath := common.PathForModuleSrc(ctx, g.properties.Tool_file)
120 g.deps = append(g.deps, toolpath)
121 tool = toolpath.String()
122 } else if g.properties.Tool != "" {
123 ctx.VisitDirectDeps(func(module blueprint.Module) {
124 if t, ok := module.(HostToolProvider); ok {
125 p := t.HostToolPath()
126 if p.Valid() {
127 g.deps = append(g.deps, p.Path())
128 tool = p.String()
129 } else {
130 ctx.ModuleErrorf("host tool %q missing output file", ctx.OtherModuleName(module))
131 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700132 } else {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700133 ctx.ModuleErrorf("unknown dependency %q", ctx.OtherModuleName(module))
Colin Crossd350ecd2015-04-28 13:25:36 -0700134 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700135 })
136 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700137
Dan Willemsenb40aab62016-04-20 14:21:14 -0700138 g.genPath = common.PathForModuleGen(ctx, "")
139
Colin Crossd350ecd2015-04-28 13:25:36 -0700140 for _, task := range g.tasks(ctx) {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700141 g.generateSourceFile(ctx, task, tool)
Colin Crossd350ecd2015-04-28 13:25:36 -0700142 }
143}
144
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700145func (g *generator) generateSourceFile(ctx common.AndroidModuleContext, task generateTask, tool string) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700146 ctx.ModuleBuild(pctx, common.ModuleBuildParams{
Colin Crossd350ecd2015-04-28 13:25:36 -0700147 Rule: g.rule,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700148 Output: task.out,
Colin Crossd350ecd2015-04-28 13:25:36 -0700149 Inputs: task.in,
150 Implicits: g.deps,
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700151 Args: map[string]string{
152 "tool": tool,
153 },
Colin Crossd350ecd2015-04-28 13:25:36 -0700154 })
155
156 g.outputFiles = append(g.outputFiles, task.out)
157}
158
159func generatorFactory(tasks taskFunc, props ...interface{}) (blueprint.Module, []interface{}) {
160 module := &generator{
161 tasks: tasks,
162 }
163
164 props = append(props, &module.properties)
165
166 return common.InitAndroidModule(module, props...)
167}
168
169func GenSrcsFactory() (blueprint.Module, []interface{}) {
170 properties := &genSrcsProperties{}
171
172 tasks := func(ctx common.AndroidModuleContext) []generateTask {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700173 srcFiles := ctx.ExpandSources(properties.Srcs, nil)
Colin Crossd350ecd2015-04-28 13:25:36 -0700174 tasks := make([]generateTask, 0, len(srcFiles))
175 for _, in := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700176 tasks = append(tasks, generateTask{
177 in: common.Paths{in},
178 out: common.GenPathWithExt(ctx, in, properties.Output_extension),
179 })
Colin Crossd350ecd2015-04-28 13:25:36 -0700180 }
181 return tasks
182 }
183
184 return generatorFactory(tasks, properties)
185}
186
187type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700188 // list of input files
Colin Cross5049f022015-03-18 13:28:46 -0700189 Srcs []string
190
Colin Cross7d5136f2015-05-11 13:39:40 -0700191 // extension that will be substituted for each output file
Colin Cross5049f022015-03-18 13:28:46 -0700192 Output_extension string
193}
194
Colin Crossd350ecd2015-04-28 13:25:36 -0700195func GenRuleFactory() (blueprint.Module, []interface{}) {
196 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700197
Colin Crossd350ecd2015-04-28 13:25:36 -0700198 tasks := func(ctx common.AndroidModuleContext) []generateTask {
199 return []generateTask{
200 {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700201 in: ctx.ExpandSources(properties.Srcs, nil),
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700202 out: common.PathForModuleGen(ctx, properties.Out),
Colin Crossd350ecd2015-04-28 13:25:36 -0700203 },
204 }
Colin Cross5049f022015-03-18 13:28:46 -0700205 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700206
207 return generatorFactory(tasks, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700208}
209
Colin Crossd350ecd2015-04-28 13:25:36 -0700210type genRuleProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700211 // list of input files
Colin Crossd350ecd2015-04-28 13:25:36 -0700212 Srcs []string
Colin Cross5049f022015-03-18 13:28:46 -0700213
Colin Cross7d5136f2015-05-11 13:39:40 -0700214 // name of the output file that will be generated
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700215 Out string
Colin Cross5049f022015-03-18 13:28:46 -0700216}