blob: 32acd8c8d561cdbc3154a56439594370b6b2bd01 [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 Crossd350ecd2015-04-28 13:25:36 -070052type HostToolProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -070053 HostToolPath() android.OptionalPath
Colin Crossd350ecd2015-04-28 13:25:36 -070054}
Colin Cross5049f022015-03-18 13:28:46 -070055
Dan Willemsend6ba0d52017-09-13 15:46:47 -070056type hostToolDependencyTag struct {
57 blueprint.BaseDependencyTag
Colin Cross08f15ab2018-10-04 23:29:14 -070058 label string
Dan Willemsend6ba0d52017-09-13 15:46:47 -070059}
60
Colin Cross7d5136f2015-05-11 13:39:40 -070061type generatorProperties struct {
Jeff Gastonefc1b412017-03-29 17:29:06 -070062 // The command to run on one or more input files. Cmd supports substitution of a few variables
63 // (the actual substitution is implemented in GenerateAndroidBuildActions below)
64 //
65 // Available variables for substitution:
66 //
Colin Cross2296f5b2017-10-17 21:38:14 -070067 // $(location): the path to the first entry in tools or tool_files
Colin Cross08f15ab2018-10-04 23:29:14 -070068 // $(location <label>): the path to the tool, tool_file, input or output with name <label>
Colin Cross2296f5b2017-10-17 21:38:14 -070069 // $(in): one or more input files
70 // $(out): a single output file
71 // $(depfile): a file to which dependencies will be written, if the depfile property is set to true
72 // $(genDir): the sandbox directory for this tool; contains $(out)
73 // $$: a literal $
Colin Cross6f080df2016-11-04 15:32:58 -070074 //
Jeff Gastonefc1b412017-03-29 17:29:06 -070075 // All files used must be declared as inputs (to ensure proper up-to-date checks).
76 // Use "$(in)" directly in Cmd to ensure that all inputs used are declared.
Nan Zhangea568a42017-11-08 21:20:04 -080077 Cmd *string
Colin Cross7d5136f2015-05-11 13:39:40 -070078
Colin Cross33bfb0a2016-11-21 17:23:08 -080079 // Enable reading a file containing dependencies in gcc format after the command completes
Nan Zhangea568a42017-11-08 21:20:04 -080080 Depfile *bool
Colin Cross33bfb0a2016-11-21 17:23:08 -080081
Colin Cross6f080df2016-11-04 15:32:58 -070082 // name of the modules (if any) that produces the host executable. Leave empty for
Colin Cross7d5136f2015-05-11 13:39:40 -070083 // prebuilts or scripts that do not need a module to build them.
Colin Cross6f080df2016-11-04 15:32:58 -070084 Tools []string
Dan Willemsenf7f3d692016-04-20 14:54:32 -070085
86 // Local file that is used as the tool
Colin Cross27b922f2019-03-04 22:35:41 -080087 Tool_files []string `android:"path"`
Colin Cross5ed99c62016-11-22 12:55:55 -080088
89 // List of directories to export generated headers from
90 Export_include_dirs []string
Colin Cross708c4242017-01-13 18:05:49 -080091
92 // list of input files
Colin Cross27b922f2019-03-04 22:35:41 -080093 Srcs []string `android:"path,arch_variant"`
Dan Willemseneefa0262018-11-17 14:01:18 -080094
95 // input files to exclude
Colin Cross27b922f2019-03-04 22:35:41 -080096 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070097}
98
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070099type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700100 android.ModuleBase
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800101 android.DefaultableModuleBase
Colin Crossd350ecd2015-04-28 13:25:36 -0700102
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700103 // For other packages to make their own genrules with extra
104 // properties
105 Extra interface{}
106
Colin Cross7d5136f2015-05-11 13:39:40 -0700107 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700108
Jeff Gaston437d23c2017-11-08 12:38:00 -0800109 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700110
Colin Cross2a076922018-10-04 23:28:25 -0700111 deps android.Paths
112 rule blueprint.Rule
113 rawCommand string
Colin Crossd350ecd2015-04-28 13:25:36 -0700114
Colin Cross5ed99c62016-11-22 12:55:55 -0800115 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700116
Colin Cross635c3b02016-05-18 15:37:25 -0700117 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800118 outputDeps android.Paths
Colin Crossa4ad2b02019-03-18 22:15:32 -0700119
120 subName string
Colin Crossd350ecd2015-04-28 13:25:36 -0700121}
122
Jeff Gaston437d23c2017-11-08 12:38:00 -0800123type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700124
125type generateTask struct {
Colin Crossbaccf5b2018-02-21 14:07:48 -0800126 in android.Paths
127 out android.WritablePaths
128 sandboxOuts []string
129 cmd string
Colin Crossd350ecd2015-04-28 13:25:36 -0700130}
131
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700132func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700133 return g.outputFiles
134}
135
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700136func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700137 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800138}
139
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700140func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800141 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700142}
143
Dan Willemsen9da9d492018-02-21 18:28:18 -0800144func (g *Module) GeneratedDeps() android.Paths {
145 return g.outputDeps
146}
147
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700148func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700149 if g, ok := ctx.Module().(*Module); ok {
Colin Cross08f15ab2018-10-04 23:29:14 -0700150 for _, tool := range g.properties.Tools {
151 tag := hostToolDependencyTag{label: tool}
152 if m := android.SrcIsModule(tool); m != "" {
153 tool = m
154 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800155 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700156 {Mutator: "arch", Variation: ctx.Config().BuildOsVariant},
Colin Cross08f15ab2018-10-04 23:29:14 -0700157 }, tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700158 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700159 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700160}
161
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700162func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa4ad2b02019-03-18 22:15:32 -0700163 g.subName = ctx.ModuleSubDir()
164
Colin Cross5ed99c62016-11-22 12:55:55 -0800165 if len(g.properties.Export_include_dirs) > 0 {
166 for _, dir := range g.properties.Export_include_dirs {
167 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
168 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
169 }
170 } else {
171 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
172 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700173
Colin Cross08f15ab2018-10-04 23:29:14 -0700174 locationLabels := map[string][]string{}
175 firstLabel := ""
176
177 addLocationLabel := func(label string, paths []string) {
178 if firstLabel == "" {
179 firstLabel = label
180 }
181 if _, exists := locationLabels[label]; !exists {
182 locationLabels[label] = paths
183 } else {
184 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
185 label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
186 }
187 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700188
Colin Cross6f080df2016-11-04 15:32:58 -0700189 if len(g.properties.Tools) > 0 {
Colin Cross35143d02017-11-16 00:11:20 -0800190 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700191 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
192 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700193 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700194 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700195
196 if t, ok := module.(HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800197 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800198 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800199 ctx.AddMissingDependencies([]string{tool})
200 } else {
201 ctx.ModuleErrorf("depends on disabled module %q", tool)
202 }
203 break
204 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700205 path = t.HostToolPath()
206 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
207 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
208 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700209 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700210 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
211 break
Colin Cross6f080df2016-11-04 15:32:58 -0700212 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700213 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700214 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700215 break
216 }
217
218 if path.Valid() {
219 g.deps = append(g.deps, path.Path())
Colin Cross08f15ab2018-10-04 23:29:14 -0700220 addLocationLabel(tag.label, []string{path.Path().String()})
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700221 } else {
222 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700223 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700224 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700225 })
226 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700227
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700228 if ctx.Failed() {
229 return
230 }
231
Colin Cross08f15ab2018-10-04 23:29:14 -0700232 for _, toolFile := range g.properties.Tool_files {
Colin Cross8a497952019-03-05 22:25:09 -0800233 paths := android.PathsForModuleSrc(ctx, []string{toolFile})
Colin Cross08f15ab2018-10-04 23:29:14 -0700234 g.deps = append(g.deps, paths...)
235 addLocationLabel(toolFile, paths.Strings())
236 }
237
238 var srcFiles android.Paths
239 for _, in := range g.properties.Srcs {
Colin Cross8a497952019-03-05 22:25:09 -0800240 paths := android.PathsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs)
Colin Cross08f15ab2018-10-04 23:29:14 -0700241 srcFiles = append(srcFiles, paths...)
242 addLocationLabel(in, paths.Strings())
243 }
244
245 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
246
247 for _, out := range task.out {
248 addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
Colin Cross6f080df2016-11-04 15:32:58 -0700249 }
250
Jeff Gaston02a684b2017-10-27 14:59:27 -0700251 referencedDepfile := false
252
Jeff Gaston437d23c2017-11-08 12:38:00 -0800253 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross85a2e892018-07-09 09:45:06 -0700254 // report the error directly without returning an error to android.Expand to catch multiple errors in a
255 // single run
256 reportError := func(fmt string, args ...interface{}) (string, error) {
257 ctx.PropertyErrorf("cmd", fmt, args...)
258 return "SOONG_ERROR", nil
259 }
260
Colin Cross6f080df2016-11-04 15:32:58 -0700261 switch name {
262 case "location":
Colin Cross08f15ab2018-10-04 23:29:14 -0700263 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
Colin Cross85a2e892018-07-09 09:45:06 -0700264 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700265 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700266 paths := locationLabels[firstLabel]
267 if len(paths) == 0 {
268 return reportError("default label %q has no files", firstLabel)
269 } else if len(paths) > 1 {
270 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
271 firstLabel, firstLabel)
272 }
273 return locationLabels[firstLabel][0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700274 case "in":
275 return "${in}", nil
276 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700277 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800278 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700279 referencedDepfile = true
Nan Zhangea568a42017-11-08 21:20:04 -0800280 if !Bool(g.properties.Depfile) {
Colin Cross85a2e892018-07-09 09:45:06 -0700281 return reportError("$(depfile) used without depfile property")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800282 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700283 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700284 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800285 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700286 default:
287 if strings.HasPrefix(name, "location ") {
288 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Colin Cross08f15ab2018-10-04 23:29:14 -0700289 if paths, ok := locationLabels[label]; ok {
290 if len(paths) == 0 {
291 return reportError("label %q has no files", label)
292 } else if len(paths) > 1 {
293 return reportError("label %q has multiple files, use $(locations %s) to reference it",
294 label, label)
295 }
296 return paths[0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700297 } else {
Colin Cross85a2e892018-07-09 09:45:06 -0700298 return reportError("unknown location label %q", label)
Colin Cross6f080df2016-11-04 15:32:58 -0700299 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700300 } else if strings.HasPrefix(name, "locations ") {
301 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
302 if paths, ok := locationLabels[label]; ok {
303 if len(paths) == 0 {
304 return reportError("label %q has no files", label)
305 }
306 return strings.Join(paths, " "), nil
307 } else {
308 return reportError("unknown locations label %q", label)
309 }
310 } else {
311 return reportError("unknown variable '$(%s)'", name)
Colin Cross6f080df2016-11-04 15:32:58 -0700312 }
Colin Cross6f080df2016-11-04 15:32:58 -0700313 }
314 })
315
316 if err != nil {
317 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700318 return
Colin Cross6f080df2016-11-04 15:32:58 -0700319 }
320
Colin Cross85a2e892018-07-09 09:45:06 -0700321 if Bool(g.properties.Depfile) && !referencedDepfile {
322 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
323 }
324
Jeff Gastonefc1b412017-03-29 17:29:06 -0700325 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700326 buildDir := android.PathForOutput(ctx).String()
327 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700328
329 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
330 // to be replaced later by ninja_strings.go
Jeff Gaston02a684b2017-10-27 14:59:27 -0700331 depfilePlaceholder := ""
Nan Zhangea568a42017-11-08 21:20:04 -0800332 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700333 depfilePlaceholder = "$depfileArgs"
334 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800335
Jeff Gaston437d23c2017-11-08 12:38:00 -0800336 genDir := android.PathForModuleGen(ctx)
Jeff Gaston31603062017-12-08 12:45:35 -0800337 // Escape the command for the shell
338 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
Colin Cross2a076922018-10-04 23:28:25 -0700339 g.rawCommand = rawCommand
Jeff Gaston31603062017-12-08 12:45:35 -0800340 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
341 sandboxPath, genDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700342
Colin Cross33bfb0a2016-11-21 17:23:08 -0800343 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700344 Command: sandboxCommand,
345 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800346 }
Colin Cross15e86d92017-10-20 15:07:08 -0700347 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800348 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800349 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700350 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800351 }
352 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700353
Jeff Gaston437d23c2017-11-08 12:38:00 -0800354 g.generateSourceFile(ctx, task)
355
Colin Crossd350ecd2015-04-28 13:25:36 -0700356}
357
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700358func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700359 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700360 if len(task.out) == 0 {
361 ctx.ModuleErrorf("must have at least one output file")
362 return
363 }
Colin Cross67a5c132017-05-09 13:45:28 -0700364 if len(task.out) == 1 {
365 desc += " " + task.out[0].Base()
366 }
367
Jeff Gaston02a684b2017-10-27 14:59:27 -0700368 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800369 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700370 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
371 }
372
Colin Crossae887032017-10-23 17:16:14 -0700373 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700374 Rule: g.rule,
375 Description: "generate",
376 Output: task.out[0],
377 ImplicitOutputs: task.out[1:],
378 Inputs: task.in,
379 Implicits: g.deps,
380 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800381 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700382 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800383 }
Nan Zhangea568a42017-11-08 21:20:04 -0800384 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700385 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
386 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800387 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700388
Colin Crossae887032017-10-23 17:16:14 -0700389 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700390
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700391 for _, outputFile := range task.out {
392 g.outputFiles = append(g.outputFiles, outputFile)
393 }
Dan Willemsen9da9d492018-02-21 18:28:18 -0800394 g.outputDeps = append(g.outputDeps, task.out[0])
Colin Crossd350ecd2015-04-28 13:25:36 -0700395}
396
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700397// Collect information for opening IDE project files in java/jdeps.go.
398func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
399 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
400 for _, src := range g.properties.Srcs {
401 if strings.HasPrefix(src, ":") {
402 src = strings.Trim(src, ":")
403 dpInfo.Deps = append(dpInfo.Deps, src)
404 }
405 }
406}
407
Colin Crossa4ad2b02019-03-18 22:15:32 -0700408func (g *Module) AndroidMk() android.AndroidMkData {
409 return android.AndroidMkData{
410 Include: "$(BUILD_PHONY_PACKAGE)",
411 Class: "FAKE",
412 OutputFile: android.OptionalPathForPath(g.outputFiles[0]),
413 SubName: g.subName,
414 Extra: []android.AndroidMkExtraFunc{
415 func(w io.Writer, outputFile android.Path) {
416 fmt.Fprintln(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=", strings.Join(g.outputFiles.Strings(), " "))
417 },
418 },
419 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
420 android.WriteAndroidMkData(w, data)
421 if data.SubName != "" {
422 fmt.Fprintln(w, ".PHONY:", name)
423 fmt.Fprintln(w, name, ":", name+g.subName)
424 }
425 },
426 }
427}
428
Jeff Gaston437d23c2017-11-08 12:38:00 -0800429func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700430 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800431 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700432 }
433
Colin Cross36242852017-06-23 15:06:31 -0700434 module.AddProperties(props...)
435 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700436
Colin Cross36242852017-06-23 15:06:31 -0700437 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700438}
439
Colin Crossbaccf5b2018-02-21 14:07:48 -0800440// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
441func pathToSandboxOut(path android.Path, genDir android.Path) string {
442 relOut, err := filepath.Rel(genDir.String(), path.String())
443 if err != nil {
444 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
445 }
446 return filepath.Join("__SBOX_OUT_DIR__", relOut)
447
448}
449
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700450func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700451 properties := &genSrcsProperties{}
452
Jeff Gaston437d23c2017-11-08 12:38:00 -0800453 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
454 commands := []string{}
455 outFiles := android.WritablePaths{}
Colin Crossbaccf5b2018-02-21 14:07:48 -0800456 genDir := android.PathForModuleGen(ctx)
457 sandboxOuts := []string{}
Colin Crossd350ecd2015-04-28 13:25:36 -0700458 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800459 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800460 outFiles = append(outFiles, outFile)
461
Colin Crossbaccf5b2018-02-21 14:07:48 -0800462 sandboxOutfile := pathToSandboxOut(outFile, genDir)
463 sandboxOuts = append(sandboxOuts, sandboxOutfile)
464
Jeff Gaston437d23c2017-11-08 12:38:00 -0800465 command, err := android.Expand(rawCommand, func(name string) (string, error) {
466 switch name {
467 case "in":
468 return in.String(), nil
469 case "out":
470 return sandboxOutfile, nil
471 default:
472 return "$(" + name + ")", nil
473 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700474 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800475 if err != nil {
476 ctx.PropertyErrorf("cmd", err.Error())
477 }
478
479 // escape the command in case for example it contains '#', an odd number of '"', etc
Colin Cross0b9f31f2019-02-28 11:00:01 -0800480 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800481 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700482 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800483 fullCommand := strings.Join(commands, " && ")
484
485 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800486 in: srcFiles,
487 out: outFiles,
488 sandboxOuts: sandboxOuts,
489 cmd: fullCommand,
Jeff Gaston437d23c2017-11-08 12:38:00 -0800490 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700491 }
492
Jeff Gaston437d23c2017-11-08 12:38:00 -0800493 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700494}
495
Colin Cross54190b32017-10-09 15:34:10 -0700496func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700497 m := NewGenSrcs()
498 android.InitAndroidModule(m)
499 return m
500}
501
Colin Crossd350ecd2015-04-28 13:25:36 -0700502type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700503 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800504 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700505}
506
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700507func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700508 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700509
Jeff Gaston437d23c2017-11-08 12:38:00 -0800510 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700511 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800512 sandboxOuts := make([]string, len(properties.Out))
513 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700514 for i, out := range properties.Out {
515 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800516 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700517 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800518 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800519 in: srcFiles,
520 out: outs,
521 sandboxOuts: sandboxOuts,
522 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700523 }
Colin Cross5049f022015-03-18 13:28:46 -0700524 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700525
Jeff Gaston437d23c2017-11-08 12:38:00 -0800526 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700527}
528
Colin Cross54190b32017-10-09 15:34:10 -0700529func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700530 m := NewGenRule()
531 android.InitAndroidModule(m)
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800532 android.InitDefaultableModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700533 return m
534}
535
Colin Crossd350ecd2015-04-28 13:25:36 -0700536type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700537 // names of the output files that will be generated
Colin Crossef354482018-10-23 11:27:50 -0700538 Out []string `android:"arch_variant"`
Colin Cross5049f022015-03-18 13:28:46 -0700539}
Nan Zhangea568a42017-11-08 21:20:04 -0800540
541var Bool = proptools.Bool
542var String = proptools.String
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800543
544//
545// Defaults
546//
547type Defaults struct {
548 android.ModuleBase
549 android.DefaultsModuleBase
550}
551
552func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
553}
554
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800555func defaultsFactory() android.Module {
556 return DefaultsFactory()
557}
558
559func DefaultsFactory(props ...interface{}) android.Module {
560 module := &Defaults{}
561
562 module.AddProperties(props...)
563 module.AddProperties(
564 &generatorProperties{},
565 &genRuleProperties{},
566 )
567
568 android.InitDefaultsModule(module)
569
570 return module
571}