blob: c8be61ac55f1ce611f232ceed6e5882b8f7c0b2e [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"
Dan Willemsen8eded0a2017-09-13 16:07:44 -070022 "github.com/google/blueprint/bootstrap"
Nan Zhangea568a42017-11-08 21:20:04 -080023 "github.com/google/blueprint/proptools"
Colin Cross5049f022015-03-18 13:28:46 -070024
Colin Cross635c3b02016-05-18 15:37:25 -070025 "android/soong/android"
Jeff Gastonefc1b412017-03-29 17:29:06 -070026 "android/soong/shared"
27 "path/filepath"
Colin Cross5049f022015-03-18 13:28:46 -070028)
29
Colin Cross463a90e2015-06-17 14:20:06 -070030func init() {
Jaewoong Jung98716bd2018-12-10 08:13:18 -080031 android.RegisterModuleType("genrule_defaults", defaultsFactory)
32
Colin Cross54190b32017-10-09 15:34:10 -070033 android.RegisterModuleType("gensrcs", GenSrcsFactory)
34 android.RegisterModuleType("genrule", GenRuleFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070035}
36
Colin Cross5049f022015-03-18 13:28:46 -070037var (
Colin Cross635c3b02016-05-18 15:37:25 -070038 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross5049f022015-03-18 13:28:46 -070039)
40
Jeff Gastonefc1b412017-03-29 17:29:06 -070041func init() {
42 pctx.HostBinToolVariable("sboxCmd", "sbox")
43}
44
Colin Cross5049f022015-03-18 13:28:46 -070045type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070046 GeneratedSourceFiles() android.Paths
Colin Cross5ed99c62016-11-22 12:55:55 -080047 GeneratedHeaderDirs() android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -080048 GeneratedDeps() android.Paths
Colin Cross5049f022015-03-18 13:28:46 -070049}
50
Colin Crossd350ecd2015-04-28 13:25:36 -070051type HostToolProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -070052 HostToolPath() android.OptionalPath
Colin Crossd350ecd2015-04-28 13:25:36 -070053}
Colin Cross5049f022015-03-18 13:28:46 -070054
Dan Willemsend6ba0d52017-09-13 15:46:47 -070055type hostToolDependencyTag struct {
56 blueprint.BaseDependencyTag
Colin Cross08f15ab2018-10-04 23:29:14 -070057 label string
Dan Willemsend6ba0d52017-09-13 15:46:47 -070058}
59
Colin Cross7d5136f2015-05-11 13:39:40 -070060type generatorProperties struct {
Jeff Gastonefc1b412017-03-29 17:29:06 -070061 // The command to run on one or more input files. Cmd supports substitution of a few variables
62 // (the actual substitution is implemented in GenerateAndroidBuildActions below)
63 //
64 // Available variables for substitution:
65 //
Colin Cross2296f5b2017-10-17 21:38:14 -070066 // $(location): the path to the first entry in tools or tool_files
Colin Cross08f15ab2018-10-04 23:29:14 -070067 // $(location <label>): the path to the tool, tool_file, input or output with name <label>
Colin Cross2296f5b2017-10-17 21:38:14 -070068 // $(in): one or more input files
69 // $(out): a single output file
70 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
71 // $(genDir): the sandbox directory for this tool; contains $(out)
72 // $$: a literal $
Colin Cross6f080df2016-11-04 15:32:58 -070073 //
Jeff Gastonefc1b412017-03-29 17:29:06 -070074 // All files used must be declared as inputs (to ensure proper up-to-date checks).
75 // Use "$(in)" directly in Cmd to ensure that all inputs used are declared.
Nan Zhangea568a42017-11-08 21:20:04 -080076 Cmd *string
Colin Cross7d5136f2015-05-11 13:39:40 -070077
Colin Cross33bfb0a2016-11-21 17:23:08 -080078 // Enable reading a file containing dependencies in gcc format after the command completes
Nan Zhangea568a42017-11-08 21:20:04 -080079 Depfile *bool
Colin Cross33bfb0a2016-11-21 17:23:08 -080080
Colin Cross6f080df2016-11-04 15:32:58 -070081 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -070082 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -070083 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070084
85 // Local file that is used as the tool
Colin Cross27b922f2019-03-04 22:35:41 -080086 Tool_files []string `android:"path"`
Colin Cross5ed99c62016-11-22 12:55:55 -080087
88 // List of directories to export generated headers from
89 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -080090
91 // list of input files
Colin Cross27b922f2019-03-04 22:35:41 -080092 Srcs []string `android:"path,arch_variant"`
Dan Willemseneefa0262018-11-17 14:01:18 -080093
94 // input files to exclude
Colin Cross27b922f2019-03-04 22:35:41 -080095 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070096}
97
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070098type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -070099 android.ModuleBase
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800100 android.DefaultableModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -0700101
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700102 // For other packages to make their own genrules with extra
103 // properties
104 Extra interface{}
105
Colin Cross7d5136f2015-05-11 13:39:40 -0700106 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700107
Jeff Gaston437d23c2017-11-08 12:38:00 -0800108 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700109
Colin Cross2a076922018-10-04 23:28:25 -0700110 deps android.Paths
111 rule blueprint.Rule
112 rawCommand string
Colin Crossd350ecd2015-04-28 13:25:36 -0700113
Colin Cross5ed99c62016-11-22 12:55:55 -0800114 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700115
Colin Cross635c3b02016-05-18 15:37:25 -0700116 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800117 outputDeps android.Paths
Colin Crossd350ecd2015-04-28 13:25:36 -0700118}
119
Jeff Gaston437d23c2017-11-08 12:38:00 -0800120type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700121
122type generateTask struct {
Colin Crossbaccf5b2018-02-21 14:07:48 -0800123 in android.Paths
124 out android.WritablePaths
125 sandboxOuts []string
126 cmd string
Colin Crossd350ecd2015-04-28 13:25:36 -0700127}
128
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700129func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700130 return g.outputFiles
131}
132
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700133func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700134 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800135}
136
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700137func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800138 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700139}
140
Dan Willemsen9da9d492018-02-21 18:28:18 -0800141func (g *Module) GeneratedDeps() android.Paths {
142 return g.outputDeps
143}
144
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700145func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700146 if g, ok := ctx.Module().(*Module); ok {
Colin Cross08f15ab2018-10-04 23:29:14 -0700147 for _, tool := range g.properties.Tools {
148 tag := hostToolDependencyTag{label: tool}
149 if m := android.SrcIsModule(tool); m != "" {
150 tool = m
151 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800152 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700153 {Mutator: "arch", Variation: ctx.Config().BuildOsVariant},
Colin Cross08f15ab2018-10-04 23:29:14 -0700154 }, tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700155 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700156 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700157}
158
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700159func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross5ed99c62016-11-22 12:55:55 -0800160 if len(g.properties.Export_include_dirs) > 0 {
161 for _, dir := range g.properties.Export_include_dirs {
162 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
163 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
164 }
165 } else {
166 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
167 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700168
Colin Cross08f15ab2018-10-04 23:29:14 -0700169 locationLabels := map[string][]string{}
170 firstLabel := ""
171
172 addLocationLabel := func(label string, paths []string) {
173 if firstLabel == "" {
174 firstLabel = label
175 }
176 if _, exists := locationLabels[label]; !exists {
177 locationLabels[label] = paths
178 } else {
179 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
180 label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
181 }
182 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700183
Colin Cross6f080df2016-11-04 15:32:58 -0700184 if len(g.properties.Tools) > 0 {
Colin Cross35143d02017-11-16 00:11:20 -0800185 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700186 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
187 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700188 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700189 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700190
191 if t, ok := module.(HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800192 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800193 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800194 ctx.AddMissingDependencies([]string{tool})
195 } else {
196 ctx.ModuleErrorf("depends on disabled module %q", tool)
197 }
198 break
199 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700200 path = t.HostToolPath()
201 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
202 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
203 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700204 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700205 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
206 break
Colin Cross6f080df2016-11-04 15:32:58 -0700207 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700208 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700209 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700210 break
211 }
212
213 if path.Valid() {
214 g.deps = append(g.deps, path.Path())
Colin Cross08f15ab2018-10-04 23:29:14 -0700215 addLocationLabel(tag.label, []string{path.Path().String()})
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700216 } else {
217 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700218 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700219 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700220 })
221 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700222
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700223 if ctx.Failed() {
224 return
225 }
226
Colin Cross08f15ab2018-10-04 23:29:14 -0700227 for _, toolFile := range g.properties.Tool_files {
Colin Cross8a497952019-03-05 22:25:09 -0800228 paths := android.PathsForModuleSrc(ctx, []string{toolFile})
Colin Cross08f15ab2018-10-04 23:29:14 -0700229 g.deps = append(g.deps, paths...)
230 addLocationLabel(toolFile, paths.Strings())
231 }
232
233 var srcFiles android.Paths
234 for _, in := range g.properties.Srcs {
Colin Cross8a497952019-03-05 22:25:09 -0800235 paths := android.PathsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs)
Colin Cross08f15ab2018-10-04 23:29:14 -0700236 srcFiles = append(srcFiles, paths...)
237 addLocationLabel(in, paths.Strings())
238 }
239
240 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
241
242 for _, out := range task.out {
243 addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
Colin Cross6f080df2016-11-04 15:32:58 -0700244 }
245
Jeff Gaston02a684b2017-10-27 14:59:27 -0700246 referencedDepfile := false
247
Jeff Gaston437d23c2017-11-08 12:38:00 -0800248 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross85a2e892018-07-09 09:45:06 -0700249 // report the error directly without returning an error to android.Expand to catch multiple errors in a
250 // single run
251 reportError := func(fmt string, args ...interface{}) (string, error) {
252 ctx.PropertyErrorf("cmd", fmt, args...)
253 return "SOONG_ERROR", nil
254 }
255
Colin Cross6f080df2016-11-04 15:32:58 -0700256 switch name {
257 case "location":
Colin Cross08f15ab2018-10-04 23:29:14 -0700258 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
Colin Cross85a2e892018-07-09 09:45:06 -0700259 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700260 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700261 paths := locationLabels[firstLabel]
262 if len(paths) == 0 {
263 return reportError("default label %q has no files", firstLabel)
264 } else if len(paths) > 1 {
265 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
266 firstLabel, firstLabel)
267 }
268 return locationLabels[firstLabel][0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700269 case "in":
270 return "${in}", nil
271 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700272 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800273 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700274 referencedDepfile = true
Nan Zhangea568a42017-11-08 21:20:04 -0800275 if !Bool(g.properties.Depfile) {
Colin Cross85a2e892018-07-09 09:45:06 -0700276 return reportError("$(depfile) used without depfile property")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800277 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700278 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700279 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800280 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700281 default:
282 if strings.HasPrefix(name, "location ") {
283 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Colin Cross08f15ab2018-10-04 23:29:14 -0700284 if paths, ok := locationLabels[label]; ok {
285 if len(paths) == 0 {
286 return reportError("label %q has no files", label)
287 } else if len(paths) > 1 {
288 return reportError("label %q has multiple files, use $(locations %s) to reference it",
289 label, label)
290 }
291 return paths[0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700292 } else {
Colin Cross85a2e892018-07-09 09:45:06 -0700293 return reportError("unknown location label %q", label)
Colin Cross6f080df2016-11-04 15:32:58 -0700294 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700295 } else if strings.HasPrefix(name, "locations ") {
296 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
297 if paths, ok := locationLabels[label]; ok {
298 if len(paths) == 0 {
299 return reportError("label %q has no files", label)
300 }
301 return strings.Join(paths, " "), nil
302 } else {
303 return reportError("unknown locations label %q", label)
304 }
305 } else {
306 return reportError("unknown variable '$(%s)'", name)
Colin Cross6f080df2016-11-04 15:32:58 -0700307 }
Colin Cross6f080df2016-11-04 15:32:58 -0700308 }
309 })
310
311 if err != nil {
312 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700313 return
Colin Cross6f080df2016-11-04 15:32:58 -0700314 }
315
Colin Cross85a2e892018-07-09 09:45:06 -0700316 if Bool(g.properties.Depfile) && !referencedDepfile {
317 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
318 }
319
Jeff Gastonefc1b412017-03-29 17:29:06 -0700320 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700321 buildDir := android.PathForOutput(ctx).String()
322 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700323
324 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
325 // to be replaced later by ninja_strings.go
Jeff Gaston02a684b2017-10-27 14:59:27 -0700326 depfilePlaceholder := ""
Nan Zhangea568a42017-11-08 21:20:04 -0800327 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700328 depfilePlaceholder = "$depfileArgs"
329 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800330
Jeff Gaston437d23c2017-11-08 12:38:00 -0800331 genDir := android.PathForModuleGen(ctx)
Jeff Gaston31603062017-12-08 12:45:35 -0800332 // Escape the command for the shell
333 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
Colin Cross2a076922018-10-04 23:28:25 -0700334 g.rawCommand = rawCommand
Jeff Gaston31603062017-12-08 12:45:35 -0800335 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
336 sandboxPath, genDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700337
Colin Cross33bfb0a2016-11-21 17:23:08 -0800338 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700339 Command: sandboxCommand,
340 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800341 }
Colin Cross15e86d92017-10-20 15:07:08 -0700342 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800343 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800344 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700345 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800346 }
347 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700348
Jeff Gaston437d23c2017-11-08 12:38:00 -0800349 g.generateSourceFile(ctx, task)
350
Colin Crossd350ecd2015-04-28 13:25:36 -0700351}
352
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700353func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700354 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700355 if len(task.out) == 0 {
356 ctx.ModuleErrorf("must have at least one output file")
357 return
358 }
Colin Cross67a5c132017-05-09 13:45:28 -0700359 if len(task.out) == 1 {
360 desc += " " + task.out[0].Base()
361 }
362
Jeff Gaston02a684b2017-10-27 14:59:27 -0700363 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800364 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700365 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
366 }
367
Colin Crossae887032017-10-23 17:16:14 -0700368 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700369 Rule: g.rule,
370 Description: "generate",
371 Output: task.out[0],
372 ImplicitOutputs: task.out[1:],
373 Inputs: task.in,
374 Implicits: g.deps,
375 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800376 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700377 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800378 }
Nan Zhangea568a42017-11-08 21:20:04 -0800379 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700380 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
381 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800382 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700383
Colin Crossae887032017-10-23 17:16:14 -0700384 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700385
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700386 for _, outputFile := range task.out {
387 g.outputFiles = append(g.outputFiles, outputFile)
388 }
Dan Willemsen9da9d492018-02-21 18:28:18 -0800389 g.outputDeps = append(g.outputDeps, task.out[0])
Colin Crossd350ecd2015-04-28 13:25:36 -0700390}
391
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700392// Collect information for opening IDE project files in java/jdeps.go.
393func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
394 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
395 for _, src := range g.properties.Srcs {
396 if strings.HasPrefix(src, ":") {
397 src = strings.Trim(src, ":")
398 dpInfo.Deps = append(dpInfo.Deps, src)
399 }
400 }
401}
402
Jeff Gaston437d23c2017-11-08 12:38:00 -0800403func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700404 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800405 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700406 }
407
Colin Cross36242852017-06-23 15:06:31 -0700408 module.AddProperties(props...)
409 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700410
Colin Cross36242852017-06-23 15:06:31 -0700411 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700412}
413
Colin Crossbaccf5b2018-02-21 14:07:48 -0800414// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
415func pathToSandboxOut(path android.Path, genDir android.Path) string {
416 relOut, err := filepath.Rel(genDir.String(), path.String())
417 if err != nil {
418 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
419 }
420 return filepath.Join("__SBOX_OUT_DIR__", relOut)
421
422}
423
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700424func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700425 properties := &genSrcsProperties{}
426
Jeff Gaston437d23c2017-11-08 12:38:00 -0800427 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
428 commands := []string{}
429 outFiles := android.WritablePaths{}
Colin Crossbaccf5b2018-02-21 14:07:48 -0800430 genDir := android.PathForModuleGen(ctx)
431 sandboxOuts := []string{}
Colin Crossd350ecd2015-04-28 13:25:36 -0700432 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800433 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800434 outFiles = append(outFiles, outFile)
435
Colin Crossbaccf5b2018-02-21 14:07:48 -0800436 sandboxOutfile := pathToSandboxOut(outFile, genDir)
437 sandboxOuts = append(sandboxOuts, sandboxOutfile)
438
Jeff Gaston437d23c2017-11-08 12:38:00 -0800439 command, err := android.Expand(rawCommand, func(name string) (string, error) {
440 switch name {
441 case "in":
442 return in.String(), nil
443 case "out":
444 return sandboxOutfile, nil
445 default:
446 return "$(" + name + ")", nil
447 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700448 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800449 if err != nil {
450 ctx.PropertyErrorf("cmd", err.Error())
451 }
452
453 // escape the command in case for example it contains '#', an odd number of '"', etc
Colin Cross0b9f31f2019-02-28 11:00:01 -0800454 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800455 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700456 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800457 fullCommand := strings.Join(commands, " && ")
458
459 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800460 in: srcFiles,
461 out: outFiles,
462 sandboxOuts: sandboxOuts,
463 cmd: fullCommand,
Jeff Gaston437d23c2017-11-08 12:38:00 -0800464 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700465 }
466
Jeff Gaston437d23c2017-11-08 12:38:00 -0800467 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700468}
469
Colin Cross54190b32017-10-09 15:34:10 -0700470func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700471 m := NewGenSrcs()
472 android.InitAndroidModule(m)
473 return m
474}
475
Colin Crossd350ecd2015-04-28 13:25:36 -0700476type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700477 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800478 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700479}
480
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700481func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700482 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700483
Jeff Gaston437d23c2017-11-08 12:38:00 -0800484 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700485 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800486 sandboxOuts := make([]string, len(properties.Out))
487 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700488 for i, out := range properties.Out {
489 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800490 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700491 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800492 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800493 in: srcFiles,
494 out: outs,
495 sandboxOuts: sandboxOuts,
496 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700497 }
Colin Cross5049f022015-03-18 13:28:46 -0700498 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700499
Jeff Gaston437d23c2017-11-08 12:38:00 -0800500 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700501}
502
Colin Cross54190b32017-10-09 15:34:10 -0700503func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700504 m := NewGenRule()
505 android.InitAndroidModule(m)
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800506 android.InitDefaultableModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700507 return m
508}
509
Colin Crossd350ecd2015-04-28 13:25:36 -0700510type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700511 // names of the output files that will be generated
Colin Crossef354482018-10-23 11:27:50 -0700512 Out []string `android:"arch_variant"`
Colin Cross5049f022015-03-18 13:28:46 -0700513}
Nan Zhangea568a42017-11-08 21:20:04 -0800514
515var Bool = proptools.Bool
516var String = proptools.String
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800517
518//
519// Defaults
520//
521type Defaults struct {
522 android.ModuleBase
523 android.DefaultsModuleBase
524}
525
526func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
527}
528
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800529func defaultsFactory() android.Module {
530 return DefaultsFactory()
531}
532
533func DefaultsFactory(props ...interface{}) android.Module {
534 module := &Defaults{}
535
536 module.AddProperties(props...)
537 module.AddProperties(
538 &generatorProperties{},
539 &genRuleProperties{},
540 )
541
542 android.InitDefaultsModule(module)
543
544 return module
545}