blob: 7695ffbd77536cc81c674bfa7b3d56280d8247d5 [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 Cross6f080df2016-11-04 15:32:58 -070086 Tool_files []string
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 Crossef354482018-10-23 11:27:50 -070092 Srcs []string `android:"arch_variant"`
Dan Willemseneefa0262018-11-17 14:01:18 -080093
94 // input files to exclude
95 Exclude_srcs []string `android:"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) {
Colin Cross708c4242017-01-13 18:05:49 -0800146 android.ExtractSourcesDeps(ctx, g.properties.Srcs)
Colin Crossac87c992017-12-08 18:23:13 -0800147 android.ExtractSourcesDeps(ctx, g.properties.Tool_files)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700148 if g, ok := ctx.Module().(*Module); ok {
Colin Cross08f15ab2018-10-04 23:29:14 -0700149 for _, tool := range g.properties.Tools {
150 tag := hostToolDependencyTag{label: tool}
151 if m := android.SrcIsModule(tool); m != "" {
152 tool = m
153 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800154 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700155 {Mutator: "arch", Variation: ctx.Config().BuildOsVariant},
Colin Cross08f15ab2018-10-04 23:29:14 -0700156 }, tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700157 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700158 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700159}
160
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700161func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross5ed99c62016-11-22 12:55:55 -0800162 if len(g.properties.Export_include_dirs) > 0 {
163 for _, dir := range g.properties.Export_include_dirs {
164 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
165 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
166 }
167 } else {
168 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
169 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700170
Colin Cross08f15ab2018-10-04 23:29:14 -0700171 locationLabels := map[string][]string{}
172 firstLabel := ""
173
174 addLocationLabel := func(label string, paths []string) {
175 if firstLabel == "" {
176 firstLabel = label
177 }
178 if _, exists := locationLabels[label]; !exists {
179 locationLabels[label] = paths
180 } else {
181 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
182 label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
183 }
184 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700185
Colin Cross6f080df2016-11-04 15:32:58 -0700186 if len(g.properties.Tools) > 0 {
Colin Cross35143d02017-11-16 00:11:20 -0800187 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700188 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
189 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700190 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700191 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700192
193 if t, ok := module.(HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800194 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800195 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800196 ctx.AddMissingDependencies([]string{tool})
197 } else {
198 ctx.ModuleErrorf("depends on disabled module %q", tool)
199 }
200 break
201 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700202 path = t.HostToolPath()
203 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
204 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
205 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700206 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700207 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
208 break
Colin Cross6f080df2016-11-04 15:32:58 -0700209 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700210 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700211 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700212 break
213 }
214
215 if path.Valid() {
216 g.deps = append(g.deps, path.Path())
Colin Cross08f15ab2018-10-04 23:29:14 -0700217 addLocationLabel(tag.label, []string{path.Path().String()})
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700218 } else {
219 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700220 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700221 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700222 })
223 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700224
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700225 if ctx.Failed() {
226 return
227 }
228
Colin Cross08f15ab2018-10-04 23:29:14 -0700229 for _, toolFile := range g.properties.Tool_files {
230 paths := ctx.ExpandSources([]string{toolFile}, nil)
231 g.deps = append(g.deps, paths...)
232 addLocationLabel(toolFile, paths.Strings())
233 }
234
235 var srcFiles android.Paths
236 for _, in := range g.properties.Srcs {
Dan Willemseneefa0262018-11-17 14:01:18 -0800237 paths := ctx.ExpandSources([]string{in}, g.properties.Exclude_srcs)
Colin Cross08f15ab2018-10-04 23:29:14 -0700238 srcFiles = append(srcFiles, paths...)
239 addLocationLabel(in, paths.Strings())
240 }
241
242 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
243
244 for _, out := range task.out {
245 addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
Colin Cross6f080df2016-11-04 15:32:58 -0700246 }
247
Jeff Gaston02a684b2017-10-27 14:59:27 -0700248 referencedDepfile := false
249
Jeff Gaston437d23c2017-11-08 12:38:00 -0800250 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross85a2e892018-07-09 09:45:06 -0700251 // report the error directly without returning an error to android.Expand to catch multiple errors in a
252 // single run
253 reportError := func(fmt string, args ...interface{}) (string, error) {
254 ctx.PropertyErrorf("cmd", fmt, args...)
255 return "SOONG_ERROR", nil
256 }
257
Colin Cross6f080df2016-11-04 15:32:58 -0700258 switch name {
259 case "location":
Colin Cross08f15ab2018-10-04 23:29:14 -0700260 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
Colin Cross85a2e892018-07-09 09:45:06 -0700261 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700262 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700263 paths := locationLabels[firstLabel]
264 if len(paths) == 0 {
265 return reportError("default label %q has no files", firstLabel)
266 } else if len(paths) > 1 {
267 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
268 firstLabel, firstLabel)
269 }
270 return locationLabels[firstLabel][0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700271 case "in":
272 return "${in}", nil
273 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700274 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800275 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700276 referencedDepfile = true
Nan Zhangea568a42017-11-08 21:20:04 -0800277 if !Bool(g.properties.Depfile) {
Colin Cross85a2e892018-07-09 09:45:06 -0700278 return reportError("$(depfile) used without depfile property")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800279 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700280 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700281 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800282 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700283 default:
284 if strings.HasPrefix(name, "location ") {
285 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Colin Cross08f15ab2018-10-04 23:29:14 -0700286 if paths, ok := locationLabels[label]; ok {
287 if len(paths) == 0 {
288 return reportError("label %q has no files", label)
289 } else if len(paths) > 1 {
290 return reportError("label %q has multiple files, use $(locations %s) to reference it",
291 label, label)
292 }
293 return paths[0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700294 } else {
Colin Cross85a2e892018-07-09 09:45:06 -0700295 return reportError("unknown location label %q", label)
Colin Cross6f080df2016-11-04 15:32:58 -0700296 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700297 } else if strings.HasPrefix(name, "locations ") {
298 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
299 if paths, ok := locationLabels[label]; ok {
300 if len(paths) == 0 {
301 return reportError("label %q has no files", label)
302 }
303 return strings.Join(paths, " "), nil
304 } else {
305 return reportError("unknown locations label %q", label)
306 }
307 } else {
308 return reportError("unknown variable '$(%s)'", name)
Colin Cross6f080df2016-11-04 15:32:58 -0700309 }
Colin Cross6f080df2016-11-04 15:32:58 -0700310 }
311 })
312
313 if err != nil {
314 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700315 return
Colin Cross6f080df2016-11-04 15:32:58 -0700316 }
317
Colin Cross85a2e892018-07-09 09:45:06 -0700318 if Bool(g.properties.Depfile) && !referencedDepfile {
319 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
320 }
321
Jeff Gastonefc1b412017-03-29 17:29:06 -0700322 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700323 buildDir := android.PathForOutput(ctx).String()
324 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700325
326 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
327 // to be replaced later by ninja_strings.go
Jeff Gaston02a684b2017-10-27 14:59:27 -0700328 depfilePlaceholder := ""
Nan Zhangea568a42017-11-08 21:20:04 -0800329 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700330 depfilePlaceholder = "$depfileArgs"
331 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800332
Jeff Gaston437d23c2017-11-08 12:38:00 -0800333 genDir := android.PathForModuleGen(ctx)
Jeff Gaston31603062017-12-08 12:45:35 -0800334 // Escape the command for the shell
335 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
Colin Cross2a076922018-10-04 23:28:25 -0700336 g.rawCommand = rawCommand
Jeff Gaston31603062017-12-08 12:45:35 -0800337 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
338 sandboxPath, genDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700339
Colin Cross33bfb0a2016-11-21 17:23:08 -0800340 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700341 Command: sandboxCommand,
342 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800343 }
Colin Cross15e86d92017-10-20 15:07:08 -0700344 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800345 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800346 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700347 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800348 }
349 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700350
Jeff Gaston437d23c2017-11-08 12:38:00 -0800351 g.generateSourceFile(ctx, task)
352
Colin Crossd350ecd2015-04-28 13:25:36 -0700353}
354
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700355func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700356 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700357 if len(task.out) == 0 {
358 ctx.ModuleErrorf("must have at least one output file")
359 return
360 }
Colin Cross67a5c132017-05-09 13:45:28 -0700361 if len(task.out) == 1 {
362 desc += " " + task.out[0].Base()
363 }
364
Jeff Gaston02a684b2017-10-27 14:59:27 -0700365 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800366 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700367 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
368 }
369
Colin Crossae887032017-10-23 17:16:14 -0700370 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700371 Rule: g.rule,
372 Description: "generate",
373 Output: task.out[0],
374 ImplicitOutputs: task.out[1:],
375 Inputs: task.in,
376 Implicits: g.deps,
377 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800378 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700379 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800380 }
Nan Zhangea568a42017-11-08 21:20:04 -0800381 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700382 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
383 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800384 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700385
Colin Crossae887032017-10-23 17:16:14 -0700386 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700387
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700388 for _, outputFile := range task.out {
389 g.outputFiles = append(g.outputFiles, outputFile)
390 }
Dan Willemsen9da9d492018-02-21 18:28:18 -0800391 g.outputDeps = append(g.outputDeps, task.out[0])
Colin Crossd350ecd2015-04-28 13:25:36 -0700392}
393
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700394// Collect information for opening IDE project files in java/jdeps.go.
395func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
396 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
397 for _, src := range g.properties.Srcs {
398 if strings.HasPrefix(src, ":") {
399 src = strings.Trim(src, ":")
400 dpInfo.Deps = append(dpInfo.Deps, src)
401 }
402 }
403}
404
Jeff Gaston437d23c2017-11-08 12:38:00 -0800405func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700406 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800407 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700408 }
409
Colin Cross36242852017-06-23 15:06:31 -0700410 module.AddProperties(props...)
411 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700412
Colin Cross36242852017-06-23 15:06:31 -0700413 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700414}
415
Colin Crossbaccf5b2018-02-21 14:07:48 -0800416// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
417func pathToSandboxOut(path android.Path, genDir android.Path) string {
418 relOut, err := filepath.Rel(genDir.String(), path.String())
419 if err != nil {
420 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
421 }
422 return filepath.Join("__SBOX_OUT_DIR__", relOut)
423
424}
425
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700426func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700427 properties := &genSrcsProperties{}
428
Jeff Gaston437d23c2017-11-08 12:38:00 -0800429 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
430 commands := []string{}
431 outFiles := android.WritablePaths{}
Colin Crossbaccf5b2018-02-21 14:07:48 -0800432 genDir := android.PathForModuleGen(ctx)
433 sandboxOuts := []string{}
Colin Crossd350ecd2015-04-28 13:25:36 -0700434 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800435 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800436 outFiles = append(outFiles, outFile)
437
Colin Crossbaccf5b2018-02-21 14:07:48 -0800438 sandboxOutfile := pathToSandboxOut(outFile, genDir)
439 sandboxOuts = append(sandboxOuts, sandboxOutfile)
440
Jeff Gaston437d23c2017-11-08 12:38:00 -0800441 command, err := android.Expand(rawCommand, func(name string) (string, error) {
442 switch name {
443 case "in":
444 return in.String(), nil
445 case "out":
446 return sandboxOutfile, nil
447 default:
448 return "$(" + name + ")", nil
449 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700450 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800451 if err != nil {
452 ctx.PropertyErrorf("cmd", err.Error())
453 }
454
455 // escape the command in case for example it contains '#', an odd number of '"', etc
456 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape([]string{command})[0])
457 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700458 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800459 fullCommand := strings.Join(commands, " && ")
460
461 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800462 in: srcFiles,
463 out: outFiles,
464 sandboxOuts: sandboxOuts,
465 cmd: fullCommand,
Jeff Gaston437d23c2017-11-08 12:38:00 -0800466 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700467 }
468
Jeff Gaston437d23c2017-11-08 12:38:00 -0800469 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700470}
471
Colin Cross54190b32017-10-09 15:34:10 -0700472func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700473 m := NewGenSrcs()
474 android.InitAndroidModule(m)
475 return m
476}
477
Colin Crossd350ecd2015-04-28 13:25:36 -0700478type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700479 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800480 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700481}
482
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700483func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700484 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700485
Jeff Gaston437d23c2017-11-08 12:38:00 -0800486 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700487 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800488 sandboxOuts := make([]string, len(properties.Out))
489 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700490 for i, out := range properties.Out {
491 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800492 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700493 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800494 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800495 in: srcFiles,
496 out: outs,
497 sandboxOuts: sandboxOuts,
498 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700499 }
Colin Cross5049f022015-03-18 13:28:46 -0700500 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700501
Jeff Gaston437d23c2017-11-08 12:38:00 -0800502 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700503}
504
Colin Cross54190b32017-10-09 15:34:10 -0700505func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700506 m := NewGenRule()
507 android.InitAndroidModule(m)
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800508 android.InitDefaultableModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700509 return m
510}
511
Colin Crossd350ecd2015-04-28 13:25:36 -0700512type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700513 // names of the output files that will be generated
Colin Crossef354482018-10-23 11:27:50 -0700514 Out []string `android:"arch_variant"`
Colin Cross5049f022015-03-18 13:28:46 -0700515}
Nan Zhangea568a42017-11-08 21:20:04 -0800516
517var Bool = proptools.Bool
518var String = proptools.String
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800519
520//
521// Defaults
522//
523type Defaults struct {
524 android.ModuleBase
525 android.DefaultsModuleBase
526}
527
528func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
529}
530
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800531func defaultsFactory() android.Module {
532 return DefaultsFactory()
533}
534
535func DefaultsFactory(props ...interface{}) android.Module {
536 module := &Defaults{}
537
538 module.AddProperties(props...)
539 module.AddProperties(
540 &generatorProperties{},
541 &genRuleProperties{},
542 )
543
544 android.InitDefaultsModule(module)
545
546 return module
547}