blob: e259b1d94fa7c915bea75a40f8ffbe8e9f78c345 [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
Colin Crossd350ecd2015-04-28 13:25:36 -0700104
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700105 // For other packages to make their own genrules with extra
106 // properties
107 Extra interface{}
108
Colin Cross7d5136f2015-05-11 13:39:40 -0700109 properties generatorProperties
Colin Crossd350ecd2015-04-28 13:25:36 -0700110
Jeff Gaston437d23c2017-11-08 12:38:00 -0800111 taskGenerator taskFunc
Colin Crossd350ecd2015-04-28 13:25:36 -0700112
Colin Cross2a076922018-10-04 23:28:25 -0700113 deps android.Paths
114 rule blueprint.Rule
115 rawCommand string
Colin Crossd350ecd2015-04-28 13:25:36 -0700116
Colin Cross5ed99c62016-11-22 12:55:55 -0800117 exportedIncludeDirs android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700118
Colin Cross635c3b02016-05-18 15:37:25 -0700119 outputFiles android.Paths
Dan Willemsen9da9d492018-02-21 18:28:18 -0800120 outputDeps android.Paths
Colin Crossa4ad2b02019-03-18 22:15:32 -0700121
122 subName string
Colin Crossd350ecd2015-04-28 13:25:36 -0700123}
124
Jeff Gaston437d23c2017-11-08 12:38:00 -0800125type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask
Colin Crossd350ecd2015-04-28 13:25:36 -0700126
127type generateTask struct {
Colin Crossbaccf5b2018-02-21 14:07:48 -0800128 in android.Paths
129 out android.WritablePaths
130 sandboxOuts []string
131 cmd string
Colin Crossd350ecd2015-04-28 13:25:36 -0700132}
133
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700134func (g *Module) GeneratedSourceFiles() android.Paths {
Colin Crossd350ecd2015-04-28 13:25:36 -0700135 return g.outputFiles
136}
137
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700138func (g *Module) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700139 return append(android.Paths{}, g.outputFiles...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800140}
141
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700142func (g *Module) GeneratedHeaderDirs() android.Paths {
Colin Cross5ed99c62016-11-22 12:55:55 -0800143 return g.exportedIncludeDirs
Dan Willemsenb40aab62016-04-20 14:21:14 -0700144}
145
Dan Willemsen9da9d492018-02-21 18:28:18 -0800146func (g *Module) GeneratedDeps() android.Paths {
147 return g.outputDeps
148}
149
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700150func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700151 if g, ok := ctx.Module().(*Module); ok {
Colin Cross08f15ab2018-10-04 23:29:14 -0700152 for _, tool := range g.properties.Tools {
153 tag := hostToolDependencyTag{label: tool}
154 if m := android.SrcIsModule(tool); m != "" {
155 tool = m
156 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800157 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700158 {Mutator: "arch", Variation: ctx.Config().BuildOsVariant},
Colin Cross08f15ab2018-10-04 23:29:14 -0700159 }, tag, tool)
Colin Cross6362e272015-10-29 15:25:03 -0700160 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700161 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700162}
163
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700164func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa4ad2b02019-03-18 22:15:32 -0700165 g.subName = ctx.ModuleSubDir()
166
Colin Cross5ed99c62016-11-22 12:55:55 -0800167 if len(g.properties.Export_include_dirs) > 0 {
168 for _, dir := range g.properties.Export_include_dirs {
169 g.exportedIncludeDirs = append(g.exportedIncludeDirs,
170 android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
171 }
172 } else {
173 g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
174 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700175
Colin Cross08f15ab2018-10-04 23:29:14 -0700176 locationLabels := map[string][]string{}
177 firstLabel := ""
178
179 addLocationLabel := func(label string, paths []string) {
180 if firstLabel == "" {
181 firstLabel = label
182 }
183 if _, exists := locationLabels[label]; !exists {
184 locationLabels[label] = paths
185 } else {
186 ctx.ModuleErrorf("multiple labels for %q, %q and %q",
187 label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
188 }
189 }
Dan Willemsen3f4539b2016-09-28 16:19:10 -0700190
Colin Cross6f080df2016-11-04 15:32:58 -0700191 if len(g.properties.Tools) > 0 {
Colin Cross35143d02017-11-16 00:11:20 -0800192 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross08f15ab2018-10-04 23:29:14 -0700193 switch tag := ctx.OtherModuleDependencyTag(module).(type) {
194 case hostToolDependencyTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700195 tool := ctx.OtherModuleName(module)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700196 var path android.OptionalPath
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700197
Colin Crossfe17f6f2019-03-28 19:30:56 -0700198 if t, ok := module.(android.HostToolProvider); ok {
Colin Cross35143d02017-11-16 00:11:20 -0800199 if !t.(android.Module).Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800200 if ctx.Config().AllowMissingDependencies() {
Colin Cross35143d02017-11-16 00:11:20 -0800201 ctx.AddMissingDependencies([]string{tool})
202 } else {
203 ctx.ModuleErrorf("depends on disabled module %q", tool)
204 }
205 break
206 }
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700207 path = t.HostToolPath()
208 } else if t, ok := module.(bootstrap.GoBinaryTool); ok {
209 if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
210 path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
Colin Cross6f080df2016-11-04 15:32:58 -0700211 } else {
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700212 ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
213 break
Colin Cross6f080df2016-11-04 15:32:58 -0700214 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700215 } else {
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700216 ctx.ModuleErrorf("%q is not a host tool provider", tool)
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700217 break
218 }
219
220 if path.Valid() {
221 g.deps = append(g.deps, path.Path())
Colin Cross08f15ab2018-10-04 23:29:14 -0700222 addLocationLabel(tag.label, []string{path.Path().String()})
Dan Willemsen8eded0a2017-09-13 16:07:44 -0700223 } else {
224 ctx.ModuleErrorf("host tool %q missing output file", tool)
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700225 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700226 }
Dan Willemsenf7f3d692016-04-20 14:54:32 -0700227 })
228 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700229
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700230 if ctx.Failed() {
231 return
232 }
233
Colin Cross08f15ab2018-10-04 23:29:14 -0700234 for _, toolFile := range g.properties.Tool_files {
Colin Cross8a497952019-03-05 22:25:09 -0800235 paths := android.PathsForModuleSrc(ctx, []string{toolFile})
Colin Cross08f15ab2018-10-04 23:29:14 -0700236 g.deps = append(g.deps, paths...)
237 addLocationLabel(toolFile, paths.Strings())
238 }
239
240 var srcFiles android.Paths
241 for _, in := range g.properties.Srcs {
Colin Cross8a497952019-03-05 22:25:09 -0800242 paths := android.PathsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs)
Colin Cross08f15ab2018-10-04 23:29:14 -0700243 srcFiles = append(srcFiles, paths...)
244 addLocationLabel(in, paths.Strings())
245 }
246
247 task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
248
249 for _, out := range task.out {
250 addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
Colin Cross6f080df2016-11-04 15:32:58 -0700251 }
252
Jeff Gaston02a684b2017-10-27 14:59:27 -0700253 referencedDepfile := false
254
Jeff Gaston437d23c2017-11-08 12:38:00 -0800255 rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) {
Colin Cross85a2e892018-07-09 09:45:06 -0700256 // report the error directly without returning an error to android.Expand to catch multiple errors in a
257 // single run
258 reportError := func(fmt string, args ...interface{}) (string, error) {
259 ctx.PropertyErrorf("cmd", fmt, args...)
260 return "SOONG_ERROR", nil
261 }
262
Colin Cross6f080df2016-11-04 15:32:58 -0700263 switch name {
264 case "location":
Colin Cross08f15ab2018-10-04 23:29:14 -0700265 if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
Colin Cross85a2e892018-07-09 09:45:06 -0700266 return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
Colin Cross6f080df2016-11-04 15:32:58 -0700267 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700268 paths := locationLabels[firstLabel]
269 if len(paths) == 0 {
270 return reportError("default label %q has no files", firstLabel)
271 } else if len(paths) > 1 {
272 return reportError("default label %q has multiple files, use $(locations %s) to reference it",
273 firstLabel, firstLabel)
274 }
275 return locationLabels[firstLabel][0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700276 case "in":
277 return "${in}", nil
278 case "out":
Jeff Gastonefc1b412017-03-29 17:29:06 -0700279 return "__SBOX_OUT_FILES__", nil
Colin Cross33bfb0a2016-11-21 17:23:08 -0800280 case "depfile":
Jeff Gaston02a684b2017-10-27 14:59:27 -0700281 referencedDepfile = true
Nan Zhangea568a42017-11-08 21:20:04 -0800282 if !Bool(g.properties.Depfile) {
Colin Cross85a2e892018-07-09 09:45:06 -0700283 return reportError("$(depfile) used without depfile property")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800284 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700285 return "__SBOX_DEPFILE__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700286 case "genDir":
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800287 return "__SBOX_OUT_DIR__", nil
Colin Cross6f080df2016-11-04 15:32:58 -0700288 default:
289 if strings.HasPrefix(name, "location ") {
290 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Colin Cross08f15ab2018-10-04 23:29:14 -0700291 if paths, ok := locationLabels[label]; ok {
292 if len(paths) == 0 {
293 return reportError("label %q has no files", label)
294 } else if len(paths) > 1 {
295 return reportError("label %q has multiple files, use $(locations %s) to reference it",
296 label, label)
297 }
298 return paths[0], nil
Colin Cross6f080df2016-11-04 15:32:58 -0700299 } else {
Colin Cross85a2e892018-07-09 09:45:06 -0700300 return reportError("unknown location label %q", label)
Colin Cross6f080df2016-11-04 15:32:58 -0700301 }
Colin Cross08f15ab2018-10-04 23:29:14 -0700302 } else if strings.HasPrefix(name, "locations ") {
303 label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
304 if paths, ok := locationLabels[label]; ok {
305 if len(paths) == 0 {
306 return reportError("label %q has no files", label)
307 }
308 return strings.Join(paths, " "), nil
309 } else {
310 return reportError("unknown locations label %q", label)
311 }
312 } else {
313 return reportError("unknown variable '$(%s)'", name)
Colin Cross6f080df2016-11-04 15:32:58 -0700314 }
Colin Cross6f080df2016-11-04 15:32:58 -0700315 }
316 })
317
318 if err != nil {
319 ctx.PropertyErrorf("cmd", "%s", err.Error())
Colin Cross54c5dd52017-04-19 14:23:26 -0700320 return
Colin Cross6f080df2016-11-04 15:32:58 -0700321 }
322
Colin Cross85a2e892018-07-09 09:45:06 -0700323 if Bool(g.properties.Depfile) && !referencedDepfile {
324 ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
325 }
326
Jeff Gastonefc1b412017-03-29 17:29:06 -0700327 // tell the sbox command which directory to use as its sandbox root
Jeff Gaston193f2fb2017-06-12 15:00:12 -0700328 buildDir := android.PathForOutput(ctx).String()
329 sandboxPath := shared.TempDirForOutDir(buildDir)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700330
331 // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
332 // to be replaced later by ninja_strings.go
Jeff Gaston02a684b2017-10-27 14:59:27 -0700333 depfilePlaceholder := ""
Nan Zhangea568a42017-11-08 21:20:04 -0800334 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700335 depfilePlaceholder = "$depfileArgs"
336 }
Jeff Gaston5acec2b2017-11-06 14:15:16 -0800337
Jeff Gaston437d23c2017-11-08 12:38:00 -0800338 genDir := android.PathForModuleGen(ctx)
Jeff Gaston31603062017-12-08 12:45:35 -0800339 // Escape the command for the shell
340 rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
Colin Cross2a076922018-10-04 23:28:25 -0700341 g.rawCommand = rawCommand
Jeff Gaston31603062017-12-08 12:45:35 -0800342 sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
343 sandboxPath, genDir, rawCommand, depfilePlaceholder)
Jeff Gastonefc1b412017-03-29 17:29:06 -0700344
Colin Cross33bfb0a2016-11-21 17:23:08 -0800345 ruleParams := blueprint.RuleParams{
Jeff Gastonefc1b412017-03-29 17:29:06 -0700346 Command: sandboxCommand,
347 CommandDeps: []string{"$sboxCmd"},
Colin Cross33bfb0a2016-11-21 17:23:08 -0800348 }
Colin Cross15e86d92017-10-20 15:07:08 -0700349 args := []string{"allouts"}
Nan Zhangea568a42017-11-08 21:20:04 -0800350 if Bool(g.properties.Depfile) {
Colin Cross33bfb0a2016-11-21 17:23:08 -0800351 ruleParams.Deps = blueprint.DepsGCC
Jeff Gaston02a684b2017-10-27 14:59:27 -0700352 args = append(args, "depfileArgs")
Colin Cross33bfb0a2016-11-21 17:23:08 -0800353 }
354 g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
Colin Cross6f080df2016-11-04 15:32:58 -0700355
Jeff Gaston437d23c2017-11-08 12:38:00 -0800356 g.generateSourceFile(ctx, task)
357
Colin Crossd350ecd2015-04-28 13:25:36 -0700358}
359
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700360func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
Colin Cross67a5c132017-05-09 13:45:28 -0700361 desc := "generate"
Colin Cross15e86d92017-10-20 15:07:08 -0700362 if len(task.out) == 0 {
363 ctx.ModuleErrorf("must have at least one output file")
364 return
365 }
Colin Cross67a5c132017-05-09 13:45:28 -0700366 if len(task.out) == 1 {
367 desc += " " + task.out[0].Base()
368 }
369
Jeff Gaston02a684b2017-10-27 14:59:27 -0700370 var depFile android.ModuleGenPath
Nan Zhangea568a42017-11-08 21:20:04 -0800371 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700372 depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
373 }
374
Colin Crossae887032017-10-23 17:16:14 -0700375 params := android.BuildParams{
Colin Cross15e86d92017-10-20 15:07:08 -0700376 Rule: g.rule,
377 Description: "generate",
378 Output: task.out[0],
379 ImplicitOutputs: task.out[1:],
380 Inputs: task.in,
381 Implicits: g.deps,
382 Args: map[string]string{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800383 "allouts": strings.Join(task.sandboxOuts, " "),
Colin Cross15e86d92017-10-20 15:07:08 -0700384 },
Colin Cross33bfb0a2016-11-21 17:23:08 -0800385 }
Nan Zhangea568a42017-11-08 21:20:04 -0800386 if Bool(g.properties.Depfile) {
Jeff Gaston02a684b2017-10-27 14:59:27 -0700387 params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
388 params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
Colin Cross33bfb0a2016-11-21 17:23:08 -0800389 }
Jeff Gaston02a684b2017-10-27 14:59:27 -0700390
Colin Crossae887032017-10-23 17:16:14 -0700391 ctx.Build(pctx, params)
Colin Crossd350ecd2015-04-28 13:25:36 -0700392
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700393 for _, outputFile := range task.out {
394 g.outputFiles = append(g.outputFiles, outputFile)
395 }
Dan Willemsen9da9d492018-02-21 18:28:18 -0800396 g.outputDeps = append(g.outputDeps, task.out[0])
Colin Crossd350ecd2015-04-28 13:25:36 -0700397}
398
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700399// Collect information for opening IDE project files in java/jdeps.go.
400func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
401 dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
402 for _, src := range g.properties.Srcs {
403 if strings.HasPrefix(src, ":") {
404 src = strings.Trim(src, ":")
405 dpInfo.Deps = append(dpInfo.Deps, src)
406 }
407 }
408}
409
Colin Crossa4ad2b02019-03-18 22:15:32 -0700410func (g *Module) AndroidMk() android.AndroidMkData {
411 return android.AndroidMkData{
412 Include: "$(BUILD_PHONY_PACKAGE)",
413 Class: "FAKE",
414 OutputFile: android.OptionalPathForPath(g.outputFiles[0]),
415 SubName: g.subName,
416 Extra: []android.AndroidMkExtraFunc{
417 func(w io.Writer, outputFile android.Path) {
418 fmt.Fprintln(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=", strings.Join(g.outputFiles.Strings(), " "))
419 },
420 },
421 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
422 android.WriteAndroidMkData(w, data)
423 if data.SubName != "" {
424 fmt.Fprintln(w, ".PHONY:", name)
425 fmt.Fprintln(w, name, ":", name+g.subName)
426 }
427 },
428 }
429}
430
Jeff Gaston437d23c2017-11-08 12:38:00 -0800431func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700432 module := &Module{
Jeff Gaston437d23c2017-11-08 12:38:00 -0800433 taskGenerator: taskGenerator,
Colin Crossd350ecd2015-04-28 13:25:36 -0700434 }
435
Colin Cross36242852017-06-23 15:06:31 -0700436 module.AddProperties(props...)
437 module.AddProperties(&module.properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700438
Colin Cross36242852017-06-23 15:06:31 -0700439 return module
Colin Crossd350ecd2015-04-28 13:25:36 -0700440}
441
Colin Crossbaccf5b2018-02-21 14:07:48 -0800442// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
443func pathToSandboxOut(path android.Path, genDir android.Path) string {
444 relOut, err := filepath.Rel(genDir.String(), path.String())
445 if err != nil {
446 panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
447 }
448 return filepath.Join("__SBOX_OUT_DIR__", relOut)
449
450}
451
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700452func NewGenSrcs() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700453 properties := &genSrcsProperties{}
454
Jeff Gaston437d23c2017-11-08 12:38:00 -0800455 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
456 commands := []string{}
457 outFiles := android.WritablePaths{}
Colin Crossbaccf5b2018-02-21 14:07:48 -0800458 genDir := android.PathForModuleGen(ctx)
459 sandboxOuts := []string{}
Colin Crossd350ecd2015-04-28 13:25:36 -0700460 for _, in := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800461 outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800462 outFiles = append(outFiles, outFile)
463
Colin Crossbaccf5b2018-02-21 14:07:48 -0800464 sandboxOutfile := pathToSandboxOut(outFile, genDir)
465 sandboxOuts = append(sandboxOuts, sandboxOutfile)
466
Jeff Gaston437d23c2017-11-08 12:38:00 -0800467 command, err := android.Expand(rawCommand, func(name string) (string, error) {
468 switch name {
469 case "in":
470 return in.String(), nil
471 case "out":
472 return sandboxOutfile, nil
473 default:
474 return "$(" + name + ")", nil
475 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700476 })
Jeff Gaston437d23c2017-11-08 12:38:00 -0800477 if err != nil {
478 ctx.PropertyErrorf("cmd", err.Error())
479 }
480
481 // escape the command in case for example it contains '#', an odd number of '"', etc
Colin Cross0b9f31f2019-02-28 11:00:01 -0800482 command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
Jeff Gaston437d23c2017-11-08 12:38:00 -0800483 commands = append(commands, command)
Colin Crossd350ecd2015-04-28 13:25:36 -0700484 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800485 fullCommand := strings.Join(commands, " && ")
486
487 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800488 in: srcFiles,
489 out: outFiles,
490 sandboxOuts: sandboxOuts,
491 cmd: fullCommand,
Jeff Gaston437d23c2017-11-08 12:38:00 -0800492 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700493 }
494
Jeff Gaston437d23c2017-11-08 12:38:00 -0800495 return generatorFactory(taskGenerator, properties)
Colin Crossd350ecd2015-04-28 13:25:36 -0700496}
497
Colin Cross54190b32017-10-09 15:34:10 -0700498func GenSrcsFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700499 m := NewGenSrcs()
500 android.InitAndroidModule(m)
501 return m
502}
503
Colin Crossd350ecd2015-04-28 13:25:36 -0700504type genSrcsProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700505 // extension that will be substituted for each output file
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800506 Output_extension *string
Colin Cross5049f022015-03-18 13:28:46 -0700507}
508
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700509func NewGenRule() *Module {
Colin Crossd350ecd2015-04-28 13:25:36 -0700510 properties := &genRuleProperties{}
Colin Cross5049f022015-03-18 13:28:46 -0700511
Jeff Gaston437d23c2017-11-08 12:38:00 -0800512 taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700513 outs := make(android.WritablePaths, len(properties.Out))
Colin Crossbaccf5b2018-02-21 14:07:48 -0800514 sandboxOuts := make([]string, len(properties.Out))
515 genDir := android.PathForModuleGen(ctx)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700516 for i, out := range properties.Out {
517 outs[i] = android.PathForModuleGen(ctx, out)
Colin Crossbaccf5b2018-02-21 14:07:48 -0800518 sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700519 }
Jeff Gaston437d23c2017-11-08 12:38:00 -0800520 return generateTask{
Colin Crossbaccf5b2018-02-21 14:07:48 -0800521 in: srcFiles,
522 out: outs,
523 sandboxOuts: sandboxOuts,
524 cmd: rawCommand,
Colin Crossd350ecd2015-04-28 13:25:36 -0700525 }
Colin Cross5049f022015-03-18 13:28:46 -0700526 }
Colin Crossd350ecd2015-04-28 13:25:36 -0700527
Jeff Gaston437d23c2017-11-08 12:38:00 -0800528 return generatorFactory(taskGenerator, properties)
Colin Cross5049f022015-03-18 13:28:46 -0700529}
530
Colin Cross54190b32017-10-09 15:34:10 -0700531func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700532 m := NewGenRule()
533 android.InitAndroidModule(m)
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800534 android.InitDefaultableModule(m)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700535 return m
536}
537
Colin Crossd350ecd2015-04-28 13:25:36 -0700538type genRuleProperties struct {
Dan Willemsen9c8681f2016-09-28 16:21:00 -0700539 // names of the output files that will be generated
Colin Crossef354482018-10-23 11:27:50 -0700540 Out []string `android:"arch_variant"`
Colin Cross5049f022015-03-18 13:28:46 -0700541}
Nan Zhangea568a42017-11-08 21:20:04 -0800542
543var Bool = proptools.Bool
544var String = proptools.String
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800545
546//
547// Defaults
548//
549type Defaults struct {
550 android.ModuleBase
551 android.DefaultsModuleBase
552}
553
554func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
555}
556
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800557func defaultsFactory() android.Module {
558 return DefaultsFactory()
559}
560
561func DefaultsFactory(props ...interface{}) android.Module {
562 module := &Defaults{}
563
564 module.AddProperties(props...)
565 module.AddProperties(
566 &generatorProperties{},
567 &genRuleProperties{},
568 )
569
570 android.InitDefaultsModule(module)
571
572 return module
573}