blob: b55435703915080348c39d7d8b3d1e19ff879581 [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
Colin Cross5049f022015-03-18 13:28:46 -070042}
43
Colin Crossd350ecd2015-04-28 13:25:36 -070044type HostToolProvider interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070045 HostToolPath() common.OptionalPath
Colin Crossd350ecd2015-04-28 13:25:36 -070046}
Colin Cross5049f022015-03-18 13:28:46 -070047
Colin Cross7d5136f2015-05-11 13:39:40 -070048type generatorProperties struct {
49 // command to run on one or more input files. Available variables for substitution:
50 // $in: one or more input files
51 // $out: a single output file
52 // $srcDir: the root directory of the source tree
53 // The host bin directory will be in the path
54 Cmd string
55
56 // name of the module (if any) that produces the host executable. Leave empty for
57 // prebuilts or scripts that do not need a module to build them.
58 Tool string
59}
60
Colin Crossd350ecd2015-04-28 13:25:36 -070061type generator struct {
62 common.AndroidModuleBase
63
Colin Cross7d5136f2015-05-11 13:39:40 -070064 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -070065
66 tasks taskFunc
67
Dan Willemsen34cc69e2015-09-23 15:26:20 -070068 deps common.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070069 rule blueprint.Rule
70
Dan Willemsen34cc69e2015-09-23 15:26:20 -070071 outputFiles common.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -070072}
73
74type taskFunc func(ctx common.AndroidModuleContext) []generateTask
75
76type generateTask struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070077 in common.Paths
78 out common.ModuleGenPath
Colin Crossd350ecd2015-04-28 13:25:36 -070079}
80
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081func (g *generator) GeneratedSourceFiles() common.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -070082 return g.outputFiles
83}
84
Colin Cross6362e272015-10-29 15:25:03 -070085func genruleDepsMutator(ctx common.AndroidBottomUpMutatorContext) {
86 if g, ok := ctx.Module().(*generator); ok {
87 if g.properties.Tool != "" {
Dan Willemsen490fd492015-11-24 17:53:15 -080088 ctx.AddFarVariationDependencies([]blueprint.Variation{
89 {"host_or_device", common.Host.String()},
90 {"host_type", common.CurrentHostType().String()},
91 }, g.properties.Tool)
Colin Cross6362e272015-10-29 15:25:03 -070092 }
Colin Crossd350ecd2015-04-28 13:25:36 -070093 }
Colin Crossd350ecd2015-04-28 13:25:36 -070094}
95
96func (g *generator) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
97 g.rule = ctx.Rule(pctx, "generator", blueprint.RuleParams{
98 Command: "PATH=$$PATH:$hostBin " + g.properties.Cmd,
99 })
100
101 ctx.VisitDirectDeps(func(module blueprint.Module) {
102 if t, ok := module.(HostToolProvider); ok {
103 p := t.HostToolPath()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700104 if p.Valid() {
105 g.deps = append(g.deps, p.Path())
Colin Crossd350ecd2015-04-28 13:25:36 -0700106 } else {
107 ctx.ModuleErrorf("host tool %q missing output file", ctx.OtherModuleName(module))
108 }
109 } else {
110 ctx.ModuleErrorf("unknown dependency %q", ctx.OtherModuleName(module))
111 }
112 })
113
114 for _, task := range g.tasks(ctx) {
115 g.generateSourceFile(ctx, task)
116 }
117}
118
119func (g *generator) generateSourceFile(ctx common.AndroidModuleContext, task generateTask) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700120 ctx.ModuleBuild(pctx, common.ModuleBuildParams{
Colin Crossd350ecd2015-04-28 13:25:36 -0700121 Rule: g.rule,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700122 Output: task.out,
Colin Crossd350ecd2015-04-28 13:25:36 -0700123 Inputs: task.in,
124 Implicits: g.deps,
Colin Crossd350ecd2015-04-28 13:25:36 -0700125 })
126
127 g.outputFiles = append(g.outputFiles, task.out)
128}
129
130func generatorFactory(tasks taskFunc, props ...interface{}) (blueprint.Module, []interface{}) {
131 module := &generator{
132 tasks: tasks,
133 }
134
135 props = append(props, &module.properties)
136
137 return common.InitAndroidModule(module, props...)
138}
139
140func GenSrcsFactory() (blueprint.Module, []interface{}) {
141 properties := &genSrcsProperties{}
142
143 tasks := func(ctx common.AndroidModuleContext) []generateTask {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700144 srcFiles := ctx.ExpandSources(properties.Srcs, nil)
Colin Crossd350ecd2015-04-28 13:25:36 -0700145 tasks := make([]generateTask, 0, len(srcFiles))
146 for _, in := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700147 tasks = append(tasks, generateTask{
148 in: common.Paths{in},
149 out: common.GenPathWithExt(ctx, in, properties.Output_extension),
150 })
Colin Crossd350ecd2015-04-28 13:25:36 -0700151 }
152 return tasks
153 }
154
155 return generatorFactory(tasks, properties)
156}
157
158type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700159 // list of input files
Colin Cross5049f022015-03-18 13:28:46 -0700160 Srcs []string
161
Colin Cross7d5136f2015-05-11 13:39:40 -0700162 // extension that will be substituted for each output file
Colin Cross5049f022015-03-18 13:28:46 -0700163 Output_extension string
164}
165
Colin Crossd350ecd2015-04-28 13:25:36 -0700166func GenRuleFactory() (blueprint.Module, []interface{}) {
167 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700168
Colin Crossd350ecd2015-04-28 13:25:36 -0700169 tasks := func(ctx common.AndroidModuleContext) []generateTask {
170 return []generateTask{
171 {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700172 in: ctx.ExpandSources(properties.Srcs, nil),
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700173 out: properties.Out,
Colin Crossd350ecd2015-04-28 13:25:36 -0700174 },
175 }
Colin Cross5049f022015-03-18 13:28:46 -0700176 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700177
178 return generatorFactory(tasks, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700179}
180
Colin Crossd350ecd2015-04-28 13:25:36 -0700181type genRuleProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700182 // list of input files
Colin Crossd350ecd2015-04-28 13:25:36 -0700183 Srcs []string
Colin Cross5049f022015-03-18 13:28:46 -0700184
Colin Cross7d5136f2015-05-11 13:39:40 -0700185 // name of the output file that will be generated
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700186 Out common.ModuleGenPath
Colin Cross5049f022015-03-18 13:28:46 -0700187}