blob: 498858d28fc7e45d946f3d0801534563932228f3 [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 Cross6f080df2016-11-04 15:32:58 -070018 "fmt"
19 "strings"
Dan Willemsen3f4539b2016-09-28 16:19:10 -070020
Colin Cross70b40592015-03-23 12:57:34 -070021 "github.com/google/blueprint"
Colin Cross5049f022015-03-18 13:28:46 -070022
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() {
Colin Cross798bfce2016-10-12 14:28:16 -070027 android.RegisterModuleType("gensrcs", GenSrcsFactory)
28 android.RegisterModuleType("genrule", GenRuleFactory)
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
Colin Cross5049f022015-03-18 13:28:46 -070035type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070036 GeneratedSourceFiles() android.Paths
37 GeneratedHeaderDir() android.Path
Colin Cross5049f022015-03-18 13:28:46 -070038}
39
Colin Crossd350ecd2015-04-28 13:25:36 -070040type HostToolProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -070041 HostToolPath() android.OptionalPath
Colin Crossd350ecd2015-04-28 13:25:36 -070042}
Colin Cross5049f022015-03-18 13:28:46 -070043
Colin Cross7d5136f2015-05-11 13:39:40 -070044type generatorProperties struct {
45 // command to run on one or more input files. Available variables for substitution:
Colin Cross6f080df2016-11-04 15:32:58 -070046 // $(location): the path to the first entry in tools or tool_files
47 // $(location <label>): the path to the tool or tool_file with name <label>
48 // $(in): one or more input files
49 // $(out): a single output file
50 // $(genDir): the sandbox directory for this tool; contains $(out)
51 // $$: a literal $
52 //
53 // DO NOT directly reference paths to files in the source tree, or the
54 // command will be missing proper dependencies to re-run if the files
55 // change.
Colin Cross7d5136f2015-05-11 13:39:40 -070056 Cmd string
57
Colin Cross6f080df2016-11-04 15:32:58 -070058 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -070059 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -070060 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070061
62 // Local file that is used as the tool
Colin Cross6f080df2016-11-04 15:32:58 -070063 Tool_files []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
Dan Willemsen9c8681f2016-09-28 16:21:00 -070085 out android.WritablePaths
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 Cross1e676be2016-10-12 14:38:15 -070096func (g *generator) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6362e272015-10-29 15:25:03 -070097 if g, ok := ctx.Module().(*generator); ok {
Colin Cross6f080df2016-11-04 15:32:58 -070098 if len(g.properties.Tools) > 0 {
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 Cross6f080df2016-11-04 15:32:58 -0700101 }, nil, g.properties.Tools...)
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) {
Colin Cross6f080df2016-11-04 15:32:58 -0700107 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
108 ctx.ModuleErrorf("at least one `tools` or `tool_files` is required")
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700109 return
110 }
111
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700112 g.genPath = android.PathForModuleGen(ctx, "")
113
Colin Cross6f080df2016-11-04 15:32:58 -0700114 tools := map[string]android.Path{}
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700115
Colin Cross6f080df2016-11-04 15:32:58 -0700116 if len(g.properties.Tools) > 0 {
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700117 ctx.VisitDirectDeps(func(module blueprint.Module) {
118 if t, ok := module.(HostToolProvider); ok {
119 p := t.HostToolPath()
120 if p.Valid() {
121 g.deps = append(g.deps, p.Path())
Colin Cross6f080df2016-11-04 15:32:58 -0700122 tool := ctx.OtherModuleName(module)
123 if _, exists := tools[tool]; !exists {
124 tools[tool] = p.Path()
125 } else {
126 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], p.Path().String())
127 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700128 } 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 Cross6f080df2016-11-04 15:32:58 -0700137 for _, tool := range g.properties.Tool_files {
138 toolPath := android.PathForModuleSrc(ctx, tool)
139 g.deps = append(g.deps, toolPath)
140 if _, exists := tools[tool]; !exists {
141 tools[tool] = toolPath
142 } else {
143 ctx.ModuleErrorf("multiple tools for %q, %q and %q", tool, tools[tool], toolPath.String())
144 }
145 }
146
147 cmd, err := android.Expand(g.properties.Cmd, func(name string) (string, error) {
148 switch name {
149 case "location":
150 if len(g.properties.Tools) > 0 {
151 return tools[g.properties.Tools[0]].String(), nil
152 } else {
153 return tools[g.properties.Tool_files[0]].String(), nil
154 }
155 case "in":
156 return "${in}", nil
157 case "out":
158 return "${out}", nil
159 case "genDir":
160 return g.genPath.String(), nil
161 default:
162 if strings.HasPrefix(name, "location ") {
163 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
164 if tool, ok := tools[label]; ok {
165 return tool.String(), nil
166 } else {
167 return "", fmt.Errorf("unknown location label %q", label)
168 }
169 }
170 return "", fmt.Errorf("unknown variable '$(%s)'", name)
171 }
172 })
173
174 if err != nil {
175 ctx.PropertyErrorf("cmd", "%s", err.Error())
176 }
177
178 g.rule = ctx.Rule(pctx, "generator", blueprint.RuleParams{
179 Command: cmd,
180 })
181
Colin Crossd350ecd2015-04-28 13:25:36 -0700182 for _, task := range g.tasks(ctx) {
Colin Cross6f080df2016-11-04 15:32:58 -0700183 g.generateSourceFile(ctx, task)
Colin Crossd350ecd2015-04-28 13:25:36 -0700184 }
185}
186
Colin Cross6f080df2016-11-04 15:32:58 -0700187func (g *generator) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross635c3b02016-05-18 15:37:25 -0700188 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Crossd350ecd2015-04-28 13:25:36 -0700189 Rule: g.rule,
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700190 Outputs: task.out,
Colin Crossd350ecd2015-04-28 13:25:36 -0700191 Inputs: task.in,
192 Implicits: g.deps,
Colin Crossd350ecd2015-04-28 13:25:36 -0700193 })
194
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700195 for _, outputFile := range task.out {
196 g.outputFiles = append(g.outputFiles, outputFile)
197 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700198}
199
200func generatorFactory(tasks taskFunc, props ...interface{}) (blueprint.Module, []interface{}) {
201 module := &generator{
202 tasks: tasks,
203 }
204
205 props = append(props, &module.properties)
206
Colin Cross635c3b02016-05-18 15:37:25 -0700207 return android.InitAndroidModule(module, props...)
Colin Crossd350ecd2015-04-28 13:25:36 -0700208}
209
210func GenSrcsFactory() (blueprint.Module, []interface{}) {
211 properties := &genSrcsProperties{}
212
Colin Cross635c3b02016-05-18 15:37:25 -0700213 tasks := func(ctx android.ModuleContext) []generateTask {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700214 srcFiles := ctx.ExpandSources(properties.Srcs, nil)
Colin Crossd350ecd2015-04-28 13:25:36 -0700215 tasks := make([]generateTask, 0, len(srcFiles))
216 for _, in := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700217 tasks = append(tasks, generateTask{
Colin Cross635c3b02016-05-18 15:37:25 -0700218 in: android.Paths{in},
Dan Willemsen21ec4902016-11-02 20:43:13 -0700219 out: android.WritablePaths{android.GenPathWithExt(ctx, "", in, properties.Output_extension)},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700220 })
Colin Crossd350ecd2015-04-28 13:25:36 -0700221 }
222 return tasks
223 }
224
225 return generatorFactory(tasks, properties)
226}
227
228type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700229 // list of input files
Colin Cross5049f022015-03-18 13:28:46 -0700230 Srcs []string
231
Colin Cross7d5136f2015-05-11 13:39:40 -0700232 // extension that will be substituted for each output file
Colin Cross5049f022015-03-18 13:28:46 -0700233 Output_extension string
234}
235
Colin Crossd350ecd2015-04-28 13:25:36 -0700236func GenRuleFactory() (blueprint.Module, []interface{}) {
237 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700238
Colin Cross635c3b02016-05-18 15:37:25 -0700239 tasks := func(ctx android.ModuleContext) []generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700240 outs := make(android.WritablePaths, len(properties.Out))
241 for i, out := range properties.Out {
242 outs[i] = android.PathForModuleGen(ctx, out)
243 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700244 return []generateTask{
245 {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700246 in: ctx.ExpandSources(properties.Srcs, nil),
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700247 out: outs,
Colin Crossd350ecd2015-04-28 13:25:36 -0700248 },
249 }
Colin Cross5049f022015-03-18 13:28:46 -0700250 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700251
252 return generatorFactory(tasks, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700253}
254
Colin Crossd350ecd2015-04-28 13:25:36 -0700255type genRuleProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700256 // list of input files
Colin Crossd350ecd2015-04-28 13:25:36 -0700257 Srcs []string
Colin Cross5049f022015-03-18 13:28:46 -0700258
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700259 // names of the output files that will be generated
260 Out []string
Colin Cross5049f022015-03-18 13:28:46 -0700261}