blob: 34adbc06c0888b51571260fe8a0bd73f883af5f2 [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"
Colin Crossa4ad2b02019-03-18 22:15:32 -070019 "io"
Colin Cross6f080df2016-11-04 15:32:58 -070020 "strings"
Dan Willemsen3f4539b2016-09-28 16:19:10 -070021
Colin Cross70b40592015-03-23 12:57:34 -070022 "github.com/google/blueprint"
Dan Willemsen8eded0a2017-09-13 16:07:44 -070023 "github.com/google/blueprint/bootstrap"
Nan Zhangea568a42017-11-08 21:20:04 -080024 "github.com/google/blueprint/proptools"
Colin Cross5049f022015-03-18 13:28:46 -070025
Colin Cross635c3b02016-05-18 15:37:25 -070026 "android/soong/android"
Jeff Gastonefc1b412017-03-29 17:29:06 -070027 "android/soong/shared"
28 "path/filepath"
Colin Cross5049f022015-03-18 13:28:46 -070029)
30
Colin Cross463a90e2015-06-17 14:20:06 -070031func init() {
Jaewoong Jung98716bd2018-12-10 08:13:18 -080032 android.RegisterModuleType("genrule_defaults", defaultsFactory)
33
Colin Cross54190b32017-10-09 15:34:10 -070034 android.RegisterModuleType("gensrcs", GenSrcsFactory)
35 android.RegisterModuleType("genrule", GenRuleFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070036}
37
Colin Cross5049f022015-03-18 13:28:46 -070038var (
Colin Cross635c3b02016-05-18 15:37:25 -070039 pctx = android.NewPackageContext("android/soong/genrule")
Colin Cross5049f022015-03-18 13:28:46 -070040)
41
Jeff Gastonefc1b412017-03-29 17:29:06 -070042func init() {
43 pctx.HostBinToolVariable("sboxCmd", "sbox")
44}
45
Colin Cross5049f022015-03-18 13:28:46 -070046type SourceFileGenerator interface {
Colin Cross635c3b02016-05-18 15:37:25 -070047 GeneratedSourceFiles() android.Paths
Colin Cross5ed99c62016-11-22 12:55:55 -080048 GeneratedHeaderDirs() android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -080049 GeneratedDeps() android.Paths
Colin Cross5049f022015-03-18 13:28:46 -070050}
51
Colin Crossfe17f6f2019-03-28 19:30:56 -070052// Alias for android.HostToolProvider
53// Deprecated: use android.HostToolProvider instead.
Colin Crossd350ecd2015-04-28 13:25:36 -070054type HostToolProvider interface {
Colin Crossfe17f6f2019-03-28 19:30:56 -070055 android.HostToolProvider
Colin Crossd350ecd2015-04-28 13:25:36 -070056}
Colin Cross5049f022015-03-18 13:28:46 -070057
Dan Willemsend6ba0d52017-09-13 15:46:47 -070058type hostToolDependencyTag struct {
59 blueprint.BaseDependencyTag
Colin Cross08f15ab2018-10-04 23:29:14 -070060 label string
Dan Willemsend6ba0d52017-09-13 15:46:47 -070061}
62
Colin Cross7d5136f2015-05-11 13:39:40 -070063type generatorProperties struct {
Jeff Gastonefc1b412017-03-29 17:29:06 -070064 // The command to run on one or more input files. Cmd supports substitution of a few variables
65 // (the actual substitution is implemented in GenerateAndroidBuildActions below)
66 //
67 // Available variables for substitution:
68 //
Colin Cross2296f5b2017-10-17 21:38:14 -070069 // $(location): the path to the first entry in tools or tool_files
Colin Cross08f15ab2018-10-04 23:29:14 -070070 // $(location <label>): the path to the tool, tool_file, input or output with name <label>
Colin Cross2296f5b2017-10-17 21:38:14 -070071 // $(in): one or more input files
72 // $(out): a single output file
73 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
74 // $(genDir): the sandbox directory for this tool; contains $(out)
75 // $$: a literal $
Colin Cross6f080df2016-11-04 15:32:58 -070076 //
Jeff Gastonefc1b412017-03-29 17:29:06 -070077 // All files used must be declared as inputs (to ensure proper up-to-date checks).
78 // Use "$(in)" directly in Cmd to ensure that all inputs used are declared.
Nan Zhangea568a42017-11-08 21:20:04 -080079 Cmd *string
Colin Cross7d5136f2015-05-11 13:39:40 -070080
Colin Cross33bfb0a2016-11-21 17:23:08 -080081 // Enable reading a file containing dependencies in gcc format after the command completes
Nan Zhangea568a42017-11-08 21:20:04 -080082 Depfile *bool
Colin Cross33bfb0a2016-11-21 17:23:08 -080083
Colin Cross6f080df2016-11-04 15:32:58 -070084 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -070085 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -070086 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070087
88 // Local file that is used as the tool
Colin Cross27b922f2019-03-04 22:35:41 -080089 Tool_files []string `android:"path"`
Colin Cross5ed99c62016-11-22 12:55:55 -080090
91 // List of directories to export generated headers from
92 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -080093
94 // list of input files
Colin Cross27b922f2019-03-04 22:35:41 -080095 Srcs []string `android:"path,arch_variant"`
Dan Willemseneefa0262018-11-17 14:01:18 -080096
97 // input files to exclude
Colin Cross27b922f2019-03-04 22:35:41 -080098 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070099}
100
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700101type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700102 android.ModuleBase
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800103 android.DefaultableModuleBase
Jiyong Parkfc752ca2019-06-12 13:27:29 +0900104 android.ApexModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -0700105
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700106 // For other packages to make their own genrules with extra
107 // properties
108 Extra interface{}
109
Colin Cross7d5136f2015-05-11 13:39:40 -0700110 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700111
Jeff Gaston437d23c2017-11-08 12:38:00 -0800112 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700113
Colin Cross2a076922018-10-04 23:28:25 -0700114 deps android.Paths
115 rule blueprint.Rule
116 rawCommand string
Colin Crossd350ecd2015-04-28 13:25:36 -0700117
Colin Cross5ed99c62016-11-22 12:55:55 -0800118 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700119
Colin Cross635c3b02016-05-18 15:37:25 -0700120 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800121 outputDeps android.Paths
Colin Crossa4ad2b02019-03-18 22:15:32 -0700122
123 subName string
Colin Crossd350ecd2015-04-28 13:25:36 -0700124}
125
Jeff Gaston437d23c2017-11-08 12:38:00 -0800126type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700127
128type generateTask struct {
Colin Crossbaccf5b2018-02-21 14:07:48 -0800129 in android.Paths
130 out android.WritablePaths
131 sandboxOuts []string
132 cmd string
Colin Crossd350ecd2015-04-28 13:25:36 -0700133}
134
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700135func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700136 return g.outputFiles
137}
138
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700139func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700140 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800141}
142
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700143func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800144 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700145}
146
Dan Willemsen9da9d492018-02-21 18:28:18 -0800147func (g *Module) GeneratedDeps() android.Paths {
148 return g.outputDeps
149}
150
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700151func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700152 if g, ok := ctx.Module().(*Module); ok {
Colin Cross08f15ab2018-10-04 23:29:14 -0700153 for _, tool := range g.properties.Tools {
154 tag := hostToolDependencyTag{label: tool}
155 if m := android.SrcIsModule(tool); m != "" {
156 tool = m
157 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800158 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700159 {Mutator: "arch", Variation: ctx.Config().BuildOsVariant},
Colin Cross08f15ab2018-10-04 23:29:14 -0700160 }, tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700161 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700162 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700163}
164
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700165func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa4ad2b02019-03-18 22:15:32 -0700166 g.subName = ctx.ModuleSubDir()
167
Colin Cross5ed99c62016-11-22 12:55:55 -0800168 if len(g.properties.Export_include_dirs) > 0 {
169 for _, dir := range g.properties.Export_include_dirs {
170 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
171 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
172 }
173 } else {
174 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
175 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700176
Colin Cross08f15ab2018-10-04 23:29:14 -0700177 locationLabels := map[string][]string{}
178 firstLabel := ""
179
180 addLocationLabel := func(label string, paths []string) {
181 if firstLabel == "" {
182 firstLabel = label
183 }
184 if _, exists := locationLabels[label]; !exists {
185 locationLabels[label] = paths
186 } else {
187 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
188 label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
189 }
190 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700191
Colin Cross6f080df2016-11-04 15:32:58 -0700192 if len(g.properties.Tools) > 0 {
Colin Crossba71a3f2019-03-18 12:12:48 -0700193 seenTools := make(map[string]bool)
194
Colin Cross35143d02017-11-16 00:11:20 -0800195 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700196 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
197 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700198 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700199 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700200
Colin Crossfe17f6f2019-03-28 19:30:56 -0700201 if t, ok := module.(android.HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800202 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800203 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800204 ctx.AddMissingDependencies([]string{tool})
205 } else {
206 ctx.ModuleErrorf("depends on disabled module %q", tool)
207 }
208 break
209 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700210 path = t.HostToolPath()
211 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
212 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
213 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700214 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700215 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
216 break
Colin Cross6f080df2016-11-04 15:32:58 -0700217 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700218 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700219 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700220 break
221 }
222
223 if path.Valid() {
224 g.deps = append(g.deps, path.Path())
Colin Cross08f15ab2018-10-04 23:29:14 -0700225 addLocationLabel(tag.label, []string{path.Path().String()})
Colin Crossba71a3f2019-03-18 12:12:48 -0700226 seenTools[tag.label] = true
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700227 } else {
228 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700229 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700230 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700231 })
Colin Crossba71a3f2019-03-18 12:12:48 -0700232
233 // If AllowMissingDependencies is enabled, the build will not have stopped when
234 // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical
235 // "cmd: unknown location label ..." errors later. Add a dummy file to the local label. The
236 // command that uses this dummy file will never be executed because the rule will be replaced with
237 // an android.Error rule reporting the missing dependencies.
238 if ctx.Config().AllowMissingDependencies() {
239 for _, tool := range g.properties.Tools {
240 if !seenTools[tool] {
241 addLocationLabel(tool, []string{"***missing tool " + tool + "***"})
242 }
243 }
244 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700245 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700246
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700247 if ctx.Failed() {
248 return
249 }
250
Colin Cross08f15ab2018-10-04 23:29:14 -0700251 for _, toolFile := range g.properties.Tool_files {
Colin Cross8a497952019-03-05 22:25:09 -0800252 paths := android.PathsForModuleSrc(ctx, []string{toolFile})
Colin Cross08f15ab2018-10-04 23:29:14 -0700253 g.deps = append(g.deps, paths...)
254 addLocationLabel(toolFile, paths.Strings())
255 }
256
257 var srcFiles android.Paths
258 for _, in := range g.properties.Srcs {
Colin Crossba71a3f2019-03-18 12:12:48 -0700259 paths, missingDeps := android.PathsAndMissingDepsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs)
260 if len(missingDeps) > 0 {
261 if !ctx.Config().AllowMissingDependencies() {
262 panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator",
263 missingDeps))
264 }
265
266 // If AllowMissingDependencies is enabled, the build will not have stopped when
267 // the dependency was added on a missing SourceFileProducer module, which will result in nonsensical
268 // "cmd: label ":..." has no files" errors later. Add a dummy file to the local label. The
269 // command that uses this dummy file will never be executed because the rule will be replaced with
270 // an android.Error rule reporting the missing dependencies.
271 ctx.AddMissingDependencies(missingDeps)
272 addLocationLabel(in, []string{"***missing srcs " + in + "***"})
273 } else {
274 srcFiles = append(srcFiles, paths...)
275 addLocationLabel(in, paths.Strings())
276 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700277 }
278
279 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
280
281 for _, out := range task.out {
282 addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
Colin Cross6f080df2016-11-04 15:32:58 -0700283 }
284
Jeff Gaston02a684b2017-10-27 14:59:27 -0700285 referencedDepfile := false
286
Jeff Gaston437d23c2017-11-08 12:38:00 -0800287 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross85a2e892018-07-09 09:45:06 -0700288 // report the error directly without returning an error to android.Expand to catch multiple errors in a
289 // single run
290 reportError := func(fmt string, args ...interface{}) (string, error) {
291 ctx.PropertyErrorf("cmd", fmt, args...)
292 return "SOONG_ERROR", nil
293 }
294
Colin Cross6f080df2016-11-04 15:32:58 -0700295 switch name {
296 case "location":
Colin Cross08f15ab2018-10-04 23:29:14 -0700297 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
Colin Cross85a2e892018-07-09 09:45:06 -0700298 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700299 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700300 paths := locationLabels[firstLabel]
301 if len(paths) == 0 {
302 return reportError("default label %q has no files", firstLabel)
303 } else if len(paths) > 1 {
304 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
305 firstLabel, firstLabel)
306 }
307 return locationLabels[firstLabel][0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700308 case "in":
309 return "${in}", nil
310 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700311 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800312 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700313 referencedDepfile = true
Nan Zhangea568a42017-11-08 21:20:04 -0800314 if !Bool(g.properties.Depfile) {
Colin Cross85a2e892018-07-09 09:45:06 -0700315 return reportError("$(depfile) used without depfile property")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800316 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700317 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700318 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800319 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700320 default:
321 if strings.HasPrefix(name, "location ") {
322 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Colin Cross08f15ab2018-10-04 23:29:14 -0700323 if paths, ok := locationLabels[label]; ok {
324 if len(paths) == 0 {
325 return reportError("label %q has no files", label)
326 } else if len(paths) > 1 {
327 return reportError("label %q has multiple files, use $(locations %s) to reference it",
328 label, label)
329 }
330 return paths[0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700331 } else {
Colin Cross85a2e892018-07-09 09:45:06 -0700332 return reportError("unknown location label %q", label)
Colin Cross6f080df2016-11-04 15:32:58 -0700333 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700334 } else if strings.HasPrefix(name, "locations ") {
335 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
336 if paths, ok := locationLabels[label]; ok {
337 if len(paths) == 0 {
338 return reportError("label %q has no files", label)
339 }
340 return strings.Join(paths, " "), nil
341 } else {
342 return reportError("unknown locations label %q", label)
343 }
344 } else {
345 return reportError("unknown variable '$(%s)'", name)
Colin Cross6f080df2016-11-04 15:32:58 -0700346 }
Colin Cross6f080df2016-11-04 15:32:58 -0700347 }
348 })
349
350 if err != nil {
351 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700352 return
Colin Cross6f080df2016-11-04 15:32:58 -0700353 }
354
Colin Cross85a2e892018-07-09 09:45:06 -0700355 if Bool(g.properties.Depfile) && !referencedDepfile {
356 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
357 }
358
Jeff Gastonefc1b412017-03-29 17:29:06 -0700359 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700360 buildDir := android.PathForOutput(ctx).String()
361 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700362
363 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
364 // to be replaced later by ninja_strings.go
Jeff Gaston02a684b2017-10-27 14:59:27 -0700365 depfilePlaceholder := ""
Nan Zhangea568a42017-11-08 21:20:04 -0800366 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700367 depfilePlaceholder = "$depfileArgs"
368 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800369
Jeff Gaston437d23c2017-11-08 12:38:00 -0800370 genDir := android.PathForModuleGen(ctx)
Jeff Gaston31603062017-12-08 12:45:35 -0800371 // Escape the command for the shell
372 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
Colin Cross2a076922018-10-04 23:28:25 -0700373 g.rawCommand = rawCommand
Jeff Gaston31603062017-12-08 12:45:35 -0800374 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
375 sandboxPath, genDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700376
Colin Cross33bfb0a2016-11-21 17:23:08 -0800377 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700378 Command: sandboxCommand,
379 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800380 }
Colin Cross15e86d92017-10-20 15:07:08 -0700381 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800382 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800383 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700384 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800385 }
386 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700387
Jeff Gaston437d23c2017-11-08 12:38:00 -0800388 g.generateSourceFile(ctx, task)
389
Colin Crossd350ecd2015-04-28 13:25:36 -0700390}
391
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700392func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700393 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700394 if len(task.out) == 0 {
395 ctx.ModuleErrorf("must have at least one output file")
396 return
397 }
Colin Cross67a5c132017-05-09 13:45:28 -0700398 if len(task.out) == 1 {
399 desc += " " + task.out[0].Base()
400 }
401
Jeff Gaston02a684b2017-10-27 14:59:27 -0700402 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800403 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700404 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
405 }
406
Colin Crossae887032017-10-23 17:16:14 -0700407 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700408 Rule: g.rule,
409 Description: "generate",
410 Output: task.out[0],
411 ImplicitOutputs: task.out[1:],
412 Inputs: task.in,
413 Implicits: g.deps,
414 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800415 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700416 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800417 }
Nan Zhangea568a42017-11-08 21:20:04 -0800418 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700419 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
420 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800421 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700422
Colin Crossae887032017-10-23 17:16:14 -0700423 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700424
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700425 for _, outputFile := range task.out {
426 g.outputFiles = append(g.outputFiles, outputFile)
427 }
Dan Willemsen9da9d492018-02-21 18:28:18 -0800428 g.outputDeps = append(g.outputDeps, task.out[0])
Colin Crossd350ecd2015-04-28 13:25:36 -0700429}
430
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700431// Collect information for opening IDE project files in java/jdeps.go.
432func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
433 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
434 for _, src := range g.properties.Srcs {
435 if strings.HasPrefix(src, ":") {
436 src = strings.Trim(src, ":")
437 dpInfo.Deps = append(dpInfo.Deps, src)
438 }
439 }
440}
441
Colin Crossa4ad2b02019-03-18 22:15:32 -0700442func (g *Module) AndroidMk() android.AndroidMkData {
443 return android.AndroidMkData{
444 Include: "$(BUILD_PHONY_PACKAGE)",
445 Class: "FAKE",
446 OutputFile: android.OptionalPathForPath(g.outputFiles[0]),
447 SubName: g.subName,
448 Extra: []android.AndroidMkExtraFunc{
449 func(w io.Writer, outputFile android.Path) {
450 fmt.Fprintln(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=", strings.Join(g.outputFiles.Strings(), " "))
451 },
452 },
453 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
454 android.WriteAndroidMkData(w, data)
455 if data.SubName != "" {
456 fmt.Fprintln(w, ".PHONY:", name)
457 fmt.Fprintln(w, name, ":", name+g.subName)
458 }
459 },
460 }
461}
462
Jeff Gaston437d23c2017-11-08 12:38:00 -0800463func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700464 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800465 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700466 }
467
Colin Cross36242852017-06-23 15:06:31 -0700468 module.AddProperties(props...)
469 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700470
Colin Cross36242852017-06-23 15:06:31 -0700471 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700472}
473
Colin Crossbaccf5b2018-02-21 14:07:48 -0800474// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
475func pathToSandboxOut(path android.Path, genDir android.Path) string {
476 relOut, err := filepath.Rel(genDir.String(), path.String())
477 if err != nil {
478 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
479 }
480 return filepath.Join("__SBOX_OUT_DIR__", relOut)
481
482}
483
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700484func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700485 properties := &genSrcsProperties{}
486
Jeff Gaston437d23c2017-11-08 12:38:00 -0800487 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
488 commands := []string{}
489 outFiles := android.WritablePaths{}
Colin Crossbaccf5b2018-02-21 14:07:48 -0800490 genDir := android.PathForModuleGen(ctx)
491 sandboxOuts := []string{}
Colin Crossd350ecd2015-04-28 13:25:36 -0700492 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800493 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800494 outFiles = append(outFiles, outFile)
495
Colin Crossbaccf5b2018-02-21 14:07:48 -0800496 sandboxOutfile := pathToSandboxOut(outFile, genDir)
497 sandboxOuts = append(sandboxOuts, sandboxOutfile)
498
Jeff Gaston437d23c2017-11-08 12:38:00 -0800499 command, err := android.Expand(rawCommand, func(name string) (string, error) {
500 switch name {
501 case "in":
502 return in.String(), nil
503 case "out":
504 return sandboxOutfile, nil
505 default:
506 return "$(" + name + ")", nil
507 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700508 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800509 if err != nil {
510 ctx.PropertyErrorf("cmd", err.Error())
511 }
512
513 // escape the command in case for example it contains '#', an odd number of '"', etc
Colin Cross0b9f31f2019-02-28 11:00:01 -0800514 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800515 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700516 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800517 fullCommand := strings.Join(commands, " && ")
518
519 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800520 in: srcFiles,
521 out: outFiles,
522 sandboxOuts: sandboxOuts,
523 cmd: fullCommand,
Jeff Gaston437d23c2017-11-08 12:38:00 -0800524 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700525 }
526
Jeff Gaston437d23c2017-11-08 12:38:00 -0800527 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700528}
529
Colin Cross54190b32017-10-09 15:34:10 -0700530func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700531 m := NewGenSrcs()
532 android.InitAndroidModule(m)
533 return m
534}
535
Colin Crossd350ecd2015-04-28 13:25:36 -0700536type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700537 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800538 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700539}
540
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700541func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700542 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700543
Jeff Gaston437d23c2017-11-08 12:38:00 -0800544 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700545 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800546 sandboxOuts := make([]string, len(properties.Out))
547 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700548 for i, out := range properties.Out {
549 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800550 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700551 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800552 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800553 in: srcFiles,
554 out: outs,
555 sandboxOuts: sandboxOuts,
556 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700557 }
Colin Cross5049f022015-03-18 13:28:46 -0700558 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700559
Jeff Gaston437d23c2017-11-08 12:38:00 -0800560 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700561}
562
Colin Cross54190b32017-10-09 15:34:10 -0700563func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700564 m := NewGenRule()
565 android.InitAndroidModule(m)
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800566 android.InitDefaultableModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700567 return m
568}
569
Colin Crossd350ecd2015-04-28 13:25:36 -0700570type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700571 // names of the output files that will be generated
Colin Crossef354482018-10-23 11:27:50 -0700572 Out []string `android:"arch_variant"`
Colin Cross5049f022015-03-18 13:28:46 -0700573}
Nan Zhangea568a42017-11-08 21:20:04 -0800574
575var Bool = proptools.Bool
576var String = proptools.String
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800577
578//
579// Defaults
580//
581type Defaults struct {
582 android.ModuleBase
583 android.DefaultsModuleBase
584}
585
586func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
587}
588
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800589func defaultsFactory() android.Module {
590 return DefaultsFactory()
591}
592
593func DefaultsFactory(props ...interface{}) android.Module {
594 module := &Defaults{}
595
596 module.AddProperties(props...)
597 module.AddProperties(
598 &generatorProperties{},
599 &genRuleProperties{},
600 )
601
602 android.InitDefaultsModule(module)
603
604 return module
605}