blob: a195b24ff61e254b593397da66b0860edc7b3d2a [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 Cross635c3b02016-05-18 15:37:25 -070028 android.RegisterBottomUpMutator("genrule_deps", genruleDepsMutator)
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 Cross635c3b02016-05-18 15:37:25 -0700100 {"host_or_device", android.Host.String()},
101 {"host_type", android.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
Colin Cross635c3b02016-05-18 15:37:25 -0700107func (g *generator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
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 != "" {
Colin Cross635c3b02016-05-18 15:37:25 -0700119 toolpath := android.PathForModuleSrc(ctx, g.properties.Tool_file)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700120 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
Colin Cross635c3b02016-05-18 15:37:25 -0700138 g.genPath = android.PathForModuleGen(ctx, "")
Dan Willemsenb40aab62016-04-20 14:21:14 -0700139
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
Colin Cross635c3b02016-05-18 15:37:25 -0700145func (g *generator) generateSourceFile(ctx android.ModuleContext, task generateTask, tool string) {
146 ctx.ModuleBuild(pctx, android.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
Colin Cross635c3b02016-05-18 15:37:25 -0700166 return android.InitAndroidModule(module, props...)
Colin Crossd350ecd2015-04-28 13:25:36 -0700167}
168
169func GenSrcsFactory() (blueprint.Module, []interface{}) {
170 properties := &genSrcsProperties{}
171
Colin Cross635c3b02016-05-18 15:37:25 -0700172 tasks := func(ctx android.ModuleContext) []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{
Colin Cross635c3b02016-05-18 15:37:25 -0700177 in: android.Paths{in},
178 out: android.GenPathWithExt(ctx, in, properties.Output_extension),
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700179 })
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 Cross635c3b02016-05-18 15:37:25 -0700198 tasks := func(ctx android.ModuleContext) []generateTask {
Colin Crossd350ecd2015-04-28 13:25:36 -0700199 return []generateTask{
200 {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700201 in: ctx.ExpandSources(properties.Srcs, nil),
Colin Cross635c3b02016-05-18 15:37:25 -0700202 out: android.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}